From 93d48a85ac72599698eabfdbfebdc99f19e8462d Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Thu, 29 Sep 2022 12:33:53 +0300 Subject: [PATCH] added fallback handling when both contains operands are table columns --- tools/search/filter.go | 12 ++++++++++++ tools/search/filter_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/search/filter.go b/tools/search/filter.go index 286a83966..c7b02713e 100644 --- a/tools/search/filter.go +++ b/tools/search/filter.go @@ -103,16 +103,28 @@ func (f FilterData) resolveTokenizedExpr(expr fexpr.Expr, fieldResolver FieldRes case fexpr.SignNeq: return dbx.NewExp(fmt.Sprintf("COALESCE(%s, '') != COALESCE(%s, '')", lName, rName), params), nil case fexpr.SignLike: + // both sides are columns and therefore wrap the right side with "%" for contains like behavior + if len(params) == 0 { + return dbx.NewExp(fmt.Sprintf("%s LIKE ('%%' || %s || '%%')", lName, rName), params), nil + } + // normalize operands and switch sides if the left operand is a number or text if len(lParams) > 0 { return dbx.NewExp(fmt.Sprintf("%s LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil } + return dbx.NewExp(fmt.Sprintf("%s LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil case fexpr.SignNlike: + // both sides are columns and therefore wrap the right side with "%" for not-contains like behavior + if len(params) == 0 { + return dbx.NewExp(fmt.Sprintf("%s NOT LIKE ('%%' || %s || '%%')", lName, rName), params), nil + } + // normalize operands and switch sides if the left operand is a number or text if len(lParams) > 0 { return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil } + return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil case fexpr.SignLt: return dbx.NewExp(fmt.Sprintf("%s < %s", lName, rName), params), nil diff --git a/tools/search/filter_test.go b/tools/search/filter_test.go index 5e9708b53..d9c079a6d 100644 --- a/tools/search/filter_test.go +++ b/tools/search/filter_test.go @@ -32,6 +32,34 @@ func TestFilterDataBuildExpr(t *testing.T) { regexp.QuoteMeta("}") + "$", }, + // like with 2 columns + {"test1 ~ test2", false, + "^" + + regexp.QuoteMeta("[[test1]] LIKE ('%' || [[test2]] || '%')") + + "$", + }, + // reversed like with text + {"'lorem' ~ test1", false, + "^" + + regexp.QuoteMeta("[[test1]] LIKE {:") + + ".+" + + regexp.QuoteMeta("}") + + "$", + }, + // not like with 2 columns + {"test1 !~ test2", false, + "^" + + regexp.QuoteMeta("[[test1]] NOT LIKE ('%' || [[test2]] || '%')") + + "$", + }, + // reversed not like with text + {"'lorem' !~ test1", false, + "^" + + regexp.QuoteMeta("[[test1]] NOT LIKE {:") + + ".+" + + regexp.QuoteMeta("}") + + "$", + }, // current datetime constant {"test1 > @now", false, "^" +