-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Support pushdown of count aggregation with distinct
#8562
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
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
|
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.
i may have asked about that but don't remember answer -- why "only one non-count" ?
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. I am not sure what exactly @alexjo2144 had in mind here. E.g. this one will not: I suggest to just leave title line in commit message and drop the rest. |
||
| * 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.AggregateFunctionRule; | ||
| import io.trino.plugin.jdbc.JdbcClient; | ||
| import io.trino.plugin.jdbc.JdbcColumnHandle; | ||
| import io.trino.plugin.jdbc.JdbcExpression; | ||
| import io.trino.plugin.jdbc.JdbcTypeHandle; | ||
| import io.trino.spi.connector.AggregateFunction; | ||
| import io.trino.spi.expression.Variable; | ||
| import io.trino.spi.type.BigintType; | ||
| import io.trino.spi.type.CharType; | ||
| import io.trino.spi.type.VarcharType; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.google.common.base.Verify.verify; | ||
| import static io.trino.matching.Capture.newCapture; | ||
| import static io.trino.plugin.base.expression.AggregateFunctionPatterns.distinct; | ||
| import static io.trino.plugin.base.expression.AggregateFunctionPatterns.functionName; | ||
| import static io.trino.plugin.base.expression.AggregateFunctionPatterns.hasFilter; | ||
| import static io.trino.plugin.base.expression.AggregateFunctionPatterns.singleInput; | ||
| import static io.trino.plugin.base.expression.AggregateFunctionPatterns.variable; | ||
| import static io.trino.spi.type.BigintType.BIGINT; | ||
| import static java.lang.String.format; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * Implements {@code count(DISTINCT x)}. | ||
| */ | ||
| public class ImplementCountDistinct | ||
| implements AggregateFunctionRule | ||
| { | ||
| private static final Capture<Variable> INPUT = newCapture(); | ||
|
|
||
| private final JdbcTypeHandle bigintTypeHandle; | ||
| private final boolean isRemoteCollationSensitive; | ||
|
|
||
| /** | ||
| * @param bigintTypeHandle A {@link JdbcTypeHandle} that will be mapped to {@link BigintType} by {@link JdbcClient#toColumnMapping}. | ||
| */ | ||
| public ImplementCountDistinct(JdbcTypeHandle bigintTypeHandle, boolean isRemoteCollationSensitive) | ||
| { | ||
| this.bigintTypeHandle = requireNonNull(bigintTypeHandle, "bigintTypeHandle is null"); | ||
| this.isRemoteCollationSensitive = isRemoteCollationSensitive; | ||
| } | ||
|
|
||
| @Override | ||
| public Pattern<AggregateFunction> getPattern() | ||
| { | ||
| return Pattern.typeOf(AggregateFunction.class) | ||
| .with(distinct().equalTo(true)) | ||
| .with(hasFilter().equalTo(false)) | ||
| .with(functionName().equalTo("count")) | ||
| .with(singleInput().matching(variable().capturedAs(INPUT))); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext context) | ||
| { | ||
| Variable input = captures.get(INPUT); | ||
| JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(input.getName()); | ||
| verify(aggregateFunction.getOutputType() == BIGINT); | ||
|
|
||
| boolean isCaseSensitiveType = columnHandle.getColumnType() instanceof CharType || columnHandle.getColumnType() instanceof VarcharType; | ||
| if (aggregateFunction.isDistinct() && !isRemoteCollationSensitive && isCaseSensitiveType) { | ||
| // Remote database is case insensitive or compares values differently from Trino | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(new JdbcExpression( | ||
| format("count(DISTINCT %s)", context.getIdentifierQuote().apply(columnHandle.getColumnName())), | ||
| bigintTypeHandle)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.