Search in sources :

Example 1 with SqlTimeWithTimeZone

use of io.prestosql.spi.type.SqlTimeWithTimeZone in project hetu-core by openlookeng.

the class TestTimestampWithTimeZone method testCastToTimeWithTimeZone.

@Test
public void testCastToTimeWithTimeZone() {
    assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321 +07:09' as time with time zone)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    functionAssertions.assertFunctionString("cast(TIMESTAMP '2001-1-22 03:04:05.321 +07:09' as time with time zone)", TIME_WITH_TIME_ZONE, "03:04:05.321 +07:09");
}
Also used : SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 2 with SqlTimeWithTimeZone

use of io.prestosql.spi.type.SqlTimeWithTimeZone in project hetu-core by openlookeng.

the class TestDateTimeOperators method testTimeMinusInterval.

@Test
public void testTimeMinusInterval() {
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' hour", TIME, sqlTimeOf(0, 4, 5, 321));
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' day", TIME, sqlTimeOf(3, 4, 5, 321));
    assertFunction("TIME '03:04:05.321' - INTERVAL '6' hour", TIME, sqlTimeOf(21, 4, 5, 321));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 0, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' day", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '6' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 21, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
}
Also used : SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 3 with SqlTimeWithTimeZone

use of io.prestosql.spi.type.SqlTimeWithTimeZone in project hetu-core by openlookeng.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillis();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlTime(io.prestosql.spi.type.SqlTime) SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) MapType(io.prestosql.spi.type.MapType) VarcharType(io.prestosql.spi.type.VarcharType) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) SqlDate(io.prestosql.spi.type.SqlDate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(io.prestosql.spi.type.CharType) TimeZoneKey(io.prestosql.spi.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 4 with SqlTimeWithTimeZone

use of io.prestosql.spi.type.SqlTimeWithTimeZone in project hetu-core by openlookeng.

the class MaterializedResult method convertToTestTypes.

private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) {
    List<Object> convertedValues = new ArrayList<>();
    for (int field = 0; field < prestoRow.getFieldCount(); field++) {
        Object prestoValue = prestoRow.getField(field);
        Object convertedValue;
        if (prestoValue instanceof SqlDate) {
            convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays());
        } else if (prestoValue instanceof SqlTime) {
            convertedValue = DateTimeFormatter.ISO_LOCAL_TIME.parse(prestoValue.toString(), LocalTime::from);
        } else if (prestoValue instanceof SqlTimeWithTimeZone) {
            // Political timezone cannot be represented in OffsetTime and there isn't any better representation.
            long millisUtc = ((SqlTimeWithTimeZone) prestoValue).getMillisUtc();
            ZoneOffset zone = toZoneOffset(((SqlTimeWithTimeZone) prestoValue).getTimeZoneKey());
            convertedValue = OffsetTime.of(LocalTime.ofNanoOfDay(MILLISECONDS.toNanos(millisUtc) + SECONDS.toNanos(zone.getTotalSeconds())), zone);
        } else if (prestoValue instanceof SqlTimestamp) {
            convertedValue = SqlTimestamp.JSON_FORMATTER.parse(prestoValue.toString(), LocalDateTime::from);
        } else if (prestoValue instanceof SqlTimestampWithTimeZone) {
            convertedValue = Instant.ofEpochMilli(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc()).atZone(ZoneId.of(((SqlTimestampWithTimeZone) prestoValue).getTimeZoneKey().getId()));
        } else if (prestoValue instanceof SqlDecimal) {
            convertedValue = ((SqlDecimal) prestoValue).toBigDecimal();
        } else {
            convertedValue = prestoValue;
        }
        convertedValues.add(convertedValue);
    }
    return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) SqlTime(io.prestosql.spi.type.SqlTime) SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) SqlDecimal(io.prestosql.spi.type.SqlDecimal) ZoneOffset(java.time.ZoneOffset) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) SqlDate(io.prestosql.spi.type.SqlDate)

Example 5 with SqlTimeWithTimeZone

use of io.prestosql.spi.type.SqlTimeWithTimeZone in project hetu-core by openlookeng.

the class TestDateTimeOperators method testTimePlusInterval.

@Test
public void testTimePlusInterval() {
    assertFunction("TIME '03:04:05.321' + INTERVAL '3' hour", TIME, sqlTimeOf(6, 4, 5, 321));
    assertFunction("INTERVAL '3' hour + TIME '03:04:05.321'", TIME, sqlTimeOf(6, 4, 5, 321));
    assertFunction("TIME '03:04:05.321' + INTERVAL '3' day", TIME, sqlTimeOf(3, 4, 5, 321));
    assertFunction("INTERVAL '3' day + TIME '03:04:05.321'", TIME, sqlTimeOf(3, 4, 5, 321));
    assertFunction("TIME '03:04:05.321' + INTERVAL '27' hour", TIME, sqlTimeOf(6, 4, 5, 321));
    assertFunction("INTERVAL '27' hour + TIME '03:04:05.321'", TIME, sqlTimeOf(6, 4, 5, 321));
    assertFunction("TIME '03:04:05.321 +05:09' + INTERVAL '3' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 6, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("INTERVAL '3' hour + TIME '03:04:05.321 +05:09'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 6, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' + INTERVAL '3' day", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("INTERVAL '3' day + TIME '03:04:05.321 +05:09'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' + INTERVAL '27' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 6, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("INTERVAL '27' hour + TIME '03:04:05.321 +05:09'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 6, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
}
Also used : SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

SqlTimeWithTimeZone (io.prestosql.spi.type.SqlTimeWithTimeZone)9 DateTime (org.joda.time.DateTime)7 Test (org.testng.annotations.Test)7 SqlDate (io.prestosql.spi.type.SqlDate)2 SqlTime (io.prestosql.spi.type.SqlTime)2 SqlTimestampWithTimeZone (io.prestosql.spi.type.SqlTimestampWithTimeZone)2 TimeZoneKey (io.prestosql.spi.type.TimeZoneKey)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Session (io.prestosql.Session)1 FunctionAssertions (io.prestosql.operator.scalar.FunctionAssertions)1 BlockBuilder (io.prestosql.spi.block.BlockBuilder)1 ArrayType (io.prestosql.spi.type.ArrayType)1 CharType (io.prestosql.spi.type.CharType)1 MapType (io.prestosql.spi.type.MapType)1 RowType (io.prestosql.spi.type.RowType)1 SqlDecimal (io.prestosql.spi.type.SqlDecimal)1