Search in sources :

Example 26 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class CountPhase method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    executionPhaseId = in.readVInt();
    routing = Routing.fromStream(in);
    whereClause = new WhereClause(in);
    distributionInfo = DistributionInfo.fromStream(in);
}
Also used : WhereClause(io.crate.analyze.WhereClause)

Example 27 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class RoutedCollectPhase method normalize.

/**
     * normalizes the symbols of this node with the given normalizer
     *
     * @return a normalized node, if no changes occurred returns this
     */
public RoutedCollectPhase normalize(EvaluatingNormalizer normalizer, TransactionContext transactionContext) {
    assert whereClause() != null : "whereClause must not be null";
    RoutedCollectPhase result = this;
    List<Symbol> newToCollect = normalizer.normalize(toCollect(), transactionContext);
    boolean changed = newToCollect != toCollect();
    WhereClause newWhereClause = whereClause().normalize(normalizer, transactionContext);
    OrderBy orderBy = this.orderBy;
    if (orderBy != null) {
        List<Symbol> orderBySymbols = orderBy.orderBySymbols();
        List<Symbol> normalizedOrderBy = normalizer.normalize(orderBySymbols, transactionContext);
        changed = changed || orderBySymbols != normalizedOrderBy;
        orderBy = new OrderBy(normalizedOrderBy, this.orderBy.reverseFlags(), this.orderBy.nullsFirst());
    }
    changed = changed || newWhereClause != whereClause();
    if (changed) {
        result = new RoutedCollectPhase(jobId(), phaseId(), name(), routing, maxRowGranularity, newToCollect, projections, newWhereClause, distributionInfo);
        result.orderBy(orderBy);
    }
    return result;
}
Also used : OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause)

Example 28 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class RoutedCollectPhase method forQueriedTable.

public static RoutedCollectPhase forQueriedTable(Planner.Context plannerContext, QueriedTableRelation table, List<Symbol> toCollect, List<Projection> projections) {
    TableInfo tableInfo = table.tableRelation().tableInfo();
    WhereClause where = table.querySpec().where();
    if (table.tableRelation() instanceof TableFunctionRelation) {
        TableFunctionRelation tableFunctionRelation = (TableFunctionRelation) table.tableRelation();
        plannerContext.normalizer().normalizeInplace(tableFunctionRelation.function().arguments(), plannerContext.transactionContext());
        return new TableFunctionCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), plannerContext.allocateRouting(tableInfo, where, null), tableFunctionRelation, projections, toCollect, where);
    }
    return new RoutedCollectPhase(plannerContext.jobId(), plannerContext.nextExecutionPhaseId(), "collect", plannerContext.allocateRouting(tableInfo, where, null), tableInfo.rowGranularity(), toCollect, projections, where, DistributionInfo.DEFAULT_BROADCAST);
}
Also used : WhereClause(io.crate.analyze.WhereClause) TableInfo(io.crate.metadata.table.TableInfo) TableFunctionRelation(io.crate.analyze.relations.TableFunctionRelation)

Example 29 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class RoutedCollectPhase method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    distributionInfo = DistributionInfo.fromStream(in);
    toCollect = Symbols.listFromStream(in);
    maxRowGranularity = RowGranularity.fromStream(in);
    if (in.readBoolean()) {
        routing = Routing.fromStream(in);
    }
    whereClause = new WhereClause(in);
    if (in.readBoolean()) {
        nodePageSizeHint = in.readVInt();
    }
    if (in.readBoolean()) {
        orderBy = OrderBy.fromStream(in);
    }
    isPartitioned = in.readBoolean();
}
Also used : WhereClause(io.crate.analyze.WhereClause)

Example 30 with WhereClause

use of io.crate.analyze.WhereClause in project crate by crate.

the class WhereClauseAnalyzer method tieBreakPartitionQueries.

private static WhereClause tieBreakPartitionQueries(EvaluatingNormalizer normalizer, Map<Symbol, List<Literal>> queryPartitionMap, WhereClause whereClause, TransactionContext transactionContext) throws UnsupportedOperationException {
    /*
         * Got multiple normalized queries which all could match.
         * This might be the case if one partition resolved to null
         *
         * e.g.
         *
         *  p = 1 and x = 2
         *
         * might lead to
         *
         *  null and x = 2
         *  true and x = 2
         *
         * At this point it is unknown if they really match.
         * In order to figure out if they could potentially match all conditions involving references are now set to true
         *
         *  null and true   -> can't match
         *  true and true   -> can match, can use this query + partition
         *
         * If there is still more than 1 query that can match it's not possible to execute the query :(
         */
    List<Tuple<Symbol, List<Literal>>> canMatch = new ArrayList<>();
    SymbolToTrueVisitor symbolToTrueVisitor = new SymbolToTrueVisitor();
    for (Map.Entry<Symbol, List<Literal>> entry : queryPartitionMap.entrySet()) {
        Symbol query = entry.getKey();
        List<Literal> partitions = entry.getValue();
        Symbol symbol = symbolToTrueVisitor.process(query, null);
        Symbol normalized = normalizer.normalize(symbol, transactionContext);
        assert normalized instanceof Literal : "after normalization and replacing all reference occurrences with true there must only be a literal left";
        Object value = ((Literal) normalized).value();
        if (value != null && (Boolean) value) {
            canMatch.add(new Tuple<>(query, partitions));
        }
    }
    if (canMatch.size() == 1) {
        Tuple<Symbol, List<Literal>> symbolListTuple = canMatch.get(0);
        WhereClause where = new WhereClause(symbolListTuple.v1(), whereClause.docKeys().orElse(null), new ArrayList<String>(symbolListTuple.v2().size()));
        where.partitions(symbolListTuple.v2());
        return where;
    }
    throw new UnsupportedOperationException("logical conjunction of the conditions in the WHERE clause which " + "involve partitioned columns led to a query that can't be executed.");
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) SymbolToTrueVisitor(io.crate.analyze.SymbolToTrueVisitor) Literal(io.crate.analyze.symbol.Literal) ImmutableList(com.google.common.collect.ImmutableList) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

WhereClause (io.crate.analyze.WhereClause)61 Test (org.junit.Test)46 CrateUnitTest (io.crate.test.integration.CrateUnitTest)43 Symbol (io.crate.analyze.symbol.Symbol)10 PartitionName (io.crate.metadata.PartitionName)8 BytesRef (org.apache.lucene.util.BytesRef)8 DataType (io.crate.types.DataType)6 TableInfo (io.crate.metadata.table.TableInfo)5 SqlExpressions (io.crate.testing.SqlExpressions)5 QualifiedName (io.crate.sql.tree.QualifiedName)4 ArrayType (io.crate.types.ArrayType)4 ImmutableList (com.google.common.collect.ImmutableList)3 OrderBy (io.crate.analyze.OrderBy)3 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TableRelation (io.crate.analyze.relations.TableRelation)3 Function (io.crate.analyze.symbol.Function)3 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)3 CrateRegexQuery (io.crate.lucene.match.CrateRegexQuery)3 Literal (io.crate.analyze.symbol.Literal)2 Bucket (io.crate.data.Bucket)2