Skip to content

Commit 2f047e1

Browse files
Query: Adds ALL Scalar Expression (#3509)
* Add SqlAllScalarExpression to v3 DOM * updated generated parser files * Parsing for ALL * Added tests for ALL and baselines * Added more tests * added new test, cleanup * cleaning & fix typos * fixed typo * Added new baseline test file names to csproj file * renamed AggregateAll to AggregateSubquery to accomodate FIRST and LAST later * Added keywords for 'left' and 'right' and respective function calls * fixed bug from last commit * cleaning * replace tabs with spaces * cleaning
1 parent 8e82a1a commit 2f047e1

29 files changed

+2218
-1407
lines changed

Microsoft.Azure.Cosmos/src/Query/Core/Parser/CstToAstVisitor.cs

+46-13
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ public override SqlObject VisitSelect_item([NotNull] sqlParser.Select_itemContex
183183

184184
SqlScalarExpression sqlScalarExpression = (SqlScalarExpression)this.Visit(context.scalar_expression());
185185
SqlIdentifier alias;
186-
if (context.IDENTIFIER() != null)
186+
if (context.identifier() != null)
187187
{
188-
alias = SqlIdentifier.Create(context.IDENTIFIER().GetText());
188+
alias = SqlIdentifier.Create(context.identifier().GetText());
189189
}
190190
else
191191
{
@@ -233,9 +233,9 @@ public override SqlObject VisitAliasedCollectionExpression([NotNull] sqlParser.A
233233

234234
SqlCollection sqlCollection = (SqlCollection)this.Visit(context.collection());
235235
SqlIdentifier alias;
236-
if (context.IDENTIFIER() != null)
236+
if (context.identifier() != null)
237237
{
238-
alias = SqlIdentifier.Create(context.IDENTIFIER().GetText());
238+
alias = SqlIdentifier.Create(context.identifier().GetText());
239239
}
240240
else
241241
{
@@ -250,7 +250,7 @@ public override SqlObject VisitArrayIteratorCollectionExpression([NotNull] sqlPa
250250
Contract.Requires(context != null);
251251

252252
SqlCollection sqlCollection = (SqlCollection)this.Visit(context.collection());
253-
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
253+
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());
254254

255255
return SqlArrayIteratorCollectionExpression.Create(identifier, sqlCollection);
256256
}
@@ -269,7 +269,7 @@ public override SqlObject VisitInputPathCollection([NotNull] sqlParser.InputPath
269269
{
270270
Contract.Requires(context != null);
271271

272-
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
272+
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());
273273
SqlPathExpression pathExpression;
274274
if (context.path_expression() != null)
275275
{
@@ -302,7 +302,7 @@ public override SqlObject VisitIdentifierPathExpression([NotNull] sqlParser.Iden
302302
Contract.Requires(context != null);
303303

304304
SqlPathExpression pathExpression = (SqlPathExpression)this.Visit(context.path_expression());
305-
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
305+
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());
306306

307307
return SqlIdentifierPathExpression.Create(parentPath: pathExpression, value: identifier);
308308
}
@@ -458,6 +458,15 @@ public override SqlObject VisitLimit_count([NotNull] sqlParser.Limit_countContex
458458
#endregion
459459
#region ScalarExpressions
460460

461+
public override SqlObject VisitAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context)
462+
{
463+
Contract.Requires(context != null);
464+
// K_ALL '(' sql_query ')'
465+
Contract.Requires(context.ChildCount == 4);
466+
467+
SqlQuery subquery = (SqlQuery)this.Visit(context.children[2]);
468+
return SqlAllScalarExpression.Create(subquery);
469+
}
461470
public override SqlObject VisitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context)
462471
{
463472
Contract.Requires(context != null);
@@ -562,10 +571,34 @@ public override SqlObject VisitExistsScalarExpression([NotNull] sqlParser.Exists
562571
public override SqlObject VisitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context)
563572
{
564573
Contract.Requires(context != null);
565-
// (K_UDF '.')? IDENTIFIER '(' scalar_expression_list? ')'
574+
// function_call_scalar_expression
575+
576+
return this.Visit(context.function_call_scalar_expression());
577+
}
578+
579+
public override SqlObject VisitFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context)
580+
{
581+
Contract.Requires(context != null);
582+
// : (K_UDF '.')? identifier '(' scalar_expression_list ? ')'
583+
// | K_LEFT '(' scalar_expression_list ? ')'
584+
// | K_RIGHT '(' scalar_expression_list ? ')'
566585

567586
bool udf = context.K_UDF() != null;
568-
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
587+
SqlIdentifier identifier;
588+
589+
if (context.identifier() != null)
590+
{
591+
identifier = SqlIdentifier.Create(context.identifier().GetText());
592+
}
593+
else if (context.K_LEFT() != null)
594+
{
595+
identifier = SqlIdentifier.Create(context.K_LEFT().GetText());
596+
}
597+
else
598+
{
599+
identifier = SqlIdentifier.Create(context.K_RIGHT().GetText());
600+
}
601+
569602
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>();
570603
if (context.scalar_expression_list() != null)
571604
{
@@ -719,20 +752,20 @@ public override SqlObject VisitParameterRefScalarExpression([NotNull] sqlParser.
719752
public override SqlObject VisitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context)
720753
{
721754
Contract.Requires(context != null);
722-
// IDENTIFIER
755+
// identifier
723756

724757
return SqlPropertyRefScalarExpression.Create(
725758
member: null,
726-
SqlIdentifier.Create(context.IDENTIFIER().GetText()));
759+
SqlIdentifier.Create(context.identifier().GetText()));
727760
}
728761

729762
public override SqlObject VisitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context)
730763
{
731764
Contract.Requires(context != null);
732-
// primary_expression '.' IDENTIFIER
765+
// primary_expression '.' identifier
733766

734767
SqlScalarExpression memberExpression = (SqlScalarExpression)this.Visit(context.primary_expression());
735-
SqlIdentifier indentifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
768+
SqlIdentifier indentifier = SqlIdentifier.Create(context.identifier().GetText());
736769

737770
return SqlPropertyRefScalarExpression.Create(memberExpression, indentifier);
738771
}

Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlListener.cs

+74-42
Original file line numberDiff line numberDiff line change
@@ -526,101 +526,125 @@ internal interface IsqlListener : IParseTreeListener {
526526
/// <param name="context">The parse tree.</param>
527527
void ExitUnary_operator([NotNull] sqlParser.Unary_operatorContext context);
528528
/// <summary>
529-
/// Enter a parse tree produced by the <c>SubqueryScalarExpression</c>
529+
/// Enter a parse tree produced by the <c>AllScalarExpression</c>
530530
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
531531
/// </summary>
532532
/// <param name="context">The parse tree.</param>
533-
void EnterSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
533+
void EnterAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context);
534534
/// <summary>
535-
/// Exit a parse tree produced by the <c>SubqueryScalarExpression</c>
535+
/// Exit a parse tree produced by the <c>AllScalarExpression</c>
536536
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
537537
/// </summary>
538538
/// <param name="context">The parse tree.</param>
539-
void ExitSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
539+
void ExitAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context);
540540
/// <summary>
541-
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
541+
/// Enter a parse tree produced by the <c>LiteralScalarExpression</c>
542542
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
543543
/// </summary>
544544
/// <param name="context">The parse tree.</param>
545-
void EnterPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
545+
void EnterLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
546546
/// <summary>
547-
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
547+
/// Exit a parse tree produced by the <c>LiteralScalarExpression</c>
548548
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
549549
/// </summary>
550550
/// <param name="context">The parse tree.</param>
551-
void ExitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
551+
void ExitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
552552
/// <summary>
553-
/// Enter a parse tree produced by the <c>FunctionCallScalarExpression</c>
553+
/// Enter a parse tree produced by the <c>ObjectCreateScalarExpression</c>
554554
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
555555
/// </summary>
556556
/// <param name="context">The parse tree.</param>
557-
void EnterFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
557+
void EnterObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
558558
/// <summary>
559-
/// Exit a parse tree produced by the <c>FunctionCallScalarExpression</c>
559+
/// Exit a parse tree produced by the <c>ObjectCreateScalarExpression</c>
560560
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
561561
/// </summary>
562562
/// <param name="context">The parse tree.</param>
563-
void ExitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
563+
void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
564564
/// <summary>
565-
/// Enter a parse tree produced by the <c>LiteralScalarExpression</c>
565+
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
566566
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
567567
/// </summary>
568568
/// <param name="context">The parse tree.</param>
569-
void EnterLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
569+
void EnterArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
570570
/// <summary>
571-
/// Exit a parse tree produced by the <c>LiteralScalarExpression</c>
571+
/// Exit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
572572
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
573573
/// </summary>
574574
/// <param name="context">The parse tree.</param>
575-
void ExitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
575+
void ExitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
576576
/// <summary>
577-
/// Enter a parse tree produced by the <c>ObjectCreateScalarExpression</c>
577+
/// Enter a parse tree produced by the <c>MemberIndexerScalarExpression</c>
578578
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
579579
/// </summary>
580580
/// <param name="context">The parse tree.</param>
581-
void EnterObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
581+
void EnterMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
582582
/// <summary>
583-
/// Exit a parse tree produced by the <c>ObjectCreateScalarExpression</c>
583+
/// Exit a parse tree produced by the <c>MemberIndexerScalarExpression</c>
584584
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
585585
/// </summary>
586586
/// <param name="context">The parse tree.</param>
587-
void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
587+
void ExitMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
588588
/// <summary>
589-
/// Enter a parse tree produced by the <c>ParenthesizedScalarExperession</c>
589+
/// Enter a parse tree produced by the <c>SubqueryScalarExpression</c>
590590
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
591591
/// </summary>
592592
/// <param name="context">The parse tree.</param>
593-
void EnterParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
593+
void EnterSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
594594
/// <summary>
595-
/// Exit a parse tree produced by the <c>ParenthesizedScalarExperession</c>
595+
/// Exit a parse tree produced by the <c>SubqueryScalarExpression</c>
596596
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
597597
/// </summary>
598598
/// <param name="context">The parse tree.</param>
599-
void ExitParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
599+
void ExitSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
600600
/// <summary>
601-
/// Enter a parse tree produced by the <c>ParameterRefScalarExpression</c>
601+
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
602602
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
603603
/// </summary>
604604
/// <param name="context">The parse tree.</param>
605-
void EnterParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
605+
void EnterPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
606606
/// <summary>
607-
/// Exit a parse tree produced by the <c>ParameterRefScalarExpression</c>
607+
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
608608
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
609609
/// </summary>
610610
/// <param name="context">The parse tree.</param>
611-
void ExitParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
611+
void ExitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
612612
/// <summary>
613-
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
613+
/// Enter a parse tree produced by the <c>FunctionCallScalarExpression</c>
614614
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
615615
/// </summary>
616616
/// <param name="context">The parse tree.</param>
617-
void EnterArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
617+
void EnterFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
618618
/// <summary>
619-
/// Exit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
619+
/// Exit a parse tree produced by the <c>FunctionCallScalarExpression</c>
620620
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
621621
/// </summary>
622622
/// <param name="context">The parse tree.</param>
623-
void ExitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
623+
void ExitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
624+
/// <summary>
625+
/// Enter a parse tree produced by the <c>ParenthesizedScalarExperession</c>
626+
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
627+
/// </summary>
628+
/// <param name="context">The parse tree.</param>
629+
void EnterParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
630+
/// <summary>
631+
/// Exit a parse tree produced by the <c>ParenthesizedScalarExperession</c>
632+
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
633+
/// </summary>
634+
/// <param name="context">The parse tree.</param>
635+
void ExitParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
636+
/// <summary>
637+
/// Enter a parse tree produced by the <c>ParameterRefScalarExpression</c>
638+
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
639+
/// </summary>
640+
/// <param name="context">The parse tree.</param>
641+
void EnterParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
642+
/// <summary>
643+
/// Exit a parse tree produced by the <c>ParameterRefScalarExpression</c>
644+
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
645+
/// </summary>
646+
/// <param name="context">The parse tree.</param>
647+
void ExitParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
624648
/// <summary>
625649
/// Enter a parse tree produced by the <c>ExistsScalarExpression</c>
626650
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
@@ -646,29 +670,27 @@ internal interface IsqlListener : IParseTreeListener {
646670
/// <param name="context">The parse tree.</param>
647671
void ExitArrayScalarExpression([NotNull] sqlParser.ArrayScalarExpressionContext context);
648672
/// <summary>
649-
/// Enter a parse tree produced by the <c>MemberIndexerScalarExpression</c>
673+
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
650674
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
651675
/// </summary>
652676
/// <param name="context">The parse tree.</param>
653-
void EnterMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
677+
void EnterPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
654678
/// <summary>
655-
/// Exit a parse tree produced by the <c>MemberIndexerScalarExpression</c>
679+
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
656680
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
657681
/// </summary>
658682
/// <param name="context">The parse tree.</param>
659-
void ExitMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
683+
void ExitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
660684
/// <summary>
661-
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
662-
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
685+
/// Enter a parse tree produced by <see cref="sqlParser.function_call_scalar_expression"/>.
663686
/// </summary>
664687
/// <param name="context">The parse tree.</param>
665-
void EnterPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
688+
void EnterFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context);
666689
/// <summary>
667-
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
668-
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
690+
/// Exit a parse tree produced by <see cref="sqlParser.function_call_scalar_expression"/>.
669691
/// </summary>
670692
/// <param name="context">The parse tree.</param>
671-
void ExitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
693+
void ExitFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context);
672694
/// <summary>
673695
/// Enter a parse tree produced by <see cref="sqlParser.scalar_expression_list"/>.
674696
/// </summary>
@@ -700,6 +722,16 @@ internal interface IsqlListener : IParseTreeListener {
700722
/// <param name="context">The parse tree.</param>
701723
void ExitObject_property([NotNull] sqlParser.Object_propertyContext context);
702724
/// <summary>
725+
/// Enter a parse tree produced by <see cref="sqlParser.identifier"/>.
726+
/// </summary>
727+
/// <param name="context">The parse tree.</param>
728+
void EnterIdentifier([NotNull] sqlParser.IdentifierContext context);
729+
/// <summary>
730+
/// Exit a parse tree produced by <see cref="sqlParser.identifier"/>.
731+
/// </summary>
732+
/// <param name="context">The parse tree.</param>
733+
void ExitIdentifier([NotNull] sqlParser.IdentifierContext context);
734+
/// <summary>
703735
/// Enter a parse tree produced by <see cref="sqlParser.literal"/>.
704736
/// </summary>
705737
/// <param name="context">The parse tree.</param>

0 commit comments

Comments
 (0)