Search in sources :

Example 6 with SqlIntervalLiteral

use of org.apache.calcite.sql.SqlIntervalLiteral in project calcite by apache.

the class MssqlSqlDialect method unparseSqlDatetimeArithmetic.

@Override
public void unparseSqlDatetimeArithmetic(SqlWriter writer, SqlCall call, SqlKind sqlKind, int leftPrec, int rightPrec) {
    final SqlWriter.Frame frame = writer.startFunCall("DATEADD");
    SqlNode operand = call.operand(1);
    if (operand instanceof SqlIntervalLiteral) {
        // There is no DATESUB method available, so change the sign.
        unparseSqlIntervalLiteralMssql(writer, (SqlIntervalLiteral) operand, sqlKind == SqlKind.MINUS ? -1 : 1);
    } else {
        operand.unparse(writer, leftPrec, rightPrec);
    }
    writer.sep(",", true);
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    writer.endList(frame);
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlIntervalLiteral(org.apache.calcite.sql.SqlIntervalLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlIntervalLiteral

use of org.apache.calcite.sql.SqlIntervalLiteral 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)

Aggregations

SqlIntervalLiteral (org.apache.calcite.sql.SqlIntervalLiteral)7 BigDecimal (java.math.BigDecimal)4 SqlNode (org.apache.calcite.sql.SqlNode)4 RexNode (org.apache.calcite.rex.RexNode)3 SqlIntervalQualifier (org.apache.calcite.sql.SqlIntervalQualifier)3 BigInteger (java.math.BigInteger)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 RexLiteral (org.apache.calcite.rex.RexLiteral)2 BitString (org.apache.calcite.util.BitString)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 SortedSet (java.util.SortedSet)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 RelNode (org.apache.calcite.rel.RelNode)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexCall (org.apache.calcite.rex.RexCall)1