itholic commented on code in PR #45377: URL: https://github.com/apache/spark/pull/45377#discussion_r1553150727
########## python/pyspark/sql/tests/test_dataframe.py: ########## @@ -825,6 +828,231 @@ def test_duplicate_field_names(self): self.assertEqual(df.schema, schema) self.assertEqual(df.collect(), data) + def test_dataframe_error_context(self): + # SPARK-47274: Add more useful contexts for PySpark DataFrame API errors. + with self.sql_conf({"spark.sql.ansi.enabled": True}): + df = self.spark.range(10) + + # DataFrameQueryContext with pysparkCallSite - divide + with self.assertRaises(ArithmeticException) as pe: + df.withColumn("div_zero", df.id / 0).collect() + self.check_error( + exception=pe.exception, + error_class="DIVIDE_BY_ZERO", + message_parameters={"config": '"spark.sql.ansi.enabled"'}, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="divide", + ) + + # DataFrameQueryContext with pysparkCallSite - plus + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("plus_invalid_type", df.id + "string").collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="plus", + ) + + # DataFrameQueryContext with pysparkCallSite - minus + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("minus_invalid_type", df.id - "string").collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="minus", + ) + + # DataFrameQueryContext with pysparkCallSite - multiply + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("multiply_invalid_type", df.id * "string").collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="multiply", + ) + + # DataFrameQueryContext with pysparkCallSite - chained (`divide` is problematic) + with self.assertRaises(ArithmeticException) as pe: + df.withColumn("multiply_ten", df.id * 10).withColumn( + "divide_zero", df.id / 0 + ).withColumn("plus_ten", df.id + 10).withColumn("minus_ten", df.id - 10).collect() + self.check_error( + exception=pe.exception, + error_class="DIVIDE_BY_ZERO", + message_parameters={"config": '"spark.sql.ansi.enabled"'}, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="divide", + ) + + # DataFrameQueryContext with pysparkCallSite - chained (`plus` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("multiply_ten", df.id * 10).withColumn( + "divide_ten", df.id / 10 + ).withColumn("plus_string", df.id + "string").withColumn( + "minus_ten", df.id - 10 + ).collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="plus", + ) + + # DataFrameQueryContext with pysparkCallSite - chained (`minus` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("multiply_ten", df.id * 10).withColumn( + "divide_ten", df.id / 10 + ).withColumn("plus_ten", df.id + 10).withColumn( + "minus_string", df.id - "string" + ).collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="minus", + ) + + # DataFrameQueryContext with pysparkCallSite - chained (`multiply` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.withColumn("multiply_string", df.id * "string").withColumn( + "divide_ten", df.id / 10 + ).withColumn("plus_ten", df.id + 10).withColumn("minus_ten", df.id - 10).collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="multiply", + ) + + # Multiple expressions in df.select (`divide` is problematic) + with self.assertRaises(ArithmeticException) as pe: + df.select(df.id - 10, df.id + 4, df.id / 0, df.id * 5).collect() + self.check_error( + exception=pe.exception, + error_class="DIVIDE_BY_ZERO", + message_parameters={"config": '"spark.sql.ansi.enabled"'}, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="divide", + ) + + # Multiple expressions in df.select (`plus` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.select(df.id - 10, df.id + "string", df.id / 10, df.id * 5).collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="plus", + ) + + # Multiple expressions in df.select (`minus` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.select(df.id - "string", df.id + 4, df.id / 10, df.id * 5).collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="minus", + ) + + # Multiple expressions in df.select (`multiply` is problematic) + with self.assertRaises(NumberFormatException) as pe: + df.select(df.id - 10, df.id + 4, df.id / 10, df.id * "string").collect() + self.check_error( + exception=pe.exception, + error_class="CAST_INVALID_INPUT", + message_parameters={ + "expression": "'string'", + "sourceType": '"STRING"', + "targetType": '"BIGINT"', + "ansiConfig": '"spark.sql.ansi.enabled"', + }, + query_context_type=QueryContextType.DataFrame, + pyspark_fragment="multiply", + ) Review Comment: cc @ueshin added some test cases here -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org