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 2022/09/22 18:35:19 UTC
[GitHub] [arrow] lwhite1 opened a new pull request, #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
lwhite1 opened a new pull request, #14213:
URL: https://github.com/apache/arrow/pull/14213
Added some overview documentation to the ValueVector tutorial page, describing how dictionary encoding works in Arrow Java .
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979060456
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
Review Comment:
will do
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979918691
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
Readers/writers read/write RecordBatches, not Vectors.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] ursabot commented on pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #14213:
URL: https://github.com/apache/arrow/pull/14213#issuecomment-1261799721
Benchmark runs are scheduled for baseline = eec94ff4047b9e1bbcf3da46f1c34e3687a171bb and contender = 398519a054e002bb2a8b8e1da587e14108f6d04b. 398519a054e002bb2a8b8e1da587e14108f6d04b is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Finished :arrow_down:0.0% :arrow_up:0.0%] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/62bf5c289e194ea7af10ae8ae406f95a...efcf1a355fbe49adab40da36213dfcac/)
[Failed :arrow_down:0.48% :arrow_up:0.2%] [test-mac-arm](https://conbench.ursa.dev/compare/runs/175a959b85a5404384947ddb43526c24...ffa4e696e34448a3adebcd7b6b379f4c/)
[Failed :arrow_down:0.27% :arrow_up:0.27%] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/a5b51502622d4f609dc12da193e34406...522497da71ff47d98af01a8729a93389/)
[Finished :arrow_down:0.07% :arrow_up:0.11%] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/20bf8d5ee7c44654acfb7849ea364afc...6b45fdc6635249b2aabd0a0a9c116535/)
Buildkite builds:
[Finished] [`398519a0` ec2-t3-xlarge-us-east-2](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ec2-t3-xlarge-us-east-2/builds/1591)
[Failed] [`398519a0` test-mac-arm](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-test-mac-arm/builds/1610)
[Failed] [`398519a0` ursa-i9-9960x](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ursa-i9-9960x/builds/1593)
[Finished] [`398519a0` ursa-thinkcentre-m75q](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ursa-thinkcentre-m75q/builds/1605)
[Finished] [`eec94ff4` ec2-t3-xlarge-us-east-2](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ec2-t3-xlarge-us-east-2/builds/1590)
[Finished] [`eec94ff4` test-mac-arm](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-test-mac-arm/builds/1606)
[Failed] [`eec94ff4` ursa-i9-9960x](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ursa-i9-9960x/builds/1589)
[Finished] [`eec94ff4` ursa-thinkcentre-m75q](https://buildkite.com/apache-arrow/arrow-bci-benchmark-on-ursa-thinkcentre-m75q/builds/1602)
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] github-actions[bot] commented on pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #14213:
URL: https://github.com/apache/arrow/pull/14213#issuecomment-1255605678
:warning: Ticket **has not been started in JIRA**, please click 'Start Progress'.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979048882
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
I think it's nominally supported, but I'm not sure we fully support more than Integer.MAXVALUE values in a single vector.
for example, this method seems to support bigInt, but that branch of the if statement cannot execute.
```
/**
* Get the indexType according to the dictionary vector valueCount.
* @param valueCount dictionary vector valueCount.
* @return index type.
*/
public static ArrowType.Int getIndexType(int valueCount) {
Preconditions.checkArgument(valueCount >= 0);
if (valueCount <= Byte.MAX_VALUE) {
return new ArrowType.Int(8, true);
} else if (valueCount <= Character.MAX_VALUE) {
return new ArrowType.Int(16, true);
} else if (valueCount <= Integer.MAX_VALUE) {
return new ArrowType.Int(32, true);
} else {
return new ArrowType.Int(64, true);
}
}
```
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on PR #14213:
URL: https://github.com/apache/arrow/pull/14213#issuecomment-1256988046
Hi @lidavidm, Thanks for your review. I think I've addressed all the comments.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979052900
##########
docs/source/java/ipc.rst:
##########
@@ -15,6 +15,8 @@
.. specific language governing permissions and limitations
.. under the License.
+.. _ipc:
Review Comment:
so with :doc: no label is required in the destination?
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979252289
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] github-actions[bot] commented on pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #14213:
URL: https://github.com/apache/arrow/pull/14213#issuecomment-1255605658
https://issues.apache.org/jira/browse/ARROW-17811
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979258332
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
replaced :class: tags with double quotes
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on PR #14213:
URL: https://github.com/apache/arrow/pull/14213#issuecomment-1259562494
latest comments addressed.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979056667
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
I'm not that familiar with the readers but I can add a statement to that effect (although it seems odd that a reader would implement provider. I thought they were related one-to-one with a vector)
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979252252
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
added BigIntVector
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979054721
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
FWIW, I had no idea it was supposed to link. I've never worked with rst before. The highlighting is nice, though.
I can add the class markup
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979057423
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
No, dictionaries are disconnected from vectors, and DictionaryProvider is just a lookup table from dictionary ID to dictionary. Dictionaries are stored separately from batches in the IPC format. (C Data Interface muddles this: dictionaries are attached to vectors there.)
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979054697
##########
docs/source/java/ipc.rst:
##########
@@ -15,6 +15,8 @@
.. specific language governing permissions and limitations
.. under the License.
+.. _ipc:
Review Comment:
`:doc:` references files by a path, so if you're just linking to a page and not a particular part of it, it's easier and doesn't require a label
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979080350
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
Sorry, I wasn't suggesting they were linked directly to vector (and I was wrong about 1-1), but I believe a vector can be encoded by at most one dictionary. So my question is, if a Reader reads one vector, why does it need a relationship to a Map of dictionaries, rather than a single dictionary.
In any case, I guess why is not important
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979082182
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
I was following the use of :class: in the other java tutorial pages (and elsewhere in this page). I didn't see any warnings other than ones that seem to be about broken references, eg.
/Users/lawrencewhite/Documents/GitHub/apache/arrow/docs/source/java/vector.rst:22: WARNING: unknown document: ../format/Columnar
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979048882
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
I think it's nominally supported, but I'm not sure we fully support more than Integer.MAXVALUE values in a single vector.
for example, this method seems to support bigInt, but that branch of the if statement cannot execute.
```
/**
* Get the indexType according to the dictionary vector valueCount.
* @param valueCount dictionary vector valueCount.
* @return index type.
*/
public static ArrowType.Int getIndexType(int valueCount) {
Preconditions.checkArgument(valueCount >= 0);
if (valueCount <= Byte.MAX_VALUE) {
return new ArrowType.Int(8, true);
} else if (valueCount <= Character.MAX_VALUE) {
return new ArrowType.Int(16, true);
} else if (valueCount <= Integer.MAX_VALUE) {
return new ArrowType.Int(32, true);
} else {
return new ArrowType.Int(64, true);
}
}
```
I will add it, though.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm merged pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm merged PR #14213:
URL: https://github.com/apache/arrow/pull/14213
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979921572
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a ``VectorSchemaRoot`` with several dictionary-encoded vectors. This is done using an object called a ``DictionaryProvider``. as shown in the example below. Note that we don't put the dictionary vectors in the same ``VectorSchemaRoot`` as the data vectors, as they will generally have fewer values.
Review Comment:
Hmm, I guess they're not part of the root because they're not the actual values anymore, they're 'on the side' so to speak
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
Review Comment:
Maybe explicitly point out that the dictionary vector is otherwise a normal vector and can be populated with the regular APIs?
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a ``VectorSchemaRoot`` with several dictionary-encoded vectors. This is done using an object called a ``DictionaryProvider``. as shown in the example below. Note that we don't put the dictionary vectors in the same ``VectorSchemaRoot`` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The ``DictionaryProvider`` is simply a map of identifiers to ``Dictionary`` objects, where each identifier is a long value. In the above code you will see it as the first argument to the ``DictionaryEncoding`` constructor.
+
+This is where the ``DictionaryEncoding``'s 'id' attribute comes in. This value is used to connect dictionaries to instances of ``VectorSchemaRoot``, using a ``DictionaryProvider``. Here's how that works:
+
+* The ``VectorSchemaRoot`` has a ``Schema`` object containing a list of ``Field`` objects.
+* The field has an attribute called 'dictionary', but it holds a ``DictionaryEncoding`` rather than a ``Dictionary``
+* As mentioned, the ``DictionaryProvider`` holds dictionaries indexed by a long value. This value is the id from your ``DictionaryEncoding``.
+* To retrieve the dictionary for a vector in a ``VectorSchemaRoot``, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a ``DictionaryProvider`` is handy for managing the dictionaries associated with a ``VectorSchemaRoot``. More importantly, it helps package the dictionaries for a ``VectorSchemaRoot`` when it's written. The classes ``ArrowFileWriter`` and ``ArrowStreamWriter`` both accept an optional ``DictionaryProvider`` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:doc:`ipc`). ``ArrowReader`` and its subclasses also implement the ``DictionaryProvider`` interface.
Review Comment:
```suggestion
As you can see, a ``DictionaryProvider`` is handy for managing the dictionaries associated with a ``VectorSchemaRoot``. More importantly, it helps package the dictionaries for a ``VectorSchemaRoot`` when it's written. The classes ``ArrowFileWriter`` and ``ArrowStreamWriter`` both accept an optional ``DictionaryProvider`` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:doc:`ipc`). ``ArrowReader`` and its subclasses also implement the ``DictionaryProvider`` interface, so you can retrieve the actual dictionaries when reading a file.
```
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a ``VectorSchemaRoot`` with several dictionary-encoded vectors. This is done using an object called a ``DictionaryProvider``. as shown in the example below. Note that we don't put the dictionary vectors in the same ``VectorSchemaRoot`` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
Review Comment:
Extra parens here?
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979918068
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
Huh, I guess Sphinx doesn't complain in this case.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r978983237
##########
docs/source/java/ipc.rst:
##########
@@ -15,6 +15,8 @@
.. specific language governing permissions and limitations
.. under the License.
+.. _ipc:
Review Comment:
note that these labels are global, so best to namespace them
that said: `:doc:` is probably preferable to `:ref:` for this kind of thing if you're just linking to the toplevel, and you don't have to add a reference
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
IIRC BigIntVector is also supported
IIRC other implementations allow unsigned indices, but Java doesn't?
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
Worth linking to the cookbook here? (Should the cookbooks be added via intersphinx for easier linking?)
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
Review Comment:
Maybe briefly explain dictionary encoding, or link to the glossary entry via `:term:`?
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
Do we want to be consistent about linking classes?
FYI: `:class:` doesn't work for Java (I think these are failing or linking to C++?). I've been meaning to build up a workaround so it does work for Java.
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
Also, the reader classes implement DictionaryProvider themselves
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979055429
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
It's probably easiest to just use the double-backticks to put it in a monospace font, since what I'm saying is that the `:class:` markup won't actually link it properly since there's no integration between Javadoc and Sphinx
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
(You should probably have seen build warnings to this effect)
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979252230
##########
docs/source/java/ipc.rst:
##########
@@ -15,6 +15,8 @@
.. specific language governing permissions and limitations
.. under the License.
+.. _ipc:
Review Comment:
switched to doc
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r981274088
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
I took "readers" to mean FieldReaders, but I realized later you meant ArrowReaders.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r981294476
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
Review Comment:
done
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,84 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+Dictionary encoding is a form of compression where values of one type are replaced by values of a smaller type: an array of ints replacing an array of strings is a common example. The mapping between the original values and the replacements is held in a 'dictionary'. Since the dictionary needs only one copy of each of the longer values, the combination of the dictionary and the array of smaller values may use less memory. The more repetitive the original data, the greater the savings.
+
+A ``FieldVector`` can be dictionary encoded for performance or improved memory efficiency. Nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a ``Dictionary``. It will contain the dictionary vector, plus a ``DictionaryEncoding`` object that holds the encoding's metadata and settings values.
+4. Create a ``DictionaryEncoder``.
+5. Call the encode() method on the ``DictionaryEncoder`` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use ``TinyIntVector``, ``SmallIntVector``, ``IntVector``, or ``BigIntVector`` to hold them. You specify the type when you create your ``DictionaryEncoding`` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in ``DictionaryEncoding`` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an ``IntVector``) that can act in place of the original vector (for example, a ``VarCharVector``). When you write the data in arrow format, it is both the new ``IntVector`` plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a ``VectorSchemaRoot`` with several dictionary-encoded vectors. This is done using an object called a ``DictionaryProvider``. as shown in the example below. Note that we don't put the dictionary vectors in the same ``VectorSchemaRoot`` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979055550
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
I don't think there's any information in the cookbook about dictionary encoding, although there is a GitHub issue on the subject
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979049838
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
Though if we're only talking about the DictionaryEncoder here, then it's true. We may want to be more careful differentiating between what's possible in the format, and what particular APIs are capable of.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979051563
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
Java does not support unsigned indexes. Unsigned vectors in Java work with signed values (except Uint2, which uses char arrays IIRC)
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lidavidm commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lidavidm commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979049480
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
Review Comment:
It could come up via IPC, or if you manually build up a vector (by directly building the indices) though?
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lwhite1 commented on a diff in pull request #14213: ARROW-17811: [Java][Doc] Added high-level documentation for Dictionary Encoding in Java
Posted by GitBox <gi...@apache.org>.
lwhite1 commented on code in PR #14213:
URL: https://github.com/apache/arrow/pull/14213#discussion_r979080350
##########
docs/source/java/vector.rst:
##########
@@ -268,6 +268,82 @@ For example, the code below shows how to build a :class:`ListVector` of int's us
}
}
+Dictionary Encoding
+===================
+
+A :class:`FieldVector` can be dictionary encoded for performance or improved memory efficiency. While this is most often done with :class:`VarCharVector`, nearly any type of vector might be encoded if there are many values, but few unique values.
+
+There are a few steps involved in the encoding process:
+
+1. Create a regular, un-encoded vector and populate it
+2. Create a dictionary vector of the same type as the un-encoded vector. This vector must have the same values, but each unique value in the un-encoded vector need appear here only once.
+3. Create a :class:`Dictionary`. It will contain the dictionary vector, plus a :class:`DictionaryEncoding` object that holds the encoding's metadata and settings values.
+4. Create a :class:`DictionaryEncoder`.
+5. Call the encode() method on the :class:`DictionaryEncoder` to produce an encoded version of the original vector.
+6. (Optional) Call the decode() method on the encoded vector to re-create the original values.
+
+The encoded values will be integers. Depending on how many unique values you have, you can use either TinyIntVector, SmallIntVector, or IntVector to hold them. You specify the type when you create your :class:`DictionaryEncoding` instance. You might wonder where those integers come from: the dictionary vector is a regular vector, so the value's index position in that vector is used as its encoded value.
+
+Another critical attribute in :class:`DictionaryEncoding` is the id. It's important to understand how the id is used, so we cover that later in this section.
+
+This result will be a new vector (for example, an IntVector) that can act in place of the original vector (for example, a VarCharVector). When you write the data in arrow format, it is both the new IntVector plus the dictionary that is written: you will need the dictionary later to retrieve the original values.
+
+.. code-block:: Java
+
+ // 1. create a vector for the un-encoded data and populate it
+ VarCharVector unencoded = new VarCharVector("unencoded", allocator);
+ // now put some data in it before continuing
+
+ // 2. create a vector to hold the dictionary and populate it
+ VarCharVector dictionaryVector = new VarCharVector("dictionary", allocator);
+
+ // 3. create a dictionary object
+ Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null));
+
+ // 4. create a dictionary encoder
+ DictionaryEncoder encoder = new DictionaryEncoder.encode(dictionary, allocator);
+
+ // 5. encode the data
+ IntVector encoded = (IntVector) encoder.encode(unencoded);
+
+ // 6. re-create an un-encoded version from the encoded vector
+ VarCharVector decoded = (VarCharVector) encoder.decode(encoded)
+
+One thing we haven't discussed is how to create the dictionary vector from the original un-encoded values. That is left to the library user since a custom method will likely be more efficient than a general utility.
+
+Finally, you can package a number of dictionaries together, which is useful if you're working with a :class:`VectorSchemaRoot` with several dictionary-encoded vectors. This is done using an object called a :class:`DictionaryProvider`. as shown in the example below. Note that we don't put the dictionary vectors in the same :class:`VectorSchemaRoot` as the data vectors, as they will generally have fewer values.
+
+
+.. code-block:: Java
+
+ DictionaryProvider.MapDictionaryProvider provider =
+ new DictionaryProvider.MapDictionaryProvider();
+
+ provider.put(dictionary)))
+
+The :class:`DictionaryProvider` is simply a map of identifiers to :class:`Dictionary` objects, where each identifier is a long value. In the above code you will see it as the first argument to the :class:`DictionaryEncoding` constructor.
+
+This is where the :class:`DictionaryEncoding`'s 'id' attribute comes in. This value is used to connect dictionaries to instances of :class:`VectorSchemaRoot`, using a :class:`DictionaryProvider`. Here's how that works:
+
+* The :class:`VectorSchemaRoot` has a :class:`Schema` object containing a list of :class:`Field` objects.
+* The field has an attribute called 'dictionary', but it holds a :class:`DictionaryEncoding` rather than a :class:`Dictionary`
+* As mentioned, the :class:`DictionaryProvider` holds dictionaries indexed by a long value. This value is the id from your :class:`DictionaryEncoding`.
+* To retrieve the dictionary for a vector in a :class:`VectorSchemaRoot`, you get the field associated with the vector, get its dictionary attribute, and use that object's id to look up the correct dictionary in the provider.
+
+.. code-block:: Java
+
+ // create the encoded vector, the Dictionary and DictionaryProvider as discussed above
+
+ // Create a VectorSchemaRoot with one encoded vector
+ VectorSchemaRoot vsr = new VectorSchemaRoot(List.of(encoded));
+
+ // now we want to decode our vector, so we retrieve its dictionary from the provider
+ Field f = vsr.getField(encoded.getName());
+ DictionaryEncoding encoding = f.getDictionary();
+ Dictionary dictionary = provider.get(encoding.getId());
+
+As you can see, a :class:`DictionaryProvider` is handy for managing the dictionaries associated with a :class:`VectorSchemaRoot`. More importantly, it helps package the dictionaries for a :class:`VectorSchemaRoot` when it's written. The classes :class:`ArrowFileWriter` and :class:`ArrowStreamWriter` both accept an optional :class:`DictionaryProvider` argument for that purpose. You can find example code for writing dictionaries in the documentation for (:ref:`ipc`).
Review Comment:
Sorry, I wasn't suggesting they were linked directly to vector (and I was wrong about 1-1), but I believe a vector can be encoded by at most one dictionary. So my question is, if a Reader reads one vector, why does it need a relationship to a Map of dictionaries, rather than a single dictionary.
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org