org.apache.calcite.sql.SqlAggFunction Java Examples
The following examples show how to use
org.apache.calcite.sql.SqlAggFunction.
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: RelNodeCompiler.java From streamline with Apache License 2.0 | 6 votes |
private void aggregate(AggregateCall call) { SqlAggFunction aggFunction = call.getAggregation(); String aggregationName = call.getAggregation().getName(); Type ty = typeFactory.getJavaClass(call.getType()); if (call.getArgList().size() != 1) { if (aggregationName.equals("COUNT")) { if (call.getArgList().size() != 0) { throw new UnsupportedOperationException("Count with nullable fields"); } } } if (aggFunction instanceof SqlUserDefinedAggFunction) { AggregateFunction aggregateFunction = ((SqlUserDefinedAggFunction) aggFunction).function; doAggregate((AggregateFunctionImpl) aggregateFunction, reserveAggVarName(call), ty, call.getArgList()); } else { List<BuiltinAggregateFunctions.TypeClass> typeClasses = BuiltinAggregateFunctions.TABLE.get(aggregationName); if (typeClasses == null) { throw new UnsupportedOperationException(aggregationName + " Not implemented"); } doAggregate(AggregateFunctionImpl.create(findMatchingClass(aggregationName, typeClasses, ty)), reserveAggVarName(call), ty, call.getArgList()); } }
Example #2
Source File: RexUtil.java From calcite with Apache License 2.0 | 6 votes |
/** * Returns whether an array of exp contains no aggregate function calls whose * arguments are not {@link RexInputRef}s. * * @param exprs Expressions * @param litmus Whether to assert if there is such a function call */ static boolean containNoNonTrivialAggs(List<RexNode> exprs, Litmus litmus) { for (RexNode expr : exprs) { if (expr instanceof RexCall) { RexCall rexCall = (RexCall) expr; if (rexCall.getOperator() instanceof SqlAggFunction) { for (RexNode operand : rexCall.operands) { if (!(operand instanceof RexLocalRef) && !(operand instanceof RexLiteral)) { return litmus.fail("contains non trivial agg: {}", operand); } } } } } return litmus.succeed(); }
Example #3
Source File: OverUtils.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Indicates if the given OVER clause has a window frame that is automatically added by * Calcite if the frame is not specified. */ public static boolean hasDefaultFrame(SqlAggFunction operator, boolean isRows, RexWindowBound lowerBound, RexWindowBound upperBound, int oerderKeyCount) { // When Calcite parses an OVER clause with no frame, // it inject a 'default' frame depending on the function. // 1. For ROW_NUMBER(), it generates ROWS UNBOUNDED PRECEDING and CURRENT ROW. // 2. For others, it generates RANGE UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING if unsorted. // 3. If it's not ROW_NUMBER(), and it is sorted, Calcite uses RANGE UNBOUNDED PRECEDING AND CURRENT ROW // Adding these unnecessary frames cause some RDBMSes (eg SQL Server) to fail. // // This code happens in SqlToRelConverter.convertOver(), SqlValidatorImpl.resolveWindow(), // and SqlWindow.create()/SqlWindow.populateBounds(). return // Note: intentionally not simplifying this boolean for clarity. (operator == SqlStdOperatorTable.ROW_NUMBER && isRows && lowerBound.isUnbounded() && lowerBound.isPreceding() && upperBound.isCurrentRow()) // First condition. || (!isRows && (oerderKeyCount == 0) && lowerBound.isUnbounded() && lowerBound.isPreceding() && upperBound.isUnbounded() && upperBound.isFollowing()) // Second condition. || (!isRows && //(oerderKeyCount != 0) && lowerBound.isUnbounded() && lowerBound.isPreceding() && !upperBound.isUnbounded() && upperBound.isCurrentRow()); // Third condition. }
Example #4
Source File: AggregateReduceFunctionsRule.java From Bats with Apache License 2.0 | 6 votes |
private AggregateCall createAggregateCallWithBinding( RelDataTypeFactory typeFactory, SqlAggFunction aggFunction, RelDataType operandType, Aggregate oldAggRel, AggregateCall oldCall, int argOrdinal, int filter) { final Aggregate.AggCallBinding binding = new Aggregate.AggCallBinding(typeFactory, aggFunction, ImmutableList.of(operandType), oldAggRel.getGroupCount(), filter >= 0); return AggregateCall.create(aggFunction, oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argOrdinal), filter, oldCall.collation, aggFunction.inferReturnType(binding), null); }
Example #5
Source File: QueryOperationConverter.java From flink with Apache License 2.0 | 6 votes |
@Override public RelBuilder.AggCall visit(CallExpression call) { FunctionDefinition def = call.getFunctionDefinition(); if (BuiltInFunctionDefinitions.DISTINCT == def) { Expression innerAgg = call.getChildren().get(0); return innerAgg.accept(new AggCallVisitor(relBuilder, expressionConverter, name, true)); } else { SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor); return relBuilder.aggregateCall( sqlAggFunction, isDistinct, false, null, name, call.getChildren().stream().map(expr -> expr.accept(expressionConverter)) .collect(Collectors.toList())); } }
Example #6
Source File: SqlImplementor.java From dremio-oss with Apache License 2.0 | 6 votes |
protected boolean hasNestedAggregations(LogicalAggregate rel) { List<AggregateCall> aggCallList = rel.getAggCallList(); HashSet<Integer> aggregatesArgs = new HashSet<>(); for (AggregateCall aggregateCall : aggCallList) { aggregatesArgs.addAll(aggregateCall.getArgList()); } for (Integer aggregatesArg : aggregatesArgs) { SqlNode selectNode = ((SqlSelect) node).getSelectList().get(aggregatesArg); if (!(selectNode instanceof SqlBasicCall)) { continue; } for (SqlNode operand : ((SqlBasicCall) selectNode).getOperands()) { if (operand instanceof SqlCall) { final SqlOperator operator = ((SqlCall) operand).getOperator(); if (operator instanceof SqlAggFunction) { return true; } } } } return false; }
Example #7
Source File: AggregateCall.java From calcite with Apache License 2.0 | 6 votes |
/** Creates an AggregateCall, inferring its type if {@code type} is null. */ public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, boolean approximate, boolean ignoreNulls, List<Integer> argList, int filterArg, RelCollation collation, int groupCount, RelNode input, RelDataType type, String name) { if (type == null) { final RelDataTypeFactory typeFactory = input.getCluster().getTypeFactory(); final List<RelDataType> types = SqlTypeUtil.projectTypes(input.getRowType(), argList); final Aggregate.AggCallBinding callBinding = new Aggregate.AggCallBinding(typeFactory, aggFunction, types, groupCount, filterArg >= 0); type = aggFunction.inferReturnType(callBinding); } return create(aggFunction, distinct, approximate, ignoreNulls, argList, filterArg, collation, type, name); }
Example #8
Source File: RelJsonReader.java From calcite with Apache License 2.0 | 6 votes |
private AggregateCall toAggCall(RelInput relInput, Map<String, Object> jsonAggCall) { final Map<String, Object> aggMap = (Map) jsonAggCall.get("agg"); final SqlAggFunction aggregation = relJson.toAggregation(aggMap); final Boolean distinct = (Boolean) jsonAggCall.get("distinct"); @SuppressWarnings("unchecked") final List<Integer> operands = (List<Integer>) jsonAggCall.get("operands"); final Integer filterOperand = (Integer) jsonAggCall.get("filter"); final RelDataType type = relJson.toType(cluster.getTypeFactory(), jsonAggCall.get("type")); final String name = (String) jsonAggCall.get("name"); return AggregateCall.create(aggregation, distinct, false, false, operands, filterOperand == null ? -1 : filterOperand, RelCollations.EMPTY, type, name); }
Example #9
Source File: QueryOperationConverter.java From flink with Apache License 2.0 | 6 votes |
@Override public RelBuilder.AggCall visit(CallExpression call) { FunctionDefinition def = call.getFunctionDefinition(); if (BuiltInFunctionDefinitions.DISTINCT == def) { Expression innerAgg = call.getChildren().get(0); return innerAgg.accept(new AggCallVisitor(relBuilder, rexNodeConverter, name, true)); } else { SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor); return relBuilder.aggregateCall( sqlAggFunction, isDistinct, false, null, name, call.getChildren().stream().map(expr -> expr.accept(rexNodeConverter)) .collect(Collectors.toList())); } }
Example #10
Source File: SqlImplementor.java From calcite with Apache License 2.0 | 6 votes |
private boolean hasNestedAggregations(Aggregate rel) { if (node instanceof SqlSelect) { final SqlNodeList selectList = ((SqlSelect) node).getSelectList(); if (selectList != null) { final Set<Integer> aggregatesArgs = new HashSet<>(); for (AggregateCall aggregateCall : rel.getAggCallList()) { aggregatesArgs.addAll(aggregateCall.getArgList()); } for (int aggregatesArg : aggregatesArgs) { if (selectList.get(aggregatesArg) instanceof SqlBasicCall) { final SqlBasicCall call = (SqlBasicCall) selectList.get(aggregatesArg); for (SqlNode operand : call.getOperands()) { if (operand instanceof SqlCall && ((SqlCall) operand).getOperator() instanceof SqlAggFunction) { return true; } } } } } } return false; }
Example #11
Source File: DremioRelToSqlConverter.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Returns a SqlWindow that adds ORDER BY if op is one of the given functions. */ protected static SqlWindow addDummyOrderBy(SqlWindow window, DremioContext context, SqlAggFunction op, List<SqlAggFunction> opsToAddClauseFor) { if (!SqlNodeList.isEmptyList(window.getOrderList())) { return window; } // Add the ORDER BY if op is one of the given functions. for (SqlAggFunction function : opsToAddClauseFor) { if (function == op) { SqlNodeList dummyOrderByList = new SqlNodeList(POS); dummyOrderByList.add(context.field(0)); return SqlWindow.create(window.getDeclName(), window.getRefName(), window.getPartitionList(), dummyOrderByList, SqlLiteral.createBoolean(window.isRows(), POS), window.getLowerBound(), window.getUpperBound(), SqlLiteral.createBoolean(window.isAllowPartial(), POS), POS); } } return window; }
Example #12
Source File: AggregateCall.java From calcite with Apache License 2.0 | 6 votes |
/** * Creates an AggregateCall. * * @param aggFunction Aggregate function * @param distinct Whether distinct * @param approximate Whether approximate * @param argList List of ordinals of arguments * @param filterArg Ordinal of filter argument (the * {@code FILTER (WHERE ...)} clause in SQL), or -1 * @param collation How to sort values before aggregation (the * {@code WITHIN GROUP} clause in SQL) * @param type Result type * @param name Name (may be null) */ private AggregateCall(SqlAggFunction aggFunction, boolean distinct, boolean approximate, boolean ignoreNulls, List<Integer> argList, int filterArg, RelCollation collation, RelDataType type, String name) { this.type = Objects.requireNonNull(type); this.name = name; this.aggFunction = Objects.requireNonNull(aggFunction); this.argList = ImmutableList.copyOf(argList); this.filterArg = filterArg; this.collation = Objects.requireNonNull(collation); this.distinct = distinct; this.approximate = approximate; this.ignoreNulls = ignoreNulls; Preconditions.checkArgument( aggFunction.getDistinctOptionality() != Optionality.IGNORED || !distinct, "DISTINCT has no effect for this aggregate function, so must be false"); Preconditions.checkArgument(filterArg < 0 || aggFunction.allowsFilter()); }
Example #13
Source File: RelBuilder.java From Bats with Apache License 2.0 | 6 votes |
AggCallImpl(SqlAggFunction aggFunction, boolean distinct, boolean approximate, RexNode filter, String alias, ImmutableList<RexNode> operands, ImmutableList<RexNode> orderKeys) { this.aggFunction = Objects.requireNonNull(aggFunction); this.distinct = distinct; this.approximate = approximate; this.alias = alias; this.operands = Objects.requireNonNull(operands); this.orderKeys = Objects.requireNonNull(orderKeys); if (filter != null) { if (filter.getType().getSqlTypeName() != SqlTypeName.BOOLEAN) { throw RESOURCE.filterMustBeBoolean().ex(); } if (filter.getType().isNullable()) { filter = call(SqlStdOperatorTable.IS_TRUE, filter); } } this.filter = filter; }
Example #14
Source File: Window.java From calcite with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public RexWinAggCall( SqlAggFunction aggFun, RelDataType type, List<RexNode> operands, int ordinal, boolean distinct) { this(aggFun, type, operands, ordinal, distinct, false); }
Example #15
Source File: MongoAggregate.java From calcite with Apache License 2.0 | 5 votes |
private String toMongo(SqlAggFunction aggregation, List<String> inNames, List<Integer> args) { if (aggregation == SqlStdOperatorTable.COUNT) { if (args.size() == 0) { return "{$sum: 1}"; } else { assert args.size() == 1; final String inName = inNames.get(args.get(0)); return "{$sum: {$cond: [ {$eq: [" + MongoRules.quote(inName) + ", null]}, 0, 1]}}"; } } else if (aggregation instanceof SqlSumAggFunction || aggregation instanceof SqlSumEmptyIsZeroAggFunction) { assert args.size() == 1; final String inName = inNames.get(args.get(0)); return "{$sum: " + MongoRules.maybeQuote("$" + inName) + "}"; } else if (aggregation == SqlStdOperatorTable.MIN) { assert args.size() == 1; final String inName = inNames.get(args.get(0)); return "{$min: " + MongoRules.maybeQuote("$" + inName) + "}"; } else if (aggregation == SqlStdOperatorTable.MAX) { assert args.size() == 1; final String inName = inNames.get(args.get(0)); return "{$max: " + MongoRules.maybeQuote("$" + inName) + "}"; } else if (aggregation == SqlStdOperatorTable.AVG) { assert args.size() == 1; final String inName = inNames.get(args.get(0)); return "{$avg: " + MongoRules.maybeQuote("$" + inName) + "}"; } else { throw new AssertionError("unknown aggregate " + aggregation); } }
Example #16
Source File: OLAPAggregateRel.java From kylin with Apache License 2.0 | 5 votes |
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) { RelDataTypeFactory typeFactory = getCluster().getTypeFactory(); SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1)); AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz); List<RelDataType> argTypes = new ArrayList<RelDataType>(); List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>(); for (FunctionParameter o : aggFunction.getParameters()) { final RelDataType type = o.getType(typeFactory); argTypes.add(type); typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY)); } return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType), InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false, typeFactory); }
Example #17
Source File: JdbcRelBuilder.java From calcite-sql-rewriter with Apache License 2.0 | 5 votes |
public RexNode makeOver( SqlAggFunction operator, List<RexNode> expressions, List<RexNode> partitionKeys ) { final Set<SqlKind> flags = EnumSet.noneOf(SqlKind.class); // TODO // This is a temporal fix to make HAWQ work with OVER + UNLIMITED BOUNDS // HAWQ requires ORDER BY if andy BOUNDS are set even unlimited upper and lower BOUNDS (which is equal to // the entire partition - e.g. not setting BOUNDs at all -- // Note that the unnecessary ORDER BY have negative performance impact and has to be remove once either HAWQ // start supporting unbounded bounds without order by or Calcite can generate shorthand OVER PARTITION BY // syntax. List<RexFieldCollation> orderKeys = expressions.stream().map( rexNode -> new RexFieldCollation(rexNode, flags)).collect(Collectors.toList()); return makeOver( operator, expressions, partitionKeys, ImmutableList.copyOf(orderKeys), RexWindowBound.create(SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO), null), RexWindowBound.create(SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO), null), true, true, false ); }
Example #18
Source File: QueryOperationConverter.java From flink with Apache License 2.0 | 5 votes |
@Override public RelBuilder.AggCall visit(CallExpression call) { SqlAggFunction sqlAggFunction = call.accept(sqlAggFunctionVisitor); return relBuilder.aggregateCall( sqlAggFunction, false, false, null, sqlAggFunction.toString(), call.getChildren().stream().map(expr -> expr.accept(rexNodeConverter)).collect(toList())); }
Example #19
Source File: Window.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a RexWinAggCall. * * @param aggFun Aggregate function * @param type Result type * @param operands Operands to call * @param ordinal Ordinal within its partition * @param distinct Eliminate duplicates before applying aggregate function */ public RexWinAggCall( SqlAggFunction aggFun, RelDataType type, List<RexNode> operands, int ordinal, boolean distinct, boolean ignoreNulls) { super(type, aggFun, operands); this.ordinal = ordinal; this.distinct = distinct; this.ignoreNulls = ignoreNulls; }
Example #20
Source File: HiveSqlOperatorTable.java From marble with Apache License 2.0 | 5 votes |
private SqlOperator getOperatorInSqlStdOperatorTable(String opName, SqlSyntax sqlSyntax, boolean isAgg) { List<SqlOperator> ops = operatorMapOfSqlStdOperatorTable.get( opName + "_" + sqlSyntax); for (SqlOperator op : ops) { if (isAgg && op instanceof SqlAggFunction) { return op; } if (!isAgg && !(op instanceof SqlAggFunction)) { return op; } } return null; }
Example #21
Source File: AggregateCall.java From calcite with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, boolean approximate, List<Integer> argList, int filterArg, int groupCount, RelNode input, RelDataType type, String name) { return create(aggFunction, distinct, approximate, false, argList, filterArg, RelCollations.EMPTY, groupCount, input, type, name); }
Example #22
Source File: RelBuilder.java From calcite with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public AggCall aggregateCall(SqlAggFunction aggFunction, boolean distinct, boolean approximate, RexNode filter, String alias, Iterable<? extends RexNode> operands) { return aggregateCall(aggFunction, distinct, approximate, false, filter, ImmutableList.of(), alias, ImmutableList.copyOf(operands)); }
Example #23
Source File: DrillReduceAggregatesRule.java From Bats with Apache License 2.0 | 5 votes |
/** * Returns whether any of the aggregates are calls to AVG, STDDEV_*, VAR_*. * * @param aggCallList List of aggregate calls */ private boolean containsAvgStddevVarCall(List<AggregateCall> aggCallList) { for (AggregateCall call : aggCallList) { SqlAggFunction sqlAggFunction = DrillCalciteWrapperUtility.extractSqlOperatorFromWrapper(call.getAggregation()); if (sqlAggFunction instanceof SqlAvgAggFunction || sqlAggFunction instanceof SqlSumAggFunction) { return true; } } return false; }
Example #24
Source File: OLAPAggregateRule.java From kylin with Apache License 2.0 | 5 votes |
private boolean containsAvg(LogicalAggregate agg) { for (AggregateCall call : agg.getAggCallList()) { SqlAggFunction func = call.getAggregation(); if (func instanceof SqlAvgAggFunction) return true; } return false; }
Example #25
Source File: SubstitutionVisitor.java From calcite with Apache License 2.0 | 5 votes |
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 #26
Source File: LatticeSuggester.java From Bats with Apache License 2.0 | 5 votes |
private MutableMeasure(SqlAggFunction aggregate, boolean distinct, List<ColRef> arguments, String name) { this.aggregate = aggregate; this.arguments = arguments; this.distinct = distinct; this.name = name; }
Example #27
Source File: SubstitutionVisitor.java From Bats with Apache License 2.0 | 5 votes |
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 #28
Source File: RexImpTable.java From calcite with Apache License 2.0 | 5 votes |
@Override public void implementNotNullAdd(AggContext info, AggAddContext add) { Expression acc = add.accumulator().get(0); Expression arg = add.arguments().get(0); SqlAggFunction aggregation = info.aggregation(); final Method method = (aggregation == MIN ? BuiltInMethod.LESSER : BuiltInMethod.GREATER).method; Expression next = Expressions.call( method.getDeclaringClass(), method.getName(), acc, Expressions.unbox(arg)); accAdvance(add, acc, next); }
Example #29
Source File: SqlImplementor.java From dremio-oss with Apache License 2.0 | 5 votes |
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 #30
Source File: AggregateCall.java From calcite with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, List<Integer> argList, int filterArg, int groupCount, RelNode input, RelDataType type, String name) { return create(aggFunction, distinct, false, false, argList, filterArg, RelCollations.EMPTY, groupCount, input, type, name); }