-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for unregister_table procedure in Delta Lake #15856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.plugin.deltalake.procedure; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.trino.plugin.deltalake.DeltaLakeMetadataFactory; | ||
| import io.trino.plugin.deltalake.metastore.DeltaLakeMetastore; | ||
| import io.trino.spi.classloader.ThreadContextClassLoader; | ||
| import io.trino.spi.connector.ConnectorAccessControl; | ||
| import io.trino.spi.connector.ConnectorSession; | ||
| import io.trino.spi.connector.SchemaNotFoundException; | ||
| import io.trino.spi.connector.SchemaTableName; | ||
| import io.trino.spi.procedure.Procedure; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Provider; | ||
|
|
||
| import java.lang.invoke.MethodHandle; | ||
|
|
||
| import static com.google.common.base.Strings.isNullOrEmpty; | ||
| import static io.trino.plugin.base.util.Procedures.checkProcedureArgument; | ||
| import static io.trino.spi.type.VarcharType.VARCHAR; | ||
| import static java.lang.invoke.MethodHandles.lookup; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class UnregisterTableProcedure | ||
| implements Provider<Procedure> | ||
| { | ||
| private static final MethodHandle UNREGISTER_TABLE; | ||
|
|
||
| private static final String PROCEDURE_NAME = "unregister_table"; | ||
| private static final String SYSTEM_SCHEMA = "system"; | ||
|
|
||
| private static final String SCHEMA_NAME = "SCHEMA_NAME"; | ||
| private static final String TABLE_NAME = "TABLE_NAME"; | ||
|
|
||
| static { | ||
| try { | ||
| UNREGISTER_TABLE = lookup().unreflect(UnregisterTableProcedure.class.getMethod("unregisterTable", ConnectorAccessControl.class, ConnectorSession.class, String.class, String.class)); | ||
| } | ||
| catch (ReflectiveOperationException e) { | ||
| throw new AssertionError(e); | ||
| } | ||
| } | ||
|
|
||
| private final DeltaLakeMetadataFactory metadataFactory; | ||
|
|
||
| @Inject | ||
| public UnregisterTableProcedure(DeltaLakeMetadataFactory metadataFactory) | ||
| { | ||
| this.metadataFactory = requireNonNull(metadataFactory, "metadataFactory is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Procedure get() | ||
| { | ||
| return new Procedure( | ||
| SYSTEM_SCHEMA, | ||
| PROCEDURE_NAME, | ||
| ImmutableList.of( | ||
| new Procedure.Argument(SCHEMA_NAME, VARCHAR), | ||
| new Procedure.Argument(TABLE_NAME, VARCHAR)), | ||
| UNREGISTER_TABLE.bindTo(this)); | ||
| } | ||
|
|
||
| public void unregisterTable(ConnectorAccessControl accessControl, ConnectorSession session, String schemaName, String tableName) | ||
| { | ||
| try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(getClass().getClassLoader())) { | ||
| doUnregisterTable(accessControl, session, schemaName, tableName); | ||
| } | ||
| } | ||
|
|
||
| private void doUnregisterTable(ConnectorAccessControl accessControl, ConnectorSession session, String schemaName, String tableName) | ||
| { | ||
| checkProcedureArgument(!isNullOrEmpty(schemaName), "schema_name cannot be null or empty"); | ||
| checkProcedureArgument(!isNullOrEmpty(tableName), "table_name cannot be null or empty"); | ||
| SchemaTableName schemaTableName = new SchemaTableName(schemaName, tableName); | ||
|
|
||
| accessControl.checkCanDropTable(null, schemaTableName); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is null safe here, or do we need the context?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument must be null if I understand correctly.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, that's exactly what I was looking for. Thanks |
||
| DeltaLakeMetastore metastore = metadataFactory.create(session.getIdentity()).getMetastore(); | ||
|
|
||
| if (metastore.getDatabase(schemaName).isEmpty()) { | ||
| throw new SchemaNotFoundException(schemaTableName.getSchemaName()); | ||
| } | ||
|
|
||
| metastore.dropTable(session, schemaName, tableName, false); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.tests.product.deltalake; | ||
|
|
||
| import io.trino.testng.services.Flaky; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static io.trino.tempto.assertions.QueryAssert.Row.row; | ||
| import static io.trino.tempto.assertions.QueryAssert.assertQueryFailure; | ||
| import static io.trino.tempto.assertions.QueryAssert.assertThat; | ||
| import static io.trino.testing.TestingNames.randomNameSuffix; | ||
| import static io.trino.tests.product.TestGroups.DELTA_LAKE_DATABRICKS; | ||
| import static io.trino.tests.product.TestGroups.DELTA_LAKE_OSS; | ||
| import static io.trino.tests.product.TestGroups.PROFILE_SPECIFIC_TESTS; | ||
| import static io.trino.tests.product.deltalake.util.DeltaLakeTestUtils.DATABRICKS_COMMUNICATION_FAILURE_ISSUE; | ||
| import static io.trino.tests.product.deltalake.util.DeltaLakeTestUtils.DATABRICKS_COMMUNICATION_FAILURE_MATCH; | ||
| import static io.trino.tests.product.utils.QueryExecutors.onDelta; | ||
| import static io.trino.tests.product.utils.QueryExecutors.onTrino; | ||
| import static java.lang.String.format; | ||
|
|
||
| public class TestDeltaLakeProceduresCompatibility | ||
| extends BaseTestDeltaLakeS3Storage | ||
| { | ||
| @Test(groups = {DELTA_LAKE_DATABRICKS, DELTA_LAKE_OSS, PROFILE_SPECIFIC_TESTS}) | ||
| @Flaky(issue = DATABRICKS_COMMUNICATION_FAILURE_ISSUE, match = DATABRICKS_COMMUNICATION_FAILURE_MATCH) | ||
| public void testUnregisterTable() | ||
| { | ||
| String tableName = "test_dl_unregister_table" + randomNameSuffix(); | ||
| String tableDirectory = "databricks-compatibility-test-" + tableName; | ||
|
|
||
| onTrino().executeQuery(format("CREATE TABLE delta.default.%s WITH (location = 's3://%s/%s') AS SELECT 123 AS col", | ||
| tableName, | ||
| bucketName, | ||
| tableDirectory)); | ||
| try { | ||
| assertThat(onTrino().executeQuery("SELECT * FROM delta.default." + tableName)).containsOnly(row(123)); | ||
| assertThat(onDelta().executeQuery("SELECT * FROM default." + tableName)).containsOnly(row(123)); | ||
|
|
||
| onTrino().executeQuery("CALL delta.system.unregister_table('default', '" + tableName + "')"); | ||
|
|
||
| assertQueryFailure(() -> onTrino().executeQuery("SELECT * FROM delta.default." + tableName)) | ||
| .hasMessageMatching(".* Table '.*' does not exist"); | ||
| assertQueryFailure(() -> onDelta().executeQuery("SELECT * FROM default." + tableName)) | ||
| .hasMessageMatching("(?s).* Table or view not found: .*"); | ||
| } | ||
| finally { | ||
| onTrino().executeQuery("DROP TABLE IF EXISTS delta.default." + tableName); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.