bottomGroupSet.addAll(aggregate.getGroupSet().asList()); for (AggregateCall aggCall : originalAggCalls) { if (aggCall.isDistinct()) { bottomGroupSet.addAll(aggCall.getArgList()); break; // since we only have single distinct call if (!aggCall.isDistinct()) { final AggregateCall newCall = AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), aggCall.getArgList(), -1, ImmutableBitSet.of(bottomGroupSet).cardinality(), relBuilder.peek(), null, aggCall.name); bottomAggregateCalls.add(newCall); if (aggCall.isDistinct()) { List<Integer> newArgList = new ArrayList<>(); for (int arg : aggCall.getArgList()) { newArgList.add(bottomGroupSet.headSet(arg).size()); AggregateCall.create(aggCall.getAggregation(), false, aggCall.isApproximate(), newArgList, aggCall.getType(), aggCall.name); } else { Lists.newArrayList(bottomGroupSet.size() + nonDistinctAggCallProcessedSoFar);
final RelBuilder relBuilder = call.builder(); final Aggregate aggRel = (Aggregate) call.rel(0); final RexBuilder rexBuilder = aggRel.getCluster().getRexBuilder(); final Map<AggregateCall, Integer> mapping = new HashMap<>(); final List<Integer> indexes = new ArrayList<>(); final List<AggregateCall> aggCalls = aggRel.getAggCallList(); final List<AggregateCall> newAggCalls = new ArrayList<>(aggCalls.size()); int nextIdx = aggRel.getGroupCount() + aggRel.getIndicatorCount(); for (int i = 0; i < aggCalls.size(); i++) { AggregateCall aggCall = aggCalls.get(i); if (aggCall.getAggregation().getKind() == SqlKind.COUNT && !aggCall.isDistinct()) { final List<Integer> args = aggCall.getArgList(); final List<Integer> nullableArgs = new ArrayList<>(args.size()); for (int arg : args) { if (aggRel.getInput().getRowType().getFieldList().get(arg).getType().isNullable()) { nullableArgs.add(arg); aggCall = aggCall.copy(nullableArgs, aggCall.filterArg); rewrite = true; for (int i = offset; i < aggRel.getRowType().getFieldCount(); ++i) { projList.add( rexBuilder.makeInputRef( aggRel.getRowType().getFieldList().get(i).getType(), indexes.get(i-offset)));
RelDataType fieldType = aggCall.getType(); SqlAggFunction newAgg = aggCall.getAggregation(); List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList()); if (udafMap != null && udafMap.containsKey(callName)) { newArgList = truncArgList(newArgList, udafMap.get(callName)); RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false); if (newArgList.isEmpty()) { newArgList.add(field.getIndex()); AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);
public KylinAggregateCall(AggregateCall aggCall, FunctionDesc func) { super(aggCall.getAggregation(), aggCall.isDistinct(), aggCall.getArgList(), aggCall.type, aggCall.name); this.func = func; }
static String getSqlFuncName(AggregateCall aggCall) { String sqlName = aggCall.getAggregation().getName(); if (aggCall.isDistinct()) { sqlName = sqlName + "_DISTINCT"; } return sqlName; }
/** * Returns the number of count DISTINCT * * @return the number of count DISTINCT */ private int getNumCountDistinctCall(Aggregate hiveAggregate) { int cnt = 0; for (AggregateCall aggCall : hiveAggregate.getAggCallList()) { if (aggCall.isDistinct() && (aggCall.getAggregation().getName().equalsIgnoreCase("count"))) { cnt++; } } return cnt; }
+ aggregateRelOptTable.getQualifiedName() + ", rolling up " + quarkTile.bitSet() + " to " + mergedAggregate.getGroupSet()); assert quarkTile.bitSet().contains(mergedAggregate.getGroupSet()); final List<AggregateCall> aggCalls = Lists.newArrayList(); ImmutableBitSet.Builder groupSet = ImmutableBitSet.builder(); for (int key : mergedAggregate.getGroupSet()) { groupSet.set(quarkTile.bitSet().indexOf(key)); List<Integer> posList = Lists.newArrayList(); for (QuarkTile.Column quarkColumn : quarkTile.cubeColumns) { posList.add(columnCount++); for (AggregateCall aggCall : mergedAggregate.getAggCallList()) { final AggregateCall copy = AggregateCall.create(aggCall.getAggregation(), false, ImmutableList.of(index), -1, groupSet.cardinality(), rel, null, aggCall.name); aggCalls.add(copy); index++;
if (!aggregate.getGroupSet().isEmpty()) { return; final List<AggregateCall> aggCalls = aggregate.getAggCallList(); final Set<Integer> isCountStar = Sets.newHashSet(); for (AggregateCall aggCall : aggCalls) { ++k; if ((aggCall.getAggregation() instanceof SqlCountAggFunction) && (aggCall.getArgList().size() == 0)) { isCountStar.add(k); List<RexFieldAccess> correlatedJoinKeys = Lists.newArrayList(); int nFields = left.getRowType().getFieldCount(); ImmutableBitSet allCols = ImmutableBitSet.range(nFields); int nullIndicatorPos = join.getRowType().getFieldCount() - 1; for (int aggArg : aggCall.getArgList()) { argList.add(aggArg + groupCount); : aggCall.filterArg + groupCount; newAggCalls.add( aggCall.adaptTo(joinOutputProject, argList, filterArg, aggregate.getGroupCount(), groupCount));
RexNode joinCond = rexBuilder.makeLiteral(true); if ((joinType != JoinRelType.LEFT) || (joinCond != rexBuilder.makeLiteral(true))) { return; if ((!aggregate.getGroupSet().isEmpty()) || (aggregate.getAggCallList().size() != 1) || !(aggregate.getAggCallList().get(0).getAggregation() instanceof SqlSingleValueAggFunction)) { return; List<RexNode> tmpRightJoinKeys = Lists.newArrayList(); List<RexNode> correlatedJoinKeys = Lists.newArrayList(); HiveRelOptUtil.splitCorrelatedFilterCondition( filter, left.getRowType().getFieldCount() + rightJoinKeys.get(0).getIndex(); } else if (cm.mapRefRelToCorRef.containsKey(project)) { ImmutableList.of( Pair.<RexNode, String>of( rexBuilder.makeLiteral(true), "nullIndicator"))); left.getRowType().getFieldCount() + right.getRowType().getFieldCount() - 1; } else { return;
void buildAggregations() { ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType(); this.aggregations = Lists.newArrayList(); this.hackAggCalls = Maps.newHashMap(); for (int i = 0; i < this.rewriteAggCalls.size(); i++) { AggregateCall aggCall = this.rewriteAggCalls.get(i); ParameterDesc parameter = null; List<Integer> argList = aggCall.getArgList(); List<TblColRef> columns = Lists.newArrayList(); String funcName = getSqlFuncName(aggCall); int columnsCount = aggCall.getArgList().size(); if (AGGR_FUNC_PARAM_AS_MEASURE_MAP.containsKey(funcName)) { int asMeasureCnt = AGGR_FUNC_PARAM_AS_MEASURE_MAP.get(funcName); for (Integer index : aggCall.getArgList().subList(0, columnsCount)) { TblColRef column = inputColumnRowType.getColumnByIndex(index); columns.add(column); int iRowIdx = argList.get(0); TupleExpression tupleExpr = inputColumnRowType.getSourceColumnsByIndex(iRowIdx); if (aggCall.getAggregation() instanceof SqlSumAggFunction || aggCall.getAggregation() instanceof SqlSumEmptyIsZeroAggFunction) { } else if (aggCall.getAggregation() instanceof SqlCountAggFunction && !aggCall.isDistinct()) { AggregateCall newAggCall = AggregateCall.create(SqlStdOperatorTable.SUM, false, aggCall.getArgList(), -1, aggCall.getType(), aggCall.getName()); this.hackAggCalls.put(i, newAggCall);
for (ImmutableList<RexNode> nodeList : groupKey_.nodeLists) { final ImmutableBitSet groupSet2 = ImmutableBitSet.of(registerExpressions(extraNodes, nodeList)); if (!groupSet.contains(groupSet2)) { throw new IllegalArgumentException("group set element " + nodeList + " must be a subset of group key"); groupSets = ImmutableList.copyOf(groupSetSet); if (extraNodes.size() > sizeBefore) { throw new IllegalArgumentException( groupSets = ImmutableList.of(groupSet); if (extraNodes.size() > inputRowType.getFieldCount()) { project(extraNodes); : registerExpression(extraNodes, aggCall1.filter); aggregateCall = AggregateCall.create(aggCall1.aggFunction, aggCall1.distinct, args, filterArg, groupSet.cardinality(), r, null, aggCall1.alias); } else {
final RelDataType rowType = aggregate.getRowType(); aggregate.getGroupSet().rebuild(); for (AggregateCall aggCall : aggregate.getAggCallList()) { for (int i : aggCall.getArgList()) { inputFieldsUsed.set(i); ImmutableBitSet gbKeysDeleted = originalGroupSet.except(updatedGroupSet); ImmutableBitSet updatedGroupFields = ImmutableBitSet.range(originalGroupSet.cardinality()); && fieldsUsed.equals(ImmutableBitSet.range(rowType.getFieldCount()))) { return result(aggregate, Mappings.createIdentity(rowType.getFieldCount())); final ImmutableList<RexNode> args = relBuilder.fields( Mappings.apply2(inputMapping, aggCall.getArgList())); final RexNode filterArg = aggCall.filterArg < 0 ? null : relBuilder.field(Mappings.apply(inputMapping, aggCall.filterArg)); RelBuilder.AggCall newAggCall = relBuilder.aggregateCall(aggCall.getAggregation(), aggCall.isDistinct(), aggCall.isApproximate(), filterArg, aggCall.name, args); mapping.set(j, updatedGroupCount + newAggCallList.size());
if (rel.getGroupType() != Aggregate.Group.SIMPLE) { throw new AssertionError(Bug.CALCITE_461_FIXED); final RelNode oldInput = rel.getInput(); final Frame frame = getInvoke(oldInput, rel); if (frame == null) { final int oldGroupKeyCount = rel.getGroupSet().cardinality(); final List<Pair<RexNode, String>> projects = Lists.newArrayList(); newInput.getRowType().getFieldList(); final ImmutableBitSet newGroupSet = ImmutableBitSet.range(newGroupKeyCount); List<AggregateCall> newAggCalls = Lists.newArrayList(); List<AggregateCall> oldAggCalls = rel.getAggCallList(); int oldInputOutputFieldCount = rel.getGroupSet().cardinality(); int newInputOutputFieldCount = newGroupSet.cardinality(); for (AggregateCall oldAggCall : oldAggCalls) { ++i; List<Integer> oldAggArgs = oldAggCall.getArgList(); List<Integer> aggArgs = Lists.newArrayList(); oldAggCall.adaptTo(newProject, aggArgs, filterArg, oldGroupKeyCount, newGroupKeyCount));
Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) { final int nGroups = oldAggRel.getGroupCount(); final RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder(); final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory(); final int iAvgInput = oldCall.getArgList().get(0); rexBuilder.getTypeFactory(), sum0InputType, oldCall.getType()); final AggregateCall sumCall = AggregateCall.create( new HiveSqlSumAggFunction( oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), oldCall.getAggregation().getOperandTypeInference(), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.SUM, oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), rexBuilder.addAggCall(sumCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(sum0InputType)); refSum = rexBuilder.ensureType(oldCall.getType(), refSum, true); return rexBuilder.makeCast(oldCall.getType(), coalesce);
private RelNode genGBRelNode(List<ExprNodeDesc> gbExprs, List<AggInfo> aggInfoLst, List<Long> groupSets, RelNode srcRel) throws SemanticException { ImmutableMap<String, Integer> posMap = this.relToHiveColNameCalcitePosMap.get(srcRel); RexNodeConverter converter = new RexNodeConverter(this.cluster, srcRel.getRowType(), posMap, 0, false); final List<RexNode> gbChildProjLst = Lists.newArrayList(); final HashMap<String, Integer> rexNodeToPosMap = new HashMap<String, Integer>(); final List<Integer> groupSetPositions = Lists.newArrayList(); Integer gbIndx = 0; RexNode rnd; gbIndx++; final ImmutableBitSet groupSet = ImmutableBitSet.of(groupSetPositions); new HashSet<ImmutableBitSet>(groupSets.size()); for(long val: groupSets) { setTransformedGroupSets.add(convert(val, groupSet.cardinality())); List<AggregateCall> aggregateCalls = Lists.newArrayList(); for (AggInfo agg : aggInfoLst) { aggregateCalls.add(convertGBAgg(agg, srcRel, gbChildProjLst, converter, rexNodeToPosMap, AggregateCall aggCall = AggregateCall.create(HiveGroupingID.INSTANCE, false, new ImmutableList.Builder<Integer>().build(), -1, this.cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT), gbChildProjLst.add(this.cluster.getRexBuilder().makeInputRef(srcRel, 0));
SQLCall toSqlCall(AggregateCall aggCall) { ColumnRowType inputColumnRowType = ((OLAPRel) getInput()).getColumnRowType(); String function = getSqlFuncName(aggCall); List<Object> args = Lists.newArrayList(); for (Integer index : aggCall.getArgList()) { TblColRef col = inputColumnRowType.getColumnByIndexNullable(index); args.add(col); } return new SQLCall(function, args); }
final int oldGroupKeyCount = rel.getGroupSet().cardinality(); final List<Pair<RexNode, String>> projects = Lists.newArrayList(); newInput.getRowType().getFieldList(); final ImmutableBitSet newGroupSet = ImmutableBitSet.range(newGroupKeyCount); List<AggregateCall> newAggCalls = Lists.newArrayList(); List<AggregateCall> oldAggCalls = rel.getAggCallList(); int oldInputOutputFieldCount = rel.getGroupSet().cardinality(); int newInputOutputFieldCount = newGroupSet.cardinality(); for (AggregateCall oldAggCall : oldAggCalls) { ++i; List<Integer> oldAggArgs = oldAggCall.getArgList(); List<Integer> aggArgs = Lists.newArrayList(); oldAggCall.adaptTo(newProject, aggArgs, filterArg, oldGroupKeyCount, newGroupKeyCount));
/** * Creates a LogicalAggregate that removes all duplicates from the result of * an underlying relational expression. * * @param rel underlying rel * @return rel implementing SingleValueAgg */ public static RelNode createSingleValueAggRel( RelOptCluster cluster, RelNode rel, RelFactories.AggregateFactory aggregateFactory) { // assert (rel.getRowType().getFieldCount() == 1); final int aggCallCnt = rel.getRowType().getFieldCount(); final List<AggregateCall> aggCalls = new ArrayList<>(); for (int i = 0; i < aggCallCnt; i++) { aggCalls.add( AggregateCall.create( SqlStdOperatorTable.SINGLE_VALUE, false, false, ImmutableList.of(i), -1, 0, rel, null, null)); } return aggregateFactory.createAggregate(rel, false, ImmutableBitSet.of(), null, aggCalls); }
private AggregateCall createAggregateCallWithBinding( RelDataTypeFactory typeFactory, SqlAggFunction aggFunction, RelDataType operandType, Aggregate oldAggRel, AggregateCall oldCall, int argOrdinal) { final Aggregate.AggCallBinding binding = new Aggregate.AggCallBinding(typeFactory, aggFunction, ImmutableList.of(operandType), oldAggRel.getGroupCount(), oldCall.filterArg >= 0); return AggregateCall.create(aggFunction, oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argOrdinal), oldCall.filterArg, aggFunction.inferReturnType(binding), null); }
AggregateCall aggregateCall, int leftSubTotal, int rightSubTotal) { final List<RexNode> merges = new ArrayList<>(); final List<RelDataTypeField> fieldList = inputRowType.getFieldList(); if (leftSubTotal >= 0) { final RelDataType type = fieldList.get(leftSubTotal).getType(); merges.add(rexBuilder.makeInputRef(type, leftSubTotal)); merges.add(rexBuilder.makeInputRef(type, rightSubTotal)); break; case 2: node = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, merges); node = rexBuilder.makeAbstractCast(aggregateCall.type, node); break; return AggregateCall.create(new HiveSqlSumAggFunction(isDistinct, returnTypeInference, operandTypeInference, operandTypeChecker), false, ImmutableList.of(ordinal), -1, aggregateCall.type, aggregateCall.name);