LadyForest commented on code in PR #24630:
URL: https://github.com/apache/flink/pull/24630#discussion_r1570355268


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/DescribeCatalogOperation.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.table.operations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.CatalogDescriptor;
+import org.apache.flink.table.catalog.CommonCatalogOptions;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.utils.EncodingUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.flink.table.api.internal.TableResultUtils.buildTableResult;
+
+/** Operation to describe a DESCRIBE CATALOG catalog_name statement. */
+@Internal
+public class DescribeCatalogOperation implements Operation, 
ExecutableOperation {
+
+    private final String catalogName;
+    private final boolean isExtended;
+
+    public DescribeCatalogOperation(String catalogName, boolean isExtended) {
+        this.catalogName = catalogName;
+        this.isExtended = isExtended;
+    }
+
+    public String getCatalogName() {
+        return catalogName;
+    }
+
+    public boolean isExtended() {
+        return isExtended;
+    }
+
+    @Override
+    public String asSummaryString() {
+        Map<String, Object> params = new LinkedHashMap<>();
+        params.put("identifier", catalogName);
+        params.put("isExtended", isExtended);
+        return OperationUtils.formatWithChildren(
+                "DESCRIBE CATALOG", params, Collections.emptyList(), 
Operation::asSummaryString);
+    }
+
+    @Override
+    public TableResultInternal execute(Context ctx) {
+        CatalogDescriptor catalogDescriptor =
+                ctx.getCatalogManager()
+                        .getCatalogDescriptor(catalogName)
+                        .orElseThrow(
+                                () ->
+                                        new ValidationException(
+                                                String.format(
+                                                        "Cannot obtain 
metadata information from Catalog %s.",
+                                                        catalogName)));
+        Map<String, String> properties = 
catalogDescriptor.getConfiguration().toMap();
+        List<List<Object>> rows =
+                new ArrayList<>(
+                        Arrays.asList(
+                                Arrays.asList("Name", catalogName),
+                                Arrays.asList(
+                                        "Type",
+                                        properties.getOrDefault(
+                                                
CommonCatalogOptions.CATALOG_TYPE.key(), "")),
+                                Arrays.asList("Comment", "") // TODO: retain 
for future needs
+                                ));
+        if (isExtended) {
+            rows.add(Arrays.asList("Properties", 
convertPropertiesToString(properties)));
+        }
+
+        return buildTableResult(
+                Arrays.asList("catalog_description_item", 
"catalog_description_value")
+                        .toArray(new String[0]),
+                Arrays.asList(DataTypes.STRING(), 
DataTypes.STRING()).toArray(new DataType[0]),
+                rows.stream().map(List::toArray).toArray(Object[][]::new));
+    }
+
+    private String convertPropertiesToString(Map<String, String> map) {
+        StringBuilder stringBuilder = new StringBuilder();
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            stringBuilder.append(
+                    String.format(
+                            "('%s','%s'), ",
+                            EncodingUtils.escapeSingleQuotes(entry.getKey()),
+                            
EncodingUtils.escapeSingleQuotes(entry.getValue())));
+        }
+        // remove the last unnecessary comma and space
+        stringBuilder.delete(stringBuilder.length() - 2, 
stringBuilder.length());
+        return stringBuilder.toString();

Review Comment:
   Nit: can be simplified as
   
   ```java
   return map.entrySet().stream()
               .map(entry -> String.format("('%s','%s')",
                       EncodingUtils.escapeSingleQuotes(entry.getKey()),
                       EncodingUtils.escapeSingleQuotes(entry.getValue())))
               .collect(Collectors.joining(", "));
   ```



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/DescribeCatalogOperation.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.table.operations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.api.internal.TableResultInternal;
+import org.apache.flink.table.catalog.CatalogDescriptor;
+import org.apache.flink.table.catalog.CommonCatalogOptions;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.utils.EncodingUtils;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.flink.table.api.internal.TableResultUtils.buildTableResult;
+
+/** Operation to describe a DESCRIBE CATALOG catalog_name statement. */
+@Internal
+public class DescribeCatalogOperation implements Operation, 
ExecutableOperation {
+
+    private final String catalogName;
+    private final boolean isExtended;
+
+    public DescribeCatalogOperation(String catalogName, boolean isExtended) {
+        this.catalogName = catalogName;
+        this.isExtended = isExtended;
+    }
+
+    public String getCatalogName() {
+        return catalogName;
+    }
+
+    public boolean isExtended() {
+        return isExtended;
+    }
+
+    @Override
+    public String asSummaryString() {
+        Map<String, Object> params = new LinkedHashMap<>();
+        params.put("identifier", catalogName);
+        params.put("isExtended", isExtended);
+        return OperationUtils.formatWithChildren(
+                "DESCRIBE CATALOG", params, Collections.emptyList(), 
Operation::asSummaryString);
+    }
+
+    @Override
+    public TableResultInternal execute(Context ctx) {
+        CatalogDescriptor catalogDescriptor =
+                ctx.getCatalogManager()
+                        .getCatalogDescriptor(catalogName)
+                        .orElseThrow(
+                                () ->
+                                        new ValidationException(
+                                                String.format(
+                                                        "Cannot obtain 
metadata information from Catalog %s.",
+                                                        catalogName)));
+        Map<String, String> properties = 
catalogDescriptor.getConfiguration().toMap();
+        List<List<Object>> rows =
+                new ArrayList<>(
+                        Arrays.asList(
+                                Arrays.asList("Name", catalogName),
+                                Arrays.asList(
+                                        "Type",
+                                        properties.getOrDefault(
+                                                
CommonCatalogOptions.CATALOG_TYPE.key(), "")),
+                                Arrays.asList("Comment", "") // TODO: retain 
for future needs
+                                ));
+        if (isExtended) {
+            rows.add(Arrays.asList("Properties", 
convertPropertiesToString(properties)));
+        }
+
+        return buildTableResult(
+                Arrays.asList("catalog_description_item", 
"catalog_description_value")

Review Comment:
   I understand that Spark inspired this naming style, but considering Flink's 
own style, we have not introduced snake case naming convention for result 
display.
   
   Take Flink's `show table` command, for example.
   ```sql
   show tables;
   +------------+
   | table name |
   +------------+
   |   MyTable5 |
   |   MyTable6 |
   |    MyView5 |
   |    MyView6 |
   +------------+
   4 rows in set
   !ok
   ```
   
   On the other hand, I think Spark itself still does not use this schema.
   
   E.g. `describe catalog` command from Spark
   ```sql
   > DESCRIBE CATALOG main;
    info_name     info_value
    ------------  ------------------------------------
    Catalog Name                                  main
         Comment           Main catalog (auto-created)
           Owner                 metastore-admin-users
    Catalog Type                               Regular
   
   > DESCRIBE CATALOG EXTENDED main;
    info_name     info_value
    ------------  ------------------------------------
    Catalog Name                                  main
         Comment  This is a reserved catalog in Spark.
         Comment           Main catalog (auto-created)
           Owner                 metastore-admin-users
    Catalog Type                               Regular
      Created By
      Created At
      Updated By
      Updated At
   ```
   
   Actually, this schema is from Spark's `describe database`
   ```sql
   DESCRIBE DATABASE employees;
     +-------------------------+-----------------------------+
     |database_description_item|database_description_value   |
     +-------------------------+-----------------------------+
     |Database Name            |employees                    |
     |Description              |For software companies       |
     |Location                 |file:/Users/Temp/employees.db|
     +-------------------------+-----------------------------+
   ```
   
   Based on the above, I suggest that we draw inspiration from Spark for the 
schema names and consider maintaining consistency with Flink's naming style, 
using 'info name' and 'info value' as the schema. WDYT?
   
   Ref
   1. 
https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-aux-describe-catalog.html
   2. 
https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-aux-describe-database.html



-- 
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: issues-unsubscr...@flink.apache.org

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

Reply via email to