-
Notifications
You must be signed in to change notification settings - Fork 18.3k
feat(paired-t-test): migrate paired_ttest chart to v1 chart data API #41721
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
rusackas
merged 2 commits into
remove-legacy-viz-pipeline
from
legacy-viz-t2-paired-ttest
Jul 3, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
superset-frontend/plugins/legacy-plugin-chart-paired-t-test/src/buildQuery.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,59 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { | ||
| buildQueryContext, | ||
| ensureIsArray, | ||
| getMetricLabel, | ||
| QueryFormData, | ||
| QueryFormMetric, | ||
| QueryFormOrderBy, | ||
| } from '@superset-ui/core'; | ||
|
|
||
| /** | ||
| * Mirrors the legacy PairedTTestViz.query_obj: a timeseries query grouped | ||
| * by the group columns, selecting all metrics, with the sort metric | ||
| * appended to the select list and ordering applied when order_desc is set. | ||
| */ | ||
| export default function buildQuery(formData: QueryFormData) { | ||
| const { timeseries_limit_metric, order_desc } = formData; | ||
| return buildQueryContext(formData, baseQueryObject => { | ||
| let metrics: QueryFormMetric[] = ensureIsArray(baseQueryObject.metrics); | ||
| const orderby: QueryFormOrderBy[] = []; | ||
| const sortByMetric = ensureIsArray( | ||
| timeseries_limit_metric as QueryFormMetric | QueryFormMetric[], | ||
| )[0]; | ||
| if (sortByMetric) { | ||
| const sortByLabel = getMetricLabel(sortByMetric); | ||
| if (!metrics.some(metric => getMetricLabel(metric) === sortByLabel)) { | ||
| metrics = [...metrics, sortByMetric]; | ||
| } | ||
| if (order_desc) { | ||
| orderby.push([sortByMetric, !order_desc]); | ||
| } | ||
| } | ||
| return [ | ||
| { | ||
| ...baseQueryObject, | ||
| metrics, | ||
| is_timeseries: true, | ||
| orderby: orderby.length > 0 ? orderby : undefined, | ||
| }, | ||
| ]; | ||
| }); | ||
| } |
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
102 changes: 102 additions & 0 deletions
102
superset-frontend/plugins/legacy-plugin-chart-paired-t-test/src/transformData.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,102 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { DTTM_ALIAS } from '@superset-ui/core'; | ||
|
|
||
| export interface TTestSeries { | ||
| group: unknown[] | string; | ||
| values: { x: unknown; y: unknown }[]; | ||
| } | ||
|
|
||
| export type TTestData = Record<string, TTestSeries[]>; | ||
|
|
||
| /** | ||
| * Ports the legacy PairedTTestViz.get_data reshape: pivot the timeseries | ||
| * records on the timestamp with one series per (metric, group tuple), | ||
| * padding missing timestamps with null the way pandas pivot_table did. | ||
| */ | ||
| export default function transformData( | ||
| records: Record<string, unknown>[], | ||
| groupbyLabels: string[], | ||
| metricLabels: string[], | ||
| ): TTestData { | ||
| const timestamps = Array.from( | ||
| new Set(records.map(record => record[DTTM_ALIAS])), | ||
| ).sort((a, b) => { | ||
| if (a === b) return 0; | ||
| return (a as number) < (b as number) ? -1 : 1; | ||
| }); | ||
|
|
||
| const hasGroup = groupbyLabels.length > 0; | ||
| const groupKeyOf = (record: Record<string, unknown>) => | ||
| JSON.stringify(groupbyLabels.map(label => record[label])); | ||
|
|
||
| // groupKey -> tuple of group values, sorted like pandas pivot columns | ||
| const groupTuples = new Map<string, unknown[]>(); | ||
| // metric -> groupKey -> timestamp -> value | ||
| const values = new Map<string, Map<string, Map<unknown, unknown>>>(); | ||
| metricLabels.forEach(metric => values.set(metric, new Map())); | ||
|
|
||
| records.forEach(record => { | ||
| const groupKey = groupKeyOf(record); | ||
| if (!groupTuples.has(groupKey)) { | ||
| groupTuples.set( | ||
| groupKey, | ||
| groupbyLabels.map(label => record[label]), | ||
| ); | ||
| } | ||
| metricLabels.forEach(metric => { | ||
| const byGroup = values.get(metric)!; | ||
| if (!byGroup.has(groupKey)) { | ||
| byGroup.set(groupKey, new Map()); | ||
| } | ||
| byGroup.get(groupKey)!.set(record[DTTM_ALIAS], record[metric]); | ||
| }); | ||
| }); | ||
|
|
||
| // element-wise tuple comparison so numeric groups sort numerically, | ||
| // matching the pandas pivot column ordering | ||
| const compareTuples = (a: unknown[], b: unknown[]) => { | ||
| for (let i = 0; i < Math.min(a.length, b.length); i += 1) { | ||
| if (a[i] !== b[i]) { | ||
| if (typeof a[i] === 'number' && typeof b[i] === 'number') { | ||
| return (a[i] as number) - (b[i] as number); | ||
| } | ||
| return String(a[i]) < String(b[i]) ? -1 : 1; | ||
| } | ||
| } | ||
| return a.length - b.length; | ||
| }; | ||
| const sortedGroupKeys = Array.from(groupTuples.entries()) | ||
| .sort(([, a], [, b]) => compareTuples(a, b)) | ||
| .map(([key]) => key); | ||
|
|
||
| const data: TTestData = {}; | ||
| metricLabels.forEach(metric => { | ||
| const byGroup = values.get(metric)!; | ||
| const seriesKeys = hasGroup ? sortedGroupKeys : [groupKeyOf({})]; | ||
| data[metric] = seriesKeys.map(groupKey => ({ | ||
| group: hasGroup ? groupTuples.get(groupKey)! : 'All', | ||
| values: timestamps.map(timestamp => ({ | ||
| x: timestamp, | ||
| y: byGroup.get(groupKey)?.get(timestamp) ?? null, | ||
| })), | ||
| })); | ||
| }); | ||
| return data; | ||
| } |
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
56 changes: 56 additions & 0 deletions
56
superset-frontend/plugins/legacy-plugin-chart-paired-t-test/test/buildQuery.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,56 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { QueryFormData } from '@superset-ui/core'; | ||
| import buildQuery from '../src/buildQuery'; | ||
|
|
||
| const formData: QueryFormData = { | ||
| datasource: '5__table', | ||
| granularity_sqla: 'ds', | ||
| time_grain_sqla: 'P1D', | ||
| time_range: 'No filter', | ||
| viz_type: 'paired_ttest', | ||
| groupby: ['gender'], | ||
| metrics: ['sum__num'], | ||
| }; | ||
|
|
||
| test('builds a timeseries query grouped by the group columns', () => { | ||
| const [query] = buildQuery(formData).queries; | ||
| expect(query.columns).toEqual(['gender']); | ||
| expect(query.metrics).toEqual(['sum__num']); | ||
| expect(query.is_timeseries).toBe(true); | ||
| expect(query.granularity).toEqual('ds'); | ||
| }); | ||
|
|
||
| test('appends the sort metric and orders when order_desc is set', () => { | ||
| const [query] = buildQuery({ | ||
| ...formData, | ||
| timeseries_limit_metric: 'count', | ||
| order_desc: true, | ||
| }).queries; | ||
| expect(query.metrics).toEqual(['sum__num', 'count']); | ||
| expect(query.orderby).toEqual([['count', false]]); | ||
| }); | ||
|
|
||
| test('ignores residual order_by_cols from other viz types', () => { | ||
| const [query] = buildQuery({ | ||
| ...formData, | ||
| order_by_cols: ['["count", false]'], | ||
| }).queries; | ||
| expect(query.orderby).toBeUndefined(); | ||
| }); |
112 changes: 112 additions & 0 deletions
112
superset-frontend/plugins/legacy-plugin-chart-paired-t-test/test/transformData.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,112 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import transformData from '../src/transformData'; | ||
|
|
||
| const t1 = 1704067200000; | ||
| const t2 = 1704153600000; | ||
|
|
||
| test('nests records per metric and group with null padding', () => { | ||
| const data = transformData( | ||
| [ | ||
| { __timestamp: t1, gender: 'boy', sum__num: 10 }, | ||
| { __timestamp: t2, gender: 'boy', sum__num: 20 }, | ||
| { __timestamp: t1, gender: 'girl', sum__num: 30 }, | ||
| // girl is missing at t2 -> padded with null | ||
| ], | ||
| ['gender'], | ||
| ['sum__num'], | ||
| ); | ||
| expect(Object.keys(data)).toEqual(['sum__num']); | ||
| expect(data.sum__num).toEqual([ | ||
| { | ||
| group: ['boy'], | ||
| values: [ | ||
| { x: t1, y: 10 }, | ||
| { x: t2, y: 20 }, | ||
| ], | ||
| }, | ||
| { | ||
| group: ['girl'], | ||
| values: [ | ||
| { x: t1, y: 30 }, | ||
| { x: t2, y: null }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('uses the All group when no groupby is set', () => { | ||
| const data = transformData( | ||
| [ | ||
| { __timestamp: t1, sum__num: 1 }, | ||
| { __timestamp: t2, sum__num: 2 }, | ||
| ], | ||
| [], | ||
| ['sum__num'], | ||
| ); | ||
| expect(data.sum__num).toEqual([ | ||
| { | ||
| group: 'All', | ||
| values: [ | ||
| { x: t1, y: 1 }, | ||
| { x: t2, y: 2 }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('handles multiple metrics and multi-column groups', () => { | ||
| const data = transformData( | ||
| [ | ||
| { __timestamp: t1, gender: 'boy', state: 'CA', sum__num: 1, count: 5 }, | ||
| { __timestamp: t1, gender: 'girl', state: 'NY', sum__num: 2, count: 6 }, | ||
| ], | ||
| ['gender', 'state'], | ||
| ['sum__num', 'count'], | ||
| ); | ||
| expect(Object.keys(data).sort()).toEqual(['count', 'sum__num']); | ||
| expect(data.count).toEqual([ | ||
| { group: ['boy', 'CA'], values: [{ x: t1, y: 5 }] }, | ||
| { group: ['girl', 'NY'], values: [{ x: t1, y: 6 }] }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('sorts numeric groups numerically like pandas pivot columns', () => { | ||
| const data = transformData( | ||
| [ | ||
| { __timestamp: t1, decade: 10, sum__num: 1 }, | ||
| { __timestamp: t1, decade: 2, sum__num: 2 }, | ||
| ], | ||
| ['decade'], | ||
| ['sum__num'], | ||
| ); | ||
| expect(data.sum__num.map(series => series.group)).toEqual([[2], [10]]); | ||
| }); | ||
|
|
||
| test('sorts timestamps ascending like the pandas pivot index', () => { | ||
| const data = transformData( | ||
| [ | ||
| { __timestamp: t2, sum__num: 2 }, | ||
| { __timestamp: t1, sum__num: 1 }, | ||
| ], | ||
| [], | ||
| ['sum__num'], | ||
| ); | ||
| expect(data.sum__num[0].values.map(v => v.x)).toEqual([t1, t2]); | ||
| }); |
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.