Tencent/WeKnora · error

failed to retrieve: %s

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.

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, err

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Rewrite the derived table into a flat query (join the inner tables directly in the outer FROM).
  2. 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.
  3. 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

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


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/542175653f14c6da. Report an issue: GitHub.