From 51389e5f02a06b50905a5062167fa733604a4652 Mon Sep 17 00:00:00 2001 From: Krzysztof Kiewicz Date: Sun, 22 Dec 2024 16:48:15 +0100 Subject: [PATCH] Cleanup --- quesma/logger/log_with_throttling.go | 1 + quesma/model/base_visitor.go | 6 +++--- quesma/queryparser/query_parser.go | 4 +++- quesma/queryparser/query_parser_test.go | 3 --- quesma/quesma/schema_transformer.go | 4 +--- quesma/testdata/requests.go | 2 +- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/quesma/logger/log_with_throttling.go b/quesma/logger/log_with_throttling.go index 91a850ff3..5b24de79a 100644 --- a/quesma/logger/log_with_throttling.go +++ b/quesma/logger/log_with_throttling.go @@ -9,6 +9,7 @@ import ( ) // throttleMap: (reason name -> last logged time) +// We log only once per throttleDuration for each reason name, so that we don't spam the logs. var throttleMap = util.SyncMap[string, time.Time]{} const throttleDuration = 30 * time.Minute diff --git a/quesma/model/base_visitor.go b/quesma/model/base_visitor.go index 71d68d04b..88b2cede6 100644 --- a/quesma/model/base_visitor.go +++ b/quesma/model/base_visitor.go @@ -45,11 +45,11 @@ func (v *BaseExprVisitor) VisitLiteral(e LiteralExpr) interface{} { return NewLiteral(e.Value) } -func (v *BaseExprVisitor) VisitTuple(e TupleExpr) interface{} { +func (v *BaseExprVisitor) VisitTuple(t TupleExpr) interface{} { if v.OverrideVisitTuple != nil { - return v.OverrideVisitTuple(v, e) + return v.OverrideVisitTuple(v, t) } - return NewTupleExpr(v.VisitChildren(e.Exprs)...) + return NewTupleExpr(v.VisitChildren(t.Exprs)...) } func (v *BaseExprVisitor) VisitInfix(e InfixExpr) interface{} { diff --git a/quesma/queryparser/query_parser.go b/quesma/queryparser/query_parser.go index 9a8e8c657..2e947a0c5 100644 --- a/quesma/queryparser/query_parser.go +++ b/quesma/queryparser/query_parser.go @@ -918,6 +918,8 @@ func (cw *ClickhouseQueryTranslator) parseRegexp(queryMap QueryMap) (result mode var funcName string if isPatternReallySimple(pattern) { + // We'll escape this _ twice (first one here, second one in renderer, where we escape all \) + // But it's not a problem for Clickhouse! So it seems fine. pattern = strings.ReplaceAll(pattern, "_", `\_`) pattern = strings.ReplaceAll(pattern, ".*", "%") pattern = strings.ReplaceAll(pattern, ".", "_") @@ -926,7 +928,7 @@ func (cw *ClickhouseQueryTranslator) parseRegexp(queryMap QueryMap) (result mode funcName = "REGEXP" } return model.NewSimpleQuery( - model.NewInfixExpr(model.NewColumnRef(fieldName), funcName, model.NewLiteral("'"+pattern+"'")), true) + model.NewInfixExpr(model.NewColumnRef(fieldName), funcName, model.NewLiteral(util.SingleQuote(pattern))), true) } logger.ErrorWithCtx(cw.Ctx).Msg("parseRegexp: theoretically unreachable code") diff --git a/quesma/queryparser/query_parser_test.go b/quesma/queryparser/query_parser_test.go index c2f3a64a9..b857a8574 100644 --- a/quesma/queryparser/query_parser_test.go +++ b/quesma/queryparser/query_parser_test.go @@ -65,9 +65,6 @@ func TestQueryParserStringAttrConfig(t *testing.T) { for i, tt := range testdata.TestsSearch { t.Run(fmt.Sprintf("%s(%d)", tt.Name, i), func(t *testing.T) { - if i == 37 { - t.Skip("Regexp seems to be broken because of some transformations") - } body, parseErr := types.ParseJSON(tt.QueryJson) assert.NoError(t, parseErr) plan, errQuery := cw.ParseQuery(body) diff --git a/quesma/quesma/schema_transformer.go b/quesma/quesma/schema_transformer.go index a69b31e30..d4a9000e5 100644 --- a/quesma/quesma/schema_transformer.go +++ b/quesma/quesma/schema_transformer.go @@ -958,9 +958,7 @@ func (s *SchemaCheckPass) applyMatchOperator(indexSchema schema.Schema, query *m visitor.OverrideVisitInfix = func(b *model.BaseExprVisitor, e model.InfixExpr) interface{} { lhs, ok := e.Left.(model.ColumnRef) rhs, ok2 := e.Right.(model.LiteralExpr) - if e.Op == model.MatchOperator { - fmt.Println("KKKKK", e.Left, lhs, rhs, ok, ok2) - } + if ok && ok2 && e.Op == model.MatchOperator { field, found := indexSchema.ResolveFieldByInternalName(lhs.ColumnName) if !found { diff --git a/quesma/testdata/requests.go b/quesma/testdata/requests.go index b8d149863..61b2464a1 100644 --- a/quesma/testdata/requests.go +++ b/quesma/testdata/requests.go @@ -2254,7 +2254,7 @@ var TestsSearch = []SearchTestCase{ }, "track_total_hits": false }`, - []string{`"field" LIKE '%\___'`}, + []string{`"field" LIKE '%\\___'`}, // escaping _ twice ("\\_") seemed wrong, but it actually works in Clickhouse! model.ListAllFields, []string{ `SELECT "message" ` +