aokolnychyi commented on code in PR #50109: URL: https://github.com/apache/spark/pull/50109#discussion_r1992411229
########## docs/sql-ref-ansi-compliance.md: ########## @@ -648,6 +648,7 @@ Below is a list of all the keywords in Spark SQL. |PRECEDING|non-reserved|non-reserved|non-reserved| |PRIMARY|reserved|non-reserved|reserved| |PRINCIPALS|non-reserved|non-reserved|non-reserved| +|PROCEDURES|reserved|non-reserved|non-reserved| Review Comment: Do we have examples of other databases where `PROCEDURES` is a reserved keyword? What does the SQL standard say? ########## sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowProceduresCommand.scala: ########## @@ -0,0 +1,54 @@ +/* + * 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.spark.sql.execution.command + +import org.apache.spark.sql.{Row, SparkSession} +import org.apache.spark.sql.catalyst.analysis.ResolvedNamespace +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ +import org.apache.spark.sql.types.StringType + +/** + * A command for users to get procedures. + * If a namespace is not given, the current namespace will be used. + * The syntax of using this command in SQL is: + * {{{ + * SHOW TABLES [(IN|FROM) namespace]] + * }}} + */ +case class ShowProceduresCommand( + child: LogicalPlan, + override val output: Seq[Attribute] = Seq( + AttributeReference("namespace", StringType, nullable = false)(), + AttributeReference("procedure_name", StringType, nullable = false)() + )) extends UnaryRunnableCommand { + + override def run(sparkSession: SparkSession): Seq[Row] = { + child match { + case ResolvedNamespace(catalog, ns, _) => + val procedureCatalog = catalog.asProcedureCatalog + val procedures = procedureCatalog.listProcedures(ns.toArray) + procedures.toSeq.map(p => Row(p.namespace().quoted, p.name())) Review Comment: Optional: Shall we omit `()` for `p.namespace.quoted` and `p.name` for consistency? ########## sql/catalyst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryTableCatalog.scala: ########## @@ -268,6 +268,11 @@ class InMemoryTableCatalog extends BasicInMemoryTableCatalog with SupportsNamesp procedure } + override def listProcedures(namespace: Array[String]): Array[Identifier] = { + procedures.asScala.filter{case (_, p) => !p.name().equals("dummy_increment")} Review Comment: Minor: Missing ` ` before `case (_, p)`? ########## sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/ProcedureCatalog.java: ########## @@ -34,4 +34,9 @@ public interface ProcedureCatalog extends CatalogPlugin { * @return the loaded unbound procedure */ UnboundProcedure loadProcedure(Identifier ident); + + /** + * List all procedures in the specified database. Review Comment: Minor: `database` -> `namespace`? ########## sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowProceduresCommand.scala: ########## @@ -0,0 +1,54 @@ +/* + * 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.spark.sql.execution.command + +import org.apache.spark.sql.{Row, SparkSession} +import org.apache.spark.sql.catalyst.analysis.ResolvedNamespace +import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ +import org.apache.spark.sql.types.StringType + +/** + * A command for users to get procedures. + * If a namespace is not given, the current namespace will be used. + * The syntax of using this command in SQL is: + * {{{ + * SHOW TABLES [(IN|FROM) namespace]] + * }}} + */ +case class ShowProceduresCommand( + child: LogicalPlan, + override val output: Seq[Attribute] = Seq( + AttributeReference("namespace", StringType, nullable = false)(), Review Comment: Shall we also include `Procedure$description`? ########## sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala: ########## @@ -348,6 +364,92 @@ class ProcedureSuite extends QueryTest with SharedSparkSession with BeforeAndAft } } + test("SPARK-51350: Impelement SHOW procedures") { + catalog.createProcedure(Identifier.of(Array("default"), "foo"), UnboundSum) + catalog.createProcedure(Identifier.of(Array("default"), "abc"), UnboundLongSum) + catalog.createProcedure(Identifier.of(Array("default"), "xyz"), UnboundComplexProcedure) + catalog.createProcedure(Identifier.of(Array("default"), "xxx"), UnboundStructProcedure) + + sql("USE cat") + + withDatabase("cat2.default") { + sql("CREATE DATABASE cat2.default") + + catalog("cat2").createProcedure(Identifier.of(Array("default"), "foo"), Review Comment: Optional: Arguments on separate lines? ``` catalog("cat2").createProcedure( Identifier.of(Array("default"), "foo"), UnboundVoidProcedure) ``` ########## sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala: ########## @@ -1179,4 +1179,13 @@ class SparkSqlAstBuilder extends AstBuilder { } } } + + override def visitShowProcedures(ctx: ShowProceduresContext): LogicalPlan = withOrigin(ctx) { + val ns = if (ctx.identifierReference() != null) { Review Comment: Minor: Why `()` at the end if not used on the line below? It seems like a simple getter where we can omit the parentheses. ########## sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/ProcedureCatalog.java: ########## @@ -34,4 +34,9 @@ public interface ProcedureCatalog extends CatalogPlugin { * @return the loaded unbound procedure */ UnboundProcedure loadProcedure(Identifier ident); + + /** + * List all procedures in the specified database. + */ + Identifier[] listProcedures(String[] namespace); Review Comment: Question: what's our story with using varargs in Java? I see we use `String[]` in `FunctionCatalog` and `String...` in `ViewCatalog`. Given that it will likely be called by Spark, I don't think it makes a difference. Let's just be consistent. ########## sql/core/src/test/scala/org/apache/spark/sql/connector/ProcedureSuite.scala: ########## @@ -348,6 +364,92 @@ class ProcedureSuite extends QueryTest with SharedSparkSession with BeforeAndAft } } + test("SPARK-51350: Impelement SHOW procedures") { + catalog.createProcedure(Identifier.of(Array("default"), "foo"), UnboundSum) + catalog.createProcedure(Identifier.of(Array("default"), "abc"), UnboundLongSum) + catalog.createProcedure(Identifier.of(Array("default"), "xyz"), UnboundComplexProcedure) + catalog.createProcedure(Identifier.of(Array("default"), "xxx"), UnboundStructProcedure) + + sql("USE cat") + + withDatabase("cat2.default") { + sql("CREATE DATABASE cat2.default") + + catalog("cat2").createProcedure(Identifier.of(Array("default"), "foo"), + UnboundVoidProcedure) + catalog("cat2").createProcedure(Identifier.of(Array("default"), "bar"), + UnboundMultiResultProcedure) + + checkAnswer( + sql("SHOW PROCEDURES"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES IN default"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES FROM default"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES IN main.default"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES FROM main.default"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES FROM cat2.default"), + Row("default", "foo") :: + Row("default", "bar") :: Nil) + + // Switch catalog. + sql("USE cat2") + + checkAnswer( + sql("SHOW PROCEDURES IN cat.default"), + Row("default", "abc") :: + Row("default", "foo") :: + Row("default", "xxx") :: + Row("default", "xyz") :: Nil) + + checkAnswer( + sql("SHOW PROCEDURES"), + Row("default", "bar") :: Review Comment: Optional: One line where fits? -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org