prestodb/presto · error · SemanticException

MISSING_TABLE

MISSING_TABLE

Error message

Table '%s' does not exist

What it means

Thrown by AddConstraintTask.execute when ALTER TABLE ... ADD CONSTRAINT targets a table whose handle cannot be resolved via the metadata resolver, and the statement did not specify IF EXISTS. Presto fails fast because constraints can only be added to existing tables.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/execution/AddConstraintTask.java:96

        }

        return tableConstraint;
    }

    @Override
    public String getName()
    {
        return "ADD CONSTRAINT";
    }

    @Override
    public ListenableFuture<?> execute(AddConstraint statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector, String query)
    {
        QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName(), metadata);
        Optional<TableHandle> tableHandle = metadata.getMetadataResolver(session).getTableHandle(tableName);
        if (!tableHandle.isPresent()) {
            if (!statement.isTableExists()) {
                throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
            }
            return immediateFuture(null);
        }

        Optional<MaterializedViewDefinition> optionalMaterializedView = metadata.getMetadataResolver(session).getMaterializedView(tableName);
        if (optionalMaterializedView.isPresent()) {
            if (!statement.isTableExists()) {
                throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and add constraint is not supported", tableName);
            }
            return immediateFuture(null);
        }

        ConnectorId connectorId = metadata.getCatalogHandle(session, tableName.getCatalogName())
                .orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + tableName.getCatalogName()));

        accessControl.checkCanAddConstraints(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);

        metadata.addConstraint(session, tableHandle.get(), convertToTableConstraint(metadata, session, connectorId, statement.getConstraintSpecification(), warningCollector, query));

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Verify the table exists with SHOW TABLES / SHOW CREATE TABLE in the right catalog.schema
  2. Correct the qualified table name (catalog.schema.table) in the statement
  3. Add IF EXISTS semantics if your deployment should silently skip missing tables

Example fix

// before
ALTER TABLE hive.default.usr ADD CONSTRAINT uq UNIQUE (id);
// after
ALTER TABLE hive.default.users ADD CONSTRAINT uq UNIQUE (id);
Defensive patterns

Strategy: validation

Validate before calling

QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getTableName(), metadata);
if (metadata.getMetadataResolver(session).getTableHandle(tableName).isEmpty()) {
    if (!statement.isTableExists()) {
        throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
    }
}

Type guard

boolean tableExists(Metadata metadata, Session session, QualifiedObjectName name) {
    return metadata.getMetadataResolver(session).getTableHandle(name).isPresent();
}

Try / catch

try {
    executeAddConstraint(statement);
} catch (SemanticException e) {
    if (e.getCode() == MISSING_TABLE) {
        log.warn("Table %s missing; skipping constraint DDL", statement.getTableName());
    } else throw e;
}

Prevention

When it happens

Trigger: `ALTER TABLE cat.schema.t ADD CONSTRAINT ...` where t does not exist (or exists under a different case/name) and no `IF EXISTS` guard was given.

Common situations: Typos in qualified table names; running DDL against the wrong catalog or schema; table dropped by a concurrent job; case-sensitivity differences across catalogs.

Related errors


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