Search in sources :

Example 1 with DynamicReference

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));
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.expression.symbol.DynamicReference) DynamicReference(io.crate.expression.symbol.DynamicReference) DataType(io.crate.types.DataType) List(java.util.List) ColumnValidationException(io.crate.exceptions.ColumnValidationException) Map(java.util.Map)

Example 2 with DynamicReference

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnIdent(io.crate.metadata.ColumnIdent) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) DynamicReference(io.crate.expression.symbol.DynamicReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.expression.symbol.DynamicReference)

Example 3 with DynamicReference

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));
}
Also used : DynamicReference(io.crate.expression.symbol.DynamicReference) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.expression.symbol.DynamicReference) ReferenceIdent(io.crate.metadata.ReferenceIdent) Test(org.junit.Test)

Example 4 with DynamicReference

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']");
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) DynamicReference(io.crate.expression.symbol.DynamicReference) FetchReference(io.crate.expression.symbol.FetchReference) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) VoidReference(io.crate.expression.symbol.VoidReference) DynamicReference(io.crate.expression.symbol.DynamicReference) RelationName(io.crate.metadata.RelationName) ReferenceIdent(io.crate.metadata.ReferenceIdent) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 5 with DynamicReference

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);
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) ColumnPolicy(io.crate.sql.tree.ColumnPolicy) VoidReference(io.crate.expression.symbol.VoidReference) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) IndexReference(io.crate.metadata.IndexReference) DynamicReference(io.crate.expression.symbol.DynamicReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) VoidReference(io.crate.expression.symbol.VoidReference) DynamicReference(io.crate.expression.symbol.DynamicReference) CheckConstraint(io.crate.sql.tree.CheckConstraint) ReferenceIdent(io.crate.metadata.ReferenceIdent) Nullable(javax.annotation.Nullable)

Aggregations

DynamicReference (io.crate.expression.symbol.DynamicReference)6 Reference (io.crate.metadata.Reference)6 ColumnIdent (io.crate.metadata.ColumnIdent)5 ReferenceIdent (io.crate.metadata.ReferenceIdent)4 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)3 VoidReference (io.crate.expression.symbol.VoidReference)3 Test (org.junit.Test)3 GeneratedReference (io.crate.metadata.GeneratedReference)2 RelationName (io.crate.metadata.RelationName)2 ColumnValidationException (io.crate.exceptions.ColumnValidationException)1 FetchReference (io.crate.expression.symbol.FetchReference)1 IndexReference (io.crate.metadata.IndexReference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 CheckConstraint (io.crate.sql.tree.CheckConstraint)1 ColumnPolicy (io.crate.sql.tree.ColumnPolicy)1 QualifiedNameReference (io.crate.sql.tree.QualifiedNameReference)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 SymbolMatchers.isReference (io.crate.testing.SymbolMatchers.isReference)1 DataType (io.crate.types.DataType)1 LinkedHashSet (java.util.LinkedHashSet)1