@Test public void testUpdateWithEmptyObjectArray() throws Exception { UpdateAnalyzedStatement.NestedAnalyzedStatement analysis = analyze("update users set friends=? where other_id=0", new Object[] {new Map[0], 0}) .nestedStatements() .get(0); Literal friendsLiteral = (Literal) analysis .assignments() .get(new Reference(USER_TABLE_INFO.getReferenceInfo(new ColumnIdent("friends")))); assertThat(friendsLiteral.valueType().id(), is(ArrayType.ID)); assertEquals(DataTypes.OBJECT, ((ArrayType) friendsLiteral.valueType()).innerType()); assertThat(((Object[]) friendsLiteral.value()).length, is(0)); }
private TermsFilter termsFilter(String columnName, Literal arrayLiteral) { TermsFilter termsFilter; Object values = arrayLiteral.value(); if (values instanceof Collection) { termsFilter = new TermsFilter( columnName, getBytesRefs((Collection) values, TermBuilder.forType(arrayLiteral.valueType()))); } else { termsFilter = new TermsFilter( columnName, getBytesRefs((Object[]) values, TermBuilder.forType(arrayLiteral.valueType()))); } return termsFilter; }
@Override public Query apply(Function input, Context context) { Tuple<Reference, Literal> tuple = super.prepare(input); if (tuple == null) { return null; } Reference reference = tuple.v1(); Literal literal = tuple.v2(); String columnName = reference.info().ident().columnIdent().fqn(); if (DataTypes.isCollectionType(reference.valueType()) && DataTypes.isCollectionType(literal.valueType())) { // create boolean filter with term filters to pre-filter the result before applying the // functionQuery. BooleanFilter boolTermsFilter = new BooleanFilter(); DataType type = literal.valueType(); while (DataTypes.isCollectionType(type)) { type = ((CollectionType) type).innerType(); } QueryBuilderHelper builder = QueryBuilderHelper.forType(type); Object value = literal.value(); buildTermsQuery(boolTermsFilter, value, columnName, builder); if (boolTermsFilter.clauses().isEmpty()) { // all values are null... return genericFunctionQuery(input, context); } // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to // control the ordering of the filters // termsFilter is applied first // afterwards the more expensive genericFunctionFilter BooleanFilter filterClauses = new BooleanFilter(); filterClauses.add(boolTermsFilter, BooleanClause.Occur.MUST); filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST); return new FilteredQuery(Queries.newMatchAllQuery(), filterClauses); } QueryBuilderHelper builder = QueryBuilderHelper.forType(tuple.v1().valueType()); return builder.eq(columnName, tuple.v2().value()); }
@Override public Query apply(Function input, Context context) throws IOException { Tuple<Reference, Literal> tuple = prepare(input); if (tuple == null) { return null; } String field = tuple.v1().info().ident().columnIdent().fqn(); Literal literal = tuple.v2(); CollectionType dataType = ((CollectionType) literal.valueType()); Set values = (Set) literal.value(); DataType innerType = dataType.innerType(); BytesRef[] terms = getBytesRefs(values, TermBuilder.forType(innerType)); TermsFilter termsFilter = new TermsFilter(field, terms); return new FilteredQuery(Queries.newMatchAllQuery(), termsFilter); }