use of io.crate.expression.symbol.DynamicReference 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));
}
}
}
use of io.crate.expression.symbol.DynamicReference 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.expression.symbol.DynamicReference in project crate by crate.
the class LuceneReferenceResolverTest method test_ignored_dynamic_references_are_resolved_using_sourcelookup.
@Test
public void test_ignored_dynamic_references_are_resolved_using_sourcelookup() {
Reference ignored = new DynamicReference(new ReferenceIdent(name, "a", List.of("b")), RowGranularity.DOC, ColumnPolicy.IGNORED, 0);
assertThat(luceneReferenceResolver.getImplementation(ignored), instanceOf(DocCollectorExpression.ChildDocCollectorExpression.class));
}
use of io.crate.expression.symbol.DynamicReference in project crate by crate.
the class SymbolPrinterTest method testDynamicReference.
@Test
public void testDynamicReference() throws Exception {
Reference r = new DynamicReference(new ReferenceIdent(new RelationName("schema", "table"), new ColumnIdent("column", Arrays.asList("path", "nested"))), RowGranularity.DOC, 0);
assertPrint(r, "schema.\"table\".\"column\"['path']['nested']");
}
use of io.crate.expression.symbol.DynamicReference in project crate by crate.
the class DocTableInfo method getDynamic.
@Nullable
public DynamicReference getDynamic(ColumnIdent ident, boolean forWrite, boolean errorOnUnknownObjectKey) {
boolean parentIsIgnored = false;
ColumnPolicy parentPolicy = columnPolicy();
int position = 0;
if (!ident.isTopLevel()) {
// 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();
position = parentInfo.position();
}
}
switch(parentPolicy) {
case DYNAMIC:
if (!forWrite) {
if (!errorOnUnknownObjectKey) {
return new VoidReference(new ReferenceIdent(ident(), ident), rowGranularity(), position);
}
return null;
}
break;
case STRICT:
if (forWrite) {
throw new ColumnUnknownException(ident.sqlFqn(), ident());
}
return null;
case IGNORED:
parentIsIgnored = true;
break;
default:
break;
}
if (parentIsIgnored) {
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity(), ColumnPolicy.IGNORED, position);
}
return new DynamicReference(new ReferenceIdent(ident(), ident), rowGranularity(), position);
}
Aggregations