Repository navigation
fix(common-utils): support numeric range searches on String attributes #3693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aryainguz
wants to merge
10
commits into
ClickHouse:main
Choose a base branch
from
Aryainguz:aryainguz/numeric-attribute-comparisons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e2e65f
fix(common-utils): compare numeric search bounds as numbers on String…
Aryainguz 60792eb
Merge branch 'main' into aryainguz/numeric-attribute-comparisons
Aryainguz b94f4b9
refactor(common-utils): reuse parseToNumber for numeric search bounds
Aryainguz b91d878
Merge branch 'main' into aryainguz/numeric-attribute-comparisons
Aryainguz 71a2b78
fix(common-utils): keep negated comparisons and non-String types cons…
Aryainguz 8f730de
test(common-utils): cover materialized String and FixedString numeric…
Aryainguz 07cfe58
fix(common-utils): limit numeric String comparisons to range searches
Aryainguz 8579016
Merge branch 'main' into aryainguz/numeric-attribute-comparisons
Aryainguz f0cdc45
refactor(common-utils): reuse isCHFixedStringType for numeric range c…
Aryainguz cfe3d1b
Merge remote-tracking branch 'origin/aryainguz/numeric-attribute-comp…
Aryainguz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| '@hyperdx/common-utils': patch | ||
| '@hyperdx/api': patch | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| fix: support numeric range searches on String attributes | ||
|
|
||
| Range searches with numeric bounds on a String-typed field, such as | ||
| `LogAttributes.duration_ms:[10 TO 20]`, failed with "no supertype for types | ||
| String, UInt8". They now compare with `toFloat64OrNull`; values that aren't | ||
| numbers don't match. `>`, `<`, `>=` and `<=` still compare String fields as | ||
| text; use a range such as `:[1000 TO *]` for a numeric comparison. |
141 changes: 141 additions & 0 deletions
141
packages/common-utils/src/__tests__/numericStringComparisons.int.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { ClickHouseClient, createClient } from '@clickhouse/client'; | ||
|
|
||
| import { parameterizedQueryToSql } from '@/clickhouse'; | ||
| import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node'; | ||
| import { Metadata, MetadataCache } from '@/core/metadata'; | ||
| import { renderChartConfig } from '@/core/renderChartConfig'; | ||
| import { ChartConfigWithOptDateRange } from '@/types'; | ||
|
|
||
| // Unit tests only compare SQL text, which is how ranges with numeric bounds on | ||
| // String attributes shipped while ClickHouse rejected them. These run each | ||
| // search against ClickHouse and check which rows come back. | ||
| describe('numeric range bounds on String attributes', () => { | ||
| let client: ClickHouseClient; | ||
| let hdxClient: HdxClickhouseClient; | ||
| let metadata: Metadata; | ||
|
|
||
| const DATABASE = 'default'; | ||
| const TABLE_NAME = 'numeric_string_comparisons_int_test'; | ||
|
|
||
| // Code mirrors gc_pause_ms in a FixedString, padded with zero bytes. | ||
| const ROWS: Array<{ | ||
| Id: number; | ||
| Severity: string; | ||
| LogAttributes: Record<string, string>; | ||
| Code: string; | ||
| }> = [ | ||
| { | ||
| Id: 1, | ||
| Severity: 'debug', | ||
| LogAttributes: { gc_pause_ms: '5' }, | ||
| Code: '5', | ||
| }, | ||
| { | ||
| Id: 2, | ||
| Severity: 'info', | ||
| LogAttributes: { gc_pause_ms: '10' }, | ||
| Code: '10', | ||
| }, | ||
| { | ||
| Id: 3, | ||
| Severity: 'error', | ||
| LogAttributes: { gc_pause_ms: '17.25' }, | ||
| Code: '17.25', | ||
| }, | ||
| { | ||
| Id: 4, | ||
| Severity: 'info', | ||
| LogAttributes: { gc_pause_ms: '100' }, | ||
| Code: '100', | ||
| }, | ||
| { | ||
| Id: 5, | ||
| Severity: 'info', | ||
| LogAttributes: { gc_pause_ms: '150' }, | ||
| Code: '150', | ||
| }, | ||
| { | ||
| Id: 6, | ||
| Severity: 'info', | ||
| LogAttributes: { gc_pause_ms: 'abc' }, | ||
| Code: 'abc', | ||
| }, | ||
| { Id: 7, Severity: 'info', LogAttributes: { other: 'x' }, Code: '' }, | ||
| ]; | ||
|
|
||
| const queryIds = async (where: string) => { | ||
| const config: ChartConfigWithOptDateRange = { | ||
| connection: 'test-connection', | ||
| from: { databaseName: DATABASE, tableName: TABLE_NAME }, | ||
| select: 'Id', | ||
| where, | ||
| whereLanguage: 'lucene', | ||
| timestampValueExpression: 'Timestamp', | ||
| dateRange: [new Date('2025-01-01'), new Date('2025-01-02')], | ||
| orderBy: 'Id', | ||
| }; | ||
| const sql = parameterizedQueryToSql( | ||
| await renderChartConfig(config, metadata, undefined), | ||
| ); | ||
| const result = await client.query({ query: sql, format: 'JSONEachRow' }); | ||
| const rows = await result.json<{ Id: number }>(); | ||
| return rows.map(r => Number(r.Id)); | ||
| }; | ||
|
|
||
| beforeAll(async () => { | ||
| const host = process.env.CLICKHOUSE_HOST || 'http://localhost:8123'; | ||
| const username = process.env.CLICKHOUSE_USER || 'default'; | ||
| const password = process.env.CLICKHOUSE_PASSWORD || ''; | ||
|
|
||
| client = createClient({ url: host, username, password }); | ||
| hdxClient = new HdxClickhouseClient({ host, username, password }); | ||
|
|
||
| await client.command({ | ||
| query: `CREATE OR REPLACE TABLE ${DATABASE}.${TABLE_NAME} ( | ||
| Timestamp DateTime64(9), | ||
| Id UInt32, | ||
| Severity Enum8('debug' = 1, 'info' = 5, 'error' = 9), | ||
| LogAttributes Map(LowCardinality(String), String), | ||
| GcPause String MATERIALIZED LogAttributes['gc_pause_ms'], | ||
| Code FixedString(8) | ||
| ) | ||
| ENGINE = MergeTree | ||
| ORDER BY (Timestamp)`, | ||
| }); | ||
|
|
||
| await client.insert({ | ||
| table: `${DATABASE}.${TABLE_NAME}`, | ||
| values: ROWS.map(r => ({ ...r, Timestamp: '2025-01-01 12:00:00' })), | ||
| format: 'JSONEachRow', | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| metadata = new Metadata(hdxClient, new MetadataCache()); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await client.command({ | ||
| query: `DROP TABLE IF EXISTS ${DATABASE}.${TABLE_NAME}`, | ||
| }); | ||
| await hdxClient.close(); | ||
| await client.close(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['LogAttributes.gc_pause_ms:[10 TO 100]', [2, 3, 4]], | ||
| ['LogAttributes.gc_pause_ms:{10 TO 100}', [3]], | ||
| ['LogAttributes.gc_pause_ms:[100 TO *]', [4, 5]], | ||
| // Missing (7) and non-numeric (6) values match a negated range. | ||
| ['-LogAttributes.gc_pause_ms:[10 TO 100]', [1, 5, 6, 7]], | ||
| // Comparisons stay lexical: '5', '17.25' and 'abc' sort after '100'. | ||
| ['LogAttributes.gc_pause_ms:>100', [1, 3, 5, 6]], | ||
| // A materialized String column and a FixedString compare the same way. | ||
| ['GcPause:[10 TO 100]', [2, 3, 4]], | ||
| ['Code:[10 TO 100]', [2, 3, 4]], | ||
| // Enum columns keep comparing by their numeric value. | ||
| ['Severity:[1 TO 5]', [1, 2, 4, 5, 6, 7]], | ||
| ])('%s returns the matching rows', async (where, ids) => { | ||
| expect(await queryIds(where)).toEqual(ids); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.