pvary commented on a change in pull request #2512:
URL: https://github.com/apache/hive/pull/2512#discussion_r677496547



##########
File path: 
iceberg/iceberg-handler/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerWithEngine.java
##########
@@ -2271,6 +2274,116 @@ public void testStatWithPartitionedCTAS() {
     checkColStat("target", "dept");
   }
 
+  @Test
+  public void testAsOfTimestamp() throws IOException, InterruptedException {
+    Table table = prepareTableWithVersions(2);
+
+    List<Object[]> rows = shell.executeStatement(
+        "SELECT * FROM customers FOR SYSTEM_TIME AS OF '" + 
timestampAfterSnapshot(table, 0) + "'");
+
+    Assert.assertEquals(3, rows.size());
+
+    rows = shell.executeStatement(
+        "SELECT * FROM customers FOR SYSTEM_TIME AS OF '" + 
timestampAfterSnapshot(table, 1) + "'");
+
+    Assert.assertEquals(4, rows.size());
+
+    AssertHelpers.assertThrows("should throw exception", 
IllegalArgumentException.class,
+        "Cannot find a snapshot older than 1970-01-01 00:00:00", () -> {
+          shell.executeStatement("SELECT * FROM customers FOR SYSTEM_TIME AS 
OF '1970-01-01 00:00:00'");
+        });
+  }
+
+  @Test
+  public void testAsOfVersion() throws IOException, InterruptedException {
+    Table table = prepareTableWithVersions(2);
+
+    HistoryEntry first = table.history().get(0);
+    List<Object[]> rows =
+        shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION AS 
OF " + first.snapshotId());
+
+    Assert.assertEquals(3, rows.size());
+
+    HistoryEntry second = table.history().get(1);
+    rows = shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION 
AS OF " + second.snapshotId());
+
+    Assert.assertEquals(4, rows.size());
+
+    AssertHelpers.assertThrows("should throw exception", 
IllegalArgumentException.class,
+        "Cannot find snapshot with ID 1234", () -> {
+          shell.executeStatement("SELECT * FROM customers FOR SYSTEM_VERSION 
AS OF 1234");
+        });
+  }
+
+  @Test
+  public void testAsOfTimestampWithJoins() throws IOException, 
InterruptedException {
+    Table table = prepareTableWithVersions(4);
+
+    List<Object[]> rows = shell.executeStatement("SELECT * FROM " +
+        "customers FOR SYSTEM_TIME AS OF '" + timestampAfterSnapshot(table, 0) 
+ "' fv, " +
+        "customers FOR SYSTEM_TIME AS OF '" + timestampAfterSnapshot(table, 1) 
+ "' sv " +
+        "WHERE fv.first_name=sv.first_name");
+
+    Assert.assertEquals(4, rows.size());
+
+    rows = shell.executeStatement("SELECT * FROM " +
+         "customers FOR SYSTEM_TIME AS OF '" + timestampAfterSnapshot(table, 
1) + "' sv, " +
+         "customers FOR SYSTEM_TIME AS OF '" + timestampAfterSnapshot(table, 
2) + "' tv " +
+         "WHERE sv.first_name=tv.first_name");
+
+    Assert.assertEquals(8, rows.size());
+
+    rows = shell.executeStatement("SELECT * FROM " +
+        "customers FOR SYSTEM_TIME AS OF '" + timestampAfterSnapshot(table, 2) 
+ "' sv, " +
+        "customers lv " +
+        "WHERE sv.first_name=lv.first_name");
+
+    Assert.assertEquals(14, rows.size());
+  }
+
+  /**
+   * Creates the 'customers' table with the default records and creates extra 
snapshots by inserting one more line
+   * into the table.
+   * @param versions The number of snapshots we want to create
+   * @return The table created
+   * @throws IOException When there is a problem during table creation
+   * @throws InterruptedException When there is a problem during adding new 
data to the table
+   */
+  private Table prepareTableWithVersions(int versions) throws IOException, 
InterruptedException {
+    Table table = testTables.createTable(shell, "customers", 
HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA,
+        fileFormat, HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS);
+
+    for (int i = 0; i < versions - 1; ++i) {
+      // Just wait a little so we definitely will not have the same timestamp 
for the snapshots
+      Thread.sleep(100);
+      shell.executeStatement("INSERT INTO customers values(" +
+          (i + HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS.size()) + 
",'Alice','Green_" + i + "')");
+    }
+
+    table.refresh();
+
+    return table;
+  }
+
+  /**
+   * Get the timestamp string which we can use in the queries. The timestamp 
will be after the given snapshot
+   * and before the next one
+   * @param table The table which we want to query
+   * @param snapshotPosition The position of the last snapshot we want to see 
in the query results
+   * @return The timestamp which we can use in the queries
+   */
+  private String timestampAfterSnapshot(Table table, int snapshotPosition) {
+    List<HistoryEntry> history = table.history();
+    long snapshotTime = history.get(snapshotPosition).timestampMillis();
+    long time = snapshotTime + 100;
+    if (history.size() > snapshotPosition + 1) {
+      time = snapshotTime + ((history.get(snapshotPosition + 
1).timestampMillis() - snapshotTime) / 2);
+    }
+
+    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss.SSS000000");

Review comment:
       > Also, maybe one more test where we insert into a table using time 
travel? Not sure it has added value, but I can imagine CTAS being used w/ time 
travel frequently
   
   Added tests for CTAS and INSERT




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to