-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Improve ClickHouse LIKE pushdown for complex expression #23591
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
Praveen2112
merged 1 commit into
trinodb:master
from
ssheikin:ssheikin/49/trino/clickhouse-varchar-LIKE-pushdown
Oct 1, 2024
Merged
Changes from all commits
Commits
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
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
99 changes: 99 additions & 0 deletions
99
plugin/trino-clickhouse/src/main/java/io/trino/plugin/clickhouse/expression/RewriteLike.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,99 @@ | ||
| /* | ||
| * 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.clickhouse.expression; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.airlift.slice.Slice; | ||
| 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.Constant; | ||
| import io.trino.spi.expression.Variable; | ||
| import io.trino.spi.type.CharType; | ||
| import io.trino.spi.type.VarcharType; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| 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.constant; | ||
| 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.clickhouse.ClickHouseClient.supportsPushdown; | ||
| import static io.trino.spi.expression.StandardFunctions.LIKE_FUNCTION_NAME; | ||
| import static io.trino.spi.type.BooleanType.BOOLEAN; | ||
| import static java.lang.String.format; | ||
|
|
||
| public class RewriteLike | ||
| implements ConnectorExpressionRule<Call, ParameterizedExpression> | ||
| { | ||
| private static final Capture<Variable> LIKE_VALUE = newCapture(); | ||
| // TODO allow Variable as a LIKE_PATTERN: "SELECT * FROM t WHERE column_a LIKE column_b" is a valid query in ClickHouse | ||
| // only Constant is allowed as LIKE_PATTERN, because according to | ||
| // https://clickhouse.com/docs/en/sql-reference/functions/string-search-functions#like | ||
| // ClickHouse requires backslashes in strings to be quoted as well, so you would actually need to write \\%, \\_ and \\\\ to match against literal %, _ and \ | ||
| // if "column_a LIKE column_b" is pushed down, it requires more thorough consideration how to process escaping. | ||
| private static final Capture<Constant> LIKE_PATTERN = newCapture(); | ||
| private static final Pattern<Call> PATTERN = call() | ||
| .with(functionName().equalTo(LIKE_FUNCTION_NAME)) | ||
| .with(type().equalTo(BOOLEAN)) | ||
| .with(argumentCount().equalTo(2)) | ||
| .with(argument(0).matching(variable() | ||
| .with(type().matching(type -> type instanceof CharType || type instanceof VarcharType)) | ||
| .matching((Variable variable, RewriteContext<ParameterizedExpression> context) -> supportsPushdown(variable, context)) | ||
| .capturedAs(LIKE_VALUE))) | ||
| .with(argument(1).matching(constant() | ||
| .with(type().matching(type -> type instanceof CharType || type instanceof VarcharType)) | ||
| .capturedAs(LIKE_PATTERN))); | ||
|
|
||
| @Override | ||
| public Pattern<Call> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<ParameterizedExpression> rewrite(Call expression, Captures captures, RewriteContext<ParameterizedExpression> context) | ||
| { | ||
| Variable likeValue = captures.get(LIKE_VALUE); | ||
| Constant likePattern = captures.get(LIKE_PATTERN); | ||
|
|
||
| checkArgument(likePattern.getValue() != null, "When pattern is NULL, optimizer shortcuts to return fast, not reaching connector expression"); | ||
| if (((Slice) likePattern.getValue()).toStringUtf8().contains("\\")) { | ||
| // LIKE in trino does not support escape characters. | ||
| // However Clickhouse supports them with \ is for escaping literals %, _ and \. | ||
| // ClickHouse requires backslashes in strings to be quoted as well, so you would actually need to write \\%, \\_ and \\\\ to match against literal %, _ and \ | ||
| // So just to translate Trino LIKE '\' , all \ have to be escaped, and resulting clickhouse should be LIKE '\\\\' | ||
| // TODO escape `\` appropriately and pushdown: .replace("\\", "\\\\\\\\") | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return context.defaultRewrite(likeValue).flatMap(value -> | ||
| context.defaultRewrite(likePattern).map(pattern -> | ||
| new ParameterizedExpression( | ||
| format("%s LIKE %s", value.expression(), pattern.expression()), | ||
| ImmutableList.<QueryParameter>builder() | ||
| .addAll(value.parameters()) | ||
| .addAll(pattern.parameters()) | ||
| .build()))); | ||
| } | ||
| } | ||
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
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.