Search in sources :

Example 11 with CatalogPartitionSpec

use of org.apache.flink.table.catalog.CatalogPartitionSpec in project flink by apache.

the class CatalogStatisticsTest method createPartitionStats.

private void createPartitionStats(String part1, int part2, long rowCount) throws Exception {
    ObjectPath path = ObjectPath.fromString("default_database.PartT");
    LinkedHashMap<String, String> partSpecMap = new LinkedHashMap<>();
    partSpecMap.put("part1", part1);
    partSpecMap.put("part2", String.valueOf(part2));
    CatalogPartitionSpec partSpec = new CatalogPartitionSpec(partSpecMap);
    catalog.createPartition(path, partSpec, new CatalogPartitionImpl(new HashMap<>(), ""), true);
    catalog.alterPartitionStatistics(path, partSpec, new CatalogTableStatistics(rowCount, 10, 1000L, 2000L), true);
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CatalogColumnStatisticsDataString(org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataString) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) LinkedHashMap(java.util.LinkedHashMap) CatalogPartitionImpl(org.apache.flink.table.catalog.CatalogPartitionImpl) CatalogTableStatistics(org.apache.flink.table.catalog.stats.CatalogTableStatistics)

Example 12 with CatalogPartitionSpec

use of org.apache.flink.table.catalog.CatalogPartitionSpec in project flink by apache.

the class CatalogStatisticsTest method createPartitionColumnStats.

private void createPartitionColumnStats(String part1, int part2, boolean unknown) throws Exception {
    ObjectPath path = ObjectPath.fromString("default_database.PartT");
    LinkedHashMap<String, String> partSpecMap = new LinkedHashMap<>();
    partSpecMap.put("part1", part1);
    partSpecMap.put("part2", String.valueOf(part2));
    CatalogPartitionSpec partSpec = new CatalogPartitionSpec(partSpecMap);
    CatalogColumnStatisticsDataLong longColStats = new CatalogColumnStatisticsDataLong(-123L, 763322L, 23L, 77L);
    CatalogColumnStatisticsDataString stringColStats = new CatalogColumnStatisticsDataString(152L, 43.5D, 20L, 0L);
    Map<String, CatalogColumnStatisticsDataBase> colStatsMap = new HashMap<>();
    colStatsMap.put("id", unknown ? new CatalogColumnStatisticsDataLong(null, null, null, null) : longColStats);
    colStatsMap.put("name", unknown ? new CatalogColumnStatisticsDataString(null, null, null, null) : stringColStats);
    catalog.alterPartitionColumnStatistics(path, partSpec, new CatalogColumnStatistics(colStatsMap), true);
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogColumnStatisticsDataLong(org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataLong) CatalogColumnStatisticsDataBase(org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataBase) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CatalogColumnStatisticsDataString(org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataString) CatalogColumnStatisticsDataString(org.apache.flink.table.catalog.stats.CatalogColumnStatisticsDataString) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) CatalogColumnStatistics(org.apache.flink.table.catalog.stats.CatalogColumnStatistics) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with CatalogPartitionSpec

use of org.apache.flink.table.catalog.CatalogPartitionSpec in project flink by apache.

the class HiveCatalog method alterTableViaProperties.

private void alterTableViaProperties(AlterTableOp alterOp, Table hiveTable, CatalogTable catalogTable, Map<String, String> oldProps, Map<String, String> newProps, StorageDescriptor sd) {
    switch(alterOp) {
        case CHANGE_TBL_PROPS:
            oldProps.putAll(newProps);
            break;
        case CHANGE_LOCATION:
            HiveTableUtil.extractLocation(sd, newProps);
            break;
        case CHANGE_FILE_FORMAT:
            String newFileFormat = newProps.remove(STORED_AS_FILE_FORMAT);
            HiveTableUtil.setStorageFormat(sd, newFileFormat, hiveConf);
            break;
        case CHANGE_SERDE_PROPS:
            HiveTableUtil.extractRowFormat(sd, newProps);
            break;
        case ALTER_COLUMNS:
            if (hiveTable == null) {
                throw new CatalogException("ALTER COLUMNS cannot be done with ALTER PARTITION");
            }
            HiveTableUtil.alterColumns(hiveTable.getSd(), catalogTable);
            boolean cascade = Boolean.parseBoolean(newProps.remove(ALTER_COL_CASCADE));
            if (cascade) {
                if (!isTablePartitioned(hiveTable)) {
                    throw new CatalogException("ALTER COLUMNS CASCADE for non-partitioned table");
                }
                try {
                    for (CatalogPartitionSpec spec : listPartitions(new ObjectPath(hiveTable.getDbName(), hiveTable.getTableName()))) {
                        Partition partition = getHivePartition(hiveTable, spec);
                        HiveTableUtil.alterColumns(partition.getSd(), catalogTable);
                        client.alter_partition(hiveTable.getDbName(), hiveTable.getTableName(), partition);
                    }
                } catch (Exception e) {
                    throw new CatalogException("Failed to cascade add/replace columns to partitions", e);
                }
            }
            break;
        default:
            throw new CatalogException("Unsupported alter table operation " + alterOp);
    }
}
Also used : Partition(org.apache.hadoop.hive.metastore.api.Partition) CatalogPartition(org.apache.flink.table.catalog.CatalogPartition) ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) FunctionAlreadyExistException(org.apache.flink.table.catalog.exceptions.FunctionAlreadyExistException) PartitionNotExistException(org.apache.flink.table.catalog.exceptions.PartitionNotExistException) PartitionSpecInvalidException(org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) TablePartitionedException(org.apache.flink.table.catalog.exceptions.TablePartitionedException) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) TException(org.apache.thrift.TException) IOException(java.io.IOException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) FunctionNotExistException(org.apache.flink.table.catalog.exceptions.FunctionNotExistException) DatabaseNotEmptyException(org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException) DatabaseAlreadyExistException(org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException) FileNotFoundException(java.io.FileNotFoundException) TableNotPartitionedException(org.apache.flink.table.catalog.exceptions.TableNotPartitionedException) PartitionAlreadyExistsException(org.apache.flink.table.catalog.exceptions.PartitionAlreadyExistsException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException)

Example 14 with CatalogPartitionSpec

use of org.apache.flink.table.catalog.CatalogPartitionSpec in project flink by apache.

the class HiveCatalog method listPartitionsByFilter.

@Override
public List<CatalogPartitionSpec> listPartitionsByFilter(ObjectPath tablePath, List<Expression> expressions) throws TableNotExistException, TableNotPartitionedException, CatalogException {
    Table hiveTable = getHiveTable(tablePath);
    ensurePartitionedTable(tablePath, hiveTable);
    List<String> partColNames = getFieldNames(hiveTable.getPartitionKeys());
    Optional<String> filter = HiveTableUtil.makePartitionFilter(HiveTableUtil.getNonPartitionFields(hiveConf, hiveTable, hiveShim).size(), partColNames, expressions, hiveShim);
    if (!filter.isPresent()) {
        throw new UnsupportedOperationException("HiveCatalog is unable to handle the partition filter expressions: " + expressions);
    }
    try {
        PartitionSpecProxy partitionSpec = client.listPartitionSpecsByFilter(tablePath.getDatabaseName(), tablePath.getObjectName(), filter.get(), (short) -1);
        List<CatalogPartitionSpec> res = new ArrayList<>(partitionSpec.size());
        PartitionSpecProxy.PartitionIterator partitions = partitionSpec.getPartitionIterator();
        while (partitions.hasNext()) {
            Partition partition = partitions.next();
            Map<String, String> spec = new HashMap<>();
            for (int i = 0; i < partColNames.size(); i++) {
                spec.put(partColNames.get(i), partition.getValues().get(i));
            }
            res.add(new CatalogPartitionSpec(spec));
        }
        return res;
    } catch (TException e) {
        throw new UnsupportedOperationException("Failed to list partition by filter from HMS, filter expressions: " + expressions, e);
    }
}
Also used : TException(org.apache.thrift.TException) Partition(org.apache.hadoop.hive.metastore.api.Partition) CatalogPartition(org.apache.flink.table.catalog.CatalogPartition) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.metastore.api.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UniqueConstraint(org.apache.flink.table.api.constraints.UniqueConstraint) PartitionSpecProxy(org.apache.hadoop.hive.metastore.partition.spec.PartitionSpecProxy) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec)

Example 15 with CatalogPartitionSpec

use of org.apache.flink.table.catalog.CatalogPartitionSpec in project flink by apache.

the class HiveParserDDLSemanticAnalyzer method convertAlterTableDropParts.

private Operation convertAlterTableDropParts(String[] qualified, HiveParserASTNode ast) {
    boolean ifExists = ast.getFirstChildWithType(HiveASTParser.TOK_IFEXISTS) != null;
    // If the drop has to fail on non-existent partitions, we cannot batch expressions.
    // That is because we actually have to check each separate expression for existence.
    // We could do a small optimization for the case where expr has all columns and all
    // operators are equality, if we assume those would always match one partition (which
    // may not be true with legacy, non-normalized column values). This is probably a
    // popular case but that's kinda hacky. Let's not do it for now.
    Table tab = getTable(new ObjectPath(qualified[0], qualified[1]));
    // hive represents drop partition specs with generic func desc, but what we need is just
    // spec maps
    List<Map<String, String>> partSpecs = new ArrayList<>();
    for (int i = 0; i < ast.getChildCount(); i++) {
        HiveParserASTNode child = (HiveParserASTNode) ast.getChild(i);
        if (child.getType() == HiveASTParser.TOK_PARTSPEC) {
            partSpecs.add(getPartSpec(child));
        }
    }
    validateAlterTableType(tab);
    ObjectIdentifier tableIdentifier = catalogManager.qualifyIdentifier(UnresolvedIdentifier.of(qualified[0], qualified[1]));
    List<CatalogPartitionSpec> specs = partSpecs.stream().map(CatalogPartitionSpec::new).collect(Collectors.toList());
    return new DropPartitionsOperation(tableIdentifier, ifExists, specs);
}
Also used : DropPartitionsOperation(org.apache.flink.table.operations.ddl.DropPartitionsOperation) ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogTable(org.apache.flink.table.catalog.CatalogTable) SqlCreateHiveTable(org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable) Table(org.apache.hadoop.hive.ql.metadata.Table) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) HiveParserASTNode(org.apache.flink.table.planner.delegation.hive.copy.HiveParserASTNode) ArrayList(java.util.ArrayList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) NotNullConstraint(org.apache.flink.table.planner.delegation.hive.copy.HiveParserBaseSemanticAnalyzer.NotNullConstraint) UniqueConstraint(org.apache.flink.table.api.constraints.UniqueConstraint) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Aggregations

CatalogPartitionSpec (org.apache.flink.table.catalog.CatalogPartitionSpec)32 HashMap (java.util.HashMap)20 LinkedHashMap (java.util.LinkedHashMap)15 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 ObjectPath (org.apache.flink.table.catalog.ObjectPath)11 List (java.util.List)10 CatalogTable (org.apache.flink.table.catalog.CatalogTable)10 Path (org.apache.flink.core.fs.Path)8 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)8 CatalogPartition (org.apache.flink.table.catalog.CatalogPartition)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 SqlCreateHiveTable (org.apache.flink.sql.parser.hive.ddl.SqlCreateHiveTable)6 CatalogPartitionImpl (org.apache.flink.table.catalog.CatalogPartitionImpl)6 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)6 ValidationException (org.apache.flink.table.api.ValidationException)5 RowData (org.apache.flink.table.data.RowData)5 Partition (org.apache.hadoop.hive.metastore.api.Partition)5 Set (java.util.Set)4