Java Code Examples for org.apache.calcite.sql.SqlLiteral#getValueAs()
The following examples show how to use
org.apache.calcite.sql.SqlLiteral#getValueAs() .
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: OracleSqlDialect.java From Bats with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) { SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call); } else { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } } }
Example 2
Source File: HsqldbSqlDialect.java From Bats with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); final String translatedLit = convertTimeUnit(timeUnit); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } }
Example 3
Source File: PostgresqlSqlDialect.java From Bats with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } }
Example 4
Source File: OracleSqlDialect.java From calcite with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) { SqlUtil.unparseFunctionSyntax(SqlLibraryOperators.SUBSTR, writer, call); } else { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } } }
Example 5
Source File: HsqldbSqlDialect.java From calcite with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); final String translatedLit = convertTimeUnit(timeUnit); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } }
Example 6
Source File: PostgresqlSqlDialect.java From calcite with Apache License 2.0 | 6 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } }
Example 7
Source File: RelToSqlConverterUtil.java From calcite with Apache License 2.0 | 6 votes |
/** * Unparses TRIM function with value as space. * * <p>For example : * * <blockquote><pre> * SELECT TRIM(both ' ' from "ABC") → SELECT TRIM(ABC) * </pre></blockquote> * * @param writer writer * @param call the call */ private static void unparseTrimWithSpace( SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { final String operatorName; final SqlLiteral trimFlag = call.operand(0); switch (trimFlag.getValueAs(SqlTrimFunction.Flag.class)) { case LEADING: operatorName = "LTRIM"; break; case TRAILING: operatorName = "RTRIM"; break; default: operatorName = call.getOperator().getName(); break; } final SqlWriter.Frame trimFrame = writer.startFunCall(operatorName); call.operand(2).unparse(writer, leftPrec, rightPrec); writer.endFunCall(trimFrame); }
Example 8
Source File: RelToSqlConverterUtil.java From calcite with Apache License 2.0 | 6 votes |
/** * Creates regex pattern based on the TRIM flag. * * @param call SqlCall contains the values that need to be trimmed * @param trimFlag the trimFlag, either BOTH, LEADING or TRAILING * @return the regex pattern of the character to be trimmed */ public static SqlCharStringLiteral createRegexPatternLiteral(SqlNode call, SqlLiteral trimFlag) { final String regexPattern = ((SqlCharStringLiteral) call).toValue(); String escaped = escapeSpecialChar(regexPattern); final StringBuilder builder = new StringBuilder(); switch (trimFlag.getValueAs(SqlTrimFunction.Flag.class)) { case LEADING: builder.append("^(").append(escaped).append(")*"); break; case TRAILING: builder.append("(").append(escaped).append(")*$"); break; default: builder.append("^(") .append(escaped) .append(")*|(") .append(escaped) .append(")*$"); break; } return SqlLiteral.createCharString(builder.toString(), call.getParserPosition()); }
Example 9
Source File: FlinkCalciteSqlValidator.java From flink with Apache License 2.0 | 5 votes |
@Override public void validateLiteral(SqlLiteral literal) { if (literal.getTypeName() == DECIMAL) { final BigDecimal decimal = literal.getValueAs(BigDecimal.class); if (decimal.precision() > DecimalType.MAX_PRECISION) { throw newValidationError( literal, Static.RESOURCE.numberLiteralOutOfRange(decimal.toString())); } } super.validateLiteral(literal); }
Example 10
Source File: BigQuerySqlDialect.java From calcite with Apache License 2.0 | 5 votes |
/** * For usage of TRIM, LTRIM and RTRIM in BQ see * <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#trim"> * BQ Trim Function</a>. */ private void unparseTrim(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { final String operatorName; SqlLiteral trimFlag = call.operand(0); SqlLiteral valueToTrim = call.operand(1); switch (trimFlag.getValueAs(SqlTrimFunction.Flag.class)) { case LEADING: operatorName = "LTRIM"; break; case TRAILING: operatorName = "RTRIM"; break; default: operatorName = call.getOperator().getName(); break; } final SqlWriter.Frame trimFrame = writer.startFunCall(operatorName); call.operand(2).unparse(writer, leftPrec, rightPrec); // If the trimmed character is a non-space character, add it to the target SQL. // eg: TRIM(BOTH 'A' from 'ABCD' // Output Query: TRIM('ABC', 'A') if (!valueToTrim.toValue().matches("\\s+")) { writer.literal(","); call.operand(1).unparse(writer, leftPrec, rightPrec); } writer.endFunCall(trimFrame); }
Example 11
Source File: SparkSqlDialect.java From calcite with Apache License 2.0 | 5 votes |
@Override public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) { SqlUtil.unparseFunctionSyntax(SPARKSQL_SUBSTRING, writer, call); } else { switch (call.getKind()) { case FLOOR: if (call.operandCount() != 2) { super.unparseCall(writer, call, leftPrec, rightPrec); return; } final SqlLiteral timeUnitNode = call.operand(1); final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class); SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()); SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false); break; case TRIM: unparseHiveTrim(writer, call, leftPrec, rightPrec); break; default: super.unparseCall(writer, call, leftPrec, rightPrec); } } }
Example 12
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 13
Source File: SqlJsonValueFunction.java From Bats with Apache License 2.0 | 4 votes |
private boolean isDefaultLiteral(SqlLiteral literal) { return literal.getValueAs(SqlJsonValueEmptyOrErrorBehavior.class) == SqlJsonValueEmptyOrErrorBehavior.DEFAULT; }
Example 14
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 15
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 16
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()); } }