From 25ab36921b31ee8ffa1aae98870a5dfb7095a36c Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Fri, 18 Aug 2023 14:16:34 +0200 Subject: [PATCH] Skip getTableHandle call in information_schema.columns with table filter When reading `information_schema.columns` with a filter on table name (either `=` or `IN`), the code was doing `getRedirectionAwareTableHandle` call to check whether target table exists. However, the redirected tables are handled in `doListTableColumns` (also catching `TABLE_REDIRECTION_ERROR`), so the call inside `calculatePrefixesWithTableName` is redundant. --- .../InformationSchemaMetadata.java | 25 ------------------- .../tests/TestInformationSchemaConnector.java | 10 ++++---- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/connector/informationschema/InformationSchemaMetadata.java b/core/trino-main/src/main/java/io/trino/connector/informationschema/InformationSchemaMetadata.java index 61d50f444f4a..0b846d915a9b 100644 --- a/core/trino-main/src/main/java/io/trino/connector/informationschema/InformationSchemaMetadata.java +++ b/core/trino-main/src/main/java/io/trino/connector/informationschema/InformationSchemaMetadata.java @@ -22,7 +22,6 @@ import io.trino.metadata.Metadata; import io.trino.metadata.QualifiedObjectName; import io.trino.metadata.QualifiedTablePrefix; -import io.trino.spi.TrinoException; import io.trino.spi.connector.ColumnHandle; import io.trino.spi.connector.ColumnMetadata; import io.trino.spi.connector.ConnectorMetadata; @@ -64,7 +63,6 @@ import static io.trino.connector.informationschema.InformationSchemaTable.TABLE_PRIVILEGES; import static io.trino.connector.informationschema.InformationSchemaTable.VIEWS; import static io.trino.metadata.MetadataUtil.findColumnMetadata; -import static io.trino.spi.StandardErrorCode.TABLE_REDIRECTION_ERROR; import static io.trino.spi.type.VarcharType.createUnboundedVarcharType; import static java.util.Collections.emptyList; import static java.util.Locale.ENGLISH; @@ -278,29 +276,6 @@ private Set calculatePrefixesWithTableName( .flatMap(prefix -> tables.get().stream() .filter(this::isLowerCase) .map(table -> new QualifiedObjectName(catalogName, prefix.getSchemaName().get(), table))) - .filter(objectName -> { - if (!isColumnsEnumeratingTable(informationSchemaTable) || - metadata.isMaterializedView(session, objectName) || - metadata.isView(session, objectName)) { - return true; - } - - // This is a columns enumerating table and the object is not a view - try { - // Table redirection to enumerate columns from target table happens later in - // MetadataListing#listTableColumns, but also applying it here to avoid incorrect - // filtering in case the source table does not exist or there is a problem with redirection. - return metadata.getRedirectionAwareTableHandle(session, objectName).tableHandle().isPresent(); - } - catch (TrinoException e) { - if (e.getErrorCode().equals(TABLE_REDIRECTION_ERROR.toErrorCode())) { - // Ignore redirection errors for listing, treat as if the table does not exist - return false; - } - - throw e; - } - }) .filter(objectName -> predicate.isEmpty() || predicate.get().test(asFixedValues(objectName))) .map(QualifiedObjectName::asQualifiedTablePrefix) .distinct() diff --git a/testing/trino-tests/src/test/java/io/trino/tests/TestInformationSchemaConnector.java b/testing/trino-tests/src/test/java/io/trino/tests/TestInformationSchemaConnector.java index b32d698b71d0..093b689adbc2 100644 --- a/testing/trino-tests/src/test/java/io/trino/tests/TestInformationSchemaConnector.java +++ b/testing/trino-tests/src/test/java/io/trino/tests/TestInformationSchemaConnector.java @@ -249,11 +249,11 @@ public void testMetadataCalls() "SELECT count(*) from test_catalog.information_schema.columns WHERE table_schema = 'test_schema1' AND table_name = 'test_table1'", "VALUES 100", ImmutableMultiset.builder() - .addCopies("ConnectorMetadata.getSystemTable(schema=test_schema1, table=test_table1)", 8) - .addCopies("ConnectorMetadata.getMaterializedView(schema=test_schema1, table=test_table1)", 2) - .addCopies("ConnectorMetadata.getView(schema=test_schema1, table=test_table1)", 2) - .addCopies("ConnectorMetadata.redirectTable(schema=test_schema1, table=test_table1)", 2) - .addCopies("ConnectorMetadata.getTableHandle(schema=test_schema1, table=test_table1)", 2) + .addCopies("ConnectorMetadata.getSystemTable(schema=test_schema1, table=test_table1)", 4) + .add("ConnectorMetadata.getMaterializedView(schema=test_schema1, table=test_table1)") + .add("ConnectorMetadata.getView(schema=test_schema1, table=test_table1)") + .add("ConnectorMetadata.redirectTable(schema=test_schema1, table=test_table1)") + .add("ConnectorMetadata.getTableHandle(schema=test_schema1, table=test_table1)") .add("ConnectorMetadata.getTableMetadata(handle=test_schema1.test_table1)") .build()); assertMetadataCalls(