use of io.crate.analyze.RelationSource 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.RelationSource in project crate by crate.
the class RelationNormalizerTest method testFullJoinWithFiltersRewrittenToLeft.
@Test
public void testFullJoinWithFiltersRewrittenToLeft() throws Exception {
QueriedRelation relation = normalize("select t1.a, t2.i " + "from t1 full join t2 on t1.a = t2.b " + "where t2.y is null and t1.a = 'a'");
assertThat(relation, instanceOf(MultiSourceSelect.class));
MultiSourceSelect mss = (MultiSourceSelect) relation;
assertThat(mss.querySpec(), isSQL("SELECT doc.t1.a, doc.t2.i WHERE (ISNULL doc.t2.y)"));
assertThat(mss.joinPairs().get(0).joinType(), is(JoinType.LEFT));
assertThat(mss.joinPairs().get(0).left().toString(), is("doc.t1"));
assertThat(mss.joinPairs().get(0).right().toString(), is("doc.t2"));
assertThat(mss.joinPairs().get(0).condition(), isSQL("(doc.t1.a = doc.t2.b)"));
// make sure that where clause for t1 wasn't pushed down since but be applied after the FULL join
RelationSource t1 = mss.sources().get(T3.T1);
assertThat(t1.querySpec().where().query(), isSQL("(doc.t1.a = 'a')"));
RelationSource t2 = mss.sources().get(T3.T2);
assertThat(t2.querySpec().where().query(), is(nullValue()));
}
use of io.crate.analyze.RelationSource in project crate by crate.
the class RelationNormalizerTest method testSubSelectOnJoinsWithFilter.
@Test
public void testSubSelectOnJoinsWithFilter() throws Exception {
QueriedRelation relation = normalize("select col1, col2 from ( " + " select t1.a as col1, t2.i as col2, t2.y as col3 " + " from t1, t2 where t2.y > 60) as t " + "where col1 = 'a' order by col3");
assertThat(relation, instanceOf(MultiSourceSelect.class));
assertThat(relation.querySpec(), isSQL("SELECT doc.t1.a, doc.t2.i ORDER BY doc.t2.y"));
// make sure that where clause was pushed down and didn't disappear somehow
RelationSource t1 = ((MultiSourceSelect) relation).sources().get(T3.T1);
assertThat(t1.querySpec().where().query(), isSQL("(doc.t1.a = 'a')"));
RelationSource t2 = ((MultiSourceSelect) relation).sources().get(T3.T2);
assertThat(t2.querySpec().where().query(), isSQL("(true AND (doc.t2.y > 60))"));
}
use of io.crate.analyze.RelationSource in project crate by crate.
the class RelationNormalizerTest method testSubSelectOnRightJoinWithFilterRewrittenToInner.
@Test
public void testSubSelectOnRightJoinWithFilterRewrittenToInner() throws Exception {
QueriedRelation relation = normalize("select col1, col2 from ( " + " select t1.a as col1, t2.i as col2, t2.y as col3 " + " from t1 right join t2 on t1.a = t2.b where t1.x > 60) as t " + "where col2 = 10 order by col3");
assertThat(relation, instanceOf(MultiSourceSelect.class));
MultiSourceSelect mss = (MultiSourceSelect) relation;
assertThat(mss.querySpec(), isSQL("SELECT doc.t1.a, doc.t2.i ORDER BY doc.t2.y"));
assertThat(mss.joinPairs().get(0).joinType(), is(JoinType.INNER));
assertThat(mss.joinPairs().get(0).condition(), isSQL("(doc.t1.a = doc.t2.b)"));
// make sure that where clause was pushed down and didn't disappear somehow
RelationSource t1 = mss.sources().get(T3.T1);
assertThat(t1.querySpec().where().query(), isSQL("(doc.t1.x > 60)"));
RelationSource t2 = mss.sources().get(T3.T2);
assertThat(t2.querySpec().where().query(), isSQL("(doc.t2.i = 10)"));
}
Aggregations