Search in sources :

Example 1 with RelationUnknown

use of io.crate.exceptions.RelationUnknown 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 2 with RelationUnknown

use of io.crate.exceptions.RelationUnknown in project crate by crate.

the class ViewAnalyzer method analyze.

public AnalyzedDropView analyze(DropView dropView, CoordinatorTxnCtx txnCtx) {
    // No exists check to avoid stale clusterState race conditions
    ArrayList<RelationName> views = new ArrayList<>(dropView.names().size());
    ArrayList<RelationName> missing = new ArrayList<>();
    for (QualifiedName qualifiedName : dropView.names()) {
        try {
            views.add(schemas.resolveView(qualifiedName, txnCtx.sessionContext().searchPath()).v2());
        } catch (RelationUnknown e) {
            if (!dropView.ifExists()) {
                missing.add(RelationName.of(qualifiedName, txnCtx.sessionContext().searchPath().currentSchema()));
            }
        }
    }
    if (!missing.isEmpty()) {
        throw new RelationsUnknown(missing);
    }
    return new AnalyzedDropView(views, dropView.ifExists());
}
Also used : RelationsUnknown(io.crate.exceptions.RelationsUnknown) RelationUnknown(io.crate.exceptions.RelationUnknown) QualifiedName(io.crate.sql.tree.QualifiedName) ArrayList(java.util.ArrayList) RelationName(io.crate.metadata.RelationName)

Example 3 with RelationUnknown

use of io.crate.exceptions.RelationUnknown in project crate by crate.

the class Schemas method resolveRelation.

/**
 * Resolves the provided ident relation (table or view) against the search path.
 * @param ident
 * @param searchPath
 * @throws RelationUnknown in case a valid relation cannot be resolved in the search path.
 * @return the corresponding RelationName
 */
public RelationName resolveRelation(QualifiedName ident, SearchPath searchPath) {
    String identSchema = schemaName(ident);
    String relation = relationName(ident);
    ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    if (identSchema == null) {
        for (String pathSchema : searchPath) {
            RelationName tableOrViewRelation = getTableOrViewRelation(pathSchema, relation, views);
            if (tableOrViewRelation != null) {
                return tableOrViewRelation;
            }
        }
    } else {
        RelationName tableOrViewRelation = getTableOrViewRelation(identSchema, relation, views);
        if (tableOrViewRelation != null) {
            return tableOrViewRelation;
        }
    }
    throw new RelationUnknown(ident.toString());
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata)

Example 4 with RelationUnknown

use of io.crate.exceptions.RelationUnknown in project crate by crate.

the class Schemas method getTableInfo.

/**
 * @param ident the table ident to get a TableInfo for
 * @return an instance of TableInfo for the given ident, guaranteed to be not null
 * @throws io.crate.exceptions.SchemaUnknownException if schema given in <code>ident</code>
 *                                                    does not exist
 * @throws RelationUnknown  if table given in <code>ident</code> does
 *                                                    not exist in the given schema
 */
public <T extends TableInfo> T getTableInfo(RelationName ident) {
    SchemaInfo schemaInfo = getSchemaInfo(ident);
    TableInfo info = schemaInfo.getTableInfo(ident.name());
    if (info == null) {
        throw new RelationUnknown(ident);
    }
    return (T) info;
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) TableInfo(io.crate.metadata.table.TableInfo) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Example 5 with RelationUnknown

use of io.crate.exceptions.RelationUnknown in project crate by crate.

the class Schemas method resolveView.

/**
 * @throws RelationUnknown if the view cannot be resolved against the search path.
 */
public Tuple<ViewMetadata, RelationName> resolveView(QualifiedName ident, SearchPath searchPath) {
    ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    ViewMetadata view = null;
    RelationName viewRelationName = null;
    String identSchema = schemaName(ident);
    String viewName = relationName(ident);
    if (views != null) {
        if (identSchema == null) {
            for (String pathSchema : searchPath) {
                SchemaInfo schemaInfo = schemas.get(pathSchema);
                if (schemaInfo != null) {
                    viewRelationName = new RelationName(pathSchema, viewName);
                    view = views.getView(viewRelationName);
                    if (view != null) {
                        break;
                    }
                }
            }
        } else {
            viewRelationName = new RelationName(identSchema, viewName);
            view = views.getView(viewRelationName);
        }
    }
    if (view == null) {
        throw new RelationUnknown(viewName);
    }
    return Tuple.tuple(view, viewRelationName);
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) ViewMetadata(io.crate.metadata.view.ViewMetadata) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Aggregations

RelationUnknown (io.crate.exceptions.RelationUnknown)14 RelationName (io.crate.metadata.RelationName)4 TableInfo (io.crate.metadata.table.TableInfo)4 Symbol (io.crate.expression.symbol.Symbol)3 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)3 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3 ArrayList (java.util.ArrayList)3 SchemaUnknownException (io.crate.exceptions.SchemaUnknownException)2 BlobSchemaInfo (io.crate.metadata.blob.BlobSchemaInfo)2 InformationSchemaInfo (io.crate.metadata.information.InformationSchemaInfo)2 PgCatalogSchemaInfo (io.crate.metadata.pgcatalog.PgCatalogSchemaInfo)2 SysSchemaInfo (io.crate.metadata.sys.SysSchemaInfo)2 SchemaInfo (io.crate.metadata.table.SchemaInfo)2 ViewsMetadata (io.crate.metadata.view.ViewsMetadata)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 Streamer (io.crate.Streamer)1 FutureActionListener (io.crate.action.FutureActionListener)1 AnalyzedRestoreSnapshot (io.crate.analyze.AnalyzedRestoreSnapshot)1 BoundRestoreSnapshot (io.crate.analyze.BoundRestoreSnapshot)1 GenericPropertiesConverter (io.crate.analyze.GenericPropertiesConverter)1