Skip to content
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 @@ -118,7 +118,9 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -975,8 +977,28 @@ public static Expression processInPredicate(InPredicate inPredicate) {
}
return inPredicate;
}
// process string literal with numeric
boolean hitString = false;
List<Expression> newOptions = new ArrayList<>(inPredicate.getOptions());
if (!(inPredicate.getCompareExpr().getDataType().isStringLikeType())) {
ListIterator<Expression> iterator = newOptions.listIterator();
while (iterator.hasNext()) {
Expression origOption = iterator.next();
if (origOption instanceof Literal && ((Literal) origOption).isStringLikeLiteral()) {
Optional<Expression> option = TypeCoercionUtils.characterLiteralTypeCoercion(
((Literal) origOption).getStringValue(), inPredicate.getCompareExpr().getDataType());
if (option.isPresent()) {
iterator.set(option.get());
hitString = true;
}
}
}
}
final InPredicate fmtInPredicate =
hitString ? new InPredicate(inPredicate.getCompareExpr(), newOptions) : inPredicate;

Optional<DataType> optionalCommonType = TypeCoercionUtils.findWiderCommonTypeForComparison(
inPredicate.children()
fmtInPredicate.children()
.stream()
.map(Expression::getDataType).collect(Collectors.toList()),
true);
Expand All @@ -999,18 +1021,18 @@ public static Expression processInPredicate(InPredicate inPredicate) {
if (optionalCommonType.isPresent()) {
optionalCommonType = Optional.of(downgradeDecimalAndDateLikeType(
optionalCommonType.get(),
inPredicate.getCompareExpr(),
inPredicate.getOptions().toArray(new Expression[0])));
fmtInPredicate.getCompareExpr(),
fmtInPredicate.getOptions().toArray(new Expression[0])));
}

return optionalCommonType
.map(commonType -> {
List<Expression> newChildren = inPredicate.children().stream()
List<Expression> newChildren = fmtInPredicate.children().stream()
.map(e -> TypeCoercionUtils.castIfNotSameType(e, commonType))
.collect(Collectors.toList());
return inPredicate.withChildren(newChildren);
return fmtInPredicate.withChildren(newChildren);
})
.orElse(inPredicate);
.orElse(fmtInPredicate);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,27 @@ public void testProcessComparisonPredicateDowngrade() {
datetimeDowngrade = (EqualTo) TypeCoercionUtils.processComparisonPredicate(datetimeDowngrade);
Assertions.assertEquals(DateTimeType.INSTANCE, datetimeDowngrade.left().getDataType());
}

@Test
public void testProcessInStringCoercion() {
// BigInt slot vs String literal
InPredicate bigintString = new InPredicate(
new SlotReference("c1", BigIntType.INSTANCE),
ImmutableList.of(
new VarcharLiteral("200"),
new VarcharLiteral("922337203685477001")));
bigintString = (InPredicate) TypeCoercionUtils.processInPredicate(bigintString);
Assertions.assertEquals(BigIntType.INSTANCE, bigintString.getCompareExpr().getDataType());
Assertions.assertEquals(BigIntType.INSTANCE, bigintString.getOptions().get(0).getDataType());

// SmallInt slot vs String literal
InPredicate smallIntString = new InPredicate(
new SlotReference("c1", SmallIntType.INSTANCE),
ImmutableList.of(
new DecimalLiteral(new BigDecimal("987654.321")),
new VarcharLiteral("922337203685477001")));
smallIntString = (InPredicate) TypeCoercionUtils.processInPredicate(smallIntString);
Assertions.assertEquals(DecimalV3Type.createDecimalV3Type(23, 3), smallIntString.getCompareExpr().getDataType());
Assertions.assertEquals(DecimalV3Type.createDecimalV3Type(23, 3), smallIntString.getOptions().get(0).getDataType());
}
}