Hello everyone, I get a quick question about UDFs ...
# troubleshooting
x
Hello everyone, I get a quick question about UDFs in flink sql. Is it possible to parameterize functions in sql client? According to the doc, "For interactive sessions, it is also possible to parameterize functions before using or registering them.", and examples are using table API. I checked the syntax of
CREATE
in sql, and it seems like you can only define the name of UDFs. What I want to do is to initialize some objects at the start with parameters and access them in the
eval
without creating them everytime the function gets called. Do you have any advice on it? Maybe use
SET
or job parameters? Thank you in advance
d
One way is the use the Table API and then convert to SQL :
Copy code
// Define a UDF with parameters
public static class MyParameterizedUDF extends ScalarFunction {
    private final int param;

    public MyParameterizedUDF(int param) {
        this.param = param;
    }

    public Integer eval(Integer input) {
        return input + param;
    }
}

// In a TableEnvironment
TableEnvironment tableEnv = ...;
tableEnv.createTemporarySystemFunction("MyUDF", new MyParameterizedUDF(10)); // Parameter here

// Use in SQL
tableEnv.sqlQuery("SELECT MyUDF(column) FROM myTable");
Another approach could be to utilize Flink’s configuration parameters (
flink-conf.yaml
or passed programmatically) to set values that your UDF can access via
getRuntimeContext().getExecutionConfig().getGlobalJobParameters()
. This requires designing your UDF to fetch these parameters at runtime.
✅ 1
x
Thank you! I think I will go for the second approach as I am tring to build a SQL only experience for my coworkers in my use case