You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by vo...@apache.org on 2024/05/25 04:30:55 UTC
(druid) 01/11: more robust query naming
This is an automated email from the ASF dual-hosted git repository.
vogievetsky pushed a commit to branch more_explore_fixes
in repository https://gitbox.apache.org/repos/asf/druid.git
commit d1f40dde6229352f6230194a927e3835965dcf67
Author: Vadim Ogievetsky <va...@ogievetsky.com>
AuthorDate: Fri May 17 10:54:24 2024 -0700
more robust query naming
---
.../src/views/explore-view/explore-view.tsx | 16 +++-
.../explore-view/modules/table-react-module.tsx | 89 +++++++++++-----------
2 files changed, 59 insertions(+), 46 deletions(-)
diff --git a/web-console/src/views/explore-view/explore-view.tsx b/web-console/src/views/explore-view/explore-view.tsx
index cdc19d96d3e..376334c8443 100644
--- a/web-console/src/views/explore-view/explore-view.tsx
+++ b/web-console/src/views/explore-view/explore-view.tsx
@@ -18,7 +18,7 @@
import './modules';
-import { Menu, MenuItem } from '@blueprintjs/core';
+import { Intent, Menu, MenuItem } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import type { SqlExpression, SqlTable } from '@druid-toolkit/query';
import { C, L, sql, SqlLiteral, SqlQuery, T } from '@druid-toolkit/query';
@@ -28,11 +28,13 @@ import {
useParameterValues,
useSingleHost,
} from '@druid-toolkit/visuals-react';
+import copy from 'copy-to-clipboard';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { useStore } from 'zustand';
import { ShowValueDialog } from '../../dialogs/show-value-dialog/show-value-dialog';
import { useLocalStorageState, useQueryManager } from '../../hooks';
+import { AppToaster } from '../../singletons';
import { deepGet, filterMap, findMap, LocalStorageKeys, oneOf, queryDruidSql } from '../../utils';
import { ControlPane } from './control-pane/control-pane';
@@ -320,6 +322,18 @@ export const ExploreView = React.memo(function ExploreView() {
}}
moreMenu={
<Menu>
+ <MenuItem
+ icon={IconNames.DUPLICATE}
+ text="Copy last query"
+ disabled={!QUERY_HISTORY.length}
+ onClick={() => {
+ copy(QUERY_HISTORY[0]?.sqlQuery, { format: 'text/plain' });
+ AppToaster.show({
+ message: `Copied query to clipboard`,
+ intent: Intent.SUCCESS,
+ });
+ }}
+ />
<MenuItem
icon={IconNames.HISTORY}
text="Show query history"
diff --git a/web-console/src/views/explore-view/modules/table-react-module.tsx b/web-console/src/views/explore-view/modules/table-react-module.tsx
index fcc06a09784..e0f1f70db04 100644
--- a/web-console/src/views/explore-view/modules/table-react-module.tsx
+++ b/web-console/src/views/explore-view/modules/table-react-module.tsx
@@ -454,6 +454,34 @@ function TableModule(props: TableModuleProps) {
};
}
+ const makeCompareQuery = (compare: string) => {
+ return getInitQuery(table, shiftTimeInExpression(where, compare))
+ .applyIf(topValuesQuery, q =>
+ q.addInnerJoin(
+ T(TOP_VALUES_NAME),
+ getJoinCondition(splitColumns, T('t'), T(TOP_VALUES_NAME)),
+ ),
+ )
+ .applyForEach(splitColumns, (q, splitColumn) =>
+ q.addSelect(toGroupByExpression(splitColumn, timeBucket, compare), {
+ addToGroupBy: 'end',
+ }),
+ )
+ .applyIf(orderByCompareDuration === compare, q =>
+ q.applyForEach(showColumns, (q, showColumn) =>
+ q.addSelect(toShowColumnExpression(showColumn, multipleValueMode)),
+ ),
+ )
+ .applyForEach(metrics, (q, metric) =>
+ q.addSelect(metric.expression.as(`${metric.name}:cmp:${compare}:value`)),
+ )
+ .applyIf(compare === orderByCompareDuration && orderByCompareType === 'value', q =>
+ q
+ .changeOrderByExpression(effectiveOrderBy.changeExpression(C(orderByCompareMeasure!)))
+ .changeLimitValue(maxRows),
+ );
+ };
+
const main = T('main');
const leader = T(orderByCompareDuration ? `compare_${orderByCompareDuration}` : 'main');
const query = SqlQuery.from(leader)
@@ -465,36 +493,7 @@ function TableModule(props: TableModuleProps) {
).concat(
SqlWithPart.simple('main', mainQuery),
compares.map(compare =>
- SqlWithPart.simple(
- `compare_${compare}`,
- getInitQuery(table, shiftTimeInExpression(where, compare))
- .applyIf(topValuesQuery, q =>
- q.addInnerJoin(
- T(TOP_VALUES_NAME),
- getJoinCondition(splitColumns, T('t'), T(TOP_VALUES_NAME)),
- ),
- )
- .applyForEach(splitColumns, (q, splitColumn) =>
- q.addSelect(toGroupByExpression(splitColumn, timeBucket, compare), {
- addToGroupBy: 'end',
- }),
- )
- .applyIf(orderByCompareDuration === compare, q =>
- q.applyForEach(showColumns, (q, showColumn) =>
- q.addSelect(toShowColumnExpression(showColumn, multipleValueMode)),
- ),
- )
- .applyForEach(metrics, (q, metric) =>
- q.addSelect(metric.expression.as(metric.name)),
- )
- .applyIf(compare === orderByCompareDuration && orderByCompareType === 'value', q =>
- q
- .changeOrderByExpression(
- effectiveOrderBy.changeExpression(C(orderByCompareMeasure!)),
- )
- .changeLimitValue(maxRows),
- ),
- ),
+ SqlWithPart.simple(`compare_${compare}`, makeCompareQuery(compare)),
),
),
)
@@ -517,17 +516,17 @@ function TableModule(props: TableModuleProps) {
),
compares.flatMap(compare =>
metrics.flatMap(metric => {
- const c = T(`compare_${compare}`)
- .column(metric.name)
- .applyIf(NEEDS_GROUPING_TO_ORDER, anyValue)
- .applyIf(compare !== orderByCompareDuration, coalesce0);
-
const mainMetric = main
.column(metric.name)
.applyIf(NEEDS_GROUPING_TO_ORDER, anyValue)
.applyIf(orderByCompareDuration, coalesce0);
- const diff = mainMetric.subtract(c);
+ const cmp = T(`compare_${compare}`)
+ .column(`${metric.name}:cmp:${compare}:value`)
+ .applyIf(NEEDS_GROUPING_TO_ORDER, anyValue)
+ .applyIf(compare !== orderByCompareDuration, coalesce0);
+
+ const diff = mainMetric.subtract(cmp);
const ret: SqlExpression[] = [];
@@ -537,7 +536,7 @@ function TableModule(props: TableModuleProps) {
group: `Comparison to ${compare}`,
displayName: `${metric.name} (value)`,
});
- ret.push(c.as(valueName));
+ ret.push(cmp.as(valueName));
}
if (compareTypes.includes('delta')) {
@@ -550,12 +549,12 @@ function TableModule(props: TableModuleProps) {
}
if (compareTypes.includes('absDelta')) {
- const deltaName = `${metric.name}:cmp:${compare}:absDelta`;
- columnHints.set(deltaName, {
+ const absDeltaName = `${metric.name}:cmp:${compare}:absDelta`;
+ columnHints.set(absDeltaName, {
group: `Comparison to ${compare}`,
displayName: `${metric.name} (Abs. delta)`,
});
- ret.push(F('ABS', diff).as(deltaName));
+ ret.push(F('ABS', diff).as(absDeltaName));
}
if (compareTypes.includes('percent')) {
@@ -566,20 +565,20 @@ function TableModule(props: TableModuleProps) {
formatter: formatPercent,
});
ret.push(
- safeDivide0(diff.multiply(SqlLiteral.ONE_POINT_ZERO), c).as(percentName),
+ safeDivide0(diff.multiply(SqlLiteral.ONE_POINT_ZERO), cmp).as(percentName),
);
}
if (compareTypes.includes('absPercent')) {
- const percentName = `${metric.name}:cmp:${compare}:absPercent`;
- columnHints.set(percentName, {
+ const absPercentName = `${metric.name}:cmp:${compare}:absPercent`;
+ columnHints.set(absPercentName, {
group: `Comparison to ${compare}`,
displayName: `${metric.name} (abs. %)`,
formatter: formatPercent,
});
ret.push(
- F('ABS', safeDivide0(diff.multiply(SqlLiteral.ONE_POINT_ZERO), c)).as(
- percentName,
+ F('ABS', safeDivide0(diff.multiply(SqlLiteral.ONE_POINT_ZERO), cmp)).as(
+ absPercentName,
),
);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org