2021-07-28 08:01:43 -04:00
|
|
|
/**
|
|
|
|
* 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 { QueryObject, SqlaFormData } from '@superset-ui/core';
|
2022-06-05 09:17:31 -04:00
|
|
|
import { pivotOperator } from '@superset-ui/chart-controls';
|
2021-07-28 08:01:43 -04:00
|
|
|
|
|
|
|
const formData: SqlaFormData = {
|
2021-11-09 07:42:28 -05:00
|
|
|
metrics: [
|
|
|
|
'count(*)',
|
|
|
|
{ label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
|
|
|
|
],
|
2021-07-28 08:01:43 -04:00
|
|
|
time_range: '2015 : 2016',
|
|
|
|
granularity: 'month',
|
|
|
|
datasource: 'foo',
|
|
|
|
viz_type: 'table',
|
2022-07-05 04:09:16 -04:00
|
|
|
show_empty_columns: true,
|
2021-07-28 08:01:43 -04:00
|
|
|
};
|
|
|
|
const queryObject: QueryObject = {
|
2021-11-09 07:42:28 -05:00
|
|
|
metrics: [
|
|
|
|
'count(*)',
|
|
|
|
{ label: 'sum(val)', expressionType: 'SQL', sqlExpression: 'sum(val)' },
|
|
|
|
],
|
2021-07-28 08:01:43 -04:00
|
|
|
time_range: '2015 : 2016',
|
|
|
|
granularity: 'month',
|
|
|
|
post_processing: [
|
|
|
|
{
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['__timestamp'],
|
|
|
|
columns: ['nation'],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': {
|
|
|
|
operator: 'mean',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2021-09-20 09:55:08 -04:00
|
|
|
test('skip pivot', () => {
|
|
|
|
expect(pivotOperator(formData, queryObject)).toEqual(undefined);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
|
|
|
pivotOperator(formData, {
|
|
|
|
...queryObject,
|
|
|
|
metrics: [],
|
|
|
|
}),
|
|
|
|
).toEqual(undefined);
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2023-02-02 10:16:07 -05:00
|
|
|
test('pivot by __timestamp without columns', () => {
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(
|
2022-09-16 03:57:45 -04:00
|
|
|
pivotOperator(
|
|
|
|
{ ...formData, granularity_sqla: 'time_column' },
|
|
|
|
queryObject,
|
|
|
|
),
|
2021-11-09 07:42:28 -05:00
|
|
|
).toEqual({
|
2021-09-20 09:55:08 -04:00
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['__timestamp'],
|
|
|
|
columns: [],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
2021-07-28 08:01:43 -04:00
|
|
|
},
|
2021-09-20 09:55:08 -04:00
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
2021-07-28 08:01:43 -04:00
|
|
|
});
|
2021-09-20 09:55:08 -04:00
|
|
|
});
|
2021-07-28 08:01:43 -04:00
|
|
|
|
2023-02-02 10:16:07 -05:00
|
|
|
test('pivot by __timestamp with columns', () => {
|
2021-09-20 09:55:08 -04:00
|
|
|
expect(
|
2022-09-16 03:57:45 -04:00
|
|
|
pivotOperator(
|
|
|
|
{ ...formData, granularity_sqla: 'time_column' },
|
|
|
|
{
|
|
|
|
...queryObject,
|
|
|
|
columns: ['foo', 'bar'],
|
|
|
|
},
|
|
|
|
),
|
2021-09-20 09:55:08 -04:00
|
|
|
).toEqual({
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['__timestamp'],
|
|
|
|
columns: ['foo', 'bar'],
|
|
|
|
aggregates: {
|
2022-01-21 14:23:23 -05:00
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
|
|
|
},
|
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-02 10:16:07 -05:00
|
|
|
test('pivot by __timestamp with series_columns', () => {
|
|
|
|
expect(
|
|
|
|
pivotOperator(
|
|
|
|
{ ...formData, granularity_sqla: 'time_column' },
|
|
|
|
{
|
|
|
|
...queryObject,
|
|
|
|
series_columns: ['foo', 'bar'],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['__timestamp'],
|
|
|
|
columns: ['foo', 'bar'],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
|
|
|
},
|
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-21 14:23:23 -05:00
|
|
|
test('pivot by x_axis with groupby', () => {
|
|
|
|
expect(
|
|
|
|
pivotOperator(
|
|
|
|
{
|
|
|
|
...formData,
|
|
|
|
x_axis: 'baz',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...queryObject,
|
2023-02-02 10:16:07 -05:00
|
|
|
series_columns: ['foo', 'bar'],
|
2022-01-21 14:23:23 -05:00
|
|
|
},
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['baz'],
|
|
|
|
columns: ['foo', 'bar'],
|
|
|
|
aggregates: {
|
2021-09-20 09:55:08 -04:00
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
2021-07-28 08:01:43 -04:00
|
|
|
},
|
2021-09-20 09:55:08 -04:00
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
2021-07-28 08:01:43 -04:00
|
|
|
});
|
|
|
|
});
|
2022-05-13 09:29:51 -04:00
|
|
|
|
|
|
|
test('pivot by adhoc x_axis', () => {
|
|
|
|
expect(
|
|
|
|
pivotOperator(
|
|
|
|
{
|
|
|
|
...formData,
|
|
|
|
x_axis: {
|
|
|
|
label: 'my_case_expr',
|
|
|
|
expressionType: 'SQL',
|
2022-09-20 21:41:21 -04:00
|
|
|
sqlExpression: 'case when a = 1 then 1 else 0 end',
|
2022-05-13 09:29:51 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...queryObject,
|
2023-02-02 10:16:07 -05:00
|
|
|
series_columns: ['foo', 'bar'],
|
2022-05-13 09:29:51 -04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['my_case_expr'],
|
|
|
|
columns: ['foo', 'bar'],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
|
|
|
},
|
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2023-03-06 04:50:20 -05:00
|
|
|
|
|
|
|
test('pivot by x_axis with extra metrics', () => {
|
|
|
|
expect(
|
|
|
|
pivotOperator(
|
|
|
|
{
|
|
|
|
...formData,
|
|
|
|
x_axis: 'foo',
|
|
|
|
x_axis_sort: 'bar',
|
|
|
|
groupby: [],
|
|
|
|
timeseries_limit_metric: 'bar',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...queryObject,
|
|
|
|
series_columns: [],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
).toEqual({
|
|
|
|
operation: 'pivot',
|
|
|
|
options: {
|
|
|
|
index: ['foo'],
|
|
|
|
columns: [],
|
|
|
|
aggregates: {
|
|
|
|
'count(*)': { operator: 'mean' },
|
|
|
|
'sum(val)': { operator: 'mean' },
|
|
|
|
bar: { operator: 'mean' },
|
|
|
|
},
|
|
|
|
drop_missing_columns: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|