Skip to content

Commit

Permalink
Unify hits and hitsPresent #163
Browse files Browse the repository at this point in the history
- `BuildSimpleSelectQuery` is unused, but tested. We should run
`BuildNRowsQuery` instead
- Unify `hits` and `hitsPresent` logic
  • Loading branch information
jakozaur authored May 20, 2024
1 parent 602571b commit 81e478d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
6 changes: 3 additions & 3 deletions quesma/queryparser/query_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestQueryParserStringAttrConfig(t *testing.T) {
assert.True(t, simpleQuery.CanParse, "can parse")
assert.Contains(t, tt.WantedSql, simpleQuery.Sql.Stmt, "contains wanted sql")
assert.Equal(t, tt.WantedQueryType, queryInfo.Typ, "equals to wanted query type")
query := cw.BuildSimpleSelectQuery(simpleQuery.Sql.Stmt, model.DefaultSizeListQuery)
query := cw.BuildNRowsQuery("*", simpleQuery, model.DefaultSizeListQuery)
assert.Contains(t, tt.WantedQuery, *query)
})
}
Expand All @@ -79,7 +79,7 @@ func TestQueryParserNoFullTextFields(t *testing.T) {
assert.True(t, simpleQuery.CanParse, "can parse")
assert.Contains(t, tt.WantedSql, simpleQuery.Sql.Stmt, "contains wanted sql")
assert.Equal(t, tt.WantedQueryType, queryInfo.Typ, "equals to wanted query type")
query := cw.BuildSimpleSelectQuery(simpleQuery.Sql.Stmt, model.DefaultSizeListQuery)
query := cw.BuildNRowsQuery("*", simpleQuery, model.DefaultSizeListQuery)
assert.Contains(t, tt.WantedQuery, *query)
})
}
Expand All @@ -105,7 +105,7 @@ func TestQueryParserNoAttrsConfig(t *testing.T) {
assert.Contains(t, tt.WantedSql, simpleQuery.Sql.Stmt)
assert.Equal(t, tt.WantedQueryType, queryInfo.Typ)

query := cw.BuildSimpleSelectQuery(simpleQuery.Sql.Stmt, model.DefaultSizeListQuery)
query := cw.BuildNRowsQuery("*", simpleQuery, model.DefaultSizeListQuery)
assert.Contains(t, tt.WantedQuery, *query)
})
}
Expand Down
21 changes: 5 additions & 16 deletions quesma/queryparser/query_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,6 @@ func (cw *ClickhouseQueryTranslator) BuildSelectQuery(fields []string, whereClau
}
}

func (cw *ClickhouseQueryTranslator) BuildSimpleSelectQuery(whereClause string, limit int) *model.Query {
return &model.Query{
Fields: []string{"*"},
WhereClause: whereClause,
FromClause: cw.Table.FullTableName(),
SuffixClauses: []string{"LIMIT " + strconv.Itoa(cw.applySizeLimit(limit))},
CanParse: true,
}
}

func (cw *ClickhouseQueryTranslator) BuildSimpleCountQuery(whereClause string) *model.Query {
return &model.Query{
NonSchemaFields: []string{"count()"},
Expand Down Expand Up @@ -592,12 +582,11 @@ func (cw *ClickhouseQueryTranslator) BuildNRowsQuery(fieldName string, query Sim
suffixClauses = append(suffixClauses, "LIMIT "+strconv.Itoa(cw.applySizeLimit(limit)))
}
return &model.Query{
Fields: []string{fieldName},
NonSchemaFields: []string{},
WhereClause: query.Sql.Stmt,
SuffixClauses: suffixClauses,
FromClause: cw.Table.FullTableName(),
CanParse: true,
Fields: []string{fieldName},
WhereClause: query.Sql.Stmt,
SuffixClauses: suffixClauses,
FromClause: cw.Table.FullTableName(),
CanParse: true,
}
}

Expand Down
2 changes: 1 addition & 1 deletion quesma/queryparser/query_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func TestMakeResponseSearchQueryIsProperJson(t *testing.T) {
cw := ClickhouseQueryTranslator{ClickhouseLM: nil, Table: clickhouse.NewEmptyTable("@"), Ctx: context.Background()}
const limit = 1000
queries := []*model.Query{
cw.BuildSimpleSelectQuery("", limit),
cw.BuildNRowsQuery("*", SimpleQuery{}, limit),
cw.BuildNRowsQuery("@", SimpleQuery{}, 0),
}
for _, query := range queries {
Expand Down
17 changes: 3 additions & 14 deletions quesma/quesma/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin
}

var hits []model.QueryResultRow
var hitsPresent *[]model.QueryResultRow
var aggregationResults [][]model.QueryResultRow
oldHandlingUsed := false
newAggregationHandlingUsed := false
Expand Down Expand Up @@ -270,8 +269,7 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin
translatedQueryBody, aggregationResults = q.searchWorker(ctx, aggregations, columns, table, true, nil)
if queryInfo.Size > 0 {
listQuery := queryTranslator.BuildNRowsQuery("*", simpleQuery, queryInfo.Size)
hitsFallback, err := q.logManager.ProcessQuery(ctx, table, listQuery, nil)
hitsPresent = &hitsFallback
hits, err = q.logManager.ProcessQuery(ctx, table, listQuery, nil)
translatedQueryBody = append(translatedQueryBody, []byte("\n"+listQuery.String()+"\n")...)
if err != nil {
logger.ErrorWithCtx(ctx).Msgf("error processing fallback query. Err: %v, query: %+v", err, listQuery)
Expand All @@ -295,24 +293,15 @@ func (q *QueryRunner) handleSearchCommon(ctx context.Context, indexPattern strin
response, err = queryTranslator.MakeSearchResponse(hits, model.Query{QueryInfo: queryInfo, Highlighter: highlighter})
} else if newAggregationHandlingUsed {
response = queryTranslator.MakeResponseAggregation(aggregations, aggregationResults)
responseHits, err = queryTranslator.MakeSearchResponse(hits, model.Query{QueryInfo: queryInfo, Highlighter: highlighter})
response.Hits = responseHits.Hits
}
if err != nil {
logger.ErrorWithCtx(ctx).Msgf("error making response: %v, queryInfo: %+v, rows: %v", err, queryInfo, hits)
pushSecondaryInfo(q.quesmaManagementConsole, id, path, body, translatedQueryBody, responseBody, startTime)
return responseBody, err
}

if hitsPresent != nil {
if response == nil {
response, err = queryTranslator.MakeSearchResponse(*hitsPresent, model.Query{QueryInfo: queryInfo, Highlighter: highlighter})
} else {
responseHits, err = queryTranslator.MakeSearchResponse(*hitsPresent, model.Query{QueryInfo: queryInfo, Highlighter: highlighter})
response.Hits = responseHits.Hits
}
}
if err != nil {
logger.ErrorWithCtx(ctx).Msgf("error making response: %v, queryInfo: %v, rows: %v", err, queryInfo, hitsPresent)
}
responseBody, err = response.Marshal()

pushSecondaryInfo(q.quesmaManagementConsole, id, path, body, translatedQueryBody, responseBody, startTime)
Expand Down
2 changes: 1 addition & 1 deletion quesma/quesma/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestNoAsciiTableName(t *testing.T) {
assert.Equal(t, "", simpleQuery.Sql.Stmt)
assert.Equal(t, model.Normal, queryInfo.Typ)
const Limit = 1000
query := queryTranslator.BuildSimpleSelectQuery(simpleQuery.Sql.Stmt, Limit)
query := queryTranslator.BuildNRowsQuery("*", simpleQuery, Limit)
assert.True(t, query.CanParse)
assert.Equal(t, fmt.Sprintf(`SELECT * FROM "%s" LIMIT %d`, tableName, Limit), query.String())
}
Expand Down

0 comments on commit 81e478d

Please sign in to comment.