Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,15 @@ class Analyzer(
case other => Alias(other, other.toString)()
}

val nonNullBitmask = x.bitmasks.reduce(_ & _)
// The rightmost bit in the bitmasks corresponds to the last expression in groupByAliases
// with 0 indicating this expression is in the grouping set. The following line of code
// calculates the bitmask representing the expressions that absent in at least one grouping
// set (indicated by 1).
val nullBitmask = x.bitmasks.reduce(_ | _)

val attrLength = groupByAliases.length
val expandedAttributes = groupByAliases.zipWithIndex.map { case (a, idx) =>
a.toAttribute.withNullability((nonNullBitmask & 1 << idx) == 0)
a.toAttribute.withNullability(((nullBitmask >> (attrLength - idx - 1)) & 1) == 1)
}

val expand = Expand(x.bitmasks, groupByAliases, expandedAttributes, gid, x.child)
Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/grouping_set.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TEMPORARY VIEW grouping AS SELECT * FROM VALUES
("1", "2", "3", 1),
("4", "5", "6", 1),
("7", "8", "9", 1)
as grouping(a, b, c, d);

-- SPARK-17849: grouping set throws NPE #1
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS (());

-- SPARK-17849: grouping set throws NPE #2
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS ((a));

-- SPARK-17849: grouping set throws NPE #3
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS ((c));



42 changes: 42 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/grouping_set.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 4


-- !query 0
CREATE TEMPORARY VIEW grouping AS SELECT * FROM VALUES
("1", "2", "3", 1),
("4", "5", "6", 1),
("7", "8", "9", 1)
as grouping(a, b, c, d)
-- !query 0 schema
struct<>
-- !query 0 output



-- !query 1
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS (())
-- !query 1 schema
struct<a:string,b:string,c:string,count(d):bigint>
-- !query 1 output
NULL NULL NULL 3


-- !query 2
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS ((a))
-- !query 2 schema
struct<a:string,b:string,c:string,count(d):bigint>
-- !query 2 output
1 NULL NULL 1
4 NULL NULL 1
7 NULL NULL 1


-- !query 3
SELECT a, b, c, count(d) FROM grouping GROUP BY a, b, c GROUPING SETS ((c))
-- !query 3 schema
struct<a:string,b:string,c:string,count(d):bigint>
-- !query 3 output
NULL NULL 3 1
NULL NULL 6 1
NULL NULL 9 1