Skip to content

Commit

Permalink
Spark 3.3, 3.4, 3.5: Supplement test case for `RollbackToTimestampPro…
Browse files Browse the repository at this point in the history
…cedure` (apache#11171)
  • Loading branch information
hantangwangd authored Sep 20, 2024
1 parent d4af40c commit b2b65df
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -252,6 +254,39 @@ public void testRollbackToTimestampWithoutExplicitCatalog() {
sql("SELECT * FROM %s ORDER BY id", tableName));
}

@Test
public void testRollbackToTimestampBeforeOrEqualToOldestSnapshot() {
sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", tableName);
sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);

Table table = validationCatalog.loadTable(tableIdent);
Snapshot firstSnapshot = table.currentSnapshot();
Timestamp beforeFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis() - 1));
Timestamp exactFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis()));

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, beforeFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
beforeFirstSnapshot.toInstant().toEpochMilli());

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, exactFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
exactFirstSnapshot.toInstant().toEpochMilli());
}

@Test
public void testInvalidRollbackToTimestampCases() {
String timestamp = "TIMESTAMP '2007-12-03T10:15:30'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -252,6 +254,39 @@ public void testRollbackToTimestampWithoutExplicitCatalog() {
sql("SELECT * FROM %s ORDER BY id", tableName));
}

@Test
public void testRollbackToTimestampBeforeOrEqualToOldestSnapshot() {
sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", tableName);
sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);

Table table = validationCatalog.loadTable(tableIdent);
Snapshot firstSnapshot = table.currentSnapshot();
Timestamp beforeFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis() - 1));
Timestamp exactFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis()));

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, beforeFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
beforeFirstSnapshot.toInstant().toEpochMilli());

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, exactFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
exactFirstSnapshot.toInstant().toEpochMilli());
}

@Test
public void testInvalidRollbackToTimestampCases() {
String timestamp = "TIMESTAMP '2007-12-03T10:15:30'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assumptions.assumeThat;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import org.apache.iceberg.Snapshot;
Expand Down Expand Up @@ -246,6 +248,39 @@ public void testRollbackToTimestampWithoutExplicitCatalog() {
sql("SELECT * FROM %s ORDER BY id", tableName));
}

@TestTemplate
public void testRollbackToTimestampBeforeOrEqualToOldestSnapshot() {
sql("CREATE TABLE %s (id bigint NOT NULL, data string) USING iceberg", tableName);
sql("INSERT INTO TABLE %s VALUES (1, 'a')", tableName);

Table table = validationCatalog.loadTable(tableIdent);
Snapshot firstSnapshot = table.currentSnapshot();
Timestamp beforeFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis() - 1));
Timestamp exactFirstSnapshot =
Timestamp.from(Instant.ofEpochMilli(firstSnapshot.timestampMillis()));

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, beforeFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
beforeFirstSnapshot.toInstant().toEpochMilli());

assertThatThrownBy(
() ->
sql(
"CALL %s.system.rollback_to_timestamp(timestamp => TIMESTAMP '%s', table => '%s')",
catalogName, exactFirstSnapshot, tableIdent))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
"Cannot roll back, no valid snapshot older than: %s",
exactFirstSnapshot.toInstant().toEpochMilli());
}

@TestTemplate
public void testInvalidRollbackToTimestampCases() {
String timestamp = "TIMESTAMP '2007-12-03T10:15:30'";
Expand Down

0 comments on commit b2b65df

Please sign in to comment.