Skip to content

Commit

Permalink
Fix name of like function for connector expressions
Browse files Browse the repository at this point in the history
The function is misnamed. It should have been $like all along.
$like_pattern is the name of the internal function to convert
from a varchar to the compiled form of a pattern (i.e., LikeMatcher).
  • Loading branch information
martint committed Sep 6, 2022
1 parent 88f41de commit 7f07c54
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
import static io.trino.spi.expression.StandardFunctions.IS_NULL_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LESS_THAN_OPERATOR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LESS_THAN_OR_EQUAL_OPERATOR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LIKE_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.MODULUS_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.MULTIPLY_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NEGATE_FUNCTION_NAME;
Expand Down Expand Up @@ -252,7 +252,7 @@ protected Optional<Expression> translateCall(Call call)
return translate(getOnlyElement(call.getArguments())).map(argument -> new ArithmeticUnaryExpression(ArithmeticUnaryExpression.Sign.MINUS, argument));
}

if (LIKE_PATTERN_FUNCTION_NAME.equals(call.getFunctionName())) {
if (LIKE_FUNCTION_NAME.equals(call.getFunctionName())) {
return switch (call.getArguments().size()) {
case 2 -> translateLike(call.getArguments().get(0), call.getArguments().get(1), Optional.empty());
case 3 -> translateLike(call.getArguments().get(0), call.getArguments().get(1), Optional.of(call.getArguments().get(2)));
Expand Down Expand Up @@ -742,12 +742,12 @@ protected Optional<ConnectorExpression> visitLikePredicate(LikePredicate node, V
Optional<ConnectorExpression> pattern = process(node.getPattern());
if (value.isPresent() && pattern.isPresent()) {
if (node.getEscape().isEmpty()) {
return Optional.of(new Call(typeOf(node), LIKE_PATTERN_FUNCTION_NAME, List.of(value.get(), pattern.get())));
return Optional.of(new Call(typeOf(node), LIKE_FUNCTION_NAME, List.of(value.get(), pattern.get())));
}

Optional<ConnectorExpression> escape = process(node.getEscape().get());
if (escape.isPresent()) {
return Optional.of(new Call(typeOf(node), LIKE_PATTERN_FUNCTION_NAME, List.of(value.get(), pattern.get(), escape.get())));
return Optional.of(new Call(typeOf(node), LIKE_FUNCTION_NAME, List.of(value.get(), pattern.get(), escape.get())));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import io.airlift.slice.Slices;
import io.trino.likematcher.LikeMatcher;
import io.trino.spi.TrinoException;
import io.trino.spi.expression.StandardFunctions;
import io.trino.type.LikeFunctions;
import org.testng.annotations.Test;

import java.util.Optional;

import static com.google.common.base.Verify.verify;
import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.type.LikeFunctions.isLikePattern;
Expand All @@ -48,14 +45,6 @@ private static Slice offsetHeapSlice(String value)
return result.slice(2, source.length());
}

@Test
public void testFunctionNameConstantsInSync()
{
// Test may need to be updated when this changes.
verify(StandardFunctions.LIKE_PATTERN_FUNCTION_NAME.getCatalogSchema().isEmpty());
assertEquals(StandardFunctions.LIKE_PATTERN_FUNCTION_NAME.getName(), LikeFunctions.LIKE_PATTERN_FUNCTION_NAME);
}

@Test
public void testLikeBasic()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
import static io.trino.spi.expression.StandardFunctions.GREATER_THAN_OR_EQUAL_OPERATOR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.IS_NULL_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LESS_THAN_OR_EQUAL_OPERATOR_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LIKE_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NEGATE_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NOT_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.NULLIF_FUNCTION_NAME;
Expand Down Expand Up @@ -343,7 +343,7 @@ public void testTranslateLike()
new StringLiteral(pattern),
Optional.empty()),
new Call(BOOLEAN,
LIKE_PATTERN_FUNCTION_NAME,
LIKE_FUNCTION_NAME,
List.of(new Variable("varchar_symbol_1", VARCHAR_TYPE),
new Constant(Slices.wrappedBuffer(pattern.getBytes(UTF_8)), createVarcharType(pattern.length())))));

Expand All @@ -354,7 +354,7 @@ public void testTranslateLike()
new StringLiteral(pattern),
Optional.of(new StringLiteral(escape))),
new Call(BOOLEAN,
LIKE_PATTERN_FUNCTION_NAME,
LIKE_FUNCTION_NAME,
List.of(
new Variable("varchar_symbol_1", VARCHAR_TYPE),
new Constant(Slices.wrappedBuffer(pattern.getBytes(UTF_8)), createVarcharType(pattern.length())),
Expand Down
4 changes: 4 additions & 0 deletions core/trino-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@
<code>java.method.addedToInterface</code>
<new>method io.trino.spi.block.BlockBuilder io.trino.spi.block.BlockBuilder::newBlockBuilderLike(int, io.trino.spi.block.BlockBuilderStatus)</new>
</item>
<item>
<code>java.field.removed</code>
<old>field io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME</old>
</item>
</differences>
</revapi.differences>
</analysisConfiguration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private StandardFunctions() {}
*/
public static final FunctionName NEGATE_FUNCTION_NAME = new FunctionName("$negate");

public static final FunctionName LIKE_PATTERN_FUNCTION_NAME = new FunctionName("$like_pattern");
public static final FunctionName LIKE_FUNCTION_NAME = new FunctionName("$like");

/**
* {@code $in(value, array)} returns {@code true} when value is equal to an element of the array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ public void testCallPattern()
Optional.empty()));

assertExpressionPattern(
"$like_pattern(a: varchar(n), b: varchar(m))",
"$like(a: varchar(n), b: varchar(m))",
new CallPattern(
"$like_pattern",
"$like",
List.of(
new ExpressionCapture("a", type("varchar", parameter("n"))),
new ExpressionCapture("b", type("varchar", parameter("m")))),
Optional.empty()));

assertExpressionPattern(
"$like_pattern(a: varchar(n), b: varchar(m)): boolean",
"$like(a: varchar(n), b: varchar(m)): boolean",
new CallPattern(
"$like_pattern",
"$like",
List.of(
new ExpressionCapture("a", type("varchar", parameter("n"))),
new ExpressionCapture("b", type("varchar", parameter("m")))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
import static io.trino.plugin.elasticsearch.ElasticsearchTableHandle.Type.SCAN;
import static io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS;
import static io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static io.trino.spi.expression.StandardFunctions.LIKE_FUNCTION_NAME;
import static io.trino.spi.type.BigintType.BIGINT;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DoubleType.DOUBLE;
Expand Down Expand Up @@ -599,7 +599,7 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C

protected static boolean isSupportedLikeCall(Call call)
{
if (!LIKE_PATTERN_FUNCTION_NAME.equals(call.getFunctionName())) {
if (!LIKE_FUNCTION_NAME.equals(call.getFunctionName())) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public PostgreSqlClient(
.map("$divide(left: integer_type, right: integer_type)").to("left / right")
.map("$modulus(left: integer_type, right: integer_type)").to("left % right")
.map("$negate(value: integer_type)").to("-value")
.map("$like_pattern(value: varchar, pattern: varchar): boolean").to("value LIKE pattern")
.map("$like_pattern(value: varchar, pattern: varchar, escape: varchar(1)): boolean").to("value LIKE pattern ESCAPE escape")
.map("$like(value: varchar, pattern: varchar): boolean").to("value LIKE pattern")
.map("$like(value: varchar, pattern: varchar, escape: varchar(1)): boolean").to("value LIKE pattern ESCAPE escape")
.map("$not($is_null(value))").to("value IS NOT NULL")
.map("$not(value: boolean)").to("NOT value")
.map("$is_null(value)").to("value IS NULL")
Expand Down

0 comments on commit 7f07c54

Please sign in to comment.