summaryrefslogtreecommitdiffstats
path: root/tests/fixtures/optimizer/simplify.sql
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 08:11:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 08:11:50 +0000
commit8978da3b39d7ca3cf83ee30fcc63ffe0e5453fb2 (patch)
tree2e29f131dff77b31e84c957266de8f18655b6f88 /tests/fixtures/optimizer/simplify.sql
parentAdding upstream version 22.2.0. (diff)
downloadsqlglot-8978da3b39d7ca3cf83ee30fcc63ffe0e5453fb2.tar.xz
sqlglot-8978da3b39d7ca3cf83ee30fcc63ffe0e5453fb2.zip
Adding upstream version 23.7.0.upstream/23.7.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/fixtures/optimizer/simplify.sql')
-rw-r--r--tests/fixtures/optimizer/simplify.sql99
1 files changed, 98 insertions, 1 deletions
diff --git a/tests/fixtures/optimizer/simplify.sql b/tests/fixtures/optimizer/simplify.sql
index da9f26d..a10942d 100644
--- a/tests/fixtures/optimizer/simplify.sql
+++ b/tests/fixtures/optimizer/simplify.sql
@@ -52,6 +52,9 @@ FALSE;
'x' = 'x';
TRUE;
+STRUCT(NULL AS a);
+STRUCT(NULL AS a);
+
NULL AND TRUE;
NULL;
@@ -102,6 +105,10 @@ a AND b;
a AND (b AND b);
a AND b;
+-- bigquery doesn't allow unparenthesis comparisons
+(x is not null) != (y is null);
+(NOT x IS NULL) <> (y IS NULL);
+
--------------------------------------
-- Absorption
--------------------------------------
@@ -459,6 +466,18 @@ CAST('1998-09-02 00:00:00' AS DATETIME);
CAST(x AS DATETIME) + interval '1' WEEK;
CAST(x AS DATETIME) + INTERVAL '1' WEEK;
+# dialect: bigquery
+CAST('2023-01-01' AS TIMESTAMP) + INTERVAL 1 DAY;
+CAST('2023-01-02 00:00:00' AS TIMESTAMP);
+
+# dialect: bigquery
+INTERVAL 1 DAY + CAST('2023-01-01' AS TIMESTAMP);
+CAST('2023-01-02 00:00:00' AS TIMESTAMP);
+
+# dialect: bigquery
+CAST('2023-01-02' AS TIMESTAMP) - INTERVAL 1 DAY;
+CAST('2023-01-01 00:00:00' AS TIMESTAMP);
+
TS_OR_DS_TO_DATE('1998-12-01 00:00:01') - interval '90' day;
CAST('1998-09-02' AS DATE);
@@ -708,6 +727,48 @@ FUN() > 0;
RAND() > 0 OR RAND() > 1;
RAND() > 0 OR RAND() > 1;
+CAST(1 AS UINT) >= 0;
+TRUE;
+
+CAST(-1 AS TINYINT) <= 0;
+TRUE;
+
+CAST(1 AS INT) = CAST(1 AS UINT);
+TRUE;
+
+CASE WHEN CAST(1 AS TINYINT) = 1 THEN FALSE ELSE TRUE END;
+FALSE;
+
+CAST(1 AS INT) + 1;
+CAST(1 AS INT) + 1;
+
+CAST(CAST(CAST(-1 AS INT) AS INT) AS INT) = -1;
+TRUE;
+
+CAST(-1 AS UINT) <= 0;
+CAST(-1 AS UINT) <= 0;
+
+CAST(-129 AS TINYINT) <= 0;
+CAST(-129 AS TINYINT) <= 0;
+
+CAST(256 AS UINT) >= 0;
+CAST(256 AS UINT) >= 0;
+
+CAST(CAST(CAST(-1 AS INT) AS UINT) AS INT) = 1;
+CAST(CAST(CAST(-1 AS INT) AS UINT) AS INT) = 1;
+
+CAST(x AS TINYINT) = 1;
+CAST(x AS TINYINT) = 1;
+
+CAST(CAST(1 AS INT) AS BOOLEAN) = 1;
+CAST(CAST(1 AS INT) AS BOOLEAN) = 1;
+
+CAST(CAST(CAST(1 AS INT) AS BOOLEAN) AS INT) = 1;
+CAST(CAST(CAST(1 AS INT) AS BOOLEAN) AS INT) = 1;
+
+x > CAST('2023-01-01' AS DATE) AND x < CAST('2023-01-01' AS DATETIME);
+FALSE;
+
--------------------------------------
-- COALESCE
--------------------------------------
@@ -745,7 +806,7 @@ COALESCE(ROW() OVER (), 1) = 1;
ROW() OVER () = 1 OR ROW() OVER () IS NULL;
a AND b AND COALESCE(ROW() OVER (), 1) = 1;
-a AND b AND (ROW() OVER () = 1 OR ROW() OVER () IS NULL);
+(ROW() OVER () = 1 OR ROW() OVER () IS NULL) AND a AND b;
COALESCE(1, 2);
1;
@@ -824,27 +885,51 @@ DATE_TRUNC(CAST('2023-12-15' AS DATE), WEEK);
CAST('2023-12-10' AS DATE);
# dialect: bigquery
+DATE_TRUNC(CAST('2023-10-01' AS TIMESTAMP), QUARTER);
+CAST('2023-10-01 00:00:00' AS TIMESTAMP);
+
+# dialect: bigquery
DATE_TRUNC(CAST('2023-12-16' AS DATE), WEEK);
CAST('2023-12-10' AS DATE);
DATE_TRUNC('year', x) = CAST('2021-01-01' AS DATE);
x < CAST('2022-01-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE);
+# dialect: bigquery
+DATE_TRUNC(x, year) = CAST('2021-01-01' AS TIMESTAMP);
+x < CAST('2022-01-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP);
+
DATE_TRUNC('quarter', x) = CAST('2021-01-01' AS DATE);
x < CAST('2021-04-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE);
+# dialect: bigquery
+DATE_TRUNC(x, quarter) = CAST('2021-01-01' AS TIMESTAMP);
+x < CAST('2021-04-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP);
+
DATE_TRUNC('month', x) = CAST('2021-01-01' AS DATE);
x < CAST('2021-02-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE);
+# dialect: bigquery
+DATE_TRUNC(x, month) = CAST('2021-01-01' AS TIMESTAMP);
+x < CAST('2021-02-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP);
+
DATE_TRUNC('week', x) = CAST('2021-01-04' AS DATE);
x < CAST('2021-01-11' AS DATE) AND x >= CAST('2021-01-04' AS DATE);
DATE_TRUNC('day', x) = CAST('2021-01-01' AS DATE);
x < CAST('2021-01-02' AS DATE) AND x >= CAST('2021-01-01' AS DATE);
+# dialect: bigquery
+DATE_TRUNC(x, DAY) = CAST('2021-01-01' AS TIMESTAMP);
+x < CAST('2021-01-02 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP);
+
CAST('2021-01-01' AS DATE) = DATE_TRUNC('year', x);
x < CAST('2022-01-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE);
+# dialect: bigquery
+CAST('2021-01-01' AS TIMESTAMP) = DATE_TRUNC(x, year);
+x < CAST('2022-01-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP);
+
-- Always false, except for nulls
DATE_TRUNC('quarter', x) = CAST('2021-01-02' AS DATE);
DATE_TRUNC('QUARTER', x) = CAST('2021-01-02' AS DATE);
@@ -859,12 +944,20 @@ DATE_TRUNC('YEAR', x) <> CAST('2021-01-02' AS DATE);
DATE_TRUNC('year', x) <= CAST('2021-01-01' AS DATE);
x < CAST('2022-01-01' AS DATE);
+# dialect: bigquery
+DATE_TRUNC(x, year) <= CAST('2021-01-01' AS TIMESTAMP);
+x < CAST('2022-01-01 00:00:00' AS TIMESTAMP);
+
DATE_TRUNC('year', x) <= CAST('2021-01-02' AS DATE);
x < CAST('2022-01-01' AS DATE);
CAST('2021-01-01' AS DATE) >= DATE_TRUNC('year', x);
x < CAST('2022-01-01' AS DATE);
+# dialect: bigquery
+CAST('2021-01-01' AS TIMESTAMP) >= DATE_TRUNC(x, year);
+x < CAST('2022-01-01 00:00:00' AS TIMESTAMP);
+
DATE_TRUNC('year', x) < CAST('2021-01-01' AS DATE);
x < CAST('2021-01-01' AS DATE);
@@ -896,6 +989,10 @@ DATE_TRUNC('YEAR', x) <> '2021-01-02';
DATE_TRUNC('year', x) IN (CAST('2021-01-01' AS DATE), CAST('2023-01-01' AS DATE));
(x < CAST('2022-01-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE)) OR (x < CAST('2024-01-01' AS DATE) AND x >= CAST('2023-01-01' AS DATE));
+# dialect: bigquery
+DATE_TRUNC(x, year) IN (CAST('2021-01-01' AS TIMESTAMP), CAST('2023-01-01' AS TIMESTAMP));
+(x < CAST('2022-01-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2021-01-01 00:00:00' AS TIMESTAMP)) OR (x < CAST('2024-01-01 00:00:00' AS TIMESTAMP) AND x >= CAST('2023-01-01 00:00:00' AS TIMESTAMP));
+
-- merge ranges
DATE_TRUNC('year', x) IN (CAST('2021-01-01' AS DATE), CAST('2022-01-01' AS DATE));
x < CAST('2023-01-01' AS DATE) AND x >= CAST('2021-01-01' AS DATE);