HyukjinKwon commented on a change in pull request #28087: 
[SPARK-31319][SQL][DOCS] Document UDFs/UDAFs in SQL Reference
URL: https://github.com/apache/spark/pull/28087#discussion_r407395777
 
 

 ##########
 File path: 
examples/src/main/java/org/apache/spark/examples/sql/JavaUserDefinedScalar.java
 ##########
 @@ -0,0 +1,98 @@
+/*
+ * 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.examples.sql;
+
+// $example on:udf_scalar$
+import org.apache.spark.sql.*;
+import org.apache.spark.sql.api.java.UDF1;
+import org.apache.spark.sql.expressions.UserDefinedFunction;
+import static org.apache.spark.sql.functions.udf;
+import org.apache.spark.sql.types.DataTypes;
+// $example off:udf_scalar$
+
+public class JavaUserDefinedScalar {
+
+  public static void main(String[] args) {
+
+    // $example on:udf_scalar$
+    SparkSession spark = SparkSession
+      .builder()
+      .appName("Java Spark SQL UDF scalar example")
+      .getOrCreate();
+
+    // Define and register a zero-argument non-deterministic UDF
+    // UDF is deterministic by default, i.e. produces the same result for the 
same input.
+    UserDefinedFunction random = udf(
+      () -> Math.random(), DataTypes.DoubleType
+    );
+    random.asNondeterministic();
+    spark.udf().register("random", random);
+    spark.sql("SELECT random()").show();
+    // +-------+
+    // |UDF()  |
+    // +-------+
+    // |xxxxxxx|
+    // +-------+
+
+    // Define and register a one-argument UDF
+    spark.udf().register("plusOne", new UDF1<Integer, Integer>() {
+      @Override
+      public Integer call(Integer x) {
+        return x + 1;
+      }
+    }, DataTypes.IntegerType);
+    spark.sql("SELECT plusOne(5)").show();
+    // +----------+
+    // |plusOne(5)|
+    // +----------+
+    // |         6|
+    // +----------+
+
+    // Define and register a two-argument UDF
+    UserDefinedFunction strLen = udf(
+      (String s, Integer x) -> s.length() + x, DataTypes.IntegerType
+    );
+    spark.udf().register("strLen", strLen);
+    spark.sql("SELECT strLen('test', 1)").show();
 
 Review comment:
   It should have been better probably if we added SQL example with UDAF too, 
e.g.)
   
   ```sql
   val agg = Aggregator(...)
   spark.udf.register("udaf_func", agg)
   ```
   
   which was added in SPARK-27296.
   
   The current example is only Scala/Java APIs there in 
`UserDefinedTypedAggregation.scala` and `JavaUserDefinedTypedAggregation.java`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to