dtenedor commented on code in PR #41750:
URL: https://github.com/apache/spark/pull/41750#discussion_r1244647413
##########
python/pyspark/sql/tests/test_udtf.py:
##########
@@ -397,6 +397,138 @@ def test_udtf(a: int):
with self.assertRaisesRegex(TypeError, err_msg):
udtf(test_udtf, returnType="a: int")
+ def test_udtf_with_table_argument_query(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+ self.assertEqual(
+ self.spark.sql("SELECT * FROM test_udtf(TABLE (SELECT id FROM
range(0, 8)))").collect(),
+ [Row(a=6), Row(a=7)],
+ )
+
+ def test_udtf_with_table_argument_identifier(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+
+ with self.tempView("v"):
+ self.spark.sql("CREATE OR REPLACE TEMPORARY VIEW v as SELECT id
FROM range(0, 8)")
+ self.assertEqual(
+ self.spark.sql("SELECT * FROM test_udtf(TABLE v)").collect(),
+ [Row(a=6), Row(a=7)],
+ )
+
+ def test_udtf_with_table_argument_unknown_identifier(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+
+ with self.assertRaisesRegex(AnalysisException,
"TABLE_OR_VIEW_NOT_FOUND"):
+ self.spark.sql("SELECT * FROM test_udtf(TABLE v)").collect()
+
+ def test_udtf_with_table_argument_malformed_query(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+
+ with self.assertRaisesRegex(AnalysisException,
"TABLE_OR_VIEW_NOT_FOUND"):
+ self.spark.sql("SELECT * FROM test_udtf(TABLE (SELECT * FROM
v))").collect()
+
+ def test_udtf_with_table_argument_cte_inside(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+ self.assertEqual(
+ self.spark.sql(
+ """
+ SELECT * FROM test_udtf(TABLE (
+ WITH t AS (
+ SELECT id FROM range(0, 8)
+ )
+ SELECT * FROM t
+ ))
+ """
+ ).collect(),
+ [Row(a=6), Row(a=7)],
+ )
+
+ def test_udtf_with_table_argument_cte_outside(self):
+ class TestUDTF:
+ def eval(self, row: Row):
+ if row["id"] > 5:
+ yield row["id"],
+
+ func = udtf(TestUDTF, returnType="a: int")
+ self.spark.udtf.register("test_udtf", func)
+ self.assertEqual(
+ self.spark.sql(
+ """
+ WITH t AS (
+ SELECT id FROM range(0, 8)
+ )
+ SELECT * FROM test_udtf(TABLE (SELECT id FROM t))
+ """
+ ).collect(),
+ [Row(a=6), Row(a=7)],
+ )
+
+ self.assertEqual(
+ self.spark.sql(
+ """
+ WITH t AS (
+ SELECT id FROM range(0, 8)
+ )
+ SELECT * FROM test_udtf(TABLE t)
+ """
+ ).collect(),
+ [Row(a=6), Row(a=7)],
+ )
+
+ def test_udtf_with_table_argument_multiple(self):
+ class TestUDTF:
+ def eval(self, a: Row, b: Row):
+ yield a[0], b[0]
+
+ func = udtf(TestUDTF, returnType="a: int, b: int")
+ self.spark.udtf.register("test_udtf", func)
+ self.assertEqual(
+ self.spark.sql(
+ """
+ SELECT * FROM test_udtf(
+ TABLE (SELECT id FROM range(0, 2)),
+ TABLE (SELECT id FROM range(0, 3)))
Review Comment:
This is a bit weird. I guess we haven't defined any restriction that the
UDTF can't take two or more TABLE arguments, and receive the cartesian product
of all the rows of these tables. Can we create a SQL conf to decide whether we
return an error if there are > 1 TABLE argument, and test the conf in these
tests? I would imagine the conf would be true by default to restrict the number
of TABLE arguments to at most one, and then the behavior would match the SQL
standard. But then Spark users may decide which behavior they prefer as they
wish.
--
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]