Skip to content

Commit 6691348

Browse files
authored
[FLINK-37446][table-api-java] Support named arguments in Table API
This closes #26282.
1 parent 932f17d commit 6691348

File tree

15 files changed

+436
-104
lines changed

15 files changed

+436
-104
lines changed

docs/content.zh/docs/dev/table/functions/systemFunctions.md

+39
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,42 @@ table
230230
{{< /tabs >}}
231231

232232
{{< top >}}
233+
234+
Named Arguments
235+
---------------------------------------
236+
237+
By default, values and expressions are mapped to a function's arguments based on the position in the function call,
238+
for example `f(42, true)`. All functions in both SQL and Table API support position-based arguments.
239+
240+
If the function declares a static signature, named arguments are available as a convenient alternative.
241+
The framework is able to reorder named arguments and consider optional arguments accordingly, before passing them
242+
into the function call. Thus, the order of arguments doesn't matter when calling a function and optional arguments
243+
don't have to be provided.
244+
245+
In `DESCRIBE FUNCTION` and documentation a static signature is indicated by the `=>` assignment operator,
246+
for example `f(left => INT, right => BOOLEAN)`. Note that not every function supports named arguments. Named
247+
arguments are not available for signatures that are overloaded, use varargs, or any other kind of input type strategy.
248+
User-defined functions with a single `eval()` method usually qualify for named arguments.
249+
250+
Named arguments can be used as shown below:
251+
252+
{{< tabs "902fe991-5fb9-4b17-ae99-f05cbd48b4dd" >}}
253+
{{< tab "SQL" >}}
254+
```text
255+
SELECT MyUdf(input => my_column, threshold => 42)
256+
```
257+
{{< /tab >}}
258+
{{< tab "Table API" >}}
259+
```java
260+
table.select(
261+
call(
262+
MyUdf.class,
263+
$("my_column").asArgument("input"),
264+
lit(42).asArgument("threshold")
265+
)
266+
);
267+
```
268+
{{< /tab >}}
269+
{{< /tabs >}}
270+
271+
{{< top >}}

docs/content/docs/dev/table/functions/systemFunctions.md

+39
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,42 @@ table \
235235
{{< /tabs >}}
236236

237237
{{< top >}}
238+
239+
Named Arguments
240+
---------------------------------------
241+
242+
By default, values and expressions are mapped to a function's arguments based on the position in the function call,
243+
for example `f(42, true)`. All functions in both SQL and Table API support position-based arguments.
244+
245+
If the function declares a static signature, named arguments are available as a convenient alternative.
246+
The framework is able to reorder named arguments and consider optional arguments accordingly, before passing them
247+
into the function call. Thus, the order of arguments doesn't matter when calling a function and optional arguments
248+
don't have to be provided.
249+
250+
In `DESCRIBE FUNCTION` and documentation a static signature is indicated by the `=>` assignment operator,
251+
for example `f(left => INT, right => BOOLEAN)`. Note that not every function supports named arguments. Named
252+
arguments are not available for signatures that are overloaded, use varargs, or any other kind of input type strategy.
253+
User-defined functions with a single `eval()` method usually qualify for named arguments.
254+
255+
Named arguments can be used as shown below:
256+
257+
{{< tabs "902fe991-5fb9-4b17-ae99-f05cbd48b4dd" >}}
258+
{{< tab "SQL" >}}
259+
```text
260+
SELECT MyUdf(input => my_column, threshold => 42)
261+
```
262+
{{< /tab >}}
263+
{{< tab "Table API" >}}
264+
```java
265+
table.select(
266+
call(
267+
MyUdf.class,
268+
$("my_column").asArgument("input"),
269+
lit(42).asArgument("threshold")
270+
)
271+
);
272+
```
273+
{{< /tab >}}
274+
{{< /tabs >}}
275+
276+
{{< top >}}

flink-python/pyflink/table/tests/test_expression_completeness.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def excluded_methods(cls):
5757
'dividedBy',
5858
'times',
5959
'mod',
60-
'power'
60+
'power',
61+
'asArgument',
6162
}
6263

6364
@classmethod

flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/BaseExpressions.java

+30
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,36 @@ public OutType as(String name, String... extraNames) {
250250
.toArray(Expression[]::new)));
251251
}
252252

253+
/**
254+
* Converts this expression into a named argument.
255+
*
256+
* <p>If the function declares a static signature (usually indicated by the "=>" assignment
257+
* operator), the framework is able to reorder named arguments and consider optional arguments
258+
* accordingly, before passing them into the function call.
259+
*
260+
* <p>Note: Not every function supports named arguments. Named arguments are not available for
261+
* signatures that are overloaded, use varargs, or any other kind of input type strategy.
262+
*
263+
* <p>Example:
264+
*
265+
* <pre>{@code
266+
* table.select(
267+
* Expressions.call(
268+
* "MyFunction",
269+
* $("my_column").asArgument("input"),
270+
* lit(42).asArgument("threshold")
271+
* )
272+
* )
273+
* }</pre>
274+
*/
275+
public OutType asArgument(String name) {
276+
return toApiSpecificExpression(
277+
ApiExpressionUtils.unresolvedCall(
278+
BuiltInFunctionDefinitions.ASSIGNMENT,
279+
ApiExpressionUtils.valueLiteral(name),
280+
toExpr()));
281+
}
282+
253283
/**
254284
* Returns an ARRAY that contains the elements from array1 that are not in array2. If no
255285
* elements remain after excluding the elements in array2 from array1, the function returns an

0 commit comments

Comments
 (0)