fix: ORDER BY 1 returns oldest data when first col is symbol#1
Open
raphael2025 wants to merge 1 commit into
Open
fix: ORDER BY 1 returns oldest data when first col is symbol#1raphael2025 wants to merge 1 commit into
raphael2025 wants to merge 1 commit into
Conversation
agg_trades schema is (symbol, agg_id, price, qty, trade_time, ...).
ORDER BY 1 = ORDER BY symbol, BTC sorted first alphabetically, so
/v1/agg-trades?symbol=BTCUSDT always returned the very first BTC
trade (60h+ old) instead of the latest.
Fix: introduce TABLE_TIME_COL mapping (table -> time column) and
pass time_col=TABLE_TIME_COL[<table>] through _paginate. _resolve_order_col
validates the column exists in PRAGMA table_info (whitelist, no SQLi).
Affects every endpoint that called _paginate (25 endpoints):
agg-trades, trades, klines, mark-price-klines, depth/snapshots,
depth/updates, open-interest, funding-rates, liquidations, etc.
Also fixes the generic /v1/query/{table} endpoint.
Verified: after restart, /v1/agg-trades?symbol=BTCUSDT returns
trade_time ~1s ago; all 25 endpoints return fresh data ordered
by the correct timestamp column.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
/v1/agg-trades?symbol=BTCUSDT&limit=5was returning trades from2026-06-19(60h+ ago), even though the database has 4.4Magg_tradesrows with the newest written 2s earlier.Root cause
src/api.py:27:In SQLite,
ORDER BY 1means "order by the first selected column". For most tables the first column issymbol(orpair), so every paginated query was sorting alphabetically by symbol/pair, not by time. WithDESC, BTC sorted first alphabetically → always returns BTC's very first trade. The same bug affected every endpoint that went through_paginate(25 endpoints: agg-trades, trades, klines, mark-price-klines, depth/snapshots, depth/updates, open-interest, funding-rates, liquidations, ticker_*, book-ticker, …, and the generic/v1/query/{table}).Reproduction (before fix)
60h+ old. Same for every other symbol — returned its oldest trade.
Fix
TABLE_TIME_COL: dict[str, str]mapping every table to its canonical timestamp column (trade_time,event_time,open_time,snapshot_time,funding_time,delivery_time,gap_start_ms)._resolve_order_col(store, table, time_col)helper:time_colis passed, verify it exists inPRAGMA table_info(<table>)— return it. Otherwise 400. (whitelist, no SQL injection)time_colisNone, auto-detect by skippingsymbol/pairand picking the firstINTEGERcolumn._paginatenow acceptstime_col: str | Noneand uses it instead of literal1.src/api.pyto passtime_col=TABLE_TIME_COL[<table>].Test evidence (after fix, same restart of
src.main)All 25 endpoints now return fresh data ordered by the correct timestamp column.
total=counts are correct (agg_tradestotal jumped from2,110,285→2,111,479because new rows were ingested while this PR was being prepared, confirming the DB writer is unaffected).Files changed
src/api.py(+134 / −11)No DB migration needed. No changes to other files. No changes to the writer side.