diff --git a/vegafusion-sql/tests/expected/impute.toml b/vegafusion-sql/tests/expected/impute.toml index b84247b7..21e45dc9 100644 --- a/vegafusion-sql/tests/expected/impute.toml +++ b/vegafusion-sql/tests/expected/impute.toml @@ -1,63 +1,33 @@ [unordered_no_groups] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT coalesce(`a`, CAST(-1 AS INT)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT coalesce("a", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT coalesce(`a`, CAST(-1 AS BIGINT)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT coalesce(`a`, CAST(-1 AS SIGNED)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT coalesce("a", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), \ -values1 AS (SELECT coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), values1 AS (SELECT coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ result = ''' +----+----+---+----+ @@ -75,64 +45,34 @@ result = ''' [unordered_one_group] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS INT)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_key"."a" AS "a", coalesce("b", -1) AS "b", "_groups"."c" AS "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_key"."a" AS "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "_groups"."c" AS "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS BIGINT)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) \ -SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS SIGNED)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)) SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST +WITH values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST """ result = ''' +---+----+---+----+ @@ -151,64 +91,34 @@ result = ''' [unordered_two_groups] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST, `c` ASC NULLS FIRST, `d` ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS INT)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST, `c` ASC NULLS FIRST, `d` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_key"."a" AS "a", coalesce("b", -1) AS "b", "_groups"."c" AS "c", "_groups"."d" AS "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_key"."a" AS "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "_groups"."c" AS "c", "_groups"."d" AS "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) \ -SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST, `c` ASC NULLS FIRST, `d` ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS BIGINT)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) SELECT * FROM values1 ORDER BY `a` ASC NULLS FIRST, `b` ASC NULLS FIRST, `c` ASC NULLS FIRST, `d` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `a`, coalesce(`b`, -1) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) \ -SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC, `c` ASC, `d` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(0, 28, 0, -1), ROW(0, 91, 1, -1), ROW(1, 43, 0, -2), ROW(NULL, 55, 1, -2), ROW(3, 19, 0, -3), ROW(2, 81, 0, -3), ROW(2, 53, 1, -4)) AS `_values` (`a`, `b`, `c`, `d`)), values1 AS (SELECT `a`, coalesce(`b`, CAST(-1 AS SIGNED)) AS `b`, `c`, `d` FROM (SELECT `a` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)) SELECT * FROM values1 ORDER BY `a` ASC, `b` ASC, `c` ASC, `d` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4)) AS "_values" ("a", "b", "c", "d")), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), \ -values1 AS (SELECT "a", coalesce("b", -1) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) \ -SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST +WITH values0 AS (SELECT "COLUMN1" AS "a", "COLUMN2" AS "b", "COLUMN3" AS "c", "COLUMN4" AS "d" FROM (VALUES (0, 28, 0, -1), (0, 91, 1, -1), (1, 43, 0, -2), (NULL, 55, 1, -2), (3, 19, 0, -3), (2, 81, 0, -3), (2, 53, 1, -4))), values1 AS (SELECT "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d" FROM (SELECT "a" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")) SELECT * FROM values1 ORDER BY "a" ASC NULLS FIRST, "b" ASC NULLS FIRST, "c" ASC NULLS FIRST, "d" ASC NULLS FIRST """ result = ''' +---+----+---+----+ @@ -243,64 +153,34 @@ result = ''' [ordered_no_groups] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT `_order`, coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `_order` ASC NULLS FIRST +WITH values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT `_order`, coalesce(`a`, CAST(-1 AS INT)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `_order` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", coalesce("a", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `_order` ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, coalesce(`a`, CAST(-1 AS BIGINT)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `_order` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, coalesce(`a`, -1) AS `a`, `b`, `c`, `d` FROM values0) \ -SELECT * FROM values1 ORDER BY `_order` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, coalesce(`a`, CAST(-1 AS SIGNED)) AS `a`, `b`, `c`, `d` FROM values0) SELECT * FROM values1 ORDER BY `_order` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", coalesce("a", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), \ -values1 AS (SELECT "_order", coalesce("a", -1) AS "a", "b", "c", "d" FROM values0) \ -SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), values1 AS (SELECT "_order", coalesce("a", CAST(-1 AS BIGINT)) AS "a", "b", "c", "d" FROM values0) SELECT * FROM values1 ORDER BY "_order" ASC NULLS FIRST """ result = ''' +--------+----+----+---+----+ @@ -318,81 +198,34 @@ result = ''' [ordered_one_group] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS INT)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", "_key"."a" AS "a", coalesce("b", -1) AS "b", "_groups"."c" AS "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", "_key"."a" AS "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "_groups"."c" AS "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS BIGINT)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` IS NULL ASC, `_order` ASC, `_order_groups` ASC, `_order_key` ASC) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS SIGNED)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`) AS _groups LEFT JOIN values0 USING(`a`, `c`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` IS NULL ASC, `_order` ASC, `_order_groups` ASC, `_order_key` ASC) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", min("_order") AS "_order_groups" FROM values0 GROUP BY "c") AS _groups LEFT JOIN values0 USING("a", "c")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ result = ''' +--------+---+----+---+----+ @@ -411,82 +244,34 @@ result = ''' [ordered_two_groups] athena = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ bigquery = """ -WITH \ -values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS `_order`, 0 AS `a`, 28 AS `b`, 0 AS `c`, -1 AS `d` UNION ALL SELECT 2 AS `_order`, 0 AS `a`, 91 AS `b`, 1 AS `c`, -1 AS `d` UNION ALL SELECT 3 AS `_order`, 1 AS `a`, 43 AS `b`, 0 AS `c`, -2 AS `d` UNION ALL SELECT 4 AS `_order`, NULL AS `a`, 55 AS `b`, 1 AS `c`, -2 AS `d` UNION ALL SELECT 5 AS `_order`, 3 AS `a`, 19 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 6 AS `_order`, 2 AS `a`, 81 AS `b`, 0 AS `c`, -3 AS `d` UNION ALL SELECT 7 AS `_order`, 2 AS `a`, 53 AS `b`, 1 AS `c`, -4 AS `d`), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS INT)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST """ clickhouse = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", "_key"."a" AS "a", coalesce("b", -1) AS "b", "_groups"."c" AS "c", "_groups"."d" AS "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", "_key"."a" AS "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "_groups"."c" AS "c", "_groups"."d" AS "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ databricks = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS BIGINT)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` ASC NULLS LAST, `_order_groups` ASC NULLS FIRST, `_order_key` ASC NULLS FIRST) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC NULLS FIRST """ datafusion = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ duckdb = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ mysql = """ -WITH \ -values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), \ -values1 AS (SELECT `_order`, `a`, coalesce(`b`, -1) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), \ -values2 AS (SELECT row_number() OVER (ORDER BY `_order` IS NULL ASC, `_order` ASC, `_order_groups` ASC, `_order_key` ASC) AS `_order`, `a`, `b`, `c`, `d` FROM values1) \ -SELECT * FROM values2 ORDER BY `_order` ASC +WITH values0 AS (SELECT * FROM (VALUES ROW(1, 0, 28, 0, -1), ROW(2, 0, 91, 1, -1), ROW(3, 1, 43, 0, -2), ROW(4, NULL, 55, 1, -2), ROW(5, 3, 19, 0, -3), ROW(6, 2, 81, 0, -3), ROW(7, 2, 53, 1, -4)) AS `_values` (`_order`, `a`, `b`, `c`, `d`)), values1 AS (SELECT `_order`, `a`, coalesce(`b`, CAST(-1 AS SIGNED)) AS `b`, `c`, `d`, `_order_key`, `_order_groups` FROM (SELECT `a`, min(`_order`) AS `_order_key` FROM values0 WHERE `a` IS NOT NULL GROUP BY `a`) AS _key CROSS JOIN (SELECT `c`, `d`, min(`_order`) AS `_order_groups` FROM values0 GROUP BY `c`, `d`) AS _groups LEFT JOIN values0 USING(`a`, `c`, `d`)), values2 AS (SELECT row_number() OVER (ORDER BY `_order` IS NULL ASC, `_order` ASC, `_order_groups` ASC, `_order_key` ASC) AS `_order`, `a`, `b`, `c`, `d` FROM values1) SELECT * FROM values2 ORDER BY `_order` ASC """ postgres = """ -WITH \ -values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT * FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4)) AS "_values" ("_order", "a", "b", "c", "d")), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ redshift = """ -WITH \ -values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT 1 AS "_order", 0 AS "a", 28 AS "b", 0 AS "c", -1 AS "d" UNION ALL SELECT 2 AS "_order", 0 AS "a", 91 AS "b", 1 AS "c", -1 AS "d" UNION ALL SELECT 3 AS "_order", 1 AS "a", 43 AS "b", 0 AS "c", -2 AS "d" UNION ALL SELECT 4 AS "_order", NULL AS "a", 55 AS "b", 1 AS "c", -2 AS "d" UNION ALL SELECT 5 AS "_order", 3 AS "a", 19 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 6 AS "_order", 2 AS "a", 81 AS "b", 0 AS "c", -3 AS "d" UNION ALL SELECT 7 AS "_order", 2 AS "a", 53 AS "b", 1 AS "c", -4 AS "d"), values1 AS (SELECT "_order", "a", coalesce("b", CASE WHEN -1 IS NOT NULL THEN CAST(-1 AS BIGINT) ELSE NULL END) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ snowflake = """ -WITH \ -values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), \ -values1 AS (SELECT "_order", "a", coalesce("b", -1) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), \ -values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) \ -SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST - +WITH values0 AS (SELECT "COLUMN1" AS "_order", "COLUMN2" AS "a", "COLUMN3" AS "b", "COLUMN4" AS "c", "COLUMN5" AS "d" FROM (VALUES (1, 0, 28, 0, -1), (2, 0, 91, 1, -1), (3, 1, 43, 0, -2), (4, NULL, 55, 1, -2), (5, 3, 19, 0, -3), (6, 2, 81, 0, -3), (7, 2, 53, 1, -4))), values1 AS (SELECT "_order", "a", coalesce("b", CAST(-1 AS BIGINT)) AS "b", "c", "d", "_order_key", "_order_groups" FROM (SELECT "a", min("_order") AS "_order_key" FROM values0 WHERE "a" IS NOT NULL GROUP BY "a") AS _key CROSS JOIN (SELECT "c", "d", min("_order") AS "_order_groups" FROM values0 GROUP BY "c", "d") AS _groups LEFT JOIN values0 USING("a", "c", "d")), values2 AS (SELECT row_number() OVER (ORDER BY "_order" ASC NULLS LAST, "_order_groups" ASC NULLS FIRST, "_order_key" ASC NULLS FIRST) AS "_order", "a", "b", "c", "d" FROM values1) SELECT * FROM values2 ORDER BY "_order" ASC NULLS FIRST """ result = ''' +--------+---+----+---+----+