Skip to content

Commit

Permalink
Tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek committed Dec 21, 2024
1 parent 2e55980 commit 9f1b22f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 4 additions & 3 deletions quesma/logger/log_with_throttling.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"
)

// throttleMap: (reason name -> last logged time)
var throttleMap = util.SyncMap[string, time.Time]{}

const throttleDuration = 30 * time.Minute
Expand All @@ -28,11 +29,11 @@ func WarnWithCtxAndThrottling(ctx context.Context, aggrName, paramName, format s

// WarnWithThrottling - logs a warning message with throttling.
// We only log once per throttleDuration for each warnName, so that we don't spam the logs.
func WarnWithThrottling(warnName, format string, v ...any) {
timestamp, ok := throttleMap.Load(warnName)
func WarnWithThrottling(reasonName, format string, v ...any) {
timestamp, ok := throttleMap.Load(reasonName)
weThrottle := ok && time.Since(timestamp) < throttleDuration
if !weThrottle {
Warn().Msgf(format, v...)
throttleMap.Store(warnName, time.Now())
throttleMap.Store(reasonName, time.Now())
}
}
15 changes: 10 additions & 5 deletions quesma/model/expr_string_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func (v *renderer) VisitFunction(e FunctionExpr) interface{} {
}

func (v *renderer) VisitLiteral(l LiteralExpr) interface{} {
return fmt.Sprintf("%v", l.Value)
switch val := l.Value.(type) {
case string:
return escapeString(val)
default:
return fmt.Sprintf("%v", val)
}
}

func (v *renderer) VisitTuple(t TupleExpr) interface{} {
Expand Down Expand Up @@ -351,11 +356,11 @@ func (v *renderer) VisitCTE(c CTE) interface{} {
return fmt.Sprintf("%s AS (%s) ", c.Name, AsString(c.SelectCommand))
}

// EscapeString escapes the given string so that it can be used in a SQL Clickhouse query.
// escapeString escapes the given string so that it can be used in a SQL Clickhouse query.
// It escapes ' and \ characters: ' -> \', \ -> \\.
func EscapeString(s string) string {
s = strings.ReplaceAll(s, `\`, `\\`)
if len(s) > 0 && s[0] == '\'' && s[len(s)-1] == '\'' {
func escapeString(s string) string {
s = strings.ReplaceAll(s, `\`, `\\`) // \ should be escaped with no exceptions
if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' {
// don't escape the first and last '
return util.SingleQuote(strings.ReplaceAll(s[1:len(s)-1], `'`, `\'`))
}
Expand Down
4 changes: 2 additions & 2 deletions quesma/testdata/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,10 +1014,10 @@ var TestsSearch = []SearchTestCase{
},
"track_total_hits": true
}`,
[]string{`("type"='task' AND "task.enabled" IN (true,54))`},
[]string{`("type"='task' AND "task.enabled" IN (true,54,'abc','abc\'s'))`},
model.ListAllFields,
[]string{
`SELECT "message" FROM ` + TableName + ` WHERE ("type"='task' AND "task.enabled" IN (true,54)) LIMIT 10`,
`SELECT "message" FROM ` + TableName + ` WHERE ("type"='task' AND "task.enabled" IN (true,54,'abc','abc\\'s')) LIMIT 10`,
`SELECT count(*) AS "column_0" FROM ` + TableName,
},
[]string{},
Expand Down

0 comments on commit 9f1b22f

Please sign in to comment.