Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ids query DSL #1135

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion quesma/logger/log_with_throttling.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

var throttleMap = util.SyncMap[string, time.Time]{}
var throttleMap = util.NewSyncMap[string, time.Time]()

const throttleDuration = 30 * time.Minute

Expand All @@ -25,3 +25,14 @@ func WarnWithCtxAndThrottling(ctx context.Context, aggrName, paramName, format s
throttleMap.Store(mapKey, time.Now())
}
}

// 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)
weThrottle := ok && time.Since(timestamp) < throttleDuration
if !weThrottle {
Warn().Msgf(format, v...)
throttleMap.Store(warnName, time.Now())
}
}
9 changes: 9 additions & 0 deletions quesma/model/base_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package model
type BaseExprVisitor struct {
OverrideVisitFunction func(b *BaseExprVisitor, e FunctionExpr) interface{}
OverrideVisitLiteral func(b *BaseExprVisitor, l LiteralExpr) interface{}
OverrideVisitTuple func(b *BaseExprVisitor, t TupleExpr) interface{}
OverrideVisitInfix func(b *BaseExprVisitor, e InfixExpr) interface{}
OverrideVisitColumnRef func(b *BaseExprVisitor, e ColumnRef) interface{}
OverrideVisitPrefixExpr func(b *BaseExprVisitor, e PrefixExpr) interface{}
Expand Down Expand Up @@ -43,6 +44,14 @@ func (v *BaseExprVisitor) VisitLiteral(e LiteralExpr) interface{} {

return NewLiteral(e.Value)
}

func (v *BaseExprVisitor) VisitTuple(e TupleExpr) interface{} {
if v.OverrideVisitTuple != nil {
return v.OverrideVisitTuple(v, e)
}
return NewTupleExpr(v.VisitChildren(e.Exprs)...)
}

func (v *BaseExprVisitor) VisitInfix(e InfixExpr) interface{} {
if v.OverrideVisitInfix != nil {
return v.OverrideVisitInfix(v, e)
Expand Down
14 changes: 14 additions & 0 deletions quesma/model/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Expr interface {
var (
InvalidExpr = Expr(nil)
TrueExpr = NewLiteral(true)
FalseExpr = NewLiteral(false)
)

// ColumnRef is a reference to a column in a table, we can enrich it with more information (e.g. type used) as we go
Expand Down Expand Up @@ -86,6 +87,18 @@ func (e LiteralExpr) Accept(v ExprVisitor) interface{} {
return v.VisitLiteral(e)
}

type TupleExpr struct {
Exprs []Expr
}

func NewTupleExpr(exprs ...Expr) TupleExpr {
return TupleExpr{Exprs: exprs}
}

func (e TupleExpr) Accept(v ExprVisitor) interface{} {
return v.VisitTuple(e)
}

type InfixExpr struct {
Left Expr
Op string
Expand Down Expand Up @@ -278,6 +291,7 @@ func (e CTE) Accept(v ExprVisitor) interface{} {
type ExprVisitor interface {
VisitFunction(e FunctionExpr) interface{}
VisitLiteral(l LiteralExpr) interface{}
VisitTuple(t TupleExpr) interface{}
VisitInfix(e InfixExpr) interface{}
VisitColumnRef(e ColumnRef) interface{}
VisitPrefixExpr(e PrefixExpr) interface{}
Expand Down
17 changes: 17 additions & 0 deletions quesma/model/expr_string_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package model

import (
"fmt"
"quesma/logger"
"quesma/quesma/types"
"regexp"
"sort"
Expand Down Expand Up @@ -68,6 +69,22 @@ func (v *renderer) VisitLiteral(l LiteralExpr) interface{} {
return fmt.Sprintf("%v", l.Value)
}

func (v *renderer) VisitTuple(t TupleExpr) interface{} {
switch len(t.Exprs) {
case 0:
logger.WarnWithThrottling("VisitTuple", "TupleExpr with no expressions")
return "()"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be consistent, consider returning tuple()

case 1:
return t.Exprs[0].Accept(v)
default:
args := make([]string, len(t.Exprs))
for i, arg := range t.Exprs {
args[i] = arg.Accept(v).(string)
}
return fmt.Sprintf("tuple(%s)", strings.Join(args, ", ")) // can omit "tuple", but I think SQL's more readable with it
}
}

func (v *renderer) VisitInfix(e InfixExpr) interface{} {
var lhs, rhs interface{} // TODO FOR NOW LITTLE PARANOID BUT HELPS ME NOT SEE MANY PANICS WHEN TESTING
if e.Left != nil {
Expand Down
4 changes: 4 additions & 0 deletions quesma/model/simple_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func NewSimpleQuery(whereClause Expr, canParse bool) SimpleQuery {
return SimpleQuery{WhereClause: whereClause, CanParse: canParse}
}

func NewSimpleQueryInvalid() SimpleQuery {
return SimpleQuery{CanParse: false}
}

// LimitForCount returns (limit, true) if we need count(*) with limit,
// (not-important, false) if we don't need count/limit
func (s *SimpleQuery) LimitForCount() (limit int, doWeNeedLimit bool) {
Expand Down
Loading
Loading