Problem · Database
Debug Fair-Value Lookup
Problem statement
The provided Pandas starter attempts an as-of fair-value lookup, but it mishandles timestamp order, exact-time observations, invalid prices, and the staleness boundary. Fix fair_value_at so it returns a one-row DataFrame with the column price.
Lookup rules
- Read the single row in
query_paramsand convert all timestamps before comparing them. - Consider only price rows whose
timestampis at or beforequery_time. - Starting with the most recent eligible row, walk backward until the requested asset has a non-null, strictly positive price.
- If no valid observation exists, return
NULL. - Otherwise, calculate the candidate's age from its own timestamp. Return
NULLonly when that age is strictly greater thanmax_staleness_minutes; an observation exactly on the boundary is accepted.
Table schema
Pandas
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
prices
Price tape with one timestamp and three asset columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| timestampPK | Timestamp | No | — |
| Asset_1 | Decimal | Yes | — |
| Asset_2 | Decimal | Yes | — |
| Asset_3 | Decimal | Yes | — |
query_params
The one requested asset, query time, and allowed staleness.
| Column | Type | Nullable | Description |
|---|---|---|---|
| asset | Text | No | — |
| query_time | Timestamp | No | — |
| max_staleness_minutes | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| price | Decimal | Yes | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- The
pricestable contains at least1row and at most2000rows. - Price timestamps are unique but may arrive out of order.
- The
query_paramstable contains exactly one row. - The requested
assetis exactlyAsset_1,Asset_2, orAsset_3. max_staleness_minutesis a nonnegative integer.- Return exactly one row; its
pricemay beNULL.