You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2024/09/25 22:08:34 UTC

(superset) branch master updated: fix: Allows X-Axis Sort By for custom SQL (#30393)

This is an automated email from the ASF dual-hosted git repository.

michaelsmolina pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new abf2943e4d fix: Allows X-Axis Sort By for custom SQL (#30393)
abf2943e4d is described below

commit abf2943e4d8910f486f738ada22cbd5da1f7487d
Author: Michael S. Molina <70...@users.noreply.github.com>
AuthorDate: Wed Sep 25 19:08:28 2024 -0300

    fix: Allows X-Axis Sort By for custom SQL (#30393)
---
 .../src/shared-controls/customControls.tsx         | 22 +------
 .../src/utils/isSortable.ts                        | 51 ++++++++++++++++
 .../test/utils/isSortable.test.ts                  | 70 ++++++++++++++++++++++
 3 files changed, 122 insertions(+), 21 deletions(-)

diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/customControls.tsx b/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/customControls.tsx
index 7fb4f9a8b9..d25273c08e 100644
--- a/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/customControls.tsx
+++ b/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/customControls.tsx
@@ -39,6 +39,7 @@ import {
   SORT_SERIES_CHOICES,
 } from '../constants';
 import { checkColumnType } from '../utils/checkColumnType';
+import { isSortable } from '../utils/isSortable';
 
 export const contributionModeControl = {
   name: 'contributionMode',
@@ -55,27 +56,6 @@ export const contributionModeControl = {
   },
 };
 
-function isForcedCategorical(controls: ControlStateMapping): boolean {
-  return (
-    checkColumnType(
-      getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
-      controls?.datasource?.datasource,
-      [GenericDataType.Numeric],
-    ) && !!controls?.xAxisForceCategorical?.value
-  );
-}
-
-function isSortable(controls: ControlStateMapping): boolean {
-  return (
-    isForcedCategorical(controls) ||
-    checkColumnType(
-      getColumnLabel(controls?.x_axis?.value as QueryFormColumn),
-      controls?.datasource?.datasource,
-      [GenericDataType.String, GenericDataType.Boolean],
-    )
-  );
-}
-
 const xAxisSortVisibility = ({ controls }: { controls: ControlStateMapping }) =>
   isSortable(controls) &&
   ensureIsArray(controls?.groupby?.value).length === 0 &&
diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/utils/isSortable.ts b/superset-frontend/packages/superset-ui-chart-controls/src/utils/isSortable.ts
new file mode 100644
index 0000000000..65b07ec91d
--- /dev/null
+++ b/superset-frontend/packages/superset-ui-chart-controls/src/utils/isSortable.ts
@@ -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],
+    )
+  );
+}
diff --git a/superset-frontend/packages/superset-ui-chart-controls/test/utils/isSortable.test.ts b/superset-frontend/packages/superset-ui-chart-controls/test/utils/isSortable.test.ts
new file mode 100644
index 0000000000..0ef9844f99
--- /dev/null
+++ b/superset-frontend/packages/superset-ui-chart-controls/test/utils/isSortable.test.ts
@@ -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);
+});