Search in sources :

Example 16 with DataType

use of io.crate.types.DataType in project crate by crate.

the class WhereClauseAnalyzerTest method testAnyEqConvertableArrayTypeLiterals.

@Test
public void testAnyEqConvertableArrayTypeLiterals() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select * from users where name = any([1, 2, 3])");
    assertThat(whereClause.query(), isFunction(AnyEqOperator.NAME, ImmutableList.<DataType>of(DataTypes.STRING, new ArrayType(DataTypes.STRING))));
}
Also used : ArrayType(io.crate.types.ArrayType) WhereClause(io.crate.analyze.WhereClause) DataType(io.crate.types.DataType) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 17 with DataType

use of io.crate.types.DataType in project crate by crate.

the class WhereClauseAnalyzerTest method testAnyLikeConvertableArrayTypeLiterals.

@Test
public void testAnyLikeConvertableArrayTypeLiterals() throws Exception {
    WhereClause whereClause = analyzeSelectWhere("select * from users where name like any([1, 2, 3])");
    assertThat(whereClause.query(), isFunction(AnyLikeOperator.NAME, ImmutableList.<DataType>of(DataTypes.STRING, new ArrayType(DataTypes.STRING))));
}
Also used : ArrayType(io.crate.types.ArrayType) WhereClause(io.crate.analyze.WhereClause) DataType(io.crate.types.DataType) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 18 with DataType

use of io.crate.types.DataType 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 19 with DataType

use of io.crate.types.DataType in project crate by crate.

the class AbstractTableRelation method checkNestedArray.

protected Reference checkNestedArray(ColumnIdent ci, Reference reference) {
    // TODO: build type correctly as array when the tableInfo is created and remove the conversion here
    DataType dataType = null;
    ColumnIdent tmpCI = ci;
    Reference tmpRI = reference;
    while (!tmpCI.isColumn() && hasMatchingParent(tmpRI, IS_OBJECT_ARRAY)) {
        if (DataTypes.isCollectionType(reference.valueType())) {
            // TODO: remove this limitation with next type refactoring
            throw new UnsupportedOperationException("cannot query for arrays inside object arrays explicitly");
        }
        // return references of primitive types as array
        if (dataType == null) {
            dataType = new ArrayType(reference.valueType());
            if (hasNestedObjectReference(tmpRI))
                break;
        } else {
            dataType = new ArrayType(dataType);
        }
        tmpCI = tmpCI.getParent();
        tmpRI = tableInfo.getReference(tmpCI);
    }
    if (dataType != null) {
        return new Reference(reference.ident(), reference.granularity(), dataType, reference.columnPolicy(), reference.indexType(), reference.isNullable());
    } else {
        return reference;
    }
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType)

Example 20 with DataType

use of io.crate.types.DataType in project crate by crate.

the class DocIndexMetaDataTest method testNoBackwardCompatibleArrayMapping.

@Test
public void testNoBackwardCompatibleArrayMapping() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_meta").field("primary_keys", "id").startObject("columns").startObject("array_col").field("collection_type", "array").endObject().startObject("nested").startObject("properties").startObject("inner_nested").field("collection_type", "array").endObject().endObject().endObject().endObject().endObject().startObject("properties").startObject("id").field("type", "integer").field("index", "not_analyzed").endObject().startObject("title").field("type", "string").field("index", "no").endObject().startObject("array_col").field("type", "ip").field("index", "not_analyzed").endObject().startObject("nested").field("type", "nested").startObject("properties").startObject("inner_nested").field("type", "date").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject();
    IndexMetaData indexMetaData = getIndexMetaData("test1", builder);
    DocIndexMetaData docIndexMetaData = newMeta(indexMetaData, "test1");
    // ARRAY TYPES NOT DETECTED
    assertThat(docIndexMetaData.references().get(ColumnIdent.fromPath("array_col")).valueType(), is((DataType) DataTypes.IP));
    assertThat(docIndexMetaData.references().get(ColumnIdent.fromPath("nested.inner_nested")).valueType(), is((DataType) DataTypes.TIMESTAMP));
}
Also used : DataType(io.crate.types.DataType) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

DataType (io.crate.types.DataType)64 ArrayType (io.crate.types.ArrayType)30 Test (org.junit.Test)30 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28 FunctionIdent (io.crate.metadata.FunctionIdent)8 FunctionInfo (io.crate.metadata.FunctionInfo)7 WhereClause (io.crate.analyze.WhereClause)6 Symbol (io.crate.analyze.symbol.Symbol)4 Function (io.crate.analyze.symbol.Function)3 Input (io.crate.data.Input)3 Map (java.util.Map)3 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 Reference (io.crate.metadata.Reference)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 SubscriptFunction (io.crate.operation.scalar.SubscriptFunction)2 AddFunction (io.crate.operation.scalar.arithmetic.AddFunction)2 DistanceFunction (io.crate.operation.scalar.geo.DistanceFunction)2 MatchesFunction (io.crate.operation.scalar.regex.MatchesFunction)2