Skip to content

Commit

Permalink
fix subquery and tableExpr inside 'from' of 'update statement'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheetahburg authored and wenshao committed Dec 5, 2024
1 parent ee01856 commit 86598e1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3837,6 +3837,19 @@ public boolean visit(SQLUpdateStatement x) {
visit(item);
}

SQLTableSource from = x.getFrom();
if (from instanceof SQLTableSourceImpl) {
println();
indentCount++;
print0(ucase ? "FROM " : "from ");
if (from instanceof SQLSubqueryTableSource) {
visit((SQLSubqueryTableSource) from);
} else if (from instanceof SQLExprTableSource) {
visit((SQLExprTableSource) from);
}
indentCount--;
}

SQLExpr where = x.getWhere();
if (where != null) {
println();
Expand Down
1 change: 1 addition & 0 deletions core/src/test/resources/bvt/parser/bigquery/0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ WHERE
--------------------
UPDATE `p1` main
SET a = false, b = dup.x
FROM tp dup
WHERE main.x = dup.u;
------------------------------------------------------------------------------------------------------------------------
SELECT STRUCT<x int64, y string>(1, t.str_col)
Expand Down
73 changes: 73 additions & 0 deletions core/src/test/resources/bvt/parser/bigquery/9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
UPDATE `a.b.c` tbl SET
tbl.x1 = oba.x2,
tbl.x3 = oba.x4,
tbl.x5 = oba.x6,
tbl.x7 = oba.x8,
tbl.x9 = oba.x10,
tbl.x11 = oba.x12,
tbl.x13 = oba.x14
FROM (
SELECT y1 AS z1, y2 AS z2 FROM (
WITH a1 AS (
SELECT t1.y3 AS y4, t1.y5 AS y6
FROM `a.b.c` t1
LEFT JOIN UNNEST(t1.y7) oba
WHERE LOWER(t1.y6) = 'completed'
),
a2 AS (
SELECT x1 AS x2, x3 AS x4, x5 AS x6
FROM tbl1
WHERE rn = 1 AND x5 = 'SUCCESS'
), all_data AS (
SELECT t FROM `a.b.c` tbl
JOIN (
SELECT _PARTITIONDATE AS dt, x1 AS y19, x2 AS y20, x3 AS y21,
x4 AS y22, x5 AS y23, x6 AS y24, x7 AS y25,
x8 AS y26, x9 AS y27
FROM `a.b.e`
WHERE x9 = 'ID'
) temp3 ON tbl.y4 = temp3.y4
), diff AS (
SELECT * FROM all_data WHERE y2 != y3
)
SELECT * FROM all_data
UNION ALL
SELECT y1, y3 AS y2, NULL AS y3, NULL AS y11, y12 AS y12,
x4 AS x2, NULL AS x4, x5, x6, NULL AS x7, x1 AS x1
FROM diff
) GROUP BY 1, 2, 4, 11, 12
) oba
WHERE tbl.y4 = oba.y2
--------------------
UPDATE a.b.c tbl
SET tbl.x1 = oba.x2, tbl.x3 = oba.x4, tbl.x5 = oba.x6, tbl.x7 = oba.x8, tbl.x9 = oba.x10, tbl.x11 = oba.x12, tbl.x13 = oba.x14
FROM (
SELECT y1 AS z1, y2 AS z2
FROM (
SELECT *
FROM all_data
UNION ALL
SELECT y1, y3 AS y2, NULL AS y3, NULL AS y11, y12 AS y12
, x4 AS x2, NULL AS x4, x5, x6, NULL AS x7
, x1 AS x1
FROM diff
)
GROUP BY 1, 2, 4, 11, 12
) oba
WHERE tbl.y4 = oba.y2
------------------------------------------------------------------------------------------------------------------------
UPDATE `a.b.c` tbl
SET
tbl.x1 = oba.x2,
tbl.x3 = oba.x4,
tbl.x5 = oba.x6,
tbl.x7 = oba.x8,
tbl.x9 = oba.x10,
tbl.x11 = oba.x12,
tbl.x13 = oba.x14
FROM
x.y.z
--------------------
UPDATE a.b.c tbl
SET tbl.x1 = oba.x2, tbl.x3 = oba.x4, tbl.x5 = oba.x6, tbl.x7 = oba.x8, tbl.x9 = oba.x10, tbl.x11 = oba.x12, tbl.x13 = oba.x14
FROM x.y.z

0 comments on commit 86598e1

Please sign in to comment.