org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction Java Examples

The following examples show how to use org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction. 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: SqlImplementor.java    From Bats with Apache License 2.0 5 votes vote down vote up
private SqlCall createOverCall(SqlAggFunction op, List<SqlNode> operands, SqlWindow window) {
    if (op instanceof SqlSumEmptyIsZeroAggFunction) {
        // Rewrite "SUM0(x) OVER w" to "COALESCE(SUM(x) OVER w, 0)"
        final SqlCall node = createOverCall(SqlStdOperatorTable.SUM, operands, window);
        return SqlStdOperatorTable.COALESCE.createCall(POS, node, SqlLiteral.createExactNumeric("0", POS));
    }
    final SqlCall aggFunctionCall = op.createCall(POS, operands);
    return SqlStdOperatorTable.OVER.createCall(POS, aggFunctionCall, window);
}
 
Example #2
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private SqlCall createOverCall(SqlAggFunction op, List<SqlNode> operands,
                               SqlWindow window) {
  if (op instanceof SqlSumEmptyIsZeroAggFunction) {
    // Rewrite "SUM0(x) OVER w" to "COALESCE(SUM(x) OVER w, 0)"
    final SqlCall node =
      createOverCall(SqlStdOperatorTable.SUM, operands, window);
    return SqlStdOperatorTable.COALESCE.createCall(POS, node,
      SqlLiteral.createExactNumeric("0", POS));
  }
  final SqlCall aggFunctionCall = op.createCall(POS, operands);
  return SqlStdOperatorTable.OVER.createCall(POS, aggFunctionCall,
    window);
}
 
Example #3
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 #4
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 #5
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
private SqlCall createOverCall(SqlAggFunction op, List<SqlNode> operands,
    SqlWindow window) {
  if (op instanceof SqlSumEmptyIsZeroAggFunction) {
    // Rewrite "SUM0(x) OVER w" to "COALESCE(SUM(x) OVER w, 0)"
    final SqlCall node =
        createOverCall(SqlStdOperatorTable.SUM, operands, window);
    return SqlStdOperatorTable.COALESCE.createCall(POS, node,
        SqlLiteral.createExactNumeric("0", POS));
  }
  final SqlCall aggFunctionCall = op.createCall(POS, operands);
  return SqlStdOperatorTable.OVER.createCall(POS, aggFunctionCall,
      window);
}
 
Example #6
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode reduceAvg(
    Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping) {
  final PlannerSettings plannerSettings = (PlannerSettings) oldAggRel.getCluster().getPlanner().getContext();
  final boolean isInferenceEnabled = plannerSettings.isTypeInferenceEnabled();
  final int nGroups = oldAggRel.getGroupCount();
  RelDataTypeFactory typeFactory =
      oldAggRel.getCluster().getTypeFactory();
  RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
  int iAvgInput = oldCall.getArgList().get(0);
  RelDataType avgInputType =
      getFieldType(
          oldAggRel.getInput(),
          iAvgInput);
  RelDataType sumType =
      TypeInferenceUtils.getDrillSqlReturnTypeInference(SqlKind.SUM.name(),
          ImmutableList.of())
        .inferReturnType(oldCall.createBinding(oldAggRel));
  sumType =
      typeFactory.createTypeWithNullability(
          sumType,
          sumType.isNullable() || nGroups == 0);
  SqlAggFunction sumAgg =
      new DrillCalciteSqlAggFunctionWrapper(new SqlSumEmptyIsZeroAggFunction(), sumType);
  AggregateCall sumCall = AggregateCall.create(sumAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, sumType, null);
  final SqlCountAggFunction countAgg = (SqlCountAggFunction) SqlStdOperatorTable.COUNT;
  final RelDataType countType = countAgg.getReturnType(typeFactory);
  AggregateCall countCall = AggregateCall.create(countAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, countType, null);

  RexNode tmpsumRef =
      rexBuilder.addAggCall(
          sumCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));

  RexNode tmpcountRef =
      rexBuilder.addAggCall(
          countCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));

  RexNode n = rexBuilder.makeCall(SqlStdOperatorTable.CASE,
      rexBuilder.makeCall(SqlStdOperatorTable.EQUALS,
          tmpcountRef, rexBuilder.makeExactLiteral(BigDecimal.ZERO)),
          rexBuilder.constantNull(),
          tmpsumRef);

  // NOTE:  these references are with respect to the output
  // of newAggRel
  /*
  RexNode numeratorRef =
      rexBuilder.makeCall(CastHighOp,
        rexBuilder.addAggCall(
            sumCall,
            nGroups,
            newCalls,
            aggCallMapping,
            ImmutableList.of(avgInputType))
      );
  */
  RexNode numeratorRef = rexBuilder.makeCall(CastHighOp,  n);

  RexNode denominatorRef =
      rexBuilder.addAggCall(
          countCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));
  if (isInferenceEnabled) {
    return rexBuilder.makeCall(
        new DrillSqlOperator(
            "divide",
            2,
            true,
            oldCall.getType(), false),
        numeratorRef,
        denominatorRef);
  } else {
    final RexNode divideRef =
        rexBuilder.makeCall(
            SqlStdOperatorTable.DIVIDE,
            numeratorRef,
            denominatorRef);
    return rexBuilder.makeCast(
        typeFactory.createSqlType(SqlTypeName.ANY), divideRef);
  }
}
 
Example #7
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode reduceSum(
    Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping) {
  final PlannerSettings plannerSettings = (PlannerSettings) oldAggRel.getCluster().getPlanner().getContext();
  final boolean isInferenceEnabled = plannerSettings.isTypeInferenceEnabled();
  final int nGroups = oldAggRel.getGroupCount();
  RelDataTypeFactory typeFactory =
      oldAggRel.getCluster().getTypeFactory();
  RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
  int arg = oldCall.getArgList().get(0);
  RelDataType argType =
      getFieldType(
          oldAggRel.getInput(),
          arg);
  final RelDataType sumType;
  final SqlAggFunction sumZeroAgg;
  if (isInferenceEnabled) {
    sumType = oldCall.getType();
  } else {
    sumType =
        typeFactory.createTypeWithNullability(
            oldCall.getType(), argType.isNullable());
  }
  sumZeroAgg = new DrillCalciteSqlAggFunctionWrapper(
      new SqlSumEmptyIsZeroAggFunction(), sumType);
  AggregateCall sumZeroCall = AggregateCall.create(sumZeroAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, sumType, null);
  final SqlCountAggFunction countAgg = (SqlCountAggFunction) SqlStdOperatorTable.COUNT;
  final RelDataType countType = countAgg.getReturnType(typeFactory);
  AggregateCall countCall = AggregateCall.create(countAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, countType, null);
  // NOTE:  these references are with respect to the output
  // of newAggRel
  RexNode sumZeroRef =
      rexBuilder.addAggCall(
          sumZeroCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(argType));
  if (!oldCall.getType().isNullable()) {
    // If SUM(x) is not nullable, the validator must have determined that
    // nulls are impossible (because the group is never empty and x is never
    // null). Therefore we translate to SUM0(x).
    return sumZeroRef;
  }
  RexNode countRef =
      rexBuilder.addAggCall(
          countCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(argType));
  return rexBuilder.makeCall(SqlStdOperatorTable.CASE,
      rexBuilder.makeCall(SqlStdOperatorTable.EQUALS,
          countRef, rexBuilder.makeExactLiteral(BigDecimal.ZERO)),
          rexBuilder.constantNull(),
          sumZeroRef);
}
 
Example #8
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final DrillAggregateRel oldAggRel = (DrillAggregateRel) call.rels[0];

  final Map<AggregateCall, RexNode> aggCallMapping = Maps.newHashMap();
  final List<AggregateCall> newAggregateCalls = Lists.newArrayList();
  for (AggregateCall oldAggregateCall : oldAggRel.getAggCallList()) {
    if (isConversionToSumZeroNeeded(oldAggregateCall.getAggregation(), oldAggregateCall.getType())) {
      final RelDataType argType = oldAggregateCall.getType();
      final RelDataType sumType = oldAggRel.getCluster().getTypeFactory()
          .createTypeWithNullability(argType, argType.isNullable());
      final SqlAggFunction sumZeroAgg = new DrillCalciteSqlAggFunctionWrapper(
          new SqlSumEmptyIsZeroAggFunction(), sumType);
      AggregateCall sumZeroCall =
          AggregateCall.create(
              sumZeroAgg,
              oldAggregateCall.isDistinct(),
              oldAggregateCall.isApproximate(),
              oldAggregateCall.getArgList(),
              -1,
              sumType,
              oldAggregateCall.getName());
      oldAggRel.getCluster().getRexBuilder()
          .addAggCall(sumZeroCall,
              oldAggRel.getGroupCount(),
              oldAggRel.indicator,
              newAggregateCalls,
              aggCallMapping,
              ImmutableList.of(argType));
    } else {
      newAggregateCalls.add(oldAggregateCall);
    }
  }

  call.transformTo(new DrillAggregateRel(
      oldAggRel.getCluster(),
      oldAggRel.getTraitSet(),
      oldAggRel.getInput(),
      oldAggRel.indicator,
      oldAggRel.getGroupSet(),
      oldAggRel.getGroupSets(),
      newAggregateCalls));
}
 
Example #9
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@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()));
}