use of com.facebook.presto.type.SqlIntervalDayTime in project presto by prestodb.
the class AbstractTestQueries method selectLargeInterval.
@Test
public void selectLargeInterval() {
MaterializedResult result = computeActual("SELECT INTERVAL '30' DAY");
assertEquals(result.getRowCount(), 1);
assertEquals(result.getMaterializedRows().get(0).getField(0), new SqlIntervalDayTime(30, 0, 0, 0, 0));
result = computeActual("SELECT INTERVAL '" + Short.MAX_VALUE + "' YEAR");
assertEquals(result.getRowCount(), 1);
assertEquals(result.getMaterializedRows().get(0).getField(0), new SqlIntervalYearMonth(Short.MAX_VALUE, 0));
}
use of com.facebook.presto.type.SqlIntervalDayTime in project presto by prestodb.
the class AbstractTestQueries method testSelectLargeInterval.
@Test
public void testSelectLargeInterval() {
MaterializedResult result = computeActual("SELECT INTERVAL '30' DAY");
assertEquals(result.getRowCount(), 1);
assertEquals(result.getMaterializedRows().get(0).getField(0), new SqlIntervalDayTime(30, 0, 0, 0, 0));
result = computeActual("SELECT INTERVAL '" + Short.MAX_VALUE + "' YEAR");
assertEquals(result.getRowCount(), 1);
assertEquals(result.getMaterializedRows().get(0).getField(0), new SqlIntervalYearMonth(Short.MAX_VALUE, 0));
}
use of com.facebook.presto.type.SqlIntervalDayTime in project presto by prestodb.
the class LiteralInterpreter method evaluate.
public static Object evaluate(ConnectorSession session, ConstantExpression node) {
Type type = node.getType();
SqlFunctionProperties properties = session.getSqlFunctionProperties();
if (node.getValue() == null) {
return null;
}
if (type instanceof BooleanType) {
return node.getValue();
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return node.getValue();
}
if (type instanceof DoubleType) {
return node.getValue();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return intBitsToFloat(number.intValue());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(node.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType);
}
if (type instanceof VarcharType || type instanceof CharType) {
return ((Slice) node.getValue()).toStringUtf8();
}
if (type instanceof VarbinaryType) {
return new SqlVarbinary(((Slice) node.getValue()).getBytes());
}
if (type instanceof DateType) {
return new SqlDate(((Long) node.getValue()).intValue());
}
if (type instanceof TimeType) {
if (properties.isLegacyTimestamp()) {
return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTime((long) node.getValue());
}
if (type instanceof TimestampType) {
try {
if (properties.isLegacyTimestamp()) {
return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
}
return new SqlTimestamp((long) node.getValue());
} catch (RuntimeException e) {
throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
}
}
if (type instanceof IntervalDayTimeType) {
return new SqlIntervalDayTime((long) node.getValue());
}
if (type instanceof IntervalYearMonthType) {
return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
}
if (type.getJavaType().equals(Slice.class)) {
// DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
}
// We should not fail at the moment; just return the raw value (block, regex, etc) to the user
return node.getValue();
}
use of com.facebook.presto.type.SqlIntervalDayTime in project presto by prestodb.
the class TestDateTimeFunctionsBase method testParseDuration.
@Test
public void testParseDuration() {
assertFunction("parse_duration('1234 ns')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 0));
assertFunction("parse_duration('1234 us')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 1));
assertFunction("parse_duration('1234 ms')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1, 234));
assertFunction("parse_duration('1234 s')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 20, 34, 0));
assertFunction("parse_duration('1234 m')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 20, 34, 0, 0));
assertFunction("parse_duration('1234 h')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(51, 10, 0, 0, 0));
assertFunction("parse_duration('1234 d')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(1234, 0, 0, 0, 0));
assertFunction("parse_duration('1234.567 ns')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 0));
assertFunction("parse_duration('1234.567 ms')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1, 235));
assertFunction("parse_duration('1234.567 s')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1234, 567));
assertFunction("parse_duration('1234.567 m')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 20, 34, 34, 20));
assertFunction("parse_duration('1234.567 h')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(51, 10, 34, 1, 200));
assertFunction("parse_duration('1234.567 d')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(1234, 13, 36, 28, 800));
// without space
assertFunction("parse_duration('1234ns')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 0));
assertFunction("parse_duration('1234us')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 1));
assertFunction("parse_duration('1234ms')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1, 234));
assertFunction("parse_duration('1234s')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 20, 34, 0));
assertFunction("parse_duration('1234m')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 20, 34, 0, 0));
assertFunction("parse_duration('1234h')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(51, 10, 0, 0, 0));
assertFunction("parse_duration('1234d')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(1234, 0, 0, 0, 0));
assertFunction("parse_duration('1234.567ns')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 0, 0));
assertFunction("parse_duration('1234.567ms')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1, 235));
assertFunction("parse_duration('1234.567s')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 0, 0, 1234, 567));
assertFunction("parse_duration('1234.567m')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(0, 20, 34, 34, 20));
assertFunction("parse_duration('1234.567h')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(51, 10, 34, 1, 200));
assertFunction("parse_duration('1234.567d')", INTERVAL_DAY_TIME, new SqlIntervalDayTime(1234, 13, 36, 28, 800));
// invalid function calls
assertInvalidFunction("parse_duration('')", "duration is empty");
assertInvalidFunction("parse_duration('1f')", "Unknown time unit: f");
assertInvalidFunction("parse_duration('abc')", "duration is not a valid data duration string: abc");
}
use of com.facebook.presto.type.SqlIntervalDayTime in project presto by prestodb.
the class TestingPrestoClient method convertToRowValue.
private static Object convertToRowValue(Type type, Object value) {
if (value == null) {
return null;
}
if (BOOLEAN.equals(type)) {
return value;
} else if (TINYINT.equals(type)) {
return ((Number) value).byteValue();
} else if (SMALLINT.equals(type)) {
return ((Number) value).shortValue();
} else if (INTEGER.equals(type)) {
return ((Number) value).intValue();
} else if (BIGINT.equals(type)) {
return ((Number) value).longValue();
} else if (DOUBLE.equals(type)) {
return ((Number) value).doubleValue();
} else if (REAL.equals(type)) {
return ((Number) value).floatValue();
} else if (type instanceof VarcharType) {
return value;
} else if (isCharType(type)) {
return value;
} else if (VARBINARY.equals(type)) {
return value;
} else if (DATE.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(((String) value), LocalDate::from);
} else if (TIME.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_TIME.parse(((String) value), LocalTime::from);
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
// Only zone-offset timezones are supported (TODO remove political timezones support for TIME WITH TIME ZONE)
try {
return timeWithUtcZoneFormat.parse(((String) value), LocalTime::from).atOffset(ZoneOffset.UTC);
} catch (DateTimeParseException e) {
return timeWithZoneOffsetFormat.parse(((String) value), OffsetTime::from);
}
} else if (TIMESTAMP.equals(type)) {
return SqlTimestamp.JSON_FORMATTER.parse((String) value, LocalDateTime::from);
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
return timestampWithTimeZoneFormat.parse((String) value, ZonedDateTime::from);
} else if (INTERVAL_DAY_TIME.equals(type)) {
return new SqlIntervalDayTime(IntervalDayTime.parseMillis(String.valueOf(value)));
} else if (INTERVAL_YEAR_MONTH.equals(type)) {
return new SqlIntervalYearMonth(IntervalYearMonth.parseMonths(String.valueOf(value)));
} else if (IPADDRESS.equals(type)) {
return value;
} else if (type instanceof ArrayType) {
return ((List<Object>) value).stream().map(element -> convertToRowValue(((ArrayType) type).getElementType(), element)).collect(toList());
} else if (type instanceof MapType) {
return ((Map<Object, Object>) value).entrySet().stream().collect(Collectors.toMap(e -> convertToRowValue(((MapType) type).getKeyType(), e.getKey()), e -> convertToRowValue(((MapType) type).getValueType(), e.getValue())));
} else if (type instanceof RowType) {
Map<String, Object> data = (Map<String, Object>) value;
RowType rowType = (RowType) type;
return rowType.getFields().stream().map(field -> convertToRowValue(field.getType(), data.get(field.getName().get()))).collect(toList());
} else if (type instanceof DecimalType) {
return new BigDecimal((String) value);
} else if (type instanceof JsonType) {
return value;
} else if (type instanceof VarcharEnumType) {
return value;
} else if (type instanceof BigintEnumType) {
return ((Number) value).longValue();
} else if (type instanceof TypeWithName) {
return convertToRowValue(((TypeWithName) type).getType(), value);
} else if (type.getTypeSignature().getBase().equals("ObjectId")) {
return value;
} else {
throw new AssertionError("unhandled type: " + type);
}
}
Aggregations