Search in sources :

Example 11 with SqlDate

use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.

the class TestTimestampWithTimeZoneBase method testCastToDate.

@Test
public void testCastToDate() {
    long millis = new DateTime(2001, 1, 22, 0, 0, UTC).getMillis();
    assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321 +07:09' as date)", DATE, new SqlDate((int) TimeUnit.MILLISECONDS.toDays(millis)));
}
Also used : SqlDate(com.facebook.presto.common.type.SqlDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 12 with SqlDate

use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.

the class TestDateBase method testGreatest.

@Test
public void testGreatest() {
    int days = (int) TimeUnit.MILLISECONDS.toDays(new DateTime(2013, 3, 30, 0, 0, UTC).getMillis());
    assertFunction("greatest(DATE '2013-03-30', DATE '2012-05-23')", DATE, new SqlDate(days));
    assertFunction("greatest(DATE '2013-03-30', DATE '2012-05-23', DATE '2012-06-01')", DATE, new SqlDate(days));
}
Also used : SqlDate(com.facebook.presto.common.type.SqlDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 13 with SqlDate

use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.

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(com.facebook.presto.common.type.SqlTime) SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) ZoneOffset(java.time.ZoneOffset) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) SqlDate(com.facebook.presto.common.type.SqlDate)

Example 14 with SqlDate

use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.

the class TestTimestampBase method testCastToDate.

@Test
public void testCastToDate() {
    long millis = new DateTime(2001, 1, 22, 0, 0, UTC).getMillis();
    assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321' as date)", DATE, new SqlDate((int) TimeUnit.MILLISECONDS.toDays(millis)));
}
Also used : SqlDate(com.facebook.presto.common.type.SqlDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 15 with SqlDate

use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.

the class AbstractTestHiveFileFormats method checkPageSource.

protected void checkPageSource(ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types, int rowCount) throws IOException {
    try {
        MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types);
        assertEquals(result.getMaterializedRows().size(), rowCount);
        for (MaterializedRow row : result) {
            for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) {
                TestColumn testColumn = testColumns.get(i);
                Type type = types.get(i);
                Object actualValue = row.getField(i);
                Object expectedValue = testColumn.getExpectedValue();
                if (expectedValue instanceof Slice) {
                    expectedValue = ((Slice) expectedValue).toStringUtf8();
                }
                if (actualValue == null || expectedValue == null) {
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("float")) {
                    assertEquals((float) actualValue, (float) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("double")) {
                    assertEquals((double) actualValue, (double) expectedValue, EPSILON, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("date")) {
                    SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue());
                    assertEquals(actualValue, expectedDate, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().equals("int") || testColumn.getObjectInspector().getTypeName().equals("smallint") || testColumn.getObjectInspector().getTypeName().equals("tinyint")) {
                    assertEquals(actualValue, expectedValue);
                } else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) {
                    SqlTimestamp expectedTimestamp = sqlTimestampOf((Long) expectedValue, SESSION);
                    assertEquals(actualValue, expectedTimestamp, "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getTypeName().startsWith("char")) {
                    assertEquals(actualValue, padEnd((String) expectedValue, ((CharType) type).getLength(), ' '), "Wrong value for column " + testColumn.getName());
                } else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) {
                    if (expectedValue instanceof Slice) {
                        expectedValue = ((Slice) expectedValue).toStringUtf8();
                    }
                    if (actualValue instanceof Slice) {
                        actualValue = ((Slice) actualValue).toStringUtf8();
                    }
                    if (actualValue instanceof SqlVarbinary) {
                        actualValue = new String(((SqlVarbinary) actualValue).getBytes(), UTF_8);
                    }
                    if (actualValue instanceof SqlDecimal) {
                        actualValue = new BigDecimal(actualValue.toString());
                    }
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                } else {
                    BlockBuilder builder = type.createBlockBuilder(null, 1);
                    type.writeObject(builder, expectedValue);
                    expectedValue = type.getObjectValue(SESSION.getSqlFunctionProperties(), builder.build(), 0);
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                }
            }
        }
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) BigDecimal(java.math.BigDecimal) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) RowType(com.facebook.presto.common.type.RowType) TimestampType(com.facebook.presto.common.type.TimestampType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) HiveTestUtils.mapType(com.facebook.presto.hive.HiveTestUtils.mapType) Type(com.facebook.presto.common.type.Type) DateType(com.facebook.presto.common.type.DateType) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SerDeUtils.serializeObject(com.facebook.presto.hive.util.SerDeUtils.serializeObject) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) CharType(com.facebook.presto.common.type.CharType) MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

SqlDate (com.facebook.presto.common.type.SqlDate)18 DecimalType (com.facebook.presto.common.type.DecimalType)8 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)8 DateTime (org.joda.time.DateTime)7 SqlDecimal (com.facebook.presto.common.type.SqlDecimal)6 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)6 Type (com.facebook.presto.common.type.Type)6 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 ImmutableList (com.google.common.collect.ImmutableList)5 Slice (io.airlift.slice.Slice)5 List (java.util.List)5 Collectors.toList (java.util.stream.Collectors.toList)5 Block (com.facebook.presto.common.block.Block)4 ArrayType (com.facebook.presto.common.type.ArrayType)4 RowType (com.facebook.presto.common.type.RowType)4 CharType (com.facebook.presto.common.type.CharType)3 VarcharType (com.facebook.presto.common.type.VarcharType)3 OrcLazyObject (com.facebook.hive.orc.lazy.OrcLazyObject)2 Page (com.facebook.presto.common.Page)2