lincoln-lil commented on code in PR #25133:
URL: https://github.com/apache/flink/pull/25133#discussion_r1716809004


##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -1244,6 +1244,19 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
                     
.outputTypeStrategy(nullableIfArgs(explicit(DataTypes.STRING())))
                     .build();
 
+    public static final BuiltInFunctionDefinition PRINTF =
+            BuiltInFunctionDefinition.newBuilder()
+                    .name("PRINTF")
+                    .kind(SCALAR)
+                    .inputTypeStrategy(
+                            varyingSequence(
+                                    Arrays.asList("strfmt", "obj"),

Review Comment:
   strfmt -> format



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/PrintfFunction.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+
+import javax.annotation.Nullable;
+
+import java.util.Formatter;
+import java.util.Locale;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#PRINTF}. */
+@Internal
+public class PrintfFunction extends BuiltInScalarFunction {
+
+    public PrintfFunction(SpecializedFunction.SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.PRINTF, context);
+    }
+
+    public @Nullable StringData eval(@Nullable StringData strfmt, Object... 
obj) {
+        if (strfmt == null) {
+            return null;
+        }
+
+        StringBuilder strBuf = new StringBuilder();
+        Formatter formatter = new Formatter(strBuf, Locale.US);

Review Comment:
   `Formatter` is a `Closeable`, so we can use try-with-resource pattern here.



##########
docs/data/sql_functions.yml:
##########
@@ -260,6 +260,13 @@ string:
   - sql: POSITION(string1 IN string2)
     table: STRING1.position(STRING2)
     description: Returns the position (start from 1) of the first occurrence 
of STRING1 in STRING2; returns 0 if STRING1 cannot be found in STRING2.
+  - sql: PRINTF(strfmt, obj...)

Review Comment:
   strfmt -> format



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/PrintfFunction.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryStringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction;
+
+import javax.annotation.Nullable;
+
+import java.util.Formatter;
+import java.util.Locale;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#PRINTF}. */
+@Internal
+public class PrintfFunction extends BuiltInScalarFunction {
+
+    public PrintfFunction(SpecializedFunction.SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.PRINTF, context);
+    }
+
+    public @Nullable StringData eval(@Nullable StringData strfmt, Object... 
obj) {

Review Comment:
   strfmt ->  format



##########
docs/data/sql_functions.yml:
##########
@@ -260,6 +260,13 @@ string:
   - sql: POSITION(string1 IN string2)
     table: STRING1.position(STRING2)
     description: Returns the position (start from 1) of the first occurrence 
of STRING1 in STRING2; returns 0 if STRING1 cannot be found in STRING2.
+  - sql: PRINTF(strfmt, obj...)
+    table: strfmt.printf(obj...)
+    description: |
+      Returns a formatted string from printf-style format string.
+      The function exploits the java.util.Formatter class with Locale.US.
+      strfmt <CHAR | VARCHAR>, obj <ANY>
+      Returns a STRING representation of the formatted strfmt. `NULL` if 
strfmt is `NULL` or invalid.

Review Comment:
   Need to clarify what output is expected if a null object exists.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -1244,6 +1244,19 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
                     
.outputTypeStrategy(nullableIfArgs(explicit(DataTypes.STRING())))
                     .build();
 
+    public static final BuiltInFunctionDefinition PRINTF =
+            BuiltInFunctionDefinition.newBuilder()
+                    .name("PRINTF")
+                    .kind(SCALAR)
+                    .inputTypeStrategy(
+                            varyingSequence(
+                                    Arrays.asList("strfmt", "obj"),
+                                    Arrays.asList(
+                                            
logical(LogicalTypeFamily.CHARACTER_STRING), ANY)))
+                    .outputTypeStrategy(explicit(DataTypes.STRING()))

Review Comment:
   should be forcenullable



-- 
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