Search in sources :

Example 11 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by apache.

the class RecordDataType method getRowType.

/**
   * This method constructs a {@link org.apache.calcite.rel.type.RelDataType} based on the
   * {@link org.apache.drill.exec.store.RecordDataType}'s field sql types and field names.
   *
   * @param factory helps construct a {@link org.apache.calcite.rel.type.RelDataType}
   * @return the constructed type
   */
public final RelDataType getRowType(RelDataTypeFactory factory) {
    final List<SqlTypeName> types = getFieldSqlTypeNames();
    final List<String> names = getFieldNames();
    final List<RelDataType> fields = Lists.newArrayList();
    for (final SqlTypeName typeName : types) {
        switch(typeName) {
            case VARCHAR:
                fields.add(factory.createSqlType(typeName, Integer.MAX_VALUE));
                break;
            default:
                fields.add(factory.createSqlType(typeName));
        }
    }
    return factory.createStructType(fields, names);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 12 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by apache.

the class FindLimit0Visitor method getDirectScanRelIfFullySchemaed.

/**
   * If all field types of the given node are {@link #TYPES recognized types} and honored by execution, then this
   * method returns the tree: DrillDirectScanRel(field types). Otherwise, the method returns null.
   *
   * @param rel calcite logical rel tree
   * @return drill logical rel tree
   */
public static DrillRel getDirectScanRelIfFullySchemaed(RelNode rel) {
    final List<RelDataTypeField> fieldList = rel.getRowType().getFieldList();
    final List<TypeProtos.MajorType> columnTypes = Lists.newArrayList();
    for (final RelDataTypeField field : fieldList) {
        final SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
        if (!TYPES.contains(sqlTypeName)) {
            return null;
        } else {
            final TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder().setMode(field.getType().isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED).setMinorType(TypeInferenceUtils.getDrillTypeFromCalciteType(sqlTypeName));
            if (TypeInferenceUtils.isScalarStringType(sqlTypeName)) {
                builder.setPrecision(field.getType().getPrecision());
            }
            columnTypes.add(builder.build());
        }
    }
    final RelTraitSet traits = rel.getTraitSet().plus(DrillRel.DRILL_LOGICAL);
    final RelDataTypeReader reader = new RelDataTypeReader(rel.getRowType().getFieldNames(), columnTypes);
    return new DrillDirectScanRel(rel.getCluster(), traits, new DirectGroupScan(reader, ScanStats.ZERO_RECORD_TABLE), rel.getRowType());
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DirectGroupScan(org.apache.drill.exec.store.direct.DirectGroupScan) RelTraitSet(org.apache.calcite.plan.RelTraitSet) DrillDirectScanRel(org.apache.drill.exec.planner.logical.DrillDirectScanRel)

Example 13 with SqlTypeName

use of org.apache.calcite.sql.type.SqlTypeName in project drill by apache.

the class SetOptionHandler method createOptionValue.

private static OptionValue createOptionValue(final String name, final OptionValue.OptionType type, final SqlLiteral literal) {
    final Object object = literal.getValue();
    final SqlTypeName typeName = literal.getTypeName();
    switch(typeName) {
        case DECIMAL:
            {
                final BigDecimal bigDecimal = (BigDecimal) object;
                if (bigDecimal.scale() == 0) {
                    return OptionValue.createLong(type, name, bigDecimal.longValue());
                } else {
                    return OptionValue.createDouble(type, name, bigDecimal.doubleValue());
                }
            }
        case DOUBLE:
        case FLOAT:
            return OptionValue.createDouble(type, name, ((BigDecimal) object).doubleValue());
        case SMALLINT:
        case TINYINT:
        case BIGINT:
        case INTEGER:
            return OptionValue.createLong(type, name, ((BigDecimal) object).longValue());
        case VARBINARY:
        case VARCHAR:
        case CHAR:
            return OptionValue.createString(type, name, ((NlsString) object).getValue());
        case BOOLEAN:
            return OptionValue.createBoolean(type, name, (Boolean) object);
        default:
            throw UserException.validationError().message("Drill doesn't support assigning literals of type %s in SET statements.", typeName).build(logger);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) BigDecimal(java.math.BigDecimal)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)13 ISE (io.druid.java.util.common.ISE)6 RexNode (org.apache.calcite.rex.RexNode)6 BigDecimal (java.math.BigDecimal)3 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)2 DimensionSpec (io.druid.query.dimension.DimensionSpec)2 AndDimFilter (io.druid.query.filter.AndDimFilter)2 DimFilter (io.druid.query.filter.DimFilter)2 NotDimFilter (io.druid.query.filter.NotDimFilter)2 ValueType (io.druid.segment.column.ValueType)2 Aggregation (io.druid.sql.calcite.aggregation.Aggregation)2 RowExtraction (io.druid.sql.calcite.expression.RowExtraction)2 Calendar (java.util.Calendar)2 RexCall (org.apache.calcite.rex.RexCall)2 SqlKind (org.apache.calcite.sql.SqlKind)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 Function (com.google.common.base.Function)1 IAE (io.druid.java.util.common.IAE)1 ExprType (io.druid.math.expr.ExprType)1 QueryInterruptedException (io.druid.query.QueryInterruptedException)1