forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement SUBSTRING function pushdown for Teradata connector #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
caf6a4d
Implement substring function rewriting for Teradata connector
sc250072 5cc0c02
Refactor substring length handling in RewriteSubstring class
sc250072 5474274
Merge remote-tracking branch 'origin/Substring_Pushdown' into Substri…
dv255037 973aaf1
Remove unused LENGTH capture in RewriteSubstring class
dv255037 a9a7640
Merge remote-tracking branch 'origin/Substring_Pushdown' into Substri…
dv255037 da7b0a8
Merge branch 'develop' into Substring_Pushdown
sc250072 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
plugin/trino-teradata/src/main/java/io/trino/plugin/teradata/RewriteSubstring.java
This file contains hidden or 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,95 @@ | ||
| /* | ||
| * 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.teradata; | ||
|
|
||
| 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.QueryParameter; | ||
| import io.trino.plugin.jdbc.expression.ParameterizedExpression; | ||
| import io.trino.spi.expression.Call; | ||
| import io.trino.spi.expression.ConnectorExpression; | ||
| import io.trino.spi.expression.FunctionName; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| 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 java.lang.String.format; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| public class RewriteSubstring | ||
| implements ConnectorExpressionRule<Call, ParameterizedExpression> | ||
| { | ||
| private static final Capture<ConnectorExpression> VALUE = newCapture(); | ||
| private static final Capture<ConnectorExpression> START = newCapture(); | ||
|
|
||
| public static final FunctionName SUBSTRING_FUNCTION_NAME = new FunctionName("substring"); | ||
|
|
||
| @Override | ||
| public Pattern<Call> getPattern() | ||
| { | ||
| return call() | ||
| .with(functionName().equalTo(SUBSTRING_FUNCTION_NAME)) | ||
| .with(argumentCount().matching(count -> count == 2 || count == 3)) | ||
| .with(argument(0).matching(expression().capturedAs(VALUE))) | ||
| .with(argument(1).matching(expression().capturedAs(START))); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<ParameterizedExpression> rewrite(Call call, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
| { | ||
| Optional<ParameterizedExpression> value = context.defaultRewrite(captures.get(VALUE)); | ||
| Optional<ParameterizedExpression> start = context.defaultRewrite(captures.get(START)); | ||
|
|
||
| if (value.isEmpty() || start.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (call.getArguments().size() == 3) { | ||
| Optional<ParameterizedExpression> length = context.defaultRewrite(call.getArguments().get(2)); | ||
| if (length.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(new ParameterizedExpression( | ||
| format("SUBSTRING(%s FROM %s FOR %s)", | ||
| value.get().expression(), | ||
| start.get().expression(), | ||
| length.get().expression()), | ||
| combineParameters(value.get(), start.get(), length.get()))); | ||
| } | ||
| else { | ||
| return Optional.of(new ParameterizedExpression( | ||
| format("SUBSTRING(%s FROM %s)", | ||
| value.get().expression(), | ||
| start.get().expression()), | ||
| combineParameters(value.get(), start.get()))); | ||
| } | ||
| } | ||
|
|
||
| private List<QueryParameter> combineParameters(ParameterizedExpression... expressions) | ||
| { | ||
| return Stream.of(expressions) | ||
| .flatMap(expr -> expr.parameters().stream()) | ||
| .collect(toList()); | ||
| } | ||
| } |
135 changes: 135 additions & 0 deletions
135
plugin/trino-teradata/src/main/java/io/trino/plugin/teradata/RewriteSubstringFunction.java
This file contains hidden or 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,135 @@ | ||
| /* | ||
| * 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.teradata; | ||
|
|
||
| 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.projection.ProjectFunctionRule; | ||
| import io.trino.plugin.jdbc.JdbcColumnHandle; | ||
| import io.trino.plugin.jdbc.JdbcExpression; | ||
| import io.trino.plugin.jdbc.JdbcTypeHandle; | ||
| import io.trino.plugin.jdbc.QueryParameter; | ||
| import io.trino.plugin.jdbc.expression.ParameterizedExpression; | ||
| import io.trino.spi.connector.ConnectorTableHandle; | ||
| import io.trino.spi.expression.Call; | ||
| import io.trino.spi.expression.ConnectorExpression; | ||
| import io.trino.spi.expression.FunctionName; | ||
| import io.trino.spi.expression.Variable; | ||
| import io.trino.spi.type.VarcharType; | ||
|
|
||
| import java.sql.JDBCType; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| 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.base.expression.ConnectorExpressionPatterns.type; | ||
| import static java.lang.String.format; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| public class RewriteSubstringFunction | ||
| implements ProjectFunctionRule<JdbcExpression, ParameterizedExpression> | ||
| { | ||
| private static final Capture<ConnectorExpression> VALUE = newCapture(); | ||
|
|
||
| private static final Pattern<Call> PATTERN = call() | ||
| .with(functionName().equalTo(new FunctionName("substring"))) | ||
| .with(type().matching(type -> type instanceof VarcharType)) | ||
| .with(argumentCount().matching(count -> count >= 2 && count <= 3)) | ||
| .with(argument(0).matching(expression().capturedAs(VALUE).with(type().matching(type -> type instanceof VarcharType)))); | ||
|
|
||
| @Override | ||
| public Pattern<? extends ConnectorExpression> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<JdbcExpression> rewrite(ConnectorTableHandle handle, ConnectorExpression projectionExpression, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
| { | ||
| Call call = (Call) projectionExpression; | ||
| ConnectorExpression valueExpression = captures.get(VALUE); | ||
|
|
||
| // Get JDBC type handle for the value expression | ||
| JdbcTypeHandle typeHandle = getTypeHandle(valueExpression, context); | ||
| if (typeHandle == null) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| // Only rewrite for plain VARCHAR JDBC type named "varchar" | ||
| if (JDBCType.valueOf(typeHandle.jdbcType()) != JDBCType.VARCHAR || | ||
| !typeHandle.jdbcTypeName().map(name -> name.equalsIgnoreCase("varchar")).orElse(false)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| Optional<ParameterizedExpression> value = context.rewriteExpression(valueExpression); | ||
| if (value.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| String expression; | ||
| List<QueryParameter> parameters; | ||
| if (call.getArguments().size() == 2) { | ||
| // Two argument SUBSTRING(value, start) | ||
| Optional<ParameterizedExpression> start = context.rewriteExpression(call.getArguments().get(1)); | ||
| if (start.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| expression = format("SUBSTRING(%s FROM %s)", value.get().expression(), start.get().expression()); | ||
| parameters = combineParameters(value.get(), start.get()); | ||
| } | ||
| else if (call.getArguments().size() == 3) { | ||
| // Three argument SUBSTRING(value, start, length) | ||
| Optional<ParameterizedExpression> start = context.rewriteExpression(call.getArguments().get(1)); | ||
| Optional<ParameterizedExpression> length = context.rewriteExpression(call.getArguments().get(2)); | ||
| if (start.isEmpty() || length.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| expression = format("SUBSTRING(%s FROM %s FOR %s)", | ||
| value.get().expression(), | ||
| start.get().expression(), | ||
| length.get().expression()); | ||
| parameters = combineParameters(value.get(), start.get(), length.get()); | ||
| } | ||
| else { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(new JdbcExpression(expression, ImmutableList.copyOf(parameters), typeHandle)); | ||
| } | ||
|
|
||
| private JdbcTypeHandle getTypeHandle(ConnectorExpression expression, RewriteContext<ParameterizedExpression> context) | ||
| { | ||
| if (expression instanceof Variable variable) { | ||
| return ((JdbcColumnHandle) context.getAssignment(variable.getName())).getJdbcTypeHandle(); | ||
| } | ||
| // For non-variable expressions, we might need to derive the type handle differently | ||
| // This is a simplified approach - you might need more sophisticated type handling | ||
| return new JdbcTypeHandle(JDBCType.VARCHAR.getVendorTypeNumber(), Optional.of("varchar"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()); | ||
| } | ||
|
|
||
| private List<QueryParameter> combineParameters(ParameterizedExpression... expressions) | ||
| { | ||
| return Stream.of(expressions) | ||
| .flatMap(expr -> expr.parameters().stream()) | ||
| .collect(toList()); | ||
| } | ||
| } | ||
This file contains hidden or 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
115 changes: 115 additions & 0 deletions
115
plugin/trino-teradata/src/test/java/io/trino/plugin/unit/TestRewriteSubstring.java
This file contains hidden or 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,115 @@ | ||
| /* | ||
|
sc250072 marked this conversation as resolved.
|
||
| * 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.unit; | ||
|
|
||
| import io.trino.plugin.teradata.RewriteSubstring; | ||
| import io.trino.spi.expression.Call; | ||
| import io.trino.spi.expression.Constant; | ||
| import io.trino.spi.expression.FunctionName; | ||
| import io.trino.spi.expression.Variable; | ||
| import io.trino.spi.type.IntegerType; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static io.trino.spi.type.VarcharType.VARCHAR; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class TestRewriteSubstring | ||
| { | ||
| private final RewriteSubstring rewriteSubstring = new RewriteSubstring(); | ||
|
|
||
| @Test | ||
| public void testPatternMatchesSubstringWithTwoArguments() | ||
| { | ||
| Variable value = new Variable("test_column", VARCHAR); | ||
| Constant start = new Constant(1L, IntegerType.INTEGER); | ||
|
|
||
| Call substringCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("substring"), | ||
| List.of(value, start)); | ||
| boolean matches = rewriteSubstring.getPattern().match(substringCall).findFirst().isPresent(); | ||
| assertThat(matches).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPatternMatchesSubstringWithThreeArguments() | ||
| { | ||
| Variable value = new Variable("test_column", VARCHAR); | ||
| Constant start = new Constant(1L, IntegerType.INTEGER); | ||
| Constant length = new Constant(5L, IntegerType.INTEGER); | ||
|
|
||
| Call substringCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("substring"), | ||
| List.of(value, start, length)); | ||
| boolean matches = rewriteSubstring.getPattern().match(substringCall).findFirst().isPresent(); | ||
| assertThat(matches).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPatternDoesNotMatchOtherFunctions() | ||
| { | ||
| Variable value = new Variable("test_column", VARCHAR); | ||
| Constant start = new Constant(1L, IntegerType.INTEGER); | ||
|
|
||
| Call upperCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("upper"), | ||
| List.of(value, start)); | ||
| boolean matches = rewriteSubstring.getPattern().match(upperCall).findFirst().isPresent(); | ||
| assertThat(matches).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPatternDoesNotMatchWithOneArgument() | ||
| { | ||
| Variable value = new Variable("test_column", VARCHAR); | ||
|
|
||
| Call substringCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("substring"), | ||
| List.of(value)); | ||
| boolean matches = rewriteSubstring.getPattern().match(substringCall).findFirst().isPresent(); | ||
| assertThat(matches).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPatternDoesNotMatchWithFourArguments() | ||
| { | ||
| Variable value = new Variable("test_column", VARCHAR); | ||
| Constant start = new Constant(1L, IntegerType.INTEGER); | ||
| Constant length = new Constant(5L, IntegerType.INTEGER); | ||
| Constant extra = new Constant(10L, IntegerType.INTEGER); | ||
|
|
||
| Call substringCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("substring"), | ||
| List.of(value, start, length, extra)); | ||
| boolean matches = rewriteSubstring.getPattern().match(substringCall).findFirst().isPresent(); | ||
| assertThat(matches).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPatternDoesNotMatchEmptyArguments() | ||
| { | ||
| Call substringCall = new Call( | ||
| VARCHAR, | ||
| new FunctionName("substring"), | ||
| List.of()); | ||
| boolean matches = rewriteSubstring.getPattern().match(substringCall).findFirst().isPresent(); | ||
| assertThat(matches).isFalse(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.