You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by "caican00 (via GitHub)" <gi...@apache.org> on 2024/08/05 01:42:35 UTC

Re: [PR] [#4291] feat(catalog-lakehouse-paimon): Support Paimon table with primary keys in Gravitino [gravitino]

caican00 commented on code in PR #4292:
URL: https://github.com/apache/gravitino/pull/4292#discussion_r1703452833


##########
catalogs/catalog-lakehouse-paimon/src/test/java/org/apache/gravitino/catalog/lakehouse/paimon/TestGravitinoPaimonTable.java:
##########
@@ -201,6 +207,161 @@ void testCreatePaimonTable() {
             .contains(String.format("Paimon table %s already exists", tableIdentifier)));
   }
 
+  @Test
+  void testCreatePaimonPartitionedTable() {
+    String paimonTableName = "test_paimon_partitioned_table";
+    NameIdentifier tableIdentifier = NameIdentifier.of(paimonSchema.name(), paimonTableName);
+    Map<String, String> properties = Maps.newHashMap();
+    properties.put("key1", "val1");
+    properties.put("key2", "val2");
+
+    GravitinoPaimonColumn col1 =
+        fromPaimonColumn(new DataField(0, "col_1", DataTypes.INT().nullable(), PAIMON_COMMENT));
+    GravitinoPaimonColumn col2 =
+        fromPaimonColumn(new DataField(1, "col_2", DataTypes.DATE().notNull(), PAIMON_COMMENT));
+    RowType rowTypeInside =
+        RowType.builder()
+            .field("integer_field_inside", DataTypes.INT().notNull())
+            .field("string_field_inside", DataTypes.STRING().notNull())
+            .build();
+    RowType rowType =
+        RowType.builder()
+            .field("integer_field", DataTypes.INT().notNull())
+            .field("string_field", DataTypes.STRING().notNull(), "string field")
+            .field("struct_field", rowTypeInside.nullable(), "struct field")
+            .build();
+    GravitinoPaimonColumn col3 =
+        fromPaimonColumn(new DataField(2, "col_3", rowType.notNull(), PAIMON_COMMENT));
+    Column[] columns = new Column[] {col1, col2, col3};
+
+    Transform[] transforms = new Transform[] {identity("col_1"), identity("col_2")};
+    String[] partitionKeys = new String[] {"col_1", "col_2"};
+
+    Table table =
+        paimonCatalogOperations.createTable(
+            tableIdentifier,
+            columns,
+            PAIMON_COMMENT,
+            properties,
+            transforms,
+            Distributions.NONE,
+            new SortOrder[0]);
+
+    Assertions.assertEquals(tableIdentifier.name(), table.name());
+    Assertions.assertEquals(PAIMON_COMMENT, table.comment());
+    Assertions.assertEquals("val1", table.properties().get("key1"));
+    Assertions.assertEquals("val2", table.properties().get("key2"));
+    Assertions.assertArrayEquals(transforms, table.partitioning());
+
+    Table loadedTable = paimonCatalogOperations.loadTable(tableIdentifier);
+
+    Assertions.assertEquals("val1", loadedTable.properties().get("key1"));
+    Assertions.assertEquals("val2", loadedTable.properties().get("key2"));
+    Assertions.assertTrue(loadedTable.columns()[0].nullable());
+    Assertions.assertFalse(loadedTable.columns()[1].nullable());
+    Assertions.assertFalse(loadedTable.columns()[2].nullable());
+    Assertions.assertArrayEquals(transforms, loadedTable.partitioning());
+    String[] loadedPartitionKeys =
+        Arrays.stream(loadedTable.partitioning())
+            .map(
+                transform -> {
+                  NamedReference[] references = transform.references();
+                  Assertions.assertTrue(
+                      references.length == 1
+                          && references[0] instanceof NamedReference.FieldReference);
+                  NamedReference.FieldReference fieldReference =
+                      (NamedReference.FieldReference) references[0];
+                  return fieldReference.fieldName()[0];
+                })
+            .toArray(String[]::new);
+    Assertions.assertArrayEquals(partitionKeys, loadedPartitionKeys);
+
+    Assertions.assertTrue(paimonCatalogOperations.tableExists(tableIdentifier));
+    NameIdentifier[] tableIdents = paimonCatalogOperations.listTables(tableIdentifier.namespace());
+    Assertions.assertTrue(Arrays.asList(tableIdents).contains(tableIdentifier));
+  }
+
+  @Test
+  void testCreatePaimonPrimaryKeyTable() {
+    String paimonTableName = "test_paimon_primary_key_table";
+    NameIdentifier tableIdentifier = NameIdentifier.of(paimonSchema.name(), paimonTableName);
+    Map<String, String> properties = Maps.newHashMap();
+    properties.put("key1", "val1");
+    properties.put("key2", "val2");
+
+    GravitinoPaimonColumn col1 =
+        fromPaimonColumn(new DataField(0, "col_1", DataTypes.INT().nullable(), PAIMON_COMMENT));
+    GravitinoPaimonColumn col2 =
+        fromPaimonColumn(new DataField(1, "col_2", DataTypes.DATE().notNull(), PAIMON_COMMENT));
+    RowType rowTypeInside =
+        RowType.builder()
+            .field("integer_field_inside", DataTypes.INT().notNull())
+            .field("string_field_inside", DataTypes.STRING().notNull())
+            .build();
+    RowType rowType =
+        RowType.builder()
+            .field("integer_field", DataTypes.INT().notNull())
+            .field("string_field", DataTypes.STRING().notNull(), "string field")
+            .field("struct_field", rowTypeInside.nullable(), "struct field")
+            .build();
+    GravitinoPaimonColumn col3 =
+        fromPaimonColumn(new DataField(2, "col_3", rowType.notNull(), PAIMON_COMMENT));
+    Column[] columns = new Column[] {col1, col2, col3};
+
+    Transform[] transforms = new Transform[] {identity("col_1")};
+    String[] partitionKeys = new String[] {"col_1"};
+    String[] primaryKeys = new String[] {"col_2"};
+    properties.put(PRIMARY_KEY_IDENTIFIER, String.join(",", primaryKeys));
+
+    Table table =
+        paimonCatalogOperations.createTable(
+            tableIdentifier,
+            columns,
+            PAIMON_COMMENT,
+            properties,
+            transforms,
+            Distributions.NONE,
+            new SortOrder[0]);
+
+    Assertions.assertEquals(tableIdentifier.name(), table.name());
+    Assertions.assertEquals(PAIMON_COMMENT, table.comment());
+    Map<String, String> resultProp = table.properties();
+    for (Map.Entry<String, String> entry : properties.entrySet()) {
+      Assertions.assertTrue(resultProp.containsKey(entry.getKey()));
+      Assertions.assertEquals(entry.getValue(), resultProp.get(entry.getKey()));
+    }
+    Assertions.assertArrayEquals(transforms, table.partitioning());
+
+    Table loadedTable = paimonCatalogOperations.loadTable(tableIdentifier);
+    resultProp = loadedTable.properties();
+    for (Map.Entry<String, String> entry : properties.entrySet()) {
+      Assertions.assertTrue(resultProp.containsKey(entry.getKey()));
+      Assertions.assertEquals(entry.getValue(), resultProp.get(entry.getKey()));
+    }
+    Assertions.assertTrue(loadedTable.columns()[0].nullable());
+    Assertions.assertFalse(loadedTable.columns()[1].nullable());
+    Assertions.assertFalse(loadedTable.columns()[2].nullable());
+    Assertions.assertArrayEquals(transforms, loadedTable.partitioning());
+    String[] loadedPartitionKeys =
+        Arrays.stream(loadedTable.partitioning())
+            .map(
+                transform -> {
+                  NamedReference[] references = transform.references();
+                  Assertions.assertTrue(
+                      references.length == 1
+                          && references[0] instanceof NamedReference.FieldReference);
+                  NamedReference.FieldReference fieldReference =
+                      (NamedReference.FieldReference) references[0];
+                  return fieldReference.fieldName()[0];
+                })
+            .toArray(String[]::new);
+    Assertions.assertArrayEquals(partitionKeys, loadedPartitionKeys);
+
+    Assertions.assertTrue(paimonCatalogOperations.tableExists(tableIdentifier));

Review Comment:
   fixed.



##########
catalogs/catalog-lakehouse-paimon/src/main/java/org/apache/gravitino/catalog/lakehouse/paimon/GravitinoPaimonTable.java:
##########
@@ -51,7 +64,25 @@ protected TableOperations newOps() {
    * @return The converted Paimon table.
    */
   public Schema toPaimonTableSchema() {
-    Schema.Builder builder = Schema.newBuilder().comment(comment).options(properties);
+    Schema.Builder builder = Schema.newBuilder().comment(comment);
+    if (properties == null) {
+      properties = Maps.newHashMap();
+    }
+    if (partitioning == null) {
+      partitioning = EMPTY_PARTITIONING;
+    }
+
+    Map<String, String> normalizedProperties = new HashMap<>(properties);
+    normalizedProperties.remove(PRIMARY_KEY_IDENTIFIER);

Review Comment:
   fixed.



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org