io.trino.plugin.pinot.query.aggregation.ImplementApproxDistinct Maven / Gradle / Ivy
/*
* 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.pinot.query.aggregation;
import io.trino.matching.Capture;
import io.trino.matching.Captures;
import io.trino.matching.Pattern;
import io.trino.plugin.base.aggregation.AggregateFunctionRule;
import io.trino.plugin.pinot.PinotColumnHandle;
import io.trino.plugin.pinot.query.AggregateExpression;
import io.trino.spi.connector.AggregateFunction;
import io.trino.spi.expression.Variable;
import java.util.Optional;
import java.util.function.Function;
import static io.trino.matching.Capture.newCapture;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.basicAggregation;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.functionName;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.outputType;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.singleArgument;
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.variable;
import static io.trino.spi.type.BigintType.BIGINT;
import static java.util.Objects.requireNonNull;
public class ImplementApproxDistinct
implements AggregateFunctionRule
{
// Extracted from io.trino.plugin.jdbc.expression
private static final Capture ARGUMENT = newCapture();
private final Function identifierQuote;
public ImplementApproxDistinct(Function identifierQuote)
{
this.identifierQuote = requireNonNull(identifierQuote, "identifierQuote is null");
}
@Override
public Pattern getPattern()
{
return basicAggregation()
.with(functionName().equalTo("approx_distinct"))
.with(outputType().equalTo(BIGINT))
.with(singleArgument().matching(variable().capturedAs(ARGUMENT)));
}
@Override
public Optional rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext context)
{
Variable argument = captures.get(ARGUMENT);
PinotColumnHandle columnHandle = (PinotColumnHandle) context.getAssignment(argument.getName());
return Optional.of(new AggregateExpression("distinctcounthll", identifierQuote.apply(columnHandle.getColumnName()), false));
}
}