forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allows X-Axis Sort By for custom SQL (apache#30393)
(cherry picked from commit abf2943)
- Loading branch information
1 parent
a3d6ef0
commit c864e6c
Showing
3 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
51 changes: 51 additions & 0 deletions
51
superset-frontend/packages/superset-ui-chart-controls/src/utils/isSortable.ts
This file contains 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,51 @@ | ||
/** | ||
* 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 { | ||
GenericDataType, | ||
getColumnLabel, | ||
isPhysicalColumn, | ||
QueryFormColumn, | ||
} from '@superset-ui/core'; | ||
import { checkColumnType, ControlStateMapping } from '..'; | ||
|
||
export function isSortable(controls: ControlStateMapping): boolean { | ||
const isForcedCategorical = | ||
checkColumnType( | ||
getColumnLabel(controls?.x_axis?.value as QueryFormColumn), | ||
controls?.datasource?.datasource, | ||
[GenericDataType.Numeric], | ||
) && !!controls?.xAxisForceCategorical?.value; | ||
|
||
const xAxisValue = controls?.x_axis?.value as QueryFormColumn; | ||
|
||
// Given that we don't know the type of a custom SQL column, | ||
// we treat it as sortable and give the responsibility to the | ||
// user to provide a sortable result. | ||
const isCustomSQL = !isPhysicalColumn(xAxisValue); | ||
|
||
return ( | ||
isForcedCategorical || | ||
isCustomSQL || | ||
checkColumnType( | ||
getColumnLabel(xAxisValue), | ||
controls?.datasource?.datasource, | ||
[GenericDataType.String, GenericDataType.Boolean], | ||
) | ||
); | ||
} |
70 changes: 70 additions & 0 deletions
70
superset-frontend/packages/superset-ui-chart-controls/test/utils/isSortable.test.ts
This file contains 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,70 @@ | ||
/** | ||
* 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 { ControlStateMapping } from '@superset-ui/chart-controls'; | ||
import { GenericDataType } from '@superset-ui/core'; | ||
import { isSortable } from '../../src/utils/isSortable'; | ||
|
||
const controls: ControlStateMapping = { | ||
datasource: { | ||
datasource: { | ||
columns: [ | ||
{ column_name: 'a', type_generic: GenericDataType.String }, | ||
{ column_name: 'b', type_generic: GenericDataType.Numeric }, | ||
{ column_name: 'c', type_generic: GenericDataType.Boolean }, | ||
], | ||
}, | ||
type: 'Select', | ||
}, | ||
}; | ||
|
||
test('should return true if the column is forced to be categorical', () => { | ||
const c: ControlStateMapping = { | ||
...controls, | ||
x_axis: { value: 'b', type: 'Select' }, | ||
xAxisForceCategorical: { value: true, type: 'Checkbox' }, | ||
}; | ||
expect(isSortable(c)).toBe(true); | ||
}); | ||
|
||
test('should return true if the column is a custom SQL column', () => { | ||
const c: ControlStateMapping = { | ||
...controls, | ||
x_axis: { | ||
value: { label: 'custom_sql', sqlExpression: 'MAX(ID)' }, | ||
type: 'Select', | ||
}, | ||
}; | ||
expect(isSortable(c)).toBe(true); | ||
}); | ||
|
||
test('should return true if the column type is String or Boolean', () => { | ||
const c: ControlStateMapping = { | ||
...controls, | ||
x_axis: { value: 'c', type: 'Checkbox' }, | ||
}; | ||
expect(isSortable(c)).toBe(true); | ||
}); | ||
|
||
test('should return false if none of the conditions are met', () => { | ||
const c: ControlStateMapping = { | ||
...controls, | ||
x_axis: { value: 'b', type: 'Input' }, | ||
}; | ||
expect(isSortable(c)).toBe(false); | ||
}); |