use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class InsertFromSubQueryAnalyzer method resolveTargetColumns.
private static Collection<Reference> resolveTargetColumns(Collection<String> targetColumnNames, DocTableInfo targetTable, int numSourceColumns) {
if (targetColumnNames.isEmpty()) {
return targetColumnsFromTargetTable(targetTable, numSourceColumns);
}
LinkedHashSet<Reference> columns = new LinkedHashSet<>(targetColumnNames.size());
for (String targetColumnName : targetColumnNames) {
ColumnIdent columnIdent = new ColumnIdent(targetColumnName);
Reference reference = targetTable.getReference(columnIdent);
Reference targetReference;
if (reference == null) {
DynamicReference dynamicReference = targetTable.getDynamic(columnIdent, true);
if (dynamicReference == null) {
throw new ColumnUnknownException(targetColumnName);
}
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 AnalyzedTableElements method changeToPartitionedByColumn.
void changeToPartitionedByColumn(ColumnIdent partitionedByIdent, boolean skipIfNotFound) {
Preconditions.checkArgument(!partitionedByIdent.name().startsWith("_"), "Cannot use system columns in PARTITIONED BY clause");
// need to call primaryKeys() before the partition column is removed from the columns list
if (!primaryKeys().isEmpty() && !primaryKeys().contains(partitionedByIdent.fqn())) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use non primary key column '%s' in PARTITIONED BY clause if primary key is set on table", partitionedByIdent.sqlFqn()));
}
AnalyzedColumnDefinition columnDefinition = columnDefinitionByIdent(partitionedByIdent);
if (columnDefinition == null) {
if (skipIfNotFound) {
return;
}
throw new ColumnUnknownException(partitionedByIdent.sqlFqn());
}
DataType columnType = DataTypes.ofMappingNameSafe(columnDefinition.dataType());
if (!DataTypes.isPrimitive(columnType)) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use column %s of type %s in PARTITIONED BY clause", columnDefinition.ident().sqlFqn(), columnDefinition.dataType()));
}
if (columnDefinition.isArrayOrInArray()) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use array column %s in PARTITIONED BY clause", columnDefinition.ident().sqlFqn()));
}
if (columnDefinition.indexConstraint().equals("analyzed")) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use column %s with fulltext index in PARTITIONED BY clause", columnDefinition.ident().sqlFqn()));
}
columnIdents.remove(columnDefinition.ident());
columnDefinition.indexConstraint(Reference.IndexType.NO.toString());
partitionedByColumns.add(columnDefinition);
}
use of io.crate.exceptions.ColumnUnknownException in project crate by crate.
the class ValueNormalizer method normalizeObjectValue.
@SuppressWarnings("unchecked")
private void normalizeObjectValue(Map<String, Object> value, Reference info) {
for (Map.Entry<String, Object> entry : value.entrySet()) {
AnalyzedColumnDefinition.validateName(entry.getKey());
ColumnIdent nestedIdent = ColumnIdent.getChild(info.ident().columnIdent(), entry.getKey());
TableInfo tableInfo = schemas.getTableInfo(info.ident().tableIdent());
Reference nestedInfo = tableInfo.getReference(nestedIdent);
if (nestedInfo == null) {
if (info.columnPolicy() == ColumnPolicy.IGNORED) {
continue;
}
DynamicReference dynamicReference = null;
if (tableInfo instanceof DocTableInfo) {
dynamicReference = ((DocTableInfo) tableInfo).getDynamic(nestedIdent, true);
}
if (dynamicReference == null) {
throw new ColumnUnknownException(nestedIdent.sqlFqn());
}
DataType type = DataTypes.guessType(entry.getValue());
if (type == null) {
throw new ColumnValidationException(info.ident().columnIdent().sqlFqn(), "Invalid value");
}
dynamicReference.valueType(type);
nestedInfo = dynamicReference;
} else {
if (entry.getValue() == null) {
continue;
}
}
if (nestedInfo.valueType() == DataTypes.OBJECT && entry.getValue() instanceof Map) {
normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo);
} else if (isObjectArray(nestedInfo.valueType()) && entry.getValue() instanceof Object[]) {
normalizeObjectArrayValue((Object[]) entry.getValue(), nestedInfo);
} else {
entry.setValue(normalizePrimitiveValue(entry.getValue(), nestedInfo));
}
}
}
use of io.crate.exceptions.ColumnUnknownException 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.ColumnUnknownException in project crate by crate.
the class ValueNormalizer method normalizeObjectValue.
@SuppressWarnings("unchecked")
private static void normalizeObjectValue(Map<String, Object> value, Reference info, TableInfo tableInfo) {
for (Map.Entry<String, Object> entry : value.entrySet()) {
ColumnIdent nestedIdent = ColumnIdent.getChildSafe(info.column(), entry.getKey());
Reference nestedInfo = tableInfo.getReference(nestedIdent);
if (nestedInfo == null) {
if (info.columnPolicy() == ColumnPolicy.IGNORED) {
continue;
}
DynamicReference dynamicReference = null;
if (tableInfo instanceof DocTableInfo) {
dynamicReference = ((DocTableInfo) tableInfo).getDynamic(nestedIdent, true, true);
}
if (dynamicReference == null) {
throw new ColumnUnknownException(nestedIdent.sqlFqn(), tableInfo.ident());
}
DataType type = DataTypes.guessType(entry.getValue());
if (type == null) {
throw new ColumnValidationException(info.column().sqlFqn(), tableInfo.ident(), "Invalid value");
}
dynamicReference.valueType(type);
nestedInfo = dynamicReference;
} else {
if (entry.getValue() == null) {
continue;
}
}
if (nestedInfo.valueType().id() == ObjectType.ID && entry.getValue() instanceof Map) {
normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo, tableInfo);
} else if (isObjectArray(nestedInfo.valueType()) && entry.getValue() instanceof List) {
normalizeObjectArrayValue((List<Map<String, Object>>) entry.getValue(), nestedInfo, tableInfo);
} else {
entry.setValue(normalizePrimitiveValue(entry.getValue(), nestedInfo));
}
}
}
Aggregations