Java Code Examples for org.apache.calcite.sql.fun.SqlStdOperatorTable#SUM0

The following examples show how to use org.apache.calcite.sql.fun.SqlStdOperatorTable#SUM0 . 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: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Get rollup aggregation function.
 */
protected SqlAggFunction getRollup(SqlAggFunction aggregation) {
    if (aggregation == SqlStdOperatorTable.SUM || aggregation == SqlStdOperatorTable.MIN
            || aggregation == SqlStdOperatorTable.MAX || aggregation == SqlStdOperatorTable.SUM0
            || aggregation == SqlStdOperatorTable.ANY_VALUE) {
        return aggregation;
    } else if (aggregation == SqlStdOperatorTable.COUNT) {
        return SqlStdOperatorTable.SUM0;
    } else {
        return null;
    }
}
 
Example 2
Source File: AggregateUnionTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
private List<AggregateCall> transformAggCalls(RelNode input, int groupCount,
    List<AggregateCall> origCalls) {
  final List<AggregateCall> newCalls = new ArrayList<>();
  for (Ord<AggregateCall> ord : Ord.zip(origCalls)) {
    final AggregateCall origCall = ord.e;
    if (origCall.isDistinct()
        || !SUPPORTED_AGGREGATES.containsKey(origCall.getAggregation()
            .getClass())) {
      return null;
    }
    final SqlAggFunction aggFun;
    final RelDataType aggType;
    if (origCall.getAggregation() == SqlStdOperatorTable.COUNT) {
      aggFun = SqlStdOperatorTable.SUM0;
      // count(any) is always not null, however nullability of sum might
      // depend on the number of columns in GROUP BY.
      // Here we use SUM0 since we are sure we will not face nullable
      // inputs nor we'll face empty set.
      aggType = null;
    } else {
      aggFun = origCall.getAggregation();
      aggType = origCall.getType();
    }
    AggregateCall newCall =
        AggregateCall.create(aggFun, origCall.isDistinct(),
            origCall.isApproximate(),
            ImmutableList.of(groupCount + ord.i), -1,
            origCall.collation,
            groupCount,
            input,
            aggType,
            origCall.getName());
    newCalls.add(newCall);
  }
  return newCalls;
}
 
Example 3
Source File: SubstitutionVisitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static SqlAggFunction getRollup(SqlAggFunction aggregation) {
    if (aggregation == SqlStdOperatorTable.SUM || aggregation == SqlStdOperatorTable.MIN
            || aggregation == SqlStdOperatorTable.MAX || aggregation == SqlStdOperatorTable.SUM0
            || aggregation == SqlStdOperatorTable.ANY_VALUE) {
        return aggregation;
    } else if (aggregation == SqlStdOperatorTable.COUNT) {
        return SqlStdOperatorTable.SUM0;
    } else {
        return null;
    }
}
 
Example 4
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    // rebuild function
    String callName = getSqlFuncName(aggCall);
    RelDataType fieldType = aggCall.getType();
    SqlAggFunction newAgg = aggCall.getAggregation();

    Map<String, Class<?>> udafMap = func.getMeasureType().getRewriteCalciteAggrFunctions();
    if (func.isCount()) {
        newAgg = SqlStdOperatorTable.SUM0;
    } else if (udafMap != null && udafMap.containsKey(callName)) {
        newAgg = createCustomAggFunction(callName, fieldType, udafMap.get(callName));
    }

    // rebuild parameters
    List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList());
    if (udafMap != null && udafMap.containsKey(callName)) {
        newArgList = truncArgList(newArgList, udafMap.get(callName));
    }
    if (func.needRewriteField()) {
        RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false);
        if (newArgList.isEmpty()) {
            newArgList.add(field.getIndex());
        } else {
            // TODO: only the first column got overwritten
            newArgList.set(0, field.getIndex());
        }
    }

    // rebuild aggregate call
    AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);

    return newAggCall;
}
 
Example 5
Source File: OLAPAggregateRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private AggregateCall rewriteAggregateCall(AggregateCall aggCall, FunctionDesc func) {
    // rebuild function
    String callName = getSqlFuncName(aggCall);
    RelDataType fieldType = aggCall.getType();
    SqlAggFunction newAgg = aggCall.getAggregation();

    Map<String, Class<?>> udafMap = func.getMeasureType().getRewriteCalciteAggrFunctions();
    if (func.isCount()) {
        newAgg = SqlStdOperatorTable.SUM0;
    } else if (udafMap != null && udafMap.containsKey(callName)) {
        newAgg = createCustomAggFunction(callName, fieldType, udafMap.get(callName));
    }

    // rebuild parameters
    List<Integer> newArgList = Lists.newArrayList(aggCall.getArgList());
    if (udafMap != null && udafMap.containsKey(callName)) {
        newArgList = truncArgList(newArgList, udafMap.get(callName));
    }
    if (func.needRewriteField()) {
        RelDataTypeField field = getInput().getRowType().getField(func.getRewriteFieldName(), true, false);
        if (newArgList.isEmpty()) {
            newArgList.add(field.getIndex());
        } else {
            // TODO: only the first column got overwritten
            newArgList.set(0, field.getIndex());
        }
    }

    // rebuild aggregate call
    AggregateCall newAggCall = new AggregateCall(newAgg, false, newArgList, fieldType, callName);

    return newAggCall;
}
 
Example 6
Source File: MaterializedViewAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Get rollup aggregation function.
 */
protected SqlAggFunction getRollup(SqlAggFunction aggregation) {
  if (aggregation == SqlStdOperatorTable.SUM
      || aggregation == SqlStdOperatorTable.MIN
      || aggregation == SqlStdOperatorTable.MAX
      || aggregation == SqlStdOperatorTable.SUM0
      || aggregation == SqlStdOperatorTable.ANY_VALUE) {
    return aggregation;
  } else if (aggregation == SqlStdOperatorTable.COUNT) {
    return SqlStdOperatorTable.SUM0;
  } else {
    return null;
  }
}
 
Example 7
Source File: AggregateUnionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private List<AggregateCall> transformAggCalls(RelNode input, int groupCount,
    List<AggregateCall> origCalls) {
  final List<AggregateCall> newCalls = new ArrayList<>();
  for (Ord<AggregateCall> ord : Ord.zip(origCalls)) {
    final AggregateCall origCall = ord.e;
    if (origCall.isDistinct()
        || !SUPPORTED_AGGREGATES.containsKey(origCall.getAggregation()
            .getClass())) {
      return null;
    }
    final SqlAggFunction aggFun;
    final RelDataType aggType;
    if (origCall.getAggregation() == SqlStdOperatorTable.COUNT) {
      aggFun = SqlStdOperatorTable.SUM0;
      // count(any) is always not null, however nullability of sum might
      // depend on the number of columns in GROUP BY.
      // Here we use SUM0 since we are sure we will not face nullable
      // inputs nor we'll face empty set.
      aggType = null;
    } else {
      aggFun = origCall.getAggregation();
      aggType = origCall.getType();
    }
    AggregateCall newCall =
        AggregateCall.create(aggFun, origCall.isDistinct(),
            origCall.isApproximate(), origCall.ignoreNulls(),
            ImmutableList.of(groupCount + ord.i), -1, origCall.collation,
            groupCount, input, aggType, origCall.getName());
    newCalls.add(newCall);
  }
  return newCalls;
}
 
Example 8
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlAggFunction getRollup(SqlAggFunction aggregation) {
  if (aggregation == SqlStdOperatorTable.SUM
      || aggregation == SqlStdOperatorTable.MIN
      || aggregation == SqlStdOperatorTable.MAX
      || aggregation == SqlStdOperatorTable.SUM0
      || aggregation == SqlStdOperatorTable.ANY_VALUE) {
    return aggregation;
  } else if (aggregation == SqlStdOperatorTable.COUNT) {
    return SqlStdOperatorTable.SUM0;
  } else {
    return null;
  }
}
 
Example 9
Source File: SqlSplittableAggFunction.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public SqlAggFunction getMergeAggFunctionOfTopSplit() {
  return SqlStdOperatorTable.SUM0;
}
 
Example 10
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public SqlCall toSql(RexProgram program, RexOver rexOver) {
  final RexWindow rexWindow = rexOver.getWindow();
  final SqlNodeList partitionList = new SqlNodeList(
    toSql(program, rexWindow.partitionKeys), POS);

  ImmutableList.Builder<SqlNode> orderNodes = ImmutableList.builder();
  if (rexWindow.orderKeys != null) {
    for (RexFieldCollation rfc : rexWindow.orderKeys) {
      // Omit ORDER BY <ordinal> clauses, which are parsed by Calcite but not actually
      // used for sorting.
      if (!(rfc.getKey() instanceof RexLiteral)) {
        if (rfc.getNullDirection() != dialect.defaultNullDirection(rfc.getDirection())) {
          // Get the SQL Node for the column being sorted on only.
          final SqlNode orderingColumnNode = toSql(program, rfc.left);

          final SqlNode emulatedNullDirNode = dialect.emulateNullDirection(orderingColumnNode,
            rfc.getNullDirection() == RelFieldCollation.NullDirection.FIRST, rfc.getDirection().isDescending());
          if (emulatedNullDirNode != null) {
            // Dialect requires emulating null direction.
            // Put the emulation in the order list first, then the ordering on the column only.
            orderNodes.add(emulatedNullDirNode);
            orderNodes.add(orderingColumnNode);
          } else {
            // Dialect implements NULLS FIRST and NULLS LAST clauses. These will get
            // unparsed as part of the RexFieldCollation.
            orderNodes.add(toSql(program, rfc));
          }
        } else {
          orderNodes.add(toSql(program, rfc));
        }
      }
    }
  }

  final SqlNodeList orderList =
    new SqlNodeList(orderNodes.build(), POS);

  final SqlLiteral isRows =
    SqlLiteral.createBoolean(rexWindow.isRows(), POS);

  final SqlNode lowerBound;
  final SqlNode upperBound;

  // Remove unnecessary Window Frames. When Calcite parses an OVER clause with no frame,
  final boolean hasUnnecessaryFrame = getDialect().removeDefaultWindowFrame(rexOver)
    && OverUtils.hasDefaultFrame(rexOver);

  if (hasUnnecessaryFrame) {
    lowerBound = null;
    upperBound = null;
  } else {
    lowerBound = createSqlWindowBound(rexWindow.getLowerBound());
    upperBound = createSqlWindowBound(rexWindow.getUpperBound());
  }

  // null defaults to true.
  // During parsing the allowPartial == false (e.g. disallow partial)
  // is expand into CASE expression and is handled as a such.
  // Not sure if we can collapse this CASE expression back into
  // "disallow partial" and set the allowPartial = false.
  final SqlLiteral allowPartial = null;

  final SqlWindow sqlWindow = getDremioRelToSqlConverter().adjustWindowForSource(
    this, rexOver.getAggOperator(), SqlWindow.create(
      null, null, partitionList,
      orderList, isRows, lowerBound, upperBound, allowPartial, POS));

  final List<SqlNode> nodeList = toSql(program, rexOver.getOperands());

  // Create the call for the aggregate in the window function.
  // If it happens to be a SUM0 call, we need to swap that with our own version which sets
  // the name to just SUM, rather than $SUM0, so that it can be translated to SQL compatible
  // with RDBMSes.
  final SqlAggFunction operator = rexOver.getAggOperator() != SqlStdOperatorTable.SUM0 ?
    rexOver.getAggOperator() : SUM0_FUNCTION;

  final SqlCall aggFunctionCall = operator.createCall(POS, nodeList);

  return SqlStdOperatorTable.OVER.createCall(POS, aggFunctionCall,
    sqlWindow);
}
 
Example 11
Source File: SqlSplittableAggFunction.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlAggFunction getMergeAggFunctionOfTopSplit() {
  return SqlStdOperatorTable.SUM0;
}