Tencent/WeKnora · error
failed to retrieve
Error message
failed to retrieve: %s
What it means
A RangeSubselect (a parenthesized subquery used as a FROM item, e.g. `FROM (SELECT ...) x`) was encountered. When checkSubqueries is enabled the validator rejects such FROM subqueries outright, because they can hide arbitrary constructs (dangerous functions, more subqueries) inside an opaque derived table.
Solutions
- Rewrite the derived table into a flat query (join the inner tables directly in the outer FROM).
- Flatten aggregates: replace `FROM (SELECT dept, MAX(s) m FROM emp GROUP BY dept) d` with a CTE-free join or HAVING on the outer query where possible.
- If subqueries are required and the input is trusted, disable checkSubqueries in the validator configuration so the subquery is recursed into and validated instead of rejected.
Example fix
// before SELECT * FROM (SELECT id, name FROM users WHERE active = true) t; // after SELECT id, name FROM users WHERE active = true;
Defensive patterns
Strategy: validation
Validate before calling
var fromSubselect = regexp.MustCompile(`(?i)\bfrom\s*\(`)
if fromSubselect.MatchString(sql) {
// flatten the derived table before validating/executing
} Try / catch
if err := validator.ValidateQuery(sql); err != nil {
if strings.Contains(err.Error(), "subqueries in FROM clause") {
return fmt.Errorf("rewrite query without derived tables: %w", err)
}
} Prevention
- Write flat queries with direct joins instead of inline views.
- Configure ORMs to avoid subquery-based pagination wrappers where a JOIN/LIMIT form exists.
- Enable checkSubqueries whenever input is not fully trusted so this class is rejected early.
When it happens
Trigger: Validating `SELECT * FROM (SELECT id FROM users) t` or `SELECT ... FROM (SELECT * FROM read_text('/etc/passwd')) x` while subquery checking is enabled.
Common situations: Queries written with derived tables/inline views, which is idiomatic SQL; ORM-generated paginated queries using `FROM (SELECT ... LIMIT ...) sub`; refactored queries that wrapped an original flat SELECT in a subquery.
Related errors
- 2201
- join request not found
- opensearch: authentication failed
- opensearch: index not found
- opensearch: invalid index config
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/542175653f14c6da.
Report an issue: GitHub.
Appendix: source
Thrown at internal/application/repository/retriever/elasticsearch/v7/repository.go:789
results, err := e.processSearchResponse(ctx, response, typesLocal.KeywordsRetrieverType)
if err != nil {
return nil, err
}
return results, nil
}
// processSearchResponse Process search response
func (e *elasticsearchRepository) processSearchResponse(ctx context.Context,
response *esapi.Response, retrieverType typesLocal.RetrieverType,
) ([]*typesLocal.IndexWithScore, error) {
log := logger.GetLogger(ctx)
if response.IsError() {
errMsg := fmt.Sprintf("failed to retrieve: %s", response.String())
log.Errorf("[ElasticsearchV7] %s", errMsg)
return nil, errors.New(errMsg)
}
// Decode response body
rJson, err := e.decodeSearchResponse(ctx, response)
if err != nil {
return nil, err
}
// Extract hits from response
hitsList, err := e.extractHitsFromResponse(ctx, rJson)
if err != nil {
return nil, err
}
// Process hits into results
results, err := e.processHits(ctx, hitsList, retrieverType)
if err != nil {
return nil, errView on GitHub (pinned to 988cbb0330)