Search in sources :

Example 1 with DynamicReference

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;
}
Also used : ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) DynamicReference(io.crate.analyze.symbol.DynamicReference) DynamicReference(io.crate.analyze.symbol.DynamicReference)

Example 2 with DynamicReference

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

Example 3 with DynamicReference

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());
}
Also used : ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) DynamicReference(io.crate.analyze.symbol.DynamicReference) DynamicReference(io.crate.analyze.symbol.DynamicReference) Nullable(javax.annotation.Nullable)

Example 4 with DynamicReference

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);
}
Also used : DynamicReference(io.crate.analyze.symbol.DynamicReference) DynamicReference(io.crate.analyze.symbol.DynamicReference) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

DynamicReference (io.crate.analyze.symbol.DynamicReference)4 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)3 ColumnValidationException (io.crate.exceptions.ColumnValidationException)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 Reference (io.crate.metadata.Reference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1 DataType (io.crate.types.DataType)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 BytesRef (org.apache.lucene.util.BytesRef)1 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)1 Test (org.junit.Test)1