Skip to content

Commit 16fcf8c

Browse files
committed
Fixed error when using subquery without alias
Fixes #706
1 parent 7cfa02c commit 16fcf8c

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9367,5 +9367,39 @@ select a.name
93679367
var insert = AssertNode<InsertNode>(plans[0]);
93689368
var fetch = AssertNode<FetchXmlScan>(insert.Source);
93699369
}
9370+
9371+
[TestMethod]
9372+
public void GlobalOptionSetsInSubquery()
9373+
{
9374+
// Simplified version of https://github.com/MarkMpn/Sql4Cds/issues/706
9375+
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);
9376+
9377+
var query = @"
9378+
select name,
9379+
(SELECT W.displayname FROM metadata.globaloptionset W WHERE W.metadataid = primarycontactid)
9380+
from account";
9381+
9382+
var plans = planBuilder.Build(query, null, out _);
9383+
9384+
Assert.AreEqual(1, plans.Length);
9385+
9386+
var select = AssertNode<SelectNode>(plans[0]);
9387+
Assert.AreEqual("account.name", select.ColumnSet[0].SourceColumn);
9388+
Assert.AreEqual("W.displayname", select.ColumnSet[1].SourceColumn);
9389+
var hash = AssertNode<HashJoinNode>(select.Source);
9390+
Assert.AreEqual(QualifiedJoinType.RightOuter, hash.JoinType);
9391+
Assert.AreEqual("W.metadataid", hash.LeftAttribute.ToSql());
9392+
var optionset = AssertNode<GlobalOptionSetQueryNode>(hash.LeftSource);
9393+
Assert.AreEqual("W", optionset.OptionSetAlias);
9394+
Assert.AreEqual("account.primarycontactid", hash.RightAttribute.ToSql());
9395+
var fetch = AssertNode<FetchXmlScan>(hash.RightSource);
9396+
AssertFetchXml(fetch, @"
9397+
<fetch>
9398+
<entity name='account'>
9399+
<attribute name='name' />
9400+
<attribute name='primarycontactid' />
9401+
</entity>
9402+
</fetch>");
9403+
}
93709404
}
93719405
}

MarkMpn.Sql4Cds.Engine/ExecutionPlanBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5018,7 +5018,7 @@ private bool UseMergeJoin(IDataExecutionPlanNodeInternal node, IDataExecutionPla
50185018
subAlias = alias.Alias;
50195019
}
50205020

5021-
if (rightAttribute.MultiPartIdentifier.Identifiers.Count == 2)
5021+
if (subAlias != null && rightAttribute.MultiPartIdentifier.Identifiers.Count == 2)
50225022
rightAttribute.MultiPartIdentifier.Identifiers[0].Value = subAlias;
50235023

50245024
// Add the required column with the expected alias (used for scalar subqueries and IN predicates, not for CROSS/OUTER APPLY

0 commit comments

Comments
 (0)