Search in sources :

Example 6 with RelationSource

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);
    }
}
Also used : RelationSource(io.crate.analyze.RelationSource) Reference(io.crate.metadata.Reference) QualifiedName(io.crate.sql.tree.QualifiedName) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 7 with RelationSource

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

Example 8 with RelationSource

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

Example 9 with RelationSource

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

Aggregations

RelationSource (io.crate.analyze.RelationSource)9 MultiSourceSelect (io.crate.analyze.MultiSourceSelect)7 CrateUnitTest (io.crate.test.integration.CrateUnitTest)7 Test (org.junit.Test)7 QuerySpec (io.crate.analyze.QuerySpec)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 Field (io.crate.analyze.symbol.Field)1 Function (io.crate.analyze.symbol.Function)1 Symbol (io.crate.analyze.symbol.Symbol)1 Reference (io.crate.metadata.Reference)1 QualifiedName (io.crate.sql.tree.QualifiedName)1