Search in sources :

Example 1 with Call

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

the class TestPushProjectionIntoTableScan method mockApplyProjection.

private Optional<ProjectionApplicationResult<ConnectorTableHandle>> mockApplyProjection(ConnectorSession session, ConnectorTableHandle tableHandle, List<ConnectorExpression> projections, Map<String, ColumnHandle> assignments) {
    // Prepare new table handle
    SchemaTableName inputSchemaTableName = ((MockConnectorTableHandle) tableHandle).getTableName();
    SchemaTableName projectedTableName = new SchemaTableName(inputSchemaTableName.getSchemaName(), "projected_" + inputSchemaTableName.getTableName());
    // Prepare new column handles
    ImmutableList.Builder<ConnectorExpression> outputExpressions = ImmutableList.builder();
    ImmutableList.Builder<Assignment> outputAssignments = ImmutableList.builder();
    ImmutableList.Builder<ColumnHandle> projectedColumnsBuilder = ImmutableList.builder();
    for (ConnectorExpression projection : projections) {
        String variablePrefix;
        if (projection instanceof Variable) {
            variablePrefix = "projected_variable_";
        } else if (projection instanceof FieldDereference) {
            variablePrefix = "projected_dereference_";
        } else if (projection instanceof Constant) {
            variablePrefix = "projected_constant_";
        } else if (projection instanceof Call) {
            variablePrefix = "projected_call_";
        } else {
            throw new UnsupportedOperationException();
        }
        String newVariableName = variablePrefix + projection.toString();
        Variable newVariable = new Variable(newVariableName, projection.getType());
        ColumnHandle newColumnHandle = new TpchColumnHandle(newVariableName, projection.getType());
        outputExpressions.add(newVariable);
        outputAssignments.add(new Assignment(newVariableName, newColumnHandle, projection.getType()));
        projectedColumnsBuilder.add(newColumnHandle);
    }
    return Optional.of(new ProjectionApplicationResult<>(new MockConnectorTableHandle(projectedTableName, TupleDomain.all(), Optional.of(projectedColumnsBuilder.build())), outputExpressions.build(), outputAssignments.build(), false));
}
Also used : TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) FunctionCall(io.trino.sql.tree.FunctionCall) Call(io.trino.spi.expression.Call) Variable(io.trino.spi.expression.Variable) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Constant(io.trino.spi.expression.Constant) SchemaTableName(io.trino.spi.connector.SchemaTableName) Assignment(io.trino.spi.connector.Assignment) FieldDereference(io.trino.spi.expression.FieldDereference) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle)

Example 2 with Call

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

the class TestExpressionMatching method testMatchCall.

@Test
public void testMatchCall() {
    ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
    ExpressionPattern pattern = expressionPattern("add(foo: decimal(p, s), bar: bigint)");
    Match match = pattern.getPattern().match(expression).collect(onlyElement());
    MatchContext matchContext = new MatchContext();
    pattern.resolve(match.captures(), matchContext);
    assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo", "bar");
    assertThat(matchContext.get("p")).isEqualTo(10L);
    assertThat(matchContext.get("s")).isEqualTo(2L);
    assertThat(matchContext.get("foo")).isEqualTo(new Variable("first", createDecimalType(10, 2)));
    assertThat(matchContext.get("bar")).isEqualTo(new Variable("second", BIGINT));
}
Also used : Call(io.trino.spi.expression.Call) FunctionName(io.trino.spi.expression.FunctionName) Variable(io.trino.spi.expression.Variable) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) Test(org.testng.annotations.Test)

Example 3 with Call

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

the class TestExpressionMatching method testExpressionCapture.

@Test
public void testExpressionCapture() {
    ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
    ExpressionPattern pattern = expressionPattern("foo: decimal(p, s)");
    Match match = pattern.getPattern().match(expression).collect(onlyElement());
    MatchContext matchContext = new MatchContext();
    pattern.resolve(match.captures(), matchContext);
    assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo");
    assertThat(matchContext.get("p")).isEqualTo(21L);
    assertThat(matchContext.get("s")).isEqualTo(2L);
    assertThat(matchContext.get("foo")).isSameAs(expression);
}
Also used : Call(io.trino.spi.expression.Call) FunctionName(io.trino.spi.expression.FunctionName) Variable(io.trino.spi.expression.Variable) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) Test(org.testng.annotations.Test)

Example 4 with Call

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

the class TestGenericRewrite method testRewriteCall.

@Test
public void testRewriteCall() {
    GenericRewrite rewrite = new GenericRewrite("add(foo: decimal(p, s), bar: bigint): decimal(rp, rs)", "foo + bar::decimal(rp,rs)");
    ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
    Match match = rewrite.getPattern().match(expression).collect(onlyElement());
    Optional<String> rewritten = rewrite.rewrite(expression, match.captures(), new RewriteContext<>() {

        @Override
        public Map<String, ColumnHandle> getAssignments() {
            throw new UnsupportedOperationException();
        }

        @Override
        public ConnectorSession getSession() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Optional<String> defaultRewrite(ConnectorExpression expression) {
            if (expression instanceof Variable) {
                return Optional.of("\"" + ((Variable) expression).getName().replace("\"", "\"\"") + "\"");
            }
            return Optional.empty();
        }
    });
    assertThat(rewritten).hasValue("(\"first\") + (\"second\")::decimal(21,2)");
}
Also used : Call(io.trino.spi.expression.Call) Variable(io.trino.spi.expression.Variable) Optional(java.util.Optional) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) Match(io.trino.matching.Match) FunctionName(io.trino.spi.expression.FunctionName) ConnectorSession(io.trino.spi.connector.ConnectorSession) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with Call

use of io.trino.spi.expression.Call 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

Call (io.trino.spi.expression.Call)7 Variable (io.trino.spi.expression.Variable)7 ConnectorExpression (io.trino.spi.expression.ConnectorExpression)6 Test (org.testng.annotations.Test)5 Constant (io.trino.spi.expression.Constant)4 Match (io.trino.matching.Match)3 ColumnHandle (io.trino.spi.connector.ColumnHandle)3 FunctionName (io.trino.spi.expression.FunctionName)3 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 MockConnectorTableHandle (io.trino.connector.MockConnectorTableHandle)2 TpchColumnHandle (io.trino.plugin.tpch.TpchColumnHandle)2 Assignment (io.trino.spi.connector.Assignment)2 ConnectorSession (io.trino.spi.connector.ConnectorSession)2 SchemaTableName (io.trino.spi.connector.SchemaTableName)2 FieldDereference (io.trino.spi.expression.FieldDereference)2 TupleDomain (io.trino.spi.predicate.TupleDomain)2