use of org.apache.calcite.util.TimeString 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.calcite.util.TimeString in project calcite by apache.
the class RexBuilderTest method testTimeStringSecondError.
/**
* Tests {@link TimeString} second range.
*/
@Test
public void testTimeStringSecondError() {
try {
final TimeString timeString = new TimeString(12, 34, 567);
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Second out of range: [567]"));
}
try {
final TimeString timeString = new TimeString(12, 34, -4);
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Second out of range: [-4]"));
}
try {
final TimeString timeString = new TimeString("12:34:60");
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Second out of range: [60]"));
}
}
use of org.apache.calcite.util.TimeString in project calcite by apache.
the class RexBuilderTest method testTimeStringHourError.
/**
* Tests {@link TimeString} hour range.
*/
@Test
public void testTimeStringHourError() {
try {
final TimeString timeString = new TimeString(111, 34, 56);
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Hour out of range: [111]"));
}
try {
final TimeString timeString = new TimeString("24:00:00");
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Hour out of range: [24]"));
}
try {
final TimeString timeString = new TimeString("24:00");
fail("expected exception, got " + timeString);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Invalid time format: [24:00]"));
}
}
use of org.apache.calcite.util.TimeString in project calcite by apache.
the class RexBuilderTest method testTimeLiteral.
/**
* Tests {@link RexBuilder#makeTimeLiteral(TimeString, int)}.
*/
@Test
public void testTimeLiteral() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RelDataType timeType = typeFactory.createSqlType(SqlTypeName.TIME);
final RelDataType timeType3 = typeFactory.createSqlType(SqlTypeName.TIME, 3);
final RelDataType timeType9 = typeFactory.createSqlType(SqlTypeName.TIME, 9);
final RelDataType timeType18 = typeFactory.createSqlType(SqlTypeName.TIME, 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);
checkTime(builder.makeLiteral(calendar, timeType, false));
// Old way #2: Provide a Long
checkTime(builder.makeLiteral(MOON_TIME, timeType, false));
// The new way
final TimeString t = new TimeString(2, 56, 15);
assertThat(t.getMillisOfDay(), is(10575000));
checkTime(builder.makeLiteral(t, timeType, false));
// Now with milliseconds
final TimeString t2 = t.withMillis(56);
assertThat(t2.getMillisOfDay(), is(10575056));
assertThat(t2.toString(), is("02:56:15.056"));
final RexNode literal2 = builder.makeLiteral(t2, timeType3, false);
assertThat(((RexLiteral) literal2).getValueAs(TimeString.class).toString(), is("02:56:15.056"));
// Now with nanoseconds
final TimeString t3 = t.withNanos(2345678);
assertThat(t3.getMillisOfDay(), is(10575002));
final RexNode literal3 = builder.makeLiteral(t3, timeType9, false);
assertThat(((RexLiteral) literal3).getValueAs(TimeString.class).toString(), is("02:56:15.002"));
// Now with a very long fraction
final TimeString t4 = t.withFraction("102030405060708090102");
assertThat(t4.getMillisOfDay(), is(10575102));
final RexNode literal4 = builder.makeLiteral(t4, timeType18, false);
assertThat(((RexLiteral) literal4).getValueAs(TimeString.class).toString(), is("02:56:15.102"));
// toString
assertThat(t2.round(1).toString(), is("02:56:15"));
assertThat(t2.round(2).toString(), is("02:56:15.05"));
assertThat(t2.round(3).toString(), is("02:56:15.056"));
assertThat(t2.round(4).toString(), is("02:56:15.056"));
assertThat(t2.toString(6), is("02:56:15.056000"));
assertThat(t2.toString(1), is("02:56:15.0"));
assertThat(t2.toString(0), is("02:56:15"));
assertThat(t2.round(0).toString(), is("02:56:15"));
assertThat(t2.round(0).toString(0), is("02:56:15"));
assertThat(t2.round(0).toString(1), is("02:56:15.0"));
assertThat(t2.round(0).toString(2), is("02:56:15.00"));
assertThat(TimeString.fromMillisOfDay(53560123).toString(), is("14:52:40.123"));
}
use of org.apache.calcite.util.TimeString in project flink by apache.
the class RexNodeJsonDeserializer method deserializeLiteralValue.
@Nullable
private static Object deserializeLiteralValue(JsonNode literalNode, SqlTypeName sqlTypeName, SerdeContext serdeContext) {
final JsonNode valueNode = literalNode.required(FIELD_NAME_VALUE);
if (valueNode.isNull()) {
return null;
}
switch(sqlTypeName) {
case BOOLEAN:
return valueNode.booleanValue();
case TINYINT:
case SMALLINT:
case INTEGER:
case BIGINT:
case FLOAT:
case DOUBLE:
case DECIMAL:
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:
return new BigDecimal(valueNode.asText());
case DATE:
return new DateString(valueNode.asText());
case TIME:
return new TimeString(valueNode.asText());
case TIMESTAMP:
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
return new TimestampString(valueNode.asText());
case BINARY:
case VARBINARY:
return ByteString.ofBase64(valueNode.asText());
case CHAR:
case VARCHAR:
return serdeContext.getRexBuilder().makeLiteral(valueNode.asText()).getValue();
case SYMBOL:
final JsonNode symbolNode = literalNode.required(FIELD_NAME_SYMBOL);
final SerializableSymbol symbol = SerializableSymbol.of(symbolNode.asText(), valueNode.asText());
return serializableToCalcite(symbol);
default:
throw new TableException("Unknown literal: " + valueNode);
}
}
Aggregations