davisusanibar commented on a change in pull request #113:
URL: https://github.com/apache/arrow-cookbook/pull/113#discussion_r781625568
##########
File path: java/source/data.rst
##########
@@ -0,0 +1,316 @@
+=================
+Data manipulation
+=================
+
+Recipes related to compare, filtering or transforming data.
+
+.. contents::
+
+We are going to use this util for data manipulation:
+
+.. code-block:: java
+
+ import org.apache.arrow.algorithm.sort.VectorValueComparator;
+ import org.apache.arrow.memory.RootAllocator;
+ import org.apache.arrow.vector.IntVector;
+ import org.apache.arrow.vector.VarCharVector;
+
+ void setVector(IntVector vector, Integer... values) {
+ final int length = values.length;
+ vector.allocateNew(length);
+ for (int i = 0; i < length; i++) {
+ if (values[i] != null) {
+ vector.set(i, values[i]);
+ }
+ }
+ vector.setValueCount(length);
+ }
+
+ class TestVarCharSorter extends VectorValueComparator<VarCharVector> {
+ @Override
+ public int compareNotNull(int index1, int index2) {
+ byte b1 = vector1.get(index1)[0];
+ byte b2 = vector2.get(index2)[0];
+ return b1 - b2;
+ }
+
+ @Override
+ public VectorValueComparator<VarCharVector> createNew() {
+ return new TestVarCharSorter();
+ }
+ }
+ RootAllocator rootAllocator = new RootAllocator(Long.MAX_VALUE); // deal
with byte buffer allocation
+
+Compare fields on the array
+===========================
+
+.. code-block:: java
+ :emphasize-lines: 10
+
+ import org.apache.arrow.vector.IntVector;
+ import org.apache.arrow.vector.compare.TypeEqualsVisitor;
+
+ IntVector right = new IntVector("int", rootAllocator);
+ IntVector left1 = new IntVector("int", rootAllocator);
+ IntVector left2 = new IntVector("int2", rootAllocator);
+
+ setVector(right, 10,20,30);
+
+ TypeEqualsVisitor visitor = new TypeEqualsVisitor(right); // equal or
unequal
+
+Comparing vector fields:
+
+.. code-block:: java
+ :emphasize-lines: 1-4
+
+ jshell> visitor.equals(left1); visitor.equals(left2);
+
+ true
+ false
+
+Compare values on the array
+===========================
+
+.. code-block:: java
+ :emphasize-lines: 15-17
+
+ import org.apache.arrow.algorithm.sort.StableVectorComparator;
+ import org.apache.arrow.algorithm.sort.VectorValueComparator;
+ import org.apache.arrow.vector.VarCharVector;
+
+ // compare two values at the given indices in the vectors.
+ // comparing org.apache.arrow.algorithm.sort.VectorValueComparator on
algorithm
+ VarCharVector vec = new VarCharVector("valueindexcomparator",
rootAllocator);
+ vec.allocateNew(100, 5);
+ vec.setValueCount(10);
+ vec.set(0, "ba".getBytes());
+ vec.set(1, "abc".getBytes());
+ vec.set(2, "aa".getBytes());
+ vec.set(3, "abc".getBytes());
+ vec.set(4, "a".getBytes());
+ VectorValueComparator<VarCharVector> comparatorValues = new
TestVarCharSorter(); // less than, equal to, greater than
+ VectorValueComparator<VarCharVector> stableComparator = new
StableVectorComparator<>(comparatorValues);//Stable comparator only supports
comparing values from the same vector
+ stableComparator.attachVector(vec);
+
+Comparing two values at the given indices in the vectors:
+
+.. code-block:: java
+ :emphasize-lines: 1-8
+
+ jshell> stableComparator.compare(0, 1) > 0; stableComparator.compare(1, 2)
< 0; stableComparator.compare(2, 3) < 0; stableComparator.compare(1, 3) < 0;
stableComparator.compare(3, 1) > 0; stableComparator.compare(3, 3) == 0;
Review comment:
Changed
--
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]