Skip to content

Commit

Permalink
Merge pull request #613 from MarkMpn/in-variables
Browse files Browse the repository at this point in the history
Fixed use of IN with a single variable
  • Loading branch information
MarkMpn authored Dec 10, 2024
2 parents 53287e4 + e9bbba1 commit 30326e5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
57 changes: 57 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8956,6 +8956,63 @@ FROM account AS c
<condition attribute='parentaccountid' operator='not-null' />
</filter>
</entity>
</fetch>");
}

[TestMethod]
public void InVariable()
{
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
DECLARE @AccountId UNIQUEIDENTIFIER = NEWID();
SELECT name FROM account WHERE accountid IN (@AccountId)";

var plans = planBuilder.Build(query, null, out _);

Assert.AreEqual(3, plans.Length);

var select = AssertNode<SelectNode>(plans[2]);
var fetch = AssertNode<FetchXmlScan>(select.Source);
AssertFetchXml(fetch, @"
<fetch xmlns:generator='MarkMpn.SQL4CDS'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='accountid' operator='in'>
<value generator:IsVariable='true'>@AccountId</value>
</condition>
</filter>
</entity>
</fetch>");
}

[TestMethod]
public void InVariableAndLiteral()
{
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
DECLARE @AccountId UNIQUEIDENTIFIER = NEWID();
SELECT name FROM account WHERE accountid IN (@AccountId, '81349C3B-C0CA-46B6-8CF8-A5B82D27EC2F')";

var plans = planBuilder.Build(query, null, out _);

Assert.AreEqual(3, plans.Length);

var select = AssertNode<SelectNode>(plans[2]);
var fetch = AssertNode<FetchXmlScan>(select.Source);
AssertFetchXml(fetch, @"
<fetch xmlns:generator='MarkMpn.SQL4CDS'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='accountid' operator='in'>
<value generator:IsVariable='true'>@AccountId</value>
<value>81349C3B-C0CA-46B6-8CF8-A5B82D27EC2F</value>
</condition>
</filter>
</entity>
</fetch>");
}
}
Expand Down
2 changes: 1 addition & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/BaseDataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ private bool TranslateFetchXMLCriteriaWithVirtualAttributes(NodeCompilationConte
@operator = op,
value = usesItems ? null : value,
Items = usesItems ? values : null,
IsVariable = isVariable
IsVariable = !usesItems && isVariable
};

// Filtering on "solution" entity via an outer join seems to generate an error:
Expand Down

0 comments on commit 30326e5

Please sign in to comment.