use of io.crate.analyze.WhereClause in project crate by crate.
the class WhereClauseAnalyzer method analyze.
public WhereClause analyze(WhereClause whereClause, TransactionContext transactionContext) {
if (!whereClause.hasQuery()) {
return whereClause;
}
Set<Symbol> clusteredBy = null;
if (whereClause.hasQuery()) {
WhereClauseValidator.validate(whereClause);
Symbol query = GENERATED_COLUMN_COMPARISON_REPLACER.replaceIfPossible(whereClause.query(), tableInfo);
if (!whereClause.query().equals(query)) {
whereClause = new WhereClause(normalizer.normalize(query, transactionContext));
}
}
List<ColumnIdent> pkCols;
boolean versionInQuery = Symbols.containsColumn(whereClause.query(), DocSysColumns.VERSION);
if (versionInQuery) {
pkCols = new ArrayList<>(tableInfo.primaryKey().size() + 1);
pkCols.addAll(tableInfo.primaryKey());
pkCols.add(DocSysColumns.VERSION);
} else {
pkCols = tableInfo.primaryKey();
}
List<List<Symbol>> pkValues = eqExtractor.extractExactMatches(pkCols, whereClause.query(), transactionContext);
if (!pkCols.isEmpty() && pkValues != null) {
int clusterdIdx = -1;
if (tableInfo.clusteredBy() != null) {
clusterdIdx = tableInfo.primaryKey().indexOf(tableInfo.clusteredBy());
clusteredBy = new HashSet<>(pkValues.size());
}
List<Integer> partitionsIdx = null;
if (tableInfo.isPartitioned()) {
partitionsIdx = new ArrayList<>(tableInfo.partitionedByColumns().size());
for (ColumnIdent columnIdent : tableInfo.partitionedBy()) {
int posPartitionColumn = tableInfo.primaryKey().indexOf(columnIdent);
if (posPartitionColumn >= 0) {
partitionsIdx.add(posPartitionColumn);
}
}
}
whereClause.docKeys(new DocKeys(pkValues, versionInQuery, clusterdIdx, partitionsIdx));
if (clusterdIdx >= 0) {
for (List<Symbol> row : pkValues) {
clusteredBy.add(row.get(clusterdIdx));
}
whereClause.clusteredBy(clusteredBy);
}
} else {
clusteredBy = getClusteredByLiterals(whereClause, eqExtractor, transactionContext);
}
if (clusteredBy != null) {
whereClause.clusteredBy(clusteredBy);
}
if (tableInfo.isPartitioned() && !whereClause.docKeys().isPresent()) {
whereClause = resolvePartitions(new WhereClause(normalizer.normalize(whereClause.query(), transactionContext)), tableInfo, functions, transactionContext);
}
return whereClause;
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class RelationSplitter method processWhere.
private void processWhere() {
if (!querySpec.where().hasQuery()) {
return;
}
Symbol query = querySpec.where().query();
assert query != null : "query must not be null";
QuerySplittingVisitor.Context context = QuerySplittingVisitor.INSTANCE.process(querySpec.where().query(), joinPairs);
JoinConditionValidator.INSTANCE.process(context.query(), null);
querySpec.where(new WhereClause(context.query()));
for (Map.Entry<QualifiedName, Collection<Symbol>> entry : context.queries().asMap().entrySet()) {
getSpec(entry.getKey()).where(new WhereClause(AndOperator.join(entry.getValue())));
}
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class DeleteStatementPlanner method planDelete.
public static Plan planDelete(DeleteAnalyzedStatement analyzedStatement, Planner.Context context) {
DocTableRelation tableRelation = analyzedStatement.analyzedRelation();
List<WhereClause> whereClauses = new ArrayList<>(analyzedStatement.whereClauses().size());
List<DocKeys.DocKey> docKeys = new ArrayList<>(analyzedStatement.whereClauses().size());
Map<Integer, Integer> itemToBulkIdx = new HashMap<>();
int bulkIdx = -1;
int itemIdx = 0;
for (WhereClause whereClause : analyzedStatement.whereClauses()) {
bulkIdx++;
if (whereClause.noMatch()) {
continue;
}
if (whereClause.docKeys().isPresent() && whereClause.docKeys().get().size() == 1) {
DocKeys.DocKey docKey = whereClause.docKeys().get().getOnlyKey();
if (docKey.id() != null) {
docKeys.add(docKey);
itemToBulkIdx.put(itemIdx, bulkIdx);
itemIdx++;
}
} else if (!whereClause.noMatch()) {
whereClauses.add(whereClause);
}
}
if (!docKeys.isEmpty()) {
return new ESDelete(context.jobId(), context.nextExecutionPhaseId(), tableRelation.tableInfo(), docKeys, itemToBulkIdx, analyzedStatement.whereClauses().size());
} else if (!whereClauses.isEmpty()) {
return deleteByQuery(tableRelation.tableInfo(), whereClauses, context);
}
return new NoopPlan(context.jobId());
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class InternalCountOperationTest method testCount.
@Test
public void testCount() throws Exception {
execute("create table t (name string) clustered into 1 shards with (number_of_replicas = 0)");
ensureYellow();
execute("insert into t (name) values ('Marvin'), ('Arthur'), ('Trillian')");
execute("refresh table t");
CountOperation countOperation = internalCluster().getDataNodeInstance(CountOperation.class);
assertThat(countOperation.count("t", 0, WhereClause.MATCH_ALL), is(3L));
Schemas schemas = internalCluster().getInstance(Schemas.class);
TableInfo tableInfo = schemas.getTableInfo(new TableIdent(null, "t"));
TableRelation tableRelation = new TableRelation(tableInfo);
Map<QualifiedName, AnalyzedRelation> tableSources = ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName(tableInfo.ident().name()), tableRelation);
SqlExpressions sqlExpressions = new SqlExpressions(tableSources, tableRelation);
WhereClause whereClause = new WhereClause(sqlExpressions.normalize(sqlExpressions.asSymbol("name = 'Marvin'")));
assertThat(countOperation.count("t", 0, whereClause), is(1L));
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class DocLevelCollectTest method testCollectDocLevelWhereClause.
@Test
public void testCollectDocLevelWhereClause() throws Throwable {
EqOperator op = (EqOperator) functions.get(new FunctionIdent(EqOperator.NAME, ImmutableList.<DataType>of(DataTypes.INTEGER, DataTypes.INTEGER)));
List<Symbol> toCollect = Collections.<Symbol>singletonList(testDocLevelReference);
WhereClause whereClause = new WhereClause(new Function(op.info(), Arrays.<Symbol>asList(testDocLevelReference, Literal.of(2))));
RoutedCollectPhase collectNode = getCollectNode(toCollect, whereClause);
Bucket result = collect(collectNode);
assertThat(result, contains(isRow(2)));
}
Aggregations