You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by am...@apache.org on 2021/05/13 06:50:59 UTC

[superset] 02/02: feat(native-filters): add sort metric to select (#14590)

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

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

commit 09e871eb634020dc5252d31870df6db39d4af06e
Author: Ville Brofeldt <33...@users.noreply.github.com>
AuthorDate: Wed May 12 23:28:45 2021 +0300

    feat(native-filters): add sort metric to select (#14590)
    
    (cherry picked from commit f1c32b95761343e50ad69c5cc05c0848d582aba4)
---
 .../filters/components/Select/buildQuery.test.ts   | 59 ++++++++++++++++++++++
 .../src/filters/components/Select/buildQuery.ts    |  1 +
 2 files changed, 60 insertions(+)

diff --git a/superset-frontend/src/filters/components/Select/buildQuery.test.ts b/superset-frontend/src/filters/components/Select/buildQuery.test.ts
new file mode 100644
index 0000000..967fee5
--- /dev/null
+++ b/superset-frontend/src/filters/components/Select/buildQuery.test.ts
@@ -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 buildQuery from './buildQuery';
+
+describe('Select buildQuery', () => {
+  const formData = {
+    datasource: '5__table',
+    groupby: ['my_col'],
+    viz_type: 'filter_select',
+    sortAscending: false,
+    sortMetric: undefined,
+    filters: undefined,
+    enableEmptyFilter: false,
+    inverseSelection: false,
+    multiSelect: false,
+    defaultToFirstItem: false,
+    height: 100,
+    width: 100,
+  };
+
+  it('should build a default query', () => {
+    const queryContext = buildQuery(formData);
+    expect(queryContext.queries.length).toEqual(1);
+    const [query] = queryContext.queries;
+    expect(query.groupby).toEqual(['my_col']);
+    expect(query.metrics).toEqual([]);
+    expect(query.apply_fetch_values_predicate).toEqual(true);
+    expect(query.orderby).toEqual([]);
+  });
+
+  it('should handle sort metric correctly', () => {
+    const queryContext = buildQuery({
+      ...formData,
+      sortMetric: 'my_metric',
+      sortAscending: false,
+    });
+    expect(queryContext.queries.length).toEqual(1);
+    const [query] = queryContext.queries;
+    expect(query.groupby).toEqual(['my_col']);
+    expect(query.metrics).toEqual(['my_metric']);
+    expect(query.orderby).toEqual([['my_metric', false]]);
+  });
+});
diff --git a/superset-frontend/src/filters/components/Select/buildQuery.ts b/superset-frontend/src/filters/components/Select/buildQuery.ts
index 27fe3fa..9960b1b 100644
--- a/superset-frontend/src/filters/components/Select/buildQuery.ts
+++ b/superset-frontend/src/filters/components/Select/buildQuery.ts
@@ -30,6 +30,7 @@ export default function buildQuery(formData: PluginFilterSelectQueryFormData) {
         ...baseQueryObject,
         apply_fetch_values_predicate: true,
         groupby: columns,
+        metrics: sortMetric ? [sortMetric] : [],
         filters: filters.concat(
           columns.map(column => ({ col: column, op: 'IS NOT NULL' })),
         ),