summaryrefslogtreecommitdiffstats
path: root/tests/fixtures/optimizer
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-08-06 07:48:11 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-08-06 07:48:11 +0000
commit379c6d1f52e1d311867c4f789dc389da1d9af898 (patch)
treec9ca62eb7b8b7e861cc67248850db220ad0881c9 /tests/fixtures/optimizer
parentReleasing debian version 17.7.0-1. (diff)
downloadsqlglot-379c6d1f52e1d311867c4f789dc389da1d9af898.tar.xz
sqlglot-379c6d1f52e1d311867c4f789dc389da1d9af898.zip
Merging upstream version 17.9.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/fixtures/optimizer')
-rw-r--r--tests/fixtures/optimizer/normalize_identifiers.sql7
-rw-r--r--tests/fixtures/optimizer/optimizer.sql19
-rw-r--r--tests/fixtures/optimizer/qualify_columns.sql10
-rw-r--r--tests/fixtures/optimizer/qualify_columns_ddl.sql35
-rw-r--r--tests/fixtures/optimizer/tpc-ds/tpc-ds.sql30
5 files changed, 95 insertions, 6 deletions
diff --git a/tests/fixtures/optimizer/normalize_identifiers.sql b/tests/fixtures/optimizer/normalize_identifiers.sql
index ddb755f..2ab4778 100644
--- a/tests/fixtures/optimizer/normalize_identifiers.sql
+++ b/tests/fixtures/optimizer/normalize_identifiers.sql
@@ -1,3 +1,10 @@
+foo;
+foo;
+
+# dialect: snowflake
+foo + "bar".baz;
+FOO + "bar".BAZ;
+
SELECT a FROM x;
SELECT a FROM x;
diff --git a/tests/fixtures/optimizer/optimizer.sql b/tests/fixtures/optimizer/optimizer.sql
index 14f5cfe..981e052 100644
--- a/tests/fixtures/optimizer/optimizer.sql
+++ b/tests/fixtures/optimizer/optimizer.sql
@@ -638,7 +638,7 @@ SELECT
FROM "users" AS "u"
CROSS JOIN LATERAL (
SELECT
- "l"."log_date"
+ "l"."log_date" AS "log_date"
FROM "logs" AS "l"
WHERE
"l"."log_date" <= 100 AND "l"."user_id" = "u"."user_id"
@@ -890,3 +890,20 @@ FROM (
JOIN "y" AS "y"
ON "x"."a" = "y"."c"
);
+
+# title: replace scalar subquery, wrap resulting column in a MAX
+SELECT a, SUM(c) / (SELECT SUM(c) FROM y) * 100 AS foo FROM y INNER JOIN x ON y.b = x.b GROUP BY a;
+WITH "_u_0" AS (
+ SELECT
+ SUM("y"."c") AS "_col_0"
+ FROM "y" AS "y"
+)
+SELECT
+ "x"."a" AS "a",
+ SUM("y"."c") / MAX("_u_0"."_col_0") * 100 AS "foo"
+FROM "y" AS "y"
+CROSS JOIN "_u_0" AS "_u_0"
+JOIN "x" AS "x"
+ ON "y"."b" = "x"."b"
+GROUP BY
+ "x"."a";
diff --git a/tests/fixtures/optimizer/qualify_columns.sql b/tests/fixtures/optimizer/qualify_columns.sql
index 90505ac..8a2519e 100644
--- a/tests/fixtures/optimizer/qualify_columns.sql
+++ b/tests/fixtures/optimizer/qualify_columns.sql
@@ -94,6 +94,16 @@ SELECT 'a' AS a FROM x GROUP BY 1;
SELECT 'a' AS a FROM x AS x GROUP BY 1;
# execute: false
+# dialect: oracle
+SELECT t."col" FROM tbl t;
+SELECT T."col" AS "col" FROM TBL T;
+
+# execute: false
+# dialect: oracle
+WITH base AS (SELECT x.dummy AS COL_1 FROM dual x) SELECT b."COL_1" FROM base b;
+WITH BASE AS (SELECT X.DUMMY AS COL_1 FROM DUAL X) SELECT B.COL_1 AS COL_1 FROM BASE B;
+
+# execute: false
-- this query seems to be invalid in postgres and duckdb but valid in bigquery
SELECT 2 a FROM x GROUP BY 1 HAVING a > 1;
SELECT 2 AS a FROM x AS x GROUP BY 1 HAVING a > 1;
diff --git a/tests/fixtures/optimizer/qualify_columns_ddl.sql b/tests/fixtures/optimizer/qualify_columns_ddl.sql
new file mode 100644
index 0000000..87e0f6d
--- /dev/null
+++ b/tests/fixtures/optimizer/qualify_columns_ddl.sql
@@ -0,0 +1,35 @@
+# title: Create with CTE
+WITH cte AS (SELECT b FROM y) CREATE TABLE s AS SELECT * FROM cte;
+WITH cte AS (SELECT y.b AS b FROM y AS y) CREATE TABLE s AS SELECT cte.b AS b FROM cte;
+
+# title: Create without CTE
+CREATE TABLE foo AS SELECT a FROM tbl;
+CREATE TABLE foo AS SELECT tbl.a AS a FROM tbl AS tbl;
+
+# title: Create with complex CTE with derived table
+WITH cte AS (SELECT a FROM (SELECT a from x)) CREATE TABLE s AS SELECT * FROM cte;
+WITH cte AS (SELECT _q_0.a AS a FROM (SELECT x.a AS a FROM x AS x) AS _q_0) CREATE TABLE s AS SELECT cte.a AS a FROM cte;
+
+# title: Create wtih multiple CTEs
+WITH cte1 AS (SELECT b FROM y), cte2 AS (SELECT b FROM cte1) CREATE TABLE s AS SELECT * FROM cte2;
+WITH cte1 AS (SELECT y.b AS b FROM y AS y), cte2 AS (SELECT cte1.b AS b FROM cte1) CREATE TABLE s AS SELECT cte2.b AS b FROM cte2;
+
+# title: Create with multiple CTEs, selecting only from the first CTE (unnecessary code)
+WITH cte1 AS (SELECT b FROM y), cte2 AS (SELECT b FROM cte1) CREATE TABLE s AS SELECT * FROM cte1;
+WITH cte1 AS (SELECT y.b AS b FROM y AS y), cte2 AS (SELECT cte1.b AS b FROM cte1) CREATE TABLE s AS SELECT cte1.b AS b FROM cte1;
+
+# title: Create with multiple derived tables
+CREATE TABLE s AS SELECT * FROM (SELECT b FROM (SELECT b FROM y));
+CREATE TABLE s AS SELECT _q_1.b AS b FROM (SELECT _q_0.b AS b FROM (SELECT y.b AS b FROM y AS y) AS _q_0) AS _q_1;
+
+# title: Create with a CTE and a derived table
+WITH cte AS (SELECT b FROM y) CREATE TABLE s AS SELECT * FROM (SELECT b FROM (SELECT b FROM cte));
+WITH cte AS (SELECT y.b AS b FROM y AS y) CREATE TABLE s AS SELECT _q_1.b AS b FROM (SELECT _q_0.b AS b FROM (SELECT cte.b AS b FROM cte) AS _q_0) AS _q_1;
+
+# title: Insert with CTE
+WITH cte AS (SELECT b FROM y) INSERT INTO s SELECT * FROM cte;
+WITH cte AS (SELECT y.b AS b FROM y AS y) INSERT INTO s SELECT cte.b AS b FROM cte;
+
+# title: Insert without CTE
+INSERT INTO foo SELECT a FROM tbl;
+INSERT INTO foo SELECT tbl.a AS a FROM tbl AS tbl;
diff --git a/tests/fixtures/optimizer/tpc-ds/tpc-ds.sql b/tests/fixtures/optimizer/tpc-ds/tpc-ds.sql
index 8aaf50c..1205c33 100644
--- a/tests/fixtures/optimizer/tpc-ds/tpc-ds.sql
+++ b/tests/fixtures/optimizer/tpc-ds/tpc-ds.sql
@@ -1449,11 +1449,31 @@ WITH "_u_0" AS (
"store_sales"."ss_quantity" <= 80 AND "store_sales"."ss_quantity" >= 61
)
SELECT
- CASE WHEN "_u_0"."_col_0" > 3672 THEN "_u_1"."_col_0" ELSE "_u_2"."_col_0" END AS "bucket1",
- CASE WHEN "_u_3"."_col_0" > 3392 THEN "_u_4"."_col_0" ELSE "_u_5"."_col_0" END AS "bucket2",
- CASE WHEN "_u_6"."_col_0" > 32784 THEN "_u_7"."_col_0" ELSE "_u_8"."_col_0" END AS "bucket3",
- CASE WHEN "_u_9"."_col_0" > 26032 THEN "_u_10"."_col_0" ELSE "_u_11"."_col_0" END AS "bucket4",
- CASE WHEN "_u_12"."_col_0" > 23982 THEN "_u_13"."_col_0" ELSE "_u_14"."_col_0" END AS "bucket5"
+ CASE
+ WHEN MAX("_u_0"."_col_0") > 3672
+ THEN MAX("_u_1"."_col_0")
+ ELSE MAX("_u_2"."_col_0")
+ END AS "bucket1",
+ CASE
+ WHEN MAX("_u_3"."_col_0") > 3392
+ THEN MAX("_u_4"."_col_0")
+ ELSE MAX("_u_5"."_col_0")
+ END AS "bucket2",
+ CASE
+ WHEN MAX("_u_6"."_col_0") > 32784
+ THEN MAX("_u_7"."_col_0")
+ ELSE MAX("_u_8"."_col_0")
+ END AS "bucket3",
+ CASE
+ WHEN MAX("_u_9"."_col_0") > 26032
+ THEN MAX("_u_10"."_col_0")
+ ELSE MAX("_u_11"."_col_0")
+ END AS "bucket4",
+ CASE
+ WHEN MAX("_u_12"."_col_0") > 23982
+ THEN MAX("_u_13"."_col_0")
+ ELSE MAX("_u_14"."_col_0")
+ END AS "bucket5"
FROM "reason" AS "reason"
CROSS JOIN "_u_0" AS "_u_0"
CROSS JOIN "_u_1" AS "_u_1"