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();
}
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);
}
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);
}
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));
}
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("]");
}
}
Aggregations