use of org.apache.calcite.util.TimeString in project calcite by apache.
the class SqlParserUtil method parseTimeLiteral.
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
final String dateStr = parseString(s);
final DateTimeUtils.PrecisionTime pt = DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr, Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
if (pt == null) {
throw SqlUtil.newContextException(pos, RESOURCE.illegalLiteral("TIME", s, RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
}
final TimeString t = TimeString.fromCalendarFields(pt.getCalendar()).withFraction(pt.getFraction());
return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
use of org.apache.calcite.util.TimeString in project calcite by apache.
the class RexProgramTest method testSimplifyCastLiteral.
@Test
public void testSimplifyCastLiteral() {
final List<RexLiteral> literals = new ArrayList<>();
literals.add(rexBuilder.makeExactLiteral(BigDecimal.ONE, typeFactory.createSqlType(SqlTypeName.INTEGER)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(2), typeFactory.createSqlType(SqlTypeName.BIGINT)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(3), typeFactory.createSqlType(SqlTypeName.SMALLINT)));
literals.add(rexBuilder.makeExactLiteral(BigDecimal.valueOf(4), typeFactory.createSqlType(SqlTypeName.TINYINT)));
literals.add(rexBuilder.makeExactLiteral(new BigDecimal("1234"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 4, 0)));
literals.add(rexBuilder.makeExactLiteral(new BigDecimal("123.45"), typeFactory.createSqlType(SqlTypeName.DECIMAL, 5, 2)));
literals.add(rexBuilder.makeApproxLiteral(new BigDecimal("3.1415"), typeFactory.createSqlType(SqlTypeName.REAL)));
literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.E), typeFactory.createSqlType(SqlTypeName.FLOAT)));
literals.add(rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Math.PI), typeFactory.createSqlType(SqlTypeName.DOUBLE)));
literals.add(rexBuilder.makeLiteral(true));
literals.add(rexBuilder.makeLiteral(false));
literals.add(rexBuilder.makeLiteral("hello world"));
literals.add(rexBuilder.makeLiteral("1969-07-20 12:34:56"));
literals.add(rexBuilder.makeLiteral("1969-07-20"));
literals.add(rexBuilder.makeLiteral("12:34:45"));
literals.add((RexLiteral) rexBuilder.makeLiteral(new ByteString(new byte[] { 1, 2, -34, 0, -128 }), typeFactory.createSqlType(SqlTypeName.BINARY, 5), false));
literals.add(rexBuilder.makeDateLiteral(new DateString(1974, 8, 9)));
literals.add(rexBuilder.makeTimeLiteral(new TimeString(1, 23, 45), 0));
literals.add(rexBuilder.makeTimestampLiteral(new TimestampString(1974, 8, 9, 1, 23, 45), 0));
final Multimap<SqlTypeName, RexLiteral> map = LinkedHashMultimap.create();
for (RexLiteral literal : literals) {
map.put(literal.getTypeName(), literal);
}
final List<RelDataType> types = new ArrayList<>();
types.add(typeFactory.createSqlType(SqlTypeName.INTEGER));
types.add(typeFactory.createSqlType(SqlTypeName.BIGINT));
types.add(typeFactory.createSqlType(SqlTypeName.SMALLINT));
types.add(typeFactory.createSqlType(SqlTypeName.TINYINT));
types.add(typeFactory.createSqlType(SqlTypeName.REAL));
types.add(typeFactory.createSqlType(SqlTypeName.FLOAT));
types.add(typeFactory.createSqlType(SqlTypeName.DOUBLE));
types.add(typeFactory.createSqlType(SqlTypeName.BOOLEAN));
types.add(typeFactory.createSqlType(SqlTypeName.VARCHAR, 10));
types.add(typeFactory.createSqlType(SqlTypeName.CHAR, 5));
types.add(typeFactory.createSqlType(SqlTypeName.VARBINARY, 60));
types.add(typeFactory.createSqlType(SqlTypeName.BINARY, 3));
types.add(typeFactory.createSqlType(SqlTypeName.TIMESTAMP));
types.add(typeFactory.createSqlType(SqlTypeName.TIME));
types.add(typeFactory.createSqlType(SqlTypeName.DATE));
for (RelDataType fromType : types) {
for (RelDataType toType : types) {
if (SqlTypeAssignmentRules.instance(false).canCastFrom(toType.getSqlTypeName(), fromType.getSqlTypeName())) {
for (RexLiteral literal : map.get(fromType.getSqlTypeName())) {
final RexNode cast = rexBuilder.makeCast(toType, literal);
if (cast instanceof RexLiteral) {
assertThat(cast.getType(), is(toType));
// makeCast already simplified
continue;
}
final RexNode simplified = simplify.simplify(cast);
boolean expectedSimplify = literal.getTypeName() != toType.getSqlTypeName() || (literal.getTypeName() == SqlTypeName.CHAR && ((NlsString) literal.getValue()).getValue().length() > toType.getPrecision()) || (literal.getTypeName() == SqlTypeName.BINARY && ((ByteString) literal.getValue()).length() > toType.getPrecision());
boolean couldSimplify = !cast.equals(simplified);
final String reason = (expectedSimplify ? "expected to simplify, but could not: " : "simplified, but did not expect to: ") + cast + " --> " + simplified;
assertThat(reason, couldSimplify, is(expectedSimplify));
}
}
}
}
}
use of org.apache.calcite.util.TimeString in project calcite by apache.
the class RexBuilder method makeLiteral.
/**
* Internal method to create a call to a literal. Code outside this package
* should call one of the type-specific methods such as
* {@link #makeDateLiteral(DateString)}, {@link #makeLiteral(boolean)},
* {@link #makeLiteral(String)}.
*
* @param o Value of literal, must be appropriate for the type
* @param type Type of literal
* @param typeName SQL type of literal
* @return Literal
*/
protected RexLiteral makeLiteral(Comparable o, RelDataType type, SqlTypeName typeName) {
// All literals except NULL have NOT NULL types.
type = typeFactory.createTypeWithNullability(type, o == null);
int p;
switch(typeName) {
case CHAR:
// from the type if necessary.
assert o instanceof NlsString;
NlsString nlsString = (NlsString) o;
if ((nlsString.getCollation() == null) || (nlsString.getCharset() == null)) {
assert type.getSqlTypeName() == SqlTypeName.CHAR;
assert type.getCharset().name() != null;
assert type.getCollation() != null;
o = new NlsString(nlsString.getValue(), type.getCharset().name(), type.getCollation());
}
break;
case TIME:
assert o instanceof TimeString;
p = type.getPrecision();
if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
p = 0;
}
o = ((TimeString) o).round(p);
break;
case TIME_WITH_LOCAL_TIME_ZONE:
assert o instanceof TimeString;
p = type.getPrecision();
if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
p = 0;
}
o = ((TimeString) o).round(p);
break;
case TIMESTAMP:
assert o instanceof TimestampString;
p = type.getPrecision();
if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
p = 0;
}
o = ((TimestampString) o).round(p);
break;
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
assert o instanceof TimestampString;
p = type.getPrecision();
if (p == RelDataType.PRECISION_NOT_SPECIFIED) {
p = 0;
}
o = ((TimestampString) o).round(p);
}
return new RexLiteral(o, type, typeName);
}
use of org.apache.calcite.util.TimeString in project calcite by apache.
the class RexBuilderTest method testTimeStringMinuteError.
/**
* Tests {@link TimeString} minute range.
*/
@Test
public void testTimeStringMinuteError() {
try {
final TimeString timeString = new TimeString(12, 334, 56);
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Minute out of range: [334]"));
}
try {
final TimeString timeString = new TimeString("12:60:23");
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Minute out of range: [60]"));
}
}
use of org.apache.calcite.util.TimeString in project flink by apache.
the class RexNodeJsonSerdeTest method testRexNodeSerde.
// --------------------------------------------------------------------------------------------
// Test data
// --------------------------------------------------------------------------------------------
@SuppressWarnings("UnstableApiUsage")
private static Stream<RexNode> testRexNodeSerde() {
final RexBuilder rexBuilder = new RexBuilder(FACTORY);
final RelDataType inputType = FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.INTEGER), FACTORY.createSqlType(SqlTypeName.BIGINT), FACTORY.createStructType(StructKind.PEEK_FIELDS_NO_EXPAND, Arrays.asList(FACTORY.createSqlType(SqlTypeName.VARCHAR), FACTORY.createSqlType(SqlTypeName.VARCHAR)), Arrays.asList("n1", "n2"))), Arrays.asList("f1", "f2", "f3"));
return Stream.of(rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.VARCHAR)), rexBuilder.makeLiteral(true), rexBuilder.makeExactLiteral(new BigDecimal(Byte.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.TINYINT)), rexBuilder.makeExactLiteral(new BigDecimal(Short.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.SMALLINT)), rexBuilder.makeExactLiteral(new BigDecimal(Integer.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeExactLiteral(new BigDecimal(Long.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.BIGINT)), rexBuilder.makeExactLiteral(BigDecimal.valueOf(Double.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.DOUBLE)), rexBuilder.makeApproxLiteral(BigDecimal.valueOf(Float.MAX_VALUE), FACTORY.createSqlType(SqlTypeName.FLOAT)), rexBuilder.makeExactLiteral(new BigDecimal("23.1234567890123456789012345678")), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(100), new SqlIntervalQualifier(TimeUnit.YEAR, 4, TimeUnit.YEAR, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.YEAR, 2, TimeUnit.MONTH, RelDataType.PRECISION_NOT_SPECIFIED, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.DAY, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeIntervalLiteral(BigDecimal.valueOf(3), new SqlIntervalQualifier(TimeUnit.SECOND, 2, TimeUnit.SECOND, 6, SqlParserPos.ZERO)), rexBuilder.makeDateLiteral(DateString.fromDaysSinceEpoch(10)), rexBuilder.makeDateLiteral(new DateString("2000-12-12")), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(1234), 3), rexBuilder.makeTimeLiteral(TimeString.fromMillisOfDay(123456), 6), rexBuilder.makeTimeLiteral(new TimeString("01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(1234), 3), rexBuilder.makeTimestampLiteral(TimestampString.fromMillisSinceEpoch(123456789), 9), rexBuilder.makeTimestampLiteral(new TimestampString("0001-01-01 01:01:01.000000001"), 9), rexBuilder.makeTimestampLiteral(new TimestampString("2000-12-12 12:30:57.1234"), 4), rexBuilder.makeBinaryLiteral(ByteString.EMPTY), rexBuilder.makeBinaryLiteral(ByteString.ofBase64("SGVsbG8gV29ybGQh")), rexBuilder.makeLiteral(""), rexBuilder.makeLiteral("abc"), rexBuilder.makeFlag(SqlTrimFunction.Flag.BOTH), rexBuilder.makeFlag(TimeUnitRange.DAY), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(10)))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, ImmutableRangeSet.of(Range.range(BigDecimal.valueOf(1), BoundType.OPEN, BigDecimal.valueOf(10), BoundType.CLOSED))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeSearchArgumentLiteral(Sarg.of(false, TreeRangeSet.create(Arrays.asList(Range.closed(BigDecimal.valueOf(1), BigDecimal.valueOf(1)), Range.closed(BigDecimal.valueOf(3), BigDecimal.valueOf(3)), Range.closed(BigDecimal.valueOf(6), BigDecimal.valueOf(6))))), FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.BIGINT), 0), rexBuilder.makeCorrel(inputType, new CorrelationId("$cor1")), rexBuilder.makeFieldAccess(rexBuilder.makeCorrel(inputType, new CorrelationId("$cor2")), "f2", true), // cast($1 as smallint)
rexBuilder.makeCast(FACTORY.createSqlType(SqlTypeName.SMALLINT), rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), // $1 in (1, 3, 5)
rexBuilder.makeIn(rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), Arrays.asList(rexBuilder.makeExactLiteral(new BigDecimal(1)), rexBuilder.makeExactLiteral(new BigDecimal(3)), rexBuilder.makeExactLiteral(new BigDecimal(5)))), // null or $1 is null
rexBuilder.makeCall(SqlStdOperatorTable.OR, rexBuilder.makeNullLiteral(FACTORY.createSqlType(SqlTypeName.INTEGER)), rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1))), // $1 >= 10
rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1), rexBuilder.makeExactLiteral(new BigDecimal(10))), // hash_code($1)
rexBuilder.makeCall(FlinkSqlOperatorTable.HASH_CODE, rexBuilder.makeInputRef(FACTORY.createSqlType(SqlTypeName.INTEGER), 1)), rexBuilder.makePatternFieldRef("test", FACTORY.createSqlType(SqlTypeName.INTEGER), 0));
}
Aggregations