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

@ -277,6 +277,57 @@ describe('QuestDB 10.2.0 query layer', () => {
// Builder is reset again after successful execute()
expect(() => builder.build()).toThrow('FROM clause is required');
});
it('terminates trailing line comments with a newline before subsequent predicates, LATEST ON, and SAMPLE BY', async () => {
const fake = new FakeQuestDBClient();
const client = createClientWithFakeQuery(fake);
const builderResult = QuestDBQueryBuilder.create(fake)
.select('listing_id', 'ts', 'close')
.from('bars_1m')
.where('session = $1 -- regular session', ['RTH'])
.where('close > $1 -- minimum price', [100])
.latestOn('ts', 'listing_id')
.buildParameterized();
expect(builderResult.sql).toBe(
'SELECT listing_id, ts, close FROM bars_1m ' +
'WHERE session = $1 -- regular session\n ' +
'AND close > $2 -- minimum price\n ' +
'LATEST ON ts PARTITION BY listing_id'
);
expect(builderResult.params).toEqual(['RTH', 100]);
await client.latestBy('bars_1m', ['listing_id', 'ts', 'close'], 'listing_id', {
where: 'session = $1 -- regular session',
params: ['RTH'],
});
await client.sampleBy(
'bars_1m',
['ts', 'close'],
'1h',
'ts',
'session = $1 -- regular session',
['RTH']
);
expect(fake.calls).toEqual([
{
sql:
'SELECT listing_id, ts, close FROM bars_1m ' +
'WHERE session = $1 -- regular session\n ' +
'LATEST ON ts PARTITION BY listing_id',
params: ['RTH'],
},
{
sql:
'SELECT ts, close FROM bars_1m ' +
'WHERE session = $1 -- regular session\n ' +
'SAMPLE BY 1h ALIGN TO CALENDAR',
params: ['RTH'],
},
]);
});
});
describe('identifier and interval validation', () => {
@ -337,6 +388,22 @@ describe('QuestDB 10.2.0 query layer', () => {
params: [hostileTable],
});
});
it('binds table name in QuestDBClient.getTableInfo() via table_columns($1)', async () => {
const fake = new FakeQuestDBClient();
const client = createClientWithFakeQuery(fake);
fake.setRows([{ column: 'ts', type: 'TIMESTAMP' }]);
const rows = await client.getTableInfo('bars_1m');
expect(rows).toEqual([{ column: 'ts', type: 'TIMESTAMP' }]);
expect(fake.calls).toEqual([
{
sql: 'SELECT * FROM table_columns($1)',
params: ['bars_1m'],
},
]);
});
});
describe('M2 intraday contract DDL', () => {
@ -395,8 +462,12 @@ describe('QuestDB 10.2.0 query layer', () => {
});
describe('readBars1m storage reader', () => {
it('sends one parameterized [from, to) query and converts decimal-string listing_id to INT32', async () => {
it('sends one parameterized [from, to) query with UTC epoch-microsecond projection and converts listing_id to INT32', async () => {
const fake = new FakeQuestDBClient();
// 2024-01-02T14:30:00.000000Z = 1704205800000000 us
// 2024-01-02T14:31:00.000700Z = 1704205860000700 us (6 fractional digits preserved)
// 2024-01-02T14:31:00.000000Z = 1704205860000000 us
// 2024-01-02T14:32:00.123456Z = 1704205920123456 us
const fixtureRows = [
{
listing_id: '1042',
@ -408,8 +479,8 @@ describe('QuestDB 10.2.0 query layer', () => {
vwap: 185.28,
session: 'RTH',
source: 'eod_unadj',
known_at: new Date('2024-01-02T14:31:00.000Z'),
ts: new Date('2024-01-02T14:30:00.000Z'),
known_at_us: '1704205860000700',
ts_us: '1704205800000000',
},
{
listing_id: '1042',
@ -421,8 +492,8 @@ describe('QuestDB 10.2.0 query layer', () => {
vwap: null,
session: 'RTH',
source: 'ib_live',
known_at: '2024-01-02T14:32:00.000Z',
ts: '2024-01-02T14:31:00.000Z',
known_at_us: 1704205920123456n,
ts_us: 1704205860000000,
},
];
fake.setRows(fixtureRows);
@ -436,7 +507,7 @@ describe('QuestDB 10.2.0 query layer', () => {
expect(fake.calls).toHaveLength(2);
expect(fake.calls[0]).toEqual({
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',
@ -455,8 +526,8 @@ describe('QuestDB 10.2.0 query layer', () => {
vwap: 185.28,
session: 'RTH',
source: 'eod_unadj',
known_at: new Date('2024-01-02T14:31:00.000Z'),
ts: new Date('2024-01-02T14:30:00.000Z'),
known_at: 1704205860000700n,
ts: 1704205800000000n,
},
{
listing_id: 1042,
@ -468,11 +539,72 @@ describe('QuestDB 10.2.0 query layer', () => {
vwap: null,
session: 'RTH',
source: 'ib_live',
known_at: new Date('2024-01-02T14:32:00.000Z'),
ts: new Date('2024-01-02T14:31:00.000Z'),
known_at: 1704205920123456n,
ts: 1704205860000000n,
},
]);
expect(typeof firstRead[0]!.listing_id).toBe('number');
// Verify sub-millisecond ordering is preserved (14:31:00.000700Z > 14:31:00.000000Z)
expect(firstRead[0]!.known_at > 1704205860000000n).toBe(true);
});
it('preserves 6-digit microsecond bounds and produces identical results under non-UTC TZ', async () => {
const fake = new FakeQuestDBClient();
fake.setRows([
{
listing_id: '1042',
open: 185.1,
high: 185.5,
low: 184.9,
close: 185.4,
volume: 12500,
vwap: 185.28,
session: 'RTH',
source: 'eod_unadj',
known_at_us: '1704205860000700',
ts_us: '1704205800000000',
},
]);
const previousTz = process.env.TZ;
try {
process.env.TZ = 'UTC';
const utcRows = await readBars1m(
fake,
1042,
'2024-01-02T14:30:00.000000Z',
'2024-01-02T14:31:00.000700Z'
);
process.env.TZ = 'America/Toronto';
const torontoRows = await readBars1m(
fake,
1042,
'2024-01-02T14:30:00.000000Z',
'2024-01-02T14:31:00.000700Z'
);
expect(torontoRows).toEqual(utcRows);
expect(torontoRows[0]!.known_at).toBe(1704205860000700n);
expect(torontoRows[0]!.ts).toBe(1704205800000000n);
expect(fake.calls[0]?.params).toEqual([
'1042',
'2024-01-02T14:30:00.000000Z',
'2024-01-02T14:31:00.000700Z',
]);
expect(fake.calls[1]?.params).toEqual(fake.calls[0]?.params);
} finally {
if (previousTz === undefined) {
delete process.env.TZ;
} else {
process.env.TZ = previousTz;
}
}
// Reject timezone-naive string bounds that would depend on host local timezone
await expect(
readBars1m(fake, 1042, '2024-01-02T14:30:00', '2024-01-02T14:35:00Z')
).rejects.toThrow(/timezone/i);
});
it('returns empty array when no bars exist in range', async () => {
@ -515,8 +647,8 @@ describe('QuestDB 10.2.0 query layer', () => {
vwap: 1.5,
session: 'RTH',
source: 'eod_unadj',
known_at: from,
ts: from,
known_at_us: '1704205800000000',
ts_us: '1704205800000000',
},
]);
await expect(readBars1m(fake, 42, from, to)).rejects.toThrow(/listing_id SYMBOL/i);