ueshin commented on code in PR #49055:
URL: https://github.com/apache/spark/pull/49055#discussion_r1893025275


##########
python/pyspark/sql/udtf.py:
##########
@@ -372,22 +373,43 @@ def _create_judtf(self, func: Type) -> "JavaObject":
             )
         return judtf
 
-    def __call__(self, *args: "ColumnOrName", **kwargs: "ColumnOrName") -> 
"DataFrame":
+    def __call__(
+        self, *args: Union["ColumnOrName", "TableArg"], **kwargs: 
Union["ColumnOrName", "TableArg"]

Review Comment:
   The type hint should be `TableValuedFunctionArgument`?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.sql
+
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Expression, 
FunctionTableSubqueryArgumentExpression, SortOrder}
+
+
+class TableArg(
+    val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession
+)  extends TableValuedFunctionArgument {
+  import sparkSession.toRichColumn
+
+  def partitionBy(cols: Seq[Column]): TableArg = {

Review Comment:
   This should be: 
   ```scala
   @scala.annotation.varargs
   def partitionBy(cols: Column*): TableArg
   ```
   ?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,71 @@ def eval(self, row: Row):
         func = udtf(TestUDTF, returnType="a: int")
         return func
 
+    def test_df_asTable_chaining_methods(self):
+        class TestUDTF:
+            def eval(self, row: Row):
+                yield row["key"], row["value"]
+
+            def terminate(self):
+                if False:
+                    yield
+
+        func = udtf(TestUDTF, returnType="key: int, value: string")
+        df = self.spark.createDataFrame([(1, "a"), (1, "b"), (2, "c"), (2, 
"d")], ["key", "value"])
+        assertDataFrameEqual(
+            func(df.asTable().orderBy(df.value)),

Review Comment:
   Similarly, what happens if, e.g., 
`func(df.asTable().partitionBy(df.key).orderBy(df.value)).partitionBy()`?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,71 @@ def eval(self, row: Row):
         func = udtf(TestUDTF, returnType="a: int")
         return func
 
+    def test_df_asTable_chaining_methods(self):
+        class TestUDTF:
+            def eval(self, row: Row):
+                yield row["key"], row["value"]
+
+            def terminate(self):
+                if False:
+                    yield

Review Comment:
   What is this for?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,71 @@ def eval(self, row: Row):
         func = udtf(TestUDTF, returnType="a: int")
         return func
 
+    def test_df_asTable_chaining_methods(self):
+        class TestUDTF:
+            def eval(self, row: Row):
+                yield row["key"], row["value"]
+
+            def terminate(self):
+                if False:
+                    yield
+
+        func = udtf(TestUDTF, returnType="key: int, value: string")
+        df = self.spark.createDataFrame([(1, "a"), (1, "b"), (2, "c"), (2, 
"d")], ["key", "value"])
+        assertDataFrameEqual(
+            func(df.asTable().orderBy(df.value)),

Review Comment:
   Wha't the behavior of this? In SQL, `order by` only is not allowed, IIRC. cc 
@dtenedor 



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.sql
+
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Expression, 
FunctionTableSubqueryArgumentExpression, SortOrder}
+
+
+class TableArg(
+    val expression: FunctionTableSubqueryArgumentExpression,

Review Comment:
   It should be `private[sql] val` or something?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.sql
+
+import org.apache.spark.sql.catalyst.expressions.{Ascending, Expression, 
FunctionTableSubqueryArgumentExpression, SortOrder}
+
+
+class TableArg(
+    val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession
+)  extends TableValuedFunctionArgument {
+  import sparkSession.toRichColumn
+
+  def partitionBy(cols: Seq[Column]): TableArg = {
+    if (expression.withSinglePartition) {
+      throw new IllegalArgumentException(
+        "Cannot call partitionBy() after withSinglePartition() has been 
called."
+      )
+    }
+    val partitionByExpressions = cols.map(_.expr)
+    new TableArg(
+      expression.copy(
+        partitionByExpressions = partitionByExpressions, withSinglePartition = 
false),
+      sparkSession
+    )
+  }
+
+  def orderBy(cols: Seq[Column]): TableArg = {

Review Comment:
   ditto.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to