Search in sources :

Example 91 with SqlTypeName

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

the class RexLiteral method appendAsJava.

/**
 * Appends the specified value in the provided destination as a Java string. The value must be
 * consistent with the type, as per {@link #valueMatchesType}.
 *
 * <p>Typical return values:
 *
 * <ul>
 *   <li>true
 *   <li>null
 *   <li>"Hello, world!"
 *   <li>1.25
 *   <li>1234ABCD
 * </ul>
 *
 * @param value Value to be appended to the provided destination as a Java string
 * @param sb Destination to which to append the specified value
 * @param typeName Type name to be used for the transformation of the value to a Java string
 * @param type Type to be used for the transformation of the value to a Java string
 * @param includeType Whether to include the data type in the Java representation
 */
private static void appendAsJava(Comparable value, StringBuilder sb, SqlTypeName typeName, RelDataType type, boolean java, RexDigestIncludeType includeType) {
    switch(typeName) {
        case CHAR:
            NlsString nlsString = (NlsString) value;
            if (java) {
                Util.printJavaString(sb, nlsString.getValue(), true);
            } else {
                boolean includeCharset = (nlsString.getCharsetName() != null) && !nlsString.getCharsetName().equals(CalciteSystemProperty.DEFAULT_CHARSET.value());
                sb.append(nlsString.asSql(includeCharset, false));
            }
            break;
        case BOOLEAN:
            assert value instanceof Boolean;
            sb.append(value.toString());
            break;
        case DECIMAL:
            assert value instanceof BigDecimal;
            sb.append(value.toString());
            break;
        case DOUBLE:
            assert value instanceof BigDecimal;
            sb.append(Util.toScientificNotation((BigDecimal) value));
            break;
        case BIGINT:
            assert value instanceof BigDecimal;
            long narrowLong = ((BigDecimal) value).longValue();
            sb.append(String.valueOf(narrowLong));
            sb.append('L');
            break;
        case BINARY:
            assert value instanceof ByteString;
            sb.append("X'");
            sb.append(((ByteString) value).toString(16));
            sb.append("'");
            break;
        case NULL:
            assert value == null;
            sb.append("null");
            break;
        case SARG:
            assert value instanceof Sarg;
            // noinspection unchecked,rawtypes
            Util.asStringBuilder(sb, sb2 -> printSarg(sb2, (Sarg) value, type));
            break;
        case SYMBOL:
            assert value instanceof Enum;
            sb.append("FLAG(");
            sb.append(value.toString());
            sb.append(")");
            break;
        case DATE:
            assert value instanceof DateString;
            sb.append(value.toString());
            break;
        case TIME:
        case TIME_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimeString;
            sb.append(value.toString());
            break;
        case TIMESTAMP:
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            assert value instanceof TimestampString;
            sb.append(value.toString());
            break;
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            assert value instanceof BigDecimal;
            sb.append(value.toString());
            break;
        case MULTISET:
        case ROW:
            final List<RexLiteral> list = (List) value;
            Util.asStringBuilder(sb, sb2 -> Util.printList(sb, list.size(), (sb3, i) -> sb3.append(list.get(i).computeDigest(includeType))));
            break;
        case GEOMETRY:
            final String wkt = GeoFunctions.ST_AsWKT((Geometries.Geom) value);
            sb.append(wkt);
            break;
        default:
            assert valueMatchesType(value, typeName, true);
            throw Util.needToImplement(typeName);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) TimeUnit(org.apache.calcite.avatica.util.TimeUnit) ByteBuffer(java.nio.ByteBuffer) TimeString(org.apache.calcite.util.TimeString) BigDecimal(java.math.BigDecimal) Calendar(java.util.Calendar) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) DateTimeUtils(org.apache.calcite.avatica.util.DateTimeUtils) Geometries(org.apache.calcite.runtime.Geometries) Locale(java.util.Locale) Map(java.util.Map) SqlOperator(org.apache.calcite.sql.SqlOperator) CalciteSystemProperty(org.apache.calcite.config.CalciteSystemProperty) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) Functions(org.apache.calcite.linq4j.function.Functions) PrintWriter(java.io.PrintWriter) RelDataType(org.apache.calcite.rel.type.RelDataType) Litmus(org.apache.calcite.util.Litmus) Sarg(org.apache.calcite.util.Sarg) SqlKind(org.apache.calcite.sql.SqlKind) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimeZone(java.util.TimeZone) TimestampString(org.apache.calcite.util.TimestampString) ConversionUtil(org.apache.calcite.util.ConversionUtil) RelNode(org.apache.calcite.rel.RelNode) SqlCollation(org.apache.calcite.sql.SqlCollation) CompositeList(org.apache.calcite.util.CompositeList) Objects(java.util.Objects) List(java.util.List) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) FlatLists(org.apache.calcite.runtime.FlatLists) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Preconditions(com.google.common.base.Preconditions) Util(org.apache.calcite.util.Util) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) Sarg(org.apache.calcite.util.Sarg) TimeString(org.apache.calcite.util.TimeString) ByteString(org.apache.calcite.avatica.util.ByteString) Geometries(org.apache.calcite.runtime.Geometries) TimeString(org.apache.calcite.util.TimeString) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ByteString(org.apache.calcite.avatica.util.ByteString) TimestampString(org.apache.calcite.util.TimestampString) BigDecimal(java.math.BigDecimal) DateString(org.apache.calcite.util.DateString) NlsString(org.apache.calcite.util.NlsString) ImmutableList(com.google.common.collect.ImmutableList) CompositeList(org.apache.calcite.util.CompositeList) List(java.util.List) TimestampString(org.apache.calcite.util.TimestampString)

Example 92 with SqlTypeName

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

the class SqlImplementor method toSql.

/**
 * Converts a {@link RexLiteral} to a {@link SqlLiteral}.
 */
public static SqlNode toSql(RexLiteral literal) {
    SqlTypeName typeName = literal.getTypeName();
    switch(typeName) {
        case SYMBOL:
            final Enum symbol = (Enum) literal.getValue();
            return SqlLiteral.createSymbol(symbol, POS);
        case ROW:
            // noinspection unchecked
            final List<RexLiteral> list = castNonNull(literal.getValueAs(List.class));
            return SqlStdOperatorTable.ROW.createCall(POS, list.stream().map(e -> toSql(e)).collect(Util.toImmutableList()));
        case SARG:
            final Sarg arg = literal.getValueAs(Sarg.class);
            throw new AssertionError("sargs [" + arg + "] should be handled as part of predicates, not as literals");
        default:
            break;
    }
    SqlTypeFamily family = requireNonNull(typeName.getFamily(), () -> "literal " + literal + " has null SqlTypeFamily, and is SqlTypeName is " + typeName);
    switch(family) {
        case CHARACTER:
            return SqlLiteral.createCharString((String) castNonNull(literal.getValue2()), POS);
        case NUMERIC:
        case EXACT_NUMERIC:
            return SqlLiteral.createExactNumeric(castNonNull(literal.getValueAs(BigDecimal.class)).toPlainString(), POS);
        case APPROXIMATE_NUMERIC:
            return SqlLiteral.createApproxNumeric(castNonNull(literal.getValueAs(BigDecimal.class)).toPlainString(), POS);
        case BOOLEAN:
            return SqlLiteral.createBoolean(castNonNull(literal.getValueAs(Boolean.class)), POS);
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_DAY_TIME:
            final boolean negative = castNonNull(literal.getValueAs(Boolean.class));
            return SqlLiteral.createInterval(negative ? -1 : 1, castNonNull(literal.getValueAs(String.class)), castNonNull(literal.getType().getIntervalQualifier()), POS);
        case DATE:
            return SqlLiteral.createDate(castNonNull(literal.getValueAs(DateString.class)), POS);
        case TIME:
            return SqlLiteral.createTime(castNonNull(literal.getValueAs(TimeString.class)), literal.getType().getPrecision(), POS);
        case TIMESTAMP:
            return SqlLiteral.createTimestamp(castNonNull(literal.getValueAs(TimestampString.class)), literal.getType().getPrecision(), POS);
        case ANY:
        case NULL:
            switch(typeName) {
                case NULL:
                    return SqlLiteral.createNull(POS);
                default:
                    break;
            }
        // fall through
        default:
            throw new AssertionError(literal + ": " + typeName);
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Sarg(org.apache.calcite.util.Sarg) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) BigDecimal(java.math.BigDecimal)

Example 93 with SqlTypeName

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

the class TrainDBJdbcSchema method sqlType.

private static RelDataType sqlType(RelDataTypeFactory typeFactory, int dataType, int precision, int scale, @Nullable String typeString) {
    // Fall back to ANY if type is unknown
    final SqlTypeName sqlTypeName = Util.first(SqlTypeName.getNameForJdbcType(dataType), SqlTypeName.ANY);
    switch(sqlTypeName) {
        case ARRAY:
            RelDataType component = null;
            if (typeString != null && typeString.endsWith(" ARRAY")) {
                // E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type
                // "INTEGER".
                final String remaining = typeString.substring(0, typeString.length() - " ARRAY".length());
                component = parseTypeString(typeFactory, remaining);
            }
            if (component == null) {
                component = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.ANY), true);
            }
            return typeFactory.createArrayType(component, -1);
        default:
            break;
    }
    if (precision >= 0 && scale >= 0 && sqlTypeName.allowsPrecScale(true, true)) {
        return typeFactory.createSqlType(sqlTypeName, precision, scale);
    } else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) {
        return typeFactory.createSqlType(sqlTypeName, precision);
    } else {
        assert sqlTypeName.allowsNoPrecNoScale();
        return typeFactory.createSqlType(sqlTypeName);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 94 with SqlTypeName

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

the class TrainDBJdbcSchema method parseTypeString.

/**
 * Given "INTEGER", returns BasicSqlType(INTEGER).
 * Given "VARCHAR(10)", returns BasicSqlType(VARCHAR, 10).
 * Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
 */
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 95 with SqlTypeName

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

the class HazelcastTypeFactory method leastRestrictive.

/**
 * Finds the widest bit width integer type belonging to the same type name
 * (family) as the given target integer type from the given list of types.
 *
 * @param targetType the target type to find the widest instance of.
 * @param types      the list of types to inspect.
 * @return the widest integer type found.
 */
private static RelDataType leastRestrictive(RelDataType targetType, List<RelDataType> types) {
    SqlTypeName typeName = targetType.getSqlTypeName();
    assert HazelcastTypeUtils.isNumericIntegerType(typeName);
    int maxBitWidth = -1;
    RelDataType maxBitWidthType = null;
    for (RelDataType type : types) {
        if (type.getSqlTypeName() != typeName) {
            continue;
        }
        int bitWidth = ((HazelcastIntegerType) type).getBitWidth();
        if (bitWidth > maxBitWidth) {
            maxBitWidth = bitWidth;
            maxBitWidthType = type;
        }
    }
    assert maxBitWidthType != null;
    assert maxBitWidthType.getSqlTypeName() == typeName;
    return HazelcastIntegerType.create((HazelcastIntegerType) maxBitWidthType, targetType.isNullable());
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

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