Search in sources :

Example 1 with PartitionDesign

use of io.trino.plugin.kudu.properties.PartitionDesign in project trino by trinodb.

the class KuduMetadata method beginCreateTable.

@Override
public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, Optional<ConnectorTableLayout> layout) {
    PartitionDesign design = KuduTableProperties.getPartitionDesign(tableMetadata.getProperties());
    boolean generateUUID = !design.hasPartitions();
    ConnectorTableMetadata finalTableMetadata = tableMetadata;
    if (generateUUID) {
        String rowId = KuduColumnHandle.ROW_ID;
        List<ColumnMetadata> copy = new ArrayList<>(tableMetadata.getColumns());
        Map<String, Object> columnProperties = new HashMap<>();
        columnProperties.put(KuduTableProperties.PRIMARY_KEY, true);
        copy.add(0, ColumnMetadata.builder().setName(rowId).setType(VarcharType.VARCHAR).setComment(Optional.of("key=true")).setHidden(true).setProperties(columnProperties).build());
        List<ColumnMetadata> finalColumns = ImmutableList.copyOf(copy);
        Map<String, Object> propsCopy = new HashMap<>(tableMetadata.getProperties());
        propsCopy.put(KuduTableProperties.PARTITION_BY_HASH_COLUMNS, ImmutableList.of(rowId));
        propsCopy.put(KuduTableProperties.PARTITION_BY_HASH_BUCKETS, 2);
        Map<String, Object> finalProperties = ImmutableMap.copyOf(propsCopy);
        finalTableMetadata = new ConnectorTableMetadata(tableMetadata.getTable(), finalColumns, finalProperties, tableMetadata.getComment());
    }
    KuduTable table = clientSession.createTable(finalTableMetadata, false);
    Schema schema = table.getSchema();
    List<ColumnSchema> columns = schema.getColumns();
    List<Type> columnTypes = columns.stream().map(TypeHelper::fromKuduColumn).collect(toImmutableList());
    List<Type> columnOriginalTypes = finalTableMetadata.getColumns().stream().map(ColumnMetadata::getType).collect(toImmutableList());
    return new KuduOutputTableHandle(finalTableMetadata.getTable(), columnOriginalTypes, columnTypes, generateUUID, table);
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) HashBucketSchema(org.apache.kudu.client.PartitionSchema.HashBucketSchema) ArrayList(java.util.ArrayList) KuduTable(org.apache.kudu.client.KuduTable) ColumnSchema(org.apache.kudu.ColumnSchema) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) VarbinaryType(io.trino.spi.type.VarbinaryType) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) PartitionDesign(io.trino.plugin.kudu.properties.PartitionDesign)

Example 2 with PartitionDesign

use of io.trino.plugin.kudu.properties.PartitionDesign in project trino by trinodb.

the class KuduClientSession method changeRangePartition.

private void changeRangePartition(SchemaTableName schemaTableName, RangePartition rangePartition, RangePartitionChange change) {
    try {
        String rawName = schemaEmulation.toRawName(schemaTableName);
        KuduTable table = client.openTable(rawName);
        Schema schema = table.getSchema();
        PartitionDesign design = KuduTableProperties.getPartitionDesign(table);
        RangePartitionDefinition definition = design.getRange();
        if (definition == null) {
            throw new TrinoException(QUERY_REJECTED, "Table " + schemaTableName + " has no range partition");
        }
        PartialRow lowerBound = KuduTableProperties.toRangeBoundToPartialRow(schema, definition, rangePartition.getLower());
        PartialRow upperBound = KuduTableProperties.toRangeBoundToPartialRow(schema, definition, rangePartition.getUpper());
        AlterTableOptions alterOptions = new AlterTableOptions();
        switch(change) {
            case ADD:
                alterOptions.addRangePartition(lowerBound, upperBound);
                break;
            case DROP:
                alterOptions.dropRangePartition(lowerBound, upperBound);
                break;
        }
        client.alterTable(rawName, alterOptions);
    } catch (KuduException e) {
        throw new TrinoException(GENERIC_INTERNAL_ERROR, e);
    }
}
Also used : AlterTableOptions(org.apache.kudu.client.AlterTableOptions) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) HashBucketSchema(org.apache.kudu.client.PartitionSchema.HashBucketSchema) TrinoException(io.trino.spi.TrinoException) PartialRow(org.apache.kudu.client.PartialRow) KuduTable(org.apache.kudu.client.KuduTable) RangePartitionDefinition(io.trino.plugin.kudu.properties.RangePartitionDefinition) PartitionDesign(io.trino.plugin.kudu.properties.PartitionDesign) KuduException(org.apache.kudu.client.KuduException)

Example 3 with PartitionDesign

use of io.trino.plugin.kudu.properties.PartitionDesign in project trino by trinodb.

the class KuduClientSession method buildCreateTableOptions.

private CreateTableOptions buildCreateTableOptions(Schema schema, Map<String, Object> properties) {
    CreateTableOptions options = new CreateTableOptions();
    RangePartitionDefinition rangePartitionDefinition = null;
    PartitionDesign partitionDesign = KuduTableProperties.getPartitionDesign(properties);
    if (partitionDesign.getHash() != null) {
        for (HashPartitionDefinition partition : partitionDesign.getHash()) {
            options.addHashPartitions(partition.getColumns(), partition.getBuckets());
        }
    }
    if (partitionDesign.getRange() != null) {
        rangePartitionDefinition = partitionDesign.getRange();
        options.setRangePartitionColumns(rangePartitionDefinition.getColumns());
    }
    List<RangePartition> rangePartitions = KuduTableProperties.getRangePartitions(properties);
    if (rangePartitionDefinition != null && !rangePartitions.isEmpty()) {
        for (RangePartition rangePartition : rangePartitions) {
            PartialRow lower = KuduTableProperties.toRangeBoundToPartialRow(schema, rangePartitionDefinition, rangePartition.getLower());
            PartialRow upper = KuduTableProperties.toRangeBoundToPartialRow(schema, rangePartitionDefinition, rangePartition.getUpper());
            options.addRangePartition(lower, upper);
        }
    }
    Optional<Integer> numReplicas = KuduTableProperties.getNumReplicas(properties);
    numReplicas.ifPresent(options::setNumReplicas);
    return options;
}
Also used : RangePartition(io.trino.plugin.kudu.properties.RangePartition) HashPartitionDefinition(io.trino.plugin.kudu.properties.HashPartitionDefinition) PartialRow(org.apache.kudu.client.PartialRow) RangePartitionDefinition(io.trino.plugin.kudu.properties.RangePartitionDefinition) CreateTableOptions(org.apache.kudu.client.CreateTableOptions) PartitionDesign(io.trino.plugin.kudu.properties.PartitionDesign)

Aggregations

PartitionDesign (io.trino.plugin.kudu.properties.PartitionDesign)3 RangePartitionDefinition (io.trino.plugin.kudu.properties.RangePartitionDefinition)2 ColumnSchema (org.apache.kudu.ColumnSchema)2 Schema (org.apache.kudu.Schema)2 KuduTable (org.apache.kudu.client.KuduTable)2 PartialRow (org.apache.kudu.client.PartialRow)2 HashBucketSchema (org.apache.kudu.client.PartitionSchema.HashBucketSchema)2 HashPartitionDefinition (io.trino.plugin.kudu.properties.HashPartitionDefinition)1 RangePartition (io.trino.plugin.kudu.properties.RangePartition)1 TrinoException (io.trino.spi.TrinoException)1 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)1 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)1 Type (io.trino.spi.type.Type)1 VarbinaryType (io.trino.spi.type.VarbinaryType)1 VarcharType (io.trino.spi.type.VarcharType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 AlterTableOptions (org.apache.kudu.client.AlterTableOptions)1 CreateTableOptions (org.apache.kudu.client.CreateTableOptions)1