use of io.crate.analyze.symbol.DynamicReference 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.analyze.symbol.DynamicReference 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.analyze.symbol.DynamicReference in project crate by crate.
the class DocTableInfo method getDynamic.
@Nullable
public DynamicReference getDynamic(ColumnIdent ident, boolean forWrite) {
boolean parentIsIgnored = false;
ColumnPolicy parentPolicy = columnPolicy();
if (!ident.isColumn()) {
// see if parent is strict object
ColumnIdent parentIdent = ident.getParent();
Reference parentInfo = null;
while (parentIdent != null) {
parentInfo = getReference(parentIdent);
if (parentInfo != null) {
break;
}
parentIdent = parentIdent.getParent();
}
if (parentInfo != null) {
parentPolicy = parentInfo.columnPolicy();
}
}
switch(parentPolicy) {
case DYNAMIC:
if (!forWrite)
return null;
break;
case STRICT:
if (forWrite)
throw new ColumnUnknownException(ident.sqlFqn());
return null;
case IGNORED:
parentIsIgnored = true;
break;
default:
break;
}
if (parentIsIgnored) {
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity(), ColumnPolicy.IGNORED);
}
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity());
}
use of io.crate.analyze.symbol.DynamicReference in project crate by crate.
the class DocTableInfoTest method testGetColumnInfo.
@Test
public void testGetColumnInfo() throws Exception {
TableIdent tableIdent = new TableIdent(null, "dummy");
DocTableInfo info = new DocTableInfo(tableIdent, ImmutableList.of(new Reference(new ReferenceIdent(tableIdent, new ColumnIdent("o", ImmutableList.of())), RowGranularity.DOC, DataTypes.OBJECT)), ImmutableList.of(), ImmutableList.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableList.of(), null, false, true, new String[0], null, new IndexNameExpressionResolver(Settings.EMPTY), 5, new BytesRef("0"), ImmutableMap.of(), ImmutableList.of(), ImmutableList.of(), ColumnPolicy.DYNAMIC, DocIndexMetaData.DEFAULT_ROUTING_HASH_FUNCTION, Version.CURRENT, null, Operation.ALL, executorService);
Reference foobar = info.getReference(new ColumnIdent("o", ImmutableList.of("foobar")));
assertNull(foobar);
DynamicReference reference = info.getDynamic(new ColumnIdent("o", ImmutableList.of("foobar")), false);
assertNull(reference);
reference = info.getDynamic(new ColumnIdent("o", ImmutableList.of("foobar")), true);
assertNotNull(reference);
assertSame(reference.valueType(), DataTypes.UNDEFINED);
}
Aggregations