Search in sources :

Example 46 with ArrayType

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

the class SelectStatementAnalyzerTest method testTryCastExpression.

@Test
public void testTryCastExpression() throws Exception {
    SelectAnalyzedStatement analysis = analyze("select try_cast(other_id as string) from users");
    assertThat(analysis.relation().querySpec().outputs().get(0), isFunction(CastFunctionResolver.tryFunctionsMap().get(DataTypes.STRING), Arrays.<DataType>asList(DataTypes.LONG)));
    analysis = analyze("select try_cast(1+1 as string) from users");
    assertThat(analysis.relation().querySpec().outputs().get(0), isLiteral("2", DataTypes.STRING));
    analysis = analyze("select try_cast(null as string) from users");
    assertThat(analysis.relation().querySpec().outputs().get(0), isLiteral(null, DataTypes.STRING));
    analysis = analyze("select try_cast(counters as array(boolean)) from users");
    assertThat(analysis.relation().querySpec().outputs().get(0), isFunction(CastFunctionResolver.tryFunctionsMap().get(new ArrayType(DataTypes.BOOLEAN)), Arrays.<DataType>asList(new ArrayType(DataTypes.LONG))));
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 47 with ArrayType

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

the class ParameterContextTest method testBulkNestedNested.

@Test
public void testBulkNestedNested() throws Exception {
    Object[][] bulkArgs = new Object[][] { new Object[] { new String[][] { new String[] { null } } }, new Object[] { new String[][] { new String[] { "foo" } } } };
    ParameterContext ctx = new ParameterContext(Row.EMPTY, Rows.of(bulkArgs));
    assertThat(ctx.getAsSymbol(0), isLiteral(bulkArgs[0][0], new ArrayType(new ArrayType(DataTypes.UNDEFINED))));
}
Also used : ArrayType(io.crate.types.ArrayType) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 48 with ArrayType

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

the class Literal method typeMatchesValue.

private static boolean typeMatchesValue(DataType type, Object value) {
    if (value == null) {
        return true;
    }
    if (type.equals(DataTypes.STRING) && (value instanceof BytesRef || value instanceof String)) {
        return true;
    }
    if (type instanceof ArrayType) {
        DataType innerType = ((ArrayType) type).innerType();
        while (innerType instanceof ArrayType && value.getClass().isArray()) {
            type = innerType;
            innerType = ((ArrayType) innerType).innerType();
            value = ((Object[]) value)[0];
        }
        if (innerType.equals(DataTypes.STRING)) {
            for (Object o : ((Object[]) value)) {
                if (o != null && !(o instanceof String || o instanceof BytesRef)) {
                    return false;
                }
            }
            return true;
        } else {
            return Arrays.equals((Object[]) value, ((ArrayType) type).value(value));
        }
    }
    // types like GeoPoint are represented as arrays
    if (value.getClass().isArray() && Arrays.equals((Object[]) value, (Object[]) type.value(value))) {
        return true;
    }
    return type.value(value).equals(value);
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType) BytesRef(org.apache.lucene.util.BytesRef)

Example 49 with ArrayType

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

the class ESFieldExtractorTest method testNullInList.

@Test
public void testNullInList() throws Exception {
    ESFieldExtractor.Source ex = new ESFieldExtractor.Source(new ColumnIdent("top", "child1"), new ArrayType(DataTypes.INTEGER));
    // test null value in list
    HashMap<String, Object> nullMap = new HashMap<String, Object>(1);
    nullMap.put("child1", null);
    ImmutableMap<String, Object> source = ImmutableMap.<String, Object>of("top", ImmutableList.of(nullMap, ImmutableMap.of("child1", 33)));
    Object[] objects = (Object[]) ex.toValue(source);
    assertThat(objects[0], nullValue());
    assertThat(((Integer) objects[1]), is(33));
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) HashMap(java.util.HashMap) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 50 with ArrayType

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

the class AnyLikeOperatorTest method normalizeSymbol.

private static Symbol normalizeSymbol(String pattern, String... expressions) {
    Literal patternLiteral = Literal.of(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }
    Literal valuesLiteral = Literal.of(new ArrayType(DataTypes.STRING), value);
    AnyLikeOperator impl = (AnyLikeOperator) new AnyLikeOperator.AnyLikeResolver().getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));
    Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    return impl.normalizeSymbol(function, new TransactionContext(SessionContext.SYSTEM_SESSION));
}
Also used : ArrayType(io.crate.types.ArrayType) Function(io.crate.analyze.symbol.Function) TransactionContext(io.crate.metadata.TransactionContext) Literal(io.crate.analyze.symbol.Literal) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ArrayType (io.crate.types.ArrayType)51 Test (org.junit.Test)36 CrateUnitTest (io.crate.test.integration.CrateUnitTest)35 DataType (io.crate.types.DataType)30 BytesRef (org.apache.lucene.util.BytesRef)9 Literal (io.crate.analyze.symbol.Literal)6 DocTableInfo (io.crate.metadata.doc.DocTableInfo)5 WhereClause (io.crate.analyze.WhereClause)4 Function (io.crate.analyze.symbol.Function)4 Input (io.crate.data.Input)4 TransactionContext (io.crate.metadata.TransactionContext)4 ColumnIdent (io.crate.metadata.ColumnIdent)3 QualifiedName (io.crate.sql.tree.QualifiedName)3 SqlExpressions (io.crate.testing.SqlExpressions)3 Before (org.junit.Before)3 TableRelation (io.crate.analyze.relations.TableRelation)2 FunctionImplementation (io.crate.metadata.FunctionImplementation)2 TableIdent (io.crate.metadata.TableIdent)2 NotPredicate (io.crate.operation.predicate.NotPredicate)2 HashMap (java.util.HashMap)2