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());
}
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;
}
}
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;
}
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")));
}
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;
}
Aggregations