Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.algorithm.dictionary;

import java.util.HashMap;

import org.apache.arrow.memory.util.ArrowBufPointer;
import org.apache.arrow.memory.util.hash.ArrowBufHasher;
import org.apache.arrow.memory.util.hash.SimpleHasher;
import org.apache.arrow.vector.BaseIntVector;
import org.apache.arrow.vector.ElementAddressableVector;

/**
* Dictionary encoder based on hash table.
* @param <E> encoded vector type.
* @param <D> decoded vector type, which is also the dictionary type.
*/
public class HashTableDictionaryEncoder<E extends BaseIntVector, D extends ElementAddressableVector> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are generic necessary here, what value do they provide?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in general, a generic type makes the parameter type fixed in compilation time, which saves some effort at run time to look up the virtual table for the correct implementation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a reference? My understanding was these types are erased. The only times that types can be referenced at runtime is a subclass is create which the pattern that Guice's TypeLiteral uses

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emkornfield
I am sorry because my previous comment makes no sense. I should have thought carefully before uttering a comment.

In general, generic has almost no impact on performance due to type erasure, as you have indicated. The only impact (if any) is related to type cast.

The benefit of generic is that it gives the Java compiler a chance to discover potential (type related) errors in the code, and prevent them from happening at runtime proactively.

This is a general problem. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type of errors would the compiler detect using generics that it wouldn't detected if you replaces all references to E with BaseIntVector and D with ElementAddressableVector?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give a concrete example from this PR (I am not sure if it is appropriate).

For this PR, the encoder has a constructor like this,

public HashTableDictionaryEncoder(D dictionary)

and a encode method like this,

public void encode(D input, E output)

The generic makes sure that the input parameter to the constructor and the first parameter to the encode method must have the same type.

If we do not use generic, the constructor should be:

public HashTableDictionaryEncoder(ElementAddressableVector dictionary)

and the encode method should be

public void encode(ElementAddressableVector input, ValueVector output)

With the above code, it is possible to construct the encoder with a IntVector, and call the encode method with a VarCharVector. The compiler cannot detect this error, but an exception will be thrown at runtime.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That is a good example for "D" is there similar one for "E"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I think there is a similar example for E (I am not sure if it is appropriate).

Suppose we have an specific IPC specification to encode VarCharVector with an IntVector.
With generic, we can declare a encoder like this HashTableEncoder<VarCharVector, IntVector>.

If we break the specification above to encode a VarCharVector by a BigIntVector by calling:

BigIntVector out = ...
encoder.encode(in, out);

With generic, this problem will be detected by the java compiler.

However, without generic, this problem will only be detected at runtime, because BigIntVector is also a sub-type oc BaseIntVector.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emkornfield
BTW, do you think we should support ListVector/MapVector, etc. with generic types?


/**
* The dictionary for encoding/decoding.
* It must be sorted.
*/
private final D dictionary;

/**
* The hasher used to compute the hash code.
*/
private final ArrowBufHasher hasher;

/**
* A flag indicating if null should be encoded.
*/
private final boolean encodeNull;

/**
* The hash map for distinct dictionary entries.
* The key is the pointer to the dictionary element, whereas the value is the index in the dictionary.
*/
private HashMap<ArrowBufPointer, Integer> hashMap = new HashMap<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a dictionary implementation in Vector that doesn't require boxing/unboxing. Is it possible to use that (or does that limit the ability to use a custom hasher)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The dictionary implementation no longer requries boxing/unboxing. However, it does not support a custom hasher.
I think solving this problem requires some non-trivial changes to our code base. So can we do it in the future in a separate issue?


/**
* The pointer used to probe each element to encode.
*/
private ArrowBufPointer reusablePointer;

/**
* Constructs a dictionary encoder.
* @param dictionary the dictionary.
*
*/
public HashTableDictionaryEncoder(D dictionary) {
this(dictionary, false);
}

/**
* Constructs a dictionary encoder.
* @param dictionary the dictionary.
* @param encodeNull a flag indicating if null should be encoded.
* It determines the behaviors for processing null values in the input during encoding/decoding.
* <li>
* For encoding, when a null is encountered in the input,
* 1) If the flag is set to true, the encoder searches for the value in the dictionary,
* and outputs the index in the dictionary.
* 2) If the flag is set to false, the encoder simply produces a null in the output.
* </li>
* <li>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decodingis not longer done in this class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Thank you.
The descriptions about decoding is removed.

* For decoding, when a null is encountered in the input,
* 1) If the flag is set to true, the decoder should never expect a null in the input.
* 2) If set to false, the decoder simply produces a null in the output.
* </li>
*/
public HashTableDictionaryEncoder(D dictionary, boolean encodeNull) {
this(dictionary, encodeNull, SimpleHasher.INSTANCE);
}

/**
* Constructs a dictionary encoder.
* @param dictionary the dictionary.
* @param encodeNull a flag indicating if null should be encoded.
* It determines the behaviors for processing null values in the input during encoding.
* When a null is encountered in the input,
* 1) If the flag is set to true, the encoder searches for the value in the dictionary,
* and outputs the index in the dictionary.
* 2) If the flag is set to false, the encoder simply produces a null in the output.
* @param hasher the hasher used to calculate the hash code.
*/
public HashTableDictionaryEncoder(D dictionary, boolean encodeNull, ArrowBufHasher hasher) {
this.dictionary = dictionary;
this.hasher = hasher;
this.encodeNull = encodeNull;

reusablePointer = new ArrowBufPointer(hasher);

buildHashMap();
}

private void buildHashMap() {
for (int i = 0; i < dictionary.getValueCount(); i++) {
ArrowBufPointer pointer = new ArrowBufPointer(hasher);
dictionary.getDataPointer(i, pointer);
hashMap.put(pointer, i);
}
}

/**
* Encodes an input vector by a hash table.
* So the algorithm takes O(n) time, where n is the length of the input vector.
*
* @param input the input vector.
* @param output the output vector.
**/
public void encode(D input, E output) {
for (int i = 0; i < input.getValueCount(); i++) {
if (!encodeNull && input.isNull(i)) {
continue;
}

input.getDataPointer(i, reusablePointer);
Integer index = hashMap.get(reusablePointer);

if (index == null) {
throw new IllegalArgumentException("The data element is not found in the dictionary");
}
output.setWithPossibleTruncate(i, index);
}
output.setValueCount(input.getValueCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,11 @@ public SearchDictionaryEncoder(D dictionary, VectorValueComparator<D> comparator
* @param dictionary the dictionary. It must be in sorted order.
* @param comparator the criteria for sorting.
* @param encodeNull a flag indicating if null should be encoded.
* It determines the behaviors for processing null values in the input during encoding/decoding.
* <li>
* For encoding, when a null is encountered in the input,
* 1) If the flag is set to true, the encoder searches for the value in the dictionary,
* and outputs the index in the dictionary.
* 2) If the flag is set to false, the encoder simply produces a null in the output.
* </li>
* <li>
* For decoding, when a null is encountered in the input,
* 1) If the flag is set to true, the decoder should never expect a null in the input.
* 2) If set to false, the decoder simply produces a null in the output.
* </li>
* It determines the behaviors for processing null values in the input during encoding.
* When a null is encountered in the input,
* 1) If the flag is set to true, the encoder searches for the value in the dictionary,
* and outputs the index in the dictionary.
* 2) If the flag is set to false, the encoder simply produces a null in the output.
*/
public SearchDictionaryEncoder(D dictionary, VectorValueComparator<D> comparator, boolean encodeNull) {
this.dictionary = dictionary;
Expand Down
Loading