Search in sources :

Example 71 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project druid by druid-io.

the class DruidSchemaTest method testGetTableMap.

@Test
public void testGetTableMap() {
    Assert.assertEquals(ImmutableSet.of("foo", "foo2"), schema.getTableNames());
    final Map<String, Table> tableMap = schema.getTableMap();
    Assert.assertEquals(ImmutableSet.of("foo", "foo2"), tableMap.keySet());
    final DruidTable fooTable = (DruidTable) tableMap.get("foo");
    final RelDataType rowType = fooTable.getRowType(new JavaTypeFactoryImpl());
    final List<RelDataTypeField> fields = rowType.getFieldList();
    Assert.assertEquals(6, fields.size());
    Assert.assertEquals("__time", fields.get(0).getName());
    Assert.assertEquals(SqlTypeName.TIMESTAMP, fields.get(0).getType().getSqlTypeName());
    Assert.assertEquals("cnt", fields.get(1).getName());
    Assert.assertEquals(SqlTypeName.BIGINT, fields.get(1).getType().getSqlTypeName());
    Assert.assertEquals("dim1", fields.get(2).getName());
    Assert.assertEquals(SqlTypeName.VARCHAR, fields.get(2).getType().getSqlTypeName());
    Assert.assertEquals("m1", fields.get(3).getName());
    Assert.assertEquals(SqlTypeName.BIGINT, fields.get(3).getType().getSqlTypeName());
    Assert.assertEquals("unique_dim1", fields.get(4).getName());
    Assert.assertEquals(SqlTypeName.OTHER, fields.get(4).getType().getSqlTypeName());
    Assert.assertEquals("dim2", fields.get(5).getName());
    Assert.assertEquals(SqlTypeName.VARCHAR, fields.get(5).getType().getSqlTypeName());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) DruidTable(io.druid.sql.calcite.table.DruidTable) Table(org.apache.calcite.schema.Table) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) DruidTable(io.druid.sql.calcite.table.DruidTable) RelDataType(org.apache.calcite.rel.type.RelDataType) Test(org.junit.Test)

Example 72 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project drill by apache.

the class DrillReduceAggregatesRule method reduceAvg.

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 = typeFactory.createTypeWithNullability(avgInputType, avgInputType.isNullable() || nGroups == 0);
    SqlAggFunction sumAgg = new SqlSumEmptyIsZeroAggFunction();
    AggregateCall sumCall = AggregateCall.create(sumAgg, oldCall.isDistinct(), 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.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);
    }
}
Also used : SqlSumEmptyIsZeroAggFunction(org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction) AggregateCall(org.apache.calcite.rel.core.AggregateCall) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlCountAggFunction(org.apache.calcite.sql.fun.SqlCountAggFunction) RexNode(org.apache.calcite.rex.RexNode) DrillSqlOperator(org.apache.drill.exec.planner.sql.DrillSqlOperator)

Example 73 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project drill by apache.

the class DrillViewTable method toRel.

@Override
public RelNode toRel(ToRelContext context, RelOptTable relOptTable) {
    ViewExpansionContext.ViewExpansionToken token = null;
    try {
        RelDataType rowType = relOptTable.getRowType();
        RelNode rel;
        if (viewExpansionContext.isImpersonationEnabled()) {
            token = viewExpansionContext.reserveViewExpansionToken(viewOwner);
            rel = context.expandView(rowType, view.getSql(), token.getSchemaTree(), view.getWorkspaceSchemaPath());
        } else {
            rel = context.expandView(rowType, view.getSql(), view.getWorkspaceSchemaPath());
        }
        // If the View's field list is not "*", create a cast.
        if (!view.isDynamic() && !view.hasStar()) {
            rel = RelOptUtil.createCastRel(rel, rowType, true);
        }
        return rel;
    } finally {
        if (token != null) {
            token.release();
        }
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelDataType(org.apache.calcite.rel.type.RelDataType) ViewExpansionContext(org.apache.drill.exec.ops.ViewExpansionContext)

Example 74 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project drill by apache.

the class DrillValuesRel method verifyRowType.

private static void verifyRowType(final ImmutableList<ImmutableList<RexLiteral>> tuples, RelDataType rowType) {
    for (List<RexLiteral> tuple : tuples) {
        assert (tuple.size() == rowType.getFieldCount());
        for (Pair<RexLiteral, RelDataTypeField> pair : Pair.zip(tuple, rowType.getFieldList())) {
            RexLiteral literal = pair.left;
            RelDataType fieldType = pair.right.getType();
            if ((!(RexLiteral.isNullLiteral(literal))) && (!(SqlTypeUtil.canAssignFrom(fieldType, literal.getType())))) {
                throw new AssertionError("to " + fieldType + " from " + literal);
            }
        }
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 75 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project drill by apache.

the class DrillWriterRel method resolvePartitionKeys.

private List<Integer> resolvePartitionKeys() {
    final List<Integer> keys = Lists.newArrayList();
    final RelDataType inputRowType = getInput().getRowType();
    final List<String> partitionCol = getCreateTableEntry().getPartitionColumns();
    for (final String col : partitionCol) {
        final RelDataTypeField field = inputRowType.getField(col, false, false);
        Preconditions.checkArgument(field != null, String.format("partition col %s could not be resolved in table's column lists!", col));
        keys.add(field.getIndex());
    }
    return keys;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)88 RexNode (org.apache.calcite.rex.RexNode)48 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelNode (org.apache.calcite.rel.RelNode)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)25 ArrayList (java.util.ArrayList)21 RexInputRef (org.apache.calcite.rex.RexInputRef)16 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)13 ImmutableList (com.google.common.collect.ImmutableList)9 BigDecimal (java.math.BigDecimal)8 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)7 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 Prel (org.apache.drill.exec.planner.physical.Prel)6 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)6 Builder (com.google.common.collect.ImmutableList.Builder)5 LinkedList (java.util.LinkedList)5