diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/named-window.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/named-window.sql.out new file mode 100644 index 000000000000..4350aa1d87e2 --- /dev/null +++ b/sql/core/src/test/resources/sql-tests/analyzer-results/named-window.sql.out @@ -0,0 +1,619 @@ +-- Automatically generated by SQLQueryTestSuite +-- !query +CREATE OR REPLACE TEMPORARY VIEW nation AS SELECT * FROM VALUES +(0, 'ALGERIA', 0), +(1, 'ARGENTINA', 1), +(2, 'BRAZIL', 1), +(3, 'CANADA', 1), +(4, 'EGYPT', 4), +(5, 'ETHIOPIA', 0), +(6, 'FRANCE', 3), +(7, 'GERMANY', 3), +(8, 'INDIA', 2), +(9, 'INDONESIA', 2), +(10, 'IRAN', 4), +(11, 'IRAQ', 4), +(12, 'JAPAN', 2), +(13, 'JORDAN', 4), +(14, 'KENYA', 0), +(15, 'MOROCCO', 0), +(16, 'MOZAMBIQUE', 0), +(17, 'PERU', 1), +(18, 'CHINA', 2), +(19, 'ROMANIA', 3), +(20, 'SAUDI ARABIA', 4), +(21, 'VIETNAM', 2), +(22, 'RUSSIA', 3), +(23, 'UNITED KINGDOM', 3), +(24, 'UNITED STATES', 1) +AS nation(n_nationkey, n_name, n_regionkey) +-- !query analysis +CreateViewCommand `nation`, SELECT * FROM VALUES +(0, 'ALGERIA', 0), +(1, 'ARGENTINA', 1), +(2, 'BRAZIL', 1), +(3, 'CANADA', 1), +(4, 'EGYPT', 4), +(5, 'ETHIOPIA', 0), +(6, 'FRANCE', 3), +(7, 'GERMANY', 3), +(8, 'INDIA', 2), +(9, 'INDONESIA', 2), +(10, 'IRAN', 4), +(11, 'IRAQ', 4), +(12, 'JAPAN', 2), +(13, 'JORDAN', 4), +(14, 'KENYA', 0), +(15, 'MOROCCO', 0), +(16, 'MOZAMBIQUE', 0), +(17, 'PERU', 1), +(18, 'CHINA', 2), +(19, 'ROMANIA', 3), +(20, 'SAUDI ARABIA', 4), +(21, 'VIETNAM', 2), +(22, 'RUSSIA', 3), +(23, 'UNITED KINGDOM', 3), +(24, 'UNITED STATES', 1) +AS nation(n_nationkey, n_name, n_regionkey), false, true, LocalTempView, UNSUPPORTED, true + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +CREATE OR REPLACE TEMPORARY VIEW region AS SELECT * FROM VALUES +(0, 'AFRICA'), +(1, 'AMERICA'), +(2, 'ASIA'), +(3, 'EUROPE'), +(4, 'MIDDLE EAST') +AS region(r_regionkey, r_name) +-- !query analysis +CreateViewCommand `region`, SELECT * FROM VALUES +(0, 'AFRICA'), +(1, 'AMERICA'), +(2, 'ASIA'), +(3, 'EUROPE'), +(4, 'MIDDLE EAST') +AS region(r_regionkey, r_name), false, true, LocalTempView, UNSUPPORTED, true + +- Project [r_regionkey#x, r_name#x] + +- SubqueryAlias region + +- LocalRelation [r_regionkey#x, r_name#x] + + +-- !query +SELECT n_nationkey, n_name, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, n_name#x, ws#xL] + +- Project [n_nationkey#x, n_name#x, ws#xL, ws#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS ws#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_name#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_regionkey, n_nationkey, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query analysis +Sort [n_regionkey#x ASC NULLS FIRST, n_nationkey#x ASC NULLS FIRST], true ++- Project [n_regionkey#x, n_nationkey#x, ws#xL] + +- Project [n_regionkey#x, n_nationkey#x, ws#xL, ws#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS ws#xL], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_regionkey#x, n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_nationkey, n_name, + sum(n_nationkey) OVER w AS total, + rank() OVER w AS rnk +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query analysis +Project [n_nationkey#x, n_name#x, total#xL, rnk#x] ++- Sort [n_regionkey#x ASC NULLS FIRST, n_nationkey#x ASC NULLS FIRST], true + +- Project [n_nationkey#x, n_name#x, total#xL, rnk#x, n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x, total#xL, rnk#x, total#xL, rnk#x] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS total#xL, rank(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rnk#x], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_regionkey, n_nationkey, + sum(n_nationkey) OVER w1 AS running_sum, + rank() OVER w2 AS rgn_rank +FROM nation +WINDOW + w1 AS (ORDER BY n_nationkey), + w2 AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_regionkey#x, n_nationkey#x, running_sum#xL, rgn_rank#x] + +- Project [n_regionkey#x, n_nationkey#x, running_sum#xL, rgn_rank#x, running_sum#xL, rgn_rank#x] + +- Window [rank(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rgn_rank#x], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS running_sum#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_regionkey#x, n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w1 AS s +FROM nation +WINDOW w1 AS w2, w2 AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, s#xL] + +- Project [n_nationkey#x, s#xL, s#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w2 AS s +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1 +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, s#xL] + +- Project [n_nationkey#x, s#xL, s#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_nationkey, + sum(n_nationkey) OVER w2 AS s2, + rank() OVER w3 AS r3 +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w1 +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, s2#xL, r3#x] + +- Project [n_nationkey#x, s2#xL, r3#x, s2#xL, r3#x] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s2#xL, rank(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS r3#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT sum(n_nationkey) OVER w1 FROM nation WINDOW w1 AS w2, w2 AS w1 +-- !query analysis +org.apache.spark.sql.catalyst.parser.ParseException +{ + "errorClass" : "INVALID_SQL_SYNTAX.INVALID_WINDOW_REFERENCE", + "sqlState" : "42000", + "messageParameters" : { + "windowName" : "`w2`" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 45, + "stopIndex" : 69, + "fragment" : "WINDOW w1 AS w2, w2 AS w1" + } ] +} + + +-- !query +SELECT sum(n_nationkey) OVER w3 FROM nation WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w2 +-- !query analysis +org.apache.spark.sql.catalyst.parser.ParseException +{ + "errorClass" : "INVALID_SQL_SYNTAX.INVALID_WINDOW_REFERENCE", + "sqlState" : "42000", + "messageParameters" : { + "windowName" : "`w2`" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 45, + "stopIndex" : 99, + "fragment" : "WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w2" + } ] +} + + +-- !query +SELECT sum(n_nationkey) OVER w FROM nation +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n_nationkey, n_name, rank() OVER w1 AS rnk +FROM nation +WINDOW + w1 AS (PARTITION BY n_regionkey ORDER BY n_nationkey), + w2 AS (ORDER BY n_name) +ORDER BY n_regionkey, n_nationkey +-- !query analysis +Project [n_nationkey#x, n_name#x, rnk#x] ++- Sort [n_regionkey#x ASC NULLS FIRST, n_nationkey#x ASC NULLS FIRST], true + +- Project [n_nationkey#x, n_name#x, rnk#x, n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x, rnk#x, rnk#x] + +- Window [rank(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rnk#x], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +WITH cte AS ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) +SELECT * FROM cte +ORDER BY n_nationkey +-- !query analysis +WithCTE +:- CTERelationDef xxxx, false +: +- SubqueryAlias cte +: +- Project [n_nationkey#x, s#xL] +: +- Project [n_nationkey#x, s#xL, s#xL] +: +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] +: +- Project [n_nationkey#x] +: +- SubqueryAlias nation +: +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) +: +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] +: +- Project [n_nationkey#x, n_name#x, n_regionkey#x] +: +- SubqueryAlias nation +: +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] ++- Sort [n_nationkey#x ASC NULLS FIRST], true + +- Project [n_nationkey#x, s#xL] + +- SubqueryAlias cte + +- CTERelationRef xxxx, true, [n_nationkey#x, s#xL], false, false + + +-- !query +SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, s#xL] + +- SubqueryAlias sub + +- Project [n_nationkey#x, s#xL] + +- Project [n_nationkey#x, s#xL, s#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT * FROM ( + SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) + ) inner_sub +) outer_sub +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, s#xL] + +- SubqueryAlias outer_sub + +- Project [n_nationkey#x, s#xL] + +- SubqueryAlias inner_sub + +- Project [n_nationkey#x, s#xL] + +- Project [n_nationkey#x, s#xL, s#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT n_nationkey, n_regionkey, inner_sum, sum(n_nationkey) OVER w AS outer_sum +FROM ( + SELECT n_nationkey, n_regionkey, sum(n_nationkey) OVER w AS inner_sum + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query analysis +Sort [n_regionkey#x ASC NULLS FIRST, n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, n_regionkey#x, inner_sum#xL, outer_sum#xL] + +- Project [n_nationkey#x, n_regionkey#x, inner_sum#xL, outer_sum#xL, outer_sum#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS outer_sum#xL], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_regionkey#x, inner_sum#xL] + +- SubqueryAlias sub + +- Project [n_nationkey#x, n_regionkey#x, inner_sum#xL] + +- Project [n_nationkey#x, n_regionkey#x, inner_sum#xL, inner_sum#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS inner_sum#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_regionkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +WITH cte AS ( + SELECT sum(n_nationkey) OVER w AS s FROM nation +) +SELECT * FROM cte +WINDOW w AS (ORDER BY n_nationkey) +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT * +FROM (SELECT sum(n_nationkey) OVER w AS s FROM nation) sub +WINDOW w AS (ORDER BY n_nationkey) +-- !query analysis +Project [s#xL] ++- SubqueryAlias sub + +- Project [s#xL] + +- Project [n_nationkey#x, s#xL, s#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x] + +- SubqueryAlias nation + +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias nation + +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + + +-- !query +SELECT (SELECT sum(n_regionkey) OVER w FROM nation) FROM (SELECT 1) WINDOW w AS (ORDER BY n_nationkey) +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n_nationkey, + (SELECT sum(r_regionkey) OVER w FROM region WHERE r_regionkey = n_regionkey) AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n.n_regionkey, n.n_nationkey, n.n_name, + sum(n.n_nationkey) OVER w AS regional_sum +FROM nation n JOIN region r ON n.n_regionkey = r.r_regionkey +WINDOW w AS (PARTITION BY n.n_regionkey ORDER BY n.n_nationkey) +ORDER BY n.n_regionkey, n.n_nationkey +-- !query analysis +Sort [n_regionkey#x ASC NULLS FIRST, n_nationkey#x ASC NULLS FIRST], true ++- Project [n_regionkey#x, n_nationkey#x, n_name#x, regional_sum#xL] + +- Project [n_regionkey#x, n_nationkey#x, n_name#x, regional_sum#xL, regional_sum#xL] + +- Window [sum(n_nationkey#x) windowspecdefinition(n_regionkey#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS regional_sum#xL], [n_regionkey#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_regionkey#x, n_nationkey#x, n_name#x] + +- Join Inner, (n_regionkey#x = r_regionkey#x) + :- SubqueryAlias n + : +- SubqueryAlias nation + : +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + : +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + : +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + : +- SubqueryAlias nation + : +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias r + +- SubqueryAlias region + +- View (`region`, [r_regionkey#x, r_name#x]) + +- Project [cast(r_regionkey#x as int) AS r_regionkey#x, cast(r_name#x as string) AS r_name#x] + +- Project [r_regionkey#x, r_name#x] + +- SubqueryAlias region + +- LocalRelation [r_regionkey#x, r_name#x] + + +-- !query +SELECT n_nationkey, n_name, r_name, + count(*) OVER w AS cnt, + rank() OVER w AS rnk +FROM nation JOIN region ON n_regionkey = r_regionkey +WINDOW w AS (PARTITION BY r_name ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST], true ++- Project [n_nationkey#x, n_name#x, r_name#x, cnt#xL, rnk#x] + +- Project [n_nationkey#x, n_name#x, r_name#x, cnt#xL, rnk#x, cnt#xL, rnk#x] + +- Window [count(1) windowspecdefinition(r_name#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS cnt#xL, rank(n_nationkey#x) windowspecdefinition(r_name#x, n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rnk#x], [r_name#x], [n_nationkey#x ASC NULLS FIRST] + +- Project [n_nationkey#x, n_name#x, r_name#x] + +- Join Inner, (n_regionkey#x = r_regionkey#x) + :- SubqueryAlias nation + : +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + : +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + : +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + : +- SubqueryAlias nation + : +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + +- SubqueryAlias region + +- View (`region`, [r_regionkey#x, r_name#x]) + +- Project [cast(r_regionkey#x as int) AS r_regionkey#x, cast(r_name#x as string) AS r_name#x] + +- Project [r_regionkey#x, r_name#x] + +- SubqueryAlias region + +- LocalRelation [r_regionkey#x, r_name#x] + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +UNION ALL +SELECT r_regionkey, sum(r_regionkey) OVER w AS s +FROM region +WINDOW w AS (ORDER BY r_regionkey RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +ORDER BY 1, 2 +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST, s#xL ASC NULLS FIRST], true ++- Union false, false + :- Project [n_nationkey#x, s#xL] + : +- Project [n_nationkey#x, s#xL, s#xL] + : +- Window [sum(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [n_nationkey#x ASC NULLS FIRST] + : +- Project [n_nationkey#x] + : +- SubqueryAlias nation + : +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + : +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + : +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + : +- SubqueryAlias nation + : +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + +- Project [r_regionkey#x, s#xL] + +- Project [r_regionkey#x, s#xL, s#xL] + +- Window [sum(r_regionkey#x) windowspecdefinition(r_regionkey#x ASC NULLS FIRST, specifiedwindowframe(RangeFrame, unboundedpreceding$(), currentrow$())) AS s#xL], [r_regionkey#x ASC NULLS FIRST] + +- Project [r_regionkey#x] + +- SubqueryAlias region + +- View (`region`, [r_regionkey#x, r_name#x]) + +- Project [cast(r_regionkey#x as int) AS r_regionkey#x, cast(r_name#x as string) AS r_name#x] + +- Project [r_regionkey#x, r_name#x] + +- SubqueryAlias region + +- LocalRelation [r_regionkey#x, r_name#x] + + +-- !query +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER (ORDER BY r_regionkey) AS rk +FROM region +ORDER BY 1, 2 +-- !query analysis +Sort [n_nationkey#x ASC NULLS FIRST, rk#x ASC NULLS FIRST], true ++- Union false, false + :- Project [n_nationkey#x, rk#x] + : +- Project [n_nationkey#x, rk#x, rk#x] + : +- Window [rank(n_nationkey#x) windowspecdefinition(n_nationkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rk#x], [n_nationkey#x ASC NULLS FIRST] + : +- Project [n_nationkey#x] + : +- SubqueryAlias nation + : +- View (`nation`, [n_nationkey#x, n_name#x, n_regionkey#x]) + : +- Project [cast(n_nationkey#x as int) AS n_nationkey#x, cast(n_name#x as string) AS n_name#x, cast(n_regionkey#x as int) AS n_regionkey#x] + : +- Project [n_nationkey#x, n_name#x, n_regionkey#x] + : +- SubqueryAlias nation + : +- LocalRelation [n_nationkey#x, n_name#x, n_regionkey#x] + +- Project [r_regionkey#x, rk#x] + +- Project [r_regionkey#x, rk#x, rk#x] + +- Window [rank(r_regionkey#x) windowspecdefinition(r_regionkey#x ASC NULLS FIRST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rk#x], [r_regionkey#x ASC NULLS FIRST] + +- Project [r_regionkey#x] + +- SubqueryAlias region + +- View (`region`, [r_regionkey#x, r_name#x]) + +- Project [cast(r_regionkey#x as int) AS r_regionkey#x, cast(r_name#x as string) AS r_name#x] + +- Project [r_regionkey#x, r_name#x] + +- SubqueryAlias region + +- LocalRelation [r_regionkey#x, r_name#x] + + +-- !query +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER w AS rk +FROM region +ORDER BY 1 +-- !query analysis +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} diff --git a/sql/core/src/test/resources/sql-tests/inputs/named-window.sql b/sql/core/src/test/resources/sql-tests/inputs/named-window.sql new file mode 100644 index 000000000000..e60f24e5808b --- /dev/null +++ b/sql/core/src/test/resources/sql-tests/inputs/named-window.sql @@ -0,0 +1,207 @@ +-- Tests for named window references (WINDOW clause). + +-- Test data: simplified TPC-H nation and region tables. +CREATE OR REPLACE TEMPORARY VIEW nation AS SELECT * FROM VALUES +(0, 'ALGERIA', 0), +(1, 'ARGENTINA', 1), +(2, 'BRAZIL', 1), +(3, 'CANADA', 1), +(4, 'EGYPT', 4), +(5, 'ETHIOPIA', 0), +(6, 'FRANCE', 3), +(7, 'GERMANY', 3), +(8, 'INDIA', 2), +(9, 'INDONESIA', 2), +(10, 'IRAN', 4), +(11, 'IRAQ', 4), +(12, 'JAPAN', 2), +(13, 'JORDAN', 4), +(14, 'KENYA', 0), +(15, 'MOROCCO', 0), +(16, 'MOZAMBIQUE', 0), +(17, 'PERU', 1), +(18, 'CHINA', 2), +(19, 'ROMANIA', 3), +(20, 'SAUDI ARABIA', 4), +(21, 'VIETNAM', 2), +(22, 'RUSSIA', 3), +(23, 'UNITED KINGDOM', 3), +(24, 'UNITED STATES', 1) +AS nation(n_nationkey, n_name, n_regionkey); + +CREATE OR REPLACE TEMPORARY VIEW region AS SELECT * FROM VALUES +(0, 'AFRICA'), +(1, 'AMERICA'), +(2, 'ASIA'), +(3, 'EUROPE'), +(4, 'MIDDLE EAST') +AS region(r_regionkey, r_name); + +-- Basic named window reference: OVER w with WINDOW clause. +SELECT n_nationkey, n_name, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey; + +-- Named window reference with PARTITION BY. +SELECT n_regionkey, n_nationkey, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey; + +-- Multiple window functions referencing the same named window. +SELECT n_nationkey, n_name, + sum(n_nationkey) OVER w AS total, + rank() OVER w AS rnk +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey; + +-- Multiple named windows with different specs, each used by a different function. +SELECT n_regionkey, n_nationkey, + sum(n_nationkey) OVER w1 AS running_sum, + rank() OVER w2 AS rgn_rank +FROM nation +WINDOW + w1 AS (ORDER BY n_nationkey), + w2 AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_nationkey; + +-- Forward reference: w1 references w2 which is defined after it. +SELECT n_nationkey, sum(n_nationkey) OVER w1 AS s +FROM nation +WINDOW w1 AS w2, w2 AS (ORDER BY n_nationkey) +ORDER BY n_nationkey; + +-- Backward reference: w2 references w1 which is defined before it. +SELECT n_nationkey, sum(n_nationkey) OVER w2 AS s +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1 +ORDER BY n_nationkey; + +-- Multiple windows aliasing the same inline spec: w2 and w3 both reference w1. +SELECT n_nationkey, + sum(n_nationkey) OVER w2 AS s2, + rank() OVER w3 AS r3 +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w1 +ORDER BY n_nationkey; + +-- Circular reference: w1 AS w2, w2 AS w1 -- neither resolves to an inline spec. +SELECT sum(n_nationkey) OVER w1 FROM nation WINDOW w1 AS w2, w2 AS w1; + +-- Multi-level indirection: w3 references w2 which references w1 (error: ref to ref). +SELECT sum(n_nationkey) OVER w3 FROM nation WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w2; + +-- OVER w without a WINDOW clause should produce an error. +SELECT sum(n_nationkey) OVER w FROM nation; + +-- A window defined in the WINDOW clause but not referenced should be silently accepted. +SELECT n_nationkey, n_name, rank() OVER w1 AS rnk +FROM nation +WINDOW + w1 AS (PARTITION BY n_regionkey ORDER BY n_nationkey), + w2 AS (ORDER BY n_name) +ORDER BY n_regionkey, n_nationkey; + +-- Window definition inside a CTE body is local to the CTE and works correctly. +WITH cte AS ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) +SELECT * FROM cte +ORDER BY n_nationkey; + +-- Named window inside a FROM subquery works independently of the outer query. +SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +ORDER BY n_nationkey; + +-- Nested subqueries: only the innermost FROM subquery defines WINDOW w. +SELECT * FROM ( + SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) + ) inner_sub +) outer_sub +ORDER BY n_nationkey; + +-- Same window name 'w' independently defined in outer query and FROM subquery. +SELECT n_nationkey, n_regionkey, inner_sum, sum(n_nationkey) OVER w AS outer_sum +FROM ( + SELECT n_nationkey, n_regionkey, sum(n_nationkey) OVER w AS inner_sum + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey; + +-- Outer WINDOW clause is not visible inside a CTE body (error). +WITH cte AS ( + SELECT sum(n_nationkey) OVER w AS s FROM nation +) +SELECT * FROM cte +WINDOW w AS (ORDER BY n_nationkey); + +-- Outer WINDOW clause is not visible inside a FROM subquery (error). +SELECT * +FROM (SELECT sum(n_nationkey) OVER w AS s FROM nation) sub +WINDOW w AS (ORDER BY n_nationkey); + +-- Named window definitions are not visible inside scalar subqueries (error). +SELECT (SELECT sum(n_regionkey) OVER w FROM nation) FROM (SELECT 1) WINDOW w AS (ORDER BY n_nationkey); + +-- Outer WINDOW w is not visible inside a correlated scalar subquery (error). +SELECT n_nationkey, + (SELECT sum(r_regionkey) OVER w FROM region WHERE r_regionkey = n_regionkey) AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey; + +-- Named window used in a query with a JOIN. +SELECT n.n_regionkey, n.n_nationkey, n.n_name, + sum(n.n_nationkey) OVER w AS regional_sum +FROM nation n JOIN region r ON n.n_regionkey = r.r_regionkey +WINDOW w AS (PARTITION BY n.n_regionkey ORDER BY n.n_nationkey) +ORDER BY n.n_regionkey, n.n_nationkey; + +-- Multiple window functions over a named window using columns from both sides of a JOIN. +SELECT n_nationkey, n_name, r_name, + count(*) OVER w AS cnt, + rank() OVER w AS rnk +FROM nation JOIN region ON n_regionkey = r_regionkey +WINDOW w AS (PARTITION BY r_name ORDER BY n_nationkey) +ORDER BY n_nationkey; + +-- Both UNION branches define WINDOW w with different specs; each resolves independently. +SELECT n_nationkey, sum(n_nationkey) OVER w AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +UNION ALL +SELECT r_regionkey, sum(r_regionkey) OVER w AS s +FROM region +WINDOW w AS (ORDER BY r_regionkey RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +ORDER BY 1, 2; + +-- One UNION branch uses a named WINDOW w, the other uses an equivalent inline spec. +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER (ORDER BY r_regionkey) AS rk +FROM region +ORDER BY 1, 2; + +-- Only one UNION branch defines WINDOW w; the other references w without defining it (error). +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER w AS rk +FROM region +ORDER BY 1; diff --git a/sql/core/src/test/resources/sql-tests/results/named-window.sql.out b/sql/core/src/test/resources/sql-tests/results/named-window.sql.out new file mode 100644 index 000000000000..6df530641824 --- /dev/null +++ b/sql/core/src/test/resources/sql-tests/results/named-window.sql.out @@ -0,0 +1,822 @@ +-- Automatically generated by SQLQueryTestSuite +-- !query +CREATE OR REPLACE TEMPORARY VIEW nation AS SELECT * FROM VALUES +(0, 'ALGERIA', 0), +(1, 'ARGENTINA', 1), +(2, 'BRAZIL', 1), +(3, 'CANADA', 1), +(4, 'EGYPT', 4), +(5, 'ETHIOPIA', 0), +(6, 'FRANCE', 3), +(7, 'GERMANY', 3), +(8, 'INDIA', 2), +(9, 'INDONESIA', 2), +(10, 'IRAN', 4), +(11, 'IRAQ', 4), +(12, 'JAPAN', 2), +(13, 'JORDAN', 4), +(14, 'KENYA', 0), +(15, 'MOROCCO', 0), +(16, 'MOZAMBIQUE', 0), +(17, 'PERU', 1), +(18, 'CHINA', 2), +(19, 'ROMANIA', 3), +(20, 'SAUDI ARABIA', 4), +(21, 'VIETNAM', 2), +(22, 'RUSSIA', 3), +(23, 'UNITED KINGDOM', 3), +(24, 'UNITED STATES', 1) +AS nation(n_nationkey, n_name, n_regionkey) +-- !query schema +struct<> +-- !query output + + + +-- !query +CREATE OR REPLACE TEMPORARY VIEW region AS SELECT * FROM VALUES +(0, 'AFRICA'), +(1, 'AMERICA'), +(2, 'ASIA'), +(3, 'EUROPE'), +(4, 'MIDDLE EAST') +AS region(r_regionkey, r_name) +-- !query schema +struct<> +-- !query output + + + +-- !query +SELECT n_nationkey, n_name, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 ALGERIA 0 +1 ARGENTINA 1 +2 BRAZIL 3 +3 CANADA 6 +4 EGYPT 10 +5 ETHIOPIA 15 +6 FRANCE 21 +7 GERMANY 28 +8 INDIA 36 +9 INDONESIA 45 +10 IRAN 55 +11 IRAQ 66 +12 JAPAN 78 +13 JORDAN 91 +14 KENYA 105 +15 MOROCCO 120 +16 MOZAMBIQUE 136 +17 PERU 153 +18 CHINA 171 +19 ROMANIA 190 +20 SAUDI ARABIA 210 +21 VIETNAM 231 +22 RUSSIA 253 +23 UNITED KINGDOM 276 +24 UNITED STATES 300 + + +-- !query +SELECT n_regionkey, n_nationkey, sum(n_nationkey) OVER w AS ws +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query schema +struct +-- !query output +0 0 0 +0 5 5 +0 14 19 +0 15 34 +0 16 50 +1 1 1 +1 2 3 +1 3 6 +1 17 23 +1 24 47 +2 8 8 +2 9 17 +2 12 29 +2 18 47 +2 21 68 +3 6 6 +3 7 13 +3 19 32 +3 22 54 +3 23 77 +4 4 4 +4 10 14 +4 11 25 +4 13 38 +4 20 58 + + +-- !query +SELECT n_nationkey, n_name, + sum(n_nationkey) OVER w AS total, + rank() OVER w AS rnk +FROM nation +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query schema +struct +-- !query output +0 ALGERIA 0 1 +5 ETHIOPIA 5 2 +14 KENYA 19 3 +15 MOROCCO 34 4 +16 MOZAMBIQUE 50 5 +1 ARGENTINA 1 1 +2 BRAZIL 3 2 +3 CANADA 6 3 +17 PERU 23 4 +24 UNITED STATES 47 5 +8 INDIA 8 1 +9 INDONESIA 17 2 +12 JAPAN 29 3 +18 CHINA 47 4 +21 VIETNAM 68 5 +6 FRANCE 6 1 +7 GERMANY 13 2 +19 ROMANIA 32 3 +22 RUSSIA 54 4 +23 UNITED KINGDOM 77 5 +4 EGYPT 4 1 +10 IRAN 14 2 +11 IRAQ 25 3 +13 JORDAN 38 4 +20 SAUDI ARABIA 58 5 + + +-- !query +SELECT n_regionkey, n_nationkey, + sum(n_nationkey) OVER w1 AS running_sum, + rank() OVER w2 AS rgn_rank +FROM nation +WINDOW + w1 AS (ORDER BY n_nationkey), + w2 AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 0 1 +1 1 1 1 +1 2 3 2 +1 3 6 3 +4 4 10 1 +0 5 15 2 +3 6 21 1 +3 7 28 2 +2 8 36 1 +2 9 45 2 +4 10 55 2 +4 11 66 3 +2 12 78 3 +4 13 91 4 +0 14 105 3 +0 15 120 4 +0 16 136 5 +1 17 153 4 +2 18 171 4 +3 19 190 3 +4 20 210 5 +2 21 231 5 +3 22 253 4 +3 23 276 5 +1 24 300 5 + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w1 AS s +FROM nation +WINDOW w1 AS w2, w2 AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 +1 1 +2 3 +3 6 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w2 AS s +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1 +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 +1 1 +2 3 +3 6 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT n_nationkey, + sum(n_nationkey) OVER w2 AS s2, + rank() OVER w3 AS r3 +FROM nation +WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w1 +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 1 +1 1 2 +2 3 3 +3 6 4 +4 10 5 +5 15 6 +6 21 7 +7 28 8 +8 36 9 +9 45 10 +10 55 11 +11 66 12 +12 78 13 +13 91 14 +14 105 15 +15 120 16 +16 136 17 +17 153 18 +18 171 19 +19 190 20 +20 210 21 +21 231 22 +22 253 23 +23 276 24 +24 300 25 + + +-- !query +SELECT sum(n_nationkey) OVER w1 FROM nation WINDOW w1 AS w2, w2 AS w1 +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.parser.ParseException +{ + "errorClass" : "INVALID_SQL_SYNTAX.INVALID_WINDOW_REFERENCE", + "sqlState" : "42000", + "messageParameters" : { + "windowName" : "`w2`" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 45, + "stopIndex" : 69, + "fragment" : "WINDOW w1 AS w2, w2 AS w1" + } ] +} + + +-- !query +SELECT sum(n_nationkey) OVER w3 FROM nation WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w2 +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.parser.ParseException +{ + "errorClass" : "INVALID_SQL_SYNTAX.INVALID_WINDOW_REFERENCE", + "sqlState" : "42000", + "messageParameters" : { + "windowName" : "`w2`" + }, + "queryContext" : [ { + "objectType" : "", + "objectName" : "", + "startIndex" : 45, + "stopIndex" : 99, + "fragment" : "WINDOW w1 AS (ORDER BY n_nationkey), w2 AS w1, w3 AS w2" + } ] +} + + +-- !query +SELECT sum(n_nationkey) OVER w FROM nation +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n_nationkey, n_name, rank() OVER w1 AS rnk +FROM nation +WINDOW + w1 AS (PARTITION BY n_regionkey ORDER BY n_nationkey), + w2 AS (ORDER BY n_name) +ORDER BY n_regionkey, n_nationkey +-- !query schema +struct +-- !query output +0 ALGERIA 1 +5 ETHIOPIA 2 +14 KENYA 3 +15 MOROCCO 4 +16 MOZAMBIQUE 5 +1 ARGENTINA 1 +2 BRAZIL 2 +3 CANADA 3 +17 PERU 4 +24 UNITED STATES 5 +8 INDIA 1 +9 INDONESIA 2 +12 JAPAN 3 +18 CHINA 4 +21 VIETNAM 5 +6 FRANCE 1 +7 GERMANY 2 +19 ROMANIA 3 +22 RUSSIA 4 +23 UNITED KINGDOM 5 +4 EGYPT 1 +10 IRAN 2 +11 IRAQ 3 +13 JORDAN 4 +20 SAUDI ARABIA 5 + + +-- !query +WITH cte AS ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) +SELECT * FROM cte +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 +1 1 +2 3 +3 6 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 +1 1 +2 3 +3 6 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT * FROM ( + SELECT * FROM ( + SELECT n_nationkey, sum(n_nationkey) OVER w AS s + FROM nation + WINDOW w AS (ORDER BY n_nationkey) + ) inner_sub +) outer_sub +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 0 +1 1 +2 3 +3 6 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT n_nationkey, n_regionkey, inner_sum, sum(n_nationkey) OVER w AS outer_sum +FROM ( + SELECT n_nationkey, n_regionkey, sum(n_nationkey) OVER w AS inner_sum + FROM nation + WINDOW w AS (ORDER BY n_nationkey) +) sub +WINDOW w AS (PARTITION BY n_regionkey ORDER BY n_nationkey) +ORDER BY n_regionkey, n_nationkey +-- !query schema +struct +-- !query output +0 0 0 0 +5 0 15 5 +14 0 105 19 +15 0 120 34 +16 0 136 50 +1 1 1 1 +2 1 3 3 +3 1 6 6 +17 1 153 23 +24 1 300 47 +8 2 36 8 +9 2 45 17 +12 2 78 29 +18 2 171 47 +21 2 231 68 +6 3 21 6 +7 3 28 13 +19 3 190 32 +22 3 253 54 +23 3 276 77 +4 4 10 4 +10 4 55 14 +11 4 66 25 +13 4 91 38 +20 4 210 58 + + +-- !query +WITH cte AS ( + SELECT sum(n_nationkey) OVER w AS s FROM nation +) +SELECT * FROM cte +WINDOW w AS (ORDER BY n_nationkey) +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT * +FROM (SELECT sum(n_nationkey) OVER w AS s FROM nation) sub +WINDOW w AS (ORDER BY n_nationkey) +-- !query schema +struct +-- !query output +0 +1 +10 +105 +120 +136 +15 +153 +171 +190 +21 +210 +231 +253 +276 +28 +3 +300 +36 +45 +55 +6 +66 +78 +91 + + +-- !query +SELECT (SELECT sum(n_regionkey) OVER w FROM nation) FROM (SELECT 1) WINDOW w AS (ORDER BY n_nationkey) +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n_nationkey, + (SELECT sum(r_regionkey) OVER w FROM region WHERE r_regionkey = n_regionkey) AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +} + + +-- !query +SELECT n.n_regionkey, n.n_nationkey, n.n_name, + sum(n.n_nationkey) OVER w AS regional_sum +FROM nation n JOIN region r ON n.n_regionkey = r.r_regionkey +WINDOW w AS (PARTITION BY n.n_regionkey ORDER BY n.n_nationkey) +ORDER BY n.n_regionkey, n.n_nationkey +-- !query schema +struct +-- !query output +0 0 ALGERIA 0 +0 5 ETHIOPIA 5 +0 14 KENYA 19 +0 15 MOROCCO 34 +0 16 MOZAMBIQUE 50 +1 1 ARGENTINA 1 +1 2 BRAZIL 3 +1 3 CANADA 6 +1 17 PERU 23 +1 24 UNITED STATES 47 +2 8 INDIA 8 +2 9 INDONESIA 17 +2 12 JAPAN 29 +2 18 CHINA 47 +2 21 VIETNAM 68 +3 6 FRANCE 6 +3 7 GERMANY 13 +3 19 ROMANIA 32 +3 22 RUSSIA 54 +3 23 UNITED KINGDOM 77 +4 4 EGYPT 4 +4 10 IRAN 14 +4 11 IRAQ 25 +4 13 JORDAN 38 +4 20 SAUDI ARABIA 58 + + +-- !query +SELECT n_nationkey, n_name, r_name, + count(*) OVER w AS cnt, + rank() OVER w AS rnk +FROM nation JOIN region ON n_regionkey = r_regionkey +WINDOW w AS (PARTITION BY r_name ORDER BY n_nationkey) +ORDER BY n_nationkey +-- !query schema +struct +-- !query output +0 ALGERIA AFRICA 1 1 +1 ARGENTINA AMERICA 1 1 +2 BRAZIL AMERICA 2 2 +3 CANADA AMERICA 3 3 +4 EGYPT MIDDLE EAST 1 1 +5 ETHIOPIA AFRICA 2 2 +6 FRANCE EUROPE 1 1 +7 GERMANY EUROPE 2 2 +8 INDIA ASIA 1 1 +9 INDONESIA ASIA 2 2 +10 IRAN MIDDLE EAST 2 2 +11 IRAQ MIDDLE EAST 3 3 +12 JAPAN ASIA 3 3 +13 JORDAN MIDDLE EAST 4 4 +14 KENYA AFRICA 3 3 +15 MOROCCO AFRICA 4 4 +16 MOZAMBIQUE AFRICA 5 5 +17 PERU AMERICA 4 4 +18 CHINA ASIA 4 4 +19 ROMANIA EUROPE 3 3 +20 SAUDI ARABIA MIDDLE EAST 5 5 +21 VIETNAM ASIA 5 5 +22 RUSSIA EUROPE 4 4 +23 UNITED KINGDOM EUROPE 5 5 +24 UNITED STATES AMERICA 5 5 + + +-- !query +SELECT n_nationkey, sum(n_nationkey) OVER w AS s +FROM nation +WINDOW w AS (ORDER BY n_nationkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +UNION ALL +SELECT r_regionkey, sum(r_regionkey) OVER w AS s +FROM region +WINDOW w AS (ORDER BY r_regionkey RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) +ORDER BY 1, 2 +-- !query schema +struct +-- !query output +0 0 +0 0 +1 1 +1 1 +2 3 +2 3 +3 6 +3 6 +4 10 +4 10 +5 15 +6 21 +7 28 +8 36 +9 45 +10 55 +11 66 +12 78 +13 91 +14 105 +15 120 +16 136 +17 153 +18 171 +19 190 +20 210 +21 231 +22 253 +23 276 +24 300 + + +-- !query +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER (ORDER BY r_regionkey) AS rk +FROM region +ORDER BY 1, 2 +-- !query schema +struct +-- !query output +0 1 +0 1 +1 2 +1 2 +2 3 +2 3 +3 4 +3 4 +4 5 +4 5 +5 6 +6 7 +7 8 +8 9 +9 10 +10 11 +11 12 +12 13 +13 14 +14 15 +15 16 +16 17 +17 18 +18 19 +19 20 +20 21 +21 22 +22 23 +23 24 +24 25 + + +-- !query +SELECT n_nationkey, rank() OVER w AS rk +FROM nation +WINDOW w AS (ORDER BY n_nationkey) +UNION ALL +SELECT r_regionkey, rank() OVER w AS rk +FROM region +ORDER BY 1 +-- !query schema +struct<> +-- !query output +org.apache.spark.sql.catalyst.ExtendedAnalysisException +{ + "errorClass" : "MISSING_WINDOW_SPECIFICATION", + "sqlState" : "42P20", + "messageParameters" : { + "docroot" : "https://spark.apache.org/docs/latest", + "windowName" : "w" + } +}