HyukjinKwon opened a new pull request, #168:
URL: https://github.com/apache/spark-connect-rust/pull/168

   ### What changes were proposed in this pull request?
   
   [SPARK-60913](https://issues.apache.org/jira/browse/SPARK-60913): the 
keyword arguments of `SparkSession.sql(sqlQuery, args=None, **kwargs)` now 
format the query, as in PySpark Connect.
   
   - The query is formatted as `str.format` does, with the formatter of 
`pyspark.sql.connect.sql_formatter`, a shim that mirrors PySpark's:
     - A DataFrame becomes a generated name, 
`_pyspark_connect_temp_view_<uuid>`, one for each DataFrame.
     - A plain column reference, such as `col("id")` or `df.id`, becomes its 
name. Another expression raises `PySparkValueError`.
     - A string becomes its SQL literal, and another value its string.
   - Each DataFrame is sent with the statement as a reference that the server 
resolves its name to. The statement's relation is then a `WithRelations` of the 
query with a `SubqueryAlias` of each DataFrame, as in PySpark.
   - `args` still binds parameters, and now raises `INVALID_TYPE` when it is 
neither a dict nor a list, as in PySpark.
   - In the Rust core:
     - `LogicalPlan::WithRelations { root, references }` is sent as the 
`with_relations` relation.
     - `SparkSession::sql_with_references` takes the `(DataFrame, name)` pairs 
that the query names, and `sql_with_args` calls it without any.
   - In the Python client, `pyspark._pyspark.column_reference_name` tells the 
formatter whether a Column is a plain column reference, and gives its name. It 
is a function rather than an attribute, as the attributes of a PySpark Column 
are its fields 
([SPARK-60949](https://issues.apache.org/jira/browse/SPARK-60949)).
   
   ### Why are the changes needed?
   
   The keyword arguments were bound as named parameters, and the query was sent 
as is:
   
   - `spark.sql("SELECT {x} AS v", x=1)` failed with `PARSE_SYNTAX_ERROR`, 
rather than returning 1.
   - `spark.sql("SELECT count(*) AS n FROM {df}", df=spark.range(3))` raised 
`TypeError`, rather than returning 3.
   - `spark.sql("SELECT :p AS v", p=5)` returned 5, where PySpark fails with 
`UNBOUND_SQL_PARAMETER`, as `p` is not a parameter.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, `sql()` with keyword arguments now behaves as in PySpark Connect 4.3.0. 
A keyword argument is no longer a named parameter; `args` binds those.
   
   ### How was this patch tested?
   
   - Against a local Spark 4.3.0 server, a probe of nineteen cases gives output 
identical to official PySpark 4.3.0, apart from one. The cases cover:
     - values, strings with quotes and backslashes, `col()`, `df.col`, and 
format specs;
     - one DataFrame named twice, and a join of two;
     - `args` together with keyword arguments, and `args` as a tuple;
     - escaped braces with and without keyword arguments, and a command that 
reads a DataFrame;
     - the formatter's own errors.
   
     The one difference is the text of the Column in the error for an 
expression that is not a column reference: `Column<'(id + 1)'>` rather than 
`Column<'+(id, 1)'>`, which 
[SPARK-60948](https://issues.apache.org/jira/browse/SPARK-60948) tracks.
   - A new unit test checks the `WithRelations` relation, with a plan ID for 
each node.
   - A new e2e test, in Rust, checks a DataFrame that the query names twice.
   - New live tests check the formatted queries and that a keyword argument is 
not a parameter; they also pass with official PySpark 4.3.0. New offline tests 
check the formatter's output and its errors, whose cases also agree with 
official PySpark 4.3.0 against the server.
   - The lib tests and every integration test, e2e included, against the local 
server (639 passed), the build of all tests with `wasm-udf`, the offline Python 
suites (587 passed, 8 live cases skipped), and the live drop-in and Arrow 
suites (54 passed) and live cases (10 passed). Clippy reports no new 
diagnostics in the changed lines.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


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