Search in sources :

Example 6 with NullableValue

use of io.trino.spi.predicate.NullableValue in project trino by trinodb.

the class HiveBucketing method getHiveBuckets.

private static Optional<Set<Integer>> getHiveBuckets(HiveBucketProperty hiveBucketProperty, List<Column> dataColumns, Map<ColumnHandle, List<NullableValue>> bindings) {
    if (bindings.isEmpty()) {
        return Optional.empty();
    }
    // Get bucket columns names
    List<String> bucketColumns = hiveBucketProperty.getBucketedBy();
    // Verify the bucket column types are supported
    Map<String, HiveType> hiveTypes = new HashMap<>();
    for (Column column : dataColumns) {
        hiveTypes.put(column.getName(), column.getType());
    }
    for (String column : bucketColumns) {
        if (!SUPPORTED_TYPES_FOR_BUCKET_FILTER.contains(hiveTypes.get(column))) {
            return Optional.empty();
        }
    }
    // Get bindings for bucket columns
    Map<String, List<NullableValue>> bucketBindings = new HashMap<>();
    for (Entry<ColumnHandle, List<NullableValue>> entry : bindings.entrySet()) {
        HiveColumnHandle columnHandle = (HiveColumnHandle) entry.getKey();
        if (bucketColumns.contains(columnHandle.getName())) {
            bucketBindings.put(columnHandle.getName(), entry.getValue());
        }
    }
    // Check that we have bindings for all bucket columns
    if (bucketBindings.size() != bucketColumns.size()) {
        return Optional.empty();
    }
    // Order bucket column bindings accordingly to bucket columns order
    List<List<NullableValue>> orderedBindings = bucketColumns.stream().map(bucketBindings::get).collect(toImmutableList());
    // Get TypeInfos for bucket columns
    List<TypeInfo> typeInfos = bucketColumns.stream().map(name -> hiveTypes.get(name).getTypeInfo()).collect(toImmutableList());
    return getHiveBuckets(hiveBucketProperty.getBucketingVersion(), hiveBucketProperty.getBucketCount(), typeInfos, orderedBindings);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) BUCKETING_V2(io.trino.plugin.hive.util.HiveBucketing.BucketingVersion.BUCKETING_V2) Lists.cartesianProduct(com.google.common.collect.Lists.cartesianProduct) BUCKETING_V1(io.trino.plugin.hive.util.HiveBucketing.BucketingVersion.BUCKETING_V1) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) Column(io.trino.plugin.hive.metastore.Column) Map(java.util.Map) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) ImmutableSet(com.google.common.collect.ImmutableSet) Table(io.trino.plugin.hive.metastore.Table) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) HiveTimestampPrecision(io.trino.plugin.hive.HiveTimestampPrecision) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) ValueSet(io.trino.spi.predicate.ValueSet) Objects(java.util.Objects) List(java.util.List) BUCKET_COLUMN_NAME(io.trino.plugin.hive.HiveColumnHandle.BUCKET_COLUMN_NAME) StandardErrorCode(io.trino.spi.StandardErrorCode) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) NullableValue(io.trino.spi.predicate.NullableValue) Page(io.trino.spi.Page) HashMap(java.util.HashMap) HiveBucketProperty(io.trino.plugin.hive.HiveBucketProperty) HashSet(java.util.HashSet) HiveType(io.trino.plugin.hive.HiveType) HIVE_INVALID_METADATA(io.trino.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA) ImmutableList(com.google.common.collect.ImmutableList) HiveTableHandle(io.trino.plugin.hive.HiveTableHandle) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ColumnHandle(io.trino.spi.connector.ColumnHandle) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TABLE_BUCKETING_VERSION(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.TABLE_BUCKETING_VERSION) HiveBucketHandle(io.trino.plugin.hive.HiveBucketHandle) HiveUtil.getRegularColumnHandles(io.trino.plugin.hive.util.HiveUtil.getRegularColumnHandles) HiveSessionProperties.getTimestampPrecision(io.trino.plugin.hive.HiveSessionProperties.getTimestampPrecision) SPARK_TABLE_PROVIDER_KEY(io.trino.plugin.hive.util.HiveUtil.SPARK_TABLE_PROVIDER_KEY) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) SortingColumn(io.trino.plugin.hive.metastore.SortingColumn) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TypeManager(io.trino.spi.type.TypeManager) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) HashMap(java.util.HashMap) 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) Column(io.trino.plugin.hive.metastore.Column) SortingColumn(io.trino.plugin.hive.metastore.SortingColumn) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HiveType(io.trino.plugin.hive.HiveType) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle)

Example 7 with NullableValue

use of io.trino.spi.predicate.NullableValue in project trino by trinodb.

the class AbstractTestHive method testBucketedTableBigintBoolean.

@SuppressWarnings("ConstantConditions")
@Test
public void testBucketedTableBigintBoolean() throws Exception {
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        ConnectorSession session = newSession();
        metadata.beginQuery(session);
        ConnectorTableHandle tableHandle = getTableHandle(metadata, tableBucketedBigintBoolean);
        List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(session, tableHandle).values());
        Map<String, Integer> columnIndex = indexColumns(columnHandles);
        assertTableIsBucketed(tableHandle, transaction, session);
        ConnectorTableProperties properties = metadata.getTableProperties(newSession(ImmutableMap.of("propagate_table_scan_sorting_properties", true)), tableHandle);
        // trino_test_bucketed_by_bigint_boolean does not define sorting, therefore local properties is empty
        assertTrue(properties.getLocalProperties().isEmpty());
        assertTrue(metadata.getTableProperties(newSession(), tableHandle).getLocalProperties().isEmpty());
        String testString = "test";
        Long testBigint = 89L;
        Boolean testBoolean = true;
        ImmutableMap<ColumnHandle, NullableValue> bindings = ImmutableMap.<ColumnHandle, NullableValue>builder().put(columnHandles.get(columnIndex.get("t_string")), NullableValue.of(createUnboundedVarcharType(), utf8Slice(testString))).put(columnHandles.get(columnIndex.get("t_bigint")), NullableValue.of(BIGINT, testBigint)).put(columnHandles.get(columnIndex.get("t_boolean")), NullableValue.of(BOOLEAN, testBoolean)).buildOrThrow();
        MaterializedResult result = readTable(transaction, tableHandle, columnHandles, session, TupleDomain.fromFixedValues(bindings), OptionalInt.of(1), Optional.empty());
        boolean rowFound = false;
        for (MaterializedRow row : result) {
            if (testString.equals(row.getField(columnIndex.get("t_string"))) && testBigint.equals(row.getField(columnIndex.get("t_bigint"))) && testBoolean.equals(row.getField(columnIndex.get("t_boolean")))) {
                rowFound = true;
                break;
            }
        }
        assertTrue(rowFound);
    }
}
Also used : HiveColumnHandle.bucketColumnHandle(io.trino.plugin.hive.HiveColumnHandle.bucketColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) NullableValue(io.trino.spi.predicate.NullableValue) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) OptionalLong(java.util.OptionalLong) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) MaterializedResult(io.trino.testing.MaterializedResult) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties) MaterializedRow(io.trino.testing.MaterializedRow) Test(org.testng.annotations.Test)

Example 8 with NullableValue

use of io.trino.spi.predicate.NullableValue in project trino by trinodb.

the class AbstractTestHive method setupHive.

protected void setupHive(String databaseName) {
    database = databaseName;
    tablePartitionFormat = new SchemaTableName(database, "trino_test_partition_format");
    tableUnpartitioned = new SchemaTableName(database, "trino_test_unpartitioned");
    tableOffline = new SchemaTableName(database, "trino_test_offline");
    tableOfflinePartition = new SchemaTableName(database, "trino_test_offline_partition");
    tableNotReadable = new SchemaTableName(database, "trino_test_not_readable");
    view = new SchemaTableName(database, "trino_test_view");
    invalidTable = new SchemaTableName(database, INVALID_TABLE);
    tableBucketedStringInt = new SchemaTableName(database, "trino_test_bucketed_by_string_int");
    tableBucketedBigintBoolean = new SchemaTableName(database, "trino_test_bucketed_by_bigint_boolean");
    tableBucketedDoubleFloat = new SchemaTableName(database, "trino_test_bucketed_by_double_float");
    tablePartitionSchemaChange = new SchemaTableName(database, "trino_test_partition_schema_change");
    tablePartitionSchemaChangeNonCanonical = new SchemaTableName(database, "trino_test_partition_schema_change_non_canonical");
    tableBucketEvolution = new SchemaTableName(database, "trino_test_bucket_evolution");
    invalidTableHandle = new HiveTableHandle(database, INVALID_TABLE, ImmutableMap.of(), ImmutableList.of(), ImmutableList.of(), Optional.empty());
    dsColumn = createBaseColumn("ds", -1, HIVE_STRING, VARCHAR, PARTITION_KEY, Optional.empty());
    fileFormatColumn = createBaseColumn("file_format", -1, HIVE_STRING, VARCHAR, PARTITION_KEY, Optional.empty());
    dummyColumn = createBaseColumn("dummy", -1, HIVE_INT, INTEGER, PARTITION_KEY, Optional.empty());
    intColumn = createBaseColumn("t_int", -1, HIVE_INT, INTEGER, PARTITION_KEY, Optional.empty());
    invalidColumnHandle = createBaseColumn(INVALID_COLUMN, 0, HIVE_STRING, VARCHAR, REGULAR, Optional.empty());
    List<ColumnHandle> partitionColumns = ImmutableList.of(dsColumn, fileFormatColumn, dummyColumn);
    tablePartitionFormatPartitions = ImmutableList.<HivePartition>builder().add(new HivePartition(tablePartitionFormat, "ds=2012-12-29/file_format=textfile/dummy=1", ImmutableMap.<ColumnHandle, NullableValue>builder().put(dsColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("2012-12-29"))).put(fileFormatColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("textfile"))).put(dummyColumn, NullableValue.of(INTEGER, 1L)).buildOrThrow())).add(new HivePartition(tablePartitionFormat, "ds=2012-12-29/file_format=sequencefile/dummy=2", ImmutableMap.<ColumnHandle, NullableValue>builder().put(dsColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("2012-12-29"))).put(fileFormatColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("sequencefile"))).put(dummyColumn, NullableValue.of(INTEGER, 2L)).buildOrThrow())).add(new HivePartition(tablePartitionFormat, "ds=2012-12-29/file_format=rctext/dummy=3", ImmutableMap.<ColumnHandle, NullableValue>builder().put(dsColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("2012-12-29"))).put(fileFormatColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("rctext"))).put(dummyColumn, NullableValue.of(INTEGER, 3L)).buildOrThrow())).add(new HivePartition(tablePartitionFormat, "ds=2012-12-29/file_format=rcbinary/dummy=4", ImmutableMap.<ColumnHandle, NullableValue>builder().put(dsColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("2012-12-29"))).put(fileFormatColumn, NullableValue.of(createUnboundedVarcharType(), utf8Slice("rcbinary"))).put(dummyColumn, NullableValue.of(INTEGER, 4L)).buildOrThrow())).build();
    tableUnpartitionedPartitions = ImmutableList.of(new HivePartition(tableUnpartitioned));
    tablePartitionFormatProperties = new ConnectorTableProperties(TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("2012-12-29"))), false), fileFormatColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("textfile")), Range.equal(createUnboundedVarcharType(), utf8Slice("sequencefile")), Range.equal(createUnboundedVarcharType(), utf8Slice("rctext")), Range.equal(createUnboundedVarcharType(), utf8Slice("rcbinary"))), false), dummyColumn, Domain.create(ValueSet.ofRanges(Range.equal(INTEGER, 1L), Range.equal(INTEGER, 2L), Range.equal(INTEGER, 3L), Range.equal(INTEGER, 4L)), false))), Optional.empty(), Optional.empty(), Optional.of(new DiscretePredicates(partitionColumns, ImmutableList.of(TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("2012-12-29"))), false), fileFormatColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("textfile"))), false), dummyColumn, Domain.create(ValueSet.ofRanges(Range.equal(INTEGER, 1L)), false))), TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("2012-12-29"))), false), fileFormatColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("sequencefile"))), false), dummyColumn, Domain.create(ValueSet.ofRanges(Range.equal(INTEGER, 2L)), false))), TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("2012-12-29"))), false), fileFormatColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("rctext"))), false), dummyColumn, Domain.create(ValueSet.ofRanges(Range.equal(INTEGER, 3L)), false))), TupleDomain.withColumnDomains(ImmutableMap.of(dsColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("2012-12-29"))), false), fileFormatColumn, Domain.create(ValueSet.ofRanges(Range.equal(createUnboundedVarcharType(), utf8Slice("rcbinary"))), false), dummyColumn, Domain.create(ValueSet.ofRanges(Range.equal(INTEGER, 4L)), false)))))), ImmutableList.of());
    tableUnpartitionedProperties = new ConnectorTableProperties();
}
Also used : HiveColumnHandle.bucketColumnHandle(io.trino.plugin.hive.HiveColumnHandle.bucketColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) NullableValue(io.trino.spi.predicate.NullableValue) DiscretePredicates(io.trino.spi.connector.DiscretePredicates) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties)

Example 9 with NullableValue

use of io.trino.spi.predicate.NullableValue in project trino by trinodb.

the class CassandraSession method getPartitions.

/**
 * Get the list of partitions matching the given filters on partition keys.
 *
 * @param table the table to get partitions from
 * @param filterPrefixes the list of possible values for each partition key.
 * Order of values should match {@link CassandraTable#getPartitionKeyColumns()}
 * @return list of {@link CassandraPartition}
 */
public List<CassandraPartition> getPartitions(CassandraTable table, List<Set<Object>> filterPrefixes) {
    List<CassandraColumnHandle> partitionKeyColumns = table.getPartitionKeyColumns();
    if (filterPrefixes.size() != partitionKeyColumns.size()) {
        return ImmutableList.of(CassandraPartition.UNPARTITIONED);
    }
    Iterable<Row> rows;
    if (getCassandraVersion().compareTo(PARTITION_FETCH_WITH_IN_PREDICATE_VERSION) > 0) {
        log.debug("Using IN predicate to fetch partitions.");
        rows = queryPartitionKeysWithInClauses(table, filterPrefixes);
    } else {
        log.debug("Using combination of partition values to fetch partitions.");
        rows = queryPartitionKeysLegacyWithMultipleQueries(table, filterPrefixes);
    }
    if (rows == null) {
        // just split the whole partition range
        return ImmutableList.of(CassandraPartition.UNPARTITIONED);
    }
    ByteBuffer buffer = ByteBuffer.allocate(1000);
    HashMap<ColumnHandle, NullableValue> map = new HashMap<>();
    Set<String> uniquePartitionIds = new HashSet<>();
    StringBuilder stringBuilder = new StringBuilder();
    boolean isComposite = partitionKeyColumns.size() > 1;
    ImmutableList.Builder<CassandraPartition> partitions = ImmutableList.builder();
    for (Row row : rows) {
        buffer.clear();
        map.clear();
        stringBuilder.setLength(0);
        for (int i = 0; i < partitionKeyColumns.size(); i++) {
            ByteBuffer component = row.getBytesUnsafe(i);
            if (isComposite) {
                // build composite key
                short len = (short) component.limit();
                buffer.putShort(len);
                buffer.put(component);
                buffer.put((byte) 0);
            } else {
                buffer.put(component);
            }
            CassandraColumnHandle columnHandle = partitionKeyColumns.get(i);
            NullableValue keyPart = columnHandle.getCassandraType().getColumnValue(row, i);
            map.put(columnHandle, keyPart);
            if (i > 0) {
                stringBuilder.append(" AND ");
            }
            stringBuilder.append(CassandraCqlUtils.validColumnName(columnHandle.getName()));
            stringBuilder.append(" = ");
            stringBuilder.append(columnHandle.getCassandraType().getColumnValueForCql(row, i));
        }
        buffer.flip();
        byte[] key = new byte[buffer.limit()];
        buffer.get(key);
        TupleDomain<ColumnHandle> tupleDomain = TupleDomain.fromFixedValues(map);
        String partitionId = stringBuilder.toString();
        if (uniquePartitionIds.add(partitionId)) {
            partitions.add(new CassandraPartition(key, partitionId, tupleDomain, false));
        }
    }
    return partitions.build();
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) HashMap(java.util.HashMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) NullableValue(io.trino.spi.predicate.NullableValue) ByteBuffer(java.nio.ByteBuffer) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet)

Example 10 with NullableValue

use of io.trino.spi.predicate.NullableValue in project trino by trinodb.

the class CassandraType method buildTupleValue.

private Block buildTupleValue(GettableByIndexData row, int position) {
    verify(this.kind == Kind.TUPLE, "Not a TUPLE type");
    TupleValue tupleValue = row.getTupleValue(position);
    RowBlockBuilder blockBuilder = (RowBlockBuilder) this.trinoType.createBlockBuilder(null, 1);
    SingleRowBlockWriter singleRowBlockWriter = blockBuilder.beginBlockEntry();
    int tuplePosition = 0;
    for (CassandraType argumentType : this.getArgumentTypes()) {
        int finalTuplePosition = tuplePosition;
        NullableValue value = argumentType.getColumnValue(tupleValue, tuplePosition, () -> tupleValue.getType().getComponentTypes().get(finalTuplePosition));
        writeNativeValue(argumentType.getTrinoType(), singleRowBlockWriter, value.getValue());
        tuplePosition++;
    }
    // can I just return singleRowBlockWriter here? It extends AbstractSingleRowBlock and tests pass.
    blockBuilder.closeEntry();
    return (Block) this.trinoType.getObject(blockBuilder, 0);
}
Also used : RowBlockBuilder(io.trino.spi.block.RowBlockBuilder) NullableValue(io.trino.spi.predicate.NullableValue) Block(io.trino.spi.block.Block) SingleRowBlockWriter(io.trino.spi.block.SingleRowBlockWriter) TupleValue(com.datastax.driver.core.TupleValue)

Aggregations

NullableValue (io.trino.spi.predicate.NullableValue)33 ColumnHandle (io.trino.spi.connector.ColumnHandle)22 Optional (java.util.Optional)16 ConnectorSession (io.trino.spi.connector.ConnectorSession)15 TupleDomain (io.trino.spi.predicate.TupleDomain)15 ImmutableList (com.google.common.collect.ImmutableList)14 List (java.util.List)14 Map (java.util.Map)14 Set (java.util.Set)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)12 Domain (io.trino.spi.predicate.Domain)12 Objects.requireNonNull (java.util.Objects.requireNonNull)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 ImmutableSet (com.google.common.collect.ImmutableSet)10 TrinoException (io.trino.spi.TrinoException)10 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)10 Constraint (io.trino.spi.connector.Constraint)9 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)8 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)8 SchemaTableName (io.trino.spi.connector.SchemaTableName)8