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
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ class SingleStatementExec(
override def reset(): Unit = isExecuted = false
}

/**
* NO-OP leaf node, which does nothing when returned to the iterator.
* It is emitted by empty BEGIN END blocks.
*/
class NoOpStatementExec extends LeafStatementExec {
override def reset(): Unit = ()
}

/**
* Executable node for CompoundBody.
* @param statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ case class SqlScriptingInterpreter(session: SparkSession) {
.map(varName => DropVariable(varName, ifExists = true))
.map(new SingleStatementExec(_, Origin(), args, isInternal = true))
.reverse
new CompoundBodyExec(
collection.map(st => transformTreeIntoExecutable(st, args)) ++ dropVariables,
label)

val statements =
collection.map(st => transformTreeIntoExecutable(st, args)) ++ dropVariables match {
case Nil => Seq(new NoOpStatementExec)
case s => s
}

new CompoundBodyExec(statements, label)

case IfElseStatement(conditions, conditionalBodies, elseBody) =>
val conditionsExec = conditions.map(condition =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,61 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}
}

test("empty begin end block") {
val sqlScript =
"""
|BEGIN
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(sqlScript, expected)
}

test("empty begin end blocks") {
val sqlScript =
"""
|BEGIN
| BEGIN
| END;
| BEGIN
| END;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(sqlScript, expected)
}

test("empty begin end blocks with single statement") {
val sqlScript =
"""
|BEGIN
| BEGIN
| END;
| SELECT 1;
| BEGIN
| END;
|END
|""".stripMargin
val expected = Seq(Seq(Row(1)))
verifySqlScriptResult(sqlScript, expected)
}

test("empty begin end blocks - nested") {
Comment thread
dusantism-db marked this conversation as resolved.
Outdated
val sqlScript =
"""
|BEGIN
| BEGIN
| BEGIN
| END;
| BEGIN
| END;
| END;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(sqlScript, expected)
}

test("session vars - set and read (SET VAR)") {
val sqlScript =
"""
Expand Down Expand Up @@ -240,6 +295,40 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
verifySqlScriptResult(commands, expected)
}

test("if - empty body") {
val commands =
"""
|BEGIN
| IF 1=1 THEN
| BEGIN
| END;
| END IF;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("if - nested empty body") {
val commands =
"""
|BEGIN
| IF 1=1 THEN
| BEGIN
| BEGIN
| END;
| END;
| BEGIN
| BEGIN
| END;
| END;
| END IF;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("if nested") {
val commands =
"""
Expand Down Expand Up @@ -389,6 +478,42 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
verifySqlScriptResult(commands, expected)
}

test("searched case - empty body") {
val commands =
"""
|BEGIN
| CASE
| WHEN 1 = 1 THEN
| BEGIN
| END;
| END CASE;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("searched case - nested empty body") {
val commands =
"""
|BEGIN
| CASE
| WHEN 1 = 1 THEN
| BEGIN
| BEGIN
| END;
| END;
| BEGIN
| BEGIN
| END;
| END;
| END CASE;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("searched case nested") {
val commands =
"""
Expand Down Expand Up @@ -589,6 +714,42 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
verifySqlScriptResult(commands, expected)
}

test("simple case - empty body") {
val commands =
"""
|BEGIN
| CASE 1
| WHEN 1 THEN
| BEGIN
| END;
| END CASE;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("simple case - nested empty body") {
val commands =
"""
|BEGIN
| CASE 1
| WHEN 1 THEN
| BEGIN
| BEGIN
| END;
| END;
| BEGIN
| BEGIN
| END;
| END;
| END CASE;
|END
|""".stripMargin
val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("simple case nested") {
val commands =
"""
Expand Down Expand Up @@ -985,6 +1146,42 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
verifySqlScriptResult(commands, expected)
}

test("repeat - empty body") {
val commands =
"""
|BEGIN
| REPEAT
| BEGIN
| END;
| UNTIL 1 = 1
| END REPEAT;
|END
|""".stripMargin

val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("repeat - nested empty body") {
val commands =
"""
|BEGIN
| REPEAT
| BEGIN
| BEGIN
| END;
| END;
| BEGIN
| END;
| UNTIL 1 = 1
| END REPEAT;
|END
|""".stripMargin

val expected = Seq.empty[Seq[Row]]
verifySqlScriptResult(commands, expected)
}

test("nested repeat") {
val commands =
"""
Expand Down Expand Up @@ -1795,7 +1992,7 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}
}

test("for statement empty result") {
test("for statement - empty result") {
withTable("t") {
val sqlScript =
"""
Expand All @@ -1814,6 +2011,64 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}
}

test("for statement - empty body") {
withTable("t") {
val sqlScript =
"""
|BEGIN
| CREATE TABLE t (intCol INT, stringCol STRING, doubleCol DOUBLE) using parquet;
| INSERT INTO t VALUES (1, 'first', 1.0);
| FOR row AS SELECT * FROM t DO
| BEGIN
| END;
| END FOR;
|END
|""".stripMargin

val expected = Seq(
Seq.empty[Row], // create table
Seq.empty[Row], // insert
Seq.empty[Row], // drop local var
Seq.empty[Row], // drop local var
Seq.empty[Row], // drop local var
Seq.empty[Row] // drop local var
)
verifySqlScriptResult(sqlScript, expected)
}
}

test("for statement - nested empty body") {
withTable("t") {
val sqlScript =
"""
|BEGIN
| CREATE TABLE t (intCol INT, stringCol STRING, doubleCol DOUBLE) using parquet;
| INSERT INTO t VALUES (1, 'first', 1.0);
| FOR row AS SELECT * FROM t DO
| BEGIN
| BEGIN
| END;
| END;
| BEGIN
| BEGIN
| END;
| END;
| END FOR;
|END
|""".stripMargin

val expected = Seq(
Seq.empty[Row], // create table
Seq.empty[Row], // insert
Seq.empty[Row], // drop local var
Seq.empty[Row], // drop local var
Seq.empty[Row], // drop local var
Seq.empty[Row] // drop local var
)
verifySqlScriptResult(sqlScript, expected)
}
}

test("for statement iterate") {
withTable("t") {
val sqlScript =
Expand Down