Search in sources :

Example 21 with QualifiedName

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

the class FieldProviderTest method testTooManyParts.

@Test
public void testTooManyParts() throws Exception {
    expectedException.expect(IllegalArgumentException.class);
    FieldProvider<Field> resolver = new FullQualifedNameFieldProvider(dummySources);
    resolver.resolveField(new QualifiedName(Arrays.asList("a", "b", "c", "d")), Operation.READ);
}
Also used : Field(io.crate.analyze.symbol.Field) QualifiedName(io.crate.sql.tree.QualifiedName) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 22 with QualifiedName

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

the class QuerySplitterTest method testSplitQueryWith2TableJoinAnd3TableJoin.

@Test
public void testSplitQueryWith2TableJoinAnd3TableJoin() throws Exception {
    Symbol symbol = asSymbol("t1.a = t2.b and t2.b = t3.c || t1.a");
    Map<Set<QualifiedName>, Symbol> split = QuerySplitter.split(symbol);
    assertThat(split.size(), is(2));
    Symbol t1t2 = asSymbol("t1.a = t2.b");
    Symbol t1t2t3 = asSymbol("t2.b = t3.c || t1.a");
    Set<QualifiedName> tr1AndTr2 = Sets.newHashSet(tr1, tr2);
    assertThat(split.containsKey(tr1AndTr2), is(true));
    assertThat(split.get(tr1AndTr2), is(t1t2));
    Set<QualifiedName> tr1AndTr2AndTr3 = Sets.newHashSet(tr1, tr2, tr3);
    assertThat(split.containsKey(tr1AndTr2AndTr3), is(true));
    assertThat(split.get(tr1AndTr2AndTr3), is(t1t2t3));
}
Also used : Set(java.util.Set) Symbol(io.crate.analyze.symbol.Symbol) QualifiedName(io.crate.sql.tree.QualifiedName) Test(org.junit.Test)

Example 23 with QualifiedName

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

the class ManyTableConsumerTest method testNoOptimizeWithSortingAndOuterJoin.

@Test
public void testNoOptimizeWithSortingAndOuterJoin() throws Exception {
    JoinPair pair1 = new JoinPair(T3.T1, T3.T2, JoinType.LEFT);
    JoinPair pair2 = new JoinPair(T3.T2, T3.T3, JoinType.LEFT);
    @SuppressWarnings("unchecked") Collection<QualifiedName> qualifiedNames = ManyTableConsumer.orderByJoinConditions(Arrays.asList(T3.T1, T3.T2, T3.T3), ImmutableSet.<Set<QualifiedName>>of(), ImmutableList.of(pair1, pair2), ImmutableList.of(T3.T3, T3.T2));
    assertThat(qualifiedNames, Matchers.contains(T3.T1, T3.T2, T3.T3));
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) JoinPair(io.crate.analyze.relations.JoinPair) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 24 with QualifiedName

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

the class FullQualifedNameFieldProvider method resolveField.

public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
    List<String> parts = qualifiedName.getParts();
    String columnSchema = null;
    String columnTableName = null;
    ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
    switch(parts.size()) {
        case 1:
            break;
        case 2:
            columnTableName = parts.get(0);
            break;
        case 3:
            columnSchema = parts.get(0);
            columnTableName = parts.get(1);
            break;
        default:
            throw new IllegalArgumentException("Column reference \"%s\" has too many parts. " + "A column reference can have at most 3 parts and must have one of the following formats:  " + "\"<column>\", \"<table>.<column>\" or \"<schema>.<table>.<column>\"");
    }
    boolean schemaMatched = false;
    boolean tableNameMatched = false;
    Field lastField = null;
    for (Map.Entry<QualifiedName, AnalyzedRelation> entry : sources.entrySet()) {
        List<String> sourceParts = entry.getKey().getParts();
        String sourceSchema = null;
        String sourceTableOrAlias;
        if (sourceParts.size() == 1) {
            sourceTableOrAlias = sourceParts.get(0);
        } else if (sourceParts.size() == 2) {
            sourceSchema = sourceParts.get(0);
            sourceTableOrAlias = sourceParts.get(1);
        } else {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "sources key (QualifiedName) must have 1 or 2 parts, not %d", sourceParts.size()));
        }
        AnalyzedRelation sourceRelation = entry.getValue();
        if (columnSchema != null && sourceSchema != null && !columnSchema.equals(sourceSchema)) {
            continue;
        }
        schemaMatched = true;
        if (columnTableName != null && !sourceTableOrAlias.equals(columnTableName)) {
            continue;
        }
        tableNameMatched = true;
        Field newField = sourceRelation.getField(columnIdent, operation);
        if (newField != null) {
            if (lastField != null) {
                throw new AmbiguousColumnException(columnIdent);
            }
            lastField = newField;
        }
    }
    if (lastField == null) {
        if (!schemaMatched || !tableNameMatched) {
            throw RelationUnknownException.of(columnSchema, columnTableName);
        }
        throw new ColumnUnknownException(columnIdent.sqlFqn());
    }
    return lastField;
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) ColumnIdent(io.crate.metadata.ColumnIdent) Field(io.crate.analyze.symbol.Field) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) AmbiguousColumnException(io.crate.exceptions.AmbiguousColumnException) Map(java.util.Map)

Example 25 with QualifiedName

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

the class RelationAnalysisContext method addJoinType.

void addJoinType(JoinType joinType, @Nullable Symbol joinCondition) {
    int size = sources.size();
    assert size >= 2 : "sources must be added first, cannot add join type for only 1 source";
    Iterator<QualifiedName> it = sources.keySet().iterator();
    QualifiedName left = null;
    QualifiedName right = null;
    int idx = 0;
    while (it.hasNext()) {
        QualifiedName sourceName = it.next();
        if (idx == size - 2) {
            left = sourceName;
        } else if (idx == size - 1) {
            right = sourceName;
        }
        idx++;
    }
    addJoinPair(new JoinPair(left, right, joinType, joinCondition));
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName)

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