Search in sources :

Example 81 with SqlTypeName

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

the class JavaTypeFactoryImpl method toSql.

/**
 * Converts a type in Java format to a SQL-oriented type.
 */
public static RelDataType toSql(final RelDataTypeFactory typeFactory, RelDataType type) {
    if (type instanceof RelRecordType) {
        return typeFactory.createTypeWithNullability(typeFactory.createStructType(type.getFieldList().stream().map(field -> toSql(typeFactory, field.getType())).collect(Collectors.toList()), type.getFieldNames()), type.isNullable());
    } else if (type instanceof JavaType) {
        SqlTypeName sqlTypeName = type.getSqlTypeName();
        final RelDataType relDataType;
        if (SqlTypeUtil.isArray(type)) {
            // Transform to sql type, take care for two cases:
            // 1. type.getJavaClass() is collection with erased generic type
            // 2. ElementType returned by JavaType is also of JavaType,
            // and needs conversion using typeFactory
            final RelDataType elementType = toSqlTypeWithNullToAny(typeFactory, type.getComponentType());
            relDataType = typeFactory.createArrayType(elementType, -1);
        } else if (SqlTypeUtil.isMap(type)) {
            final RelDataType keyType = toSqlTypeWithNullToAny(typeFactory, type.getKeyType());
            final RelDataType valueType = toSqlTypeWithNullToAny(typeFactory, type.getValueType());
            relDataType = typeFactory.createMapType(keyType, valueType);
        } else {
            relDataType = typeFactory.createSqlType(sqlTypeName);
        }
        return typeFactory.createTypeWithNullability(relDataType, type.isNullable());
    }
    return type;
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) LocalDateTime(java.time.LocalDateTime) AbstractList(java.util.AbstractList) HashMap(java.util.HashMap) Ord(org.apache.calcite.linq4j.Ord) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Primitive(org.apache.calcite.linq4j.tree.Primitive) Unit(org.apache.calcite.runtime.Unit) Geometries(org.apache.calcite.runtime.Geometries) Pair(org.apache.calcite.util.Pair) Duration(java.time.Duration) Map(java.util.Map) RelDataTypeSystem(org.apache.calcite.rel.type.RelDataTypeSystem) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Types(org.apache.calcite.linq4j.tree.Types) Period(java.time.Period) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ByteString(org.apache.calcite.avatica.util.ByteString) RelRecordType(org.apache.calcite.rel.type.RelRecordType) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) JavaToSqlTypeConversionRules(org.apache.calcite.sql.type.JavaToSqlTypeConversionRules) Objects(java.util.Objects) List(java.util.List) Type(java.lang.reflect.Type) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Modifier(java.lang.reflect.Modifier) LocalDate(java.time.LocalDate) BasicSqlType(org.apache.calcite.sql.type.BasicSqlType) IntervalSqlType(org.apache.calcite.sql.type.IntervalSqlType) Util(org.apache.calcite.util.Util) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType)

Example 82 with SqlTypeName

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

the class CalciteConvertors method parseTypeString.

private static RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
    int precision = -1;
    int scale = -1;
    int open = typeString.indexOf("(");
    if (open >= 0) {
        int close = typeString.indexOf(")", open);
        if (close >= 0) {
            String rest = typeString.substring(open + 1, close);
            typeString = typeString.substring(0, open);
            int comma = rest.indexOf(",");
            if (comma >= 0) {
                precision = Integer.parseInt(rest.substring(0, comma));
                scale = Integer.parseInt(rest.substring(comma));
            } else {
                precision = Integer.parseInt(rest);
            }
        }
    }
    try {
        final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
        return typeName.allowsPrecScale(true, true) ? typeFactory.createSqlType(typeName, precision, scale) : typeName.allowsPrecScale(true, false) ? typeFactory.createSqlType(typeName, precision) : typeFactory.createSqlType(typeName);
    } catch (IllegalArgumentException e) {
        return typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 83 with SqlTypeName

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

the class CalciteConvertors method getRelDataType.

public static RelDataType getRelDataType(final List<SimpleColumnInfo> columnInfos, final RelDataTypeFactory factory) {
    final RelDataTypeFactory.Builder builder = new RelDataTypeFactory.Builder(factory);
    for (SimpleColumnInfo columnInfo : columnInfos) {
        final JDBCType columnType = columnInfo.getJdbcType();
        final RelDataType type;
        if (columnType == JDBCType.VARCHAR) {
            type = factory.createTypeWithCharsetAndCollation(factory.createSqlType(SqlTypeName.VARCHAR), StandardCharsets.UTF_8, SqlCollation.IMPLICIT);
        } else if (columnType == JDBCType.LONGVARBINARY) {
            type = factory.createSqlType(SqlTypeName.VARBINARY);
        } else {
            SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(columnType.getVendorTypeNumber());
            if (sqlTypeName == null) {
                sqlTypeName = SqlTypeName.VARCHAR;
            }
            type = factory.createSqlType(sqlTypeName);
        }
        builder.add(columnInfo.getColumnName(), factory.createTypeWithNullability(type, columnInfo.isNullable()));
    }
    return builder.build();
}
Also used : SimpleColumnInfo(io.mycat.SimpleColumnInfo) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 84 with SqlTypeName

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

the class EarliestLatestAnySqlAggregator method toDruidAggregation.

@Nullable
@Override
public Aggregation toDruidAggregation(final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final String name, final AggregateCall aggregateCall, final Project project, final List<Aggregation> existingAggregations, final boolean finalizeAggregations) {
    final List<RexNode> rexNodes = aggregateCall.getArgList().stream().map(i -> Expressions.fromFieldAccess(rowSignature, project, i)).collect(Collectors.toList());
    final List<DruidExpression> args = Expressions.toDruidExpressions(plannerContext, rowSignature, rexNodes);
    if (args == null) {
        return null;
    }
    final String aggregatorName = finalizeAggregations ? Calcites.makePrefixedName(name, "a") : name;
    final ColumnType outputType = Calcites.getColumnTypeForRelDataType(aggregateCall.getType());
    if (outputType == null) {
        throw new ISE("Cannot translate output sqlTypeName[%s] to Druid type for aggregator[%s]", aggregateCall.getType().getSqlTypeName(), aggregateCall.getName());
    }
    final String fieldName = getColumnName(plannerContext, virtualColumnRegistry, args.get(0), rexNodes.get(0));
    final AggregatorFactory theAggFactory;
    switch(args.size()) {
        case 1:
            theAggFactory = aggregatorType.createAggregatorFactory(aggregatorName, fieldName, null, outputType, -1);
            break;
        case 2:
            theAggFactory = aggregatorType.createAggregatorFactory(aggregatorName, fieldName, null, outputType, RexLiteral.intValue(rexNodes.get(1)));
            break;
        default:
            throw new IAE("aggregation[%s], Invalid number of arguments[%,d] to [%s] operator", aggregatorName, args.size(), aggregatorType.name());
    }
    return Aggregation.create(Collections.singletonList(theAggFactory), finalizeAggregations ? new FinalizingFieldAccessPostAggregator(name, aggregatorName) : null);
}
Also used : Project(org.apache.calcite.rel.core.Project) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Optionality(org.apache.calcite.util.Optionality) StringAnyAggregatorFactory(org.apache.druid.query.aggregation.any.StringAnyAggregatorFactory) RexNode(org.apache.calcite.rex.RexNode) FloatAnyAggregatorFactory(org.apache.druid.query.aggregation.any.FloatAnyAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) VirtualColumnRegistry(org.apache.druid.sql.calcite.rel.VirtualColumnRegistry) LongLastAggregatorFactory(org.apache.druid.query.aggregation.last.LongLastAggregatorFactory) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) LongFirstAggregatorFactory(org.apache.druid.query.aggregation.first.LongFirstAggregatorFactory) DoubleAnyAggregatorFactory(org.apache.druid.query.aggregation.any.DoubleAnyAggregatorFactory) IAE(org.apache.druid.java.util.common.IAE) FloatFirstAggregatorFactory(org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory) Nullable(javax.annotation.Nullable) SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) InferTypes(org.apache.calcite.sql.type.InferTypes) RexBuilder(org.apache.calcite.rex.RexBuilder) RexLiteral(org.apache.calcite.rex.RexLiteral) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) SqlFunctionCategory(org.apache.calcite.sql.SqlFunctionCategory) ISE(org.apache.druid.java.util.common.ISE) SqlTypeUtil(org.apache.calcite.sql.type.SqlTypeUtil) Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) FloatLastAggregatorFactory(org.apache.druid.query.aggregation.last.FloatLastAggregatorFactory) Collectors(java.util.stream.Collectors) SqlReturnTypeInference(org.apache.calcite.sql.type.SqlReturnTypeInference) List(java.util.List) RowSignature(org.apache.druid.segment.column.RowSignature) OperandTypes(org.apache.calcite.sql.type.OperandTypes) ColumnType(org.apache.druid.segment.column.ColumnType) AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) DoubleFirstAggregatorFactory(org.apache.druid.query.aggregation.first.DoubleFirstAggregatorFactory) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Collections(java.util.Collections) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) DoubleLastAggregatorFactory(org.apache.druid.query.aggregation.last.DoubleLastAggregatorFactory) Expressions(org.apache.druid.sql.calcite.expression.Expressions) ColumnType(org.apache.druid.segment.column.ColumnType) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) ISE(org.apache.druid.java.util.common.ISE) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) StringAnyAggregatorFactory(org.apache.druid.query.aggregation.any.StringAnyAggregatorFactory) FloatAnyAggregatorFactory(org.apache.druid.query.aggregation.any.FloatAnyAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) LongLastAggregatorFactory(org.apache.druid.query.aggregation.last.LongLastAggregatorFactory) LongFirstAggregatorFactory(org.apache.druid.query.aggregation.first.LongFirstAggregatorFactory) DoubleAnyAggregatorFactory(org.apache.druid.query.aggregation.any.DoubleAnyAggregatorFactory) FloatFirstAggregatorFactory(org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) FloatLastAggregatorFactory(org.apache.druid.query.aggregation.last.FloatLastAggregatorFactory) DoubleFirstAggregatorFactory(org.apache.druid.query.aggregation.first.DoubleFirstAggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) DoubleLastAggregatorFactory(org.apache.druid.query.aggregation.last.DoubleLastAggregatorFactory) IAE(org.apache.druid.java.util.common.IAE) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 85 with SqlTypeName

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

the class Expressions method literalToDruidExpression.

@Nullable
private static DruidExpression literalToDruidExpression(final PlannerContext plannerContext, final RexNode rexNode) {
    final SqlTypeName sqlTypeName = rexNode.getType().getSqlTypeName();
    // Translate literal.
    final ColumnType columnType = Calcites.getColumnTypeForRelDataType(rexNode.getType());
    if (RexLiteral.isNullLiteral(rexNode)) {
        return DruidExpression.ofLiteral(columnType, DruidExpression.nullLiteral());
    } else if (SqlTypeName.NUMERIC_TYPES.contains(sqlTypeName)) {
        return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral((Number) RexLiteral.value(rexNode)));
    } else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
        // Calcite represents DAY-TIME intervals in milliseconds.
        final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
        return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(milliseconds));
    } else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
        // Calcite represents YEAR-MONTH intervals in months.
        final long months = ((Number) RexLiteral.value(rexNode)).longValue();
        return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(months));
    } else if (SqlTypeName.STRING_TYPES.contains(sqlTypeName)) {
        return DruidExpression.ofStringLiteral(RexLiteral.stringValue(rexNode));
    } else if (SqlTypeName.TIMESTAMP == sqlTypeName || SqlTypeName.DATE == sqlTypeName) {
        if (RexLiteral.isNullLiteral(rexNode)) {
            return DruidExpression.ofLiteral(columnType, DruidExpression.nullLiteral());
        } else {
            return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(Calcites.calciteDateTimeLiteralToJoda(rexNode, plannerContext.getTimeZone()).getMillis()));
        }
    } else if (SqlTypeName.BOOLEAN == sqlTypeName) {
        return DruidExpression.ofLiteral(columnType, DruidExpression.numberLiteral(RexLiteral.booleanValue(rexNode) ? 1 : 0));
    } else {
        // Can't translate other literals.
        return null;
    }
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Nullable(javax.annotation.Nullable)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)184 RelDataType (org.apache.calcite.rel.type.RelDataType)62 Test (org.junit.jupiter.api.Test)39 List (java.util.List)31 BigDecimal (java.math.BigDecimal)30 ArrayList (java.util.ArrayList)30 ImmutableList (com.google.common.collect.ImmutableList)26 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 Map (java.util.Map)25 RexNode (org.apache.calcite.rex.RexNode)25 NlsString (org.apache.calcite.util.NlsString)21 DateString (org.apache.calcite.util.DateString)18 TimeString (org.apache.calcite.util.TimeString)18 TimestampString (org.apache.calcite.util.TimestampString)18 ByteString (org.apache.calcite.avatica.util.ByteString)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)16 SqlKind (org.apache.calcite.sql.SqlKind)15 Calendar (java.util.Calendar)14 Objects (java.util.Objects)13 Util (org.apache.calcite.util.Util)13