forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushdown CAST in JOIN criteria for PostgreSql
Co-authored-by: Semion Paramonow <[email protected]>
- Loading branch information
Showing
3 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/RewriteCast.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,77 @@ | ||
/* | ||
* 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 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.WriteMapping; | ||
import io.trino.spi.connector.ConnectorSession; | ||
import io.trino.spi.expression.Call; | ||
import io.trino.spi.expression.ConnectorExpression; | ||
import io.trino.spi.type.Type; | ||
|
||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
|
||
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.expression; | ||
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.functionName; | ||
import static io.trino.spi.expression.StandardFunctions.CAST_FUNCTION_NAME; | ||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class RewriteCast | ||
implements ConnectorExpressionRule<Call, ParameterizedExpression> | ||
{ | ||
private static final Capture<ConnectorExpression> VALUE = newCapture(); | ||
private final BiFunction<ConnectorSession, Type, WriteMapping> toWriteMapping; | ||
|
||
public RewriteCast(BiFunction<ConnectorSession, Type, WriteMapping> toWriteMapping) | ||
{ | ||
this.toWriteMapping = requireNonNull(toWriteMapping, "toWriteMapping is null"); | ||
} | ||
|
||
@Override | ||
public Pattern<Call> getPattern() | ||
{ | ||
return call() | ||
.with(functionName().equalTo(CAST_FUNCTION_NAME)) | ||
.with(argumentCount().equalTo(1)) | ||
.with(argument(0).matching(expression().capturedAs(VALUE))); | ||
} | ||
|
||
@Override | ||
public Optional<ParameterizedExpression> rewrite(Call call, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
{ | ||
Type trinoType = call.getType(); | ||
ConnectorExpression capturedValue = captures.get(VALUE); | ||
|
||
Optional<ParameterizedExpression> value = context.defaultRewrite(capturedValue); | ||
if (value.isEmpty()) { | ||
// if argument is a call chain that can't be rewritten, then we can't push it down | ||
return Optional.empty(); | ||
} | ||
|
||
String targetType = toWriteMapping.apply(context.getSession(), trinoType).getDataType(); | ||
|
||
return Optional.of(new ParameterizedExpression( | ||
format("CAST(%s AS %s)", value.get().expression(), targetType), | ||
value.get().parameters())); | ||
} | ||
} |
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