You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/01/04 03:54:35 UTC

[GitHub] [arrow] nbruno opened a new pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

nbruno opened a new pull request #9088:
URL: https://github.com/apache/arrow/pull/9088


   Based on [this commit](https://github.com/apache/arrow/commit/c15a511ea19b7ed58e706ef6c8197fe9ed982a8b), it seems that the correct format for metadata serialization is `List<Map<String, String>>` since it was decided to deserialize it in this format. 
   
   There are several ways to accomplish this, but this PR takes a less-impactful approach of defining the correct getter for Jackson to use. Other approaches could be more aggressive and propagate this change throughout the codebase, modifying the `Schema#getCustomMetadata` and `Field#getMetadata` method to return `List<Map<String, String>>`, but at that point it would probably make more sense to get rid of the notion of "converting" metadata entirely.


----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551304487



##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }

Review comment:
       It seems there is not much need to extract this as a method, as the method body is fairly short?




----------------------------------------------------------------
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.

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



[GitHub] [arrow] github-actions[bot] commented on pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#issuecomment-753748456


   https://issues.apache.org/jira/browse/ARROW-11114


----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551302282



##########
File path: java/vector/src/main/java/org/apache/arrow/vector/types/pojo/Schema.java
##########
@@ -137,15 +138,35 @@ private Schema(@JsonProperty("fields") Iterable<Field> fields,
         .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
   }
 
+  static List<Map<String, String>> convertMetadata(Map<String, String> metadata) {
+    return (metadata == null) ? null : metadata.entrySet()
+        .stream()
+        .map(Schema::convertEntryToKeyValueMap)
+        .collect(Collectors.toList());
+  }
+
+  private static Map<String, String> convertEntryToKeyValueMap(Map.Entry<String, String> entry) {
+    Map<String, String> map = new HashMap<>(2);
+    map.put("key", entry.getKey());

Review comment:
       maybe we can extract the literals as static final variables?




----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551868939



##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.apache.arrow.vector.types.pojo.Schema.METADATA_KEY;
+import static org.apache.arrow.vector.types.pojo.Schema.METADATA_VALUE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }
+
+  @Test
+  public void testMetadata() throws IOException {
+    Map<String, String> metadata = new HashMap<>(1);
+    metadata.put("testKey", "testValue");
+
+    Schema schema = new Schema(Collections.singletonList(
+        field("a", false, new Int(8, true), metadata)
+    ));
+    contains(schema, "\"" + METADATA_KEY + "\" : \"testKey\"", "\"" + METADATA_VALUE + "\" : \"testValue\"");
+
+    String json = schema.toJson();
+    Schema actual = Schema.fromJSON(json);
+
+    Map<String, String> actualMetadata = actual.getFields().get(0).getMetadata();
+    assertEquals(1, actualMetadata.size());
+    assertEquals("testValue", actualMetadata.get("testKey"));
+  }
+
+  private void contains(Schema schema, String... s) {

Review comment:
       To avoid duplicate call to `toJson`, maybe the first parameter could be a `String`?
   In addition, the method name could be `jsonContains`?




----------------------------------------------------------------
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.

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



[GitHub] [arrow] nbruno commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
nbruno commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551353030



##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }

Review comment:
       I actually followed the same paradigm in TestSchema for this, but the methods defined there did not include metadata as a parameter. I assume this method would be useful in the future for additional unit tests for the Field class, but I can inline it if you'd like.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] nbruno commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
nbruno commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551997285



##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.apache.arrow.vector.types.pojo.Schema.METADATA_KEY;
+import static org.apache.arrow.vector.types.pojo.Schema.METADATA_VALUE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }
+
+  @Test
+  public void testMetadata() throws IOException {
+    Map<String, String> metadata = new HashMap<>(1);
+    metadata.put("testKey", "testValue");
+
+    Schema schema = new Schema(Collections.singletonList(
+        field("a", false, new Int(8, true), metadata)
+    ));
+    contains(schema, "\"" + METADATA_KEY + "\" : \"testKey\"", "\"" + METADATA_VALUE + "\" : \"testValue\"");
+
+    String json = schema.toJson();
+    Schema actual = Schema.fromJSON(json);
+
+    Map<String, String> actualMetadata = actual.getFields().get(0).getMetadata();
+    assertEquals(1, actualMetadata.size());
+    assertEquals("testValue", actualMetadata.get("testKey"));
+  }
+
+  private void contains(Schema schema, String... s) {

Review comment:
       Sounds good, done.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 closed pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 closed pull request #9088:
URL: https://github.com/apache/arrow/pull/9088


   


----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 commented on pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 commented on pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#issuecomment-756497680


   Merging. Thank you @nbruno 


----------------------------------------------------------------
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.

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



[GitHub] [arrow] liyafan82 commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
liyafan82 commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551303786



##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }
+
+  @Test
+  public void testMetadata() throws IOException {
+    Map<String, String> metadata = new HashMap<>(1);
+    metadata.put("testKey", "testValue");
+
+    Schema schema = new Schema(Collections.singletonList(
+        field("a", false, new Int(8, true), metadata)
+    ));
+
+    String json = schema.toJson();
+    Schema actual = Schema.fromJSON(json);
+
+    Map<String, String> actualMetadata = actual.getFields().get(0).getMetadata();
+    assertEquals(1, actualMetadata.size());
+    assertEquals("testValue", actualMetadata.get("testKey"));
+
+    contains(schema, "\"key\" : \"testKey\"", "\"value\" : \"testValue\"");

Review comment:
       we should place this immediately after the schema is generated?




----------------------------------------------------------------
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.

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



[GitHub] [arrow] nbruno commented on a change in pull request #9088: ARROW-11114: [Java] Fix Schema and Field metadata JSON serialization

Posted by GitBox <gi...@apache.org>.
nbruno commented on a change in pull request #9088:
URL: https://github.com/apache/arrow/pull/9088#discussion_r551352339



##########
File path: java/vector/src/main/java/org/apache/arrow/vector/types/pojo/Schema.java
##########
@@ -137,15 +138,35 @@ private Schema(@JsonProperty("fields") Iterable<Field> fields,
         .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
   }
 
+  static List<Map<String, String>> convertMetadata(Map<String, String> metadata) {
+    return (metadata == null) ? null : metadata.entrySet()
+        .stream()
+        .map(Schema::convertEntryToKeyValueMap)
+        .collect(Collectors.toList());
+  }
+
+  private static Map<String, String> convertEntryToKeyValueMap(Map.Entry<String, String> entry) {
+    Map<String, String> map = new HashMap<>(2);
+    map.put("key", entry.getKey());

Review comment:
       Done, I added constants for these keys and updated the unit tests to also reference those constants. 

##########
File path: java/vector/src/test/java/org/apache/arrow/vector/types/pojo/TestField.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.vector.types.pojo;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.arrow.vector.types.pojo.ArrowType.Int;
+import org.junit.Test;
+
+public class TestField {
+
+  private static Field field(String name, boolean nullable, ArrowType type, Map<String, String> metadata) {
+    return new Field(name, new FieldType(nullable, type, null, metadata), Collections.emptyList());
+  }
+
+  @Test
+  public void testMetadata() throws IOException {
+    Map<String, String> metadata = new HashMap<>(1);
+    metadata.put("testKey", "testValue");
+
+    Schema schema = new Schema(Collections.singletonList(
+        field("a", false, new Int(8, true), metadata)
+    ));
+
+    String json = schema.toJson();
+    Schema actual = Schema.fromJSON(json);
+
+    Map<String, String> actualMetadata = actual.getFields().get(0).getMetadata();
+    assertEquals(1, actualMetadata.size());
+    assertEquals("testValue", actualMetadata.get("testKey"));
+
+    contains(schema, "\"key\" : \"testKey\"", "\"value\" : \"testValue\"");

Review comment:
       Done




----------------------------------------------------------------
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.

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