hackergin commented on code in PR #23130: URL: https://github.com/apache/flink/pull/23130#discussion_r1290381308
########## docs/content/docs/dev/table/procedures.md: ########## @@ -0,0 +1,544 @@ +--- +title: "Procedures" +is_beta: true +weight: 50 +type: docs +aliases: + - /dev/table/procedures.html +--- +<!-- +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. +--> + +# Procedures + +Flink Table API & SQL empowers users to perform data manipulation or administrative tasks with procedures. Procedures can run FLINK jobs with `StreamExecutionEnvironment` provided, which make procedures more powerful and flexible. + +## Implementation Guide + +To call a procedure, the procedure should be provided by a catalog. To provide procedures in a catalog, you must implement a procedure and then return the procedure Review Comment: ```suggestion To call a procedure, it must be available in a catalog. To provide procedures in a catalog, you need to implement the procedure and return it using the `Catalog.getProcedure(ObjectPath procedurePath)` method. The following steps will guide you on how to implement and provide a procedure in a catalog. ``` ########## docs/content/docs/dev/table/sql/call.md: ########## @@ -0,0 +1,118 @@ +--- +title: "CALL Statements" +weight: 19 +type: docs +aliases: +- /dev/table/sql/call.html +--- +<!-- +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. +--> + +# Call Statements + +`Call` statements are used to call a stored procedure which is usually provided to perform data manipulation or administrative tasks. + +<span class="label label-danger">Attention</span> Currently, `Call` statements require the procedure called to exist in the corresponding catalog. So, please make sure the procedure exists in the catalog. +If it doesn't exist, it'll throw an exception. You may need to refer to the doc of the catalog to see the available procedures. To implement an procedure, please refer to [Procedure]({{< ref "docs/dev/table/procedures" >}}). + +## Run a CALL statement + +{{< tabs "call statement" >}} + +{{< tab "Java" >}} + +CALL statements can be executed with the `executeSql()` method of the `TableEnvironment`. The `executeSql()` will call the procedure immediately, and return a `TableResult` instance which associates the procedure. + +The following examples show how to run a CALL statement in `TableEnvironment`. + +{{< /tab >}} +{{< tab "Scala" >}} + +CALL statements can be executed with the `executeSql()` method of the `TableEnvironment`. The `executeSql()` will call the procedure immediately, and return a `TableResult` instance which associates the procedure. + +The following examples show how to run a single CALL statement in `TableEnvironment`. +{{< /tab >}} +{{< tab "Python" >}} + +CALL statements can be executed with the `execute_sql()` method of the `TableEnvironment`. The `executeSql()` will call the procedure immediately, and return a `TableResult` instance which associates the procedure. + +The following examples show how to run a single CALL statement in `TableEnvironment`. + +{{< /tab >}} +{{< tab "SQL CLI" >}} + +CALL statements can be executed in [SQL CLI]({{< ref "docs/dev/table/sqlClient" >}}). + +The following examples show how to run a CALL statement in SQL CLI. Review Comment: ```suggestion The following examples show how to execute a CALL statement in SQL CLI. ``` ########## docs/content/docs/dev/table/procedures.md: ########## @@ -0,0 +1,544 @@ +--- +title: "Procedures" +is_beta: true +weight: 50 +type: docs +aliases: + - /dev/table/procedures.html +--- +<!-- +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. +--> + +# Procedures + +Flink Table API & SQL empowers users to perform data manipulation or administrative tasks with procedures. Procedures can run FLINK jobs with `StreamExecutionEnvironment` provided, which make procedures more powerful and flexible. + +## Implementation Guide + +To call a procedure, the procedure should be provided by a catalog. To provide procedures in a catalog, you must implement a procedure and then return the procedure +in method `Catalog.getProcedure(ObjectPath procedurePath)`. The following steps will guild how to implement a procedure and provide it in a catalog. + +### Procedure Class + +An implementation class must implement the interface `org.apache.flink.table.procedures.Procedure`. + +The class must be declared `public`, not `abstract`, and should be globally accessible. Thus, non-static inner or anonymous classes are not allowed. + +### Call Methods + +The interface doesn't provide any method,you have to define a method named `call` in which you can implement the logic of the procedure. +The methods must be declared `public` and take a well-defined set of arguments. + +Please note: + +* The first parameter of the method `call` should always be `ProcedureContext` which provides the method `getExecutionEnvironment` to get a `StreamExecutionEnvironment` to enable to run a Flink Job +* The return type should always be an array, like `int[]`, `String[]`, etc + +More detail can be found in the Java doc of the class `org.apache.flink.table.procedures.Procedure`. + +Regular JVM method calling semantics apply. Therefore, it is possible to: +- implement overloaded methods such as `call(ProcedureContext, Integer)` and `call(ProcedureContext, LocalDateTime)` +- use var-args such as `call(ProcedureContext, Integer...)` +- use object inheritance such as `call(ProcedureContext, Object)` that takes both `LocalDateTime` and `Integer` +- and combinations of the above such as `call(ProcedureContext, Object...)` that takes all kinds of arguments + +If you intend to implement procedures in Scala, please add the `scala.annotation.varargs` annotation in +case of variable arguments. Furthermore, it is recommended to use boxed primitives (e.g. `java.lang.Integer` +instead of `Int`) to support `NULL`. + +The following snippets shows an example of an overloaded procedure: + +{{< tabs "0819d780-3052-11ee-be56-0242ac120002" >}} +{{< tab "Java" >}} + +```java +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.table.procedure.ProcedureContext; +import org.apache.flink.table.procedures.Procedure; + +// procedure with overloaded call methods +public class GenerateSequenceProcedure implements Procedure { + + public long[] call(ProcedureContext context, int n) { + return generate(context.getExecutionEnvironment(), n); + } + + public long[] call(ProcedureContext context, String n) { + return generate(context.getExecutionEnvironment(), Integer.parseInt(n)); + } + + private long[] generate(StreamExecutionEnvironment env, int n) throws Exception { + long[] sequenceN = new long[n]; + int i = 0; + try (CloseableIterator<Long> result = env.fromSequence(0, n - 1).executeAndCollect()) { + while (result.hasNext()) { + sequenceN[i++] = result.next(); + } + } + return sequenceN; + } +} + +``` +{{< /tab >}} +{{< tab "Scala" >}} +```scala +import org.apache.flink.table.procedure.ProcedureContext +import org.apache.flink.table.procedures.Procedure +import scala.annotation.varargs + +// procedures with overloaded call methods +class GenerateSequenceProcedure extends Procedure { + + def call(context: ProcedureContext, a: Integer, b: Integer): Array[Integer] = { + Array(a + b) + } + + def call(context: ProcedureContext, a: String, b: String): Array[Integer] = { + Array(Integer.valueOf(a) + Integer.valueOf(b)) + } + + @varargs // generate var-args like Java + def eval(context: ProcedureContext, d: Double*): Array[Integer] = { + Array(d.sum.toInt) + } +} + +``` +{{< /tab >}} +{{< /tabs >}} + +### Type Inference +The table ecosystem (similar to the SQL standard) is a strongly typed API. Therefore, both procedure parameters and return types must be mapped to a [data type]({{< ref "docs/dev/table/types" >}}). + +From a logical perspective, the planner needs information about expected types, precision, and scale. From a JVM perspective, the planner needs information about how internal data structures are represented as JVM objects when calling a procedure. + +The logic for validating input arguments and deriving data types for both the parameters and the result of a procedure is summarized under the term _type inference_. + +Flink's procedures implement an automatic type inference extraction that derives data types from the procedure's class and its `call` methods via reflection. If this implicit reflective extraction approach is not successful, the extraction process can be supported by annotating affected parameters, classes, or methods with `@DataTypeHint` and `@ProcedureHint`. More examples on how to annotate procedures are shown below. + +Note: although the return type in `call` method must be array type `T[]`, if use `@DataTypeHint` to annotate the return type , it's actually expected to annotate the component type of the array type, which is actually `T`. Review Comment: ```suggestion Note: although the return type in `call` method must be array type `T[]`, if use `@DataTypeHint` to annotate the return type, it's actually expected to annotate the component type of the array type, which is actually `T`. ``` ########## docs/content/docs/dev/table/sql/call.md: ########## @@ -0,0 +1,118 @@ +--- +title: "CALL Statements" +weight: 19 +type: docs +aliases: +- /dev/table/sql/call.html +--- +<!-- +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. +--> + +# Call Statements + +`Call` statements are used to call a stored procedure which is usually provided to perform data manipulation or administrative tasks. + +<span class="label label-danger">Attention</span> Currently, `Call` statements require the procedure called to exist in the corresponding catalog. So, please make sure the procedure exists in the catalog. +If it doesn't exist, it'll throw an exception. You may need to refer to the doc of the catalog to see the available procedures. To implement an procedure, please refer to [Procedure]({{< ref "docs/dev/table/procedures" >}}). + +## Run a CALL statement + +{{< tabs "call statement" >}} + +{{< tab "Java" >}} + +CALL statements can be executed with the `executeSql()` method of the `TableEnvironment`. The `executeSql()` will call the procedure immediately, and return a `TableResult` instance which associates the procedure. + +The following examples show how to run a CALL statement in `TableEnvironment`. Review Comment: ```suggestion The following examples show how to execute a CALL statement in `TableEnvironment`. ``` -- 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