Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek committed Dec 28, 2024
1 parent d27823c commit c564b0c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
72 changes: 18 additions & 54 deletions quesma/model/bucket_aggregations/terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package bucket_aggregations

import (
"context"
"fmt"
"quesma/logger"
"quesma/model"
"quesma/util"
Expand Down Expand Up @@ -33,65 +32,30 @@ func (query Terms) TranslateSqlResponseToJson(rows []model.QueryResultRow) model
return model.JsonMap{}
}

fmt.Println(rows)

var keyIsBool bool
var response []model.JsonMap
for _, row := range rows {
key := query.key(row)
if key == nil {
continue
docCount := query.docCount(row)
bucket := model.JsonMap{
"doc_count": docCount,
}

switch query.key(row).(type) {
case bool, *bool:
keyIsBool = true
default:
keyIsBool = false
if query.significant {
bucket["score"] = docCount
bucket["bg_count"] = docCount
}
break
}

var response []model.JsonMap
if !keyIsBool {
for _, row := range rows {
docCount := query.docCount(row)
bucket := model.JsonMap{
"key": query.key(row),
"doc_count": docCount,
}
if query.significant {
bucket["score"] = docCount
bucket["bg_count"] = docCount
}
response = append(response, bucket)
// response for bool keys is different
key := query.key(row)
if boolPtr, isBoolPtr := key.(*bool); isBoolPtr {
key = *boolPtr
}
} else { // key is bool
for _, row := range rows {
var (
key any
keyAsString string
)
if val, err := util.ExtractBool(query.key(row)); err == nil {
if val {
key = 1
keyAsString = "true"
} else {
key = 0
keyAsString = "false"
}
}
docCount := query.docCount(row)
bucket := model.JsonMap{
"key": key,
"key_as_string": keyAsString,
"doc_count": docCount,
}
if query.significant {
bucket["score"] = docCount
bucket["bg_count"] = docCount
}
response = append(response, bucket)
if keyAsBool, ok := key.(bool); ok {
bucket["key"] = util.BoolToInt(keyAsBool)
bucket["key_as_string"] = util.BoolToString(keyAsBool)
} else {
bucket["key"] = key
}

response = append(response, bucket)
}

if !query.significant {
Expand Down
4 changes: 0 additions & 4 deletions quesma/queryparser/pancake_sql_query_generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ func TestPancakeQueryGeneration(t *testing.T) {
t.Skip("Fix filters")
}

if i != 104 {
//t.Skip()
}

if test.TestName == "Line, Y-axis: Min, Buckets: Date Range, X-Axis: Terms, Split Chart: Date Histogram(file:kibana-visualize/agg_req,nr:9)" {
t.Skip("Date range is broken, fix in progress (PR #971)")
}
Expand Down
14 changes: 14 additions & 0 deletions quesma/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,20 @@ func ExtractBool(value any) (bool, error) {
return false, fmt.Errorf("ExtractBool, value of incorrect type. Expected (*)bool, received: %v; type: %T", value, value)
}

func BoolToInt(b bool) int {
if b {
return 1
}
return 0
}

func BoolToString(b bool) string {
if b {
return "true"
}
return "false"
}

type sqlMockMismatchSql struct {
expected string
actual string
Expand Down

0 comments on commit c564b0c

Please sign in to comment.