-
Notifications
You must be signed in to change notification settings - Fork 215
[Feature] percentfield and showperc with top and rare commands.
#5642
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
base: main
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 |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ public enum CommandType { | |
| public enum Option { | ||
| countField, | ||
| showCount, | ||
| percentField, | ||
| showPerc, | ||
| useNull, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,6 +223,8 @@ public class CalciteRelNodeVisitor extends AbstractNodeVisitor<RelNode, CalciteP | |
| private final MapPathPreMaterializer mapPathMaterializer; | ||
| private final ForeachPlanner foreachPlanner; | ||
|
|
||
| private static final int PERCENT_DECIMAL_PLACES = 6; | ||
|
|
||
| public CalciteRelNodeVisitor(DataSourceService dataSourceService) { | ||
| this.rexVisitor = new CalciteRexNodeVisitor(this); | ||
| this.aggVisitor = new CalciteAggCallVisitor(rexVisitor); | ||
|
|
@@ -3264,6 +3266,45 @@ public RelNode visitRareTopN(RareTopN node, CalcitePlanContext context) { | |
| orderKeys.add(countField); | ||
| orderKeys.addAll(tieBreakKeys); | ||
|
|
||
| // 3. compute percentage if showperc=true | ||
| Boolean showPerc = (Boolean) argumentMap.get(RareTopN.Option.showPerc.name()).getValue(); | ||
| if (showPerc) { | ||
| String percentFieldName = | ||
| (String) argumentMap.get(RareTopN.Option.percentField.name()).getValue(); | ||
|
|
||
| if (context.relBuilder.peek().getRowType().getFieldNames().contains(percentFieldName)) { | ||
| throw new IllegalArgumentException( | ||
| "Field `" | ||
| + percentFieldName | ||
| + "` already exists in the output. Consider renaming with percentfield='xyz'"); | ||
| } | ||
| RexNode totalWindowOver = | ||
|
AjimelecGonzalez marked this conversation as resolved.
|
||
| PlanUtils.makeOver( | ||
| context, | ||
| BuiltinFunctionName.SUM, | ||
| context.relBuilder.field(countFieldName), | ||
| List.of(), | ||
| partitionKeys, | ||
| List.of(), | ||
| WindowFrame.rowsUnbounded()); | ||
|
Collaborator
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. issue: This has a correctness issue for high-cardinality fields because the total is based on the returned buckets. The current impl works well for low-cardinality fields, but for high cardinality we get a truncated bucket set. I'm worried about long-tail types of top-N cases (e.g. "top 10 endpoints" from a service with many assets). In principle we should be using the If it's a lot to patch, I think we can leave this behavior, but should document the limitation. You can manually work around it by using a subquery or something like eventstats to work with
Contributor
Author
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. Will look into it and document, I believe that the using the |
||
|
|
||
| RexNode hundred = context.relBuilder.literal(new BigDecimal("100.0")); | ||
| RexNode countCast = | ||
| context.relBuilder.cast(context.relBuilder.field(countFieldName), SqlTypeName.DOUBLE); | ||
| RexNode totalCast = context.relBuilder.cast(totalWindowOver, SqlTypeName.DOUBLE); | ||
| RexNode numerator = context.relBuilder.call(SqlStdOperatorTable.MULTIPLY, hundred, countCast); | ||
| RexNode percValue = context.relBuilder.call(SqlStdOperatorTable.DIVIDE, numerator, totalCast); | ||
|
|
||
| // Round the percent value 6 decimal places | ||
| RexNode roundedPerc = | ||
| context.relBuilder.call( | ||
| SqlStdOperatorTable.ROUND, | ||
| percValue, | ||
| context.relBuilder.literal(PERCENT_DECIMAL_PLACES)); | ||
|
|
||
| context.relBuilder.projectPlus(context.relBuilder.alias(roundedPerc, percentFieldName)); | ||
| } | ||
|
|
||
| RexNode rowNumberWindowOver = | ||
| PlanUtils.makeOver( | ||
| context, | ||
|
|
@@ -3276,14 +3317,14 @@ public RelNode visitRareTopN(RareTopN node, CalcitePlanContext context) { | |
| context.relBuilder.projectPlus( | ||
| context.relBuilder.alias(rowNumberWindowOver, ROW_NUMBER_COLUMN_FOR_RARE_TOP)); | ||
|
|
||
| // 3. filter row_number() <= k in each partition | ||
| // 4. filter row_number() <= k in each partition | ||
| int k = node.getNoOfResults(); | ||
| context.relBuilder.filter( | ||
| context.relBuilder.lessThanOrEqual( | ||
| context.relBuilder.field(ROW_NUMBER_COLUMN_FOR_RARE_TOP), | ||
| context.relBuilder.literal(k))); | ||
|
|
||
| // 4. project final output. the default output is group by list + field list | ||
| // 5. project final output: group by list + field list, optionally count and percent | ||
| Boolean showCount = (Boolean) argumentMap.get(RareTopN.Option.showCount.name()).getValue(); | ||
| if (showCount) { | ||
| context.relBuilder.projectExcept(context.relBuilder.field(ROW_NUMBER_COLUMN_FOR_RARE_TOP)); | ||
|
|
||
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.
suggestion: Would it be better user experience if we provided a new percent field automatically?
e.g. generate
percent0,percent1, etc. Joins already do this with join IDs (id->id0, etc).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.
Sure, will remove the check.