Search in sources :

Example 36 with Variable

use of io.trino.spi.expression.Variable in project trino by trinodb.

the class ImplementStddevPop method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(aggregateFunction.getOutputType() == columnHandle.getColumnType());
    return Optional.of(new JdbcExpression(format("stddev_pop(%s)", context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 37 with Variable

use of io.trino.spi.expression.Variable in project trino by trinodb.

the class ImplementSum method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    JdbcTypeHandle resultTypeHandle;
    if (columnHandle.getColumnType().equals(aggregateFunction.getOutputType())) {
        resultTypeHandle = columnHandle.getJdbcTypeHandle();
    } else if (aggregateFunction.getOutputType() instanceof DecimalType) {
        Optional<JdbcTypeHandle> decimalTypeHandle = this.decimalTypeHandle.apply(((DecimalType) aggregateFunction.getOutputType()));
        if (decimalTypeHandle.isEmpty()) {
            return Optional.empty();
        }
        resultTypeHandle = decimalTypeHandle.get();
    } else {
        return Optional.empty();
    }
    return Optional.of(new JdbcExpression(format("sum(%s)", context.rewriteExpression(argument).orElseThrow()), resultTypeHandle));
}
Also used : JdbcTypeHandle(io.trino.plugin.jdbc.JdbcTypeHandle) Variable(io.trino.spi.expression.Variable) Optional(java.util.Optional) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) DecimalType(io.trino.spi.type.DecimalType) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 38 with Variable

use of io.trino.spi.expression.Variable in project trino by trinodb.

the class ImplementVariancePop method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(aggregateFunction.getOutputType() == columnHandle.getColumnType());
    return Optional.of(new JdbcExpression(format("var_pop(%s)", context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 39 with Variable

use of io.trino.spi.expression.Variable in project trino by trinodb.

the class ImplementVarianceSamp method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(aggregateFunction.getOutputType() == columnHandle.getColumnType());
    return Optional.of(new JdbcExpression(format("var_samp(%s)", context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression)

Example 40 with Variable

use of io.trino.spi.expression.Variable in project trino by trinodb.

the class ElasticsearchMetadata method applyFilter.

@Override
public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(ConnectorSession session, ConnectorTableHandle table, Constraint constraint) {
    ElasticsearchTableHandle handle = (ElasticsearchTableHandle) table;
    if (isPassthroughQuery(handle)) {
        // filter pushdown currently not supported for passthrough query
        return Optional.empty();
    }
    Map<ColumnHandle, Domain> supported = new HashMap<>();
    Map<ColumnHandle, Domain> unsupported = new HashMap<>();
    if (constraint.getSummary().getDomains().isPresent()) {
        for (Map.Entry<ColumnHandle, Domain> entry : constraint.getSummary().getDomains().get().entrySet()) {
            ElasticsearchColumnHandle column = (ElasticsearchColumnHandle) entry.getKey();
            if (column.isSupportsPredicates()) {
                supported.put(column, entry.getValue());
            } else {
                unsupported.put(column, entry.getValue());
            }
        }
    }
    TupleDomain<ColumnHandle> oldDomain = handle.getConstraint();
    TupleDomain<ColumnHandle> newDomain = oldDomain.intersect(TupleDomain.withColumnDomains(supported));
    ConnectorExpression oldExpression = constraint.getExpression();
    Map<String, String> newRegexes = new HashMap<>(handle.getRegexes());
    List<ConnectorExpression> expressions = ConnectorExpressions.extractConjuncts(constraint.getExpression());
    List<ConnectorExpression> notHandledExpressions = new ArrayList<>();
    for (ConnectorExpression expression : expressions) {
        if (expression instanceof Call) {
            Call call = (Call) expression;
            if (isSupportedLikeCall(call)) {
                List<ConnectorExpression> arguments = call.getArguments();
                String variableName = ((Variable) arguments.get(0)).getName();
                ElasticsearchColumnHandle column = (ElasticsearchColumnHandle) constraint.getAssignments().get(variableName);
                verifyNotNull(column, "No assignment for %s", variableName);
                String columnName = column.getName();
                Object pattern = ((Constant) arguments.get(1)).getValue();
                Optional<Slice> escape = Optional.empty();
                if (arguments.size() == 3) {
                    escape = Optional.of((Slice) (((Constant) arguments.get(2)).getValue()));
                }
                if (!newRegexes.containsKey(columnName) && pattern instanceof Slice) {
                    IndexMetadata metadata = client.getIndexMetadata(handle.getIndex());
                    if (metadata.getSchema().getFields().stream().anyMatch(field -> columnName.equals(field.getName()) && field.getType() instanceof PrimitiveType && "keyword".equals(((PrimitiveType) field.getType()).getName()))) {
                        newRegexes.put(columnName, likeToRegexp(((Slice) pattern), escape));
                        continue;
                    }
                }
            }
        }
        notHandledExpressions.add(expression);
    }
    ConnectorExpression newExpression = ConnectorExpressions.and(notHandledExpressions);
    if (oldDomain.equals(newDomain) && oldExpression.equals(newExpression)) {
        return Optional.empty();
    }
    handle = new ElasticsearchTableHandle(handle.getType(), handle.getSchema(), handle.getIndex(), newDomain, newRegexes, handle.getQuery(), handle.getLimit());
    return Optional.of(new ConstraintApplicationResult<>(handle, TupleDomain.withColumnDomains(unsupported), newExpression, false));
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Call(io.trino.spi.expression.Call) Variable(io.trino.spi.expression.Variable) HashMap(java.util.HashMap) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Constant(io.trino.spi.expression.Constant) ArrayList(java.util.ArrayList) Slice(io.airlift.slice.Slice) PrimitiveType(io.trino.plugin.elasticsearch.client.IndexMetadata.PrimitiveType) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) IndexMetadata(io.trino.plugin.elasticsearch.client.IndexMetadata) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) HashMap(java.util.HashMap)

Aggregations

Variable (io.trino.spi.expression.Variable)51 JdbcExpression (io.trino.plugin.jdbc.JdbcExpression)21 ConnectorExpression (io.trino.spi.expression.ConnectorExpression)21 JdbcColumnHandle (io.trino.plugin.jdbc.JdbcColumnHandle)18 Test (org.testng.annotations.Test)12 ImmutableList (com.google.common.collect.ImmutableList)11 ColumnHandle (io.trino.spi.connector.ColumnHandle)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 Assignment (io.trino.spi.connector.Assignment)10 Map (java.util.Map)9 Optional (java.util.Optional)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 TupleDomain (io.trino.spi.predicate.TupleDomain)8 List (java.util.List)8 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)7 AggregateFunction (io.trino.spi.connector.AggregateFunction)7 ProjectionApplicationResult (io.trino.spi.connector.ProjectionApplicationResult)7 AggregateExpression (io.trino.plugin.pinot.query.AggregateExpression)6 Domain (io.trino.spi.predicate.Domain)6 Verify.verify (com.google.common.base.Verify.verify)5