-
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.
Implement count(distinct) pushdown for PostgreSQL
- Loading branch information
1 parent
25965e9
commit d55bb1c
Showing
9 changed files
with
233 additions
and
17 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
90 changes: 90 additions & 0 deletions
90
...trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/expression/ImplementCountDistinct.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,90 @@ | ||
/* | ||
* 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)); | ||
} | ||
} |
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
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
Oops, something went wrong.