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;
}
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());
}
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());
}
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;
}
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);
}
Aggregations