Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/sql-ref-ansi-compliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ Below is a list of all the keywords in Spark SQL.
|REGEXP|non-reserved|non-reserved|not a keyword|
|RENAME|non-reserved|non-reserved|non-reserved|
|REPAIR|non-reserved|non-reserved|non-reserved|
|REPEAT|non-reserved|non-reserved|non-reserved|
|REPEATABLE|non-reserved|non-reserved|non-reserved|
|REPLACE|non-reserved|non-reserved|non-reserved|
|RESET|non-reserved|non-reserved|non-reserved|
Expand Down Expand Up @@ -734,6 +735,7 @@ Below is a list of all the keywords in Spark SQL.
|UNLOCK|non-reserved|non-reserved|non-reserved|
|UNPIVOT|non-reserved|non-reserved|non-reserved|
|UNSET|non-reserved|non-reserved|non-reserved|
|UNTIL|non-reserved|non-reserved|non-reserved|
|UPDATE|non-reserved|non-reserved|reserved|
|USE|non-reserved|non-reserved|non-reserved|
|USER|reserved|non-reserved|reserved|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ REFERENCES: 'REFERENCES';
REFRESH: 'REFRESH';
RENAME: 'RENAME';
REPAIR: 'REPAIR';
REPEAT: 'REPEAT';
REPEATABLE: 'REPEATABLE';
REPLACE: 'REPLACE';
RESET: 'RESET';
Expand Down Expand Up @@ -453,6 +454,7 @@ UNKNOWN: 'UNKNOWN';
UNLOCK: 'UNLOCK';
UNPIVOT: 'UNPIVOT';
UNSET: 'UNSET';
UNTIL: 'UNTIL';
UPDATE: 'UPDATE';
USE: 'USE';
USER: 'USER';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ compoundStatement
| beginEndCompoundBlock
| ifElseStatement
| whileStatement
| repeatStatement
| leaveStatement
| iterateStatement
;
Expand All @@ -85,6 +86,10 @@ ifElseStatement
(ELSE elseBody=compoundBody)? END IF
;

repeatStatement
: beginLabel? REPEAT compoundBody UNTIL booleanExpression END REPEAT endLabel?
;

leaveStatement
: LEAVE multipartIdentifier
;
Expand Down Expand Up @@ -1660,6 +1665,7 @@ ansiNonReserved
| REFRESH
| RENAME
| REPAIR
| REPEAT
| REPEATABLE
| REPLACE
| RESET
Expand Down Expand Up @@ -1735,6 +1741,7 @@ ansiNonReserved
| UNLOCK
| UNPIVOT
| UNSET
| UNTIL
| UPDATE
| USE
| VALUES
Expand Down Expand Up @@ -2023,6 +2030,7 @@ nonReserved
| REFRESH
| RENAME
| REPAIR
| REPEAT
| REPEATABLE
| REPLACE
| RESET
Expand Down Expand Up @@ -2107,6 +2115,7 @@ nonReserved
| UNLOCK
| UNPIVOT
| UNSET
| UNTIL
| UPDATE
| USE
| USER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ class AstBuilder extends DataTypeAstBuilder
WhileStatement(condition, body, Some(labelText))
}

override def visitRepeatStatement(ctx: RepeatStatementContext): RepeatStatement = {
val labelText = generateLabelText(Option(ctx.beginLabel()), Option(ctx.endLabel()))
val boolExpr = ctx.booleanExpression()

val condition = withOrigin(boolExpr) {
SingleStatement(
Project(
Seq(Alias(expression(boolExpr), "condition")()),
OneRowRelation()))}
val body = visitCompoundBody(ctx.compoundBody())

RepeatStatement(condition, body, Some(labelText))
}

private def leaveOrIterateContextHasLabel(
ctx: RuleContext, label: String, isIterate: Boolean): Boolean = {
ctx match {
Expand All @@ -275,6 +289,10 @@ class AstBuilder extends DataTypeAstBuilder
if Option(c.beginLabel()).isDefined &&
c.beginLabel().multipartIdentifier().getText.toLowerCase(Locale.ROOT).equals(label)
=> true
case c: RepeatStatementContext
if Option(c.beginLabel()).isDefined &&
c.beginLabel().multipartIdentifier().getText.toLowerCase(Locale.ROOT).equals(label)
=> true
Comment on lines +292 to +295
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge this with the above case for WhileStatementContext? You have multiple options for this, using | operator or Either type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't be trivial, as there is no common supertype between RepeatStatementContext and WhileStatementContext with the beginLabel() method defined. Maybe we could have a labeledStatement grammar rule, or something similar, to abstract some of this label logic from all statements with labels (ifElse, while, repeat..)

case _ => false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ case class WhileStatement(
body: CompoundBody,
label: Option[String]) extends CompoundPlanStatement

/**
* Logical operator for REPEAT statement.
* @param condition Any expression evaluating to a Boolean.
* Body is executed as long as the condition evaluates to false
* @param body Compound body is a collection of statements that are executed once no matter what,
* and then as long as condition is false.
* @param label An optional label for the loop which is unique amongst all labels for statements
* within which the LOOP statement is contained.
* If an end label is specified it must match the beginning label.
* The label can be used to LEAVE or ITERATE the loop.
*/
case class RepeatStatement(
condition: SingleStatement,
body: CompoundBody,
label: Option[String]) extends CompoundPlanStatement


/**
* Logical operator for LEAVE statement.
* The statement can be used both for compounds or any kind of loops.
Expand Down
Loading