Search in sources :

Example 16 with QualifiedName

use of io.crate.sql.tree.QualifiedName in project crate by crate.

the class SymbolPrinterTest method testPrintRelationColumn.

@Test
public void testPrintRelationColumn() throws Exception {
    Symbol relationColumn = new RelationColumn(new QualifiedName(TABLE_NAME), 42, DataTypes.STRING);
    assertPrint(relationColumn, "RELCOL(formatter, 42)");
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 17 with QualifiedName

use of io.crate.sql.tree.QualifiedName 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 18 with QualifiedName

use of io.crate.sql.tree.QualifiedName in project crate by crate.

the class FieldProviderTest method testRelationFromTwoTables.

@Test
public void testRelationFromTwoTables() throws Exception {
    // select name from doc.t, custom.t
    FieldProvider<Field> resolver = new FullQualifedNameFieldProvider(ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName(Arrays.asList("custom", "t")), new DummyRelation("address"), new QualifiedName(Arrays.asList("doc", "t")), new DummyRelation("name")));
    resolver.resolveField(new QualifiedName(Arrays.asList("t", "name")), Operation.READ);
}
Also used : Field(io.crate.analyze.symbol.Field) QualifiedName(io.crate.sql.tree.QualifiedName) DummyRelation(io.crate.testing.DummyRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 19 with QualifiedName

use of io.crate.sql.tree.QualifiedName in project crate by crate.

the class FieldProviderTest method testRelationFromTwoTablesWithSameNameDifferentSchemaIsAmbiguous.

@Test
public void testRelationFromTwoTablesWithSameNameDifferentSchemaIsAmbiguous() throws Exception {
    // select t.name from custom.t.name, doc.t.name
    expectedException.expect(AmbiguousColumnException.class);
    expectedException.expectMessage("Column \"name\" is ambiguous");
    FieldProvider<Field> resolver = new FullQualifedNameFieldProvider(ImmutableMap.<QualifiedName, AnalyzedRelation>of(new QualifiedName(Arrays.asList("custom", "t")), new DummyRelation("name"), new QualifiedName(Arrays.asList("doc", "t")), new DummyRelation("name")));
    resolver.resolveField(new QualifiedName(Arrays.asList("t", "name")), Operation.READ);
}
Also used : Field(io.crate.analyze.symbol.Field) QualifiedName(io.crate.sql.tree.QualifiedName) DummyRelation(io.crate.testing.DummyRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 20 with QualifiedName

use of io.crate.sql.tree.QualifiedName in project crate by crate.

the class FieldProviderTest method testRelationOutputFromAlias.

@Test
public void testRelationOutputFromAlias() throws Exception {
    // t.name from doc.foo t
    AnalyzedRelation relation = new DummyRelation("name");
    FieldProvider<Field> resolver = new FullQualifedNameFieldProvider(ImmutableMap.of(new QualifiedName(Arrays.asList("t")), relation));
    Field field = resolver.resolveField(newQN("t.name"), Operation.READ);
    assertThat(field.relation(), equalTo(relation));
    assertThat(field.path().outputName(), is("name"));
}
Also used : Field(io.crate.analyze.symbol.Field) QualifiedName(io.crate.sql.tree.QualifiedName) DummyRelation(io.crate.testing.DummyRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

QualifiedName (io.crate.sql.tree.QualifiedName)28 Test (org.junit.Test)15 CrateUnitTest (io.crate.test.integration.CrateUnitTest)12 Field (io.crate.analyze.symbol.Field)8 SqlExpressions (io.crate.testing.SqlExpressions)7 DocTableInfo (io.crate.metadata.doc.DocTableInfo)6 DummyRelation (io.crate.testing.DummyRelation)6 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)5 TableRelation (io.crate.analyze.relations.TableRelation)5 WhereClause (io.crate.analyze.WhereClause)4 TableIdent (io.crate.metadata.TableIdent)4 Before (org.junit.Before)4 ArrayType (io.crate.types.ArrayType)3 DocTableRelation (io.crate.analyze.relations.DocTableRelation)2 JoinPair (io.crate.analyze.relations.JoinPair)2 Symbol (io.crate.analyze.symbol.Symbol)2 PartitionName (io.crate.metadata.PartitionName)2 TableInfo (io.crate.metadata.table.TableInfo)2 Set (java.util.Set)2 Nullable (javax.annotation.Nullable)2