-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for complex function pushdown in JDBC connectors. This comes with example implementation for LIKE pushdown in PostgreSQL.
- Loading branch information
Showing
28 changed files
with
716 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...in-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionPatterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...in-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionRewriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...plugin-toolkit/src/main/java/io/trino/plugin/base/expression/ConnectorExpressionRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.