Skip to content

Commit

Permalink
Query: Adds ALL Scalar Expression (#3509)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ezrahaleva-msft authored Nov 28, 2022
1 parent 8e82a1a commit 2f047e1
Show file tree
Hide file tree
Showing 29 changed files with 2,218 additions and 1,407 deletions.
59 changes: 46 additions & 13 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/CstToAstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ public override SqlObject VisitSelect_item([NotNull] sqlParser.Select_itemContex

SqlScalarExpression sqlScalarExpression = (SqlScalarExpression)this.Visit(context.scalar_expression());
SqlIdentifier alias;
if (context.IDENTIFIER() != null)
if (context.identifier() != null)
{
alias = SqlIdentifier.Create(context.IDENTIFIER().GetText());
alias = SqlIdentifier.Create(context.identifier().GetText());
}
else
{
Expand Down Expand Up @@ -233,9 +233,9 @@ public override SqlObject VisitAliasedCollectionExpression([NotNull] sqlParser.A

SqlCollection sqlCollection = (SqlCollection)this.Visit(context.collection());
SqlIdentifier alias;
if (context.IDENTIFIER() != null)
if (context.identifier() != null)
{
alias = SqlIdentifier.Create(context.IDENTIFIER().GetText());
alias = SqlIdentifier.Create(context.identifier().GetText());
}
else
{
Expand All @@ -250,7 +250,7 @@ public override SqlObject VisitArrayIteratorCollectionExpression([NotNull] sqlPa
Contract.Requires(context != null);

SqlCollection sqlCollection = (SqlCollection)this.Visit(context.collection());
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());

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

SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());
SqlPathExpression pathExpression;
if (context.path_expression() != null)
{
Expand Down Expand Up @@ -302,7 +302,7 @@ public override SqlObject VisitIdentifierPathExpression([NotNull] sqlParser.Iden
Contract.Requires(context != null);

SqlPathExpression pathExpression = (SqlPathExpression)this.Visit(context.path_expression());
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
SqlIdentifier identifier = SqlIdentifier.Create(context.identifier().GetText());

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

public override SqlObject VisitAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context)
{
Contract.Requires(context != null);
// K_ALL '(' sql_query ')'
Contract.Requires(context.ChildCount == 4);

SqlQuery subquery = (SqlQuery)this.Visit(context.children[2]);
return SqlAllScalarExpression.Create(subquery);
}
public override SqlObject VisitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context)
{
Contract.Requires(context != null);
Expand Down Expand Up @@ -562,10 +571,34 @@ public override SqlObject VisitExistsScalarExpression([NotNull] sqlParser.Exists
public override SqlObject VisitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context)
{
Contract.Requires(context != null);
// (K_UDF '.')? IDENTIFIER '(' scalar_expression_list? ')'
// function_call_scalar_expression

return this.Visit(context.function_call_scalar_expression());
}

public override SqlObject VisitFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context)
{
Contract.Requires(context != null);
// : (K_UDF '.')? identifier '(' scalar_expression_list ? ')'
// | K_LEFT '(' scalar_expression_list ? ')'
// | K_RIGHT '(' scalar_expression_list ? ')'

bool udf = context.K_UDF() != null;
SqlIdentifier identifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
SqlIdentifier identifier;

if (context.identifier() != null)
{
identifier = SqlIdentifier.Create(context.identifier().GetText());
}
else if (context.K_LEFT() != null)
{
identifier = SqlIdentifier.Create(context.K_LEFT().GetText());
}
else
{
identifier = SqlIdentifier.Create(context.K_RIGHT().GetText());
}

List<SqlScalarExpression> arguments = new List<SqlScalarExpression>();
if (context.scalar_expression_list() != null)
{
Expand Down Expand Up @@ -719,20 +752,20 @@ public override SqlObject VisitParameterRefScalarExpression([NotNull] sqlParser.
public override SqlObject VisitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context)
{
Contract.Requires(context != null);
// IDENTIFIER
// identifier

return SqlPropertyRefScalarExpression.Create(
member: null,
SqlIdentifier.Create(context.IDENTIFIER().GetText()));
SqlIdentifier.Create(context.identifier().GetText()));
}

public override SqlObject VisitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context)
{
Contract.Requires(context != null);
// primary_expression '.' IDENTIFIER
// primary_expression '.' identifier

SqlScalarExpression memberExpression = (SqlScalarExpression)this.Visit(context.primary_expression());
SqlIdentifier indentifier = SqlIdentifier.Create(context.IDENTIFIER().GetText());
SqlIdentifier indentifier = SqlIdentifier.Create(context.identifier().GetText());

return SqlPropertyRefScalarExpression.Create(memberExpression, indentifier);
}
Expand Down
116 changes: 74 additions & 42 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,101 +526,125 @@ internal interface IsqlListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitUnary_operator([NotNull] sqlParser.Unary_operatorContext context);
/// <summary>
/// Enter a parse tree produced by the <c>SubqueryScalarExpression</c>
/// Enter a parse tree produced by the <c>AllScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
void EnterAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>SubqueryScalarExpression</c>
/// Exit a parse tree produced by the <c>AllScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
void ExitAllScalarExpression([NotNull] sqlParser.AllScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
/// Enter a parse tree produced by the <c>LiteralScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
void EnterLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
/// Exit a parse tree produced by the <c>LiteralScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
void ExitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>FunctionCallScalarExpression</c>
/// Enter a parse tree produced by the <c>ObjectCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
void EnterObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>FunctionCallScalarExpression</c>
/// Exit a parse tree produced by the <c>ObjectCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>LiteralScalarExpression</c>
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
void EnterArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>LiteralScalarExpression</c>
/// Exit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitLiteralScalarExpression([NotNull] sqlParser.LiteralScalarExpressionContext context);
void ExitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ObjectCreateScalarExpression</c>
/// Enter a parse tree produced by the <c>MemberIndexerScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
void EnterMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ObjectCreateScalarExpression</c>
/// Exit a parse tree produced by the <c>MemberIndexerScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
void ExitMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ParenthesizedScalarExperession</c>
/// Enter a parse tree produced by the <c>SubqueryScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
void EnterSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ParenthesizedScalarExperession</c>
/// Exit a parse tree produced by the <c>SubqueryScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
void ExitSubqueryScalarExpression([NotNull] sqlParser.SubqueryScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ParameterRefScalarExpression</c>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
void EnterPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ParameterRefScalarExpression</c>
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionBase</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
void ExitPropertyRefScalarExpressionBase([NotNull] sqlParser.PropertyRefScalarExpressionBaseContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// Enter a parse tree produced by the <c>FunctionCallScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
void EnterFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// Exit a parse tree produced by the <c>FunctionCallScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitArrayCreateScalarExpression([NotNull] sqlParser.ArrayCreateScalarExpressionContext context);
void ExitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ParenthesizedScalarExperession</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ParenthesizedScalarExperession</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitParenthesizedScalarExperession([NotNull] sqlParser.ParenthesizedScalarExperessionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ParameterRefScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>ParameterRefScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitParameterRefScalarExpression([NotNull] sqlParser.ParameterRefScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ExistsScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
Expand All @@ -646,29 +670,27 @@ internal interface IsqlListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitArrayScalarExpression([NotNull] sqlParser.ArrayScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>MemberIndexerScalarExpression</c>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
void EnterPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
/// <summary>
/// Exit a parse tree produced by the <c>MemberIndexerScalarExpression</c>
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitMemberIndexerScalarExpression([NotNull] sqlParser.MemberIndexerScalarExpressionContext context);
void ExitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
/// <summary>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// Enter a parse tree produced by <see cref="sqlParser.function_call_scalar_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
void EnterFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// Exit a parse tree produced by <see cref="sqlParser.function_call_scalar_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitPropertyRefScalarExpressionRecursive([NotNull] sqlParser.PropertyRefScalarExpressionRecursiveContext context);
void ExitFunction_call_scalar_expression([NotNull] sqlParser.Function_call_scalar_expressionContext context);
/// <summary>
/// Enter a parse tree produced by <see cref="sqlParser.scalar_expression_list"/>.
/// </summary>
Expand Down Expand Up @@ -700,6 +722,16 @@ internal interface IsqlListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitObject_property([NotNull] sqlParser.Object_propertyContext context);
/// <summary>
/// Enter a parse tree produced by <see cref="sqlParser.identifier"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterIdentifier([NotNull] sqlParser.IdentifierContext context);
/// <summary>
/// Exit a parse tree produced by <see cref="sqlParser.identifier"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitIdentifier([NotNull] sqlParser.IdentifierContext context);
/// <summary>
/// Enter a parse tree produced by <see cref="sqlParser.literal"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
Expand Down
Loading

0 comments on commit 2f047e1

Please sign in to comment.