-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow char to varchar coercion for hive tables
- Loading branch information
1 parent
090aa31
commit 2cd89ce
Showing
8 changed files
with
134 additions
and
2 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
38 changes: 38 additions & 0 deletions
38
plugin/trino-hive/src/main/java/io/trino/plugin/hive/coercions/CharToVarcharCoercer.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,38 @@ | ||
/* | ||
* 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.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.type.CharType; | ||
import io.trino.spi.type.VarcharType; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static io.trino.spi.type.Varchars.truncateToLength; | ||
|
||
public class CharToVarcharCoercer | ||
extends TypeCoercer<CharType, VarcharType> | ||
{ | ||
public CharToVarcharCoercer(CharType fromType, VarcharType toType) | ||
{ | ||
super(fromType, toType); | ||
checkArgument(toType.getBoundedLength() < fromType.getLength(), "Coercer to a wider varchar type should not be required"); | ||
} | ||
|
||
@Override | ||
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position) | ||
{ | ||
toType.writeSlice(blockBuilder, truncateToLength(fromType.getSlice(block, position), 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
54 changes: 54 additions & 0 deletions
54
plugin/trino-hive/src/test/java/io/trino/plugin/hive/coercions/TestCharToVarcharCoercer.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,54 @@ | ||
/* | ||
* 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.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.airlift.slice.Slices.utf8Slice; | ||
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.CharType.createCharType; | ||
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 TestCharToVarcharCoercer | ||
{ | ||
@Test | ||
public void testCharToSmallerVarcharCoercions() | ||
{ | ||
assertCharToVarcharCoercion("a", createCharType(3), "a", createVarcharType(2)); | ||
assertCharToVarcharCoercion(" a", createCharType(3), " a", createVarcharType(2)); | ||
assertCharToVarcharCoercion(" aa", createCharType(4), " a", createVarcharType(2)); | ||
assertCharToVarcharCoercion("a", createCharType(3), "a", createVarcharType(2)); | ||
assertCharToVarcharCoercion("\uD83D\uDCB0\uD83D\uDCB0\uD83D\uDCB0", createCharType(3), "\uD83D\uDCB0", createVarcharType(1)); | ||
assertCharToVarcharCoercion("\uD83D\uDCB0\uD83D\uDCB0", createCharType(7), "\uD83D\uDCB0\uD83D\uDCB0", createVarcharType(6)); | ||
assertCharToVarcharCoercion("\uD83D\uDCB0", createCharType(3), "\uD83D\uDCB0", createVarcharType(1)); | ||
assertCharToVarcharCoercion("\uD83D\uDCB0 \uD83D\uDCB0", createCharType(7), "\uD83D\uDCB0 \uD83D\uDCB0", createVarcharType(6)); | ||
} | ||
|
||
private static void assertCharToVarcharCoercion(String actualValue, Type fromType, String expectedValue, Type toType) | ||
{ | ||
Block coercedBlock = createCoercer(TESTING_TYPE_MANAGER, toHiveType(fromType), toHiveType(toType), new CoercionContext(DEFAULT_PRECISION, false)).orElseThrow() | ||
.apply(nativeValueToBlock(fromType, utf8Slice(actualValue))); | ||
assertThat(blockToNativeValue(toType, coercedBlock)) | ||
.isEqualTo(utf8Slice(expectedValue)); | ||
} | ||
} |
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
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