Search in sources :

Example 96 with SqlTypeName

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

the class RexToExpression method extractRangeFromSearch.

@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
public static RangeSet extractRangeFromSearch(RexLiteral literal) {
    Sarg<?> sarg = literal.getValueAs(Sarg.class);
    if (sarg == null) {
        return null;
    }
    RelDataType literalType = literal.getType();
    SqlTypeName sqlType = literalType.getSqlTypeName();
    return RangeSets.copy(sarg.rangeSet, value -> convertSargValue(value, sqlType));
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 97 with SqlTypeName

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

the class OptUtils method convert.

/**
 * Converts a {@link TableField} to {@link RelDataType}.
 */
public static RelDataType convert(TableField field, RelDataTypeFactory typeFactory) {
    QueryDataType fieldType = field.getType();
    SqlTypeName sqlTypeName = HazelcastTypeUtils.toCalciteType(fieldType);
    if (sqlTypeName == null) {
        throw new IllegalStateException("Unsupported type family: " + fieldType + ", getSqlTypeName should never return null.");
    }
    if (sqlTypeName == SqlTypeName.OTHER) {
        return convertCustomType(fieldType);
    } else {
        RelDataType relType = typeFactory.createSqlType(sqlTypeName);
        return typeFactory.createTypeWithNullability(relType, true);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 98 with SqlTypeName

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

the class LiteralUtils method literal.

public static Literal literal(RexNode node) {
    if (node.getKind() != SqlKind.LITERAL) {
        // Not a literal
        return null;
    }
    RexLiteral literal = (RexLiteral) node;
    SqlTypeName typeName = literal.getTypeName();
    // Intercept DATE, TIME, TIMESTAMP types to return (Offset, Local)(Date, Time) instead of Calendar class
    Object value = (DATETIME_TYPES.contains(typeName) && typeName != SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE) ? createDateTimeValue(typeName, literal) : literal.getValue();
    return literal0(node.getType().getSqlTypeName(), value);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 99 with SqlTypeName

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

the class OperatorUtils method getFieldName.

/**
 * This method gives field name for POJO class for given {@link RelDataTypeField} object.
 *
 * @param field field object that needs to be converted to POJO class field name
 * @return Return field name from POJO class
 */
public static String getFieldName(RelDataTypeField field) {
    SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
    String name = getValidFieldName(field);
    name = (sqlTypeName == SqlTypeName.TIMESTAMP) ? (name + "Ms") : ((sqlTypeName == SqlTypeName.DATE) ? (name + "Sec") : name);
    return name;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Example 100 with SqlTypeName

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

the class BeamSqlUnparseContext method toSql.

@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind().equals(SqlKind.LITERAL)) {
        final RexLiteral literal = (RexLiteral) rex;
        SqlTypeName name = literal.getTypeName();
        SqlTypeFamily family = name.getFamily();
        if (SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE.equals(name)) {
            TimestampString timestampString = literal.getValueAs(TimestampString.class);
            return new SqlDateTimeLiteral(timestampString, POS);
        } else if (SqlTypeFamily.BINARY.equals(family)) {
            ByteString byteString = literal.getValueAs(ByteString.class);
            BitString bitString = BitString.createFromHexString(byteString.toString(16));
            return new SqlByteStringLiteral(bitString, POS);
        } else if (SqlTypeFamily.CHARACTER.equals(family)) {
            String escaped = ESCAPE_FOR_ZETA_SQL.translate(literal.getValueAs(String.class));
            return SqlLiteral.createCharString(escaped, POS);
        } else if (SqlTypeName.SYMBOL.equals(literal.getTypeName())) {
            Enum symbol = literal.getValueAs(Enum.class);
            if (TimeUnitRange.DOW.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFWEEK");
            } else if (TimeUnitRange.DOY.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "DAYOFYEAR");
            } else if (TimeUnitRange.WEEK.equals(symbol)) {
                return new ReplaceLiteral(literal, POS, "ISOWEEK");
            }
        }
    } else if (rex.getKind().equals(SqlKind.DYNAMIC_PARAM)) {
        final RexDynamicParam param = (RexDynamicParam) rex;
        final int index = param.getIndex();
        final String name = "null_param_" + index;
        nullParams.put(name, param.getType());
        return new NamedDynamicParam(index, POS, name);
    } else if (SqlKind.SEARCH.equals(rex.getKind())) {
        // Workaround CALCITE-4716
        RexCall search = (RexCall) rex;
        RexLocalRef ref = (RexLocalRef) search.operands.get(1);
        RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
        rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
    }
    return super.toSql(program, rex);
}
Also used : RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) SqlTypeName(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName) SqlTypeFamily(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) ByteString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) BitString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString) RexLocalRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef) TimestampString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString) RexDynamicParam(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)

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