Java Code Examples for org.apache.calcite.sql.SqlIntervalLiteral#IntervalValue
The following examples show how to use
org.apache.calcite.sql.SqlIntervalLiteral#IntervalValue .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Db2SqlDialect.java From Bats with Apache License 2.0 | 6 votes |
@Override public void unparseSqlIntervalLiteral(SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) { // A duration is a positive or negative number representing an interval of time. // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS. // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS. // If one operand is a timestamp, the other operand can be any of teh duration. SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); if (interval.getSign() == -1) { writer.print("-"); } writer.literal(literal.getValue().toString()); unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(), RelDataTypeSystem.DEFAULT); }
Example 2
Source File: BigQuerySqlDialect.java From calcite with Apache License 2.0 | 6 votes |
/** BigQuery interval syntax: INTERVAL int64 time_unit. */ @Override public void unparseSqlIntervalLiteral( SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) { SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); writer.keyword("INTERVAL"); if (interval.getSign() == -1) { writer.print("-"); } Long intervalValueInLong; try { intervalValueInLong = Long.parseLong(literal.getValue().toString()); } catch (NumberFormatException e) { throw new RuntimeException("Only INT64 is supported as the interval value for BigQuery."); } writer.literal(intervalValueInLong.toString()); unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(), RelDataTypeSystem.DEFAULT); }
Example 3
Source File: Db2SqlDialect.java From calcite with Apache License 2.0 | 6 votes |
@Override public void unparseSqlIntervalLiteral(SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) { // A duration is a positive or negative number representing an interval of time. // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS. // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS. // If one operand is a timestamp, the other operand can be any of teh duration. SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); if (interval.getSign() == -1) { writer.print("-"); } writer.literal(literal.getValue().toString()); unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(), RelDataTypeSystem.DEFAULT); }
Example 4
Source File: AbstractSqlTester.java From calcite with Apache License 2.0 | 6 votes |
public void checkIntervalConv(String sql, String expected) { SqlValidator validator = getValidator(); final SqlCall n = (SqlCall) parseAndValidate(validator, sql); SqlNode node = null; for (int i = 0; i < n.operandCount(); i++) { node = stripAs(n.operand(i)); if (node instanceof SqlCall) { node = ((SqlCall) node).operand(0); break; } } assertNotNull(node); SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node; SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue(); long l = interval.getIntervalQualifier().isYearMonth() ? SqlParserUtil.intervalToMonths(interval) : SqlParserUtil.intervalToMillis(interval); String actual = l + ""; assertEquals(expected, actual); }
Example 5
Source File: StandardConvertletTable.java From Bats with Apache License 2.0 | 5 votes |
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) { final boolean floor = call.getKind() == SqlKind.FLOOR; // Rewrite floor, ceil of interval if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) { final SqlIntervalLiteral literal = call.operand(0); SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier; RexNode rexInterval = cx.convertExpression(literal); final RexBuilder rexBuilder = cx.getRexBuilder(); RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0)); RexNode cond = ge(rexBuilder, rexInterval, zero); RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE)); RexNode cast = rexBuilder.makeReinterpretCast( rexInterval.getType(), pad, rexBuilder.makeLiteral(false)); RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast); RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval); RexNode factor = rexBuilder.makeExactLiteral(val); RexNode div = divideInt(rexBuilder, kase, factor); return multiply(rexBuilder, div, factor); } // normal floor, ceil function return convertFunction(cx, (SqlFunction) call.getOperator(), call); }
Example 6
Source File: MssqlSqlDialect.java From Bats with Apache License 2.0 | 5 votes |
private void unparseSqlIntervalLiteralMssql( SqlWriter writer, SqlIntervalLiteral literal, int sign) { final SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(), RelDataTypeSystem.DEFAULT); writer.sep(",", true); if (interval.getSign() * sign == -1) { writer.print("-"); } writer.literal(literal.getValue().toString()); }
Example 7
Source File: StandardConvertletTable.java From calcite with Apache License 2.0 | 5 votes |
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) { final boolean floor = call.getKind() == SqlKind.FLOOR; // Rewrite floor, ceil of interval if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) { final SqlIntervalLiteral literal = call.operand(0); SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier; RexNode rexInterval = cx.convertExpression(literal); final RexBuilder rexBuilder = cx.getRexBuilder(); RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0)); RexNode cond = ge(rexBuilder, rexInterval, zero); RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE)); RexNode cast = rexBuilder.makeReinterpretCast( rexInterval.getType(), pad, rexBuilder.makeLiteral(false)); RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast); RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval); RexNode factor = rexBuilder.makeExactLiteral(val); RexNode div = divideInt(rexBuilder, kase, factor); return multiply(rexBuilder, div, factor); } // normal floor, ceil function return convertFunction(cx, (SqlFunction) call.getOperator(), call); }
Example 8
Source File: MssqlSqlDialect.java From calcite with Apache License 2.0 | 5 votes |
private void unparseSqlIntervalLiteralMssql( SqlWriter writer, SqlIntervalLiteral literal, int sign) { final SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(), RelDataTypeSystem.DEFAULT); writer.sep(",", true); if (interval.getSign() * sign == -1) { writer.print("-"); } writer.literal(literal.getValue().toString()); }
Example 9
Source File: SqlNodeToRexConverterImpl.java From Bats with Apache License 2.0 | 4 votes |
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: // approximate type // 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()); } }
Example 10
Source File: SqlValidatorImpl.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
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: // default is to do nothing } }
Example 11
Source File: SqlValidatorImpl.java From flink with Apache License 2.0 | 4 votes |
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: // default is to do nothing } }
Example 12
Source File: SqlNodeToRexConverterImpl.java From calcite with Apache License 2.0 | 4 votes |
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.makeNullLiteral(type); } 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: // approximate type // 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()); } }
Example 13
Source File: SqlParserUtil.java From Bats with Apache License 2.0 | 3 votes |
/** * Converts the interval value into a millisecond representation. * * @param interval Interval * @return a long value that represents millisecond equivalent of the * interval value. */ public static long intervalToMillis( SqlIntervalLiteral.IntervalValue interval) { return intervalToMillis( interval.getIntervalLiteral(), interval.getIntervalQualifier()); }
Example 14
Source File: SqlParserUtil.java From Bats with Apache License 2.0 | 3 votes |
/** * Converts the interval value into a months representation. * * @param interval Interval * @return a long value that represents months equivalent of the interval * value. */ public static long intervalToMonths( SqlIntervalLiteral.IntervalValue interval) { return intervalToMonths( interval.getIntervalLiteral(), interval.getIntervalQualifier()); }
Example 15
Source File: SqlParserUtil.java From calcite with Apache License 2.0 | 3 votes |
/** * Converts the interval value into a millisecond representation. * * @param interval Interval * @return a long value that represents millisecond equivalent of the * interval value. */ public static long intervalToMillis( SqlIntervalLiteral.IntervalValue interval) { return intervalToMillis( interval.getIntervalLiteral(), interval.getIntervalQualifier()); }
Example 16
Source File: SqlParserUtil.java From calcite with Apache License 2.0 | 3 votes |
/** * Converts the interval value into a months representation. * * @param interval Interval * @return a long value that represents months equivalent of the interval * value. */ public static long intervalToMonths( SqlIntervalLiteral.IntervalValue interval) { return intervalToMonths( interval.getIntervalLiteral(), interval.getIntervalQualifier()); }