io.trino.plugin.sqlserver.ImplementSqlServerStdev Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trino-sqlserver Show documentation
Show all versions of trino-sqlserver Show documentation
Trino - SQL Server connector
/*
* 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.sqlserver;
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.jdbc.JdbcColumnHandle;
import io.trino.plugin.jdbc.JdbcExpression;
import io.trino.plugin.jdbc.expression.ParameterizedExpression;
import io.trino.spi.connector.AggregateFunction;
import io.trino.spi.expression.Variable;
import io.trino.spi.type.DoubleType;
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.aggregation.AggregateFunctionPatterns.basicAggregation;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.functionName;
import static io.trino.plugin.base.aggregation.AggregateFunctionPatterns.singleArgument;
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.type;
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.variable;
import static io.trino.spi.type.DoubleType.DOUBLE;
import static java.lang.String.format;
public class ImplementSqlServerStdev
implements AggregateFunctionRule
{
private static final Capture ARGUMENT = newCapture();
@Override
public Pattern getPattern()
{
return basicAggregation()
.with(functionName().equalTo("stddev"))
.with(singleArgument().matching(
variable()
.with(type().matching(DoubleType.class::isInstance))
.capturedAs(ARGUMENT)));
}
@Override
public Optional rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext context)
{
Variable argument = captures.get(ARGUMENT);
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
verify(columnHandle.getColumnType().equals(DOUBLE));
verify(aggregateFunction.getOutputType().equals(DOUBLE));
ParameterizedExpression rewrittenArgument = context.rewriteExpression(argument).orElseThrow();
return Optional.of(new JdbcExpression(
format("STDEV(%s)", rewrittenArgument.expression()),
rewrittenArgument.parameters(),
columnHandle.getJdbcTypeHandle()));
}
}