use of io.crate.analyze.WhereClause in project crate by crate.
the class WhereClauseAnalyzerTest method testSelectByIdWithCustomRouting.
@Test
public void testSelectByIdWithCustomRouting() throws Exception {
WhereClause whereClause = analyzeSelect("select name from users_clustered_by_only where _id=1");
assertFalse(whereClause.docKeys().isPresent());
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class SelectPlannerTest method testGlobalAggregateWithWhereOnPartitionColumn.
@Test
public void testGlobalAggregateWithWhereOnPartitionColumn() throws Exception {
Merge globalAggregate = e.plan("select min(name) from parted where date > 1395961100000");
Collect collect = (Collect) globalAggregate.subPlan();
WhereClause whereClause = ((RoutedCollectPhase) collect.collectPhase()).whereClause();
assertThat(whereClause.partitions().size(), is(1));
assertThat(whereClause.noMatch(), is(false));
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class TableFunctionCollectSource method getCollector.
@Override
public CrateCollector getCollector(CollectPhase collectPhase, BatchConsumer consumer, JobCollectContext jobCollectContext) {
TableFunctionCollectPhase phase = (TableFunctionCollectPhase) collectPhase;
WhereClause whereClause = phase.whereClause();
if (whereClause.noMatch()) {
return RowsCollector.empty(consumer);
}
TableFunctionImplementation functionImplementation = phase.relation().functionImplementation();
TableInfo tableInfo = functionImplementation.createTableInfo(clusterService);
//noinspection unchecked Only literals can be passed to table functions. Anything else is invalid SQL
List<Input<?>> inputs = (List<Input<?>>) (List) phase.relation().function().arguments();
List<Reference> columns = new ArrayList<>(tableInfo.columns());
List<Input<?>> topLevelInputs = new ArrayList<>(phase.toCollect().size());
InputFactory.Context<InputCollectExpression> ctx = inputFactory.ctxForRefs(i -> new InputCollectExpression(columns.indexOf(i)));
for (Symbol symbol : phase.toCollect()) {
topLevelInputs.add(ctx.add(symbol));
}
Iterable<Row> rows = Iterables.transform(functionImplementation.execute(inputs), new ValueAndInputRow<>(topLevelInputs, ctx.expressions()));
if (whereClause.hasQuery()) {
Input<Boolean> condition = (Input<Boolean>) ctx.add(whereClause.query());
rows = Iterables.filter(rows, InputCondition.asPredicate(condition));
}
OrderBy orderBy = phase.orderBy();
if (orderBy != null) {
rows = RowsTransformer.sortRows(Iterables.transform(rows, Row::materialize), phase);
}
return RowsCollector.forRows(rows, phase.toCollect().size(), consumer);
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class DeleteStatementPlanner method deleteByQuery.
private static Plan deleteByQuery(DocTableInfo tableInfo, List<WhereClause> whereClauses, Planner.Context context) {
List<Plan> planNodes = new ArrayList<>();
List<String> indicesToDelete = new ArrayList<>();
for (WhereClause whereClause : whereClauses) {
String[] indices = Planner.indices(tableInfo, whereClause);
if (indices.length > 0) {
if (!whereClause.hasQuery() && tableInfo.isPartitioned()) {
indicesToDelete.addAll(Arrays.asList(indices));
} else {
planNodes.add(collectWithDeleteProjection(tableInfo, whereClause, context));
}
}
}
if (!indicesToDelete.isEmpty()) {
assert planNodes.isEmpty() : "If a partition can be deleted that must be true for all bulk operations";
return new ESDeletePartition(context.jobId(), indicesToDelete.toArray(new String[0]));
}
if (planNodes.isEmpty()) {
return new NoopPlan(context.jobId());
}
return new Delete(planNodes, context.jobId());
}
use of io.crate.analyze.WhereClause in project crate by crate.
the class UpdateConsumer method upsertByQuery.
private static Plan upsertByQuery(UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalysis, Planner.Context plannerContext, DocTableInfo tableInfo, WhereClause whereClause) {
Symbol versionSymbol = null;
if (whereClause.hasVersions()) {
versionSymbol = VersionRewriter.get(whereClause.query());
whereClause = new WhereClause(whereClause.query(), whereClause.docKeys().orElse(null), whereClause.partitions());
}
if (!whereClause.noMatch() || !(tableInfo.isPartitioned() && whereClause.partitions().isEmpty())) {
// for updates, we always need to collect the `_id`
Reference idReference = tableInfo.getReference(DocSysColumns.ID);
Tuple<String[], Symbol[]> assignments = Assignments.convert(nestedAnalysis.assignments());
Long version = null;
if (versionSymbol != null) {
version = ValueSymbolVisitor.LONG.process(versionSymbol);
}
UpdateProjection updateProjection = new UpdateProjection(new InputColumn(0, DataTypes.STRING), assignments.v1(), assignments.v2(), version);
Routing routing = plannerContext.allocateRouting(tableInfo, whereClause, Preference.PRIMARY.type());
return createPlan(plannerContext, routing, tableInfo, idReference, updateProjection, whereClause);
} else {
return null;
}
}
Aggregations