Skip to content

[FEATURE] SQL functions in WHERE clause not evaluated for required parameters (start_time), and projected parameter/response columns return null #686

Closed

Description

@jeffreyaven

Summary

Two related issues observed querying openai_admin.usage.completions.

1. Function expressions in the WHERE clause are not evaluated for required parameters

When a required parameter is supplied via a function expression rather than a literal, the value is not resolved and the parameter is dropped from the request, producing a 400 from the provider.

SELECT
  json_extract(r.value, '$.project_id')    AS project_id,
  json_extract(r.value, '$.model')         AS model,
  date(u.start_time, 'unixepoch')          AS usage_date,
  json_extract(r.value, '$.input_tokens')  AS input_tokens,
  json_extract(r.value, '$.output_tokens') AS output_tokens
FROM openai_admin.usage.completions u, json_each(u.results) r
WHERE u.start_time = strftime('%s', date('now', '-30 days'))
  AND u.bucket_width = '1d'
  AND u.group_by = 'project_id'
ORDER BY usage_date, project_id;

Result:

http response status code: 400, response body: {
  "error": {
    "message": "Missing required parameter: 'start_time'.",
    "type": "invalid_request_error",
    "param": "start_time",
    "code": "missing_required_parameter"
  }
}

The expression itself evaluates fine standalone:

select strftime('%s', date('now', '-30 days'));
-- 1781481600

The same query with the literal substituted does dispatch the request:

WHERE u.start_time = 1781481600

Expected: scalar function expressions on the right hand side of a required parameter predicate are evaluated before request construction, and the resolved value is bound to the parameter.

2. Projected columns return null / zero when the request does succeed

With the literal form, the request succeeds but the projection is largely empty:

|-------------------------------|-------|------------|--------------|---------------|
|          project_id           | model | usage_date | input_tokens | output_tokens |
|-------------------------------|-------|------------|--------------|---------------|
| proj_RBWsjunhtdHqTO3MgxytkuB5 | null  |          0 | null         | null          |
|-------------------------------|-------|------------|--------------|---------------|

Observations:

  • project_id resolves via json_extract(r.value, '$.project_id'), so json_each(u.results) is expanding correctly.
  • input_tokens and output_tokens are null despite being siblings of project_id in the same result object.
  • usage_date is 0, indicating u.start_time is not projectable in the response row even though it was supplied as a parameter. Parameter values should be echoed into the row so they can be projected and joined against.
  • model may legitimately be null given group_by = 'project_id', but should be confirmed.

Environment

  • Provider: openai_admin
  • Resource: openai_admin.usage.completions
  • stackql version:
  • Provider version:

Suspected areas

  • Parameter binding path: literal-only extraction from the WHERE clause, function expressions ignored rather than evaluated or rejected.
  • Response projection: parameter columns not merged into the emitted row; possible schema mismatch on the nested results[].result[] shape for the usage endpoints.

Acceptance criteria

  • WHERE start_time = strftime('%s', date('now', '-30 days')) dispatches a request with the resolved epoch value.
  • If an expression cannot be evaluated at plan time, stackql returns a clear error rather than silently dropping the parameter.
  • Projecting u.start_time returns the supplied parameter value.
  • input_tokens and output_tokens return values from the response payload.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions