Search in sources :

Example 56 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 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 57 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(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 58 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 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)

Example 59 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 HazelcastOperandTypeInference method inferOperandTypes.

@Override
public void inferOperandTypes(SqlCallBinding callBinding, RelDataType returnType, RelDataType[] operandTypes) {
    SqlCall call = callBinding.getCall();
    if (ValidationUtil.hasAssignment(call)) {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        RelDataType[] parameterTypes = new RelDataType[parametersByName.size()];
        for (int i = 0; i < call.operandCount(); i++) {
            SqlCall assignment = call.operand(i);
            SqlIdentifier id = assignment.operand(1);
            String name = id.getSimple();
            HazelcastTableFunctionParameter parameter = parametersByName.get(name);
            if (parameter != null) {
                SqlTypeName parameterType = parameter.type();
                parameterTypes[parameter.ordinal()] = toType(parameterType, typeFactory);
            } else {
                throw SqlUtil.newContextException(id.getParserPosition(), RESOURCE.unknownArgumentName(name));
            }
        }
        // noinspection ResultOfMethodCallIgnored
        Arrays.stream(parameterTypes).filter(Objects::nonNull).toArray(ignored -> operandTypes);
    } else {
        positionalOperandTypeInference.inferOperandTypes(callBinding, returnType, operandTypes);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SqlCall(org.apache.calcite.sql.SqlCall) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) HazelcastTableFunctionParameter(com.hazelcast.jet.sql.impl.schema.HazelcastTableFunctionParameter) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 60 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 HazelcastTypeFactory method leastRestrictive.

@Override
public RelDataType leastRestrictive(List<RelDataType> types) {
    // special-case for JSON - see https://github.com/hazelcast/hazelcast/issues/20303
    // SqlTypeName for JSON is OTHER, there's missing handling for OTHER in SqlTypeAssignmentRule,
    // and we don't know how to add it there. And even if we did, OTHER can represent both JSON and
    // a JAVA object, and these aren't assignable.
    boolean containsNullable = false;
    boolean allJson = true;
    boolean allJsonOrVarchar = true;
    for (RelDataType type : types) {
        if (!(type instanceof HazelcastJsonType)) {
            allJson = false;
        }
        if (!(type instanceof HazelcastJsonType) && type.getSqlTypeName() != VARCHAR) {
            allJsonOrVarchar = false;
        }
        if (type.isNullable()) {
            containsNullable = true;
        }
    }
    if (allJson) {
        return containsNullable ? HazelcastJsonType.TYPE_NULLABLE : HazelcastJsonType.TYPE;
    }
    if (allJsonOrVarchar) {
        return createSqlType(VARCHAR, containsNullable);
    }
    // Calcite returns BIGINT for all integer types and DOUBLE for all inexact fractional types.
    // This code allows us to use more narrow types in these cases.
    RelDataType selected = super.leastRestrictive(types);
    if (selected == null) {
        return null;
    }
    SqlTypeName selectedTypeName = selected.getSqlTypeName();
    if (HazelcastTypeUtils.isNumericIntegerType(selectedTypeName)) {
        return leastRestrictive(selected, types);
    }
    if (selectedTypeName == DOUBLE) {
        boolean seenDouble = false;
        boolean seenReal = false;
        for (RelDataType type : types) {
            if (type.getSqlTypeName() == DOUBLE) {
                seenDouble = true;
                break;
            }
            if (type.getSqlTypeName() == REAL) {
                seenReal = true;
            }
        }
        if (!seenDouble && seenReal) {
            selected = createSqlType(REAL, selected.isNullable());
        }
    }
    return selected;
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType)

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