You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/03/24 11:36:13 UTC

[GitHub] [superset] villebro commented on a change in pull request #19319: feat: catalogs functionality (presto)

villebro commented on a change in pull request #19319:
URL: https://github.com/apache/superset/pull/19319#discussion_r834203967



##########
File path: superset-frontend/src/components/DatabaseSelector/utils.ts
##########
@@ -0,0 +1,24 @@
+/**
+ * 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 _ from 'lodash';
+import { DatabaseObject } from '.';
+
+export function isPrestoDatabase(database: DatabaseObject | undefined) {
+  return _.isEqual(database?.backend, 'presto');

Review comment:
       this seems unnecessarily heavy duty - something like this should be enough IMO:
   ```suggestion
     return database?.backend === 'presto';
   ```

##########
File path: superset-frontend/src/components/DatabaseSelector/utils.ts
##########
@@ -0,0 +1,24 @@
+/**
+ * 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 _ from 'lodash';
+import { DatabaseObject } from '.';
+
+export function isPrestoDatabase(database: DatabaseObject | undefined) {

Review comment:
       This needs to be made more generic. I would propose adding a property to `BaseEngineSpec` that defines whether or not catalogs are supported (defaults to `False`), add it to the `Database` schema and then set that to `True` for Presto, Trino and others that support it.

##########
File path: superset-frontend/src/components/TableSelector/index.tsx
##########
@@ -174,56 +185,73 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
     // reset selections
     if (database === undefined) {
       setCurrentDatabase(undefined);
+      setCurrentCatalog(undefined);
       setCurrentSchema(undefined);
       setCurrentTable(undefined);
     }
   }, [database]);
 
   useEffect(() => {
-    if (currentDatabase && currentSchema) {
+    if (shouldLoadTables(currentDatabase)) {
       setLoadingTables(true);
-      const encodedSchema = encodeURIComponent(currentSchema);
       const forceRefresh = refresh !== previousRefresh;
-      // TODO: Would be nice to add pagination in a follow-up. Needs endpoint changes.

Review comment:
       Would be nice to retain the TODO my moving it into the `getEndpoint` callback

##########
File path: superset-frontend/src/components/TableSelector/index.tsx
##########
@@ -174,56 +185,73 @@ const TableSelector: FunctionComponent<TableSelectorProps> = ({
     // reset selections
     if (database === undefined) {
       setCurrentDatabase(undefined);
+      setCurrentCatalog(undefined);
       setCurrentSchema(undefined);
       setCurrentTable(undefined);
     }
   }, [database]);
 
   useEffect(() => {
-    if (currentDatabase && currentSchema) {
+    if (shouldLoadTables(currentDatabase)) {
       setLoadingTables(true);
-      const encodedSchema = encodeURIComponent(currentSchema);
       const forceRefresh = refresh !== previousRefresh;
-      // TODO: Would be nice to add pagination in a follow-up. Needs endpoint changes.
-      const endpoint = encodeURI(
-        `/superset/tables/${currentDatabase.id}/${encodedSchema}/undefined/${forceRefresh}/`,
-      );
-
-      if (previousRefresh !== refresh) {
-        setPreviousRefresh(refresh);
-      }
-
-      SupersetClient.get({ endpoint })
-        .then(({ json }) => {
-          const options: TableOption[] = [];
-          let currentTable;
-          json.options.forEach((table: Table) => {
-            const option = {
-              value: table.value,
-              label: <TableOption table={table} />,
-              text: table.label,
-            };
-            options.push(option);
-            if (table.label === tableName) {
-              currentTable = option;
-            }
-          });
-
-          onTablesLoad?.(json.options);
-          setTableOptions(options);
-          setCurrentTable(currentTable);
-          setLoadingTables(false);
-          if (forceRefresh) addSuccessToast('List updated');
-        })
-        .catch(() => {
-          setLoadingTables(false);
-          handleError(t('There was an error loading the tables'));
-        });
+      const endpoint = getEndpoint({
+        forceRefresh,
+        schema: currentSchema || '',
+        databaseId: currentDatabase?.id || 0,
+      });
+      const encodedEndpoint = encodeURI(endpoint);
+      if (previousRefresh !== refresh) setPreviousRefresh(refresh);
+      fetchTables(encodedEndpoint, forceRefresh);
     }
-    // We are using the refresh state to re-trigger the query
-    // previousRefresh should be out of dependencies array
-    // eslint-disable-next-line react-hooks/exhaustive-deps
-  }, [currentDatabase, currentSchema, onTablesLoad, refresh]);
+  }, [refresh, currentDatabase, currentCatalog, currentSchema, onTablesLoad]);
+
+  function getEndpoint({
+    schema,
+    databaseId,
+    forceRefresh,
+  }: {
+    schema: string;
+    databaseId: number;
+    forceRefresh: boolean;
+  }) {
+    const encodedSchema = encodeURIComponent(schema);
+    return `/superset/tables/${databaseId}/${encodedSchema}/undefined/${forceRefresh}/`;
+  }

Review comment:
       While we're at it, it would be nice to wrap these in `useCallback`. Also, you should always use arrow functions in React components (applies to `shouldLoadTables` and other functions below).




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org