aihuaxu commented on code in PR #3202:
URL: https://github.com/apache/parquet-java/pull/3202#discussion_r2069805783


##########
parquet-variant/src/test/java/org/apache/parquet/variant/TestVariantScalar.java:
##########
@@ -34,91 +37,69 @@
 public class TestVariantScalar {
   private static final Logger LOG = 
LoggerFactory.getLogger(TestVariantScalar.class);
 
-  private static final ByteBuffer EMPTY_METADATA = ByteBuffer.wrap(new byte[] 
{0b1});
-
-  private void checkType(Variant v, int expectedBasicType, Variant.Type 
expectedType) {
-    Assert.assertEquals(expectedBasicType, v.value.get(v.value.position()) & 
VariantUtil.BASIC_TYPE_MASK);
-    Assert.assertEquals(expectedType, v.getType());
-  }
-
-  private void testVariant(Variant v, Consumer<Variant> consumer) {
-    consumer.accept(v);
-    // Create new Variant with different byte offsets
-    byte[] newValue = new byte[v.value.capacity() + 50];
-    byte[] newMetadata = new byte[v.metadata.capacity() + 50];
-    Arrays.fill(newValue, (byte) 0xFF);
-    Arrays.fill(newMetadata, (byte) 0xFF);
-    v.value.position(0);
-    v.value.get(newValue, 25, v.value.capacity());
-    v.value.position(0);
-    v.metadata.position(0);
-    v.metadata.get(newMetadata, 25, v.metadata.capacity());
-    v.metadata.position(0);
-    Variant v2 = new Variant(
-        ByteBuffer.wrap(newValue, 25, v.value.capacity()),
-        ByteBuffer.wrap(newMetadata, 25, v.metadata.capacity()));
-    consumer.accept(v2);
-  }
-
-  private static byte primitiveHeader(int type) {
-    return (byte) (type << 2);
-  }
-
-  private static byte metadataHeader(boolean isSorted, int offsetSize) {
-    return (byte) (((offsetSize - 1) << 6) | (isSorted ? 0b10000 : 0) | 
0b0001);
-  }
-
-  private static int getMinIntegerSize(int value) {
-    return (value <= 0xFF) ? 1 : (value <= 0xFFFF) ? 2 : (value <= 0xFFFFFF) ? 
3 : 4;
-  }
-
-  private static void writeVarlenInt(ByteBuffer buffer, int value, int 
valueSize) {
-    if (valueSize == 1) {
-      buffer.put((byte) value);
-    } else if (valueSize == 2) {
-      buffer.putShort((short) value);
-    } else if (valueSize == 3) {
-      buffer.put((byte) (value & 0xFF));
-      buffer.put((byte) ((value >> 8) & 0xFF));
-      buffer.put((byte) ((value >> 16) & 0xFF));
-    } else {
-      buffer.putInt(value);
-    }
+  @Test
+  public void testNull() {
+    Variant value = new Variant(
+        ByteBuffer.wrap(new byte[] {VariantTestUtil.primitiveHeader(0)}), 
VariantTestUtil.EMPTY_METADATA);
+    VariantTestUtil.testVariant(value, v -> VariantTestUtil.checkType(v, 
VariantUtil.NULL, Variant.Type.NULL));
   }
 
   @Test
-  public void testNull() {
-    Variant value = new Variant(ByteBuffer.wrap(new byte[] 
{primitiveHeader(0)}), EMPTY_METADATA);
-    testVariant(value, v -> checkType(v, VariantUtil.NULL, Variant.Type.NULL));
+  public void testNullBuilder() {
+    VariantBuilder vb = new VariantBuilder(false);
+    vb.appendNull();

Review Comment:
   Here we append null and then we can still append other values.
   Maybe we can have VariantBuilder.ofNull() or similar to prevent this? 
   
   



##########
parquet-variant/src/test/java/org/apache/parquet/variant/TestVariantObject.java:
##########
@@ -205,22 +137,52 @@ private static ByteBuffer constructMetadata(Boolean 
isSorted, List<String> field
 
   @Test
   public void testEmptyObject() {
-    Variant value = new Variant(ByteBuffer.wrap(new byte[] {0b10, 0x00}), 
EMPTY_METADATA);
-    testVariant(value, v -> {
-      checkType(v, VariantUtil.OBJECT, Variant.Type.OBJECT);
+    Variant value = new Variant(ByteBuffer.wrap(new byte[] {0b10, 0x00}), 
VariantTestUtil.EMPTY_METADATA);
+    VariantTestUtil.testVariant(value, v -> {
+      VariantTestUtil.checkType(v, VariantUtil.OBJECT, Variant.Type.OBJECT);
+      Assert.assertEquals(0, v.numObjectElements());
+    });
+  }
+
+  @Test
+  public void testEmptyObjectBuilder() {
+    VariantBuilder b = new VariantBuilder(true);
+    VariantObjectBuilder o = b.startObject();
+    b.endObject(o);
+    VariantTestUtil.testVariant(b.build(), v -> {
+      VariantTestUtil.checkType(v, VariantUtil.OBJECT, Variant.Type.OBJECT);
       Assert.assertEquals(0, v.numObjectElements());
     });
   }
 
   @Test
   public void testEmptyLargeObject() {
-    Variant value = new Variant(ByteBuffer.wrap(new byte[] {0b1000010, 0x00, 
0x00, 0x00, 0x00}), EMPTY_METADATA);
-    testVariant(value, v -> {
-      checkType(v, VariantUtil.OBJECT, Variant.Type.OBJECT);
+    Variant value = new Variant(
+        ByteBuffer.wrap(new byte[] {0b1000010, 0x00, 0x00, 0x00, 0x00}), 
VariantTestUtil.EMPTY_METADATA);
+    VariantTestUtil.testVariant(value, v -> {
+      VariantTestUtil.checkType(v, VariantUtil.OBJECT, Variant.Type.OBJECT);
       Assert.assertEquals(0, v.numObjectElements());
     });
   }
 
+  @Test
+  public void testLargeObjectBuilder() {
+    VariantBuilder b = new VariantBuilder(true);
+    VariantObjectBuilder o = b.startObject();
+    for (int i = 0; i < 1234; i++) {
+      b.appendObjectKey(o, "a" + i);
+      b.appendLong(i);
+    }
+    b.endObject(o);

Review Comment:
   I also did the similar implementation before to have endObject() and 
endArray(), but the caller needs to be careful to call in certain sequence.
   
   We probably can have something similar 
tohttps://docs.oracle.com/javaee/7/api/javax/json/JsonObjectBuilder.html and 
JsonArrayBuilder.
   
   
   
   
   



##########
parquet-variant/src/main/java/org/apache/parquet/variant/VariantArrayBuilder.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.parquet.variant;
+
+import java.util.ArrayList;
+
+/**
+ * Builder for creating Variant arrays, used by VariantBuilder.

Review Comment:
   @gene-db  Seems we didn't include the parseJson() implementation which your 
original PR did. I guess we will implement separately, is that right?



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