Search in sources :

Example 1 with BitString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString in project calcite by apache.

the class SqlValidatorImpl method validateLiteral.

public void validateLiteral(SqlLiteral literal) {
    switch(literal.getTypeName()) {
        case DECIMAL:
            // Decimal and long have the same precision (as 64-bit integers), so
            // the unscaled value of a decimal must fit into a long.
            // REVIEW jvs 4-Aug-2004:  This should probably be calling over to
            // the available calculator implementations to see what they
            // support.  For now use ESP instead.
            // 
            // jhyde 2006/12/21: I think the limits should be baked into the
            // type system, not dependent on the calculator implementation.
            BigDecimal bd = (BigDecimal) literal.getValue();
            BigInteger unscaled = bd.unscaledValue();
            long longValue = unscaled.longValue();
            if (!BigInteger.valueOf(longValue).equals(unscaled)) {
                // overflow
                throw newValidationError(literal, RESOURCE.numberLiteralOutOfRange(bd.toString()));
            }
            break;
        case DOUBLE:
            validateLiteralAsDouble(literal);
            break;
        case BINARY:
            final BitString bitString = (BitString) literal.getValue();
            if ((bitString.getBitCount() % 8) != 0) {
                throw newValidationError(literal, RESOURCE.binaryLiteralOdd());
            }
            break;
        case DATE:
        case TIME:
        case TIMESTAMP:
            Calendar calendar = literal.getValueAs(Calendar.class);
            final int year = calendar.get(Calendar.YEAR);
            final int era = calendar.get(Calendar.ERA);
            if (year < 1 || era == GregorianCalendar.BC || year > 9999) {
                throw newValidationError(literal, RESOURCE.dateLiteralOutOfRange(literal.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:
            if (literal instanceof SqlIntervalLiteral) {
                SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue();
                SqlIntervalQualifier intervalQualifier = interval.getIntervalQualifier();
                // ensure qualifier is good before attempting to validate literal
                validateIntervalQualifier(intervalQualifier);
                String intervalStr = interval.getIntervalLiteral();
                // throws CalciteContextException if string is invalid
                int[] values = intervalQualifier.evaluateIntervalLiteral(intervalStr, literal.getParserPosition(), typeFactory.getTypeSystem());
                Util.discard(values);
            }
            break;
        default:
    }
}
Also used : BitString(org.apache.calcite.util.BitString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) BigInteger(java.math.BigInteger) BitString(org.apache.calcite.util.BitString) BigDecimal(java.math.BigDecimal)

Example 2 with BitString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString in project flink by apache.

the class SqlValidatorImpl method validateLiteral.

public void validateLiteral(SqlLiteral literal) {
    switch(literal.getTypeName()) {
        case DECIMAL:
            // Decimal and long have the same precision (as 64-bit integers), so
            // the unscaled value of a decimal must fit into a long.
            // REVIEW jvs 4-Aug-2004:  This should probably be calling over to
            // the available calculator implementations to see what they
            // support.  For now use ESP instead.
            // 
            // jhyde 2006/12/21: I think the limits should be baked into the
            // type system, not dependent on the calculator implementation.
            BigDecimal bd = (BigDecimal) literal.getValue();
            BigInteger unscaled = bd.unscaledValue();
            long longValue = unscaled.longValue();
            if (!BigInteger.valueOf(longValue).equals(unscaled)) {
                // overflow
                throw newValidationError(literal, RESOURCE.numberLiteralOutOfRange(bd.toString()));
            }
            break;
        case DOUBLE:
            validateLiteralAsDouble(literal);
            break;
        case BINARY:
            final BitString bitString = (BitString) literal.getValue();
            if ((bitString.getBitCount() % 8) != 0) {
                throw newValidationError(literal, RESOURCE.binaryLiteralOdd());
            }
            break;
        case DATE:
        case TIME:
        case TIMESTAMP:
            Calendar calendar = literal.getValueAs(Calendar.class);
            final int year = calendar.get(Calendar.YEAR);
            final int era = calendar.get(Calendar.ERA);
            if (year < 1 || era == GregorianCalendar.BC || year > 9999) {
                throw newValidationError(literal, RESOURCE.dateLiteralOutOfRange(literal.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:
            if (literal instanceof SqlIntervalLiteral) {
                SqlIntervalLiteral.IntervalValue interval = literal.getValueAs(SqlIntervalLiteral.IntervalValue.class);
                SqlIntervalQualifier intervalQualifier = interval.getIntervalQualifier();
                // ensure qualifier is good before attempting to validate literal
                validateIntervalQualifier(intervalQualifier);
                String intervalStr = interval.getIntervalLiteral();
                // throws CalciteContextException if string is invalid
                int[] values = intervalQualifier.evaluateIntervalLiteral(intervalStr, literal.getParserPosition(), typeFactory.getTypeSystem());
                Util.discard(values);
            }
            break;
        default:
    }
}
Also used : BitString(org.apache.calcite.util.BitString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) BigInteger(java.math.BigInteger) BitString(org.apache.calcite.util.BitString) BigDecimal(java.math.BigDecimal)

Example 3 with BitString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString in project calcite by apache.

the class SqlLiteral method createSqlType.

public RelDataType createSqlType(RelDataTypeFactory typeFactory) {
    BitString bitString;
    switch(typeName) {
        case NULL:
        case BOOLEAN:
            RelDataType ret = typeFactory.createSqlType(typeName);
            ret = typeFactory.createTypeWithNullability(ret, null == value);
            return ret;
        case BINARY:
            bitString = (BitString) value;
            int bitCount = bitString.getBitCount();
            return typeFactory.createSqlType(SqlTypeName.BINARY, bitCount / 8);
        case CHAR:
            NlsString string = (NlsString) value;
            Charset charset = string.getCharset();
            if (null == charset) {
                charset = typeFactory.getDefaultCharset();
            }
            SqlCollation collation = string.getCollation();
            if (null == collation) {
                collation = SqlCollation.COERCIBLE;
            }
            RelDataType type = typeFactory.createSqlType(SqlTypeName.CHAR, string.getValue().length());
            type = typeFactory.createTypeWithCharsetAndCollation(type, charset, collation);
            return type;
        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:
            SqlIntervalLiteral.IntervalValue intervalValue = (SqlIntervalLiteral.IntervalValue) value;
            return typeFactory.createSqlIntervalType(intervalValue.getIntervalQualifier());
        case SYMBOL:
            return typeFactory.createSqlType(SqlTypeName.SYMBOL);
        // handled in derived class
        case INTEGER:
        // handled in derived class
        case TIME:
        // should never happen
        case VARCHAR:
        // should never happen
        case VARBINARY:
        default:
            throw Util.needToImplement(toString() + ", operand=" + value);
    }
}
Also used : BitString(org.apache.calcite.util.BitString) NlsString(org.apache.calcite.util.NlsString) Charset(java.nio.charset.Charset) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 4 with BitString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString in project calcite by apache.

the class SqlLiteralChainOperator method unparse.

public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
    final SqlWriter.Frame frame = writer.startList("", "");
    SqlCollation collation = null;
    for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
        SqlLiteral rand = (SqlLiteral) operand.e;
        if (operand.i > 0) {
            // SQL:2003 says there must be a newline between string
            // fragments.
            writer.newlineAndIndent();
        }
        if (rand instanceof SqlCharStringLiteral) {
            NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
            if (operand.i == 0) {
                collation = nls.getCollation();
                // print with prefix
                writer.literal(nls.asSql(true, false));
            } else {
                // print without prefix
                writer.literal(nls.asSql(false, false));
            }
        } else if (operand.i == 0) {
            // print with prefix
            rand.unparse(writer, leftPrec, rightPrec);
        } else {
            // print without prefix
            if (rand.getTypeName() == SqlTypeName.BINARY) {
                BitString bs = (BitString) rand.getValue();
                writer.literal("'" + bs.toHexString() + "'");
            } else {
                writer.literal("'" + rand.toValue() + "'");
            }
        }
    }
    if (collation != null) {
        collation.unparse(writer, 0, 0);
    }
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) BitString(org.apache.calcite.util.BitString) SqlCollation(org.apache.calcite.sql.SqlCollation) NlsString(org.apache.calcite.util.NlsString) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with BitString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString in project calcite by apache.

the class SqlNodeToRexConverterImpl method convertLiteral.

public RexNode convertLiteral(SqlRexContext cx, SqlLiteral literal) {
    RexBuilder rexBuilder = cx.getRexBuilder();
    RelDataTypeFactory typeFactory = cx.getTypeFactory();
    SqlValidator validator = cx.getValidator();
    if (literal.getValue() == null) {
        // Since there is no eq. RexLiteral of SqlLiteral.Unknown we
        // treat it as a cast(null as boolean)
        RelDataType type;
        if (literal.getTypeName() == SqlTypeName.BOOLEAN) {
            type = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
            type = typeFactory.createTypeWithNullability(type, true);
        } else {
            type = validator.getValidatedNodeType(literal);
        }
        return rexBuilder.makeCast(type, rexBuilder.constantNull());
    }
    BitString bitString;
    SqlIntervalLiteral.IntervalValue intervalValue;
    long l;
    switch(literal.getTypeName()) {
        case DECIMAL:
            // exact number
            BigDecimal bd = literal.getValueAs(BigDecimal.class);
            return rexBuilder.makeExactLiteral(bd, literal.createSqlType(typeFactory));
        case DOUBLE:
            // TODO:  preserve fixed-point precision and large integers
            return rexBuilder.makeApproxLiteral(literal.getValueAs(BigDecimal.class));
        case CHAR:
            return rexBuilder.makeCharLiteral(literal.getValueAs(NlsString.class));
        case BOOLEAN:
            return rexBuilder.makeLiteral(literal.getValueAs(Boolean.class));
        case BINARY:
            bitString = literal.getValueAs(BitString.class);
            Preconditions.checkArgument((bitString.getBitCount() % 8) == 0, "incomplete octet");
            // An even number of hexits (e.g. X'ABCD') makes whole number
            // of bytes.
            ByteString byteString = new ByteString(bitString.getAsByteArray());
            return rexBuilder.makeBinaryLiteral(byteString);
        case SYMBOL:
            return rexBuilder.makeFlag(literal.getValueAs(Enum.class));
        case TIMESTAMP:
            return rexBuilder.makeTimestampLiteral(literal.getValueAs(TimestampString.class), ((SqlTimestampLiteral) literal).getPrec());
        case TIME:
            return rexBuilder.makeTimeLiteral(literal.getValueAs(TimeString.class), ((SqlTimeLiteral) literal).getPrec());
        case DATE:
            return rexBuilder.makeDateLiteral(literal.getValueAs(DateString.class));
        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:
            SqlIntervalQualifier sqlIntervalQualifier = literal.getValueAs(SqlIntervalLiteral.IntervalValue.class).getIntervalQualifier();
            return rexBuilder.makeIntervalLiteral(literal.getValueAs(BigDecimal.class), sqlIntervalQualifier);
        default:
            throw Util.unexpected(literal.getTypeName());
    }
}
Also used : TimeString(org.apache.calcite.util.TimeString) SqlIntervalQualifier(org.apache.calcite.sql.SqlIntervalQualifier) ByteString(org.apache.calcite.avatica.util.ByteString) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) BitString(org.apache.calcite.util.BitString) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) DateString(org.apache.calcite.util.DateString) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) NlsString(org.apache.calcite.util.NlsString) TimestampString(org.apache.calcite.util.TimestampString)

Aggregations

BitString (org.apache.calcite.util.BitString)5 BigDecimal (java.math.BigDecimal)3 SqlIntervalLiteral (org.apache.calcite.sql.SqlIntervalLiteral)3 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)3 NlsString (org.apache.calcite.util.NlsString)3 BigInteger (java.math.BigInteger)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Charset (java.nio.charset.Charset)1 ByteString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString)1 RexCall (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall)1 RexDynamicParam (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexDynamicParam)1 RexLiteral (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral)1 RexLocalRef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef)1 SqlTypeFamily (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeFamily)1 SqlTypeName (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.SqlTypeName)1 BitString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.BitString)1 TimestampString (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString)1 ByteString (org.apache.calcite.avatica.util.ByteString)1