Search in sources :

Example 21 with WhereClause

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());
}
Also used : WhereClause(io.crate.analyze.WhereClause) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 22 with WhereClause

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));
}
Also used : WhereClause(io.crate.analyze.WhereClause) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 23 with WhereClause

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);
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.operation.InputFactory) TableFunctionImplementation(io.crate.metadata.tablefunctions.TableFunctionImplementation) Reference(io.crate.metadata.Reference) Symbol(io.crate.analyze.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) ArrayList(java.util.ArrayList) Input(io.crate.data.Input) TableInfo(io.crate.metadata.table.TableInfo) ArrayList(java.util.ArrayList) List(java.util.List) Row(io.crate.data.Row) TableFunctionCollectPhase(io.crate.planner.node.dql.TableFunctionCollectPhase)

Example 24 with WhereClause

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());
}
Also used : ESDelete(io.crate.planner.node.dml.ESDelete) Delete(io.crate.planner.node.dml.Delete) NoopPlan(io.crate.planner.NoopPlan) WhereClause(io.crate.analyze.WhereClause) Plan(io.crate.planner.Plan) NoopPlan(io.crate.planner.NoopPlan) ESDeletePartition(io.crate.planner.node.ddl.ESDeletePartition)

Example 25 with WhereClause

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;
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) WhereClause(io.crate.analyze.WhereClause) Routing(io.crate.metadata.Routing) SysUpdateProjection(io.crate.planner.projection.SysUpdateProjection) UpdateProjection(io.crate.planner.projection.UpdateProjection)

Aggregations

WhereClause (io.crate.analyze.WhereClause)61 Test (org.junit.Test)46 CrateUnitTest (io.crate.test.integration.CrateUnitTest)43 Symbol (io.crate.analyze.symbol.Symbol)10 PartitionName (io.crate.metadata.PartitionName)8 BytesRef (org.apache.lucene.util.BytesRef)8 DataType (io.crate.types.DataType)6 TableInfo (io.crate.metadata.table.TableInfo)5 SqlExpressions (io.crate.testing.SqlExpressions)5 QualifiedName (io.crate.sql.tree.QualifiedName)4 ArrayType (io.crate.types.ArrayType)4 ImmutableList (com.google.common.collect.ImmutableList)3 OrderBy (io.crate.analyze.OrderBy)3 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TableRelation (io.crate.analyze.relations.TableRelation)3 Function (io.crate.analyze.symbol.Function)3 SQLTransportIntegrationTest (io.crate.integrationtests.SQLTransportIntegrationTest)3 CrateRegexQuery (io.crate.lucene.match.CrateRegexQuery)3 Literal (io.crate.analyze.symbol.Literal)2 Bucket (io.crate.data.Bucket)2