Search in sources :

Example 36 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project drill by apache.

the class DrillOperatorTable method populateWrappedCalciteOperators.

private void populateWrappedCalciteOperators() {
    for (SqlOperator calciteOperator : inner.getOperatorList()) {
        final SqlOperator wrapper;
        if (calciteOperator instanceof SqlAggFunction) {
            wrapper = new DrillCalciteSqlAggFunctionWrapper((SqlAggFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
        } else if (calciteOperator instanceof SqlFunction) {
            wrapper = new DrillCalciteSqlFunctionWrapper((SqlFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
        } else if (calciteOperator instanceof SqlBetweenOperator) {
            // During the procedure of converting to RexNode,
            // StandardConvertletTable.convertBetween expects the SqlOperator to be a subclass of SqlBetweenOperator
            final SqlBetweenOperator sqlBetweenOperator = (SqlBetweenOperator) calciteOperator;
            wrapper = new DrillCalciteSqlBetweenOperatorWrapper(sqlBetweenOperator);
        } else {
            final String drillOpName;
            // Otherwise, Calcite will mix them up with binary operator subtract (-) or add (+)
            if (calciteOperator == SqlStdOperatorTable.UNARY_MINUS || calciteOperator == SqlStdOperatorTable.UNARY_PLUS) {
                drillOpName = calciteOperator.getName();
            } else {
                drillOpName = FunctionCallFactory.convertToDrillFunctionName(calciteOperator.getName());
            }
            final List<DrillFuncHolder> drillFuncHolders = getFunctionListWithInference(drillOpName);
            if (drillFuncHolders.isEmpty()) {
                continue;
            }
            wrapper = new DrillCalciteSqlOperatorWrapper(calciteOperator, drillOpName, drillFuncHolders);
        }
        calciteToWrapper.put(calciteOperator, wrapper);
    }
}
Also used : DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlBetweenOperator(org.apache.calcite.sql.fun.SqlBetweenOperator) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 37 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project flink by apache.

the class HiveParserUtils method toAggCall.

public static AggregateCall toAggCall(HiveParserBaseSemanticAnalyzer.AggInfo aggInfo, HiveParserRexNodeConverter converter, Map<String, Integer> rexNodeToPos, int groupCount, RelNode input, RelOptCluster cluster, SqlFunctionConverter funcConverter) throws SemanticException {
    // 1. Get agg fn ret type in Calcite
    RelDataType aggFnRetType = HiveParserUtils.toRelDataType(aggInfo.getReturnType(), cluster.getTypeFactory());
    // 2. Convert Agg Fn args and type of args to Calcite
    // TODO: Does HQL allows expressions as aggregate args or can it only be projections from
    // child?
    List<Integer> argIndices = new ArrayList<>();
    RelDataTypeFactory typeFactory = cluster.getTypeFactory();
    List<RelDataType> calciteArgTypes = new ArrayList<>();
    for (ExprNodeDesc expr : aggInfo.getAggParams()) {
        RexNode paramRex = converter.convert(expr).accept(funcConverter);
        Integer argIndex = Preconditions.checkNotNull(rexNodeToPos.get(paramRex.toString()));
        argIndices.add(argIndex);
        // TODO: does arg need type cast?
        calciteArgTypes.add(HiveParserUtils.toRelDataType(expr.getTypeInfo(), typeFactory));
    }
    // 3. Get Aggregation FN from Calcite given name, ret type and input arg type
    final SqlAggFunction aggFunc = HiveParserSqlFunctionConverter.getCalciteAggFn(aggInfo.getUdfName(), aggInfo.isDistinct(), calciteArgTypes, aggFnRetType);
    // If we have input arguments, set type to null (instead of aggFnRetType) to let
    // AggregateCall
    // infer the type, so as to avoid nullability mismatch
    RelDataType type = null;
    if (aggInfo.isAllColumns() && argIndices.isEmpty()) {
        type = aggFnRetType;
    }
    return AggregateCall.create((SqlAggFunction) funcConverter.convertOperator(aggFunc), aggInfo.isDistinct(), false, false, argIndices, -1, RelCollations.EMPTY, groupCount, input, type, aggInfo.getAlias());
}
Also used : ArrayList(java.util.ArrayList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) RexNode(org.apache.calcite.rex.RexNode)

Example 38 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project flink by apache.

the class SqlFunctionConverter method visitOver.

@Override
public RexNode visitOver(RexOver over) {
    SqlOperator operator = convertOperator(over.getAggOperator());
    Preconditions.checkArgument(operator instanceof SqlAggFunction, "Expect converted operator to be an agg function, but got " + operator.toString());
    SqlAggFunction convertedAgg = (SqlAggFunction) operator;
    RexWindow window = over.getWindow();
    // let's not rely on the type of the RexOver created by Hive since it can be different from
    // what Flink expects
    RelDataType inferredType = builder.makeCall(convertedAgg, over.getOperands()).getType();
    // Hive may add literals to partition keys, remove them
    List<RexNode> partitionKeys = new ArrayList<>();
    for (RexNode hivePartitionKey : getPartKeys(window)) {
        if (!(hivePartitionKey instanceof RexLiteral)) {
            partitionKeys.add(hivePartitionKey);
        }
    }
    List<RexFieldCollation> convertedOrderKeys = new ArrayList<>(getOrderKeys(window).size());
    for (RexFieldCollation orderKey : getOrderKeys(window)) {
        convertedOrderKeys.add(new RexFieldCollation(orderKey.getKey().accept(this), orderKey.getValue()));
    }
    final boolean[] update = null;
    return HiveParserUtils.makeOver(builder, inferredType, convertedAgg, visitList(over.getOperands(), update), visitList(partitionKeys, update), convertedOrderKeys, window.getLowerBound(), window.getUpperBound(), window.isRows(), true, false, false, false);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) RexWindow(org.apache.calcite.rex.RexWindow) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) RexNode(org.apache.calcite.rex.RexNode)

Example 39 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project flink by apache.

the class HiveParserCalcitePlanner method getWindowRexAndType.

private Pair<RexNode, TypeInfo> getWindowRexAndType(HiveParserWindowingSpec.WindowExpressionSpec winExprSpec, RelNode srcRel) throws SemanticException {
    RexNode window;
    if (winExprSpec instanceof HiveParserWindowingSpec.WindowFunctionSpec) {
        HiveParserWindowingSpec.WindowFunctionSpec wFnSpec = (HiveParserWindowingSpec.WindowFunctionSpec) winExprSpec;
        HiveParserASTNode windowProjAst = wFnSpec.getExpression();
        // TODO: do we need to get to child?
        int wndSpecASTIndx = getWindowSpecIndx(windowProjAst);
        // 2. Get Hive Aggregate Info
        AggInfo hiveAggInfo = getHiveAggInfo(windowProjAst, wndSpecASTIndx - 1, relToRowResolver.get(srcRel), (HiveParserWindowingSpec.WindowFunctionSpec) winExprSpec, semanticAnalyzer, frameworkConfig, cluster);
        // 3. Get Calcite Return type for Agg Fn
        RelDataType calciteAggFnRetType = HiveParserUtils.toRelDataType(hiveAggInfo.getReturnType(), cluster.getTypeFactory());
        // 4. Convert Agg Fn args to Calcite
        Map<String, Integer> posMap = relToHiveColNameCalcitePosMap.get(srcRel);
        HiveParserRexNodeConverter converter = new HiveParserRexNodeConverter(cluster, srcRel.getRowType(), posMap, 0, false, funcConverter);
        List<RexNode> calciteAggFnArgs = new ArrayList<>();
        List<RelDataType> calciteAggFnArgTypes = new ArrayList<>();
        for (int i = 0; i < hiveAggInfo.getAggParams().size(); i++) {
            calciteAggFnArgs.add(converter.convert(hiveAggInfo.getAggParams().get(i)));
            calciteAggFnArgTypes.add(HiveParserUtils.toRelDataType(hiveAggInfo.getAggParams().get(i).getTypeInfo(), cluster.getTypeFactory()));
        }
        // 5. Get Calcite Agg Fn
        final SqlAggFunction calciteAggFn = HiveParserSqlFunctionConverter.getCalciteAggFn(hiveAggInfo.getUdfName(), hiveAggInfo.isDistinct(), calciteAggFnArgTypes, calciteAggFnRetType);
        // 6. Translate Window spec
        HiveParserRowResolver inputRR = relToRowResolver.get(srcRel);
        HiveParserWindowingSpec.WindowSpec wndSpec = ((HiveParserWindowingSpec.WindowFunctionSpec) winExprSpec).getWindowSpec();
        List<RexNode> partitionKeys = getPartitionKeys(wndSpec.getPartition(), converter, inputRR, new HiveParserTypeCheckCtx(inputRR, frameworkConfig, cluster), semanticAnalyzer);
        List<RexFieldCollation> orderKeys = getOrderKeys(wndSpec.getOrder(), converter, inputRR, new HiveParserTypeCheckCtx(inputRR, frameworkConfig, cluster), semanticAnalyzer);
        RexWindowBound lowerBound = getBound(wndSpec.getWindowFrame().getStart(), cluster);
        RexWindowBound upperBound = getBound(wndSpec.getWindowFrame().getEnd(), cluster);
        boolean isRows = wndSpec.getWindowFrame().getWindowType() == HiveParserWindowingSpec.WindowType.ROWS;
        window = HiveParserUtils.makeOver(cluster.getRexBuilder(), calciteAggFnRetType, calciteAggFn, calciteAggFnArgs, partitionKeys, orderKeys, lowerBound, upperBound, isRows, true, false, false, false);
        window = window.accept(funcConverter);
    } else {
        throw new SemanticException("Unsupported window Spec");
    }
    return new Pair<>(window, HiveParserTypeConverter.convert(window.getType()));
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) HiveParserRowResolver(org.apache.flink.table.planner.delegation.hive.copy.HiveParserRowResolver) RexWindowBound(org.apache.calcite.rex.RexWindowBound) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) Pair(org.apache.calcite.util.Pair) ObjectPair(org.apache.hadoop.hive.common.ObjectPair) HiveParserASTNode(org.apache.flink.table.planner.delegation.hive.copy.HiveParserASTNode) HiveParserBaseSemanticAnalyzer.getHiveAggInfo(org.apache.flink.table.planner.delegation.hive.copy.HiveParserBaseSemanticAnalyzer.getHiveAggInfo) AggInfo(org.apache.flink.table.planner.delegation.hive.copy.HiveParserBaseSemanticAnalyzer.AggInfo) HiveParserWindowingSpec(org.apache.flink.table.planner.delegation.hive.copy.HiveParserWindowingSpec) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) HiveParserTypeCheckCtx(org.apache.flink.table.planner.delegation.hive.copy.HiveParserTypeCheckCtx) RexNode(org.apache.calcite.rex.RexNode)

Example 40 with SqlAggFunction

use of org.apache.calcite.sql.SqlAggFunction in project flink by apache.

the class OverConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    List<Expression> children = call.getChildren();
    if (call.getFunctionDefinition() == BuiltInFunctionDefinitions.OVER) {
        FlinkTypeFactory typeFactory = context.getTypeFactory();
        Expression agg = children.get(0);
        FunctionDefinition def = ((CallExpression) agg).getFunctionDefinition();
        boolean isDistinct = BuiltInFunctionDefinitions.DISTINCT == def;
        SqlAggFunction aggFunc = agg.accept(new SqlAggFunctionVisitor(context.getRelBuilder()));
        RelDataType aggResultType = typeFactory.createFieldTypeFromLogicalType(fromDataTypeToLogicalType(((ResolvedExpression) agg).getOutputDataType()));
        // assemble exprs by agg children
        List<RexNode> aggExprs = agg.getChildren().stream().map(child -> {
            if (isDistinct) {
                return context.toRexNode(child.getChildren().get(0));
            } else {
                return context.toRexNode(child);
            }
        }).collect(Collectors.toList());
        // assemble order by key
        Expression orderKeyExpr = children.get(1);
        Set<SqlKind> kinds = new HashSet<>();
        RexNode collationRexNode = createCollation(context.toRexNode(orderKeyExpr), RelFieldCollation.Direction.ASCENDING, null, kinds);
        ImmutableList<RexFieldCollation> orderKey = ImmutableList.of(new RexFieldCollation(collationRexNode, kinds));
        // assemble partition by keys
        List<RexNode> partitionKeys = children.subList(4, children.size()).stream().map(context::toRexNode).collect(Collectors.toList());
        // assemble bounds
        Expression preceding = children.get(2);
        boolean isPhysical = fromDataTypeToLogicalType(((ResolvedExpression) preceding).getOutputDataType()).is(LogicalTypeRoot.BIGINT);
        Expression following = children.get(3);
        RexWindowBound lowerBound = createBound(context, preceding, SqlKind.PRECEDING);
        RexWindowBound upperBound = createBound(context, following, SqlKind.FOLLOWING);
        // build RexOver
        return Optional.of(context.getRelBuilder().getRexBuilder().makeOver(aggResultType, aggFunc, aggExprs, partitionKeys, orderKey, lowerBound, upperBound, isPhysical, true, false, isDistinct));
    }
    return Optional.empty();
}
Also used : SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) CallExpression(org.apache.flink.table.expressions.CallExpression) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) Expression(org.apache.flink.table.expressions.Expression) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BigDecimal(java.math.BigDecimal) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) DecimalType(org.apache.flink.table.types.logical.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlOperator(org.apache.calcite.sql.SqlOperator) ExpressionConverter.extractValue(org.apache.flink.table.planner.expressions.converter.ExpressionConverter.extractValue) RexWindowBound(org.apache.calcite.rex.RexWindowBound) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlKind(org.apache.calcite.sql.SqlKind) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) TableException(org.apache.flink.table.api.TableException) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) Set(java.util.Set) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) List(java.util.List) LogicalTypeDataTypeConverter.fromDataTypeToLogicalType(org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType) Optional(java.util.Optional) SqlAggFunctionVisitor(org.apache.flink.table.planner.expressions.SqlAggFunctionVisitor) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) RexCall(org.apache.calcite.rex.RexCall) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlKind(org.apache.calcite.sql.SqlKind) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlAggFunctionVisitor(org.apache.flink.table.planner.expressions.SqlAggFunctionVisitor) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) RexWindowBound(org.apache.calcite.rex.RexWindowBound) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

Aggregations

SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)43 RelDataType (org.apache.calcite.rel.type.RelDataType)30 ArrayList (java.util.ArrayList)22 RexNode (org.apache.calcite.rex.RexNode)22 AggregateCall (org.apache.calcite.rel.core.AggregateCall)17 RexBuilder (org.apache.calcite.rex.RexBuilder)13 List (java.util.List)11 RelNode (org.apache.calcite.rel.RelNode)11 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)9 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)8 ImmutableList (com.google.common.collect.ImmutableList)7 HashMap (java.util.HashMap)7 Aggregate (org.apache.calcite.rel.core.Aggregate)7 RelBuilder (org.apache.calcite.tools.RelBuilder)7 SqlOperator (org.apache.calcite.sql.SqlOperator)6 Type (java.lang.reflect.Type)5 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)5 RexInputRef (org.apache.calcite.rex.RexInputRef)5 Mappings (org.apache.calcite.util.mapping.Mappings)5 Join (org.apache.calcite.rel.core.Join)4