Skip to content

Commit b8e1ac7

Browse files
liyafan82kou
authored andcommitted
ARROW-8108: [Java] Extract a common interface for dictionary encoders
In this issue, we extract a common interfaces from existing dictionary encoders. This can be useful for scenarios when the client does not care about the encoder implementations. Closes #6608 from liyafan82/fly_0313_intf Authored-by: liyafan82 <fan_li_ya@foxmail.com> Signed-off-by: Micah Kornfield <emkornfield@gmail.com>
1 parent 4ad7c05 commit b8e1ac7

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.algorithm.dictionary;
19+
20+
import org.apache.arrow.vector.BaseIntVector;
21+
import org.apache.arrow.vector.ValueVector;
22+
23+
/**
24+
* A dictionary encoder translates one vector into another one based on a dictionary vector.
25+
* According to Arrow specification, the encoded vector must be an integer based vector, which
26+
* is the index of the original vector element in the dictionary.
27+
* @param <E> type of the encoded vector.
28+
* @param <D> type of the vector to encode. It is also the type of the dictionary vector.
29+
*/
30+
public interface DictionaryEncoder<E extends BaseIntVector, D extends ValueVector> {
31+
32+
/**
33+
* Translates an input vector into an output vector.
34+
* @param input the input vector.
35+
* @param output the output vector. Note that it must be in a fresh state. At least,
36+
* all its validity bits should be clear.
37+
*/
38+
void encode(D input, E output);
39+
}

algorithm/src/main/java/org/apache/arrow/algorithm/dictionary/HashTableDictionaryEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* @param <E> encoded vector type.
3131
* @param <D> decoded vector type, which is also the dictionary type.
3232
*/
33-
public class HashTableDictionaryEncoder<E extends BaseIntVector, D extends ElementAddressableVector> {
33+
public class HashTableDictionaryEncoder<E extends BaseIntVector, D extends ElementAddressableVector>
34+
implements DictionaryEncoder<E, D> {
3435

3536
/**
3637
* The dictionary for encoding/decoding.
@@ -125,6 +126,7 @@ private void buildHashMap() {
125126
* @param input the input vector.
126127
* @param output the output vector.
127128
**/
129+
@Override
128130
public void encode(D input, E output) {
129131
for (int i = 0; i < input.getValueCount(); i++) {
130132
if (!encodeNull && input.isNull(i)) {

algorithm/src/main/java/org/apache/arrow/algorithm/dictionary/LinearDictionaryEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
* @param <E> encoded vector type.
2828
* @param <D> decoded vector type, which is also the dictionary type.
2929
*/
30-
public class LinearDictionaryEncoder<E extends BaseIntVector, D extends ValueVector> {
30+
public class LinearDictionaryEncoder<E extends BaseIntVector, D extends ValueVector>
31+
implements DictionaryEncoder<E, D> {
3132

3233
/**
3334
* The dictionary for encoding.
@@ -80,6 +81,7 @@ public LinearDictionaryEncoder(D dictionary, boolean encodeNull) {
8081
* @param output the output vector. Note that it must be in a fresh state. At least,
8182
* all its validity bits should be clear.
8283
*/
84+
@Override
8385
public void encode(D input, E output) {
8486
for (int i = 0; i < input.getValueCount(); i++) {
8587
if (!encodeNull && input.isNull(i)) {

algorithm/src/main/java/org/apache/arrow/algorithm/dictionary/SearchDictionaryEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
* @param <E> encoded vector type.
2828
* @param <D> decoded vector type, which is also the dictionary type.
2929
*/
30-
public class SearchDictionaryEncoder<E extends BaseIntVector, D extends ValueVector> {
30+
public class SearchDictionaryEncoder<E extends BaseIntVector, D extends ValueVector>
31+
implements DictionaryEncoder<E, D> {
3132

3233
/**
3334
* The dictionary for encoding/decoding.
@@ -79,6 +80,7 @@ public SearchDictionaryEncoder(D dictionary, VectorValueComparator<D> comparator
7980
* @param output the output vector. Note that it must be in a fresh state. At least,
8081
* all its validity bits should be clear.
8182
*/
83+
@Override
8284
public void encode(D input, E output) {
8385
for (int i = 0; i < input.getValueCount(); i++) {
8486
if (!encodeNull && input.isNull(i)) {

0 commit comments

Comments
 (0)