-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(tesseract): pre-aggregation table name corruption when one name is a prefix of another #11000
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
Merged
+100
−3
Merged
Changes from all commits
Commits
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
83 changes: 83 additions & 0 deletions
83
packages/cubejs-query-orchestrator/test/unit/ReplacePreAggregationTableNames.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,83 @@ | ||
| import { QueryCache } from '../../src'; | ||
| import type { PreAggTableToTempTableNames } from '../../src'; | ||
|
|
||
| describe('QueryCache.replacePreAggregationTableNames', () => { | ||
| test('replaces a single table name', () => { | ||
| const result = QueryCache.replacePreAggregationTableNames( | ||
| 'SELECT * FROM dev_pre_aggregations.orders_rollup', | ||
| [['dev_pre_aggregations.orders_rollup', { targetTableName: 'dev_pre_aggregations.orders_rollup_20250401_abc' }]], | ||
| ); | ||
| expect(result).toBe('SELECT * FROM dev_pre_aggregations.orders_rollup_20250401_abc'); | ||
| }); | ||
|
|
||
| test('does not corrupt names that are prefixes of other names (name1 vs name10)', () => { | ||
| const baseName = 'dev_pre_aggregations.orders_rollup'; | ||
| const entries: PreAggTableToTempTableNames[] = Array.from( | ||
| { length: 12 }, | ||
| (_, i): PreAggTableToTempTableNames => [ | ||
| `${baseName}${i}`, | ||
| { targetTableName: `(SELECT * FROM ${baseName}_20250401_part${i})` }, | ||
| ], | ||
| ); | ||
| const query = entries | ||
| .map(([tableName], i) => `SELECT * FROM ${tableName} AS "alias${i}"`) | ||
| .join(' UNION ALL '); | ||
|
|
||
| const result = QueryCache.replacePreAggregationTableNames(query, entries) as string; | ||
|
|
||
| entries.forEach(([, { targetTableName }], i) => { | ||
| expect(result).toContain(`${targetTableName} AS "alias${i}"`); | ||
| }); | ||
| // No stray suffix digits left behind, e.g. `...)0 AS "alias10"` | ||
| expect(result).not.toMatch(/\)\d+ AS/); | ||
| expect(result).not.toContain(`${baseName}10`); | ||
| }); | ||
|
|
||
| test('does not match source names inside already inserted target names', () => { | ||
| // Real-world target shape: tableName + '_' + versions, so the target | ||
| // of `rollup10` contains `rollup1` as a substring | ||
| const entries: PreAggTableToTempTableNames[] = [ | ||
| ['pa.rollup1', { targetTableName: 'pa.rollup1_aaa_bbb_111' }], | ||
| ['pa.rollup10', { targetTableName: 'pa.rollup10_ccc_ddd_222' }], | ||
| ]; | ||
| const result = QueryCache.replacePreAggregationTableNames( | ||
| 'SELECT * FROM pa.rollup10 JOIN pa.rollup1', | ||
| entries, | ||
| ); | ||
| expect(result).toBe('SELECT * FROM pa.rollup10_ccc_ddd_222 JOIN pa.rollup1_aaa_bbb_111'); | ||
| }); | ||
|
|
||
| test('keeps params and query options for QueryWithParams input', () => { | ||
| const result = QueryCache.replacePreAggregationTableNames( | ||
| ['SELECT * FROM dev_pre_aggregations.orders_rollup WHERE id = ?', ['1'], { external: true }], | ||
| [['dev_pre_aggregations.orders_rollup', { targetTableName: 'dev_pre_aggregations.orders_rollup_20250401_abc' }]], | ||
| ); | ||
| expect(result).toEqual([ | ||
| 'SELECT * FROM dev_pre_aggregations.orders_rollup_20250401_abc WHERE id = ?', | ||
| ['1'], | ||
| { external: true }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('returns query as is for empty replacements', () => { | ||
| const result = QueryCache.replacePreAggregationTableNames('SELECT 1', []); | ||
| expect(result).toBe('SELECT 1'); | ||
| }); | ||
|
|
||
| test('treats $ in target names literally', () => { | ||
| const result = QueryCache.replacePreAggregationTableNames( | ||
| 'SELECT * FROM pa.rollup', | ||
| [['pa.rollup', { targetTableName: 'pa.rollup_$&_$1' }]], | ||
| ); | ||
| expect(result).toBe('SELECT * FROM pa.rollup_$&_$1'); | ||
| }); | ||
|
|
||
| test('does not mutate the incoming array order', () => { | ||
| const entries: PreAggTableToTempTableNames[] = [ | ||
| ['name1', { targetTableName: 'target1' }], | ||
| ['name10', { targetTableName: 'target10' }], | ||
| ]; | ||
| QueryCache.replacePreAggregationTableNames('SELECT * FROM name1, name10', entries); | ||
| expect(entries.map(([tableName]) => tableName)).toEqual(['name1', 'name10']); | ||
| }); | ||
| }); |
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.