forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
4 deletions.
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") | ||
} | ||
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()) | ||
} | ||
} |