use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class InsertAnalyzer method resolveTargetColumns.
private static Collection<Reference> resolveTargetColumns(Collection<String> targetColumnNames, DocTableInfo targetTable) {
if (targetColumnNames.isEmpty()) {
return targetTable.columns();
}
LinkedHashSet<Reference> columns = new LinkedHashSet<>(targetColumnNames.size());
for (String targetColumnName : targetColumnNames) {
ColumnIdent columnIdent = ColumnIdent.fromPath(targetColumnName);
Reference reference = targetTable.getReference(columnIdent);
Reference targetReference;
if (reference == null) {
DynamicReference dynamicReference = targetTable.getDynamic(columnIdent, true, true);
if (dynamicReference == null) {
throw new ColumnUnknownException(targetColumnName, targetTable.ident());
}
targetReference = dynamicReference;
} else {
targetReference = reference;
}
if (!columns.add(targetReference)) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "reference '%s' repeated", targetColumnName));
}
}
return columns;
}
use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class InsertAnalyzer method verifyOnConflictTargets.
private static void verifyOnConflictTargets(CoordinatorTxnCtx txnCtx, ExpressionAnalyzer expressionAnalyzer, DocTableInfo docTable, Insert.DuplicateKeyContext<Expression> duplicateKeyContext) {
List<Expression> constraintColumns = duplicateKeyContext.getConstraintColumns();
if (constraintColumns.isEmpty()) {
return;
}
List<ColumnIdent> pkColumnIdents = docTable.primaryKey();
if (constraintColumns.size() != pkColumnIdents.size()) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Number of conflict targets (%s) did not match the number of primary key columns (%s)", constraintColumns, pkColumnIdents));
}
ExpressionAnalysisContext ctx = new ExpressionAnalysisContext(txnCtx.sessionContext());
List<Symbol> conflictTargets = Lists2.map(constraintColumns, x -> {
try {
return expressionAnalyzer.convert(x, ctx);
} catch (ColumnUnknownException e) {
// Going through ExpressionAnalyzer again to still have a "column must exist" validation
if (x instanceof QualifiedNameReference) {
QualifiedName name = ((QualifiedNameReference) x).getName();
Expression subscriptExpression = MetadataToASTNodeResolver.expressionFromColumn(ColumnIdent.fromPath(name.toString()));
return expressionAnalyzer.convert(subscriptExpression, ctx);
}
throw e;
}
});
for (Symbol conflictTarget : conflictTargets) {
if (!pkColumnIdents.contains(Symbols.pathFromSymbol(conflictTarget))) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Conflict target (%s) did not match the primary key columns (%s)", conflictTarget, pkColumnIdents));
}
}
}
use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class TableReferenceResolver method resolveField.
@Override
public Reference resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
List<String> parts = qualifiedName.getParts();
ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
if (parts.size() != 1) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Column reference \"%s\" has too many parts. " + "A column must not have a schema or a table here.", qualifiedName));
}
for (Reference reference : tableReferences) {
if (reference.ident().columnIdent().equals(columnIdent)) {
if (reference instanceof GeneratedReference) {
throw new IllegalArgumentException("A generated column cannot be based on a generated column");
}
references.add(reference);
return reference;
}
}
throw new ColumnUnknownException(columnIdent.sqlFqn());
}
use of io.crate.exceptions.ColumnUnknownException 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;
}
use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class NameFieldProvider method resolveField.
public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
List<String> parts = qualifiedName.getParts();
ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
if (parts.size() != 1) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Column reference \"%s\" has too many parts. " + "A column must not have a schema or a table here.", qualifiedName));
}
Field field = relation.getField(columnIdent, operation);
if (field == null) {
throw new ColumnUnknownException(columnIdent.sqlFqn());
}
return field;
}
Aggregations