Skip to content

Commit b332faa

Browse files
committed
perf: Improve getKeyValues query performance for JSON keys
1 parent b806116 commit b332faa

File tree

2 files changed

+12
-71
lines changed

2 files changed

+12
-71
lines changed

packages/common-utils/src/__tests__/metadata.test.ts

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,6 @@ describe('Metadata', () => {
251251
],
252252
}),
253253
});
254-
255-
// Mock getColumns to return columns including materialized ones
256-
mockCache.getOrFetch.mockImplementation((key, queryFn) => {
257-
if (key.includes('.columns')) {
258-
return Promise.resolve([
259-
{ name: 'regular_column', type: 'String', default_type: '' },
260-
{
261-
name: 'materialized_column',
262-
type: 'String',
263-
default_type: 'MATERIALIZED',
264-
},
265-
{ name: 'default_column', type: 'String', default_type: 'DEFAULT' },
266-
]);
267-
}
268-
return queryFn();
269-
});
270254
});
271255

272256
it('should apply row limit when disableRowLimit is false', async () => {
@@ -355,54 +339,15 @@ describe('Metadata', () => {
355339
expect(result).toEqual([{ key: 'column1', value: ['value1', 'value2'] }]);
356340
});
357341

358-
it('should include materialized fields when selecting all columns', async () => {
342+
it('should fallback to * when no keys are provided', async () => {
359343
const renderChartConfigSpy = jest.spyOn(
360344
renderChartConfigModule,
361345
'renderChartConfig',
362346
);
363347

364348
await metadata.getKeyValues({
365349
chartConfig: mockChartConfig,
366-
keys: ['column1'],
367-
limit: 10,
368-
});
369-
370-
// Verify that renderChartConfig was called with the expanded select list
371-
// that includes all columns by name (including materialized ones)
372-
expect(renderChartConfigSpy).toHaveBeenCalledWith(
373-
expect.objectContaining({
374-
with: [
375-
expect.objectContaining({
376-
name: 'sampledData',
377-
chartConfig: expect.objectContaining({
378-
// Should expand to all column names instead of using '*'
379-
select:
380-
'`regular_column`, `materialized_column`, `default_column`',
381-
}),
382-
}),
383-
],
384-
}),
385-
metadata,
386-
);
387-
});
388-
389-
it('should fallback to * when no columns are found', async () => {
390-
// Mock getColumns to return empty array
391-
mockCache.getOrFetch.mockImplementation((key, queryFn) => {
392-
if (key.includes('.columns')) {
393-
return Promise.resolve([]);
394-
}
395-
return queryFn();
396-
});
397-
398-
const renderChartConfigSpy = jest.spyOn(
399-
renderChartConfigModule,
400-
'renderChartConfig',
401-
);
402-
403-
await metadata.getKeyValues({
404-
chartConfig: mockChartConfig,
405-
keys: ['column1'],
350+
keys: [],
406351
limit: 10,
407352
});
408353

packages/common-utils/src/metadata.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -668,29 +668,20 @@ export class Metadata {
668668
return this.cache.getOrFetch(
669669
`${chartConfig.connection}.${chartConfig.from.databaseName}.${chartConfig.from.tableName}.${keys.join(',')}.${chartConfig.dateRange.toString()}.${disableRowLimit}.values`,
670670
async () => {
671-
const selectClause = keys
672-
.map((k, i) => `groupUniqArray(${limit})(${k}) AS param${i}`)
673-
.join(', ');
674-
675671
// When disableRowLimit is true, query directly without CTE
676672
// Otherwise, use CTE with row limits for sampling
677673
const sqlConfig = disableRowLimit
678674
? {
679675
...chartConfig,
680-
select: selectClause,
676+
select: keys
677+
.map((k, i) => `groupUniqArray(${limit})(${k}) AS param${i}`)
678+
.join(', '),
681679
}
682680
: await (async () => {
683-
// Get all columns including materialized ones
684-
const columns = await this.getColumns({
685-
databaseName: chartConfig.from.databaseName,
686-
tableName: chartConfig.from.tableName,
687-
connectionId: chartConfig.connection,
688-
});
689-
690681
// Build select expression that includes all columns by name
691682
// This ensures materialized columns are included
692683
const selectExpr =
693-
columns.map(col => `\`${col.name}\``).join(', ') || '*';
684+
keys.map((k, i) => `${k} as param${i}`).join(', ') || '*';
694685

695686
return {
696687
with: [
@@ -710,7 +701,12 @@ export class Metadata {
710701
isSubquery: true,
711702
},
712703
],
713-
select: selectClause,
704+
select: keys
705+
.map(
706+
(_, i) =>
707+
`groupUniqArray(${limit})(param${i}) AS param${i}`,
708+
)
709+
.join(', '),
714710
connection: chartConfig.connection,
715711
from: { databaseName: '', tableName: 'sampledData' },
716712
where: '',

0 commit comments

Comments
 (0)