Skip to content

Commit

Permalink
Encode unbounded varchar value without a CAST
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jun 21, 2021
1 parent d0037b3 commit 2602248
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,12 @@ protected Object visitLikePredicate(LikePredicate node, Object context)
if (!valueType.equals(superType)) {
valueExpression = new Cast(valueExpression, toSqlType(superType), false, typeCoercion.isTypeOnlyCoercion(valueType, superType));
}
Expression patternExpression = toExpression(unescapedPattern, patternType);
if (!patternType.equals(superType)) {
Expression patternExpression;
if (superType instanceof VarcharType) {
patternExpression = toExpression(unescapedPattern, superType);
}
else {
patternExpression = toExpression(unescapedPattern, patternType);
patternExpression = new Cast(patternExpression, toSqlType(superType), false, typeCoercion.isTypeOnlyCoercion(patternType, superType));
}
return new ComparisonExpression(ComparisonExpression.Operator.EQUAL, valueExpression, patternExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ public Expression toExpression(Object object, Type type)
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
Slice value = (Slice) object;
if (varcharType.isUnbounded()) {
return new GenericLiteral("VARCHAR", value.toStringUtf8());
}
StringLiteral stringLiteral = new StringLiteral(value.toStringUtf8());

if (!varcharType.isUnbounded() && varcharType.getBoundedLength() == SliceUtf8.countCodePoints(value)) {
if (varcharType.getBoundedLength() == SliceUtf8.countCodePoints(value)) {
return stringLiteral;
}
return new Cast(stringLiteral, toSqlType(type), false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public void testNonStraddlingJoinExpression()
assertPlan(
"SELECT * FROM orders JOIN lineitem ON orders.orderkey = lineitem.orderkey AND cast(lineitem.linenumber AS varchar) = '2'",
anyTree(
join(INNER, ImmutableList.of(equiJoinClause("LINEITEM_OK", "ORDERS_OK")),
join(INNER, ImmutableList.of(equiJoinClause("ORDERS_OK", "LINEITEM_OK")),
anyTree(
filter("cast(LINEITEM_LINENUMBER as varchar) = cast('2' as varchar)",
tableScan("orders", ImmutableMap.of("ORDERS_OK", "orderkey"))),
anyTree(
filter("cast(LINEITEM_LINENUMBER as varchar) = VARCHAR '2'",
tableScan("lineitem", ImmutableMap.of(
"LINEITEM_OK", "orderkey",
"LINEITEM_LINENUMBER", "linenumber")))),
anyTree(
tableScan("orders", ImmutableMap.of("ORDERS_OK", "orderkey"))))));
"LINEITEM_LINENUMBER", "linenumber")))))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void testEncodeVarchar()
{
assertEncode(utf8Slice("hello"), createVarcharType(5), "'hello'");
assertEncode(utf8Slice("hello"), createVarcharType(13), "CAST('hello' AS varchar(13))");
assertEncode(utf8Slice("hello"), VARCHAR, "CAST('hello' AS varchar)");
assertEncode(utf8Slice("hello"), VARCHAR, "VARCHAR 'hello'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void testPredicateTypeWithCoercion()
output(
ImmutableList.of("DEST_COL_B"),
project(ImmutableMap.of("DEST_COL_B", expression("DEST_COL_B")),
filter("CAST(DEST_COL_A AS VARCHAR) = CAST('foo' AS VARCHAR)",
filter("CAST(DEST_COL_A AS VARCHAR) = VARCHAR 'foo'",
tableScan(
new MockConnectorTableHandle(
DESTINATION_TABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void testApplyTableScanRedirectionWithFilter()
.withSession(MOCK_SESSION)
.matches(
filter(
"DEST_COL = CAST('foo' AS varchar)",
"DEST_COL = VARCHAR 'foo'",
tableScan(
new MockConnectorTableHandle(DESTINATION_TABLE)::equals,
TupleDomain.all(),
Expand All @@ -273,7 +273,7 @@ public void testApplyTableScanRedirectionWithFilter()
project(
ImmutableMap.of("expr", expression("DEST_COL_B")),
filter(
"DEST_COL_A = CAST('foo' AS varchar)",
"DEST_COL_A = VARCHAR 'foo'",
tableScan(
new MockConnectorTableHandle(DESTINATION_TABLE)::equals,
TupleDomain.all(),
Expand Down

0 comments on commit 2602248

Please sign in to comment.