Skip to content

Commit

Permalink
feat: Add # of planned read rows
Browse files Browse the repository at this point in the history
Utilizes the EXPLAIN ESTIMATE feature and queries CH to return # of rows that will be potentially read.

Ref: HDX-1136
  • Loading branch information
teeohhem committed Dec 23, 2024
1 parent 83f9997 commit 90f3023
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
78 changes: 60 additions & 18 deletions packages/app/src/DBSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import WhereLanguageControlled from '@/components/WhereLanguageControlled';
import { IS_LOCAL_MODE } from '@/config';
import { DisplayType } from '@/DisplayType';
import { useQueriedChartConfig } from '@/hooks/useChartConfig';
import { useExplainQuery } from '@/hooks/useExplainQuery';
import { withAppNav } from '@/layout';
import {
ChartConfig,
Expand Down Expand Up @@ -151,6 +152,33 @@ function SearchTotalCount({
);
}

function SearchNumRows({
config,
enabled,
}: {
config: ChartConfigWithDateRange;
enabled: boolean;
}) {
const { data, isLoading, error } = useExplainQuery(config, {
enabled,
});

if (!enabled) {
return null;
}

const numRows = data?.[0]?.rows;
return (
<Text size="xs" c="gray.4" mb={4}>
{isLoading
? 'Read Rows ...'
: error || !numRows
? ''
: `Read Rows: ${numRows}`}
</Text>
);
}

function SaveSearchModal({
searchedConfig,
opened,
Expand Down Expand Up @@ -1006,26 +1034,40 @@ function DBSearchPage() {
)}
<div style={{ display: 'flex', flexDirection: 'column' }}>
{analysisMode === 'results' && (
<Box style={{ height: 120, minHeight: 120 }} p="xs" mb="md">
<Box
style={{ height: 140, minHeight: 140 }}
p="xs"
pb="md"
mb="md"
>
{chartConfig && (
<>
<SearchTotalCount
config={{
...chartConfig,
select: [
{
aggFn: 'count',
aggCondition: '',
valueExpression: '',
},
],
orderBy: undefined,
granularity: 'auto',
dateRange: searchedTimeRange,
displayType: DisplayType.StackedBar,
}}
queryKeyPrefix={QUERY_KEY_PREFIX}
/>
<Group justify="space-between" mb={4}>
<SearchTotalCount
config={{
...chartConfig,
select: [
{
aggFn: 'count',
aggCondition: '',
valueExpression: '',
},
],
orderBy: undefined,
granularity: 'auto',
dateRange: searchedTimeRange,
displayType: DisplayType.StackedBar,
}}
queryKeyPrefix={QUERY_KEY_PREFIX}
/>
<SearchNumRows
config={{
...chartConfig,
dateRange: searchedTimeRange,
}}
enabled={isReady}
/>
</Group>
<DBTimeChart
sourceId={searchedConfig.source ?? undefined}
showLegend={false}
Expand Down
32 changes: 32 additions & 0 deletions packages/app/src/hooks/useExplainQuery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query';

import { sendQuery } from '@/clickhouse';
import {
ChartConfigWithDateRange,
renderChartConfig,
} from '@/renderChartConfig';

export function useExplainQuery(
config: ChartConfigWithDateRange,
options?: Omit<UseQueryOptions<any>, 'queryKey' | 'queryFn'>,
) {
const { data, isLoading, error } = useQuery({
queryKey: ['explain', config],
queryFn: async ({ signal }) => {
const query = await renderChartConfig(config);
const response = await sendQuery<'JSONEachRow'>({
query: `EXPLAIN ESTIMATE ${query.sql}`,
query_params: query.params,
format: 'JSONEachRow',
abort_signal: signal,
connectionId: config.connection,
});
return response.json();
},
retry: false,
staleTime: 1000 * 60,
...options,
});

return { data, isLoading, error };
}

0 comments on commit 90f3023

Please sign in to comment.