Search in sources :

Example 6 with SqlTimestamp

use of com.facebook.presto.spi.type.SqlTimestamp in project presto by prestodb.

the class TestDateTimeOperators method testTimeZoneGap.

@Test
public void testTimeZoneGap() {
    assertFunction("TIMESTAMP '2013-03-31 00:05' + INTERVAL '1' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 1, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-03-31 00:05' + INTERVAL '2' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 3, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-03-31 00:05' + INTERVAL '3' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 4, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-03-31 04:05' - INTERVAL '3' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-03-31 03:05' - INTERVAL '2' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-03-31 01:05' - INTERVAL '1' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 3, 31, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
}
Also used : SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 7 with SqlTimestamp

use of com.facebook.presto.spi.type.SqlTimestamp in project presto by prestodb.

the class TestDateTimeOperators method testTimeZoneDuplicate.

@Test
public void testTimeZoneDuplicate() {
    assertFunction("TIMESTAMP '2013-10-27 00:05' + INTERVAL '1' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 1, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 00:05' + INTERVAL '2' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 2, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    // we need to manipulate millis directly here because 2 am has two representations in out time zone, and we need the second one
    assertFunction("TIMESTAMP '2013-10-27 00:05' + INTERVAL '3' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 0, 5, 0, 0, TIME_ZONE).getMillis() + HOURS.toMillis(3), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 00:05' + INTERVAL '4' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 3, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 03:05' - INTERVAL '4' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 02:05' - INTERVAL '2' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 01:05' - INTERVAL '1' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 0, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 03:05' - INTERVAL '1' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 0, 5, 0, 0, TIME_ZONE).getMillis() + HOURS.toMillis(3), TIME_ZONE_KEY));
    assertFunction("TIMESTAMP '2013-10-27 03:05' - INTERVAL '2' hour", TIMESTAMP, new SqlTimestamp(new DateTime(2013, 10, 27, 2, 5, 0, 0, TIME_ZONE).getMillis(), TIME_ZONE_KEY));
}
Also used : SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 8 with SqlTimestamp

use of com.facebook.presto.spi.type.SqlTimestamp in project presto by prestodb.

the class OrcTester method preprocessWriteValueOld.

private static Object preprocessWriteValueOld(TypeInfo typeInfo, Object value) {
    if (value == null) {
        return null;
    }
    switch(typeInfo.getCategory()) {
        case PRIMITIVE:
            PrimitiveObjectInspector.PrimitiveCategory primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
            switch(primitiveCategory) {
                case BOOLEAN:
                    return value;
                case BYTE:
                    return ((Number) value).byteValue();
                case SHORT:
                    return ((Number) value).shortValue();
                case INT:
                    return ((Number) value).intValue();
                case LONG:
                    return ((Number) value).longValue();
                case FLOAT:
                    return ((Number) value).floatValue();
                case DOUBLE:
                    return ((Number) value).doubleValue();
                case DECIMAL:
                    return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
                case STRING:
                    return value;
                case CHAR:
                    return new HiveChar(value.toString(), ((CharTypeInfo) typeInfo).getLength());
                case DATE:
                    int days = ((SqlDate) value).getDays();
                    LocalDate localDate = LocalDate.ofEpochDay(days);
                    ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
                    long millis = zonedDateTime.toEpochSecond() * 1000;
                    Date date = new Date(0);
                    // mills must be set separately to avoid masking
                    date.setTime(millis);
                    return date;
                case TIMESTAMP:
                    long millisUtc = (int) ((SqlTimestamp) value).getMillisUtc();
                    return new Timestamp(millisUtc);
                case BINARY:
                    return ((SqlVarbinary) value).getBytes();
            }
            break;
        case MAP:
            MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
            TypeInfo keyTypeInfo = mapTypeInfo.getMapKeyTypeInfo();
            TypeInfo valueTypeInfo = mapTypeInfo.getMapValueTypeInfo();
            Map<Object, Object> newMap = new HashMap<>();
            for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                newMap.put(preprocessWriteValueOld(keyTypeInfo, entry.getKey()), preprocessWriteValueOld(valueTypeInfo, entry.getValue()));
            }
            return newMap;
        case LIST:
            ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
            TypeInfo elementTypeInfo = listTypeInfo.getListElementTypeInfo();
            List<Object> newList = new ArrayList<>(((Collection<?>) value).size());
            for (Object element : (Iterable<?>) value) {
                newList.add(preprocessWriteValueOld(elementTypeInfo, element));
            }
            return newList;
        case STRUCT:
            StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
            List<?> fieldValues = (List<?>) value;
            List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
            List<Object> newStruct = new ArrayList<>();
            for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
                newStruct.add(preprocessWriteValueOld(fieldTypeInfos.get(fieldId), fieldValues.get(fieldId)));
            }
            return newStruct;
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo));
}
Also used : HashMap(java.util.HashMap) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) ArrayList(java.util.ArrayList) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrestoException(com.facebook.presto.spi.PrestoException) LocalDate(java.time.LocalDate) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) Timestamp(java.sql.Timestamp) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ZonedDateTime(java.time.ZonedDateTime) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) SqlDate(com.facebook.presto.spi.type.SqlDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) SqlDate(com.facebook.presto.spi.type.SqlDate) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 9 with SqlTimestamp

use of com.facebook.presto.spi.type.SqlTimestamp in project presto by prestodb.

the class TestingOrcPredicate method createOrcPredicate.

public static OrcPredicate createOrcPredicate(ObjectInspector objectInspector, Iterable<?> values) {
    List<Object> expectedValues = newArrayList(values);
    if (!(objectInspector instanceof PrimitiveObjectInspector)) {
        return new BasicOrcPredicate<>(expectedValues, Object.class);
    }
    PrimitiveObjectInspector primitiveObjectInspector = (PrimitiveObjectInspector) objectInspector;
    PrimitiveCategory primitiveCategory = primitiveObjectInspector.getPrimitiveCategory();
    switch(primitiveCategory) {
        case BOOLEAN:
            return new BooleanOrcPredicate(expectedValues);
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
            return new LongOrcPredicate(expectedValues.stream().map(value -> value == null ? null : ((Number) value).longValue()).collect(toList()));
        case TIMESTAMP:
            return new LongOrcPredicate(expectedValues.stream().map(value -> value == null ? null : ((SqlTimestamp) value).getMillisUtc()).collect(toList()));
        case DATE:
            return new DateOrcPredicate(expectedValues.stream().map(value -> value == null ? null : (long) ((SqlDate) value).getDays()).collect(toList()));
        case FLOAT:
        case DOUBLE:
            return new DoubleOrcPredicate(expectedValues.stream().map(value -> value == null ? null : ((Number) value).doubleValue()).collect(toList()));
        case BINARY:
            // binary does not have stats
            return new BasicOrcPredicate<>(expectedValues, Object.class);
        case STRING:
            return new StringOrcPredicate(expectedValues);
        case CHAR:
            return new CharOrcPredicate(expectedValues);
        case DECIMAL:
            return new DecimalOrcPredicate(expectedValues);
        default:
            throw new IllegalArgumentException("Unsupported types " + primitiveCategory);
    }
}
Also used : SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 10 with SqlTimestamp

use of com.facebook.presto.spi.type.SqlTimestamp in project presto by prestodb.

the class AbstractTestOrcReader method testRoundTripNumeric.

private void testRoundTripNumeric(Iterable<? extends Number> values) throws Exception {
    List<Long> writeValues = ImmutableList.copyOf(values).stream().map(Number::longValue).collect(toList());
    tester.testRoundTrip(javaByteObjectInspector, writeValues.stream().map(// truncate values to byte range
    value -> (long) value.byteValue()).collect(toList()), BIGINT);
    tester.testRoundTrip(javaShortObjectInspector, writeValues.stream().map(// truncate values to short range
    value -> (long) value.shortValue()).collect(toList()), BIGINT);
    tester.testRoundTrip(javaIntObjectInspector, writeValues, BIGINT);
    tester.testRoundTrip(javaLongObjectInspector, writeValues, BIGINT);
    tester.testRoundTrip(javaDateObjectInspector, writeValues.stream().map(Long::intValue).map(SqlDate::new).collect(toList()), DATE);
    tester.testRoundTrip(javaTimestampObjectInspector, writeValues.stream().map(timestamp -> new SqlTimestamp(timestamp, UTC_KEY)).collect(toList()), TIMESTAMP);
}
Also used : SqlDate(com.facebook.presto.spi.type.SqlDate) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp)

Aggregations

SqlTimestamp (com.facebook.presto.spi.type.SqlTimestamp)15 Test (org.testng.annotations.Test)11 DateTime (org.joda.time.DateTime)9 SqlDate (com.facebook.presto.spi.type.SqlDate)3 SqlTimestampWithTimeZone (com.facebook.presto.spi.type.SqlTimestampWithTimeZone)3 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)2 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)2 PrestoException (com.facebook.presto.spi.PrestoException)1 SqlDecimal (com.facebook.presto.spi.type.SqlDecimal)1 SqlTime (com.facebook.presto.spi.type.SqlTime)1 SqlTimeWithTimeZone (com.facebook.presto.spi.type.SqlTimeWithTimeZone)1 SqlVarbinary (com.facebook.presto.spi.type.SqlVarbinary)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Time (java.sql.Time)1 LocalDate (java.time.LocalDate)1 ZonedDateTime (java.time.ZonedDateTime)1