Search in sources :

Example 16 with SqlTypeName

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

the class HazelcastRexBuilder method makeLiteral.

@Override
public RexNode makeLiteral(Object value, RelDataType type, boolean allowCast) {
    if (type.getSqlTypeName() == ANY && value instanceof Number) {
        Converter converter = Converters.getConverter(value.getClass());
        if (converter != null) {
            QueryDataTypeFamily typeFamily = converter.getTypeFamily();
            if (typeFamily.isNumericInteger()) {
                int bitWidth = HazelcastIntegerType.bitWidthOf(((Number) value).longValue());
                type = HazelcastIntegerType.create(bitWidth, false);
            } else {
                SqlTypeName typeName = HazelcastTypeUtils.toCalciteType(typeFamily);
                type = HazelcastTypeFactory.INSTANCE.createSqlType(typeName);
            }
        }
    }
    return super.makeLiteral(value, type, allowCast);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Converter(com.hazelcast.sql.impl.type.converter.Converter)

Example 17 with SqlTypeName

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

the class WindowOperandMetadata method checkOperandTypes.

private boolean checkOperandTypes(HazelcastCallBinding binding, boolean throwOnFailure, int columnIndex) {
    SqlNode lag = binding.operand(columnIndex);
    HazelcastSqlValidator validator = binding.getValidator();
    SqlTypeName orderingColumnType = getOrderingColumnType(binding, 1).getSqlTypeName();
    boolean result;
    SqlTypeName lagType = ((SqlValidator) validator).getValidatedNodeType(lag).getSqlTypeName();
    if (SqlTypeName.INT_TYPES.contains(orderingColumnType)) {
        result = SqlTypeName.INT_TYPES.contains(lagType);
    } else if (SqlTypeName.DATETIME_TYPES.contains(orderingColumnType)) {
        result = lagType.getFamily() == SqlTypeFamily.INTERVAL_DAY_TIME;
    } else {
        result = false;
    }
    if (!result && throwOnFailure) {
        QueryDataTypeFamily hzOrderingColumnType = toHazelcastTypeFromSqlTypeName(orderingColumnType).getTypeFamily();
        QueryDataTypeFamily hzLagType = toHazelcastTypeFromSqlTypeName(lagType).getTypeFamily();
        throw validator.newValidationError(binding.getCall(), RESOURCES.windowFunctionTypeMismatch(hzOrderingColumnType.toString(), hzLagType.toString()));
    }
    return result;
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HazelcastSqlValidator(com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator) SqlNode(org.apache.calcite.sql.SqlNode)

Example 18 with SqlTypeName

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

the class RexToExpression method convertLiteral.

/**
 * Converts the given REX literal to runtime {@link ConstantExpression
 * constant expression}.
 *
 * @param literal the literal to convert.
 * @return the resulting constant expression.
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:ReturnCount" })
public static Expression<?> convertLiteral(RexLiteral literal) {
    final RelDataType type = literal.getType();
    final SqlTypeName typeName = literal.getTypeName();
    if (literal.getValue() == null) {
        return ConstantExpression.create(null, HazelcastTypeUtils.toHazelcastType(type));
    }
    if (literal.getTypeName() == SqlTypeName.SARG) {
        return convertSargLiteral(literal, type);
    }
    switch(typeName) {
        case BOOLEAN:
            return convertBooleanLiteral(literal, type);
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case DECIMAL:
        case REAL:
        case FLOAT:
        case DOUBLE:
            return convertNumericLiteral(literal, type);
        case CHAR:
        case VARCHAR:
            return convertStringLiteral(literal, type);
        case NULL:
            return ConstantExpression.create(null, QueryDataType.NULL);
        case SYMBOL:
            return SymbolExpression.create(literal.getValue());
        case DATE:
            return convertDateLiteral(literal);
        case TIME:
            return convertTimeLiteral(literal);
        case TIMESTAMP:
            return convertTimestamp(literal);
        case INTERVAL_YEAR_MONTH:
            return convertIntervalYearMonth(literal);
        case INTERVAL_DAY_SECOND:
            return convertIntervalDaySecond(literal);
        default:
            throw QueryException.error("Unsupported literal: " + literal);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 19 with SqlTypeName

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

the class LiteralUtils method literal.

public static Literal literal(SqlNode node) {
    if (node.getKind() != SqlKind.LITERAL) {
        // Not a literal
        return null;
    }
    SqlLiteral literal = (SqlLiteral) node;
    SqlTypeName typeName = literal.getTypeName();
    Object value = CHAR_TYPES.contains(typeName) ? literal.toValue() : literal.getValue();
    return literal0(typeName, value);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlLiteral(org.apache.calcite.sql.SqlLiteral)

Example 20 with SqlTypeName

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

the class HazelcastTrimFunction method getOperandsForSignatureError.

@Override
public Collection<SqlNode> getOperandsForSignatureError(SqlCall call) {
    SqlNode fromOperand = call.operand(1);
    SqlNode targetOperand = call.operand(2);
    SqlTypeName literalType = LiteralUtils.literalTypeName(fromOperand);
    if (literalType == SqlTypeName.VARCHAR && " ".equals(((SqlLiteral) fromOperand).getValueAs(String.class))) {
        // Default value for the FROM operand, report only target operand.
        return Collections.singletonList(targetOperand);
    }
    // Non-default FROM, report both target and FROM operands.
    return Arrays.asList(fromOperand, targetOperand);
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)186 RelDataType (org.apache.calcite.rel.type.RelDataType)63 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)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)26 RexNode (org.apache.calcite.rex.RexNode)26 Map (java.util.Map)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)16 Calendar (java.util.Calendar)14 Charset (java.nio.charset.Charset)13 Objects (java.util.Objects)13