forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow varbinary to varchar coercion for hive tables
- Loading branch information
1 parent
cf5b073
commit e9f78fa
Showing
9 changed files
with
331 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...n/trino-hive/src/main/java/io/trino/plugin/hive/coercions/VarbinaryToVarcharCoercers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.hive.coercions; | ||
|
||
import io.airlift.slice.Slice; | ||
import io.airlift.slice.Slices; | ||
import io.trino.plugin.hive.HiveStorageFormat; | ||
import io.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.type.VarbinaryType; | ||
import io.trino.spi.type.VarcharType; | ||
|
||
import java.nio.charset.CharacterCodingException; | ||
import java.nio.charset.CharsetDecoder; | ||
import java.util.HexFormat; | ||
|
||
import static io.trino.plugin.hive.HiveStorageFormat.ORC; | ||
import static io.trino.plugin.hive.HiveStorageFormat.PARQUET; | ||
import static io.trino.spi.type.VarbinaryType.VARBINARY; | ||
import static io.trino.spi.type.Varchars.truncateToLength; | ||
import static java.nio.charset.CodingErrorAction.REPLACE; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class VarbinaryToVarcharCoercers | ||
{ | ||
private VarbinaryToVarcharCoercers() {} | ||
|
||
public static TypeCoercer<VarbinaryType, VarcharType> createVarbinaryToVarcharCoercer(VarcharType toType, HiveStorageFormat storageFormat) | ||
{ | ||
if (storageFormat == ORC) { | ||
return new OrcVarbinaryToVarcharCoercer(toType); | ||
} | ||
if (storageFormat == PARQUET) { | ||
return new ParquetVarbinaryToVarcharCoercer(toType); | ||
} | ||
return new VarbinaryToVarcharCoercer(toType); | ||
} | ||
|
||
private static class VarbinaryToVarcharCoercer | ||
extends TypeCoercer<VarbinaryType, VarcharType> | ||
{ | ||
public VarbinaryToVarcharCoercer(VarcharType toType) | ||
{ | ||
super(VARBINARY, toType); | ||
} | ||
|
||
@Override | ||
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position) | ||
{ | ||
try { | ||
Slice decodedValue = fromType.getSlice(block, position); | ||
if (toType.isUnbounded()) { | ||
toType.writeSlice(blockBuilder, decodedValue); | ||
return; | ||
} | ||
toType.writeSlice(blockBuilder, truncateToLength(decodedValue, toType.getBoundedLength())); | ||
} | ||
catch (RuntimeException e) { | ||
blockBuilder.appendNull(); | ||
} | ||
} | ||
} | ||
|
||
private static class ParquetVarbinaryToVarcharCoercer | ||
extends TypeCoercer<VarbinaryType, VarcharType> | ||
{ | ||
public ParquetVarbinaryToVarcharCoercer(VarcharType toType) | ||
{ | ||
super(VARBINARY, toType); | ||
} | ||
|
||
@Override | ||
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position) | ||
{ | ||
// Hive's coercion logic for Varbinary to Varchar | ||
// https://github.com/apache/hive/blob/8190d2be7b7165effa62bd21b7d60ef81fb0e4af/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java#L911 | ||
// It uses Hadoop's Text#decode replaces malformed input with a substitution character i.e U+FFFD | ||
// https://github.com/apache/hadoop/blob/706d88266abcee09ed78fbaa0ad5f74d818ab0e9/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java#L414 | ||
CharsetDecoder decoder = UTF_8.newDecoder() | ||
.onMalformedInput(REPLACE) | ||
.onUnmappableCharacter(REPLACE); | ||
|
||
try { | ||
Slice decodedValue = Slices.utf8Slice(decoder.decode(fromType.getSlice(block, position).toByteBuffer()).toString()); | ||
if (toType.isUnbounded()) { | ||
toType.writeSlice(blockBuilder, decodedValue); | ||
return; | ||
} | ||
toType.writeSlice(blockBuilder, truncateToLength(decodedValue, toType.getBoundedLength())); | ||
} | ||
catch (CharacterCodingException e) { | ||
blockBuilder.appendNull(); | ||
} | ||
} | ||
} | ||
|
||
private static class OrcVarbinaryToVarcharCoercer | ||
extends TypeCoercer<VarbinaryType, VarcharType> | ||
{ | ||
private static final HexFormat HEX_FORMAT = HexFormat.of().withDelimiter(" "); | ||
|
||
public OrcVarbinaryToVarcharCoercer(VarcharType toType) | ||
{ | ||
super(VARBINARY, toType); | ||
} | ||
|
||
@Override | ||
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position) | ||
{ | ||
Slice value = fromType.getSlice(block, position); | ||
Slice hexValue = Slices.utf8Slice(HEX_FORMAT.formatHex(value.byteArray(), value.byteArrayOffset(), value.length())); | ||
if (toType.isUnbounded()) { | ||
toType.writeSlice(blockBuilder, hexValue); | ||
return; | ||
} | ||
toType.writeSlice(blockBuilder, truncateToLength(hexValue, toType.getBoundedLength())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...rino-hive/src/test/java/io/trino/plugin/hive/coercions/TestVarbinaryToVarcharCoercer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.hive.coercions; | ||
|
||
import io.airlift.slice.Slice; | ||
import io.airlift.slice.Slices; | ||
import io.trino.plugin.hive.HiveStorageFormat; | ||
import io.trino.plugin.hive.coercions.CoercionUtils.CoercionContext; | ||
import io.trino.spi.block.Block; | ||
import io.trino.spi.type.Type; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.trino.plugin.hive.HiveStorageFormat.ORC; | ||
import static io.trino.plugin.hive.HiveStorageFormat.PARQUET; | ||
import static io.trino.plugin.hive.HiveStorageFormat.RCTEXT; | ||
import static io.trino.plugin.hive.HiveTimestampPrecision.DEFAULT_PRECISION; | ||
import static io.trino.plugin.hive.HiveType.toHiveType; | ||
import static io.trino.plugin.hive.coercions.CoercionUtils.createCoercer; | ||
import static io.trino.spi.predicate.Utils.blockToNativeValue; | ||
import static io.trino.spi.predicate.Utils.nativeValueToBlock; | ||
import static io.trino.spi.type.VarbinaryType.VARBINARY; | ||
import static io.trino.spi.type.VarcharType.VARCHAR; | ||
import static io.trino.spi.type.VarcharType.createVarcharType; | ||
import static io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class TestVarbinaryToVarcharCoercer | ||
{ | ||
private static final byte CONTINUATION_BYTE = (byte) 0b1011_1111; | ||
private static final byte START_4_BYTE = (byte) 0b1111_0111; | ||
private static final byte X_CHAR = (byte) 'X'; | ||
|
||
// Test cases are copied from https://github.com/airlift/slice/blob/master/src/test/java/io/airlift/slice/TestSliceUtf8.java | ||
|
||
@Test | ||
public void testVarbinaryToVarcharCoercion() | ||
{ | ||
assertVarbinaryToVarcharCoercion(Slices.utf8Slice("abc"), VARBINARY, Slices.utf8Slice("abc"), VARCHAR); | ||
assertVarbinaryToVarcharCoercion(Slices.utf8Slice("abc"), VARBINARY, Slices.utf8Slice("ab"), createVarcharType(2)); | ||
// Invalid UTF-8 encoding | ||
assertVarbinaryToVarcharCoercion(Slices.wrappedBuffer(X_CHAR, CONTINUATION_BYTE), VARBINARY, Slices.wrappedBuffer(X_CHAR, CONTINUATION_BYTE), VARCHAR); | ||
assertVarbinaryToVarcharCoercion( | ||
Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE), | ||
VARBINARY, | ||
Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE), | ||
VARCHAR); | ||
assertVarbinaryToVarcharCoercion( | ||
Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, X_CHAR), | ||
VARBINARY, | ||
Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, X_CHAR), | ||
VARCHAR); | ||
assertVarbinaryToVarcharCoercion( | ||
Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xA0, (byte) 0x80), | ||
VARBINARY, | ||
Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xA0, (byte) 0x80), | ||
VARCHAR); | ||
assertVarbinaryToVarcharCoercion( | ||
Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xBF, (byte) 0xBF), | ||
VARBINARY, | ||
Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xBF, (byte) 0xBF), | ||
VARCHAR); | ||
} | ||
|
||
@Test | ||
public void testVarbinaryToVarcharCoercionForParquet() | ||
{ | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.utf8Slice("abc"), VARBINARY, "abc", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.utf8Slice("abc"), VARBINARY, "ab", createVarcharType(2)); | ||
// Invalid UTF-8 encoding | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.wrappedBuffer(X_CHAR, CONTINUATION_BYTE), VARBINARY, "X\uFFFD", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE), VARBINARY, "X\uFFFD\uFFFD\uFFFD\uFFFD", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, X_CHAR), VARBINARY, "X\uFFFD\uFFFD\uFFFD\uFFFDX", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xA0, (byte) 0x80), VARBINARY, "X\uFFFD", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForParquet(Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xBF, (byte) 0xBF), VARBINARY, "X\uFFFD", VARCHAR); | ||
} | ||
|
||
@Test | ||
public void testVarbinaryToVarcharCoercionForOrc() | ||
{ | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.utf8Slice("abc"), VARBINARY, "61 62 63", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.utf8Slice("abc"), VARBINARY, "61", createVarcharType(2)); | ||
// Invalid UTF-8 encoding | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.wrappedBuffer(X_CHAR, CONTINUATION_BYTE), VARBINARY, "58 bf", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE), VARBINARY, "58 f7 bf bf bf", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.wrappedBuffer(X_CHAR, START_4_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, CONTINUATION_BYTE, X_CHAR), VARBINARY, "58 f7 bf bf bf 58", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xA0, (byte) 0x80), VARBINARY, "58 ed a0 80", VARCHAR); | ||
assertVarbinaryToVarcharCoercionForOrc(Slices.wrappedBuffer(X_CHAR, (byte) 0b11101101, (byte) 0xBF, (byte) 0xBF), VARBINARY, "58 ed bf bf", VARCHAR); | ||
} | ||
|
||
private static void assertVarbinaryToVarcharCoercion(Slice actualValue, Type fromType, Slice expectedValue, Type toType) | ||
{ | ||
assertVarbinaryToVarcharCoercion(actualValue, fromType, expectedValue, toType, RCTEXT); | ||
} | ||
|
||
private static void assertVarbinaryToVarcharCoercionForOrc(Slice actualValue, Type fromType, String expectedValue, Type toType) | ||
{ | ||
assertVarbinaryToVarcharCoercion(actualValue, fromType, Slices.utf8Slice(expectedValue), toType, ORC); | ||
} | ||
|
||
private static void assertVarbinaryToVarcharCoercionForParquet(Slice actualValue, Type fromType, String expectedValue, Type toType) | ||
{ | ||
assertVarbinaryToVarcharCoercion(actualValue, fromType, Slices.utf8Slice(expectedValue), toType, PARQUET); | ||
} | ||
|
||
private static void assertVarbinaryToVarcharCoercion(Slice actualValue, Type fromType, Slice expectedValue, Type toType, HiveStorageFormat storageFormat) | ||
{ | ||
Block coercedBlock = createCoercer(TESTING_TYPE_MANAGER, toHiveType(fromType), toHiveType(toType), new CoercionContext(DEFAULT_PRECISION, storageFormat)).orElseThrow() | ||
.apply(nativeValueToBlock(fromType, actualValue)); | ||
assertThat(blockToNativeValue(toType, coercedBlock)) | ||
.isEqualTo(expectedValue); | ||
} | ||
} |
Oops, something went wrong.