Skip to content

Commit

Permalink
Query: Adds FIRST and LAST Scalar Expressions (#3629)
Browse files Browse the repository at this point in the history
* Add FIRST and LAST objects and update visitors

* add FIRST LAST evaluation and update offline engine visitors

* Add FIRST and LAST to parser

* update another visitor

* Fix typo

* fix typo

* added new tests and baselines

* cleaning

* cleaning

---------

Co-authored-by: neildsh <35383880+neildsh@users.noreply.github.com>
Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 6, 2023
1 parent d6e04a9 commit f262f89
Show file tree
Hide file tree
Showing 32 changed files with 1,633 additions and 741 deletions.
20 changes: 20 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/CstToAstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,16 @@ public override SqlObject VisitExistsScalarExpression([NotNull] sqlParser.Exists
return SqlExistsScalarExpression.Create(subquery);
}

public override SqlObject VisitFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context)
{
Contract.Requires(context != null);
// K_FIRST '(' sql_query ')'
Contract.Requires(context.ChildCount == 4);

SqlQuery subquery = (SqlQuery)this.Visit(context.children[2]);
return SqlFirstScalarExpression.Create(subquery);
}

public override SqlObject VisitFunctionCallScalarExpression([NotNull] sqlParser.FunctionCallScalarExpressionContext context)
{
Contract.Requires(context != null);
Expand Down Expand Up @@ -627,6 +637,16 @@ public override SqlObject VisitIn_scalar_expression([NotNull] sqlParser.In_scala
return SqlInScalarExpression.Create(needle, not, searchList.ToImmutableArray());
}

public override SqlObject VisitLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context)
{
Contract.Requires(context != null);
// K_LAST '(' sql_query ')'
Contract.Requires(context.ChildCount == 4);

SqlQuery subquery = (SqlQuery)this.Visit(context.children[2]);
return SqlLastScalarExpression.Create(subquery);
}

public override SqlObject VisitLike_scalar_expression([NotNull] sqlParser.Like_scalar_expressionContext context)
{
Contract.Requires(context != null);
Expand Down
24 changes: 24 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,18 @@ internal interface IsqlListener : IParseTreeListener {
/// <param name="context">The parse tree.</param>
void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
Expand Down Expand Up @@ -670,6 +682,18 @@ 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>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void EnterLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context);
/// <summary>
/// Exit a parse tree produced by the <c>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
void ExitLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context);
/// <summary>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ internal interface IsqlVisitor<Result> : IParseTreeVisitor<Result> {
/// <return>The visitor result.</return>
Result VisitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context);
/// <summary>
/// Visit a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
Result VisitFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context);
/// <summary>
/// Visit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
Expand Down Expand Up @@ -410,6 +417,13 @@ internal interface IsqlVisitor<Result> : IParseTreeVisitor<Result> {
/// <return>The visitor result.</return>
Result VisitArrayScalarExpression([NotNull] sqlParser.ArrayScalarExpressionContext context);
/// <summary>
/// Visit a parse tree produced by the <c>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
Result VisitLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context);
/// <summary>
/// Visit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/sql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ primary_expression
| K_EXISTS '(' sql_query ')' #ExistsScalarExpression
| K_ARRAY '(' sql_query ')' #ArrayScalarExpression
| K_ALL '(' sql_query ')' #AllScalarExpression
| K_FIRST '(' sql_query ')' #FirstScalarExpression
| K_LAST '(' sql_query ')' #LastScalarExpression
| function_call_scalar_expression #FunctionCallScalarExpression
;

Expand All @@ -191,6 +193,8 @@ object_property : STRING_LITERAL ':' scalar_expression ;
identifier
: LEX_IDENTIFIER
| K_ALL
| K_FIRST
| K_LAST
;
/*--------------------------------------------------------------------------------*/

Expand All @@ -208,11 +212,13 @@ K_DESC : D E S C;
K_DISTINCT : D I S T I N C T;
K_ESCAPE: E S C A P E;
K_EXISTS : E X I S T S;
K_FIRST : F I R S T;
K_FALSE : 'false';
K_FROM : F R O M;
K_GROUP : G R O U P;
K_IN : I N ;
K_JOIN : J O I N;
K_LAST : L A S T;
K_LEFT : L E F T;
K_LIKE : L I K E;
K_LIMIT : L I M I T;
Expand Down
28 changes: 28 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/sqlBaseListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,20 @@ public virtual void EnterObjectCreateScalarExpression([NotNull] sqlParser.Object
/// <param name="context">The parse tree.</param>
public virtual void ExitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context) { }
/// <summary>
/// Exit a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
Expand Down Expand Up @@ -792,6 +806,20 @@ public virtual void EnterArrayScalarExpression([NotNull] sqlParser.ArrayScalarEx
/// <param name="context">The parse tree.</param>
public virtual void ExitArrayScalarExpression([NotNull] sqlParser.ArrayScalarExpressionContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context) { }
/// <summary>
/// Exit a parse tree produced by the <c>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>The default implementation does nothing.</para>
Expand Down
22 changes: 22 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/sqlBaseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,17 @@ internal partial class sqlBaseVisitor<Result> : AbstractParseTreeVisitor<Result>
/// <return>The visitor result.</return>
public virtual Result VisitObjectCreateScalarExpression([NotNull] sqlParser.ObjectCreateScalarExpressionContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>FirstScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
/// on <paramref name="context"/>.
/// </para>
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
public virtual Result VisitFirstScalarExpression([NotNull] sqlParser.FirstScalarExpressionContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>ArrayCreateScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>
Expand Down Expand Up @@ -648,6 +659,17 @@ internal partial class sqlBaseVisitor<Result> : AbstractParseTreeVisitor<Result>
/// <return>The visitor result.</return>
public virtual Result VisitArrayScalarExpression([NotNull] sqlParser.ArrayScalarExpressionContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>LastScalarExpression</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
/// on <paramref name="context"/>.
/// </para>
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
public virtual Result VisitLastScalarExpression([NotNull] sqlParser.LastScalarExpressionContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>PropertyRefScalarExpressionRecursive</c>
/// labeled alternative in <see cref="sqlParser.primary_expression"/>.
/// <para>
Expand Down
Loading

0 comments on commit f262f89

Please sign in to comment.