Skip to content

Commit f3e0a0d

Browse files
authored
[spark] Fix rollback not correctly identify tag or snapshot (#4947)
1 parent 7b63b5e commit f3e0a0d

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

docs/content/spark/procedures.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,17 @@ This section introduce all available spark procedures about paimon.
159159
<tr>
160160
<td>rollback</td>
161161
<td>
162-
To rollback to a specific version of target table. Argument:
162+
To rollback to a specific version of target table, note version/snapshot/tag must set one of them. Argument:
163163
<li>table: the target table identifier. Cannot be empty.</li>
164-
<li>version: id of the snapshot or name of tag that will roll back to.</li>
164+
<li>version: id of the snapshot or name of tag that will roll back to, version would be Deprecated.</li>
165+
<li>snapshot: snapshot that will roll back to.</li>
166+
<li>tag: tag that will roll back to.</li>
165167
</td>
166168
<td>
167169
CALL sys.rollback(table => 'default.T', version => 'my_tag')<br/><br/>
168-
CALL sys.rollback(table => 'default.T', version => 10)
170+
CALL sys.rollback(table => 'default.T', version => 10)<br/><br/>
171+
CALL sys.rollback(table => 'default.T', tag => 'tag1')
172+
CALL sys.rollback(table => 'default.T', snapshot => 2)
169173
</td>
170174
</tr>
171175
<tr>

paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/procedure/RollbackProcedure.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
package org.apache.paimon.spark.procedure;
2020

21+
import org.apache.paimon.utils.Preconditions;
22+
import org.apache.paimon.utils.StringUtils;
23+
2124
import org.apache.spark.sql.catalyst.InternalRow;
2225
import org.apache.spark.sql.connector.catalog.Identifier;
2326
import org.apache.spark.sql.connector.catalog.TableCatalog;
@@ -26,6 +29,7 @@
2629
import org.apache.spark.sql.types.StructField;
2730
import org.apache.spark.sql.types.StructType;
2831

32+
import static org.apache.spark.sql.types.DataTypes.LongType;
2933
import static org.apache.spark.sql.types.DataTypes.StringType;
3034

3135
/** A procedure to rollback to a snapshot or a tag. */
@@ -35,7 +39,9 @@ public class RollbackProcedure extends BaseProcedure {
3539
new ProcedureParameter[] {
3640
ProcedureParameter.required("table", StringType),
3741
// snapshot id or tag name
38-
ProcedureParameter.required("version", StringType)
42+
ProcedureParameter.optional("version", StringType),
43+
ProcedureParameter.optional("snapshot", LongType),
44+
ProcedureParameter.optional("tag", StringType)
3945
};
4046

4147
private static final StructType OUTPUT_TYPE =
@@ -61,15 +67,35 @@ public StructType outputType() {
6167
@Override
6268
public InternalRow[] call(InternalRow args) {
6369
Identifier tableIdent = toIdentifier(args.getString(0), PARAMETERS[0].name());
64-
String version = args.getString(1);
70+
String version = args.isNullAt(1) ? null : args.getString(1);
6571

6672
return modifyPaimonTable(
6773
tableIdent,
6874
table -> {
69-
if (version.chars().allMatch(Character::isDigit)) {
70-
table.rollbackTo(Long.parseLong(version));
75+
Long snapshot = null;
76+
String tag = null;
77+
if (!StringUtils.isNullOrWhitespaceOnly(version)) {
78+
Preconditions.checkState(
79+
args.isNullAt(2) && args.isNullAt(3),
80+
"only can set one of version/snapshot/tag in RollbackProcedure.");
81+
if (version.chars().allMatch(Character::isDigit)) {
82+
snapshot = Long.parseLong(version);
83+
} else {
84+
tag = version;
85+
}
86+
} else {
87+
Preconditions.checkState(
88+
(args.isNullAt(2) && !args.isNullAt(3)
89+
|| !args.isNullAt(2) && args.isNullAt(3)),
90+
"only can set one of version/snapshot/tag in RollbackProcedure.");
91+
snapshot = args.isNullAt(2) ? null : args.getLong(2);
92+
tag = args.isNullAt(3) ? null : args.getString(3);
93+
}
94+
95+
if (snapshot != null) {
96+
table.rollbackTo(snapshot);
7197
} else {
72-
table.rollbackTo(version);
98+
table.rollbackTo(tag);
7399
}
74100
InternalRow outputRow = newInternalRow(true);
75101
return new InternalRow[] {outputRow};

paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/procedure/RollbackProcedureTest.scala

+60
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,66 @@ class RollbackProcedureTest extends PaimonSparkTestBase with StreamTest {
9494
}
9595
}
9696

97+
test("Paimon Procedure: rollback to tag check test") {
98+
spark.sql(s"""
99+
|CREATE TABLE T (a INT, b STRING)
100+
|TBLPROPERTIES ('primary-key'='a', 'bucket'='3', 'file.format'='orc')
101+
|""".stripMargin)
102+
103+
val query = () => spark.sql("SELECT * FROM T ORDER BY a")
104+
105+
// snapshot-1
106+
spark.sql("insert into T select 1, 'a'")
107+
checkAnswer(query(), Row(1, "a") :: Nil)
108+
109+
checkAnswer(
110+
spark.sql("CALL paimon.sys.create_tag(table => 'test.T', tag => '20250122', snapshot => 1)"),
111+
Row(true) :: Nil)
112+
113+
// snapshot-2
114+
spark.sql("insert into T select 2, 'b'")
115+
checkAnswer(query(), Row(1, "a") :: Row(2, "b") :: Nil)
116+
117+
// snapshot-3
118+
spark.sql("insert into T select 3, 'c'")
119+
checkAnswer(query(), Row(1, "a") :: Row(2, "b") :: Row(3, "c") :: Nil)
120+
121+
// snapshot-4
122+
spark.sql("insert into T select 4, 'd'")
123+
checkAnswer(query(), Row(1, "a") :: Row(2, "b") :: Row(3, "c") :: Row(4, "d") :: Nil)
124+
125+
assertThrows[RuntimeException] {
126+
spark.sql("CALL paimon.sys.rollback(table => 'test.T_exception', version => '4')")
127+
}
128+
// rollback to snapshot
129+
checkAnswer(
130+
spark.sql("CALL paimon.sys.rollback(table => 'test.T', version => '3')"),
131+
Row(true) :: Nil)
132+
checkAnswer(query(), Row(1, "a") :: Row(2, "b") :: Row(3, "c") :: Nil)
133+
134+
// version/snapshot/tag can only set one of them
135+
assertThrows[RuntimeException] {
136+
spark.sql(
137+
"CALL paimon.sys.rollback(table => 'test.T', version => '20250122', tag => '20250122')")
138+
}
139+
140+
assertThrows[RuntimeException] {
141+
spark.sql("CALL paimon.sys.rollback(table => 'test.T', version => '20250122', snapshot => 1)")
142+
}
143+
144+
assertThrows[RuntimeException] {
145+
spark.sql("CALL paimon.sys.rollback(table => 'test.T', tag => '20250122', snapshot => 1)")
146+
}
147+
148+
// rollback to snapshot
149+
spark.sql("CALL paimon.sys.rollback(table => 'test.T', snapshot => 2)")
150+
checkAnswer(query(), Row(1, "a") :: Row(2, "b") :: Nil)
151+
152+
// rollback to tag
153+
spark.sql("CALL paimon.sys.rollback(table => 'test.T', tag => '20250122')")
154+
checkAnswer(query(), Row(1, "a") :: Nil)
155+
}
156+
97157
test("Paimon Procedure: rollback to timestamp") {
98158
failAfter(streamingTimeout) {
99159
withTempDir {

0 commit comments

Comments
 (0)