-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No change of logic, just shuffling code around. I am stepping stone towards introducing schema. Motivation: - `IQueryTranslator` is bad abstraction as we see many methods not implemented. - obvious code repetitions `applySizeLimit` and `BuildNRowsQuery` - `makeBasicQuery` with EQL is very confusing, we just create one type of EQL query `ListByField`
- Loading branch information
Showing
8 changed files
with
164 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package query_util | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"mitmproxy/quesma/logger" | ||
"mitmproxy/quesma/model" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func IsNonAggregationQuery(queryInfo model.SearchQueryInfo, body []byte) bool { | ||
return ((queryInfo.Typ == model.ListByField || | ||
queryInfo.Typ == model.ListAllFields || | ||
queryInfo.Typ == model.Normal) && | ||
!bytes.Contains(body, []byte("aggs"))) || | ||
queryInfo.Typ == model.Facets || | ||
queryInfo.Typ == model.FacetsNumeric || | ||
queryInfo.Typ == model.CountAsync | ||
} | ||
|
||
func BuildNRowsQuery(ctx context.Context, tableName string, fieldName string, query model.SimpleQuery, limit int) *model.Query { | ||
suffixClauses := make([]string, 0) | ||
if len(query.SortFields) > 0 { | ||
suffixClauses = append(suffixClauses, "ORDER BY "+AsQueryString(query.SortFields)) | ||
} | ||
if limit > 0 { | ||
suffixClauses = append(suffixClauses, "LIMIT "+strconv.Itoa(applySizeLimit(ctx, limit))) | ||
} | ||
return &model.Query{ | ||
Fields: []string{fieldName}, | ||
WhereClause: query.Sql.Stmt, | ||
SuffixClauses: suffixClauses, | ||
FromClause: tableName, | ||
CanParse: true, | ||
} | ||
} | ||
|
||
func AsQueryString(sortFields []model.SortField) string { | ||
if len(sortFields) == 0 { | ||
return "" | ||
} | ||
sortStrings := make([]string, 0, len(sortFields)) | ||
for _, sortField := range sortFields { | ||
query := strings.Builder{} | ||
query.WriteString(strconv.Quote(sortField.Field)) | ||
if sortField.Desc { | ||
query.WriteString(" desc") | ||
} | ||
sortStrings = append(sortStrings, query.String()) | ||
} | ||
return strings.Join(sortStrings, ", ") | ||
} | ||
|
||
func applySizeLimit(ctx context.Context, size int) int { | ||
// FIXME hard limit here to prevent OOM | ||
const quesmaMaxSize = 10000 | ||
if size > quesmaMaxSize { | ||
logger.WarnWithCtx(ctx).Msgf("setting hits size to=%d, got=%d", quesmaMaxSize, size) | ||
size = quesmaMaxSize | ||
} | ||
return size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.