-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support cast projection pushdown in oracle
Co-authored-by: Sasha Sheikin <[email protected]>
- Loading branch information
Showing
3 changed files
with
251 additions
and
2 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
108 changes: 108 additions & 0 deletions
108
plugin/trino-oracle/src/main/java/io/trino/plugin/oracle/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,108 @@ | ||
/* | ||
* 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.oracle; | ||
|
||
import io.trino.matching.Capture; | ||
import io.trino.matching.Captures; | ||
import io.trino.matching.Pattern; | ||
import io.trino.plugin.base.projection.ProjectFunctionRule; | ||
import io.trino.plugin.jdbc.JdbcExpression; | ||
import io.trino.plugin.jdbc.JdbcTypeHandle; | ||
import io.trino.plugin.jdbc.WriteMapping; | ||
import io.trino.plugin.jdbc.expression.ParameterizedExpression; | ||
import io.trino.spi.connector.ConnectorSession; | ||
import io.trino.spi.expression.Call; | ||
import io.trino.spi.expression.ConnectorExpression; | ||
import io.trino.spi.type.CharType; | ||
import io.trino.spi.type.Type; | ||
import io.trino.spi.type.VarcharType; | ||
import oracle.jdbc.OracleTypes; | ||
|
||
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.plugin.oracle.OracleClient.ORACLE_CHAR_MAX_CHARS; | ||
import static io.trino.plugin.oracle.OracleClient.ORACLE_VARCHAR2_MAX_CHARS; | ||
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 ProjectFunctionRule<JdbcExpression, 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<JdbcExpression> rewrite(ConnectorExpression projectionExpression, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
{ | ||
Call call = (Call) projectionExpression; | ||
Type trinoType = call.getType(); | ||
ConnectorExpression capturedValue = captures.get(VALUE); | ||
|
||
Optional<ParameterizedExpression> value = context.rewriteExpression(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(); | ||
} | ||
Optional<JdbcTypeHandle> targetTypeHandle = toJdbcTypeHandle(trinoType); | ||
if (targetTypeHandle.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
String targetType = toWriteMapping.apply(context.getSession(), trinoType).getDataType(); | ||
|
||
return Optional.of(new JdbcExpression( | ||
format("CAST(%s AS %s)", value.get().expression(), targetType), | ||
value.get().parameters(), | ||
targetTypeHandle.get())); | ||
} | ||
|
||
private static Optional<JdbcTypeHandle> toJdbcTypeHandle(Type type) | ||
{ | ||
if (type instanceof CharType charType) { | ||
if (charType.getLength() > ORACLE_CHAR_MAX_CHARS) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new JdbcTypeHandle(OracleTypes.CHAR, Optional.of(charType.getBaseName()), Optional.of(charType.getLength()), Optional.empty(), Optional.empty(), Optional.empty())); | ||
} | ||
if (type instanceof VarcharType varcharType) { | ||
if (varcharType.isUnbounded() || varcharType.getBoundedLength() > ORACLE_VARCHAR2_MAX_CHARS) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(new JdbcTypeHandle(OracleTypes.VARCHAR, Optional.of("VARCHAR2"), varcharType.getLength(), Optional.empty(), Optional.empty(), Optional.empty())); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
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