Search in sources :

Example 71 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.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 72 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.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 73 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.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 74 with SqlTypeName

use of org.apache.beam.vendor.calcite.v1_28_0.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)

Example 75 with SqlTypeName

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

the class SamzaSqlValidator method compareFieldTypes.

private boolean compareFieldTypes(RelDataType outputFieldType, SqlFieldSchema sqlFieldSchema, RelDataType selectQueryFieldType, RelSchemaProvider outputRelSchemaProvider) {
    SqlTypeName outputSqlType = outputFieldType.getSqlTypeName();
    SqlTypeName projectSqlType = selectQueryFieldType.getSqlTypeName();
    if (projectSqlType == SqlTypeName.ANY || outputSqlType == SqlTypeName.ANY) {
        return true;
    } else if (outputSqlType != SqlTypeName.ROW && outputSqlType == projectSqlType) {
        return true;
    }
    switch(outputSqlType) {
        case CHAR:
            return projectSqlType == SqlTypeName.VARCHAR;
        case VARCHAR:
            return projectSqlType == SqlTypeName.CHAR;
        case BIGINT:
            return projectSqlType == SqlTypeName.INTEGER;
        case INTEGER:
            return projectSqlType == SqlTypeName.BIGINT;
        case FLOAT:
            return projectSqlType == SqlTypeName.DOUBLE;
        case DOUBLE:
            return projectSqlType == SqlTypeName.FLOAT;
        case ROW:
            try {
                validateOutputRecords(sqlFieldSchema.getRowSchema(), (RelRecordType) outputFieldType, (RelRecordType) selectQueryFieldType, outputRelSchemaProvider);
            } catch (SamzaSqlValidatorException e) {
                LOG.error("A field in select query does not match with the output schema.", e);
                return false;
            }
            return true;
        default:
            return false;
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)117 Test (org.junit.Test)38 RelDataType (org.apache.calcite.rel.type.RelDataType)28 RexNode (org.apache.calcite.rex.RexNode)18 BigDecimal (java.math.BigDecimal)13 ArrayList (java.util.ArrayList)13 List (java.util.List)11 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)8 ImmutableList (com.google.common.collect.ImmutableList)7 Map (java.util.Map)7 DateString (org.apache.calcite.util.DateString)7 TimeString (org.apache.calcite.util.TimeString)7 TimestampString (org.apache.calcite.util.TimestampString)7 ISE (io.druid.java.util.common.ISE)6 SqlKind (org.apache.calcite.sql.SqlKind)6 NlsString (org.apache.calcite.util.NlsString)6 Calendar (java.util.Calendar)5 Nullable (javax.annotation.Nullable)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5