Search in sources :

Example 31 with RelDataType

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

the class ConvertHiveParquetScanToDrillParquetScan method createProjectRel.

/**
   * Create a project that converts the native scan output to expected output of Hive scan.
   */
private DrillProjectRel createProjectRel(final DrillScanRel hiveScanRel, final Map<String, String> partitionColMapping, final DrillScanRel nativeScanRel) {
    final List<RexNode> rexNodes = Lists.newArrayList();
    final RexBuilder rb = hiveScanRel.getCluster().getRexBuilder();
    final RelDataType hiveScanRowType = hiveScanRel.getRowType();
    for (String colName : hiveScanRowType.getFieldNames()) {
        final String dirColName = partitionColMapping.get(colName);
        if (dirColName != null) {
            rexNodes.add(createPartitionColumnCast(hiveScanRel, nativeScanRel, colName, dirColName, rb));
        } else {
            rexNodes.add(createColumnFormatConversion(hiveScanRel, nativeScanRel, colName, rb));
        }
    }
    return DrillProjectRel.create(hiveScanRel.getCluster(), hiveScanRel.getTraitSet(), nativeScanRel, rexNodes, hiveScanRowType);
}
Also used : RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 32 with RelDataType

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

the class DrillOptiqTest method testUnsupportedRexNode.

/* Method checks if we raise the appropriate error while dealing with RexNode that cannot be converted to
   * equivalent Drill expressions
   */
@Test
public void testUnsupportedRexNode() {
    try {
        // Create the data type factory.
        RelDataTypeFactory relFactory = new SqlTypeFactoryImpl(DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM);
        // Create the rex builder
        RexBuilder rex = new RexBuilder(relFactory);
        RelDataType anyType = relFactory.createSqlType(SqlTypeName.ANY);
        List<RexNode> emptyList = new LinkedList<>();
        ImmutableList<RexFieldCollation> e = ImmutableList.copyOf(new RexFieldCollation[0]);
        // create a dummy RexOver object.
        RexNode window = rex.makeOver(anyType, SqlStdOperatorTable.AVG, emptyList, emptyList, e, null, null, true, false, false);
        DrillOptiq.toDrill(null, (RelNode) null, window);
    } catch (UserException e) {
        if (e.getMessage().contains(DrillOptiq.UNSUPPORTED_REX_NODE_ERROR)) {
            // got expected error return
            return;
        }
        Assert.fail("Hit exception with unexpected error message");
    }
    Assert.fail("Failed to raise the expected exception");
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) UserException(org.apache.drill.common.exceptions.UserException) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) LinkedList(java.util.LinkedList) RexNode(org.apache.calcite.rex.RexNode) Test(org.junit.Test)

Example 33 with RelDataType

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

the class View method getRowType.

public RelDataType getRowType(RelDataTypeFactory factory) {
    // if there are no fields defined, this is a dynamic view.
    if (isDynamic()) {
        return new RelDataTypeDrillImpl(new RelDataTypeHolder(), factory);
    }
    List<RelDataType> types = Lists.newArrayList();
    List<String> names = Lists.newArrayList();
    for (FieldType field : fields) {
        names.add(field.getName());
        RelDataType type;
        if (SqlTypeFamily.INTERVAL_YEAR_MONTH == field.getType().getFamily() || SqlTypeFamily.INTERVAL_DAY_TIME == field.getType().getFamily()) {
            type = factory.createSqlIntervalType(field.getIntervalQualifier());
        } else if (field.getPrecision() == null && field.getScale() == null) {
            type = factory.createSqlType(field.getType());
        } else if (field.getPrecision() != null && field.getScale() == null) {
            type = factory.createSqlType(field.getType(), field.getPrecision());
        } else {
            type = factory.createSqlType(field.getType(), field.getPrecision(), field.getScale());
        }
        if (field.getIsNullable()) {
            types.add(factory.createTypeWithNullability(type, true));
        } else {
            types.add(type);
        }
    }
    return factory.createStructType(types, names);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeHolder(org.apache.drill.exec.planner.types.RelDataTypeHolder) RelDataTypeDrillImpl(org.apache.drill.exec.planner.types.RelDataTypeDrillImpl)

Example 34 with RelDataType

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

the class TypeInferenceUtils method convertSqlOperatorBindingToFunctionCall.

/**
   * Given a SqlOperatorBinding, convert it to FunctionCall
   * @param  opBinding    the given SqlOperatorBinding
   * @return FunctionCall the converted FunctionCall
   */
public static FunctionCall convertSqlOperatorBindingToFunctionCall(final SqlOperatorBinding opBinding) {
    final List<LogicalExpression> args = Lists.newArrayList();
    for (int i = 0; i < opBinding.getOperandCount(); ++i) {
        final RelDataType type = opBinding.getOperandType(i);
        final TypeProtos.MinorType minorType = getDrillTypeFromCalciteType(type);
        final TypeProtos.MajorType majorType;
        if (type.isNullable()) {
            majorType = Types.optional(minorType);
        } else {
            majorType = Types.required(minorType);
        }
        args.add(new MajorTypeInLogicalExpression(majorType));
    }
    final String drillFuncName = FunctionCallFactory.replaceOpWithFuncName(opBinding.getOperator().getName());
    final FunctionCall functionCall = new FunctionCall(drillFuncName, args, ExpressionPosition.UNKNOWN);
    return functionCall;
}
Also used : MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FunctionCall(org.apache.drill.common.expression.FunctionCall) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 35 with RelDataType

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

the class DefaultSqlHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    final ConvertedRelNode convertedRelNode = validateAndConvert(sqlNode);
    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
    final DrillRel drel = convertToDrel(queryRelNode, validatedRowType);
    final Prel prel = convertToPrel(drel);
    logAndSetTextPlan("Drill Physical", prel, logger);
    final PhysicalOperator pop = convertToPop(prel);
    final PhysicalPlan plan = convertToPlan(pop);
    log("Drill Plan", plan, logger);
    return plan;
}
Also used : PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) RelNode(org.apache.calcite.rel.RelNode) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) DrillRel(org.apache.drill.exec.planner.logical.DrillRel) RelDataType(org.apache.calcite.rel.type.RelDataType) Prel(org.apache.drill.exec.planner.physical.Prel)

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