Search in sources :

Example 6 with SqlDate

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

the class TestArrayAggregation method testDate.

@Test
public void testDate() throws Exception {
    InternalAggregationFunction varcharAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("array_agg", AGGREGATE, parseTypeSignature("array(date)"), parseTypeSignature(StandardTypes.DATE)));
    assertAggregation(varcharAgg, Arrays.asList(new SqlDate(1), new SqlDate(2), new SqlDate(4)), createTypedLongsBlock(DATE, ImmutableList.of(1L, 2L, 4L)));
}
Also used : Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) SqlDate(com.facebook.presto.spi.type.SqlDate) Test(org.testng.annotations.Test)

Example 7 with SqlDate

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

the class TestDate method testLeast.

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

Example 8 with SqlDate

use of com.facebook.presto.spi.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 = new SqlTimestamp((Long) expectedValue, SESSION.getTimeZoneKey());
                    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(new BlockBuilderStatus(), 1);
                    type.writeObject(builder, expectedValue);
                    expectedValue = type.getObjectValue(SESSION, builder.build(), 0);
                    assertEquals(actualValue, expectedValue, "Wrong value for column " + testColumn.getName());
                }
            }
        }
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) SqlDecimal(com.facebook.presto.spi.type.SqlDecimal) BigDecimal(java.math.BigDecimal) RowType(com.facebook.presto.type.RowType) CharType.createCharType(com.facebook.presto.spi.type.CharType.createCharType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) DateType(com.facebook.presto.spi.type.DateType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) ArrayType(com.facebook.presto.type.ArrayType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.spi.type.SqlDate) SerDeUtils.serializeObject(com.facebook.presto.hive.util.SerDeUtils.serializeObject) CharType.createCharType(com.facebook.presto.spi.type.CharType.createCharType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 9 with SqlDate

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

the class AbstractTestHiveClient method assertGetRecords.

protected void assertGetRecords(HiveStorageFormat hiveStorageFormat, ConnectorTableMetadata tableMetadata, HiveSplit hiveSplit, ConnectorPageSource pageSource, List<? extends ColumnHandle> columnHandles) throws IOException {
    try {
        MaterializedResult result = materializeSourceDataStream(newSession(), pageSource, getTypes(columnHandles));
        assertPageSourceType(pageSource, hiveStorageFormat);
        ImmutableMap<String, Integer> columnIndex = indexColumns(tableMetadata);
        long rowNumber = 0;
        long completedBytes = 0;
        for (MaterializedRow row : result) {
            try {
                assertValueTypes(row, tableMetadata.getColumns());
            } catch (RuntimeException e) {
                throw new RuntimeException("row " + rowNumber, e);
            }
            rowNumber++;
            Integer index;
            Object value;
            // STRING
            index = columnIndex.get("t_string");
            value = row.getField(index);
            if (rowNumber % 19 == 0) {
                assertNull(value);
            } else if (rowNumber % 19 == 1) {
                assertEquals(value, "");
            } else {
                assertEquals(value, "test");
            }
            // NUMBERS
            assertEquals(row.getField(columnIndex.get("t_tinyint")), (byte) (1 + rowNumber));
            assertEquals(row.getField(columnIndex.get("t_smallint")), (short) (2 + rowNumber));
            assertEquals(row.getField(columnIndex.get("t_int")), (int) (3 + rowNumber));
            index = columnIndex.get("t_bigint");
            if ((rowNumber % 13) == 0) {
                assertNull(row.getField(index));
            } else {
                assertEquals(row.getField(index), 4 + rowNumber);
            }
            assertEquals((Float) row.getField(columnIndex.get("t_float")), 5.1f + rowNumber, 0.001);
            assertEquals(row.getField(columnIndex.get("t_double")), 6.2 + rowNumber);
            // BOOLEAN
            index = columnIndex.get("t_boolean");
            if ((rowNumber % 3) == 2) {
                assertNull(row.getField(index));
            } else {
                assertEquals(row.getField(index), (rowNumber % 3) != 0);
            }
            // TIMESTAMP
            index = columnIndex.get("t_timestamp");
            if (index != null) {
                if ((rowNumber % 17) == 0) {
                    assertNull(row.getField(index));
                } else {
                    SqlTimestamp expected = new SqlTimestamp(new DateTime(2011, 5, 6, 7, 8, 9, 123, timeZone).getMillis(), UTC_KEY);
                    assertEquals(row.getField(index), expected);
                }
            }
            // BINARY
            index = columnIndex.get("t_binary");
            if (index != null) {
                if ((rowNumber % 23) == 0) {
                    assertNull(row.getField(index));
                } else {
                    assertEquals(row.getField(index), new SqlVarbinary("test binary".getBytes(UTF_8)));
                }
            }
            // DATE
            index = columnIndex.get("t_date");
            if (index != null) {
                if ((rowNumber % 37) == 0) {
                    assertNull(row.getField(index));
                } else {
                    SqlDate expected = new SqlDate(toIntExact(MILLISECONDS.toDays(new DateTime(2013, 8, 9, 0, 0, 0, UTC).getMillis())));
                    assertEquals(row.getField(index), expected);
                }
            }
            // VARCHAR(50)
            index = columnIndex.get("t_varchar");
            if (index != null) {
                value = row.getField(index);
                if (rowNumber % 39 == 0) {
                    assertNull(value);
                } else if (rowNumber % 39 == 1) {
                    // RCBINARY reads empty VARCHAR as null
                    if (hiveStorageFormat == RCBINARY) {
                        assertNull(value);
                    } else {
                        assertEquals(value, "");
                    }
                } else {
                    assertEquals(value, "test varchar");
                }
            }
            //CHAR(25)
            index = columnIndex.get("t_char");
            if (index != null) {
                value = row.getField(index);
                if ((rowNumber % 41) == 0) {
                    assertNull(value);
                } else {
                    assertEquals(value, (rowNumber % 41) == 1 ? "                         " : "test char                ");
                }
            }
            // MAP<STRING, STRING>
            index = columnIndex.get("t_map");
            if (index != null) {
                if ((rowNumber % 27) == 0) {
                    assertNull(row.getField(index));
                } else {
                    assertEquals(row.getField(index), ImmutableMap.of("test key", "test value"));
                }
            }
            // ARRAY<STRING>
            index = columnIndex.get("t_array_string");
            if (index != null) {
                if ((rowNumber % 29) == 0) {
                    assertNull(row.getField(index));
                } else {
                    assertEquals(row.getField(index), ImmutableList.of("abc", "xyz", "data"));
                }
            }
            // ARRAY<STRUCT<s_string: STRING, s_double:DOUBLE>>
            index = columnIndex.get("t_array_struct");
            if (index != null) {
                if ((rowNumber % 31) == 0) {
                    assertNull(row.getField(index));
                } else {
                    List<Object> expected1 = ImmutableList.of("test abc", 0.1);
                    List<Object> expected2 = ImmutableList.of("test xyz", 0.2);
                    assertEquals(row.getField(index), ImmutableList.of(expected1, expected2));
                }
            }
            // MAP<INT, ARRAY<STRUCT<s_string: STRING, s_double:DOUBLE>>>
            index = columnIndex.get("t_complex");
            if (index != null) {
                if ((rowNumber % 33) == 0) {
                    assertNull(row.getField(index));
                } else {
                    List<Object> expected1 = ImmutableList.of("test abc", 0.1);
                    List<Object> expected2 = ImmutableList.of("test xyz", 0.2);
                    assertEquals(row.getField(index), ImmutableMap.of(1, ImmutableList.of(expected1, expected2)));
                }
            }
            // NEW COLUMN
            assertNull(row.getField(columnIndex.get("new_column")));
            long newCompletedBytes = pageSource.getCompletedBytes();
            assertTrue(newCompletedBytes >= completedBytes);
            assertTrue(newCompletedBytes <= hiveSplit.getLength());
            completedBytes = newCompletedBytes;
        }
        assertTrue(completedBytes <= hiveSplit.getLength());
        assertEquals(rowNumber, 100);
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) DateTime(org.joda.time.DateTime) SqlDate(com.facebook.presto.spi.type.SqlDate) MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Example 10 with SqlDate

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

the class MaterializedResult method convertToJdbcTypes.

private static MaterializedRow convertToJdbcTypes(MaterializedRow prestoRow) {
    List<Object> jdbcValues = new ArrayList<>();
    for (int field = 0; field < prestoRow.getFieldCount(); field++) {
        Object prestoValue = prestoRow.getField(field);
        Object jdbcValue;
        if (prestoValue instanceof SqlDate) {
            int days = ((SqlDate) prestoValue).getDays();
            jdbcValue = new Date(TimeUnit.DAYS.toMillis(days));
        } else if (prestoValue instanceof SqlTime) {
            jdbcValue = new Time(((SqlTime) prestoValue).getMillisUtc());
        } else if (prestoValue instanceof SqlTimeWithTimeZone) {
            jdbcValue = new Time(((SqlTimeWithTimeZone) prestoValue).getMillisUtc());
        } else if (prestoValue instanceof SqlTimestamp) {
            jdbcValue = new Timestamp(((SqlTimestamp) prestoValue).getMillisUtc());
        } else if (prestoValue instanceof SqlTimestampWithTimeZone) {
            jdbcValue = new Timestamp(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc());
        } else if (prestoValue instanceof SqlDecimal) {
            jdbcValue = ((SqlDecimal) prestoValue).toBigDecimal();
        } else {
            jdbcValue = prestoValue;
        }
        jdbcValues.add(jdbcValue);
    }
    return new MaterializedRow(prestoRow.getPrecision(), jdbcValues);
}
Also used : ArrayList(java.util.ArrayList) SqlTime(com.facebook.presto.spi.type.SqlTime) SqlTimeWithTimeZone(com.facebook.presto.spi.type.SqlTimeWithTimeZone) Time(java.sql.Time) SqlTime(com.facebook.presto.spi.type.SqlTime) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) SqlDecimal(com.facebook.presto.spi.type.SqlDecimal) SqlTimestamp(com.facebook.presto.spi.type.SqlTimestamp) Timestamp(java.sql.Timestamp) SqlDate(com.facebook.presto.spi.type.SqlDate) Date(java.sql.Date) SqlTimestampWithTimeZone(com.facebook.presto.spi.type.SqlTimestampWithTimeZone) SqlDate(com.facebook.presto.spi.type.SqlDate)

Aggregations

SqlDate (com.facebook.presto.spi.type.SqlDate)13 SqlTimestamp (com.facebook.presto.spi.type.SqlTimestamp)7 DateTime (org.joda.time.DateTime)6 Test (org.testng.annotations.Test)6 SqlVarbinary (com.facebook.presto.spi.type.SqlVarbinary)5 SqlDecimal (com.facebook.presto.spi.type.SqlDecimal)4 ArrayList (java.util.ArrayList)4 DecimalType (com.facebook.presto.spi.type.DecimalType)3 ImmutableList (com.google.common.collect.ImmutableList)3 Date (java.sql.Date)3 Timestamp (java.sql.Timestamp)3 List (java.util.List)3 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)2 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)2 Type (com.facebook.presto.spi.type.Type)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2 MaterializedRow (com.facebook.presto.testing.MaterializedRow)2 ArrayType (com.facebook.presto.type.ArrayType)2 MapType (com.facebook.presto.type.MapType)2 RowType (com.facebook.presto.type.RowType)2