Skip to content

Commit

Permalink
JDBC function predicate pushdown
Browse files Browse the repository at this point in the history
Add support for complex function pushdown in JDBC connectors. This comes
with example implementation for LIKE pushdown in PostgreSQL.
  • Loading branch information
findepi committed Mar 3, 2022
1 parent 4b9bd8a commit 13e52e8
Show file tree
Hide file tree
Showing 28 changed files with 716 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
package io.trino.plugin.base.aggregation;

import com.google.common.collect.ImmutableList;
import io.trino.matching.Captures;
import io.trino.matching.Match;
import io.trino.matching.Pattern;
import io.trino.matching.PatternVisitor;
import io.trino.matching.Property;
import io.trino.plugin.base.expression.ConnectorExpressionPatterns;
import io.trino.spi.connector.AggregateFunction;
import io.trino.spi.expression.ConnectorExpression;
import io.trino.spi.expression.Variable;
Expand All @@ -29,9 +29,6 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;

public final class AggregateFunctionPatterns
{
private AggregateFunctionPatterns() {}
Expand Down Expand Up @@ -85,9 +82,10 @@ public static Pattern<AggregateFunction> basicAggregation()
return Property.property("hasFilter", aggregateFunction -> aggregateFunction.getFilter().isPresent());
}

@Deprecated
public static Pattern<Variable> variable()
{
return Pattern.typeOf(Variable.class);
return ConnectorExpressionPatterns.variable();
}

public static Pattern<List<Variable>> variables()
Expand Down Expand Up @@ -115,16 +113,15 @@ public void accept(PatternVisitor patternVisitor)
};
}

@Deprecated
public static Property<ConnectorExpression, ?, Type> expressionType()
{
return Property.property("type", ConnectorExpression::getType);
return ConnectorExpressionPatterns.type();
}

@Deprecated
public static Predicate<List<? extends ConnectorExpression>> expressionTypes(Type... types)
{
List<Type> expectedTypes = ImmutableList.copyOf(requireNonNull(types, "types is null"));
return expressions -> expectedTypes.equals(expressions.stream()
.map(ConnectorExpression::getType)
.collect(toImmutableList()));
return ConnectorExpressionPatterns.expressionTypes(types);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.base.expression;

import com.google.common.collect.ImmutableList;
import io.trino.matching.Pattern;
import io.trino.matching.Property;
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.type.Type;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class ConnectorExpressionPatterns
{
private ConnectorExpressionPatterns() {}

public static Pattern<ConnectorExpression> expression()
{
return Pattern.typeOf(ConnectorExpression.class);
}

public static Property<ConnectorExpression, ?, Type> type()
{
return Property.property("type", ConnectorExpression::getType);
}

public static Pattern<Call> call()
{
return Pattern.typeOf(Call.class);
}

/**
* @see #functionUnqualifiedName()
*/
public static Property<Call, ?, FunctionName> functionName()
{
return Property.property("functionName", Call::getFunctionName);
}

/**
* @see #functionName()
*/
public static Property<Call, ?, String> functionUnqualifiedName()
{
return Property.optionalProperty("functionUnqualifiedName", call -> {
FunctionName functionName = call.getFunctionName();
if (functionName.getCatalogSchema().isPresent()) {
// The name is qualified.
return Optional.empty();
}
return Optional.of(functionName.getName());
});
}

public static Property<Call, ?, Integer> argumentCount()
{
return Property.property("argumentCount", call -> call.getArguments().size());
}

public static Property<Call, ?, ConnectorExpression> argument(int argument)
{
checkArgument(0 <= argument, "Invalid argument index: %s", argument);
return Property.optionalProperty(format("argument(%s)", argument), call -> {
if (argument < call.getArguments().size()) {
return Optional.of(call.getArguments().get(argument));
}
return Optional.empty();
});
}

public static Property<Call, ?, Type> argumentType(int argument)
{
checkArgument(0 <= argument, "Invalid argument index: %s", argument);
return Property.optionalProperty(format("argumentType(%s)", argument), call -> {
if (argument < call.getArguments().size()) {
return Optional.of(call.getArguments().get(argument).getType());
}
return Optional.empty();
});
}

public static Property<Call, ?, List<Type>> argumentTypes()
{
return Property.property("argumentTypes", call -> call.getArguments().stream()
.map(ConnectorExpression::getType)
.collect(toImmutableList()));
}

public static Pattern<Constant> constant()
{
return Pattern.typeOf(Constant.class);
}

public static Pattern<Variable> variable()
{
return Pattern.typeOf(Variable.class);
}

public static Predicate<List<? extends ConnectorExpression>> expressionTypes(Type... types)
{
List<Type> expectedTypes = ImmutableList.copyOf(requireNonNull(types, "types is null"));
return expressions -> expectedTypes.equals(expressions.stream()
.map(ConnectorExpression::getType)
.collect(toImmutableList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.base.expression;

import com.google.common.collect.ImmutableSet;
import io.trino.matching.Capture;
import io.trino.matching.Match;
import io.trino.matching.Pattern;
import io.trino.plugin.base.expression.ConnectorExpressionRule.RewriteContext;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.expression.ConnectorExpression;

import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import static com.google.common.base.Verify.verify;
import static io.trino.matching.Capture.newCapture;
import static java.util.Objects.requireNonNull;

public final class ConnectorExpressionRewriter<Result>
{
private final Function<String, String> identifierQuote;
private final Set<ConnectorExpressionRule<?, Result>> rules;

public ConnectorExpressionRewriter(Function<String, String> identifierQuote, Set<ConnectorExpressionRule<?, Result>> rules)
{
this.identifierQuote = requireNonNull(identifierQuote, "identifierQuote is null");
this.rules = ImmutableSet.copyOf(requireNonNull(rules, "rules is null"));
}

public Optional<Result> rewrite(ConnectorSession session, ConnectorExpression expression, Map<String, ColumnHandle> assignments)
{
requireNonNull(session, "session is null");
requireNonNull(expression, "expression is null");
requireNonNull(assignments, "assignments is null");

RewriteContext<Result> context = new RewriteContext<>()
{
@Override
public Map<String, ColumnHandle> getAssignments()
{
return assignments;
}

@Override
public Function<String, String> getIdentifierQuote()
{
return identifierQuote;
}

@Override
public ConnectorSession getSession()
{
return session;
}

@Override
public Optional<Result> defaultRewrite(ConnectorExpression expression)
{
return rewrite(expression, this);
}
};

return rewrite(expression, context);
}

private Optional<Result> rewrite(ConnectorExpression expression, RewriteContext<Result> context)
{
for (ConnectorExpressionRule<?, Result> rule : rules) {
Optional<Result> rewritten = rewrite(rule, expression, context);
if (rewritten.isPresent()) {
return rewritten;
}
}

return Optional.empty();
}

private <ExpressionType extends ConnectorExpression> Optional<Result> rewrite(
ConnectorExpressionRule<ExpressionType, Result> rule,
ConnectorExpression expression,
RewriteContext<Result> context)
{
Capture<ExpressionType> expressionCapture = newCapture();
Pattern<ExpressionType> pattern = rule.getPattern().capturedAs(expressionCapture);
Iterator<Match> matches = pattern.match(expression, context).iterator();
while (matches.hasNext()) {
Match match = matches.next();
ExpressionType capturedExpression = match.capture(expressionCapture);
verify(capturedExpression == expression);
Optional<Result> rewritten = rule.rewrite(capturedExpression, match.captures(), context);
if (rewritten.isPresent()) {
return rewritten;
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.base.expression;

import io.trino.matching.Captures;
import io.trino.matching.Pattern;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.expression.ConnectorExpression;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;

public interface ConnectorExpressionRule<ExpressionType extends ConnectorExpression, Result>
{
Pattern<ExpressionType> getPattern();

Optional<Result> rewrite(ExpressionType expression, Captures captures, RewriteContext<Result> context);

interface RewriteContext<Result>
{
default ColumnHandle getAssignment(String name)
{
requireNonNull(name, "name is null");
ColumnHandle columnHandle = getAssignments().get(name);
verifyNotNull(columnHandle, "No assignment for %s", name);
return columnHandle;
}

Map<String, ColumnHandle> getAssignments();

Function<String, String> getIdentifierQuote();

ConnectorSession getSession();

Optional<Result> defaultRewrite(ConnectorExpression expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -403,7 +404,17 @@ protected PreparedQuery prepareQuery(
columns,
columnExpressions,
table.getConstraint(),
split.flatMap(JdbcSplit::getAdditionalPredicate)));
getAdditionalPredicate(table.getConstraintExpressions(), split.flatMap(JdbcSplit::getAdditionalPredicate))));
}

protected static Optional<String> getAdditionalPredicate(List<String> constraintExpressions, Optional<String> splitPredicate)
{
if (constraintExpressions.isEmpty() && splitPredicate.isEmpty()) {
return Optional.empty();
}
return Optional.of(
Stream.concat(constraintExpressions.stream(), splitPredicate.stream())
.collect(joining(" AND ")));
}

@Override
Expand Down
Loading

0 comments on commit 13e52e8

Please sign in to comment.