Skip to content

Commit

Permalink
Define constant for the LIKE function call
Browse files Browse the repository at this point in the history
Connectors should be able to refer to known constants for builtin
functions. This is especially true for function calls resulting from
desugaring, i.e. otherwise undocumented functions.
  • Loading branch information
findepi committed Mar 4, 2022
1 parent b6eba77 commit ebbcc0c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.trino.spi.expression.Constant;
import io.trino.spi.expression.FieldDereference;
import io.trino.spi.expression.FunctionName;
import io.trino.spi.expression.StandardFunctions;
import io.trino.spi.expression.Variable;
import io.trino.spi.type.Decimals;
import io.trino.spi.type.RowType;
Expand Down Expand Up @@ -62,7 +63,6 @@
import static io.airlift.slice.SliceUtf8.countCodePoints;
import static io.trino.SystemSessionProperties.isComplexExpressionPushdown;
import static io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression;
import static io.trino.type.LikeFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static java.util.Objects.requireNonNull;

public final class ConnectorExpressionTranslator
Expand Down Expand Up @@ -126,17 +126,18 @@ protected Optional<Expression> translateCall(Call call)
if (call.getFunctionName().getCatalogSchema().isPresent()) {
return Optional.empty();
}

// TODO Support ESCAPE character
if (StandardFunctions.LIKE_PATTERN_FUNCTION_NAME.equals(call.getFunctionName()) && call.getArguments().size() == 2) {
return translateLike(call.getArguments().get(0), call.getArguments().get(1));
}

QualifiedName name = QualifiedName.of(call.getFunctionName().getName());
List<TypeSignature> argumentTypes = call.getArguments().stream()
.map(argument -> argument.getType().getTypeSignature())
.collect(toImmutableList());
ResolvedFunction resolved = plannerContext.getMetadata().resolveFunction(session, name, TypeSignatureProvider.fromTypeSignatures(argumentTypes));

// TODO Support ESCAPE character
if (LIKE_PATTERN_FUNCTION_NAME.equals(resolved.getSignature().getName()) && call.getArguments().size() == 2) {
return translateLike(call.getArguments().get(0), call.getArguments().get(1));
}

FunctionCallBuilder builder = FunctionCallBuilder.resolve(session, plannerContext.getMetadata())
.setName(name);
for (int i = 0; i < call.getArguments().size(); i++) {
Expand Down Expand Up @@ -275,7 +276,7 @@ protected Optional<ConnectorExpression> visitLikePredicate(LikePredicate node, V
Optional<ConnectorExpression> value = process(node.getValue());
Optional<ConnectorExpression> pattern = process(node.getPattern());
if (value.isPresent() && pattern.isPresent()) {
return Optional.of(new Call(typeOf(node), new FunctionName(LIKE_PATTERN_FUNCTION_NAME), List.of(value.get(), pattern.get())));
return Optional.of(new Call(typeOf(node), StandardFunctions.LIKE_PATTERN_FUNCTION_NAME, List.of(value.get(), pattern.get())));
}
}
return Optional.empty();
Expand Down
10 changes: 10 additions & 0 deletions core/trino-main/src/test/java/io/trino/sql/TestLikeFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import io.airlift.slice.Slices;
import io.trino.operator.scalar.AbstractTestFunctions;
import io.trino.spi.TrinoException;
import io.trino.spi.expression.StandardFunctions;
import io.trino.type.JoniRegexp;
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 @@ -47,6 +49,14 @@ 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 @@ -40,6 +40,7 @@
import java.util.Optional;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static io.trino.spi.type.BooleanType.BOOLEAN;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static io.trino.spi.type.IntegerType.INTEGER;
Expand All @@ -50,7 +51,6 @@
import static io.trino.sql.planner.TestingPlannerContext.PLANNER_CONTEXT;
import static io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer;
import static io.trino.transaction.TransactionBuilder.transaction;
import static io.trino.type.LikeFunctions.LIKE_PATTERN_FUNCTION_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;

Expand Down Expand Up @@ -96,7 +96,7 @@ public void testTranslationToConnectorExpression()
new StringLiteral(pattern),
Optional.empty()),
Optional.of(new Call(BOOLEAN,
new FunctionName(LIKE_PATTERN_FUNCTION_NAME),
LIKE_PATTERN_FUNCTION_NAME,
List.of(new Variable("varchar_symbol_1", VARCHAR_TYPE),
new Constant(Slices.wrappedBuffer(pattern.getBytes(UTF_8)), createVarcharType(pattern.length()))))));

Expand Down Expand Up @@ -131,7 +131,7 @@ public void testTranslationFromConnectorExpression()
String pattern = "%pattern%";
assertTranslationFromConnectorExpression(
new Call(VARCHAR_TYPE,
new FunctionName(LIKE_PATTERN_FUNCTION_NAME),
LIKE_PATTERN_FUNCTION_NAME,
List.of(new Variable("varchar_symbol_1", VARCHAR_TYPE),
new Constant(Slices.wrappedBuffer(pattern.getBytes(UTF_8)), createVarcharType(pattern.length())))),
new LikePredicate(new SymbolReference("varchar_symbol_1"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.spi.expression;

public final class StandardFunctions
{
private StandardFunctions() {}

public static final FunctionName LIKE_PATTERN_FUNCTION_NAME = new FunctionName("$like_pattern");
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import io.trino.spi.expression.Call;
import io.trino.spi.expression.ConnectorExpression;
import io.trino.spi.expression.Constant;
import io.trino.spi.expression.FunctionName;
import io.trino.spi.expression.Variable;
import io.trino.spi.predicate.Domain;
import io.trino.spi.predicate.TupleDomain;
Expand Down Expand Up @@ -88,6 +87,7 @@
import static io.trino.plugin.elasticsearch.ElasticsearchTableHandle.Type.QUERY;
import static io.trino.plugin.elasticsearch.ElasticsearchTableHandle.Type.SCAN;
import static io.trino.spi.StandardErrorCode.INVALID_ARGUMENTS;
import static io.trino.spi.expression.StandardFunctions.LIKE_PATTERN_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 @@ -544,7 +544,7 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
if (expression instanceof Call) {
Call call = (Call) expression;
// TODO Support ESCAPE character when it's pushed down by the engine
if (new FunctionName("$like_pattern").equals(call.getFunctionName()) && call.getArguments().size() == 2 &&
if (LIKE_PATTERN_FUNCTION_NAME.equals(call.getFunctionName()) && call.getArguments().size() == 2 &&
call.getArguments().get(0) instanceof Variable && call.getArguments().get(1) instanceof Constant) {
String columnName = ((Variable) call.getArguments().get(0)).getName();
Object pattern = ((Constant) call.getArguments().get(1)).getValue();
Expand Down

0 comments on commit ebbcc0c

Please sign in to comment.