Java Code Examples for org.apache.calcite.rel.core.Window#RexWinAggCall
The following examples show how to use
org.apache.calcite.rel.core.Window#RexWinAggCall .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrillReduceAggregatesRule.java From Bats with Apache License 2.0 | 5 votes |
@Override public boolean matches(RelOptRuleCall call) { final DrillWindowRel oldWinRel = (DrillWindowRel) call.rels[0]; for (Window.Group group : oldWinRel.groups) { for (Window.RexWinAggCall rexWinAggCall : group.aggCalls) { if (isConversionToSumZeroNeeded(rexWinAggCall.getOperator(), rexWinAggCall.getType())) { return true; } } } return false; }
Example 2
Source File: DrillReduceAggregatesRule.java From Bats with Apache License 2.0 | 4 votes |
@Override public void onMatch(RelOptRuleCall call) { final DrillWindowRel oldWinRel = (DrillWindowRel) call.rels[0]; final ImmutableList.Builder<Window.Group> builder = ImmutableList.builder(); for (Window.Group group : oldWinRel.groups) { final List<Window.RexWinAggCall> aggCalls = Lists.newArrayList(); for (Window.RexWinAggCall rexWinAggCall : group.aggCalls) { if (isConversionToSumZeroNeeded(rexWinAggCall.getOperator(), rexWinAggCall.getType())) { final RelDataType argType = rexWinAggCall.getType(); final RelDataType sumType = oldWinRel.getCluster().getTypeFactory() .createTypeWithNullability(argType, argType.isNullable()); final SqlAggFunction sumZeroAgg = new DrillCalciteSqlAggFunctionWrapper( new SqlSumEmptyIsZeroAggFunction(), sumType); final Window.RexWinAggCall sumZeroCall = RexBuilder.getRexFactory().makeWinAggCall( sumZeroAgg, sumType, rexWinAggCall.getOperands(), rexWinAggCall.getOrdinal(), rexWinAggCall.isDistinct()); aggCalls.add(sumZeroCall); } else { aggCalls.add(rexWinAggCall); } } final Window.Group newGroup = new Window.Group( group.keys, group.isRows, group.lowerBound, group.upperBound, group.orderKeys, aggCalls); builder.add(newGroup); } call.transformTo(new DrillWindowRel( oldWinRel.getCluster(), oldWinRel.getTraitSet(), oldWinRel.getInput(), oldWinRel.constants, oldWinRel.getRowType(), builder.build())); }
Example 3
Source File: WindowUtil.java From dremio-oss with Apache License 2.0 | 4 votes |
public static List<RexOver> getOver(Window w) { RelNode input = w.getInput(); int inputFieldCount = input.getRowType().getFieldCount(); final List<RexOver> rexOvers = new ArrayList<>(); for (Window.Group group : w.groups) { final List<RexNode> partitionKeys = new ArrayList<>(); final List<RexFieldCollation> orderKeys = new ArrayList<>(); // Convert RelFieldCollation to RexFieldCollation for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) { Set<SqlKind> directions = new HashSet<>(); if (collation.direction.isDescending()) { directions.add(SqlKind.DESCENDING); } if (collation.nullDirection == RelFieldCollation.NullDirection.LAST) { directions.add(SqlKind.NULLS_LAST); } else if (collation.nullDirection == RelFieldCollation.NullDirection.FIRST) { directions.add(SqlKind.NULLS_FIRST); } RexFieldCollation rexCollation = new RexFieldCollation(w.getCluster().getRexBuilder().makeInputRef(input, collation.getFieldIndex()), directions); orderKeys.add(rexCollation); } // Add partition keys for (int partition : group.keys) { partitionKeys.add(w.getCluster().getRexBuilder().makeInputRef(input, partition)); } // Create RexWindow RexWindow window = new RexWindow(partitionKeys, orderKeys, group.lowerBound, group.upperBound, group.isRows); // For each window agg call, create rex over for (Window.RexWinAggCall winAggCall : group.aggCalls) { RexShuttle replaceConstants = new RexShuttle() { @Override public RexNode visitInputRef(RexInputRef inputRef) { int index = inputRef.getIndex(); RexNode ref; if (index > inputFieldCount - 1) { ref = w.constants.get(index - inputFieldCount); } else { ref = inputRef; } return ref; } }; RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants); SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator(); RexOver over = new RexOver(winAggCall.getType(), aggFunction, aggCall.operands, window, winAggCall.distinct); rexOvers.add(over); } } return rexOvers; }
Example 4
Source File: SqlImplementor.java From dremio-oss with Apache License 2.0 | 4 votes |
public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constants, int inputFieldCount) { final List<SqlNode> rexOvers = new ArrayList<>(); final List<SqlNode> partitionKeys = new ArrayList<>(); final List<SqlNode> orderByKeys = new ArrayList<>(); for (int partition : group.keys) { partitionKeys.add(this.field(partition)); } for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) { this.addOrderItem(orderByKeys, collation); } SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS); SqlNode lowerBound = null; SqlNode upperBound = null; final SqlLiteral allowPartial = null; for (Window.RexWinAggCall winAggCall : group.aggCalls) { SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator(); final SqlWindow sqlWindow = SqlWindow.create(null, null, new SqlNodeList(partitionKeys, POS), new SqlNodeList(orderByKeys, POS), isRows, lowerBound, upperBound, allowPartial, POS); if (aggFunction.allowsFraming()) { lowerBound = createSqlWindowBound(group.lowerBound); upperBound = createSqlWindowBound(group.upperBound); sqlWindow.setLowerBound(lowerBound); sqlWindow.setUpperBound(upperBound); } RexShuttle replaceConstants = new RexShuttle() { @Override public RexNode visitInputRef(RexInputRef inputRef) { int index = inputRef.getIndex(); RexNode ref; if (index > inputFieldCount - 1) { ref = constants.get(index - inputFieldCount); } else { ref = inputRef; } return ref; } }; RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants); List<SqlNode> operands = toSql(null, aggCall.operands); rexOvers.add(createOverCall(aggFunction, operands, sqlWindow)); } return rexOvers; }
Example 5
Source File: PigRelToSqlConverter.java From calcite with Apache License 2.0 | 4 votes |
/** @see #dispatch */ public Result visit(Window e) { final Result x = visitChild(0, e.getInput()); final Builder builder = x.builder(e, Clause.SELECT); final List<SqlNode> selectList = new ArrayList<>(builder.context.fieldList()); for (Window.Group winGroup : e.groups) { final List<SqlNode> partitionList = Expressions.list(); for (int i : winGroup.keys) { partitionList.add(builder.context.field(i)); } final List<SqlNode> orderList = Expressions.list(); for (RelFieldCollation orderKey : winGroup.collation().getFieldCollations()) { orderList.add(builder.context.toSql(orderKey)); } final SqlNode sqlWindow = SqlWindow.create( null, // Window declaration name null, // Window reference name new SqlNodeList(partitionList, POS), new SqlNodeList(orderList, POS), SqlLiteral.createBoolean(winGroup.isRows, POS), builder.context.toSql(winGroup.lowerBound), builder.context.toSql(winGroup.upperBound), null, // allowPartial POS); for (Window.RexWinAggCall winFunc : winGroup.aggCalls) { final List<SqlNode> winFuncOperands = Expressions.list(); for (RexNode operand : winFunc.getOperands()) { winFuncOperands.add(builder.context.toSql(null, operand)); } SqlNode aggFunc = winFunc.getOperator().createCall(new SqlNodeList(winFuncOperands, POS)); selectList.add(SqlStdOperatorTable.OVER.createCall(POS, aggFunc, sqlWindow)); } builder.setSelect(new SqlNodeList(selectList, POS)); } return builder.result(); }
Example 6
Source File: SqlImplementor.java From calcite with Apache License 2.0 | 4 votes |
public List<SqlNode> toSql(Window.Group group, ImmutableList<RexLiteral> constants, int inputFieldCount) { final List<SqlNode> rexOvers = new ArrayList<>(); final List<SqlNode> partitionKeys = new ArrayList<>(); final List<SqlNode> orderByKeys = new ArrayList<>(); for (int partition: group.keys) { partitionKeys.add(this.field(partition)); } for (RelFieldCollation collation: group.orderKeys.getFieldCollations()) { this.addOrderItem(orderByKeys, collation); } SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS); SqlNode lowerBound = null; SqlNode upperBound = null; final SqlLiteral allowPartial = null; for (Window.RexWinAggCall winAggCall: group.aggCalls) { SqlAggFunction aggFunction = (SqlAggFunction) winAggCall.getOperator(); final SqlWindow sqlWindow = SqlWindow.create(null, null, new SqlNodeList(partitionKeys, POS), new SqlNodeList(orderByKeys, POS), isRows, lowerBound, upperBound, allowPartial, POS); if (aggFunction.allowsFraming()) { lowerBound = createSqlWindowBound(group.lowerBound); upperBound = createSqlWindowBound(group.upperBound); sqlWindow.setLowerBound(lowerBound); sqlWindow.setUpperBound(upperBound); } RexShuttle replaceConstants = new RexShuttle() { @Override public RexNode visitInputRef(RexInputRef inputRef) { int index = inputRef.getIndex(); RexNode ref; if (index > inputFieldCount - 1) { ref = constants.get(index - inputFieldCount); } else { ref = inputRef; } return ref; } }; RexCall aggCall = (RexCall) winAggCall.accept(replaceConstants); List<SqlNode> operands = toSql(null, aggCall.operands); rexOvers.add(createOverCall(aggFunction, operands, sqlWindow)); } return rexOvers; }