-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix subquery and tableExpr inside 'from' of 'update statement'
- Loading branch information
1 parent
ee01856
commit 86598e1
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |