Skip to content

Commit

Permalink
chore(embedded/document): count with limit in subquery
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <[email protected]>
  • Loading branch information
jeroiraz committed Oct 13, 2023
1 parent 420d631 commit b5c6917
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions embedded/document/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func (e *Engine) ReplaceDocuments(ctx context.Context, username string, query *p

queryStmt := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, documentIdFieldName)},
query.CollectionName,
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
sql.NewInteger(int64(query.Limit)),
Expand Down Expand Up @@ -983,7 +983,7 @@ func (e *Engine) GetDocuments(ctx context.Context, query *protomodel.Query, offs

op := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, DocumentBLOBField)},
query.CollectionName,
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
sql.NewInteger(int64(query.Limit)),
Expand Down Expand Up @@ -1022,15 +1022,24 @@ func (e *Engine) CountDocuments(ctx context.Context, query *protomodel.Query, of
return 0, err
}

op := sql.NewSelectStmt(
[]sql.Selector{sql.NewAggColSelector(sql.COUNT, query.CollectionName, "*")},
query.CollectionName,
ds := sql.NewSelectStmt(
[]sql.Selector{sql.NewColSelector(query.CollectionName, table.Cols()[0].Name())},
sql.NewTableRef(query.CollectionName, ""),
queryCondition,
generateSQLOrderByClauses(table, query.OrderBy),
sql.NewInteger(int64(query.Limit)),
sql.NewInteger(offset),
)

op := sql.NewSelectStmt(
[]sql.Selector{sql.NewAggColSelector(sql.COUNT, query.CollectionName, "*")},
ds,
nil,
nil,
nil,
nil,
)

r, err := e.sqlEngine.QueryPreparedStmt(ctx, sqlTx, op, nil)
if err != nil {
return 0, err
Expand Down

0 comments on commit b5c6917

Please sign in to comment.