Search in sources :

Example 1 with PartitionExpression

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

the class WhereClauseAnalyzer method resolvePartitions.

private static WhereClause resolvePartitions(WhereClause whereClause, DocTableInfo tableInfo, Functions functions, TransactionContext transactionContext) {
    assert tableInfo.isPartitioned() : "table must be partitioned in order to resolve partitions";
    assert whereClause.partitions().isEmpty() : "partitions must not be analyzed twice";
    if (tableInfo.partitions().isEmpty()) {
        // table is partitioned but has no data / no partitions
        return WhereClause.NO_MATCH;
    }
    PartitionReferenceResolver partitionReferenceResolver = preparePartitionResolver(tableInfo.partitionedByColumns());
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.PARTITION, ReplaceMode.COPY, 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(whereClause.query(), transactionContext);
        assert normalized != null : "normalizing a query must not return null";
        if (normalized.equals(whereClause.query())) {
            // no partition columns inside the where clause
            return whereClause;
        }
        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());
        whereClause = new WhereClause(entry.getKey(), whereClause.docKeys().orElse(null), new ArrayList<String>(entry.getValue().size()));
        whereClause.partitions(entry.getValue());
        return whereClause;
    } else if (queryPartitionMap.size() > 0) {
        return tieBreakPartitionQueries(normalizer, queryPartitionMap, whereClause, transactionContext);
    } else {
        return WhereClause.NO_MATCH;
    }
}
Also used : EvaluatingNormalizer(io.crate.analyze.EvaluatingNormalizer) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) PartitionExpression(io.crate.operation.reference.partitioned.PartitionExpression) Literal(io.crate.analyze.symbol.Literal) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 EvaluatingNormalizer (io.crate.analyze.EvaluatingNormalizer)1 WhereClause (io.crate.analyze.WhereClause)1 Literal (io.crate.analyze.symbol.Literal)1 Symbol (io.crate.analyze.symbol.Symbol)1 PartitionExpression (io.crate.operation.reference.partitioned.PartitionExpression)1