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;
}
}
Aggregations