pitrou commented on code in PR #38305:
URL: https://github.com/apache/arrow/pull/38305#discussion_r1365607188


##########
java/vector/src/main/codegen/templates/DenseUnionVector.java:
##########
@@ -714,8 +714,22 @@ public int getBufferSizeFor(final int count) {
     if (count == 0) {
       return 0;
     }
+
+    int[] legCounts = new int[typeFields.length];

Review Comment:
   What is a "leg" here? This is not common terminology in Arrow.



##########
java/vector/src/main/codegen/templates/DenseUnionVector.java:
##########
@@ -714,8 +714,22 @@ public int getBufferSizeFor(final int count) {
     if (count == 0) {
       return 0;
     }
+
+    int[] legCounts = new int[typeFields.length];
+    for (int i = 0; i < count; i++) {
+      byte tid = getTypeId(i);
+      legCounts[tid]++;
+    }
+
+    long legBytes = 0;

Review Comment:
   Same here: call this `totalChildBytes` for example.



##########
java/vector/src/test/java/org/apache/arrow/vector/complex/TestDUVBufferSize.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.complex;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BaseValueVector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.holders.NullableIntHolder;
+import org.apache.arrow.vector.holders.NullableVarBinaryHolder;
+import org.apache.arrow.vector.types.UnionMode;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.util.DataSizeRoundingUtil;
+import org.junit.jupiter.api.Test;
+
+public class TestDUVBufferSize {
+  @Test
+  public void testBufferSize() {
+    try (BufferAllocator allocator = new RootAllocator();
+         DenseUnionVector duv = new DenseUnionVector("duv", allocator,
+                 FieldType.nullable(new ArrowType.Union(UnionMode.Dense, 
null)), null)) {
+
+      List<Field> fields = Arrays.asList(
+              new Field("a", FieldType.notNullable(new ArrowType.Int(32, 
true)), null),
+              new Field("b", FieldType.notNullable(new ArrowType.Binary()), 
null)
+      );
+
+      duv.initializeChildrenFromFields(fields);
+
+      NullableIntHolder nih = new NullableIntHolder();
+      NullableVarBinaryHolder nvbh = new NullableVarBinaryHolder();
+
+      int ac = BaseValueVector.INITIAL_VALUE_ALLOCATION + 1;
+      for (int i = 0; i < ac; i++) {
+        duv.setTypeId(i, (byte) 0);
+        duv.setSafe(i, nih);
+      }
+
+      int bc = 1;
+      for (int i = 0; i < bc; i++) {
+        duv.setTypeId(i + ac, (byte) 1);
+        duv.setSafe(i + ac, nvbh);
+      }
+
+      int count = ac + bc;
+      duv.setValueCount(count);
+
+      // will not necessarily see an error unless bounds checking is on.
+      assertDoesNotThrow(duv::getBufferSize);

Review Comment:
   Can we also test `getBufferSize` with an explicit argument?



##########
java/vector/src/test/java/org/apache/arrow/vector/complex/TestDUVBufferSize.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.complex;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BaseValueVector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.holders.NullableIntHolder;
+import org.apache.arrow.vector.holders.NullableVarBinaryHolder;
+import org.apache.arrow.vector.types.UnionMode;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.util.DataSizeRoundingUtil;
+import org.junit.jupiter.api.Test;
+
+public class TestDUVBufferSize {
+  @Test
+  public void testBufferSize() {
+    try (BufferAllocator allocator = new RootAllocator();
+         DenseUnionVector duv = new DenseUnionVector("duv", allocator,
+                 FieldType.nullable(new ArrowType.Union(UnionMode.Dense, 
null)), null)) {
+
+      List<Field> fields = Arrays.asList(
+              new Field("a", FieldType.notNullable(new ArrowType.Int(32, 
true)), null),
+              new Field("b", FieldType.notNullable(new ArrowType.Binary()), 
null)
+      );
+
+      duv.initializeChildrenFromFields(fields);
+
+      NullableIntHolder nih = new NullableIntHolder();
+      NullableVarBinaryHolder nvbh = new NullableVarBinaryHolder();
+
+      int ac = BaseValueVector.INITIAL_VALUE_ALLOCATION + 1;
+      for (int i = 0; i < ac; i++) {
+        duv.setTypeId(i, (byte) 0);
+        duv.setSafe(i, nih);
+      }
+
+      int bc = 1;
+      for (int i = 0; i < bc; i++) {
+        duv.setTypeId(i + ac, (byte) 1);
+        duv.setSafe(i + ac, nvbh);
+      }
+
+      int count = ac + bc;

Review Comment:
   Please, can you avoid cryptic variable names? (`nih`, `ac`, etc.)



##########
java/vector/src/main/codegen/templates/DenseUnionVector.java:
##########
@@ -714,8 +714,22 @@ public int getBufferSizeFor(final int count) {
     if (count == 0) {
       return 0;
     }
+
+    int[] legCounts = new int[typeFields.length];
+    for (int i = 0; i < count; i++) {
+      byte tid = getTypeId(i);
+      legCounts[tid]++;
+    }
+
+    long legBytes = 0;
+    byte tid = 0;
+    for (ValueVector v : internalStruct) {
+      legBytes += (long)v.getBufferSizeFor(legCounts[tid]);
+      tid++;
+    }

Review Comment:
   I don't think this is right: `internalStruct` has its fields indexed by 
physical child id (that is: 0, 1, 2...) but the type ids are logical values 
that are unrelated to the physical structure.
   
   But I think that you should just copy what is already done in 
`setChildVectorValueCounts` and adapt it for the use case of counting buffer 
bytes.



##########
java/vector/src/test/java/org/apache/arrow/vector/complex/TestDUVBufferSize.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.complex;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BaseValueVector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.holders.NullableIntHolder;
+import org.apache.arrow.vector.holders.NullableVarBinaryHolder;
+import org.apache.arrow.vector.types.UnionMode;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.util.DataSizeRoundingUtil;
+import org.junit.jupiter.api.Test;
+
+public class TestDUVBufferSize {
+  @Test
+  public void testBufferSize() {
+    try (BufferAllocator allocator = new RootAllocator();
+         DenseUnionVector duv = new DenseUnionVector("duv", allocator,
+                 FieldType.nullable(new ArrowType.Union(UnionMode.Dense, 
null)), null)) {
+
+      List<Field> fields = Arrays.asList(
+              new Field("a", FieldType.notNullable(new ArrowType.Int(32, 
true)), null),
+              new Field("b", FieldType.notNullable(new ArrowType.Binary()), 
null)
+      );
+
+      duv.initializeChildrenFromFields(fields);

Review Comment:
   Ok, but that's the easy case as it will use auto-incremented type ids 
(0,1,2...).
   Instead, I would recommend calling `DenseUnionVector.addVector` which allows 
you to add child fields with non-trivial type ids (7,6,5 for example).



##########
java/vector/src/main/codegen/templates/DenseUnionVector.java:
##########
@@ -714,8 +714,22 @@ public int getBufferSizeFor(final int count) {
     if (count == 0) {
       return 0;
     }
+
+    int[] legCounts = new int[typeFields.length];

Review Comment:
   Could call this `typeCounts` or `childCounts`.



##########
java/vector/src/main/codegen/templates/DenseUnionVector.java:
##########
@@ -714,8 +714,22 @@ public int getBufferSizeFor(final int count) {
     if (count == 0) {
       return 0;
     }
+
+    int[] legCounts = new int[typeFields.length];
+    for (int i = 0; i < count; i++) {
+      byte tid = getTypeId(i);
+      legCounts[tid]++;
+    }
+
+    long legBytes = 0;
+    byte tid = 0;
+    for (ValueVector v : internalStruct) {
+      legBytes += (long)v.getBufferSizeFor(legCounts[tid]);
+      tid++;
+    }
+
     return (int) (count * TYPE_WIDTH + (long) count * OFFSET_WIDTH
-        + DataSizeRoundingUtil.divideBy8Ceil(count) + 
internalStruct.getBufferSizeFor(count));
+        + DataSizeRoundingUtil.divideBy8Ceil(count) + legBytes);

Review Comment:
   I think `DataSizeRoundingUtil.divideBy8Ceil(count)` is a mistake: union 
vectors don't have a validity bitmap, so they don't need any buffer for it.



##########
java/vector/src/test/java/org/apache/arrow/vector/complex/TestDUVBufferSize.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.complex;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.BaseValueVector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.holders.NullableIntHolder;
+import org.apache.arrow.vector.holders.NullableVarBinaryHolder;
+import org.apache.arrow.vector.types.UnionMode;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.util.DataSizeRoundingUtil;
+import org.junit.jupiter.api.Test;
+
+public class TestDUVBufferSize {

Review Comment:
   I'm not sure "DUV" is a common abbreviation, can you just spell it out in 
full? (also in the file name)



-- 
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: github-unsubscr...@arrow.apache.org

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

Reply via email to