Search in sources :

Example 21 with ArrayType

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

the class ArrayDifferenceFunction method evaluate.

@Override
public Object[] evaluate(Input[] args) {
    Object[] originalArray = (Object[]) args[0].value();
    if (originalArray == null) {
        return null;
    }
    DataType innerType = ((ArrayType) this.info().returnType()).innerType();
    Set<Object> localSubtractSet;
    if (!optionalSubtractSet.isPresent()) {
        localSubtractSet = new HashSet<>();
        for (int i = 1; i < args.length; i++) {
            Object argValue = args[i].value();
            if (argValue == null) {
                continue;
            }
            Object[] array = (Object[]) argValue;
            for (Object element : array) {
                localSubtractSet.add(innerType.value(element));
            }
        }
    } else {
        localSubtractSet = optionalSubtractSet.get();
    }
    List<Object> resultList = new ArrayList<>(originalArray.length);
    for (Object anOriginalArray : originalArray) {
        Object element = innerType.value(anOriginalArray);
        if (!localSubtractSet.contains(element)) {
            resultList.add(element);
        }
    }
    return resultList.toArray();
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType)

Example 22 with ArrayType

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

the class ArrayDifferenceFunction method compile.

@Override
public Scalar<Object[], Object> compile(List<Symbol> arguments) {
    Symbol symbol = arguments.get(1);
    if (!symbol.symbolType().isValueSymbol()) {
        // arguments are no values, we can't compile
        return this;
    }
    Input input = (Input) symbol;
    Object inputValue = input.value();
    DataType innerType = ((ArrayType) this.info().returnType()).innerType();
    Object[] array = (Object[]) inputValue;
    Set<Object> subtractSet = new HashSet<>();
    if (array.length > 0) {
        for (Object element : array) {
            subtractSet.add(innerType.value(element));
        }
    }
    return new ArrayDifferenceFunction(this.functionInfo, subtractSet);
}
Also used : ArrayType(io.crate.types.ArrayType) Input(io.crate.data.Input) Symbol(io.crate.analyze.symbol.Symbol) DataType(io.crate.types.DataType)

Example 23 with ArrayType

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

the class TransportShardUpsertActionTest method testGeneratedColumnsValidationWorksForArrayColumns.

@Test
public void testGeneratedColumnsValidationWorksForArrayColumns() throws Exception {
    // test no exception are thrown when validating array generated columns
    Map<String, Object> updatedColumns = MapBuilder.<String, Object>newMapBuilder().put("obj", MapBuilder.<String, Object>newMapBuilder().put("arr", new Object[] { 1 }).map()).map();
    Map<String, Object> updatedGeneratedColumns = MapBuilder.<String, Object>newMapBuilder().put("arr", new Object[] { 1 }).map();
    DocTableInfo docTableInfo = new TestingTableInfo.Builder(new TableIdent(null, "generated_column"), new Routing(Collections.<String, Map<String, List<Integer>>>emptyMap())).add("obj", DataTypes.OBJECT, null).add("obj", new ArrayType(DataTypes.INTEGER), Arrays.asList("arr")).addGeneratedColumn("arr", new ArrayType(DataTypes.INTEGER), "obj['arr']", false).build(getFunctions());
    transportShardUpsertAction.processGeneratedColumns(docTableInfo, updatedColumns, updatedGeneratedColumns, false);
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) MapBuilder(org.elasticsearch.common.collect.MapBuilder) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 24 with ArrayType

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

the class ESFieldExtractorTest method testPath2.

@Test
public void testPath2() throws Exception {
    ESFieldExtractor.Source ex = new ESFieldExtractor.Source(new ColumnIdent("top", "child1"), DataTypes.INTEGER);
    Map<String, Object> source;
    source = ImmutableMap.of();
    assertNull(ex.toValue(source));
    source = ImmutableMap.<String, Object>of("top", ImmutableMap.of("child1", 1, "child2", 2));
    assertEquals(1, ex.toValue(source));
    ex = new ESFieldExtractor.Source(new ColumnIdent("top", "child1"), new ArrayType(DataTypes.INTEGER));
    source = ImmutableMap.<String, Object>of("top", ImmutableList.of(ImmutableMap.of("child1", 1), ImmutableMap.of("child1", 2), ImmutableMap.of("child2", 22)));
    assertThat((Integer) ((Object[]) ex.toValue(source))[0], is(1));
    assertThat((Integer) ((Object[]) ex.toValue(source))[1], is(2));
    // if the container is present we get an empty list instead of null, to reflect the container exitence
    source = ImmutableMap.<String, Object>of("top", ImmutableList.of(ImmutableMap.of("child2", 22), ImmutableMap.of("child3", 33)));
    assertThat(((Object[]) ex.toValue(source)).length, is(0));
    // if the container does not match -> null
    source = ImmutableMap.<String, Object>of("nomatch", ImmutableList.of(ImmutableMap.of("child2", 22), ImmutableMap.of("child3", 33)));
    assertNull(ex.toValue(source));
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 25 with ArrayType

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

the class Function method printSubscriptFunction.

private void printSubscriptFunction(StringBuilder builder, Style style) {
    Symbol base = arguments.get(0);
    if (base instanceof Reference && base.valueType() instanceof ArrayType && ((Reference) base).column().path().size() > 0) {
        Reference firstArgument = (Reference) base;
        builder.append(firstArgument.column().name());
        builder.append("[");
        builder.append(arguments.get(1).toString(style));
        builder.append("]");
        builder.append("['");
        builder.append(firstArgument.column().path().get(0));
        builder.append("']");
    } else {
        builder.append(base.toString(style));
        builder.append("[");
        builder.append(arguments.get(1).toString(style));
        builder.append("]");
    }
}
Also used : ArrayType(io.crate.types.ArrayType) Reference(io.crate.metadata.Reference)

Aggregations

ArrayType (io.crate.types.ArrayType)76 Test (org.junit.Test)53 DataType (io.crate.types.DataType)35 CrateUnitTest (io.crate.test.integration.CrateUnitTest)20 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 Symbol (io.crate.expression.symbol.Symbol)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 BytesRef (org.apache.lucene.util.BytesRef)8 DocTableInfo (io.crate.metadata.doc.DocTableInfo)7 Literal (io.crate.analyze.symbol.Literal)6 Literal (io.crate.expression.symbol.Literal)6 ColumnIdent (io.crate.metadata.ColumnIdent)6 Input (io.crate.data.Input)5 HashMap (java.util.HashMap)5 Function (io.crate.analyze.symbol.Function)4 Reference (io.crate.metadata.Reference)4 DataTypes (io.crate.types.DataTypes)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TransactionContext (io.crate.metadata.TransactionContext)3