Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit f2e03fe

Browse files
authored
Rearrange query.go and remove dead code (#209)
Removed some dead code, comments, and grouped related things together
1 parent dc8c289 commit f2e03fe

File tree

3 files changed

+93
-111
lines changed

3 files changed

+93
-111
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package model
2+
3+
// PipelineQueryType is an interface for pipeline aggregations
4+
// It's an extension to QueryType interface
5+
// Adds a method to calculate result rows from its parent aggregation
6+
type PipelineQueryType interface {
7+
// TranslateSqlResponseToJson 'level' - we want to translate [level:] (metrics aggr) or [level-1:] (bucket aggr) columns to JSON
8+
// Previous columns are used for bucketing.
9+
// For 'bucket' aggregation result is a slice of buckets, for 'metrics' aggregation it's a single bucket (only look at [0])
10+
TranslateSqlResponseToJson(rows []QueryResultRow, level int) []JsonMap
11+
12+
// IsBucketAggregation if true, result from 'MakeResponse' will be a slice of buckets
13+
// if false, it's a metrics aggregation and result from 'MakeResponse' will be a single bucket
14+
IsBucketAggregation() bool
15+
16+
// CalculateResultWhenMissing calculates the result of this aggregation when it's a NoDBQuery
17+
// (we don't query the DB for the results, but calculate them from the parent aggregation)
18+
CalculateResultWhenMissing(query *Query, parentRows []QueryResultRow) []QueryResultRow
19+
20+
String() string
21+
}

quesma/model/query.go

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,55 @@ import (
88
"strings"
99
)
1010

11-
const RowNumberColumnName = "row_number"
12-
const EmptyFieldSelection = "''" // we can query SELECT '', that's why such quotes
11+
const (
12+
RowNumberColumnName = "row_number"
13+
EmptyFieldSelection = "''" // we can query SELECT '', that's why such quotes
14+
)
1315

1416
type (
17+
Query struct {
18+
IsDistinct bool // true <=> query is SELECT DISTINCT
19+
Fields []string // Fields in 'SELECT Fields FROM ...'
20+
NonSchemaFields []string // Fields that are not in schema, but are in 'SELECT ...', e.g. count()
21+
WhereClause string // "WHERE ..." until next clause like GROUP BY/ORDER BY, etc.
22+
GroupByFields []string // if not empty, we do GROUP BY GroupByFields... They are quoted if they are column names, unquoted if non-schema. So no quotes need to be added.
23+
SuffixClauses []string // ORDER BY, etc.
24+
FromClause string // usually just "tableName", or databaseName."tableName". Sometimes a subquery e.g. (SELECT ...)
25+
CanParse bool // true <=> query is valid
26+
QueryInfo SearchQueryInfo
27+
Highlighter Highlighter
28+
NoDBQuery bool // true <=> we don't need query to DB here, true in some pipeline aggregations
29+
Parent string // parent aggregation name, used in some pipeline aggregations
30+
Aggregators []Aggregator // keeps names of aggregators, e.g. "0", "1", "2", "suggestions". Needed for JSON response.
31+
Type QueryType
32+
SortFields SortFields // fields to sort by
33+
SubSelect string
34+
// dictionary to add as 'meta' field in the response.
35+
// WARNING: it's probably not passed everywhere where it's needed, just in one place.
36+
// But it works for the test + our dashboards, so let's fix it later if necessary.
37+
// NoMetadataField (nil) is a valid option and means no meta field in the response.
38+
Metadata JsonMap
39+
}
40+
QueryType interface {
41+
// TranslateSqlResponseToJson 'level' - we want to translate [level:] (metrics aggr) or [level-1:] (bucket aggr) columns to JSON
42+
// Previous columns are used for bucketing.
43+
// For 'bucket' aggregation result is a slice of buckets, for 'metrics' aggregation it's a single bucket (only look at [0])
44+
TranslateSqlResponseToJson(rows []QueryResultRow, level int) []JsonMap
45+
46+
PostprocessResults(rowsFromDB []QueryResultRow) (ultimateRows []QueryResultRow)
47+
48+
// IsBucketAggregation if true, result from 'MakeResponse' will be a slice of buckets
49+
// if false, it's a metrics aggregation and result from 'MakeResponse' will be a single bucket
50+
IsBucketAggregation() bool
51+
String() string
52+
}
53+
Highlighter struct {
54+
Tokens []string
55+
Fields map[string]bool
56+
57+
PreTags []string
58+
PostTags []string
59+
}
1560
SortFields []SortField
1661
SortField struct {
1762
Field string
@@ -27,52 +72,6 @@ func (sf SortFields) Properties() []string {
2772
return properties
2873
}
2974

30-
type Highlighter struct {
31-
Tokens []string
32-
Fields map[string]bool
33-
34-
PreTags []string
35-
PostTags []string
36-
}
37-
38-
// implements String() (now) and MakeResponse() interface (in the future (?))
39-
type Query struct {
40-
IsDistinct bool // true <=> query is SELECT DISTINCT
41-
Fields []string // Fields in 'SELECT Fields FROM ...'
42-
NonSchemaFields []string // Fields that are not in schema, but are in 'SELECT ...', e.g. count()
43-
WhereClause string // "WHERE ..." until next clause like GROUP BY/ORDER BY, etc.
44-
GroupByFields []string // if not empty, we do GROUP BY GroupByFields... They are quoted if they are column names, unquoted if non-schema. So no quotes need to be added.
45-
SuffixClauses []string // ORDER BY, etc.
46-
FromClause string // usually just "tableName", or databaseName."tableName". Sometimes a subquery e.g. (SELECT ...)
47-
CanParse bool // true <=> query is valid
48-
QueryInfo SearchQueryInfo
49-
Highlighter Highlighter
50-
NoDBQuery bool // true <=> we don't need query to DB here, true in some pipeline aggregations
51-
Parent string // parent aggregation name, used in some pipeline aggregations
52-
Aggregators []Aggregator // keeps names of aggregators, e.g. "0", "1", "2", "suggestions". Needed for JSON response.
53-
Type QueryType
54-
SortFields SortFields // fields to sort by
55-
SubSelect string
56-
// dictionary to add as 'meta' field in the response.
57-
// WARNING: it's probably not passed everywhere where it's needed, just in one place.
58-
// But it works for the test + our dashboards, so let's fix it later if necessary.
59-
// NoMetadataField (nil) is a valid option and means no meta field in the response.
60-
Metadata JsonMap
61-
}
62-
63-
/*
64-
type subQuery struct {
65-
sql string
66-
innerJoin string
67-
name string
68-
}
69-
70-
func newSubQuery(sql, innerJoin, name string) subQuery {
71-
return subQuery{sql: sql, innerJoin: innerJoin, name: name}
72-
}
73-
erase if still not used after 'respect size in aggregations' issue
74-
*/
75-
7675
var NoMetadataField JsonMap = nil
7776

7877
// returns string with SQL query
@@ -353,5 +352,30 @@ func (h *Highlighter) SetTokens(tokens []string) {
353352
sort.Slice(h.Tokens, func(i, j int) bool {
354353
return len(h.Tokens[i]) > len(h.Tokens[j])
355354
})
355+
}
356+
357+
// UnknownAggregationType is a placeholder for an aggregation type that'll be determined in the future,
358+
// after descending further into the aggregation tree
359+
type UnknownAggregationType struct {
360+
ctx context.Context
361+
}
362+
363+
func NewUnknownAggregationType(ctx context.Context) UnknownAggregationType {
364+
return UnknownAggregationType{ctx: ctx}
365+
}
366+
367+
func (query UnknownAggregationType) IsBucketAggregation() bool {
368+
return false
369+
}
370+
371+
func (query UnknownAggregationType) TranslateSqlResponseToJson(rows []QueryResultRow, level int) []JsonMap {
372+
return make([]JsonMap, 0)
373+
}
374+
375+
func (query UnknownAggregationType) String() string {
376+
return "unknown aggregation type"
377+
}
356378

379+
func (query UnknownAggregationType) PostprocessResults(rowsFromDB []QueryResultRow) []QueryResultRow {
380+
return rowsFromDB
357381
}

quesma/model/query_type.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)