Java Code Examples for org.apache.calcite.rel.core.AggregateCall#getArgList()

The following examples show how to use org.apache.calcite.rel.core.AggregateCall#getArgList() . 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: RelMdColumnOrigins.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Set<RelColumnOrigin> getColumnOrigins(Aggregate rel,
    RelMetadataQuery mq, int iOutputColumn) {
  if (iOutputColumn < rel.getGroupCount()) {
    // Group columns pass through directly.
    return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
  }

  // Aggregate columns are derived from input columns
  AggregateCall call =
      rel.getAggCallList().get(iOutputColumn
              - rel.getGroupCount());

  final Set<RelColumnOrigin> set = new HashSet<>();
  for (Integer iInput : call.getArgList()) {
    Set<RelColumnOrigin> inputSet =
        mq.getColumnOrigins(rel.getInput(), iInput);
    inputSet = createDerivedColumnOrigins(inputSet);
    if (inputSet != null) {
      set.addAll(inputSet);
    }
  }
  return set;
}
 
Example 2
Source File: RexBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a reference to an aggregate call, checking for repeated calls.
 *
 * <p>Argument types help to optimize for repeated aggregates.
 * For instance count(42) is equivalent to count(*).</p>
 *
 * @param aggCall aggregate call to be added
 * @param groupCount number of groups in the aggregate relation
 * @param aggCalls destination list of aggregate calls
 * @param aggCallMapping the dictionary of already added calls
 * @param aggArgTypes Argument types, not null
 *
 * @return Rex expression for the given aggregate call
 */
public RexNode addAggCall(AggregateCall aggCall, int groupCount,
    List<AggregateCall> aggCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    final List<RelDataType> aggArgTypes) {
  if (aggCall.getAggregation() instanceof SqlCountAggFunction
      && !aggCall.isDistinct()) {
    final List<Integer> args = aggCall.getArgList();
    final List<Integer> nullableArgs = nullableArgs(args, aggArgTypes);
    if (!nullableArgs.equals(args)) {
      aggCall = aggCall.copy(nullableArgs, aggCall.filterArg,
          aggCall.collation);
    }
  }
  RexNode rex = aggCallMapping.get(aggCall);
  if (rex == null) {
    int index = aggCalls.size() + groupCount;
    aggCalls.add(aggCall);
    rex = makeInputRef(aggCall.getType(), index);
    aggCallMapping.put(aggCall, rex);
  }
  return rex;
}
 
Example 3
Source File: WindowPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
protected LogicalExpression toExpr(AggregateCall call, List<String> fn) {
  ParseContext context = new ParseContext(PrelUtil.getSettings(getCluster()));

  List<LogicalExpression> args = Lists.newArrayList();
  for (Integer i : call.getArgList()) {
    final int indexInConstants = i - fn.size();
    if (i < fn.size()) {
      args.add(new FieldReference(fn.get(i)));
    } else {
      final RexLiteral constant = constants.get(indexInConstants);
      LogicalExpression expr = RexToExpr.toExpr(context, getInput().getRowType(), getCluster().getRexBuilder(), constant);
      args.add(expr);
    }
  }

  // for count(1).
  if (args.isEmpty()) {
    args.add(new ValueExpressions.LongExpression(1l));
  }

  return new FunctionCall(call.getAggregation().getName().toLowerCase(), args);
}
 
Example 4
Source File: SqlSplittableAggFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  final RexNode rexOne = rexBuilder.makeExactLiteral(
      BigDecimal.ONE, aggregateCall.getType());
  if (predicate == null) {
    return rexOne;
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate, rexOne,
        rexBuilder.makeExactLiteral(BigDecimal.ZERO, aggregateCall.getType()));
  }
}
 
Example 5
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Compute which input fields are used by the aggregate.
 */
private ImmutableBitSet.Builder getInputFieldUsed(Aggregate aggregate, RelNode input) {
	// 1. group fields are always used
	final ImmutableBitSet.Builder inputFieldsUsed =
		aggregate.getGroupSet().rebuild();
	// 2. agg functions
	for (AggregateCall aggCall : aggregate.getAggCallList()) {
		for (int i : aggCall.getArgList()) {
			inputFieldsUsed.set(i);
		}
		if (aggCall.filterArg >= 0) {
			inputFieldsUsed.set(aggCall.filterArg);
		}
	}
	// 3. window time field if the aggregate is a group window aggregate.
	if (aggregate instanceof LogicalWindowAggregate) {
		inputFieldsUsed.set(getWindowTimeFieldIndex(((LogicalWindowAggregate) aggregate).getWindow(), input));
	}
	return inputFieldsUsed;
}
 
Example 6
Source File: AggPrelBase.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public Prel prepareForLateralUnnestPipeline(List<RelNode> children) {
  List<Integer> groupingCols = Lists.newArrayList();
  groupingCols.add(0);
  for (int groupingCol : groupSet.asList()) {
    groupingCols.add(groupingCol + 1);
  }

  ImmutableBitSet groupingSet = ImmutableBitSet.of(groupingCols);
  List<ImmutableBitSet> groupingSets = Lists.newArrayList();
  groupingSets.add(groupingSet);
  List<AggregateCall> aggregateCalls = Lists.newArrayList();
  for (AggregateCall aggCall : aggCalls) {
    List<Integer> arglist = Lists.newArrayList();
    for (int arg : aggCall.getArgList()) {
      arglist.add(arg + 1);
    }
    aggregateCalls.add(AggregateCall.create(aggCall.getAggregation(), aggCall.isDistinct(),
            aggCall.isApproximate(), arglist, aggCall.filterArg, aggCall.type, aggCall.name));
  }
  return (Prel) copy(traitSet, children.get(0),indicator,groupingSet,groupingSets, aggregateCalls);
}
 
Example 7
Source File: WindowPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
  DrillParseContext context = new DrillParseContext(PrelUtil.getSettings(getCluster()));

  List<LogicalExpression> args = Lists.newArrayList();
  for (Integer i : call.getArgList()) {
    final int indexInConstants = i - fn.size();
    if (i < fn.size()) {
      args.add(new FieldReference(fn.get(i)));
    } else {
      final RexLiteral constant = constants.get(indexInConstants);
      LogicalExpression expr = DrillOptiq.toDrill(context, getInput(), constant);
      args.add(expr);
    }
  }

  // for count(1).
  if (args.isEmpty()) {
    args.add(new ValueExpressions.LongExpression(1l));
  }

  return new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
}
 
Example 8
Source File: RexBuilder.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a reference to an aggregate call, checking for repeated calls.
 *
 * <p>Argument types help to optimize for repeated aggregates.
 * For instance count(42) is equivalent to count(*).</p>
 *
 * @param aggCall aggregate call to be added
 * @param groupCount number of groups in the aggregate relation
 * @param indicator Whether the Aggregate has indicator (GROUPING) columns
 * @param aggCalls destination list of aggregate calls
 * @param aggCallMapping the dictionary of already added calls
 * @param aggArgTypes Argument types, not null
 *
 * @return Rex expression for the given aggregate call
 */
public RexNode addAggCall(AggregateCall aggCall, int groupCount,
    boolean indicator, List<AggregateCall> aggCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    final List<RelDataType> aggArgTypes) {
  if (aggCall.getAggregation() instanceof SqlCountAggFunction
      && !aggCall.isDistinct()) {
    final List<Integer> args = aggCall.getArgList();
    final List<Integer> nullableArgs = nullableArgs(args, aggArgTypes);
    if (!nullableArgs.equals(args)) {
      aggCall = aggCall.copy(nullableArgs, aggCall.filterArg,
          aggCall.collation);
    }
  }
  RexNode rex = aggCallMapping.get(aggCall);
  if (rex == null) {
    int index = aggCalls.size() + groupCount * (indicator ? 2 : 1);
    aggCalls.add(aggCall);
    rex = makeInputRef(aggCall.getType(), index);
    aggCallMapping.put(aggCall, rex);
  }
  return rex;
}
 
Example 9
Source File: SqlSplittableAggFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  if (predicate == null) {
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate,
        rexBuilder.makeExactLiteral(BigDecimal.ONE),
        rexBuilder.makeExactLiteral(BigDecimal.ZERO));
  }
}
 
Example 10
Source File: ExtendedAggregateExtractProjectRule.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Compute which input fields are used by the aggregate.
 */
private ImmutableBitSet.Builder getInputFieldUsed(Aggregate aggregate, RelNode input) {
	// 1. group fields are always used
	final ImmutableBitSet.Builder inputFieldsUsed =
		aggregate.getGroupSet().rebuild();
	// 2. agg functions
	for (AggregateCall aggCall : aggregate.getAggCallList()) {
		for (int i : aggCall.getArgList()) {
			inputFieldsUsed.set(i);
		}
		if (aggCall.filterArg >= 0) {
			inputFieldsUsed.set(aggCall.filterArg);
		}
	}
	// 3. window time field if the aggregate is a group window aggregate.
	if (aggregate instanceof LogicalWindowAggregate) {
		inputFieldsUsed.set(getWindowTimeFieldIndex(((LogicalWindowAggregate) aggregate).getWindow(), input));
	}
	return inputFieldsUsed;
}
 
Example 11
Source File: PigAggregate.java    From calcite with Apache License 2.0 5 votes vote down vote up
private List<String> getArgNames(String relAlias, AggregateCall aggCall) {
  final List<String> result = new ArrayList<>(aggCall.getArgList().size());
  for (int fieldIndex : aggCall.getArgList()) {
    result.add(getInputFieldNameForAggCall(relAlias, aggCall, fieldIndex));
  }
  return result;
}
 
Example 12
Source File: AggPrelBase.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
  List<LogicalExpression> args = Lists.newArrayList();
  for (Integer i : call.getArgList()) {
    args.add(FieldReference.getWithQuotedRef(fn.get(i)));
  }

  // for count(1).
  if (args.isEmpty()) {
    args.add(new ValueExpressions.LongExpression(1L));
  }
  return new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
}
 
Example 13
Source File: DrillWindowRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
  List<LogicalExpression> args = Lists.newArrayList();
  for (Integer i : call.getArgList()) {
    args.add(new FieldReference(fn.get(i)));
  }

  // for count(1).
  if (args.isEmpty()) {
    args.add(new ValueExpressions.LongExpression(1l));
  }
  LogicalExpression expr = new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
  return expr;
}
 
Example 14
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: GeodeAggregate.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void implement(GeodeImplementContext geodeImplementContext) {
  geodeImplementContext.visitChild(getInput());

  List<String> inputFields = fieldNames(getInput().getRowType());

  List<String> groupByFields = new ArrayList<>();

  for (int group : groupSet) {
    groupByFields.add(inputFields.get(group));
  }

  geodeImplementContext.addGroupBy(groupByFields);

  // Find the aggregate functions (e.g. MAX, SUM ...)
  Builder<String, String> aggregateFunctionMap = ImmutableMap.builder();
  for (AggregateCall aggCall : aggCalls) {

    List<String> aggCallFieldNames = new ArrayList<>();
    for (int i : aggCall.getArgList()) {
      aggCallFieldNames.add(inputFields.get(i));
    }
    String functionName = aggCall.getAggregation().getName();

    // Workaround to handle count(*) case. Geode doesn't allow "AS" aliases on
    // 'count(*)' but allows it for count('any column name'). So we are
    // converting the count(*) into count (first input ColumnName).
    if ("COUNT".equalsIgnoreCase(functionName) && aggCallFieldNames.isEmpty()) {
      aggCallFieldNames.add(inputFields.get(0));
    }

    String oqlAggregateCall = Util.toString(aggCallFieldNames, functionName + "(", ", ",
        ")");

    aggregateFunctionMap.put(aggCall.getName(), oqlAggregateCall);
  }

  geodeImplementContext.addAggregateFunctions(aggregateFunctionMap.build());

}
 
Example 17
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a call to an aggregate function to an expression.
 */
public SqlNode toSql(AggregateCall aggCall) {
  SqlOperator op = aggCall.getAggregation();
  if (op instanceof SqlSumEmptyIsZeroAggFunction) {
    op = SqlStdOperatorTable.SUM;
  }
  final List<SqlNode> operands = Expressions.list();
  for (int arg : aggCall.getArgList()) {
    operands.add(field(arg));
  }
  return op.createCall(
    aggCall.isDistinct() ? SqlSelectKeyword.DISTINCT.symbol(POS) : null,
    POS, operands.toArray(new SqlNode[0]));
}
 
Example 18
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a call to an aggregate function to an expression.
 */
public SqlNode toSql(AggregateCall aggCall) {
  SqlOperator op = (SqlAggFunction) aggCall.getAggregation();
  if (op instanceof SqlSumEmptyIsZeroAggFunction) {
    op = SqlStdOperatorTable.SUM;
  }
  final List<SqlNode> operands = Expressions.list();
  for (int arg : aggCall.getArgList()) {
    operands.add(field(arg));
  }
  return op.createCall(
      aggCall.isDistinct() ? SqlSelectKeyword.DISTINCT.symbol(POS) : null,
      POS, operands.toArray(new SqlNode[operands.size()]));
}
 
Example 19
Source File: ElasticsearchAggregate.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void implement(Implementor implementor) {
        implementor.visitChild(0, getInput());
        ElasticsearchTable esTable = implementor.getElasticsearchTable();
        List<AggregateCall> aggCallList = getAggCallList();
        List<RelDataTypeField> fieldList = esTable.getRowType().getFieldList();
        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        final List<String> outNames = ElasticsearchRules.elasticsearchFieldNames(getRowType());
        esTable.setOutNames(outNames);

        //        final List<String> inNames = ElasticsearchRules.elasticsearchFieldNames(getInput().getRowType());
        TermsAggregationBuilder groupAggregationBuilder = null;
        AggregationBuilder groupAggregationBuilder2 = null;     //防止出现group但没有sort出错的情况
        for(int i = 0 ;i<groupSet.cardinality();i++) {
            final String inName = inNames.get(groupSet.nth(i));

            groupAggregationBuilder = AggregationBuilders.terms(inName.toLowerCase()).field(transFieldName(ElasticsearchRules.maybeQuote(inName).toLowerCase(),fieldList));
            groupAggregationBuilder2 = AggregationBuilders.terms(inName).field(transFieldName(ElasticsearchRules.maybeQuote(inName),fieldList));
            esTable.setIsGroup(true);
        }
        List<String> out = new ArrayList<String>();   //聚合函数的名称
        for(AggregateCall call : aggCallList)
        {
            SqlAggFunction function = call.getAggregation();
            List<Integer> argList = call.getArgList();
            String functionName = function.getName();
            out.add(functionName);
            switch (function.getKind())
            {
                case MIN:
                    RelDataTypeField typeField = fieldList.get(argList.get(0));
                    if (groupAggregationBuilder == null) {
                        //min值 与 原字段值 的类型是一样的
                        esTable.addAggregationBuilder(AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField.getType()).getJavaClass());
                    }else {
                        MinAggregationBuilder minAgg = AggregationBuilders.min(functionName).field(typeField.getName().toLowerCase());
//                        esTable.addAggregationBuilder(groupAggregationBuilder.subAggregation(minAgg), call.getType().getClass());
                        groupAggregationBuilder.subAggregation(minAgg);
                        groupAggregationBuilder2.subAggregation(minAgg);
                    }
                    break;
                case MAX:
                    //max值 与 原字段值 的类型是一样的
                    RelDataTypeField typeField1 = fieldList.get(argList.get(0));
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()),
                                ((RelDataTypeFactoryImpl.JavaType) typeField1.getType()).getJavaClass());
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.max(functionName).field(typeField1.getName().toLowerCase()));
                    }
                    break;
                case COUNT:
                    if(groupAggregationBuilder == null) {
                        if (argList == null || argList.size() == 0)//count(*)
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName), Long.class);
                        else
                            esTable.addAggregationBuilder(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)), Long.class);
                    }else {
                        groupAggregationBuilder.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                        groupAggregationBuilder2.subAggregation(AggregationBuilders.count(functionName).field(transFieldName(fieldList.get(argList.get(0)).getName().toLowerCase(), fieldList)));
                    }
                    break;
                case SUM:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        String s = fieldList.get(argList.get(0)).getName().toLowerCase();
                        SumAggregationBuilder sumAgg = AggregationBuilders.sum(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(sumAgg);
                        groupAggregationBuilder2.subAggregation(sumAgg);
                    }
                    break;
                case AVG:
                    if(groupAggregationBuilder == null) {
                        esTable.addAggregationBuilder(AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase()), Double.class);
                    }else {
                        AvgAggregationBuilder avgAgg = AggregationBuilders.avg(functionName).field(fieldList.get(argList.get(0)).getName().toLowerCase());
                        groupAggregationBuilder.subAggregation(avgAgg);
                        groupAggregationBuilder2.subAggregation(avgAgg);
                    }
                    break;
                default:break;
            }
        }
        if (groupAggregationBuilder != null) {
            esTable.addAggregationBuilder((AbstractAggregationBuilder) groupAggregationBuilder,String.class);
            esTable.addAggregationBuilderList2(groupAggregationBuilder);
        }
        esTable.setOut(out);
    }
 
Example 20
Source File: KylinAggregateCall.java    From kylin with Apache License 2.0 4 votes vote down vote up
public KylinAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    super(aggCall.getAggregation(), aggCall.isDistinct(), aggCall.getArgList(), aggCall.type, aggCall.name);
    this.func = func;
}