Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

events: newest-first query results and exact export date bounds

Carl-Gerhard Lindesvärdf37bb1308494
diff --git a/packages/core/src/modules/chart/src/property-key-escaping.test.ts b/packages/core/src/modules/chart/src/property-key-escaping.test.ts
new file mode 100644
index 000000000..3a8411b3e
--- /dev/null
+++ b/packages/core/src/modules/chart/src/property-key-escaping.test.ts
@@ -0,0 +1,327 @@
+/**
+ * Property keys reach the SQL builders as free text: a filter name, a
+ * breakdown name or a math-metric property from a saved report or an API
+ * call. They end up inside a ClickHouse Map access, so a key carrying a
+ * quote must stay one value — otherwise it closes the literal and the rest
+ * of the key is parsed as SQL, next to the `project_id` predicate that scopes
+ * the query to one project (V1 #483).
+ *
+ * Here every key binds as a `{pN:String}` param, so the assertions are that
+ * the rendered statement never carries the key's text, that the key comes
+ * back whole in the params, and that ClickHouse still plans the statement
+ * with the hostile key bound (EXPLAIN, same harness as sql.test.ts).
+ */
+
+import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';
+import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';
+import type { EventListQuery } from '../../event/src/sql';
+import type {
+ IChartBreakdown,
+ IChartEvent,
+ IChartEventFilter,
+} from '../../report/report.constants';
+import type { AggregateChartSqlInput, ChartSqlInput } from './statement';
+
+const cohortFindMany = mock(async () => []);
+const actualPrismaClient = await import('@openpanel/db/src/prisma-client');
+const realPrismaClient = { ...actualPrismaClient };
+mock.module('@openpanel/db/src/prisma-client', () => ({
+ ...realPrismaClient,
+ db: { cohort: { findMany: cohortFindMany } },
+}));
+
+afterAll(async () => {
+ mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);
+});
+
+let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;
+let deps: import('../../../services').ServiceDeps;
+let buildChartSql: typeof import('./statement').getChartSql;
+let buildAggregateChartSql: typeof import('./statement').getAggregateChartSql;
+let fieldResolution: typeof import('./field-resolution');
+let compiled: typeof import('./compiled');
+let filterWhere: typeof import('./filter-where');
+let eventSql: typeof import('../../event/src/sql');
+
+const PROJECT_ID = 'test-sql-validation';
+const START = '2026-04-14 00:00:00';
+const END = '2026-05-15 00:00:00';
+
+// A key that would close the literal, append its own predicate, and reopen a
+// literal so the tail of the original render still parses.
+const BREAKOUT_KEY = "x'] = '' OR 1 = 1 OR properties['y";
+const BREAKOUT_PROPERTY = `properties.${BREAKOUT_KEY}`;
+const INJECTED_PREDICATE = "'] = '' OR 1 = 1";
+
+type LooseChartInput = Partial<ChartSqlInput> &
+ Pick<ChartSqlInput, 'event' | 'projectId' | 'timezone'>;
+type LooseAggregateInput = Partial<AggregateChartSqlInput> &
+ Pick<AggregateChartSqlInput, 'event' | 'projectId' | 'timezone'>;
+
+const event = (overrides: Partial<IChartEvent> = {}): IChartEvent => ({
+ id: 'A',
+ name: 'screen_view',
+ segment: 'event',
+ filters: [],
+ ...overrides,
+});
+
+const breakdown = (name: string): IChartBreakdown => ({ id: name, name });
+
+const hostileFilter: IChartEventFilter = {
+ id: 'f1',
+ name: BREAKOUT_PROPERTY,
+ operator: 'is',
+ value: ['pro'],
+};
+
+const base = {
+ interval: 'day' as const,
+ startDate: START,
+ endDate: END,
+ projectId: PROJECT_ID,
+ timezone: 'UTC',
+};
+
+function statement(fragment: SqlFragment) {
+ return fragment.toStatement();
+}
+
+/**
+ * The key never reaches the SQL text: it travels whole as a String param,
+ * and the injected predicate appears nowhere in the statement.
+ */
+function keyBinding(fragment: SqlFragment) {
+ const { query, query_params } = statement(fragment);
+ return {
+ textCarriesInjectedPredicate: query.includes(INJECTED_PREDICATE),
+ textCarriesKey: query.includes(BREAKOUT_KEY),
+ paramsCarryKey: Object.values(query_params).includes(BREAKOUT_KEY),
+ };
+}
+
+const KEY_BOUND = {
+ textCarriesInjectedPredicate: false,
+ textCarriesKey: false,
+ paramsCarryKey: true,
+};
+
+async function getChartSql(input: LooseChartInput): Promise<SqlFragment> {
+ return buildChartSql(deps, input as ChartSqlInput);
+}
+
+async function getAggregateChartSql(
+ input: LooseAggregateInput
+): Promise<SqlFragment> {
+ return buildAggregateChartSql(deps, input as AggregateChartSqlInput);
+}
+
+async function explain(fragment: SqlFragment): Promise<void> {
+ const { query, query_params } = statement(fragment);
+ await ch.command({ query: `EXPLAIN ${query}`, query_params });
+}
+
+beforeAll(async () => {
+ ({ ch } = await import('@openpanel/db/src/clickhouse/client'));
+ const { testServiceDeps } = await import('../../../../test/service-deps');
+ deps = await testServiceDeps();
+ ({
+ getChartSql: buildChartSql,
+ getAggregateChartSql: buildAggregateChartSql,
+ } = await import('./statement'));
+ fieldResolution = await import('./field-resolution');
+ compiled = await import('./compiled');
+ filterWhere = await import('./filter-where');
+ eventSql = await import('../../event/src/sql');
+ const { bootstrapTestDatabases } = await import(
+ '../../../../../../test/bootstrap-databases'
+ );
+ await bootstrapTestDatabases();
+}, 30_000);
+
+describe('getSelectPropertyKey / key binding', () => {
+ it('binds a key with a quote as one String param', () => {
+ const { query, query_params } = statement(
+ fieldResolution.getSelectPropertyKey(
+ BREAKOUT_PROPERTY,
+ undefined,
+ undefined,
+ undefined,
+ 'e'
+ )
+ );
+ expect(query).toBe('e.properties[{p1:String}]');
+ expect(query_params).toEqual({ p1: BREAKOUT_KEY });
+ });
+
+ it('binds backslashes and ] untouched', () => {
+ const { query, query_params } = statement(
+ fieldResolution.getSelectPropertyKey('properties.a\\b]c')
+ );
+ expect(query).toBe('properties[{p1:String}]');
+ expect(query_params).toEqual({ p1: 'a\\b]c' });
+ });
+
+ it('binds profile property keys the same way', () => {
+ const { query, query_params } = statement(
+ fieldResolution.getSelectPropertyKey("profile.properties.pl'an")
+ );
+ expect(query).toBe('profile.properties[{p1:String}]');
+ expect(query_params).toEqual({ p1: "pl'an" });
+ });
+
+ it('renders ordinary keys as before', () => {
+ expect(
+ statement(fieldResolution.getSelectPropertyKey('properties.foo'))
+ ).toEqual({
+ query: 'properties[{p1:String}]',
+ query_params: { p1: 'foo' },
+ });
+ expect(
+ statement(fieldResolution.getSelectPropertyKey('properties.a.*')).query
+ ).toBe(
+ 'arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(properties, {p1:String})))'
+ );
+ expect(
+ statement(fieldResolution.getSelectPropertyKey('country')).query
+ ).toBe('country');
+ });
+});
+
+describe('chart SQL with a hostile property key', () => {
+ it('keeps the project scope for a filter name', async () => {
+ const fragment = await getChartSql({
+ event: event({ filters: [hostileFilter] }),
+ breakdowns: [],
+ ...base,
+ });
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+
+ it('keeps the project scope for a breakdown name', async () => {
+ const fragment = await getChartSql({
+ event: event(),
+ breakdowns: [breakdown(BREAKOUT_PROPERTY)],
+ ...base,
+ });
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+
+ it('keeps the project scope for a math-metric property', async () => {
+ const fragment = await getChartSql({
+ event: event({
+ segment: 'property_average',
+ property: BREAKOUT_PROPERTY,
+ }),
+ breakdowns: [],
+ ...base,
+ });
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+
+ it('keeps the project scope in aggregate chart SQL', async () => {
+ const fragment = await getAggregateChartSql({
+ event: event({ segment: 'property_sum', property: BREAKOUT_PROPERTY }),
+ breakdowns: [breakdown(BREAKOUT_PROPERTY)],
+ ...base,
+ });
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+});
+
+describe('event SQL with a hostile property key', () => {
+ const listQuery = (): EventListQuery => ({
+ projectId: PROJECT_ID,
+ columns: ['created_at', 'id', 'name'],
+ take: 10,
+ filterClauses: filterWhere.getEventFiltersWhereClause(
+ [hostileFilter],
+ PROJECT_ID,
+ 'e'
+ ),
+ joins: eventSql.NO_FILTER_JOINS,
+ startDate: new Date(START),
+ endDate: new Date(END),
+ });
+
+ it('keeps the project scope in the event list query', async () => {
+ const fragment = eventSql.eventListQuery(listQuery());
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+
+ it('keeps the project scope in the event count query', async () => {
+ const { columns: _columns, take: _take, ...query } = listQuery();
+ const fragment = eventSql.eventsCountQuery(query);
+ expect(keyBinding(fragment)).toEqual(KEY_BOUND);
+ await explain(fragment);
+ });
+});
+
+describe('profile-property narrowing with a quoted key', () => {
+ const key = "pl'an";
+ const name = `profile.properties.${key}`;
+
+ it('narrows the key and rewrites its reference to the CTE column', () => {
+ const { keys, needsFullMap } = fieldResolution.collectProfilePropertyKeys([
+ { name },
+ ]);
+ expect(keys).toEqual([key]);
+ expect(needsFullMap).toBe(false);
+
+ const cteSelect = statement(
+ fieldResolution.profilePropertiesCteSelect(keys, needsFullMap)
+ );
+ expect(cteSelect.query).toBe(
+ "properties[{p1:String}] as `profile.properties.pl'an`"
+ );
+ expect(cteSelect.query_params).toEqual({ p1: key });
+
+ // The ref is a bound param, so the rewrite collapses the param itself
+ // into the alias instead of searching the text for an escaped literal.
+ const rewritten = statement(
+ compiled.fragmentWithProfileRefs(
+ fieldResolution.getSelectPropertyKey(name),
+ keys
+ )
+ );
+ expect(rewritten.query).toBe("`profile.properties.pl'an`");
+ expect(rewritten.query_params).toEqual({});
+ });
+
+ it('falls back to the full Map for keys it cannot alias', () => {
+ const { keys, needsFullMap } = fieldResolution.collectProfilePropertyKeys([
+ { name: 'profile.properties.a\\b' },
+ ]);
+ expect(keys).toEqual([]);
+ expect(needsFullMap).toBe(true);
+ expect(
+ statement(fieldResolution.profilePropertiesCteSelect(keys, needsFullMap))
+ .query
+ ).toBe('properties as "profile.properties"');
+ });
+
+ it('leaves ordinary keys narrowing exactly as before', () => {
+ const { keys, needsFullMap } = fieldResolution.collectProfilePropertyKeys([
+ { name: 'profile.properties.plan' },
+ ]);
+ expect(keys).toEqual(['plan']);
+ expect(needsFullMap).toBe(false);
+ expect(
+ statement(fieldResolution.profilePropertiesCteSelect(keys, needsFullMap))
+ .query
+ ).toBe('properties[{p1:String}] as `profile.properties.plan`');
+ expect(
+ statement(
+ compiled.fragmentWithProfileRefs(
+ fieldResolution.getSelectPropertyKey('profile.properties.plan'),
+ keys
+ )
+ ).query
+ ).toBe('`profile.properties.plan`');
+ });
+});
diff --git a/packages/core/src/modules/chart/src/sql.ts b/packages/core/src/modules/chart/src/sql.ts
index b15a4cce9..896b4f849 100644
--- a/packages/core/src/modules/chart/src/sql.ts
+++ b/packages/core/src/modules/chart/src/sql.ts
@@ -649,18 +649,6 @@ export function groupPropertyValuesQuery(
return sql`SELECT distinct ${expression} as values FROM ${sql.id(CHART_TABLE.groups)} FINAL WHERE project_id = ${sql.string(projectId)} AND deleted = 0 AND ${expression} != '' AND ${expression} IS NOT NULL ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;
}
-/** Distinct values of an events column / property over the last six months. */
-export function eventFieldValuesQuery(
- projectId: string,
- selectExpression: SqlFragment,
- event: string
-): SqlFragment {
- // V1 only skipped the name clause for `*` — an empty event name filters on ''.
- const eventName =
- event !== '*' ? sql` AND name = ${sql.string(event)}` : sql.empty;
- return sql`SELECT distinct ${selectExpression} as values FROM ${sql.id(CHART_TABLE.events)} WHERE project_id = ${sql.string(projectId)} AND created_at > (now() - INTERVAL 6 MONTH)${eventName} ORDER BY created_at DESC LIMIT ${sql.uint64(PROPERTY_VALUES_LIMIT)}`;
-}
-
// --- getProfiles ------------------------------------------------------------
export interface ChartBucketProfilesInput {
diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.ts
index adfdf526c..a21ec1856 100644
--- a/packages/core/src/modules/event/event.service.ts
+++ b/packages/core/src/modules/event/event.service.ts
@@ -692,13 +692,15 @@ async function compileEventFilters(
}
/**
- * V1 applied the lookback window for a Date cursor and for "no cursor and no
- * date range" — where a numeric `cursor` of 0 counts as no cursor, so the
- * first numeric page is windowed and later pages are not.
+ * The lookback window applies for a Date cursor and when there is no cursor
+ * and no date bound at all. A numeric cursor of 0 is the first page, not "no
+ * cursor", and either date bound alone is enough to skip the window (#477).
*/
function hasEventListLookback(options: GetEventListOptions): boolean {
const { cursor, startDate, endDate } = options;
- return cursor instanceof Date || !(cursor || (startDate && endDate));
+ return (
+ cursor instanceof Date || (cursor === undefined && !startDate && !endDate)
+ );
}
export async function getEventList(
diff --git a/packages/core/src/modules/event/src/dates.ts b/packages/core/src/modules/event/src/dates.ts
index bcfcf8cb5..e118fafbd 100644
--- a/packages/core/src/modules/event/src/dates.ts
+++ b/packages/core/src/modules/event/src/dates.ts
@@ -13,6 +13,15 @@ export function formatClickhouseDate(date: Date | string): string {
.replace(CLICKHOUSE_DATE_SUFFIX, '');
}
+/**
+ * `YYYY-MM-DD HH:mm:ss.SSS` in UTC. `created_at` is DateTime64(3), so a bound
+ * that went through `formatClickhouseDate` would lose the millisecond and
+ * could in/exclude events inside the same truncated second.
+ */
+export function formatClickhouseDateTime64(date: Date | string): string {
+ return new Date(date).toISOString().replace('T', ' ').replace('Z', '');
+}
+
export function convertClickhouseDateToJs(date: string): Date {
return new Date(`${date.replace(' ', 'T')}Z`);
}
diff --git a/packages/core/src/modules/event/src/sql.test.ts b/packages/core/src/modules/event/src/sql.test.ts
index b24630cac..46fd71d19 100644
--- a/packages/core/src/modules/event/src/sql.test.ts
+++ b/packages/core/src/modules/event/src/sql.test.ts
@@ -122,7 +122,7 @@ describe('eventListQuery', () => {
}).toStatement();
expect(query).toBe(
- "SELECT created_at, id, name FROM events e WHERE project_id = {p1:String} AND ((device_id IN (SELECT device_id as did FROM events WHERE project_id = {p2:String} AND device_id != '' AND profile_id = {p3:String} group by did) AND profile_id = device_id) OR profile_id = {p4:String}) AND session_id = {p5:String} AND has(groups, {p6:String}) AND profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p7:String} AND project_id = {p8:String}) AND toDate(created_at) BETWEEN toDate({p9:String}) AND toDate({p10:String}) AND name IN {p11:Array(String)} AND (path = '/') AND name IN {p12:Array(String)} ORDER BY toDate(created_at) DESC, created_at DESC, id ASC LIMIT {p13:UInt64}"
+ "SELECT created_at, id, name FROM events e WHERE project_id = {p1:String} AND ((device_id IN (SELECT device_id as did FROM events WHERE project_id = {p2:String} AND device_id != '' AND profile_id = {p3:String} group by did) AND profile_id = device_id) OR profile_id = {p4:String}) AND session_id = {p5:String} AND has(groups, {p6:String}) AND profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p7:String} AND project_id = {p8:String}) AND created_at >= toDateTime64({p9:String}, 3) AND created_at <= toDateTime64({p10:String}, 3) AND name IN {p11:Array(String)} AND (path = '/') AND name IN {p12:Array(String)} ORDER BY toDate(created_at) DESC, created_at DESC, id ASC LIMIT {p13:UInt64}"
);
expect(query).not.toContain(HOSTILE);
expect(query_params).toEqual({
@@ -134,14 +134,36 @@ describe('eventListQuery', () => {
p6: 'grp-1',
p7: 'coh-1',
p8: PROJECT_ID,
- p9: '2026-08-01 00:00:00',
- p10: '2026-08-31 23:59:59',
+ p9: '2026-08-01 00:00:00.000',
+ p10: '2026-08-31 23:59:59.000',
p11: ['a', HOSTILE],
p12: ['signup'],
p13: 50,
});
});
+ test('date bounds are exact timestamps and apply independently (#477)', () => {
+ const endOnly = eventListQuery({
+ ...minimal,
+ endDate: new Date('2026-09-03T00:00:00.000Z'),
+ }).toStatement();
+ expect(endOnly.query).toContain(
+ 'WHERE project_id = {p1:String} AND created_at <= toDateTime64({p2:String}, 3) ORDER BY'
+ );
+ expect(endOnly.query).not.toContain('toDate(created_at) BETWEEN');
+ expect(endOnly.query_params.p2).toBe('2026-09-03 00:00:00.000');
+
+ const startOnly = eventListQuery({
+ ...minimal,
+ startDate: new Date('2026-08-01T12:34:56.789Z'),
+ }).toStatement();
+ expect(startOnly.query).toContain(
+ 'WHERE project_id = {p1:String} AND created_at >= toDateTime64({p2:String}, 3) ORDER BY'
+ );
+ // Millisecond precision survives: created_at is DateTime64(3).
+ expect(startOnly.query_params.p2).toBe('2026-08-01 12:34:56.789');
+ });
+
test('empty events / conversionNames lists add no clause', () => {
const { query } = eventListQuery({
...minimal,
@@ -186,7 +208,7 @@ describe('eventsCountQuery', () => {
}).toStatement();
expect(query).toBe(
- "SELECT count(*) as count FROM events e WHERE project_id = {p1:String} AND profile_id = {p2:String} AND has(groups, {p3:String}) AND profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p4:String} AND project_id = {p5:String}) AND toDate(created_at) BETWEEN toDate({p6:String}) AND toDate({p7:String}) AND name IN {p8:Array(String)} AND (path = '/')"
+ "SELECT count(*) as count FROM events e WHERE project_id = {p1:String} AND profile_id = {p2:String} AND has(groups, {p3:String}) AND profile_id IN (SELECT profile_id FROM cohort_members FINAL WHERE cohort_id = {p4:String} AND project_id = {p5:String}) AND created_at >= toDateTime64({p6:String}, 3) AND created_at <= toDateTime64({p7:String}, 3) AND name IN {p8:Array(String)} AND (path = '/')"
);
expect(query).not.toContain('device_id IN');
expect(query_params).toMatchObject({ p1: PROJECT_ID, p2: HOSTILE });
@@ -286,11 +308,24 @@ describe('queryEventsQuery', () => {
limit: 100,
}).toStatement();
expect(query).toBe(
- 'SELECT * FROM events WHERE project_id = {p1:String} LIMIT {p2:UInt64}'
+ 'SELECT * FROM events WHERE project_id = {p1:String} ORDER BY created_at DESC LIMIT {p2:UInt64}'
);
expect(query_params).toEqual({ p1: PROJECT_ID, p2: 100 });
});
+ // Callers label these rows newest-first, so the cut has to happen after the
+ // sort rather than wherever the scan starts (#476).
+ test('takes the newest rows, not an arbitrary slice', () => {
+ const { query } = queryEventsQuery({
+ projectId: PROJECT_ID,
+ equals: {},
+ filterClauses: NO_FILTERS,
+ limit: 20,
+ }).toStatement();
+ expect(query.indexOf('ORDER BY created_at DESC')).toBeGreaterThan(-1);
+ expect(query.indexOf('ORDER BY')).toBeLessThan(query.indexOf('LIMIT'));
+ });
+
test('equality columns follow V1 order regardless of input order; keys bind too', () => {
const { query, query_params } = queryEventsQuery({
projectId: PROJECT_ID,
@@ -306,7 +341,7 @@ describe('queryEventsQuery', () => {
}).toStatement();
expect(query).toBe(
- 'SELECT * FROM events WHERE project_id = {p1:String} AND session_id = {p2:String} AND profile_id = {p3:String} AND profile_id IN {p4:Array(String)} AND name IN {p5:Array(String)} AND path = {p6:String} AND country = {p7:String} AND browser = {p8:String} AND properties[{p9:String}] = {p10:String} AND created_at BETWEEN {p11:String} AND {p12:String} AND (1 = 1) LIMIT {p13:UInt64}'
+ 'SELECT * FROM events WHERE project_id = {p1:String} AND session_id = {p2:String} AND profile_id = {p3:String} AND profile_id IN {p4:Array(String)} AND name IN {p5:Array(String)} AND path = {p6:String} AND country = {p7:String} AND browser = {p8:String} AND properties[{p9:String}] = {p10:String} AND created_at BETWEEN {p11:String} AND {p12:String} AND (1 = 1) ORDER BY created_at DESC LIMIT {p13:UInt64}'
);
expect(query).not.toContain(HOSTILE);
expect(query_params).toMatchObject({
diff --git a/packages/core/src/modules/event/src/sql.ts b/packages/core/src/modules/event/src/sql.ts
index 867589366..1e50ad374 100644
--- a/packages/core/src/modules/event/src/sql.ts
+++ b/packages/core/src/modules/event/src/sql.ts
@@ -24,7 +24,7 @@ import {
QUERY_EVENTS_EQUALITY_COLUMNS,
type QueryEventsEqualityColumn,
} from '../event.constants';
-import { formatClickhouseDate } from './dates';
+import { formatClickhouseDate, formatClickhouseDateTime64 } from './dates';
import {
type CompiledFilterClauses,
compiledFilterFragments,
@@ -61,8 +61,24 @@ function optional(
return condition ? fragment() : sql.empty;
}
-function calendarDayRange(startDate: Date, endDate: Date): SqlFragment {
- return sql`toDate(created_at) BETWEEN toDate(${sql.string(formatClickhouseDate(startDate))}) AND toDate(${sql.string(formatClickhouseDate(endDate))})`;
+/**
+ * Each bound applies on its own and at the exact timestamp: `toDate()` on both
+ * sides of a BETWEEN truncated them to calendar days, so an end of midnight
+ * silently included the whole day (#477).
+ */
+function exactDateBounds(startDate?: Date, endDate?: Date): SqlFragment[] {
+ const bounds: SqlFragment[] = [];
+ if (startDate) {
+ bounds.push(
+ sql`created_at >= toDateTime64(${sql.string(formatClickhouseDateTime64(startDate))}, 3)`
+ );
+ }
+ if (endDate) {
+ bounds.push(
+ sql`created_at <= toDateTime64(${sql.string(formatClickhouseDateTime64(endDate))}, 3)`
+ );
+ }
+ return bounds;
}
export interface EventFilterJoins {
@@ -165,9 +181,7 @@ function eventListConditions(query: EventListQuery): SqlFragment[] {
sql`profile_id IN (SELECT profile_id FROM ${sql.id(TABLE.cohortMembers)} FINAL WHERE cohort_id = ${sql.string(query.cohortId)} AND project_id = ${projectId})`
);
}
- if (query.startDate && query.endDate) {
- conditions.push(calendarDayRange(query.startDate, query.endDate));
- }
+ conditions.push(...exactDateBounds(query.startDate, query.endDate));
if (query.events && query.events.length > 0) {
conditions.push(sql`name IN ${sql.array('String', query.events)}`);
}
@@ -229,9 +243,7 @@ export function eventsCountQuery(query: EventsCountQuery): SqlFragment {
sql`profile_id IN (SELECT profile_id FROM ${sql.id(TABLE.cohortMembers)} FINAL WHERE cohort_id = ${sql.string(query.cohortId)} AND project_id = ${projectId})`
);
}
- if (query.startDate && query.endDate) {
- conditions.push(calendarDayRange(query.startDate, query.endDate));
- }
+ conditions.push(...exactDateBounds(query.startDate, query.endDate));
if (query.events && query.events.length > 0) {
conditions.push(sql`name IN ${sql.array('String', query.events)}`);
}
@@ -369,11 +381,15 @@ function queryEventsConditions(query: QueryEventsQuery): SqlFragment[] {
return conditions;
}
+/**
+ * Without an explicit order ClickHouse returns whatever it reads first, so a
+ * bare LIMIT hands back an arbitrary slice of the window, not the newest.
+ */
export function queryEventsQuery(query: QueryEventsQuery): SqlFragment {
return sql`SELECT * FROM ${sql.id(TABLE.events)} WHERE ${sql.join(
queryEventsConditions(query),
' AND '
- )} LIMIT ${sql.uint64(query.limit)}`;
+ )} ORDER BY created_at DESC LIMIT ${sql.uint64(query.limit)}`;
}
// ---- packages/trpc/src/routers/event.ts's inline queries