Search in sources :

Example 1 with DistributionMode

use of org.apache.iceberg.DistributionMode in project iceberg by apache.

the class TestMerge method testMergeWithDaysTransform.

@Test
public void testMergeWithDaysTransform() {
    for (DistributionMode mode : DistributionMode.values()) {
        createAndInitTable("id INT, ts TIMESTAMP");
        sql("ALTER TABLE %s ADD PARTITION FIELD days(ts)", tableName);
        sql("ALTER TABLE %s SET TBLPROPERTIES('%s' '%s')", tableName, WRITE_DISTRIBUTION_MODE, mode.modeName());
        append(tableName, "id INT, ts TIMESTAMP", "{ \"id\": 1, \"ts\": \"2000-01-01 00:00:00\" }\n" + "{ \"id\": 6, \"ts\": \"2000-01-06 00:00:00\" }");
        createOrReplaceView("source", "id INT, ts TIMESTAMP", "{ \"id\": 2, \"ts\": \"2001-01-02 00:00:00\" }\n" + "{ \"id\": 1, \"ts\": \"2001-01-01 00:00:00\" }\n" + "{ \"id\": 6, \"ts\": \"2001-01-06 00:00:00\" }");
        sql("MERGE INTO %s AS t USING source AS s " + "ON t.id == s.id " + "WHEN MATCHED AND t.id = 1 THEN " + "  UPDATE SET * " + "WHEN MATCHED AND t.id = 6 THEN " + "  DELETE " + "WHEN NOT MATCHED AND s.id = 2 THEN " + "  INSERT *", tableName);
        ImmutableList<Object[]> expectedRows = ImmutableList.of(// updated
        row(1, "2001-01-01 00:00:00"), // new
        row(2, "2001-01-02 00:00:00"));
        assertEquals("Should have expected rows", expectedRows, sql("SELECT id, CAST(ts AS STRING) FROM %s ORDER BY id", tableName));
        removeTables();
    }
}
Also used : DistributionMode(org.apache.iceberg.DistributionMode) Test(org.junit.Test)

Example 2 with DistributionMode

use of org.apache.iceberg.DistributionMode in project iceberg by apache.

the class TestMerge method testMergeWithBucketTransform.

@Test
public void testMergeWithBucketTransform() {
    for (DistributionMode mode : DistributionMode.values()) {
        createAndInitTable("id INT, dep STRING");
        sql("ALTER TABLE %s ADD PARTITION FIELD bucket(2, dep)", tableName);
        sql("ALTER TABLE %s SET TBLPROPERTIES('%s' '%s')", tableName, WRITE_DISTRIBUTION_MODE, mode.modeName());
        append(tableName, "{ \"id\": 1, \"dep\": \"emp-id-one\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        createOrReplaceView("source", "id INT, dep STRING", "{ \"id\": 2, \"dep\": \"emp-id-2\" }\n" + "{ \"id\": 1, \"dep\": \"emp-id-1\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        sql("MERGE INTO %s AS t USING source AS s " + "ON t.id == s.id " + "WHEN MATCHED AND t.id = 1 THEN " + "  UPDATE SET * " + "WHEN MATCHED AND t.id = 6 THEN " + "  DELETE " + "WHEN NOT MATCHED AND s.id = 2 THEN " + "  INSERT *", tableName);
        ImmutableList<Object[]> expectedRows = ImmutableList.of(// updated
        row(1, "emp-id-1"), // new
        row(2, "emp-id-2"));
        assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s ORDER BY id", tableName));
        removeTables();
    }
}
Also used : DistributionMode(org.apache.iceberg.DistributionMode) Test(org.junit.Test)

Example 3 with DistributionMode

use of org.apache.iceberg.DistributionMode in project iceberg by apache.

the class TestMerge method testMergeWithTruncateTransform.

@Test
public void testMergeWithTruncateTransform() {
    for (DistributionMode mode : DistributionMode.values()) {
        createAndInitTable("id INT, dep STRING");
        sql("ALTER TABLE %s ADD PARTITION FIELD truncate(dep, 2)", tableName);
        sql("ALTER TABLE %s SET TBLPROPERTIES('%s' '%s')", tableName, WRITE_DISTRIBUTION_MODE, mode.modeName());
        append(tableName, "{ \"id\": 1, \"dep\": \"emp-id-one\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        createOrReplaceView("source", "id INT, dep STRING", "{ \"id\": 2, \"dep\": \"emp-id-2\" }\n" + "{ \"id\": 1, \"dep\": \"emp-id-1\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        sql("MERGE INTO %s AS t USING source AS s " + "ON t.id == s.id " + "WHEN MATCHED AND t.id = 1 THEN " + "  UPDATE SET * " + "WHEN MATCHED AND t.id = 6 THEN " + "  DELETE " + "WHEN NOT MATCHED AND s.id = 2 THEN " + "  INSERT *", tableName);
        ImmutableList<Object[]> expectedRows = ImmutableList.of(// updated
        row(1, "emp-id-1"), // new
        row(2, "emp-id-2"));
        assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s ORDER BY id", tableName));
        removeTables();
    }
}
Also used : DistributionMode(org.apache.iceberg.DistributionMode) Test(org.junit.Test)

Example 4 with DistributionMode

use of org.apache.iceberg.DistributionMode in project iceberg by apache.

the class TestMerge method testMergeIntoPartitionedAndOrderedTable.

@Test
public void testMergeIntoPartitionedAndOrderedTable() {
    for (DistributionMode mode : DistributionMode.values()) {
        createAndInitTable("id INT, dep STRING");
        sql("ALTER TABLE %s ADD PARTITION FIELD dep", tableName);
        sql("ALTER TABLE %s WRITE ORDERED BY (id)", tableName);
        sql("ALTER TABLE %s SET TBLPROPERTIES('%s' '%s')", tableName, WRITE_DISTRIBUTION_MODE, mode.modeName());
        append(tableName, "{ \"id\": 1, \"dep\": \"emp-id-one\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        createOrReplaceView("source", "id INT, dep STRING", "{ \"id\": 2, \"dep\": \"emp-id-2\" }\n" + "{ \"id\": 1, \"dep\": \"emp-id-1\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        sql("MERGE INTO %s AS t USING source AS s " + "ON t.id == s.id " + "WHEN MATCHED AND t.id = 1 THEN " + "  UPDATE SET * " + "WHEN MATCHED AND t.id = 6 THEN " + "  DELETE " + "WHEN NOT MATCHED AND s.id = 2 THEN " + "  INSERT *", tableName);
        ImmutableList<Object[]> expectedRows = ImmutableList.of(// updated
        row(1, "emp-id-1"), // new
        row(2, "emp-id-2"));
        assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s ORDER BY id", tableName));
        removeTables();
    }
}
Also used : DistributionMode(org.apache.iceberg.DistributionMode) Test(org.junit.Test)

Example 5 with DistributionMode

use of org.apache.iceberg.DistributionMode in project iceberg by apache.

the class TestMerge method testMergeWithIdentityTransform.

@Test
public void testMergeWithIdentityTransform() {
    for (DistributionMode mode : DistributionMode.values()) {
        createAndInitTable("id INT, dep STRING");
        sql("ALTER TABLE %s ADD PARTITION FIELD identity(dep)", tableName);
        sql("ALTER TABLE %s SET TBLPROPERTIES('%s' '%s')", tableName, WRITE_DISTRIBUTION_MODE, mode.modeName());
        append(tableName, "{ \"id\": 1, \"dep\": \"emp-id-one\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        createOrReplaceView("source", "id INT, dep STRING", "{ \"id\": 2, \"dep\": \"emp-id-2\" }\n" + "{ \"id\": 1, \"dep\": \"emp-id-1\" }\n" + "{ \"id\": 6, \"dep\": \"emp-id-6\" }");
        sql("MERGE INTO %s AS t USING source AS s " + "ON t.id == s.id " + "WHEN MATCHED AND t.id = 1 THEN " + "  UPDATE SET * " + "WHEN MATCHED AND t.id = 6 THEN " + "  DELETE " + "WHEN NOT MATCHED AND s.id = 2 THEN " + "  INSERT *", tableName);
        ImmutableList<Object[]> expectedRows = ImmutableList.of(// updated
        row(1, "emp-id-1"), // new
        row(2, "emp-id-2"));
        assertEquals("Should have expected rows", expectedRows, sql("SELECT * FROM %s ORDER BY id", tableName));
        removeTables();
    }
}
Also used : DistributionMode(org.apache.iceberg.DistributionMode) Test(org.junit.Test)

Aggregations

DistributionMode (org.apache.iceberg.DistributionMode)8 Test (org.junit.Test)5 Distribution (org.apache.spark.sql.connector.distributions.Distribution)3 SortOrder (org.apache.spark.sql.connector.expressions.SortOrder)3