Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-invalid-dates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperdx/common-utils": patch
---

Skip metadata key/value rollup queries when the date range contains an Invalid Date instead of binding NaN timestamps as ClickHouse `Int64` params, which failed with `BAD_QUERY_PARAMETER` (457).
25 changes: 21 additions & 4 deletions packages/app/src/timeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
subMilliseconds,
} from 'date-fns';
import {
parseAsFloat,
createParser,
parseAsString,
useQueryState,
useQueryStates,
Expand Down Expand Up @@ -408,10 +408,27 @@ const getRelativeInterval = (start: Date, end: Date): string | undefined => {
return `Past ${durationStr}`;
};

// This needs to be a stable reference to prevent rerenders
// `Date`'s own valid range is roughly ±8.64e15ms. nuqs's parseAsFloat only
// rejects NaN, so a URL from/to like `1e400` parses as Infinity (not NaN)
// and survives, later producing `new Date(Infinity)` = Invalid Date, which
// flows into metadata rollup queries as a "nan" ClickHouse Int64 param
// (#2933). Reject anything outside Date's valid range at the source.
const MAX_VALID_EPOCH_MS = 8_640_000_000_000_000;

const parseAsValidEpochMs = createParser({
parse: (v) => {
const float = parseFloat(v);
if (!Number.isFinite(float) || Math.abs(float) > MAX_VALID_EPOCH_MS) {
return null;
}
return float;
},
serialize: v => v.toString(),
});

const timeRangeQueryStateMap = {
from: parseAsFloat,
to: parseAsFloat,
from: parseAsValidEpochMs,
to: parseAsValidEpochMs,
};

export function useNewTimeQuery({
Expand Down
47 changes: 47 additions & 0 deletions packages/common-utils/src/core/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Metadata, MetadataCache } from '@/core/metadata';
import { isDateRangeValid } from '@/core/utils';
import type { BaseClickhouseClient } from '@/clickhouse';
const invalidDate = new Date('not-a-date');

describe('isDateRangeValid', () => {
it('accepts valid date ranges', () => {
expect(
isDateRangeValid([
new Date('2026-08-01T00:00:00Z'),
new Date('2026-08-02T00:00:00Z'),
]),
).toBe(true);
});

it('rejects a range with an invalid start', () => {
expect(isDateRangeValid([invalidDate, new Date()])).toBe(false);
});

it('rejects a range with an invalid end', () => {
expect(isDateRangeValid([new Date(), invalidDate])).toBe(false);
});
});

describe('getMapKeys date range guard', () => {
it('returns [] and never queries ClickHouse for an invalid date range', async () => {
const query = jest
.fn()
.mockRejectedValue(new Error('should not be called'));
const metadata = new Metadata(
query as unknown as BaseClickhouseClient,
new MetadataCache(),
);

const keys = await metadata.getMapKeys({
databaseName: 'db',
tableName: 'tbl',
column: 'attributes',
connectionId: 'conn',
metadataMVs: { granularity: 'minute' } as any,
dateRange: [invalidDate, invalidDate],
});

expect(keys).toEqual([]);
expect(query).not.toHaveBeenCalled();
});
});
19 changes: 19 additions & 0 deletions packages/common-utils/src/core/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import {
getAlignedDateRange,
getDistributedTableArgs,
isDateRangeValid,
MetadataMVQueryOptions,
objectHash,
TextIndexColumnQueryOptions,
Expand Down Expand Up @@ -662,6 +663,15 @@ export class Metadata {
signal?: AbortSignal;
}) {
inlineNonNegativeInt(maxKeys, 'maxKeys');
if (dateRange && !isDateRangeValid(dateRange)) {
console.warn(
`getMapKeys: skipping metadata queries, dateRange contains an invalid date. ` +
`start=${dateRange[0]?.toString()} end=${dateRange[1]?.toString()} ` +
`databaseName=${databaseName} tableName=${tableName} column=${column} connectionId=${connectionId}`,
new Error('getMapKeys invalid date range').stack,
);
return [];

@wrn14897 wrn14897 Aug 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's a good idea to return [] here, since that would produce falsy results. For this ticket, the intent is to figure out what in the app is generating the invalid date range.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wrn14897 Traced the root cause: parseAsFloat in timeQuery.ts only rejects NaN, not Infinity or out-of-range values. A URL like ?from=1e400 parses to Infinity, which becomes new Date(Infinity) = Invalid Date downstream - that's what's producing the "nan" Int64 param.

Two changes pushed to this branch:

  1. timeQuery.ts - added a parser that rejects non-finite/out-of-range from/to values at the URL-parsing stage, so the Invalid Date can't be constructed from this path anymore.
  2. metadata.ts - kept the return [] guard as-is per your concern, but added call-site context (database/table/column) and a stack trace to the warning, so if it ever fires again, it's traceable back to the caller instead of silent.

Let me know if there's another producer you've seen in the query_log this doesn't cover.

}

// Align date range to rollup granularity for consistent cache keys
const alignedDateRange =
Expand Down Expand Up @@ -1336,6 +1346,15 @@ export class Metadata {
maxValuesPerKey: number;
signal?: AbortSignal;
}): Promise<KeyValues[] | undefined> {
if (!isDateRangeValid(dateRange)) {
console.warn(
`getMetadataMVKeyValues: skipping rollup query, dateRange contains an invalid date. ` +
`start=${dateRange[0]?.toString()} end=${dateRange[1]?.toString()} ` +
`databaseName=${databaseName} connectionId=${connectionId}`,
new Error('getMetadataMVKeyValues invalid date range').stack,
);
return undefined;
}
const queryOptionsHash = objectHash(queryOptions);
const metadataMVsHash = objectHash(metadataMVs ?? {});
const cacheKey = `${databaseName}.${connectionId}.${dateRange[0].toString()}.${dateRange[1].toString()}.${maxValuesPerKey}.${metadataMVsHash}.${queryOptionsHash}.getMetadataMVKeyValues`;
Expand Down
7 changes: 6 additions & 1 deletion packages/common-utils/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,12 @@ export function getAlignedDateRange(

return [alignedStart, alignedEnd];
}

export function isDateRangeValid(dateRange: [Date, Date]): boolean {
return (
Number.isFinite(dateRange[0].getTime()) &&
Number.isFinite(dateRange[1].getTime())
);
}
export function isDateRangeEqual(range1: [Date, Date], range2: [Date, Date]) {
return (
range1[0].getTime() === range2[0].getTime() &&
Expand Down
Loading