Search in sources :

Example 41 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class UpdateConsumer method createSysTableUpdatePlan.

private static Plan createSysTableUpdatePlan(TableInfo tableInfo, Planner.Context plannerContext, UpdateAnalyzedStatement.NestedAnalyzedStatement nestedStatement) {
    Routing routing = plannerContext.allocateRouting(tableInfo, nestedStatement.whereClause(), Preference.PRIMARY.type());
    Reference idReference = tableInfo.getReference(DocSysColumns.ID);
    assert idReference != null : "table has no _id column";
    SysUpdateProjection updateProjection = new SysUpdateProjection(idReference.valueType(), nestedStatement.assignments());
    return createPlan(plannerContext, routing, tableInfo, idReference, updateProjection, nestedStatement.whereClause());
}
Also used : SysUpdateProjection(io.crate.planner.projection.SysUpdateProjection) Reference(io.crate.metadata.Reference) Routing(io.crate.metadata.Routing)

Example 42 with Reference

use of io.crate.metadata.Reference 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)

Example 43 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class QueriedDocTableFetchPushDown method pushDown.

@Nullable
QueriedDocTable pushDown() {
    if (querySpec.groupBy().isPresent() || querySpec.having().isPresent() || querySpec.hasAggregates()) {
        return null;
    }
    Optional<OrderBy> orderBy = querySpec.orderBy();
    FetchRequiredVisitor.Context context;
    if (orderBy.isPresent()) {
        context = new FetchRequiredVisitor.Context(new LinkedHashSet<>(orderBy.get().orderBySymbols()));
    } else {
        context = new FetchRequiredVisitor.Context();
    }
    boolean fetchRequired = FetchRequiredVisitor.INSTANCE.process(querySpec.outputs(), context);
    if (!fetchRequired)
        return null;
    // build the subquery
    QuerySpec sub = new QuerySpec();
    Reference docIdReference = DocSysColumns.forTable(tableRelation.tableInfo().ident(), DocSysColumns.FETCHID);
    List<Symbol> outputs = new ArrayList<>();
    if (orderBy.isPresent()) {
        sub.orderBy(orderBy.get());
        outputs.add(docIdReference);
        outputs.addAll(context.querySymbols());
    } else {
        outputs.add(docIdReference);
    }
    for (Symbol symbol : querySpec.outputs()) {
        if (Symbols.containsColumn(symbol, DocSysColumns.SCORE) && !outputs.contains(symbol)) {
            outputs.add(symbol);
        }
    }
    sub.outputs(outputs);
    QueriedDocTable subRelation = new QueriedDocTable(tableRelation, sub);
    HashMap<Symbol, InputColumn> symbolMap = new HashMap<>(sub.outputs().size());
    int index = 0;
    for (Symbol symbol : sub.outputs()) {
        symbolMap.put(symbol, new InputColumn(index++, symbol.valueType()));
    }
    // push down the where clause
    sub.where(querySpec.where());
    querySpec.where(null);
    ToFetchReferenceVisitor toFetchReferenceVisitor = new ToFetchReferenceVisitor();
    toFetchReferenceVisitor.processInplace(querySpec.outputs(), symbolMap);
    if (orderBy.isPresent()) {
        // replace order by symbols with input columns, we need to copy the order by since it was pushed down to the
        // subquery before
        List<Symbol> newOrderBySymbols = MappingSymbolVisitor.copying().process(orderBy.get().orderBySymbols(), symbolMap);
        querySpec.orderBy(new OrderBy(newOrderBySymbols, orderBy.get().reverseFlags(), orderBy.get().nullsFirst()));
    }
    sub.limit(querySpec.limit());
    sub.offset(querySpec.offset());
    return subRelation;
}
Also used : OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) QueriedDocTable(io.crate.analyze.relations.QueriedDocTable) QuerySpec(io.crate.analyze.QuerySpec) Nullable(javax.annotation.Nullable)

Example 44 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class InsertPlannerTest method testInsertPlanMultipleValues.

@Test
public void testInsertPlanMultipleValues() throws Exception {
    UpsertById upsertById = e.plan("insert into users (id, name) values (42, 'Deep Thought'), (99, 'Marvin')");
    assertThat(upsertById.insertColumns().length, is(2));
    Reference idRef = upsertById.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = upsertById.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(upsertById.items().size(), is(2));
    UpsertById.Item item1 = upsertById.items().get(0);
    assertThat(item1.index(), is("users"));
    assertThat(item1.id(), is("42"));
    assertThat(item1.routing(), is("42"));
    assertThat(item1.insertValues().length, is(2));
    assertThat((Long) item1.insertValues()[0], is(42L));
    assertThat((BytesRef) item1.insertValues()[1], is(new BytesRef("Deep Thought")));
    UpsertById.Item item2 = upsertById.items().get(1);
    assertThat(item2.index(), is("users"));
    assertThat(item2.id(), is("99"));
    assertThat(item2.routing(), is("99"));
    assertThat(item2.insertValues().length, is(2));
    assertThat((Long) item2.insertValues()[0], is(99L));
    assertThat((BytesRef) item2.insertValues()[1], is(new BytesRef("Marvin")));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 45 with Reference

use of io.crate.metadata.Reference in project crate by crate.

the class GroupByConsumer method groupedByPrimaryKeys.

private static boolean groupedByPrimaryKeys(DocTableRelation tableRelation, List<Symbol> groupBy) {
    List<ColumnIdent> primaryKeys = tableRelation.tableInfo().primaryKey();
    if (groupBy.size() != primaryKeys.size()) {
        return false;
    }
    for (int i = 0, groupBySize = groupBy.size(); i < groupBySize; i++) {
        Symbol groupBySymbol = groupBy.get(i);
        if (groupBySymbol instanceof Reference) {
            ColumnIdent columnIdent = ((Reference) groupBySymbol).ident().columnIdent();
            ColumnIdent pkIdent = primaryKeys.get(i);
            if (!pkIdent.equals(columnIdent)) {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference)

Aggregations

Reference (io.crate.metadata.Reference)84 Test (org.junit.Test)57 CrateUnitTest (io.crate.test.integration.CrateUnitTest)55 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)27 HashMap (java.util.HashMap)20 Map (java.util.Map)20 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)17 ColumnIdent (io.crate.metadata.ColumnIdent)15 Symbol (io.crate.analyze.symbol.Symbol)14 BytesRef (org.apache.lucene.util.BytesRef)11 TableIdent (io.crate.metadata.TableIdent)9 ReferenceIdent (io.crate.metadata.ReferenceIdent)8 InputColumn (io.crate.analyze.symbol.InputColumn)6 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)6 ShardId (org.elasticsearch.index.shard.ShardId)5 Routing (io.crate.metadata.Routing)4 UUID (java.util.UUID)4 OrderBy (io.crate.analyze.OrderBy)3 ShardResponse (io.crate.executor.transport.ShardResponse)3 ShardUpsertRequest (io.crate.executor.transport.ShardUpsertRequest)3