Skip to content

Commit

Permalink
Added id join in include queries
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyGaluzo committed Dec 12, 2024
1 parent e56f599 commit c59a7ce
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,13 @@ BEGIN
WITH (DATA_COMPRESSION = PAGE) ON PartitionScheme_ResourceTypeId (ResourceTypeId)

EXECUTE('
CREATE VIEW dbo.ReferenceSearchParam
AS
SELECT A.ResourceTypeId
,ResourceSurrogateId
,SearchParamId
,BaseUri
,ReferenceResourceTypeId
,ReferenceResourceId = B.ResourceId
,ReferenceResourceIdInt
,IsResourceRef
FROM dbo.ResourceReferenceSearchParams A
LEFT OUTER JOIN dbo.ResourceIdIntMap B ON B.ResourceTypeId = A.ReferenceResourceTypeId AND B.ResourceIdInt = A.ReferenceResourceIdInt
Expand All @@ -553,6 +552,7 @@ SELECT ResourceTypeId
,BaseUri
,NULL
,ReferenceResourceId
,NULL
,IsResourceRef
FROM dbo.StringReferenceSearchParams
UNION ALL
Expand All @@ -563,6 +563,7 @@ SELECT ResourceTypeId
,ReferenceResourceTypeId
,ReferenceResourceId
,NULL
,NULL
FROM dbo.ReferenceSearchParamTbl
')

Expand Down Expand Up @@ -2092,6 +2093,7 @@ SELECT A.ResourceTypeId
,BaseUri
,ReferenceResourceTypeId
,ReferenceResourceId = B.ResourceId
,ReferenceResourceIdInt
,IsResourceRef
FROM dbo.ResourceReferenceSearchParams A
LEFT OUTER JOIN dbo.ResourceIdIntMap B ON B.ResourceTypeId = A.ReferenceResourceTypeId AND B.ResourceIdInt = A.ReferenceResourceIdInt
Expand All @@ -2102,6 +2104,7 @@ SELECT ResourceTypeId
,BaseUri
,NULL
,ReferenceResourceId
,NULL
,IsResourceRef
FROM dbo.StringReferenceSearchParams
GO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ CREATE TABLE dbo.ReferenceSearchParam (
SearchParamId SMALLINT NOT NULL,
BaseUri VARCHAR (128) COLLATE Latin1_General_100_CS_AS NULL,
ReferenceResourceTypeId SMALLINT NULL,
ReferenceResourceId VARCHAR (768) COLLATE Latin1_General_100_CS_AS NOT NULL,
ReferenceResourceIdInt BIGINT NOT NULL,
ReferenceResourceId VARCHAR (768) COLLATE Latin1_General_100_CS_AS NOT NULL
IsResourceRef BIT NOT NULL
);


Expand Down Expand Up @@ -6058,6 +6059,7 @@ SELECT A.ResourceTypeId,
BaseUri,
ReferenceResourceTypeId,
B.ResourceId AS ReferenceResourceId,
ReferenceResourceIdInt,
IsResourceRef
FROM dbo.ResourceReferenceSearchParams AS A
LEFT OUTER JOIN
Expand All @@ -6071,6 +6073,7 @@ SELECT ResourceTypeId,
BaseUri,
NULL,
ReferenceResourceId,
NULL,
IsResourceRef
FROM dbo.StringReferenceSearchParams;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ CREATE TABLE dbo.ReferenceSearchParam
,SearchParamId smallint NOT NULL
,BaseUri varchar(128) COLLATE Latin1_General_100_CS_AS NULL
,ReferenceResourceTypeId smallint NULL
,ReferenceResourceIdInt bigint NOT NULL
,ReferenceResourceId varchar(768) COLLATE Latin1_General_100_CS_AS NOT NULL
,ReferenceResourceIdInt bigint NOT NULL
,IsResourceRef bit NOT NULL
)
GO
DROP TABLE dbo.ReferenceSearchParam
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SELECT A.ResourceTypeId
,BaseUri
,ReferenceResourceTypeId
,ReferenceResourceId = B.ResourceId
,ReferenceResourceIdInt
,IsResourceRef
FROM dbo.ResourceReferenceSearchParams A
LEFT OUTER JOIN dbo.ResourceIdIntMap B ON B.ResourceTypeId = A.ReferenceResourceTypeId AND B.ResourceIdInt = A.ReferenceResourceIdInt
Expand All @@ -16,6 +17,7 @@ SELECT ResourceTypeId
,BaseUri
,NULL
,ReferenceResourceId
,NULL
,IsResourceRef
FROM dbo.StringReferenceSearchParams
GO
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public override object VisitSqlRoot(SqlRootExpression expression, SearchOptions
return null;
}

// TODO: Remove when code starts using TokenSearchParamHighCard table
// TODO: Remove. This is not needed as we use precise statistics.
private void AddOptionClause()
{
// if we have a complex query more than one SearchParemter, one of the parameters is "identifier", and we have an include
Expand Down Expand Up @@ -687,8 +687,16 @@ private void HandleTableKindChain(
.Append(VLatest.Resource.ResourceSurrogateId, chainedExpression.Reversed && searchParamTableExpression.ChainLevel > 1 ? referenceSourceTableAlias : referenceTargetResourceTableAlias).Append(" AS ").AppendLine(chainedExpression.Reversed && searchParamTableExpression.ChainLevel == 1 ? "Sid1 " : "Sid2 ")
.Append("FROM ").Append(VLatest.ReferenceSearchParam).Append(' ').AppendLine(referenceSourceTableAlias)
.Append(_joinShift).Append("JOIN ").Append(VLatest.Resource).Append(' ').Append(referenceTargetResourceTableAlias)
.Append(" ON ").Append(VLatest.ReferenceSearchParam.ReferenceResourceTypeId, referenceSourceTableAlias).Append(" = ").Append(VLatest.Resource.ResourceTypeId, referenceTargetResourceTableAlias)
.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceId, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.Resource.ResourceId, referenceTargetResourceTableAlias);
.Append(" ON ").Append(VLatest.ReferenceSearchParam.ReferenceResourceTypeId, referenceSourceTableAlias).Append(" = ").Append(VLatest.Resource.ResourceTypeId, referenceTargetResourceTableAlias);
if (_schemaInfo.Current >= SchemaVersionConstants.Lake)
{
StringBuilder.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceIdInt, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.CurrentResources.ResourceIdInt, referenceTargetResourceTableAlias)
.Append(" AND ").Append(VLatest.ReferenceSearchParam.IsResourceRef, referenceSourceTableAlias).AppendLine(" = 1");
}
else
{
StringBuilder.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceId, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.Resource.ResourceId, referenceTargetResourceTableAlias);
}

// For reverse chaining, if there is a parameter on the _id search parameter, we need another join to get the resource ID of the reference source (all we have is the surrogate ID at this point)
bool expressionOnTargetHandledBySecondJoin = chainedExpression.ExpressionOnTarget != null && chainedExpression.Reversed && chainedExpression.ExpressionOnTarget.AcceptVisitor(ExpressionContainsParameterVisitor.Instance, SearchParameterNames.Id);
Expand Down Expand Up @@ -784,8 +792,16 @@ private void HandleTableKindInclude(

StringBuilder.Append("FROM ").Append(VLatest.ReferenceSearchParam).Append(' ').AppendLine(referenceSourceTableAlias)
.Append(_joinShift).Append("JOIN ").Append(VLatest.Resource).Append(' ').Append(referenceTargetResourceTableAlias)
.Append(" ON ").Append(VLatest.ReferenceSearchParam.ReferenceResourceTypeId, referenceSourceTableAlias).Append(" = ").Append(VLatest.Resource.ResourceTypeId, referenceTargetResourceTableAlias)
.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceId, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.Resource.ResourceId, referenceTargetResourceTableAlias);
.Append(" ON ").Append(VLatest.ReferenceSearchParam.ReferenceResourceTypeId, referenceSourceTableAlias).Append(" = ").Append(VLatest.Resource.ResourceTypeId, referenceTargetResourceTableAlias);
if (_schemaInfo.Current >= SchemaVersionConstants.Lake)
{
StringBuilder.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceIdInt, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.CurrentResources.ResourceIdInt, referenceTargetResourceTableAlias)
.Append(" AND ").Append(VLatest.ReferenceSearchParam.IsResourceRef, referenceSourceTableAlias).AppendLine(" = 1");
}
else
{
StringBuilder.Append(" AND ").Append(VLatest.ReferenceSearchParam.ReferenceResourceId, referenceSourceTableAlias).Append(" = ").AppendLine(VLatest.Resource.ResourceId, referenceTargetResourceTableAlias);
}

using (var delimited = StringBuilder.BeginDelimitedWhereClause())
{
Expand Down

0 comments on commit c59a7ce

Please sign in to comment.