T-135: M2.3b — libs/data/questdb query layer for QuestDB 10.2.0: LATEST ON + parameterized PGWire queries + contract DDL for bars_1m / fx_1m / bar_revisions (pure, no live QuestDB)

This commit is contained in:
Studio 2026-12-01 06:26:06 -05:00
parent 35d5de815b
commit a67d9c1644
5 changed files with 232 additions and 40 deletions

View file

@ -24,6 +24,8 @@ import type {
const INT32_MIN = -2147483648;
const INT32_MAX = 2147483647;
const CANONICAL_INT_RE = /^-?(?:0|[1-9]\d*)$/;
const ISO_UTC_OR_OFFSET_RE =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,6}))?(Z|([+-])(\d{2}):(\d{2}))$/;
const ALLOWED_PARTITIONS = new Set(['NONE', 'HOUR', 'DAY', 'MONTH', 'YEAR']);
function formatInt32ListingId(listingId: number): string {
@ -53,29 +55,67 @@ function toUtcIsoString(value: Date | string, label: string): string {
}
return value.toISOString();
}
if (typeof value === 'string' && value.trim().length > 0) {
const parsed = new Date(value);
if (!Number.isNaN(parsed.getTime())) {
return parsed.toISOString();
if (typeof value === 'string') {
const trimmed = value.trim();
const match = ISO_UTC_OR_OFFSET_RE.exec(trimmed);
if (!match) {
throw new Error(
`Invalid ${label} timestamp: expected ISO-8601 string with explicit UTC or offset timezone, received "${value}"`
);
}
const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
const hour = Number(match[4]);
const minute = Number(match[5]);
const second = Number(match[6]);
const frac = match[7];
const tz = match[8];
const baseUtcMs = Date.UTC(year, month - 1, day, hour, minute, second);
const check = new Date(baseUtcMs);
if (
check.getUTCFullYear() !== year ||
check.getUTCMonth() !== month - 1 ||
check.getUTCDate() !== day ||
check.getUTCHours() !== hour ||
check.getUTCMinutes() !== minute ||
check.getUTCSeconds() !== second
) {
throw new Error(`Invalid ${label} timestamp calendar date/time: "${value}"`);
}
if (tz === 'Z') {
return trimmed;
}
const sign = match[9] === '+' ? 1 : -1;
const offsetHours = Number(match[10]);
const offsetMinutes = Number(match[11]);
if (offsetHours > 23 || offsetMinutes > 59) {
throw new Error(`Invalid ${label} timestamp timezone offset: "${value}"`);
}
const adjustedUtcMs = baseUtcMs - sign * (offsetHours * 60 + offsetMinutes) * 60_000;
const utcDate = new Date(adjustedUtcMs);
const isoSeconds = utcDate.toISOString().slice(0, 19);
return frac ? `${isoSeconds}.${frac}Z` : `${isoSeconds}Z`;
}
throw new Error(`Invalid ${label} timestamp: "${String(value)}"`);
}
function parseBarTimestamp(raw: unknown, field: string): Date {
if (raw instanceof Date) {
if (Number.isNaN(raw.getTime())) {
throw new Error(`Invalid bars_1m ${field} timestamp: Date is NaN`);
}
return new Date(raw.getTime());
function parseUtcEpochMicros(raw: unknown, field: string): bigint {
if (typeof raw === 'bigint') {
return raw;
}
if (typeof raw === 'string' || typeof raw === 'number') {
const parsed = new Date(raw);
if (!Number.isNaN(parsed.getTime())) {
return parsed;
}
if (typeof raw === 'string' && CANONICAL_INT_RE.test(raw)) {
return BigInt(raw);
}
throw new Error(`Invalid bars_1m ${field} timestamp: "${String(raw)}"`);
if (typeof raw === 'number' && Number.isSafeInteger(raw)) {
return BigInt(raw);
}
throw new Error(`Invalid bars_1m ${field} UTC epoch microseconds: "${String(raw)}"`);
}
function parseRequiredFiniteNumber(raw: unknown, field: string): number {
@ -128,7 +168,7 @@ export async function readBars1m(
const toIso = toUtcIsoString(to, 'to');
const sql =
'SELECT listing_id, open, high, low, close, volume, vwap, session, source, known_at, ts ' +
'SELECT listing_id, open, high, low, close, volume, vwap, session, source, CAST(known_at AS LONG) AS known_at_us, CAST(ts AS LONG) AS ts_us ' +
'FROM bars_1m ' +
'WHERE listing_id = $1 AND ts >= $2 AND ts < $3 ' +
'ORDER BY ts ASC';
@ -145,8 +185,8 @@ export async function readBars1m(
vwap: parseNullableFiniteNumber(row.vwap, 'vwap'),
session: parseNullableString(row.session, 'session'),
source: parseNullableString(row.source, 'source'),
known_at: parseBarTimestamp(row.known_at, 'known_at'),
ts: parseBarTimestamp(row.ts, 'ts'),
known_at: parseUtcEpochMicros(row.known_at_us, 'known_at'),
ts: parseUtcEpochMicros(row.ts_us, 'ts'),
}));
}
@ -511,9 +551,7 @@ export class QuestDBClient {
* Get table information
*/
async getTableInfo(tableName: string): Promise<QueryResultRow[]> {
const result = await this.query(`SELECT * FROM table_columns WHERE table_name = $1`, [
tableName,
]);
const result = await this.query('SELECT * FROM table_columns($1)', [tableName]);
return result.rows;
}