Search in sources :

Example 1 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class RelationAnalyzer method visitAliasedRelation.

@Override
protected AnalyzedRelation visitAliasedRelation(AliasedRelation node, StatementAnalysisContext context) {
    context.startRelation(true);
    AnalyzedRelation childRelation = node.getRelation().accept(this, context);
    AnalyzedRelation aliasedRelation = new AliasedAnalyzedRelation(childRelation, new RelationName(null, node.getAlias()), node.getColumnNames());
    context.endRelation();
    context.currentRelationContext().addSourceRelation(aliasedRelation);
    return aliasedRelation;
}
Also used : RelationName(io.crate.metadata.RelationName)

Example 2 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class FullQualifiedNameFieldProvider method resolveField.

@Override
public Symbol resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation, boolean errorOnUnknownObjectKey) {
    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;
    Symbol lastField = null;
    for (var entry : sources.entrySet()) {
        RelationName relName = entry.getKey();
        String sourceSchema = relName.schema();
        String sourceTableOrAlias = relName.name();
        if (columnSchema != null && !columnSchema.equals(sourceSchema)) {
            continue;
        }
        schemaMatched = true;
        if (columnTableName != null && !sourceTableOrAlias.equals(columnTableName)) {
            continue;
        }
        tableNameMatched = true;
        AnalyzedRelation sourceRelation = entry.getValue();
        Symbol newField = sourceRelation.getField(columnIdent, operation, errorOnUnknownObjectKey);
        if (newField != null) {
            if (lastField != null) {
                if (errorOnUnknownObjectKey == false) {
                    /* ex) CREATE TABLE c1 (obj object as (x int));
                         *     CREATE TABLE c2 (obj object as (y int));
                         *     select obj['x'] from c1, c2;
                         *     --> ambiguous because c2.obj['x'] is another candidate with errorOnUnknownObjectKey = false
                         */
                    return resolveField(qualifiedName, path, operation, true);
                }
                throw new AmbiguousColumnException(columnIdent, newField);
            }
            lastField = newField;
        }
    }
    if (lastField == null) {
        if (!schemaMatched || !tableNameMatched) {
            String schema = columnSchema == null ? defaultSchema : columnSchema;
            raiseUnsupportedFeatureIfInParentScope(columnSchema, columnTableName, schema);
            RelationName relationName = new RelationName(schema, columnTableName);
            throw new RelationUnknown(relationName);
        }
        RelationName relationName = sources.entrySet().iterator().next().getKey();
        throw new ColumnUnknownException(columnIdent.sqlFqn(), relationName);
    }
    return lastField;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) RelationUnknown(io.crate.exceptions.RelationUnknown) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Symbol(io.crate.expression.symbol.Symbol) RelationName(io.crate.metadata.RelationName) AmbiguousColumnException(io.crate.exceptions.AmbiguousColumnException)

Example 3 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class FetchRewrite method createFetchSources.

public Map<RelationName, FetchSource> createFetchSources() {
    HashMap<RelationName, FetchSource> fetchSources = new HashMap<>();
    List<Symbol> outputs = plan.outputs();
    for (int i = 0; i < outputs.size(); i++) {
        Symbol output = outputs.get(i);
        if (output instanceof FetchMarker) {
            FetchMarker fetchMarker = (FetchMarker) output;
            RelationName tableName = fetchMarker.fetchId().ident().tableIdent();
            FetchSource fetchSource = fetchSources.get(tableName);
            if (fetchSource == null) {
                fetchSource = new FetchSource();
                fetchSources.put(tableName, fetchSource);
            }
            fetchSource.addFetchIdColumn(new InputColumn(i, fetchMarker.valueType()));
            for (Reference fetchRef : fetchMarker.fetchRefs()) {
                fetchSource.addRefToFetch(fetchRef);
            }
        }
    }
    return fetchSources;
}
Also used : FetchSource(io.crate.planner.node.fetch.FetchSource) HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) FetchMarker(io.crate.expression.symbol.FetchMarker) InputColumn(io.crate.expression.symbol.InputColumn) Reference(io.crate.metadata.Reference) RelationName(io.crate.metadata.RelationName)

Example 4 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class JoinOrdering method buildBestOrderByJoinConditions.

private static void buildBestOrderByJoinConditions(List<Set<RelationName>> sets, LinkedHashSet<RelationName> bestOrder) {
    Iterator<Set<RelationName>> setsIterator = sets.iterator();
    while (setsIterator.hasNext()) {
        Set<RelationName> set = setsIterator.next();
        for (RelationName name : set) {
            if (bestOrder.contains(name)) {
                bestOrder.addAll(set);
                setsIterator.remove();
                setsIterator = sets.iterator();
                break;
            }
        }
    }
    // Add the rest of the relations to the end of the collection
    sets.forEach(bestOrder::addAll);
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) RelationName(io.crate.metadata.RelationName)

Example 5 with RelationName

use of io.crate.metadata.RelationName in project crate by crate.

the class JoinPlanBuilder method hasAdditionalDependencies.

private static boolean hasAdditionalDependencies(JoinPair joinPair) {
    Symbol condition = joinPair.condition();
    if (condition == null) {
        return false;
    }
    boolean[] hasAdditionalDependencies = { false };
    FieldsVisitor.visitFields(condition, scopedSymbol -> {
        RelationName relation = scopedSymbol.relation();
        if (!relation.equals(joinPair.left()) && !relation.equals(joinPair.right())) {
            hasAdditionalDependencies[0] = true;
        }
    });
    return hasAdditionalDependencies[0];
}
Also used : Symbol(io.crate.expression.symbol.Symbol) RelationName(io.crate.metadata.RelationName)

Aggregations

RelationName (io.crate.metadata.RelationName)180 Test (org.junit.Test)100 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)55 PartitionName (io.crate.metadata.PartitionName)47 Symbol (io.crate.expression.symbol.Symbol)42 Reference (io.crate.metadata.Reference)37 ColumnIdent (io.crate.metadata.ColumnIdent)31 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)21 ReferenceIdent (io.crate.metadata.ReferenceIdent)21 DocTableInfo (io.crate.metadata.doc.DocTableInfo)21 Map (java.util.Map)20 HashMap (java.util.HashMap)19 SqlExpressions (io.crate.testing.SqlExpressions)18 ArrayList (java.util.ArrayList)18 List (java.util.List)17 Before (org.junit.Before)17 DocTableRelation (io.crate.analyze.relations.DocTableRelation)13 SQLExecutor (io.crate.testing.SQLExecutor)11 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)10