-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark 3.3: SQL Extensions for DROP TAG #6807
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
65aec46
059a563
5249fe6
7c211da
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,33 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.spark.sql.catalyst.plans.logical | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
|
|
||
| case class DropTag(table: Seq[String], tag: String, ifExists: Boolean) extends LeafCommand { | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
|
|
||
| override lazy val output: Seq[Attribute] = Nil | ||
|
|
||
| override def simpleString(maxFields: Int): String = { | ||
| s"DropTag tag: ${tag} for table: ${table.quoted}" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.spark.sql.execution.datasources.v2 | ||
|
|
||
| import org.apache.iceberg.spark.source.SparkTable | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.connector.catalog.Identifier | ||
| import org.apache.spark.sql.connector.catalog.TableCatalog | ||
|
|
||
| case class DropTagExec( | ||
| catalog: TableCatalog, | ||
| ident: Identifier, | ||
| tag: String, | ||
| ifExists: Boolean) extends LeafV2CommandExec { | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
|
|
||
| override lazy val output: Seq[Attribute] = Nil | ||
|
|
||
| override protected def run(): Seq[InternalRow] = { | ||
| catalog.loadTable(ident) match { | ||
| case iceberg: SparkTable => | ||
| val ref = iceberg.table().refs().get(tag) | ||
| if (ref != null || !ifExists) { | ||
| iceberg.table().manageSnapshots().removeTag(tag).commit() | ||
| } | ||
|
|
||
| case table => | ||
| throw new UnsupportedOperationException(s"Cannot drop tag on non-Iceberg table: $table") | ||
| } | ||
|
|
||
| Nil | ||
| } | ||
|
|
||
| override def simpleString(maxFields: Int): String = { | ||
| s"DropTag tag: ${tag} for table: ${ident.quoted}" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,8 +85,10 @@ public void testCreateTagWithRetain() throws NoSuchTableException { | |
| tableName, tagName, firstSnapshotId, maxRefAge, timeUnit); | ||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(firstSnapshotId, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", firstSnapshotId, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to have the correct max ref age.", | ||
| TimeUnit.valueOf(timeUnit.toUpperCase(Locale.ENGLISH)).toMillis(maxRefAge), | ||
| ref.maxRefAgeMs().longValue()); | ||
| } | ||
|
|
@@ -132,8 +134,10 @@ public void testCreateTagUseDefaultConfig() throws NoSuchTableException { | |
| sql("ALTER TABLE %s CREATE TAG %s", tableName, tagName); | ||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(snapshotId, ref.snapshotId()); | ||
| Assert.assertNull(ref.maxRefAgeMs()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", snapshotId, ref.snapshotId()); | ||
| Assert.assertNull( | ||
| "The tag needs to have the default max ref age, which is null.", ref.maxRefAgeMs()); | ||
|
|
||
| AssertHelpers.assertThrows( | ||
| "Cannot create an exist tag", | ||
|
|
@@ -156,8 +160,10 @@ public void testCreateTagUseDefaultConfig() throws NoSuchTableException { | |
| sql("ALTER TABLE %s CREATE TAG %s AS OF VERSION %d", tableName, tagName, snapshotId); | ||
| table.refresh(); | ||
| ref = table.refs().get(tagName); | ||
| Assert.assertEquals(snapshotId, ref.snapshotId()); | ||
| Assert.assertNull(ref.maxRefAgeMs()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", snapshotId, ref.snapshotId()); | ||
| Assert.assertNull( | ||
| "The tag needs to have the default max ref age, which is null.", ref.maxRefAgeMs()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -170,8 +176,14 @@ public void testCreateTagIfNotExists() throws NoSuchTableException { | |
|
|
||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(table.currentSnapshot().snapshotId(), ref.snapshotId()); | ||
| Assert.assertEquals(TimeUnit.DAYS.toMillis(maxSnapshotAge), ref.maxRefAgeMs().longValue()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", | ||
| table.currentSnapshot().snapshotId(), | ||
| ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to have the correct max ref age.", | ||
| TimeUnit.DAYS.toMillis(maxSnapshotAge), | ||
| ref.maxRefAgeMs().longValue()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -208,8 +220,12 @@ public void testReplaceTag() throws NoSuchTableException { | |
| sql("ALTER TABLE %s REPLACE Tag %s AS OF VERSION %d", tableName, tagName, second); | ||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(second, ref.snapshotId()); | ||
| Assert.assertEquals(expectedMaxRefAgeMs, ref.maxRefAgeMs().longValue()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", second, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to have the correct max ref age.", | ||
| expectedMaxRefAgeMs, | ||
| ref.maxRefAgeMs().longValue()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -243,9 +259,12 @@ public void testReplaceTagWithRetain() throws NoSuchTableException { | |
|
|
||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(second, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| TimeUnit.valueOf(timeUnit).toMillis(maxRefAge), ref.maxRefAgeMs().longValue()); | ||
| "The tag needs to point to a specific snapshot id.", second, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to have the correct max ref age.", | ||
| TimeUnit.valueOf(timeUnit).toMillis(maxRefAge), | ||
| ref.maxRefAgeMs().longValue()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -261,7 +280,78 @@ public void testCreateOrReplace() throws NoSuchTableException { | |
| sql("ALTER TABLE %s CREATE OR REPLACE TAG %s AS OF VERSION %d", tableName, tagName, first); | ||
| table.refresh(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals(first, ref.snapshotId()); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", first, ref.snapshotId()); | ||
| } | ||
|
|
||
| @Test | ||
|
Contributor
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. I think we should also have a test to ensure that DROP TAG does not work when the ref is a branch. If I missed the opposite case for DROP BRANCH, I think let's add that as well for completeness. |
||
| public void testDropTag() throws NoSuchTableException { | ||
| insertRows(); | ||
| Table table = validationCatalog.loadTable(tableIdent); | ||
| String tagName = "t1"; | ||
| table.manageSnapshots().createTag(tagName, table.currentSnapshot().snapshotId()).commit(); | ||
| SnapshotRef ref = table.refs().get(tagName); | ||
| Assert.assertEquals( | ||
| "The tag needs to point to a specific snapshot id.", | ||
| table.currentSnapshot().snapshotId(), | ||
| ref.snapshotId()); | ||
|
|
||
| sql("ALTER TABLE %s DROP TAG %s", tableName, tagName); | ||
| table.refresh(); | ||
| ref = table.refs().get(tagName); | ||
| Assert.assertNull("The tag needs to be dropped.", ref); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDropTagNonConformingName() { | ||
| AssertHelpers.assertThrows( | ||
| "Non-conforming tag name", | ||
| IcebergParseException.class, | ||
| "mismatched input '123'", | ||
| () -> sql("ALTER TABLE %s DROP TAG %s", tableName, "123")); | ||
|
Comment on lines
+307
to
+311
Contributor
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. I think it's fine to include this test but could we separate it and have this test just focused on the happy case?
Contributor
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. +1 |
||
| } | ||
|
|
||
| @Test | ||
| public void testDropTagDoesNotExist() { | ||
| AssertHelpers.assertThrows( | ||
| "Cannot perform drop tag on tag which does not exist", | ||
| IllegalArgumentException.class, | ||
| "Tag does not exist: nonExistingTag", | ||
| () -> sql("ALTER TABLE %s DROP TAG %s", tableName, "nonExistingTag")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDropTagFailesForBranch() throws NoSuchTableException { | ||
| String branchName = "b1"; | ||
| Table table = insertRows(); | ||
| table.manageSnapshots().createBranch(branchName, table.currentSnapshot().snapshotId()).commit(); | ||
|
|
||
| AssertHelpers.assertThrows( | ||
| "Cannot perform drop tag on branch", | ||
| IllegalArgumentException.class, | ||
| "Ref b1 is a branch not a tag", | ||
| () -> sql("ALTER TABLE %s DROP TAG %s", tableName, branchName)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDropTagIfExists() throws NoSuchTableException { | ||
| String tagName = "nonExistingTag"; | ||
| Table table = insertRows(); | ||
| Assert.assertNull("The tag does not exists.", table.refs().get(tagName)); | ||
|
|
||
| sql("ALTER TABLE %s DROP TAG IF EXISTS %s", tableName, tagName); | ||
| table.refresh(); | ||
| Assert.assertNull("The tag still does not exist.", table.refs().get(tagName)); | ||
|
|
||
| table.manageSnapshots().createTag(tagName, table.currentSnapshot().snapshotId()).commit(); | ||
| Assert.assertEquals( | ||
| "The tag has been created successfully.", | ||
| table.currentSnapshot().snapshotId(), | ||
| table.refs().get(tagName).snapshotId()); | ||
|
|
||
| sql("ALTER TABLE %s DROP TAG IF EXISTS %s", tableName, tagName); | ||
| table.refresh(); | ||
| Assert.assertNull("The tag needs to be dropped.", table.refs().get(tagName)); | ||
| } | ||
|
|
||
| private Table insertRows() throws NoSuchTableException { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be written like :
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we discussed this in #6637 (comment), and the conclusion was that it is more clear to have them separated.
The current way for separated DROP BRANCH and DROP TAG makes the logic consistent with CREATE.
Let me know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, was mostly coming from spark code base, considering this example :
https://github.com/apache/spark/blob/46a234125d3f125ba1f9ccd6af0ec1ba61016c1e/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4#L125
If we have reached the concencus, then it should be fine.