-
Notifications
You must be signed in to change notification settings - Fork 997
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
[spark] Add rollback ddl syntax #4462
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ommon/src/main/scala/org/apache/paimon/spark/catalyst/plans/logical/RollbackCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.paimon.spark.catalyst.plans.logical | ||
|
||
import org.apache.paimon.spark.leafnode.PaimonLeafCommand | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} | ||
import org.apache.spark.sql.types.StringType | ||
|
||
case class RollbackCommand( | ||
table: Seq[String], | ||
kind: String, | ||
version: String, | ||
override val output: Seq[Attribute] = RollbackCommand.getOutputAttrs) | ||
extends PaimonLeafCommand { | ||
|
||
override def simpleString(maxFields: Int): String = { | ||
s"Rollback to $kind '$version' for table: $table" | ||
} | ||
} | ||
|
||
object RollbackCommand { | ||
private def getOutputAttrs: Seq[Attribute] = { | ||
Seq(AttributeReference("result", StringType, nullable = false)()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...k/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/RollbackExec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.paimon.spark.execution | ||
|
||
import org.apache.paimon.spark.SparkTable | ||
import org.apache.paimon.spark.leafnode.PaimonLeafV2CommandExec | ||
import org.apache.paimon.table.FileStoreTable | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.Attribute | ||
import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog} | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
import java.util.Locale | ||
|
||
case class RollbackExec( | ||
catalog: TableCatalog, | ||
ident: Identifier, | ||
output: Seq[Attribute], | ||
kind: String, | ||
version: String) | ||
extends PaimonLeafV2CommandExec { | ||
|
||
override protected def run(): Seq[InternalRow] = { | ||
val table = catalog.loadTable(ident) | ||
assert(table.isInstanceOf[SparkTable]) | ||
|
||
table.asInstanceOf[SparkTable].getTable match { | ||
case paimonTable: FileStoreTable => | ||
kind.toUpperCase(Locale.ROOT) match { | ||
|
||
case "SNAPSHOT" => | ||
assert(version.chars.allMatch(Character.isDigit)) | ||
paimonTable.rollbackTo(version.toLong) | ||
|
||
case "TAG" => | ||
paimonTable.rollbackTo(version) | ||
|
||
case "TIMESTAMP" => | ||
assert(version.chars.allMatch(Character.isDigit)) | ||
val snapshot = paimonTable.snapshotManager.earlierOrEqualTimeMills(version.toLong) | ||
assert(snapshot != null) | ||
paimonTable.rollbackTo(snapshot.id); | ||
|
||
case _ => | ||
throw new UnsupportedOperationException(s"Unsupported rollback kind '$kind'.") | ||
} | ||
case t => | ||
throw new UnsupportedOperationException( | ||
s"Can not delete tag for non-paimon FileStoreTable: $t") | ||
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. Can not delete tag? 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. my mistake, updated, ths |
||
} | ||
Seq(InternalRow(UTF8String.fromString("success"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...-spark/paimon-spark-common/src/test/scala/org/apache/paimon/spark/sql/PaimonDdlTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.paimon.spark.sql | ||
|
||
import org.apache.paimon.spark.PaimonSparkTestBase | ||
|
||
import org.apache.spark.sql.Row | ||
|
||
class PaimonDdlTest extends PaimonSparkTestBase { | ||
|
||
test("Paimon ddl: alter table t rollback syntax") { | ||
spark.sql("""CREATE TABLE T (id INT, name STRING) | ||
|USING PAIMON | ||
|TBLPROPERTIES ('primary-key'='id')""".stripMargin) | ||
|
||
val table = loadTable("T") | ||
val snapshotManager = table.snapshotManager() | ||
|
||
spark.sql("insert into T values(1, 'a')") | ||
val timestamp = System.currentTimeMillis() | ||
|
||
spark.sql("insert into T values(2, 'b')") | ||
spark.sql("insert into T values(3, 'c')") | ||
spark.sql("insert into T values(4, 'd')") | ||
assertResult(4)(snapshotManager.snapshotCount()) | ||
|
||
// rollback to snapshot | ||
checkAnswer(spark.sql("alter table T rollback to snapshot `3`"), Row("success") :: Nil) | ||
assertResult(3)(snapshotManager.latestSnapshotId()) | ||
|
||
// create a tag based on snapshot-2 | ||
spark.sql("alter table T create tag `test-tag` as of version 2") | ||
checkAnswer(spark.sql("show tags T"), Row("test-tag") :: Nil) | ||
|
||
// rollback to tag | ||
checkAnswer(spark.sql("alter table T rollback to tag `test-tag`"), Row("success") :: Nil) | ||
assertResult(2)(snapshotManager.latestSnapshotId()) | ||
|
||
// rollback to timestamp | ||
checkAnswer( | ||
spark.sql(s"alter table T rollback to timestamp `$timestamp`"), | ||
Row("success") :: Nil) | ||
assertResult(1)(snapshotManager.latestSnapshotId()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
timeTravelKind?
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.
changed.