-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Translate IN predicate to connector expression #11396
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
Changes from all commits
6c89d7c
c568122
4ad0dfd
94432ee
7390a25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,9 +16,11 @@ | |
| import com.google.common.collect.ImmutableSet; | ||
| import io.trino.plugin.base.expression.ConnectorExpressionRewriter; | ||
| import io.trino.plugin.base.expression.ConnectorExpressionRule; | ||
| import io.trino.spi.type.Type; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
||
|
|
@@ -39,9 +41,14 @@ public static JdbcConnectorExpressionRewriterBuilder newBuilder() | |
| private JdbcConnectorExpressionRewriterBuilder() {} | ||
|
|
||
| public JdbcConnectorExpressionRewriterBuilder addStandardRules(Function<String, String> identifierQuote) | ||
| { | ||
wendigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return addStandardRules(identifierQuote, type -> Optional.empty()); | ||
| } | ||
|
|
||
| public JdbcConnectorExpressionRewriterBuilder addStandardRules(Function<String, String> identifierQuote, Function<Type, Optional<String>> typeMapping) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is simple for PostgreSQL and varchar case, but generally this is tricky, since remote database and Trino have different type systems. In case of the NULL should be eliminated on the engine side (generic engine rule). Do we have other cases where this is actually useful?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
also
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
result is
This is not allowed in SQL, but it it was allowed, it would be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
BTW such rule, if written, should go in separate PR. in this PR please keep the tests exercising |
||
| { | ||
| add(new RewriteVariable(identifierQuote)); | ||
| add(new RewriteVarcharConstant()); | ||
| add(new RewriteVarcharConstant(typeMapping)); | ||
| add(new RewriteExactNumericConstant()); | ||
| add(new RewriteAnd()); | ||
| add(new RewriteOr()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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.spi.expression.Call; | ||
| import io.trino.spi.expression.ConnectorExpression; | ||
| import io.trino.spi.type.Type; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import static io.trino.matching.Capture.newCapture; | ||
| import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.argument; | ||
| import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.call; | ||
| 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, String> | ||
| { | ||
| private static final Capture<ConnectorExpression> ARGUMENT = newCapture(); | ||
|
|
||
| private static final Pattern<Call> PATTERN = call() | ||
| .with(functionName().equalTo(CAST_FUNCTION_NAME)) | ||
| .with(argument(0).capturedAs(ARGUMENT)); | ||
|
Comment on lines
+41
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fmt |
||
|
|
||
| private final Function<Type, Optional<String>> typeMapping; | ||
|
|
||
| public RewriteCast(Function<Type, Optional<String>> typeMapping) | ||
| { | ||
| this.typeMapping = requireNonNull(typeMapping, "typeMapping is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Pattern<Call> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> rewrite(Call expression, Captures captures, RewriteContext<String> context) | ||
| { | ||
| ConnectorExpression argument = captures.get(ARGUMENT); | ||
| Optional<String> typeCast = typeMapping.apply(expression.getType()); | ||
|
|
||
| if (typeCast.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| Optional<String> translatedArgument = context.defaultRewrite(argument); | ||
| if (translatedArgument.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(format("CAST(%s AS %s)", translatedArgument.get(), typeCast.get())); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.