Search in sources :

Example 1 with SqlIntervalDayTime

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));
}
Also used : SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 2 with SqlIntervalDayTime

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));
}
Also used : SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 3 with SqlIntervalDayTime

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();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 4 with SqlIntervalDayTime

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");
}
Also used : SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) Test(org.testng.annotations.Test)

Example 5 with SqlIntervalDayTime

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);
    }
}
Also used : Iterables.transform(com.google.common.collect.Iterables.transform) TestingPrestoServer(com.facebook.presto.server.testing.TestingPrestoServer) ZonedDateTime(java.time.ZonedDateTime) BigintEnumType(com.facebook.presto.common.type.BigintEnumType) QueryData(com.facebook.presto.client.QueryData) JsonType(com.facebook.presto.common.type.JsonType) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) DEFAULT_PRECISION(com.facebook.presto.testing.MaterializedResult.DEFAULT_PRECISION) Map(java.util.Map) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) VarcharEnumType(com.facebook.presto.common.type.VarcharEnumType) OffsetTime(java.time.OffsetTime) TIME(com.facebook.presto.common.type.TimeType.TIME) Function(com.google.common.base.Function) IntervalYearMonth(com.facebook.presto.client.IntervalYearMonth) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Set(java.util.Set) VarcharType(com.facebook.presto.common.type.VarcharType) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) LocalDate(java.time.LocalDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) Optional(java.util.Optional) TIME_WITH_TIME_ZONE(com.facebook.presto.common.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) INTERVAL_YEAR_MONTH(com.facebook.presto.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) TIMESTAMP_WITH_TIME_ZONE(com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) TINYINT(com.facebook.presto.common.type.TinyintType.TINYINT) LocalDateTime(java.time.LocalDateTime) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) AtomicReference(java.util.concurrent.atomic.AtomicReference) DATE(com.facebook.presto.common.type.DateType.DATE) REAL(com.facebook.presto.common.type.RealType.REAL) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) ImmutableList(com.google.common.collect.ImmutableList) INTERVAL_DAY_TIME(com.facebook.presto.type.IntervalDayTimeType.INTERVAL_DAY_TIME) PrestoWarning(com.facebook.presto.spi.PrestoWarning) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) TypeWithName(com.facebook.presto.common.type.TypeWithName) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Session(com.facebook.presto.Session) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) QueryStatusInfo(com.facebook.presto.client.QueryStatusInfo) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Collectors.toList(java.util.stream.Collectors.toList) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) MaterializedRow(com.facebook.presto.testing.MaterializedRow) DateTimeFormatter(java.time.format.DateTimeFormatter) IntervalDayTime(com.facebook.presto.client.IntervalDayTime) IPADDRESS(com.facebook.presto.type.IpAddressType.IPADDRESS) RowType(com.facebook.presto.common.type.RowType) JsonType(com.facebook.presto.common.type.JsonType) TypeWithName(com.facebook.presto.common.type.TypeWithName) VarcharType(com.facebook.presto.common.type.VarcharType) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) RowType(com.facebook.presto.common.type.RowType) LocalDate(java.time.LocalDate) MapType(com.facebook.presto.common.type.MapType) BigDecimal(java.math.BigDecimal) ArrayType(com.facebook.presto.common.type.ArrayType) DateTimeParseException(java.time.format.DateTimeParseException) ZonedDateTime(java.time.ZonedDateTime) VarcharEnumType(com.facebook.presto.common.type.VarcharEnumType) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) DecimalType(com.facebook.presto.common.type.DecimalType) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) BigintEnumType(com.facebook.presto.common.type.BigintEnumType) Map(java.util.Map)

Aggregations

SqlIntervalDayTime (com.facebook.presto.type.SqlIntervalDayTime)5 MaterializedResult (com.facebook.presto.testing.MaterializedResult)3 SqlIntervalYearMonth (com.facebook.presto.type.SqlIntervalYearMonth)3 Test (org.testng.annotations.Test)3 DecimalType (com.facebook.presto.common.type.DecimalType)2 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)2 Type (com.facebook.presto.common.type.Type)2 VarcharType (com.facebook.presto.common.type.VarcharType)2 Session (com.facebook.presto.Session)1 IntervalDayTime (com.facebook.presto.client.IntervalDayTime)1 IntervalYearMonth (com.facebook.presto.client.IntervalYearMonth)1 QueryData (com.facebook.presto.client.QueryData)1 QueryStatusInfo (com.facebook.presto.client.QueryStatusInfo)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 BigintEnumType (com.facebook.presto.common.type.BigintEnumType)1 BigintType (com.facebook.presto.common.type.BigintType)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 BOOLEAN (com.facebook.presto.common.type.BooleanType.BOOLEAN)1