Search in sources :

Example 96 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project beam by apache.

the class ZetaSqlUnnest method deriveUncollectRowType.

/**
 * Returns the row type returned by applying the 'UNNEST' operation to a relational expression.
 *
 * <p>Each column in the relational expression must be a multiset of structs or an array. The
 * return type is the type of that column, plus an ORDINALITY column if {@code withOrdinality}.
 */
public static RelDataType deriveUncollectRowType(RelNode rel, boolean withOrdinality) {
    RelDataType inputType = rel.getRowType();
    assert inputType.isStruct() : inputType + " is not a struct";
    final List<RelDataTypeField> fields = inputType.getFieldList();
    final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    if (fields.size() == 1 && fields.get(0).getType().getSqlTypeName() == SqlTypeName.ANY) {
        // and Any type.
        return builder.add(fields.get(0).getName(), SqlTypeName.ANY).nullable(true).build();
    }
    for (RelDataTypeField field : fields) {
        if (field.getType() instanceof MapSqlType) {
            builder.add(SqlUnnestOperator.MAP_KEY_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getKeyType(), "Encountered MAP type with null key type in field %s", field));
            builder.add(SqlUnnestOperator.MAP_VALUE_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getValueType(), "Encountered MAP type with null value type in field %s", field));
        } else {
            assert field.getType() instanceof ArraySqlType;
            RelDataType ret = Preconditions.checkArgumentNotNull(field.getType().getComponentType(), "Encountered ARRAY type with null component type in field %s", field);
            // Only difference than Uncollect.java: treats record type and scalar type equally
            builder.add(SqlUtil.deriveAliasFromOrdinal(field.getIndex()), ret);
        }
    }
    if (withOrdinality) {
        builder.add(SqlUnnestOperator.ORDINALITY_COLUMN_NAME, SqlTypeName.INTEGER);
    }
    return builder.build();
}
Also used : RelDataTypeField(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.MapSqlType) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) ArraySqlType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType)

Example 97 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project beam by apache.

the class LazyAggregateCombineFnTest method nonparameterizedGetUdafImpl_throwsIllegalStateException.

@Test
public void nonparameterizedGetUdafImpl_throwsIllegalStateException() {
    LazyAggregateCombineFn<?, ?, ?> combiner = new LazyAggregateCombineFn<>(new NonParameterizedAggregateFn());
    AggregateFunction aggregateFunction = combiner.getUdafImpl();
    RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    exceptions.expect(IllegalStateException.class);
    List<FunctionParameter> params = aggregateFunction.getParameters();
    params.get(0).getType(typeFactory);
}
Also used : JavaTypeFactoryImpl(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.JavaTypeFactoryImpl) AggregateFunction(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.AggregateFunction) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) FunctionParameter(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.FunctionParameter) Test(org.junit.Test)

Example 98 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory 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 99 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class RelBuilderTest method testMatchRecognize.

@Test
public void testMatchRecognize() {
    // Equivalent SQL:
    // SELECT *
    // FROM emp
    // MATCH_RECOGNIZE (
    // PARTITION BY deptno
    // ORDER BY empno asc
    // MEASURES
    // STRT.mgr as start_nw,
    // LAST(DOWN.mgr) as bottom_nw,
    // PATTERN (STRT DOWN+ UP+) WITHIN INTERVAL '5' SECOND
    // DEFINE
    // DOWN as DOWN.mgr < PREV(DOWN.mgr),
    // UP as UP.mgr > PREV(UP.mgr)
    // )
    final RelBuilder builder = RelBuilder.create(config().build()).scan("EMP");
    final RelDataTypeFactory typeFactory = builder.getTypeFactory();
    final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
    RexNode pattern = builder.patternConcat(builder.literal("STRT"), builder.patternQuantify(builder.literal("DOWN"), builder.literal(1), builder.literal(-1), builder.literal(false)), builder.patternQuantify(builder.literal("UP"), builder.literal(1), builder.literal(-1), builder.literal(false)));
    ImmutableMap.Builder<String, RexNode> pdBuilder = new ImmutableMap.Builder<>();
    RexNode downDefinition = builder.call(SqlStdOperatorTable.LESS_THAN, builder.call(SqlStdOperatorTable.PREV, builder.patternField("DOWN", intType, 3), builder.literal(0)), builder.call(SqlStdOperatorTable.PREV, builder.patternField("DOWN", intType, 3), builder.literal(1)));
    pdBuilder.put("DOWN", downDefinition);
    RexNode upDefinition = builder.call(SqlStdOperatorTable.GREATER_THAN, builder.call(SqlStdOperatorTable.PREV, builder.patternField("UP", intType, 3), builder.literal(0)), builder.call(SqlStdOperatorTable.PREV, builder.patternField("UP", intType, 3), builder.literal(1)));
    pdBuilder.put("UP", upDefinition);
    ImmutableList.Builder<RexNode> measuresBuilder = new ImmutableList.Builder<>();
    measuresBuilder.add(builder.alias(builder.patternField("STRT", intType, 3), "start_nw"));
    measuresBuilder.add(builder.alias(builder.call(SqlStdOperatorTable.LAST, builder.patternField("DOWN", intType, 3), builder.literal(0)), "bottom_nw"));
    RexNode after = builder.getRexBuilder().makeFlag(SqlMatchRecognize.AfterOption.SKIP_TO_NEXT_ROW);
    ImmutableList.Builder<RexNode> partitionKeysBuilder = new ImmutableList.Builder<>();
    partitionKeysBuilder.add(builder.field("DEPTNO"));
    ImmutableList.Builder<RexNode> orderKeysBuilder = new ImmutableList.Builder<>();
    orderKeysBuilder.add(builder.field("EMPNO"));
    RexNode interval = builder.literal("INTERVAL '5' SECOND");
    final ImmutableMap<String, TreeSet<String>> subsets = ImmutableMap.of();
    final RelNode root = builder.match(pattern, false, false, pdBuilder.build(), measuresBuilder.build(), after, subsets, false, partitionKeysBuilder.build(), orderKeysBuilder.build(), interval).build();
    final String expected = "LogicalMatch(partition=[[$7]], order=[[0]], " + "outputFields=[[$7, 'start_nw', 'bottom_nw']], allRows=[false], " + "after=[FLAG(SKIP TO NEXT ROW)], pattern=[(('STRT', " + "PATTERN_QUANTIFIER('DOWN', 1, -1, false)), " + "PATTERN_QUANTIFIER('UP', 1, -1, false))], " + "isStrictStarts=[false], isStrictEnds=[false], " + "interval=['INTERVAL ''5'' SECOND'], subsets=[[]], " + "patternDefinitions=[[<(PREV(DOWN.$3, 0), PREV(DOWN.$3, 1)), " + ">(PREV(UP.$3, 0), PREV(UP.$3, 1))]], " + "inputFields=[[EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO]])\n" + "  LogicalTableScan(table=[[scott, EMP]])\n";
    assertThat(root, hasTree(expected));
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) ImmutableList(com.google.common.collect.ImmutableList) RelBuilder(org.apache.calcite.tools.RelBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) RelNode(org.apache.calcite.rel.RelNode) TreeSet(java.util.TreeSet) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 100 with RelDataTypeFactory

use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.

the class AggregateReduceFunctionsRule method reduceAvg.

private RexNode reduceAvg(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
    final int nGroups = oldAggRel.getGroupCount();
    final RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
    final int iAvgInput = oldCall.getArgList().get(0);
    final RelDataType avgInputType = getFieldType(oldAggRel.getInput(), iAvgInput);
    final AggregateCall sumCall = AggregateCall.create(SqlStdOperatorTable.SUM, oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), null, null);
    final AggregateCall countCall = AggregateCall.create(SqlStdOperatorTable.COUNT, oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), null, null);
    // NOTE:  these references are with respect to the output
    // of newAggRel
    RexNode numeratorRef = rexBuilder.addAggCall(sumCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(avgInputType));
    final RexNode denominatorRef = rexBuilder.addAggCall(countCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(avgInputType));
    final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
    final RelDataType avgType = typeFactory.createTypeWithNullability(oldCall.getType(), numeratorRef.getType().isNullable());
    numeratorRef = rexBuilder.ensureType(avgType, numeratorRef, true);
    final RexNode divideRef = rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, numeratorRef, denominatorRef);
    return rexBuilder.makeCast(oldCall.getType(), divideRef);
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)128 RelDataType (org.apache.calcite.rel.type.RelDataType)97 RexNode (org.apache.calcite.rex.RexNode)47 RexBuilder (org.apache.calcite.rex.RexBuilder)38 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)30 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 RelNode (org.apache.calcite.rel.RelNode)20 AggregateCall (org.apache.calcite.rel.core.AggregateCall)15 SqlNode (org.apache.calcite.sql.SqlNode)14 SqlTypeFactoryImpl (org.apache.calcite.sql.type.SqlTypeFactoryImpl)14 BigDecimal (java.math.BigDecimal)12 List (java.util.List)10 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)8 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)8 Calendar (java.util.Calendar)7 RexInputRef (org.apache.calcite.rex.RexInputRef)7 Pair (org.apache.calcite.util.Pair)7 TimestampString (org.apache.calcite.util.TimestampString)7