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:
parent
35d5de815b
commit
a67d9c1644
5 changed files with 232 additions and 40 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ export function rebaseWhereCondition(
|
|||
if (ch === "'") {
|
||||
out += ch;
|
||||
i += 1;
|
||||
let closed = false;
|
||||
while (i < condition.length) {
|
||||
const cur = condition.charAt(i);
|
||||
out += cur;
|
||||
|
|
@ -101,10 +102,14 @@ export function rebaseWhereCondition(
|
|||
out += "'";
|
||||
i += 1;
|
||||
} else {
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!closed) {
|
||||
throw new Error('Unterminated string literal in WHERE condition');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -112,6 +117,7 @@ export function rebaseWhereCondition(
|
|||
if (ch === '"') {
|
||||
out += ch;
|
||||
i += 1;
|
||||
let closed = false;
|
||||
while (i < condition.length) {
|
||||
const cur = condition.charAt(i);
|
||||
out += cur;
|
||||
|
|
@ -121,25 +127,34 @@ export function rebaseWhereCondition(
|
|||
out += '"';
|
||||
i += 1;
|
||||
} else {
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!closed) {
|
||||
throw new Error('Unterminated quoted identifier in WHERE condition');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Line comment (-- ... \n)
|
||||
// Line comment (-- ... \n); terminate at EOF so subsequent clauses are never swallowed
|
||||
if (ch === '-' && next === '-') {
|
||||
out += '--';
|
||||
i += 2;
|
||||
let sawNewline = false;
|
||||
while (i < condition.length) {
|
||||
const cur = condition.charAt(i);
|
||||
out += cur;
|
||||
i += 1;
|
||||
if (cur === '\n') {
|
||||
sawNewline = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!sawNewline) {
|
||||
out += '\n';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -147,15 +162,20 @@ export function rebaseWhereCondition(
|
|||
if (ch === '/' && next === '*') {
|
||||
out += '/*';
|
||||
i += 2;
|
||||
let closed = false;
|
||||
while (i < condition.length) {
|
||||
if (condition.charAt(i) === '*' && condition.charAt(i + 1) === '/') {
|
||||
out += '*/';
|
||||
i += 2;
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
out += condition.charAt(i);
|
||||
i += 1;
|
||||
}
|
||||
if (!closed) {
|
||||
throw new Error('Unterminated block comment in WHERE condition');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,9 @@ export interface LatestByOptions {
|
|||
|
||||
/**
|
||||
* Typed 1-minute bar row returned from QuestDB `bars_1m`.
|
||||
* `listing_id` is stored as decimal string SYMBOL in QuestDB and converted to INT32 at read boundary.
|
||||
* - `listing_id` is stored as decimal string SYMBOL in QuestDB and converted to INT32 at read boundary.
|
||||
* - `known_at` and `ts` are lossless UTC epoch microseconds (`bigint`) projected via `CAST(... AS LONG)`
|
||||
* to preserve all 6 fractional digits and avoid PGWire local-timezone `Date` coercion.
|
||||
*/
|
||||
export interface Bar1mRow {
|
||||
listing_id: number;
|
||||
|
|
@ -105,8 +107,8 @@ export interface Bar1mRow {
|
|||
vwap: number | null;
|
||||
session: string | null;
|
||||
source: string | null;
|
||||
known_at: Date;
|
||||
ts: Date;
|
||||
known_at: bigint;
|
||||
ts: bigint;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue