Search in sources :

Example 66 with MaterializedRow

use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.

the class AbstractTestHiveClient method readTable.

private MaterializedResult readTable(Transaction transaction, ConnectorTableHandle hiveTableHandle, ConnectorTableLayoutHandle hiveTableLayoutHandle, List<ColumnHandle> columnHandles, ConnectorSession session, OptionalInt expectedSplitCount, Optional<HiveStorageFormat> expectedStorageFormat) throws Exception {
    List<ConnectorSplit> splits = getAllSplits(session, transaction, hiveTableLayoutHandle);
    if (expectedSplitCount.isPresent()) {
        assertEquals(splits.size(), expectedSplitCount.getAsInt());
    }
    TableHandle tableHandle = toTableHandle(transaction, hiveTableHandle, hiveTableLayoutHandle);
    ImmutableList.Builder<MaterializedRow> allRows = ImmutableList.builder();
    for (ConnectorSplit split : splits) {
        try (ConnectorPageSource pageSource = pageSourceProvider.createPageSource(transaction.getTransactionHandle(), session, split, tableHandle.getLayout().get(), columnHandles, NON_CACHEABLE)) {
            expectedStorageFormat.ifPresent(format -> assertPageSourceType(pageSource, format));
            MaterializedResult result = materializeSourceDataStream(session, pageSource, getTypes(columnHandles));
            allRows.addAll(result.getMaterializedRows());
        }
    }
    return new MaterializedResult(allRows.build(), getTypes(columnHandles));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConnectorOutputTableHandle(com.facebook.presto.spi.ConnectorOutputTableHandle) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) ConnectorInsertTableHandle(com.facebook.presto.spi.ConnectorInsertTableHandle) TableHandle(com.facebook.presto.spi.TableHandle) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) MaterializedResult(com.facebook.presto.testing.MaterializedResult) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Example 67 with MaterializedRow

use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.

the class AbstractTestHiveClient method assertBucketTableEvolutionResult.

private static void assertBucketTableEvolutionResult(MaterializedResult result, List<ColumnHandle> columnHandles, Set<Integer> bucketIds, int rowCount) {
    // Assert that only elements in the specified bucket shows up, and each element shows up 3 times.
    int bucketCount = 8;
    Set<Long> expectedIds = LongStream.range(0, rowCount).filter(x -> bucketIds.contains(toIntExact(x % bucketCount))).boxed().collect(toImmutableSet());
    // assert that content from all three buckets are the same
    Map<String, Integer> columnIndex = indexColumns(columnHandles);
    OptionalInt idColumnIndex = columnIndex.containsKey("id") ? OptionalInt.of(columnIndex.get("id")) : OptionalInt.empty();
    int nameColumnIndex = columnIndex.get("name");
    int bucketColumnIndex = columnIndex.get(BUCKET_COLUMN_NAME);
    Map<Long, Integer> idCount = new HashMap<>();
    for (MaterializedRow row : result.getMaterializedRows()) {
        String name = (String) row.getField(nameColumnIndex);
        int bucket = (int) row.getField(bucketColumnIndex);
        idCount.compute(Long.parseLong(name), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
        assertEquals(bucket, Integer.parseInt(name) % bucketCount);
        if (idColumnIndex.isPresent()) {
            long id = (long) row.getField(idColumnIndex.getAsInt());
            assertEquals(Integer.parseInt(name), id);
        }
    }
    assertEquals((int) idCount.values().stream().distinct().collect(onlyElement()), 3);
    assertEquals(idCount.keySet(), expectedIds);
}
Also used : HashMap(java.util.HashMap) OptionalLong(java.util.OptionalLong) OptionalInt(java.util.OptionalInt) Constraint(com.facebook.presto.spi.Constraint) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Example 68 with MaterializedRow

use of com.facebook.presto.testing.MaterializedRow 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)

Example 69 with MaterializedRow

use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.

the class AbstractTestHiveClient method testGetRecords.

@Test
public void testGetRecords() throws Exception {
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        ConnectorSession session = newSession();
        ConnectorTableHandle hiveTableHandle = getTableHandle(metadata, tablePartitionFormat);
        ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(session, hiveTableHandle);
        List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(session, hiveTableHandle).values());
        Map<String, Integer> columnIndex = indexColumns(columnHandles);
        ConnectorTableLayoutHandle layoutHandle = getLayout(session, transaction, hiveTableHandle, TupleDomain.all());
        List<ConnectorSplit> splits = getAllSplits(session, transaction, layoutHandle);
        assertEquals(splits.size(), partitionCount);
        for (ConnectorSplit split : splits) {
            HiveSplit hiveSplit = (HiveSplit) split;
            List<HivePartitionKey> partitionKeys = hiveSplit.getPartitionKeys();
            String ds = partitionKeys.get(0).getValue().orElse(null);
            String fileFormat = partitionKeys.get(1).getValue().orElse(null);
            HiveStorageFormat fileType = HiveStorageFormat.valueOf(fileFormat.toUpperCase());
            int dummyPartition = Integer.parseInt(partitionKeys.get(2).getValue().orElse(null));
            long rowNumber = 0;
            long completedBytes = 0;
            try (ConnectorPageSource pageSource = pageSourceProvider.createPageSource(transaction.getTransactionHandle(), session, hiveSplit, layoutHandle, columnHandles, NON_CACHEABLE)) {
                MaterializedResult result = materializeSourceDataStream(session, pageSource, getTypes(columnHandles));
                assertPageSourceType(pageSource, fileType);
                for (MaterializedRow row : result) {
                    try {
                        assertValueTypes(row, tableMetadata.getColumns());
                    } catch (RuntimeException e) {
                        throw new RuntimeException("row " + rowNumber, e);
                    }
                    rowNumber++;
                    Object value;
                    value = row.getField(columnIndex.get("t_string"));
                    if (rowNumber % 19 == 0) {
                        assertNull(value);
                    } else if (rowNumber % 19 == 1) {
                        assertEquals(value, "");
                    } else {
                        assertEquals(value, "test");
                    }
                    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")), 3 + (int) rowNumber);
                    if (rowNumber % 13 == 0) {
                        assertNull(row.getField(columnIndex.get("t_bigint")));
                    } else {
                        assertEquals(row.getField(columnIndex.get("t_bigint")), 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);
                    if (rowNumber % 3 == 2) {
                        assertNull(row.getField(columnIndex.get("t_boolean")));
                    } else {
                        assertEquals(row.getField(columnIndex.get("t_boolean")), rowNumber % 3 != 0);
                    }
                    assertEquals(row.getField(columnIndex.get("ds")), ds);
                    assertEquals(row.getField(columnIndex.get("file_format")), fileFormat);
                    assertEquals(row.getField(columnIndex.get("dummy")), dummyPartition);
                    long newCompletedBytes = pageSource.getCompletedBytes();
                    assertTrue(newCompletedBytes >= completedBytes);
                    assertTrue(newCompletedBytes <= hiveSplit.getLength());
                    completedBytes = newCompletedBytes;
                }
                assertTrue(completedBytes <= hiveSplit.getLength());
                assertEquals(rowNumber, 100);
            }
        }
    }
}
Also used : HiveColumnHandle.bucketColumnHandle(com.facebook.presto.hive.HiveColumnHandle.bucketColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) Constraint(com.facebook.presto.spi.Constraint) ConnectorTableHandle(com.facebook.presto.spi.ConnectorTableHandle) StorageFormat.fromHiveStorageFormat(com.facebook.presto.hive.metastore.StorageFormat.fromHiveStorageFormat) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession) ConnectorMetadata(com.facebook.presto.spi.connector.ConnectorMetadata) MaterializedResult(com.facebook.presto.testing.MaterializedResult) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Test(org.testng.annotations.Test)

Example 70 with MaterializedRow

use of com.facebook.presto.testing.MaterializedRow 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 {
    // Some page sources may need to read additional bytes (eg: the metadata footer) in addition to the split data
    long initialPageSourceCompletedBytes = pageSource.getCompletedBytes();
    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 = sqlTimestampOf(2011, 5, 6, 7, 8, 9, 123, timeZone, UTC_KEY, SESSION);
                    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));
                }
            }
            // STRUCT<s_string: STRING, s_double:DOUBLE>
            index = columnIndex.get("t_struct");
            if (index != null) {
                if ((rowNumber % 31) == 0) {
                    assertNull(row.getField(index));
                } else {
                    assertTrue(row.getField(index) instanceof List);
                    List values = (List) row.getField(index);
                    assertEquals(values.size(), 2);
                    assertEquals(values.get(0), "test abc");
                    assertEquals(values.get(1), 0.1);
                }
            }
            // 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() + initialPageSourceCompletedBytes);
            completedBytes = newCompletedBytes;
        }
        assertTrue(completedBytes <= hiveSplit.getLength() + initialPageSourceCompletedBytes);
        assertEquals(rowNumber, 100);
    } finally {
        pageSource.close();
    }
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) DateTime(org.joda.time.DateTime) SqlDate(com.facebook.presto.common.type.SqlDate) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Aggregations

MaterializedRow (com.facebook.presto.testing.MaterializedRow)91 MaterializedResult (com.facebook.presto.testing.MaterializedResult)80 Test (org.testng.annotations.Test)67 AbstractTestIntegrationSmokeTest (com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)31 ImmutableList (com.google.common.collect.ImmutableList)16 Constraint (com.facebook.presto.spi.Constraint)15 ConnectorSession (com.facebook.presto.spi.ConnectorSession)14 Language (org.intellij.lang.annotations.Language)13 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)12 List (java.util.List)11 Session (com.facebook.presto.Session)10 ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)10 ConnectorSplit (com.facebook.presto.spi.ConnectorSplit)10 ColumnHandle (com.facebook.presto.spi.ColumnHandle)9 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)9 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)9 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)9 UUID (java.util.UUID)9 Assert.assertEquals (org.testng.Assert.assertEquals)9 ImmutableMap (com.google.common.collect.ImmutableMap)8