-
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.
Implement complex join pushdown in JDBC connectors
Implement non-deprecated `ConnectorMetadata.applyJoin` overload in `DefaultJdbcMetadata`. Thew old implementation is retained as a safety valve. The new implementation is not limited to the `List<JdbcJoinCondition>` model, so allows pushdown of joins involving more complex expressions, such as arithmetics. The `BaseJdbcClient.implementJoin` and `QueryBuilder.prepareJoinQuery` methods logically changed, but the old implementation is left as the fallback. These methods were extension points, so the old implementations are renamed to ensure implementors are updated. For example, if an implementation was overriding `BaseJdbcClient.implementJoin` it most likely wants to override the new `implementJoin` method as well, and this is reminded about by rename of the old method.
- Loading branch information
Showing
25 changed files
with
441 additions
and
111 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
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
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
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
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
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
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
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
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
94 changes: 94 additions & 0 deletions
94
...se-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteCaseSensitiveComparison.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,94 @@ | ||
/* | ||
* 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.jdbc.expression; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.matching.Capture; | ||
import io.trino.matching.Captures; | ||
import io.trino.matching.Pattern; | ||
import io.trino.plugin.base.expression.ConnectorExpressionRule; | ||
import io.trino.plugin.jdbc.JdbcColumnHandle; | ||
import io.trino.plugin.jdbc.QueryParameter; | ||
import io.trino.spi.expression.Call; | ||
import io.trino.spi.expression.FunctionName; | ||
import io.trino.spi.expression.Variable; | ||
import io.trino.spi.type.VarcharType; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static com.google.common.collect.ImmutableSet.toImmutableSet; | ||
import static io.trino.matching.Capture.newCapture; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.argument; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.argumentCount; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.call; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.functionName; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.type; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.variable; | ||
import static io.trino.plugin.jdbc.CaseSensitivity.CASE_SENSITIVE; | ||
import static io.trino.spi.type.BooleanType.BOOLEAN; | ||
|
||
public class RewriteCaseSensitiveComparison | ||
implements ConnectorExpressionRule<Call, ParameterizedExpression> | ||
{ | ||
private static final Capture<Variable> LEFT = newCapture(); | ||
private static final Capture<Variable> RIGHT = newCapture(); | ||
|
||
private final Pattern<Call> pattern; | ||
|
||
public RewriteCaseSensitiveComparison(Set<ComparisonOperator> enabledOperators) | ||
{ | ||
Set<FunctionName> functionNames = enabledOperators.stream() | ||
.map(ComparisonOperator::getFunctionName) | ||
.collect(toImmutableSet()); | ||
|
||
pattern = call() | ||
.with(type().equalTo(BOOLEAN)) | ||
.with(functionName().matching(functionNames::contains)) | ||
.with(argumentCount().equalTo(2)) | ||
.with(argument(0).matching(variable().with(type().matching(VarcharType.class::isInstance)).capturedAs(LEFT))) | ||
.with(argument(1).matching(variable().with(type().matching(VarcharType.class::isInstance)).capturedAs(RIGHT))); | ||
} | ||
|
||
@Override | ||
public Pattern<Call> getPattern() | ||
{ | ||
return pattern; | ||
} | ||
|
||
@Override | ||
public Optional<ParameterizedExpression> rewrite(Call expression, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
{ | ||
ComparisonOperator comparison = ComparisonOperator.forFunctionName(expression.getFunctionName()); | ||
Variable firstArgument = captures.get(LEFT); | ||
Variable secondArgument = captures.get(RIGHT); | ||
|
||
if (!isCaseSensitive(firstArgument, context) || !isCaseSensitive(secondArgument, context)) { | ||
return Optional.empty(); | ||
} | ||
return context.defaultRewrite(firstArgument).flatMap(first -> | ||
context.defaultRewrite(secondArgument).map(second -> | ||
new ParameterizedExpression( | ||
"(%s) %s (%s)".formatted(first.expression(), comparison.getOperator(), second.expression()), | ||
ImmutableList.<QueryParameter>builder() | ||
.addAll(first.parameters()) | ||
.addAll(second.parameters()) | ||
.build()))); | ||
} | ||
|
||
private static boolean isCaseSensitive(Variable variable, RewriteContext<?> context) | ||
{ | ||
return ((JdbcColumnHandle) context.getAssignment(variable.getName())).getJdbcTypeHandle().getCaseSensitivity().equals(Optional.of(CASE_SENSITIVE)); | ||
} | ||
} |
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.