Skip to content

Commit

Permalink
[Fix apache#2163] Add contains support
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Dec 16, 2024
1 parent 3c78fe9 commit 130028d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.index.postgresql;

import java.util.List;

import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.query.ReturnableType;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.type.BasicTypeReference;
import org.hibernate.type.SqlTypes;

public class ContainsSQLFunction extends StandardSQLFunction {

static final String NAME = "contains";

private static final BasicTypeReference<Boolean> RETURN_TYPE = new BasicTypeReference<>("boolean", Boolean.class, SqlTypes.BOOLEAN);

public ContainsSQLFunction() {
super(NAME, RETURN_TYPE);
}

@Override
public void render(
SqlAppender sqlAppender,
List<? extends SqlAstNode> args,
ReturnableType<?> returnType,
SqlAstTranslator<?> translator) {
args.get(0).accept(translator);
sqlAppender.append(" ?? ");
args.get(1).accept(translator);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.index.postgresql;

import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.FunctionContributor;

public class CustomFunctionsContributor implements FunctionContributor {

@Override
public void contributeFunctions(FunctionContributions functionContributions) {
functionContributions.getFunctionRegistry()
.register(ContainsSQLFunction.NAME, new ContainsSQLFunction());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public static Predicate buildPredicate(AttributeFilter<?> filter, Root<?> root,
values = (List<Object>) filter.getValue();
isString = values.get(0) instanceof String;
return buildPathExpression(builder, root, filter.getAttribute(), isString).in(values.stream().map(o -> buildObjectExpression(builder, o, isString)).collect(Collectors.toList()));
case CONTAINS:
return builder.isTrue(
builder.function("contains", Boolean.class, buildPathExpression(builder, root, filter.getAttribute(), false), builder.literal(filter.getValue())));
}
throw new UnsupportedOperationException("Filter " + filter + " is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.kie.kogito.index.postgresql.CustomFunctionsContributor
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ void testProcessInstanceVariables() {
processInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(or(List.of(jsonFilter(notNull("variables.traveller.aliases")), jsonFilter(lessThan("variables.traveller.age", 22))))), null, null, null,
processInstanceId);
// TODO add support for json contains (requires writing dialect extension on hibernate)
//queryAndAssert(assertWithId(), storage, singletonList(jsonFilter(contains("variables.traveller.aliases", "TheRealThing"))), null, null, null,
// processInstanceId);
//queryAndAssert(assertEmpty(), storage, singletonList(jsonFilter(contains("variables.traveller.aliases", "TheDummyThing"))), null, null, null,
// processInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(jsonFilter(contains("variables.traveller.aliases", "TheRealThing"))), null, null, null,
processInstanceId);
queryAndAssert(assertNotId(), storage, singletonList(jsonFilter(contains("variables.traveller.aliases", "TheDummyThing"))), null, null, null,
processInstanceId);
}
}

0 comments on commit 130028d

Please sign in to comment.