diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java index 8ffbf38cfcaa..337fc8fa0dfd 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java @@ -4279,8 +4279,12 @@ private void analyzeSelectAllColumns( .orElseThrow(() -> semanticException(TABLE_NOT_FOUND, allColumns, "Unable to resolve reference %s", prefix)); if (identifierChainBasis.getBasisType() == TABLE) { RelationType relationType = identifierChainBasis.getRelationType().orElseThrow(); - List fields = filterInaccessibleFields(relationType.resolveVisibleFieldsWithRelationPrefix(Optional.of(prefix))); + List requestedFields = relationType.resolveVisibleFieldsWithRelationPrefix(Optional.of(prefix)); + List fields = filterInaccessibleFields(requestedFields); if (fields.isEmpty()) { + if (!requestedFields.isEmpty()) { + throw semanticException(TABLE_NOT_FOUND, allColumns, "Relation not found or not allowed"); + } throw semanticException(COLUMN_NOT_FOUND, allColumns, "SELECT * not allowed from relation that has no columns"); } boolean local = scope.isLocalScope(identifierChainBasis.getScope().orElseThrow()); @@ -4305,11 +4309,15 @@ private void analyzeSelectAllColumns( throw semanticException(NOT_SUPPORTED, allColumns, "Column aliases not supported"); } - List fields = filterInaccessibleFields((List) scope.getRelationType().getVisibleFields()); + List requestedFields = (List) scope.getRelationType().getVisibleFields(); + List fields = filterInaccessibleFields(requestedFields); if (fields.isEmpty()) { if (node.getFrom().isEmpty()) { throw semanticException(COLUMN_NOT_FOUND, allColumns, "SELECT * not allowed in queries without FROM clause"); } + if (!requestedFields.isEmpty()) { + throw semanticException(TABLE_NOT_FOUND, allColumns, "Relation not found or not allowed"); + } throw semanticException(COLUMN_NOT_FOUND, allColumns, "SELECT * not allowed from relation that has no columns"); }