use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString in project beam by apache.
the class BeamSqlUnparseContext method toSql.
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
if (rex.getKind().equals(SqlKind.LITERAL)) {
final RexLiteral literal = (RexLiteral) rex;
SqlTypeName name = literal.getTypeName();
SqlTypeFamily family = name.getFamily();
if (SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE.equals(name)) {
TimestampString timestampString = literal.getValueAs(TimestampString.class);
return new SqlDateTimeLiteral(timestampString, POS);
} else if (SqlTypeFamily.BINARY.equals(family)) {
ByteString byteString = literal.getValueAs(ByteString.class);
BitString bitString = BitString.createFromHexString(byteString.toString(16));
return new SqlByteStringLiteral(bitString, POS);
} else if (SqlTypeFamily.CHARACTER.equals(family)) {
String escaped = ESCAPE_FOR_ZETA_SQL.translate(literal.getValueAs(String.class));
return SqlLiteral.createCharString(escaped, POS);
} else if (SqlTypeName.SYMBOL.equals(literal.getTypeName())) {
Enum symbol = literal.getValueAs(Enum.class);
if (TimeUnitRange.DOW.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "DAYOFWEEK");
} else if (TimeUnitRange.DOY.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "DAYOFYEAR");
} else if (TimeUnitRange.WEEK.equals(symbol)) {
return new ReplaceLiteral(literal, POS, "ISOWEEK");
}
}
} else if (rex.getKind().equals(SqlKind.DYNAMIC_PARAM)) {
final RexDynamicParam param = (RexDynamicParam) rex;
final int index = param.getIndex();
final String name = "null_param_" + index;
nullParams.put(name, param.getType());
return new NamedDynamicParam(index, POS, name);
} else if (SqlKind.SEARCH.equals(rex.getKind())) {
// Workaround CALCITE-4716
RexCall search = (RexCall) rex;
RexLocalRef ref = (RexLocalRef) search.operands.get(1);
RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
}
return super.toSql(program, rex);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString in project calcite by apache.
the class RexImplicationCheckerTest method testSimpleTimeStamp.
@Test
public void testSimpleTimeStamp() {
final Fixture f = new Fixture();
final TimestampString ts = TimestampString.fromCalendarFields(Util.calendar());
final RexNode node1 = f.lt(f.ts, f.timestampLiteral(ts));
final RexNode node2 = f.le(f.ts, f.timestampLiteral(ts));
f.checkImplies(node1, node2);
f.checkNotImplies(node2, node1);
final TimestampString tsBeforeEpoch1 = TimestampString.fromMillisSinceEpoch(-1234567890L);
final TimestampString tsBeforeEpoch2 = TimestampString.fromMillisSinceEpoch(-1234567L);
final RexNode nodeBe1 = f.lt(f.ts, f.timestampLiteral(tsBeforeEpoch1));
final RexNode nodeBe2 = f.lt(f.ts, f.timestampLiteral(tsBeforeEpoch2));
f.checkImplies(nodeBe1, nodeBe2);
f.checkNotImplies(nodeBe2, nodeBe1);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString in project calcite by apache.
the class RexProgramTest method testSimplifyCastLiteral3.
@Test
public void testSimplifyCastLiteral3() {
// Default TimeZone is "America/Los_Angeles" (DummyDataContext)
final RexLiteral literalDate = rexBuilder.makeDateLiteral(new DateString("2011-07-20"));
final RexLiteral literalTime = rexBuilder.makeTimeLiteral(new TimeString("12:34:56"), 0);
final RexLiteral literalTimestamp = rexBuilder.makeTimestampLiteral(new TimestampString("2011-07-20 12:34:56"), 0);
final RexLiteral literalTimeLTZ = rexBuilder.makeTimeWithLocalTimeZoneLiteral(new TimeString(1, 23, 45), 0);
final RexLiteral timeLTZChar1 = rexBuilder.makeLiteral("12:34:45 America/Los_Angeles");
final RexLiteral timeLTZChar2 = rexBuilder.makeLiteral("12:34:45 UTC");
final RexLiteral timeLTZChar3 = rexBuilder.makeLiteral("12:34:45 GMT+01");
final RexLiteral timestampLTZChar1 = rexBuilder.makeLiteral("2011-07-20 12:34:56 Asia/Tokyo");
final RexLiteral timestampLTZChar2 = rexBuilder.makeLiteral("2011-07-20 12:34:56 GMT+01");
final RexLiteral timestampLTZChar3 = rexBuilder.makeLiteral("2011-07-20 12:34:56 UTC");
final RexLiteral literalTimestampLTZ = rexBuilder.makeTimestampWithLocalTimeZoneLiteral(new TimestampString(2011, 7, 20, 8, 23, 45), 0);
final RelDataType dateType = typeFactory.createSqlType(SqlTypeName.DATE);
final RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
final RelDataType timeLTZType = typeFactory.createSqlType(SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE);
final RelDataType timestampLTZType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
final RelDataType varCharType = typeFactory.createSqlType(SqlTypeName.VARCHAR, 40);
checkSimplify(cast(timeLTZChar1, timeLTZType), "20:34:45");
checkSimplify(cast(timeLTZChar2, timeLTZType), "12:34:45");
checkSimplify(cast(timeLTZChar3, timeLTZType), "11:34:45");
checkSimplify(cast(literalTimeLTZ, timeLTZType), "01:23:45");
checkSimplify(cast(timestampLTZChar1, timestampLTZType), "2011-07-20 03:34:56");
checkSimplify(cast(timestampLTZChar2, timestampLTZType), "2011-07-20 11:34:56");
checkSimplify(cast(timestampLTZChar3, timestampLTZType), "2011-07-20 12:34:56");
checkSimplify(cast(literalTimestampLTZ, timestampLTZType), "2011-07-20 08:23:45");
checkSimplify(cast(literalDate, timestampLTZType), "2011-07-20 07:00:00");
checkSimplify(cast(literalTime, timestampLTZType), "2011-07-20 19:34:56");
checkSimplify(cast(literalTimestamp, timestampLTZType), "2011-07-20 19:34:56");
checkSimplify(cast(literalTimestamp, dateType), "2011-07-20");
checkSimplify(cast(literalTimestampLTZ, dateType), "2011-07-20");
checkSimplify(cast(literalTimestampLTZ, timeType), "01:23:45");
checkSimplify(cast(literalTimestampLTZ, timestampType), "2011-07-20 01:23:45");
checkSimplify(cast(literalTimeLTZ, timeType), "17:23:45");
checkSimplify(cast(literalTime, timeLTZType), "20:34:56");
checkSimplify(cast(literalTimestampLTZ, timeLTZType), "08:23:45");
checkSimplify(cast(literalTimeLTZ, varCharType), "'17:23:45 America/Los_Angeles'");
checkSimplify(cast(literalTimestampLTZ, varCharType), "'2011-07-20 01:23:45 America/Los_Angeles'");
checkSimplify(cast(literalTimeLTZ, timestampType), "2011-07-19 18:23:45");
checkSimplify(cast(literalTimeLTZ, timestampLTZType), "2011-07-20 01:23:45");
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString in project calcite by apache.
the class RexLiteral method printAsJava.
/**
* Prints a value as a Java string. The value must be consistent with the
* type, as per {@link #valueMatchesType}.
*
* <p>Typical return values:
*
* <ul>
* <li>true</li>
* <li>null</li>
* <li>"Hello, world!"</li>
* <li>1.25</li>
* <li>1234ABCD</li>
* </ul>
*
* @param value Value
* @param pw Writer to write to
* @param typeName Type family
*/
private static void printAsJava(Comparable value, PrintWriter pw, SqlTypeName typeName, boolean java) {
switch(typeName) {
case CHAR:
NlsString nlsString = (NlsString) value;
if (java) {
Util.printJavaString(pw, nlsString.getValue(), true);
} else {
boolean includeCharset = (nlsString.getCharsetName() != null) && !nlsString.getCharsetName().equals(SaffronProperties.INSTANCE.defaultCharset().get());
pw.print(nlsString.asSql(includeCharset, false));
}
break;
case BOOLEAN:
assert value instanceof Boolean;
pw.print(((Boolean) value).booleanValue());
break;
case DECIMAL:
assert value instanceof BigDecimal;
pw.print(value.toString());
break;
case DOUBLE:
assert value instanceof BigDecimal;
pw.print(Util.toScientificNotation((BigDecimal) value));
break;
case BIGINT:
assert value instanceof BigDecimal;
pw.print(((BigDecimal) value).longValue());
pw.print('L');
break;
case BINARY:
assert value instanceof ByteString;
pw.print("X'");
pw.print(((ByteString) value).toString(16));
pw.print("'");
break;
case NULL:
assert value == null;
pw.print("null");
break;
case SYMBOL:
assert value instanceof Enum;
pw.print("FLAG(");
pw.print(value);
pw.print(")");
break;
case DATE:
assert value instanceof DateString;
pw.print(value);
break;
case TIME:
assert value instanceof TimeString;
pw.print(value);
break;
case TIME_WITH_LOCAL_TIME_ZONE:
assert value instanceof TimeString;
pw.print(value);
break;
case TIMESTAMP:
assert value instanceof TimestampString;
pw.print(value);
break;
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
assert value instanceof TimestampString;
pw.print(value);
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 (value instanceof BigDecimal) {
pw.print(value.toString());
} else {
assert value == null;
pw.print("null");
}
break;
case MULTISET:
case ROW:
@SuppressWarnings("unchecked") final List<RexLiteral> list = (List) value;
pw.print(new AbstractList<String>() {
public String get(int index) {
return list.get(index).digest;
}
public int size() {
return list.size();
}
});
break;
default:
assert valueMatchesType(value, typeName, true);
throw Util.needToImplement(typeName);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.TimestampString in project calcite by apache.
the class RexBuilderTest method testTimestampLiteral.
/**
* Tests {@link RexBuilder#makeTimestampLiteral(TimestampString, int)}.
*/
@Test
public void testTimestampLiteral() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
final RelDataType timestampType3 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 3);
final RelDataType timestampType9 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 9);
final RelDataType timestampType18 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 18);
final RexBuilder builder = new RexBuilder(typeFactory);
// Old way: provide a Calendar
final Calendar calendar = Util.calendar();
// one small step
calendar.set(1969, Calendar.JULY, 21, 2, 56, 15);
calendar.set(Calendar.MILLISECOND, 0);
checkTimestamp(builder.makeLiteral(calendar, timestampType, false));
// Old way #2: Provide a Long
checkTimestamp(builder.makeLiteral(MOON, timestampType, false));
// The new way
final TimestampString ts = new TimestampString(1969, 7, 21, 2, 56, 15);
checkTimestamp(builder.makeLiteral(ts, timestampType, false));
// Now with milliseconds
final TimestampString ts2 = ts.withMillis(56);
assertThat(ts2.toString(), is("1969-07-21 02:56:15.056"));
final RexNode literal2 = builder.makeLiteral(ts2, timestampType3, false);
assertThat(((RexLiteral) literal2).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.056"));
// Now with nanoseconds
final TimestampString ts3 = ts.withNanos(56);
final RexNode literal3 = builder.makeLiteral(ts3, timestampType9, false);
assertThat(((RexLiteral) literal3).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15"));
final TimestampString ts3b = ts.withNanos(2345678);
final RexNode literal3b = builder.makeLiteral(ts3b, timestampType9, false);
assertThat(((RexLiteral) literal3b).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.002"));
// Now with a very long fraction
final TimestampString ts4 = ts.withFraction("102030405060708090102");
final RexNode literal4 = builder.makeLiteral(ts4, timestampType18, false);
assertThat(((RexLiteral) literal4).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.102"));
// toString
assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15"));
assertThat(ts2.round(2).toString(), is("1969-07-21 02:56:15.05"));
assertThat(ts2.round(3).toString(), is("1969-07-21 02:56:15.056"));
assertThat(ts2.round(4).toString(), is("1969-07-21 02:56:15.056"));
assertThat(ts2.toString(6), is("1969-07-21 02:56:15.056000"));
assertThat(ts2.toString(1), is("1969-07-21 02:56:15.0"));
assertThat(ts2.toString(0), is("1969-07-21 02:56:15"));
assertThat(ts2.round(0).toString(), is("1969-07-21 02:56:15"));
assertThat(ts2.round(0).toString(0), is("1969-07-21 02:56:15"));
assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0"));
assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00"));
assertThat(TimestampString.fromMillisSinceEpoch(1456513560123L).toString(), is("2016-02-26 19:06:00.123"));
}
Aggregations