Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Health.Fhir.SqlServer.Features.Schema;
using Microsoft.Health.Fhir.SqlServer.Features.Search;
using Microsoft.Health.Fhir.SqlServer.Features.Search.Expressions;
using Microsoft.Health.Fhir.SqlServer.Features.Search.Expressions.Visitors;
using Microsoft.Health.Fhir.SqlServer.Features.Search.Expressions.Visitors.QueryGenerators;
using Microsoft.Health.Fhir.SqlServer.Features.Storage;
using Microsoft.Health.Fhir.Tests.Common;
Expand Down Expand Up @@ -136,4 +137,22 @@ public void GivenASearchTypeForHistorySoftDeleted_WhenSqlGenerated_ThenSqlFilter
Assert.Contains("IsHistory = 1", _strBuilder.ToString());
Assert.Contains("IsDeleted = 1", _strBuilder.ToString());
}

[Fact]
public void GivenASearchForReferenceSearchParam_WhenSqlGenerated_ThenIndexHintUsed()
{
var searchParamInfo = new SearchParameterInfo("subject", "subject");
Expression predicate = Expression.And([new SearchParameterExpression(searchParamInfo, new StringExpression(StringOperator.Equals, FieldName.ReferenceResourceId, null, "Patient", false))]);
var generator = new ReferenceQueryGenerator();
SqlRootExpression sqlExpression = new([new(generator, predicate, SearchParamTableExpressionKind.Normal)], new List<SearchParameterExpressionBase>());
SearchOptions searchOptions = new()
{
Sort = [],
ResourceVersionTypes = ResourceVersionType.Latest,
};

var output = _queryGenerator.VisitSqlRoot(sqlExpression, searchOptions);

Check warning

Code scanning / CodeQL

Useless assignment to local variable

This assignment to [output](1) is useless, since its value is never read.

Assert.Contains("WITH (INDEX (IXU_ReferenceResourceId_ReferenceResourceTypeId_SearchParamId_BaseUri_ResourceSurrogateId_ResourceTypeId))", _strBuilder.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ private void HandleTableKindNormal(SearchParamTableExpression searchParamTableEx
.Append(VLatest.Resource.ResourceTypeId, null).Append(" AS T1, ")
.Append(VLatest.Resource.ResourceSurrogateId, null).AppendLine(" AS Sid1")
.Append("FROM ").AppendLine(searchParamTableExpression.QueryGenerator.Table);

if (searchParamTableExpression.QueryGenerator.Table == VLatest.ReferenceSearchParam.TableName)
{
// append hint to use the index on ReferenceSearchParam
StringBuilder.AppendLine("WITH (INDEX (IXU_ReferenceResourceId_ReferenceResourceTypeId_SearchParamId_BaseUri_ResourceSurrogateId_ResourceTypeId))");
}
}
else
{
Expand Down