Search in sources :

Example 1 with PartitionExpression

use of io.crate.expression.reference.partitioned.PartitionExpression in project crate by crate.

the class WhereClauseAnalyzer method resolvePartitions.

public static PartitionResult resolvePartitions(Symbol query, DocTableInfo tableInfo, CoordinatorTxnCtx coordinatorTxnCtx, NodeContext nodeCtx) {
    assert tableInfo.isPartitioned() : "table must be partitioned in order to resolve partitions";
    assert !tableInfo.partitions().isEmpty() : "table must have at least one partition";
    PartitionReferenceResolver partitionReferenceResolver = preparePartitionResolver(tableInfo.partitionedByColumns());
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(nodeCtx, RowGranularity.PARTITION, partitionReferenceResolver, null);
    Symbol normalized;
    Map<Symbol, List<Literal>> queryPartitionMap = new HashMap<>();
    for (PartitionName partitionName : tableInfo.partitions()) {
        for (PartitionExpression partitionExpression : partitionReferenceResolver.expressions()) {
            partitionExpression.setNextRow(partitionName);
        }
        normalized = normalizer.normalize(query, coordinatorTxnCtx);
        assert normalized != null : "normalizing a query must not return null";
        if (normalized.equals(query)) {
            // no partition columns inside the where clause
            return new PartitionResult(query, Collections.emptyList());
        }
        boolean canMatch = WhereClause.canMatch(normalized);
        if (canMatch) {
            List<Literal> partitions = queryPartitionMap.get(normalized);
            if (partitions == null) {
                partitions = new ArrayList<>();
                queryPartitionMap.put(normalized, partitions);
            }
            partitions.add(Literal.of(partitionName.asIndexName()));
        }
    }
    if (queryPartitionMap.size() == 1) {
        Map.Entry<Symbol, List<Literal>> entry = Iterables.getOnlyElement(queryPartitionMap.entrySet());
        return new PartitionResult(entry.getKey(), Lists2.map(entry.getValue(), literal -> nullOrString(literal.value())));
    } else if (queryPartitionMap.size() > 0) {
        PartitionResult partitionResult = tieBreakPartitionQueries(normalizer, queryPartitionMap, coordinatorTxnCtx);
        return partitionResult == null ? // the query will then be evaluated correctly within each partition to see whether it matches or not
        new PartitionResult(query, Lists2.map(tableInfo.partitions(), PartitionName::asIndexName)) : partitionResult;
    } else {
        return new PartitionResult(Literal.BOOLEAN_FALSE, Collections.emptyList());
    }
}
Also used : Tuple(io.crate.common.collections.Tuple) ScalarsAndRefsToTrue(io.crate.analyze.ScalarsAndRefsToTrue) HashMap(java.util.HashMap) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) PartitionName(io.crate.metadata.PartitionName) ArrayList(java.util.ArrayList) Map(java.util.Map) StringUtils.nullOrString(io.crate.common.StringUtils.nullOrString) Nullable(javax.annotation.Nullable) DocTableInfo(io.crate.metadata.doc.DocTableInfo) NodeContext(io.crate.metadata.NodeContext) WhereClause(io.crate.analyze.WhereClause) PartitionReferenceResolver(io.crate.metadata.PartitionReferenceResolver) Reference(io.crate.metadata.Reference) Iterables(io.crate.common.collections.Iterables) Lists2(io.crate.common.collections.Lists2) List(java.util.List) RowGranularity(io.crate.metadata.RowGranularity) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PartitionExpression(io.crate.expression.reference.partitioned.PartitionExpression) Collections(java.util.Collections) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) PartitionName(io.crate.metadata.PartitionName) PartitionExpression(io.crate.expression.reference.partitioned.PartitionExpression) Literal(io.crate.expression.symbol.Literal) ArrayList(java.util.ArrayList) List(java.util.List) PartitionReferenceResolver(io.crate.metadata.PartitionReferenceResolver) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PartitionExpression

use of io.crate.expression.reference.partitioned.PartitionExpression in project crate by crate.

the class WhereClauseAnalyzer method preparePartitionResolver.

private static PartitionReferenceResolver preparePartitionResolver(List<Reference> partitionColumns) {
    List<PartitionExpression> partitionExpressions = new ArrayList<>(partitionColumns.size());
    int idx = 0;
    for (Reference partitionedByColumn : partitionColumns) {
        partitionExpressions.add(new PartitionExpression(partitionedByColumn, idx));
        idx++;
    }
    return new PartitionReferenceResolver(partitionExpressions);
}
Also used : PartitionExpression(io.crate.expression.reference.partitioned.PartitionExpression) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) PartitionReferenceResolver(io.crate.metadata.PartitionReferenceResolver)

Aggregations

PartitionExpression (io.crate.expression.reference.partitioned.PartitionExpression)2 PartitionReferenceResolver (io.crate.metadata.PartitionReferenceResolver)2 Reference (io.crate.metadata.Reference)2 ArrayList (java.util.ArrayList)2 ScalarsAndRefsToTrue (io.crate.analyze.ScalarsAndRefsToTrue)1 WhereClause (io.crate.analyze.WhereClause)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 StringUtils.nullOrString (io.crate.common.StringUtils.nullOrString)1 Iterables (io.crate.common.collections.Iterables)1 Lists2 (io.crate.common.collections.Lists2)1 Tuple (io.crate.common.collections.Tuple)1 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)1 Literal (io.crate.expression.symbol.Literal)1 Symbol (io.crate.expression.symbol.Symbol)1 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 NodeContext (io.crate.metadata.NodeContext)1 PartitionName (io.crate.metadata.PartitionName)1 RowGranularity (io.crate.metadata.RowGranularity)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1