use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class MultiSourceFetchPushDown method process.
void process() {
remainingOutputs = statement.querySpec().outputs();
statement.querySpec().outputs(new ArrayList<>());
HashMap<Symbol, Symbol> topLevelOutputMap = new HashMap<>(statement.canBeFetched().size());
HashMap<Symbol, Symbol> mssOutputMap = new HashMap<>(statement.querySpec().outputs().size() + 2);
ArrayList<Symbol> mssOutputs = new ArrayList<>(statement.sources().size() + statement.requiredForQuery().size());
for (Map.Entry<QualifiedName, RelationSource> entry : statement.sources().entrySet()) {
RelationSource source = entry.getValue();
if (!(source.relation() instanceof DocTableRelation)) {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
continue;
}
DocTableRelation rel = (DocTableRelation) source.relation();
HashSet<Field> canBeFetched = filterByRelation(statement.canBeFetched(), rel);
if (!canBeFetched.isEmpty()) {
RelationColumn fetchIdColumn = new RelationColumn(entry.getKey(), 0, DataTypes.LONG);
mssOutputs.add(fetchIdColumn);
InputColumn fetchIdInput = new InputColumn(mssOutputs.size() - 1);
ArrayList<Symbol> qtOutputs = new ArrayList<>(source.querySpec().outputs().size() - canBeFetched.size() + 1);
Reference fetchId = rel.tableInfo().getReference(DocSysColumns.FETCHID);
qtOutputs.add(fetchId);
for (Symbol output : source.querySpec().outputs()) {
if (!canBeFetched.contains(output)) {
qtOutputs.add(output);
RelationColumn rc = new RelationColumn(entry.getKey(), qtOutputs.size() - 1, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
for (Field field : canBeFetched) {
FetchReference fr = new FetchReference(fetchIdInput, DocReferenceConverter.toSourceLookup(rel.resolveField(field)));
allocateFetchedReference(fr, rel);
topLevelOutputMap.put(field, fr);
}
source.querySpec().outputs(qtOutputs);
} else {
int index = 0;
for (Symbol output : source.querySpec().outputs()) {
RelationColumn rc = new RelationColumn(entry.getKey(), index++, output.valueType());
mssOutputs.add(rc);
mssOutputMap.put(output, rc);
topLevelOutputMap.put(output, new InputColumn(mssOutputs.size() - 1, output.valueType()));
}
}
}
statement.querySpec().outputs(mssOutputs);
MappingSymbolVisitor.inPlace().processInplace(remainingOutputs, topLevelOutputMap);
if (statement.querySpec().orderBy().isPresent()) {
MappingSymbolVisitor.inPlace().processInplace(statement.querySpec().orderBy().get().orderBySymbols(), mssOutputMap);
}
}
use of io.crate.analyze.relations.DocTableRelation in project crate by crate.
the class UpdateAnalyzerTest method testUpdateAssignments.
@Test
public void testUpdateAssignments() throws Exception {
UpdateAnalyzedStatement statement = analyze("update users set name='Trillian'");
UpdateAnalyzedStatement.NestedAnalyzedStatement statement1 = statement.nestedStatements().get(0);
assertThat(statement1.assignments().size(), is(1));
assertThat(((DocTableRelation) statement.sourceRelation()).tableInfo().ident(), is(new TableIdent(Schemas.DEFAULT_SCHEMA_NAME, "users")));
Reference ref = statement1.assignments().keySet().iterator().next();
assertThat(ref.ident().tableIdent().name(), is("users"));
assertThat(ref.ident().columnIdent().name(), is("name"));
assertTrue(statement1.assignments().containsKey(ref));
Symbol value = statement1.assignments().entrySet().iterator().next().getValue();
assertThat(value, isLiteral("Trillian"));
}
use of io.crate.analyze.relations.DocTableRelation 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.relations.DocTableRelation in project crate by crate.
the class DeleteAnalyzerTest method testDeleteWhere.
@Test
public void testDeleteWhere() throws Exception {
DeleteAnalyzedStatement statement = e.analyze("delete from users where name='Trillian'");
DocTableRelation tableRelation = statement.analyzedRelation;
TableInfo tableInfo = tableRelation.tableInfo();
assertThat(USER_TABLE_IDENT, equalTo(tableInfo.ident()));
assertThat(tableInfo.rowGranularity(), is(RowGranularity.DOC));
Function whereClause = (Function) statement.whereClauses.get(0).query();
assertEquals(EqOperator.NAME, whereClause.info().ident().name());
assertFalse(whereClause.info().type() == FunctionInfo.Type.AGGREGATE);
assertThat(whereClause.arguments().get(0), isReference("name"));
assertThat(whereClause.arguments().get(1), isLiteral("Trillian"));
}
Aggregations