Skip to content

Commit

Permalink
add sum test
Browse files Browse the repository at this point in the history
  • Loading branch information
dusantism-db committed Nov 26, 2024
1 parent 9bf0b7a commit 9d1cf29
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,38 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}
}

test("for statement - sum of column from table") {
withTable("t") {
val sqlScript =
"""
|BEGIN
| DECLARE sumOfCols = 0;
| CREATE TABLE t (intCol INT) using parquet;
| INSERT INTO t VALUES (1), (2), (3), (4);
| FOR row AS SELECT * FROM t DO
| SET sumOfCols = sumOfCols + row.intCol;
| END FOR;
| SELECT sumOfCols;
|END
|""".stripMargin

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

test("for statement - map, struct, array") {
withTable("t") {
val sqlScript =
Expand Down Expand Up @@ -2139,6 +2171,37 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}
}

test("for statement - no variable - sum of column from table") {
withTable("t") {
val sqlScript =
"""
|BEGIN
| DECLARE sumOfCols = 0;
| CREATE TABLE t (intCol INT) using parquet;
| INSERT INTO t VALUES (1), (2), (3), (4);
| FOR SELECT * FROM t DO
| SET sumOfCols = sumOfCols + intCol;
| END FOR;
| SELECT sumOfCols;
|END
|""".stripMargin

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

test("for statement - no variable - map, struct, array") {
withTable("t") {
val sqlScript =
Expand Down

0 comments on commit 9d1cf29

Please sign in to comment.