@@ -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}
0 commit comments