prestodb/presto · error · PrestoException

HIVE_INVALID_METADATA

HIVE_INVALID_METADATA

Error message

Expected %d partition key values, but got %d

What it means

While building materialized data predicates, each partition name string is parsed into key/value pairs and compared to the table's declared partition columns. If the count of parsed partition key values does not match the number of partition columns, the metastore data is inconsistent and HIVE_INVALID_METADATA is thrown.

Source

Thrown at presto-hive/src/main/java/com/facebook/presto/hive/HiveMaterializedViewUtils.java:170

                throw new PrestoException(
                        NOT_SUPPORTED,
                        String.format("Unsupported Hive type %s found in partition keys of table %s.%s", hiveType, table.getDatabaseName(), table.getTableName()));
            }
        }

        List<HiveColumnHandle> partitionKeyColumnHandles = getPartitionKeyColumnHandles(table);
        Map<String, Type> partitionTypes = partitionKeyColumnHandles.stream()
                .collect(toImmutableMap(HiveColumnHandle::getName, column -> typeManager.getType(column.getTypeSignature())));

        List<PartitionNameWithVersion> partitionNames = metastore.getPartitionNames(metastoreContext, table.getDatabaseName(), table.getTableName())
                .orElseThrow(() -> new TableNotFoundException(new SchemaTableName(table.getDatabaseName(), table.getTableName())));

        ImmutableList.Builder<TupleDomain<String>> partitionNamesAndValues = ImmutableList.builder();
        for (PartitionNameWithVersion partitionName : partitionNames) {
            ImmutableMap.Builder<String, NullableValue> partitionNameAndValuesMap = ImmutableMap.builder();
            Map<String, String> partitions = toPartitionNamesAndValues(partitionName.getPartitionName());
            if (partitionColumns.size() != partitions.size()) {
                throw new PrestoException(HIVE_INVALID_METADATA, String.format(
                        "Expected %d partition key values, but got %d", partitionColumns.size(), partitions.size()));
            }
            partitionTypes.forEach((name, type) -> {
                String value = partitions.get(name);
                if (value == null) {
                    throw new PrestoException(HIVE_INVALID_PARTITION_VALUE, String.format("partition key value cannot be null for field: %s", name));
                }

                partitionNameAndValuesMap.put(name, parsePartitionValue(Optional.of(session), name, value, type, timeZone));
            });

            TupleDomain<String> tupleDomain = TupleDomain.fromFixedValues(partitionNameAndValuesMap.build());
            partitionNamesAndValues.add(tupleDomain);
        }

        return new MaterializedDataPredicates(partitionNamesAndValues.build(), partitionColumns.stream()
                .map(Column::getName)
                .collect(toImmutableList()));

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Run SHOW PARTITIONS / MSCK REPAIR to inspect and reconcile metastore partitions
  2. Drop the malformed partition(s) and re-add them with the correct schema
  3. Alter the table to restore the original partition column count
  4. Repair the metastore entries directly (Hive metastore client) for the offending partition names
Defensive patterns

Strategy: validation

Validate before calling

Map<String, String> parts = toPartitionNamesAndValues(partitionName);
if (parts.size() != table.getPartitionColumns().size()) {
    throw new IllegalStateException("Partition spec arity mismatch for " + partitionName);
}

Try / catch

try {
    // use partition predicates
} catch (PrestoException e) {
    if (e.getErrorCode().getCode() == HIVE_INVALID_METADATA.toErrorCode().getCode()) {
        // trigger MSCK REPAIR or drop/re-add offending partition
    } else throw e;
}

Prevention

When it happens

Trigger: Calling refresh/analysis paths that pass PartitionNameWithVersion entries whose partition name string parses to a different number of key=value components than table.getPartitionColumns().size().

Common situations: Corrupted or hand-edited partition names in the metastore; partitions created by a different engine with a different partition schema; the table's partition columns changed (ALTER) without fixing existing partitions; stale cached partition lists after schema evolution.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/28a9dc435a0c0bc9. Report an issue: GitHub.