[fix](ES catalog)Make col != '' behavior consistent with SQL (#34151)

In SQL syntax, `col != ''` equals `col.length() > 0`.
It means that this column must exist in ES doc fields and its content is not empty.
In this PR, we make a special translation for this binary predicate to keep the behavior of both consistent.

---------

Co-authored-by: Luennng <luennng@gmail.com>
This commit is contained in:
qiye
2024-04-26 19:46:49 +08:00
committed by yiguolei
parent 3ba42a7823
commit 414fbd353e
4 changed files with 21 additions and 9 deletions

View File

@ -40,6 +40,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.format.DateTimeFormat;
@ -137,6 +138,15 @@ public final class QueryBuilders {
case EQ_FOR_NULL:
return QueryBuilders.termQuery(column, value);
case NE:
// col != '' means col.length() > 0 in SQL syntax.
// The `NULL` value should not present in results.
// It equals
// '{"bool":{"must":{"bool":{"must_not":{"term":{"col":""}},"must":{"exists":{"field":"col"}}}}}}'
// in Elasticsearch
if (value instanceof String && StringUtils.isEmpty((String) value)) {
return QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(column, value))
.must(QueryBuilders.existsQuery(column));
}
return QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(column, value));
case GE:
return QueryBuilders.rangeQuery(column).gte(value);