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


##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false
+)  extends TableValuedFunctionArgument {

Review Comment:
   nit: maybe style?
   ```suggestion
       private val isPartitioned: Boolean = false)
     extends TableValuedFunctionArgument {
   ```



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false
+)  extends TableValuedFunctionArgument {
+  import sparkSession.toRichColumn
+
+  @scala.annotation.varargs
+  def partitionBy(cols: Column*): TableArg = {
+    if (isPartitioned) {
+      throw new IllegalArgumentException(
+        "partitionBy() can only be specified once."
+      )
+    }
+    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,
+      isPartitioned = true
+    )
+  }
+
+  @scala.annotation.varargs
+  def orderBy(cols: Column*): TableArg = {
+    // Validate that partitionBy has been called before orderBy
+    if (expression.partitionByExpressions.isEmpty) {
+      throw new IllegalArgumentException(
+        "Please call partitionBy() before orderBy()."
+      )
+    }
+    val orderByExpressions = cols.map { col =>
+      col.expr match {
+        case sortOrder: SortOrder => sortOrder
+        case expr: Expression => SortOrder(expr, Ascending)
+      }
+    }
+    new TableArg(
+      expression.copy(orderByExpressions = orderByExpressions),
+      sparkSession,
+      isPartitioned)
+  }
+
+  def withSinglePartition(): TableArg = {
+    if (expression.partitionByExpressions.nonEmpty) {
+      throw new IllegalArgumentException(
+        "Cannot call withSinglePartition() after partitionBy() has been 
called."
+      )
+    }
+    new TableArg(
+      expression.copy(partitionByExpressions = Seq.empty, withSinglePartition 
= true),

Review Comment:
   nit: Do we need to set `partitionByExpressions` to empty here? Isn't it 
already empty by checking it above?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,73 @@ 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().partitionBy(df.key).orderBy(df.value)),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().partitionBy(df.key).orderBy(df.value.desc())),
+            [
+                Row(key=1, value="b"),
+                Row(key=1, value="a"),
+                Row(key=2, value="d"),
+                Row(key=2, value="c"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().withSinglePartition()),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+        )
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Cannot call withSinglePartition\(\) after partitionBy\(\) has 
been called",
+        ):
+            df.asTable().partitionBy(df.key).withSinglePartition()
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Cannot call partitionBy\(\) after withSinglePartition\(\) has 
been called",
+        ):
+            df.asTable().withSinglePartition().partitionBy(df.key)
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Please call partitionBy\(\) before orderBy\(\)",
+        ):
+            df.asTable().orderBy(df.key)
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"partitionBy\(\) can only be specified once.",
+        ):
+            df.asTable().partitionBy(df.key).partitionBy()

Review Comment:
   Could you add a test with `df.asTable().partitionBy().orderBy(...)` that 
should raise an exception?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,73 @@ 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().partitionBy(df.key).orderBy(df.value)),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().partitionBy(df.key).orderBy(df.value.desc())),
+            [
+                Row(key=1, value="b"),
+                Row(key=1, value="a"),
+                Row(key=2, value="d"),
+                Row(key=2, value="c"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().withSinglePartition()),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+        )
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Cannot call withSinglePartition\(\) after partitionBy\(\) has 
been called",
+        ):
+            df.asTable().partitionBy(df.key).withSinglePartition()
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Cannot call partitionBy\(\) after withSinglePartition\(\) has 
been called",
+        ):
+            df.asTable().withSinglePartition().partitionBy(df.key)
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"Please call partitionBy\(\) before orderBy\(\)",
+        ):
+            df.asTable().orderBy(df.key)
+
+        with self.assertRaisesRegex(
+            IllegalArgumentException,
+            r"partitionBy\(\) can only be specified once.",
+        ):
+            df.asTable().partitionBy(df.key).partitionBy()
+

Review Comment:
   Could you add tests with named arguments?



##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -1064,6 +1074,73 @@ 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().partitionBy(df.key).orderBy(df.value)),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().partitionBy(df.key).orderBy(df.value.desc())),
+            [
+                Row(key=1, value="b"),
+                Row(key=1, value="a"),
+                Row(key=2, value="d"),
+                Row(key=2, value="c"),
+            ],
+            checkRowOrder=True,
+        )
+
+        assertDataFrameEqual(
+            func(df.asTable().withSinglePartition()),
+            [
+                Row(key=1, value="a"),
+                Row(key=1, value="b"),
+                Row(key=2, value="c"),
+                Row(key=2, value="d"),
+            ],
+        )

Review Comment:
   Could you add a test with `df.asTable().withSinglePartition().orderBy(...)` 
that should pass?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false

Review Comment:
   nit: we don't need `private val` here?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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}
+
+

Review Comment:
   nit: extra line



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false
+)  extends TableValuedFunctionArgument {
+  import sparkSession.toRichColumn
+
+  @scala.annotation.varargs
+  def partitionBy(cols: Column*): TableArg = {
+    if (isPartitioned) {
+      throw new IllegalArgumentException(
+        "partitionBy() can only be specified once."
+      )
+    }
+    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,
+      isPartitioned = true
+    )
+  }
+
+  @scala.annotation.varargs
+  def orderBy(cols: Column*): TableArg = {
+    // Validate that partitionBy has been called before orderBy
+    if (expression.partitionByExpressions.isEmpty) {
+      throw new IllegalArgumentException(
+        "Please call partitionBy() before orderBy()."
+      )
+    }
+    val orderByExpressions = cols.map { col =>
+      col.expr match {
+        case sortOrder: SortOrder => sortOrder
+        case expr: Expression => SortOrder(expr, Ascending)
+      }
+    }
+    new TableArg(
+      expression.copy(orderByExpressions = orderByExpressions),
+      sparkSession,
+      isPartitioned)
+  }
+
+  def withSinglePartition(): TableArg = {
+    if (expression.partitionByExpressions.nonEmpty) {
+      throw new IllegalArgumentException(
+        "Cannot call withSinglePartition() after partitionBy() has been 
called."
+      )
+    }
+    new TableArg(
+      expression.copy(partitionByExpressions = Seq.empty, withSinglePartition 
= true),
+      sparkSession)

Review Comment:
   Why we don't need to set `isPartitioned = true` here?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false
+)  extends TableValuedFunctionArgument {
+  import sparkSession.toRichColumn
+
+  @scala.annotation.varargs
+  def partitionBy(cols: Column*): TableArg = {
+    if (isPartitioned) {
+      throw new IllegalArgumentException(
+        "partitionBy() can only be specified once."
+      )
+    }
+    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),

Review Comment:
   nit: Do we need to set `withSinglePartition` here?



##########
sql/core/src/main/scala/org/apache/spark/sql/TableArg.scala:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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(
+    private[sql] val expression: FunctionTableSubqueryArgumentExpression,
+    private val sparkSession: SparkSession,
+    private val isPartitioned: Boolean = false

Review Comment:
   I'm not sure what `isPartitioned` is for?



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