Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix varchar to numeric coercion for hive tables #20766

Merged
Merged
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
Expand Up @@ -74,6 +74,7 @@
import static io.trino.plugin.hive.coercions.DecimalCoercers.createDoubleToDecimalCoercer;
import static io.trino.plugin.hive.coercions.DecimalCoercers.createIntegerNumberToDecimalCoercer;
import static io.trino.plugin.hive.coercions.DecimalCoercers.createRealToDecimalCoercer;
import static io.trino.plugin.hive.coercions.VarcharToIntegralNumericCoercers.createVarcharToIntegerNumberCoercer;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.block.ColumnarArray.toColumnarArray;
import static io.trino.spi.block.ColumnarMap.toColumnarMap;
Expand Down Expand Up @@ -107,7 +108,7 @@ public static Type createTypeFromCoercer(TypeManager typeManager, HiveType fromH
return Optional.of(new IntegerNumberToVarcharCoercer<>(fromType, toVarcharType));
}
if (fromType instanceof VarcharType fromVarcharType && (toHiveType.equals(HIVE_BYTE) || toHiveType.equals(HIVE_SHORT) || toHiveType.equals(HIVE_INT) || toHiveType.equals(HIVE_LONG))) {
return Optional.of(new VarcharToIntegerNumberCoercer<>(fromVarcharType, toType));
return Optional.of(createVarcharToIntegerNumberCoercer(fromVarcharType, toType, coercionContext.isOrcFile));
}
if (fromType instanceof VarcharType fromVarcharType && toHiveType.equals(HIVE_BOOLEAN)) {
return Optional.of(createVarcharToBooleanCoercer(fromVarcharType, coercionContext.isOrcFile()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.TrinoException;
import io.trino.spi.block.Block;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.spi.type.SmallintType.SMALLINT;
import static io.trino.spi.type.TinyintType.TINYINT;
import static java.lang.String.format;

public final class VarcharToIntegralNumericCoercers
{
private VarcharToIntegralNumericCoercers()
{
}

public static TypeCoercer<VarcharType, ? extends Type> createVarcharToIntegerNumberCoercer(VarcharType fromType, Type toType, boolean isOrcFile)
{
return isOrcFile ? new OrcVarcharToIntegralNumericCoercer<>(fromType, toType) : new VarcharToIntegralNumericCoercer<>(fromType, toType);
}

// For coercions logic for non ORC file format is derived from
// https://github.com/apache/hive/blob/4df4d75bf1e16fe0af75aad0b4179c34c07fc975/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java#L683
public static class VarcharToIntegralNumericCoercer<T extends Type>
extends TypeCoercer<VarcharType, T>
{
public VarcharToIntegralNumericCoercer(VarcharType fromType, T toType)
{
super(fromType, toType);
checkArgument(
toType.equals(TINYINT) || toType.equals(SMALLINT) || toType.equals(INTEGER) || toType.equals(BIGINT),
"Unsupported datatype for coercion from varchar to %s", toType);
}

@Override
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position)
{
try {
String valueToBeCoerced = fromType.getSlice(block, position).toStringUtf8();
if (toType.equals(TINYINT) || toType.equals(SMALLINT) || toType.equals(INTEGER)) {
int intValue = Integer.parseInt(valueToBeCoerced);
if (toType.equals(TINYINT)) {
toType.writeLong(blockBuilder, (byte) intValue);
}
else if (toType.equals(SMALLINT)) {
toType.writeLong(blockBuilder, (short) intValue);
}
else {
toType.writeLong(blockBuilder, intValue);
}
}
else if (toType.equals(BIGINT)) {
toType.writeLong(blockBuilder, Long.parseLong(valueToBeCoerced));
}
}
catch (NumberFormatException e) {
blockBuilder.appendNull();
}
}
}

// For ORC file format, if the numeric representation crosses the range of a given datatype, it is treated as null
// https://github.com/apache/orc/blob/39368b5910032e0dbb257e5ebc98061e50a5e1e1/java/core/src/java/org/apache/orc/impl/ConvertTreeReaderFactory.java#L330
public static class OrcVarcharToIntegralNumericCoercer<T extends Type>
extends TypeCoercer<VarcharType, T>
{
private final long minValue;
private final long maxValue;

public OrcVarcharToIntegralNumericCoercer(VarcharType fromType, T toType)
{
super(fromType, toType);
if (toType.equals(TINYINT)) {
minValue = Byte.MIN_VALUE;
maxValue = Byte.MAX_VALUE;
}
else if (toType.equals(SMALLINT)) {
minValue = Short.MIN_VALUE;
maxValue = Short.MAX_VALUE;
}
else if (toType.equals(INTEGER)) {
minValue = Integer.MIN_VALUE;
maxValue = Integer.MAX_VALUE;
}
else if (toType.equals(BIGINT)) {
minValue = Long.MIN_VALUE;
maxValue = Long.MAX_VALUE;
}
else {
throw new TrinoException(NOT_SUPPORTED, format("Could not create Coercer from varchar to %s", toType));
}
}

@Override
protected void applyCoercedValue(BlockBuilder blockBuilder, Block block, int position)
{
try {
long value = Long.parseLong(fromType.getSlice(block, position).toStringUtf8());
if (minValue <= value && value <= maxValue) {
toType.writeLong(blockBuilder, value);
}
else {
blockBuilder.appendNull();
}
}
catch (NumberFormatException e) {
blockBuilder.appendNull();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
import io.trino.plugin.hive.coercions.TypeCoercer;
import io.trino.plugin.hive.coercions.VarcharToDoubleCoercer;
import io.trino.plugin.hive.coercions.VarcharToFloatCoercer;
import io.trino.plugin.hive.coercions.VarcharToIntegralNumericCoercers.OrcVarcharToIntegralNumericCoercer;
import io.trino.spi.type.BigintType;
import io.trino.spi.type.BooleanType;
import io.trino.spi.type.DateType;
import io.trino.spi.type.DecimalType;
import io.trino.spi.type.DoubleType;
import io.trino.spi.type.IntegerType;
import io.trino.spi.type.RealType;
import io.trino.spi.type.SmallintType;
import io.trino.spi.type.TimestampType;
import io.trino.spi.type.TinyintType;
import io.trino.spi.type.Type;
import io.trino.spi.type.VarcharType;

Expand Down Expand Up @@ -93,6 +98,18 @@ private OrcTypeTranslator() {}
if (toTrinoType instanceof DoubleType) {
return Optional.of(new VarcharToDoubleCoercer(createUnboundedVarcharType(), true));
}
if (toTrinoType instanceof TinyintType tinyintType) {
return Optional.of(new OrcVarcharToIntegralNumericCoercer<>(createUnboundedVarcharType(), tinyintType));
}
if (toTrinoType instanceof SmallintType smallintType) {
return Optional.of(new OrcVarcharToIntegralNumericCoercer<>(createUnboundedVarcharType(), smallintType));
}
if (toTrinoType instanceof IntegerType integerType) {
return Optional.of(new OrcVarcharToIntegralNumericCoercer<>(createUnboundedVarcharType(), integerType));
}
if (toTrinoType instanceof BigintType bigintType) {
return Optional.of(new OrcVarcharToIntegralNumericCoercer<>(createUnboundedVarcharType(), bigintType));
}
return Optional.empty();
}
if (fromOrcType == DOUBLE && toTrinoType instanceof VarcharType varcharType) {
Expand Down
Loading
Loading