Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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,14 @@ class Analyzer(
case other => Alias(other, other.toString)()
}

val nonNullBitmask = x.bitmasks.reduce(_ & _)
// The left most 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 bit mask representing the expressions that exist in all the grouping sets.
val nonNullBitmask = ~ x.bitmasks.reduce(_ | _)

@hvanhovell hvanhovell Oct 10, 2016

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit manipulation magic is hard to follow. This is should be documented better. Could you add a line or two to explain how the bitmasks are structured?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll do it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hvanhovell @rxin comments are added

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the '~' here, and use (nonNullBitmask & (1 << (attrLength - idx - 1))) == 1?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean ((nonNullBitmask >> (attrLength - idx - 1)) & 1) == 1? We can only test on 0 if we left shift 1, right? @davies


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

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