-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Coral-Hive] Add missing Hive multi-dimensional aggregation functions: GROUPING_SE… #517
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
import javax.annotation.Nullable; | ||
|
||
import com.linkedin.coral.common.calcite.CalciteUtil; | ||
import org.apache.calcite.avatica.util.TimeUnit; | ||
import org.apache.calcite.sql.JoinConditionType; | ||
import org.apache.calcite.sql.JoinType; | ||
|
@@ -63,6 +64,7 @@ | |
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.lang.String.format; | ||
import static org.apache.calcite.sql.fun.SqlStdOperatorTable.*; | ||
import static org.apache.calcite.sql.parser.SqlParserPos.ZERO; | ||
|
||
|
||
|
@@ -487,6 +489,84 @@ protected SqlNode visitGroupBy(ASTNode node, ParseContext ctx) { | |
return ctx.grpBy; | ||
} | ||
|
||
|
||
|
||
@Override | ||
protected SqlNode visitGroupingSets(ASTNode node, ParseContext ctx){ | ||
// When Hive recognizes a grouping set, it omits the group by node in the constructed AST. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
// However, Calcite requires the grouping set to be within a group by node when building SqlNode. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is not very clear. You might depict the structure of the two trees using an example. |
||
// Therefore, we need to add a group by node. | ||
if(ctx.grpBy == null){ | ||
ctx.grpBy = new SqlNodeList(new ArrayList<SqlNode>(), ZERO); | ||
} | ||
|
||
List<SqlNode> groupingSets = visitChildren(node, ctx); | ||
List<SqlNode> operands = new ArrayList<>(); | ||
if (groupingSets.isEmpty()) { | ||
operands.add(CalciteUtil.createSqlNodeList(Collections.emptyList())); | ||
} else { | ||
// In Hive, fields used in GROUPING SETS must be declared after the GROUP BY clause. | ||
// However, when constructing Calcite SqlNode, the SqlIdentifier after GROUP BY isn't required; | ||
// only the child nodes of TOK_GROUPING_SETS are needed, so they are filtered out. | ||
List<SqlNode> operand = groupingSets.stream() | ||
.filter(f -> !(f instanceof SqlIdentifier)) | ||
.collect(Collectors.toList()); | ||
operands.add(new SqlNodeList(operand, ZERO)); | ||
} | ||
SqlNode groupingSetsNode = GROUPING_SETS.createCall(ZERO, operands); | ||
ctx.grpBy.add(groupingSetsNode); | ||
|
||
return groupingSetsNode; | ||
} | ||
|
||
@Override | ||
protected SqlNode visitGroupingSetsExpression(ASTNode node, ParseContext ctx){ | ||
List<SqlNode> identifiers = visitChildren(node, ctx); | ||
if (identifiers == null || identifiers.isEmpty()){ | ||
return CalciteUtil.createSqlNodeList(Collections.emptyList()); | ||
}else{ | ||
return ROW.createCall(ZERO, new SqlNodeList(identifiers, ZERO)); | ||
} | ||
} | ||
|
||
@Override | ||
protected SqlNode visitCubeGroupBy(ASTNode node, ParseContext ctx){ | ||
if(ctx.grpBy == null){ | ||
ctx.grpBy = new SqlNodeList(new ArrayList<SqlNode>(), ZERO); | ||
} | ||
Comment on lines
+534
to
+536
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a similar comment on the relationship between the two representations? Here and in |
||
|
||
List<SqlNode> cubeGroupby = visitChildren(node, ctx); | ||
List<SqlNode> operands = new ArrayList<>(); | ||
if (cubeGroupby.isEmpty()) { | ||
operands.add(CalciteUtil.createSqlNodeList(Collections.emptyList())); | ||
} else { | ||
operands.add(new SqlNodeList(cubeGroupby, ZERO)); | ||
} | ||
SqlNode cubeCall = CUBE.createCall(ZERO, operands); | ||
ctx.grpBy.add(cubeCall); | ||
|
||
return cubeCall; | ||
} | ||
|
||
@Override | ||
protected SqlNode visitRollUpGroupBy(ASTNode node, ParseContext ctx){ | ||
if(ctx.grpBy == null){ | ||
ctx.grpBy = new SqlNodeList(new ArrayList<SqlNode>(), ZERO); | ||
} | ||
|
||
List<SqlNode> rollupGroupby = visitChildren(node, ctx); | ||
List<SqlNode> operands = new ArrayList<>(); | ||
if (rollupGroupby.isEmpty()) { | ||
operands.add(CalciteUtil.createSqlNodeList(Collections.emptyList())); | ||
} else { | ||
operands.add(new SqlNodeList(rollupGroupby, ZERO)); | ||
} | ||
SqlNode rollupCall = ROLLUP.createCall(ZERO, operands); | ||
ctx.grpBy.add(rollupCall); | ||
|
||
return rollupCall; | ||
} | ||
|
||
@Override | ||
protected SqlNode visitOperator(ASTNode node, ParseContext ctx) { | ||
ArrayList<Node> children = node.getChildren(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let us remove this for consistency with the rest of the file.