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

   ### What changes were proposed in this pull request?
   
   `DataType::from_json` and `DataType::from_ddl` 
(`crates/spark-connect/src/types.rs`) each had a table of the atomic type names 
and a chain of prefix checks for `decimal(p,s)`, `char(n)`, `varchar(n)`, 
`time(p)`, `timestamp_ntz(p)`, intervals and collated strings, and each read 
`string collate <name>` with its own code.
   
   Both now go through two shared functions:
   
   - `common_atomic_type`: the names that are a type in both grammars.
   - `parse_parameterized_type(s, grammar)`: every form that takes arguments or 
is of several words.
   
   `grammar` is a `TypeStringGrammar` (`Json` or `Ddl`). It is consulted only 
where the two grammars of PySpark really differ, `_parse_datatype_json_value` 
on the client and the server's parser of DDL:
   
   | form | JSON | DDL |
   |---|---|---|
   | letter case and spacing | as written: lower case, single spaces | any 
case, any whitespace between keywords |
   | `decimal(p)` | not a type: the scale is required | scale 0 |
   | `decimal(p,-s)` | a negative scale | not a type: an argument is an 
unsigned integer |
   | `interval <a> to <b>` | any two fields of one family | `b` later than `a` |
   | `string collate <name>` | the name as written | the canonical name, 
`system.builtin.` dropped |
   
   The names of one grammar alone stay with their parser (`tinyint`, `int`, 
`dec`, `timestamp_ltz`, ... for DDL; `calendarinterval`, `unparsed(...)` for 
JSON).
   
   Neither parser checks the range of a decimal's precision or scale, as before 
and as PySpark's does not. The check that SPARK-61423 added at the conversion 
to Arrow is untouched.
   
   ### Why are the changes needed?
   
   The leaf functions were shared without knowing which grammar they were 
reading, so a change made for one grammar changed the other, and both had 
drifted from PySpark:
   
   - JSON read `decimal(5)`, which `_FIXED_DECIMAL` does not. It came with the 
DDL form of SPARK-61308.
   - JSON did not read `interval second to day`, the `json()` of 
`DayTimeIntervalType(3, 0)`, so the JSON of such a type did not read back. 
PySpark reads it.
   - DDL read a negative scale, `decimal(10,-2)`, which is not an 
`INTEGER_VALUE` of Spark's grammar.
   - Both read a signed argument (`char(-3)`, `time(+6)`, `decimal(+5,2)`), 
because the arguments were parsed as `i32`. PySpark's patterns are `\d+`.
   - `string collate` without a name and `string collateX` were read by both, 
by DDL as a string of the empty collation. JSON did not read `string  collate  
X`, which `\s+` does. Neither required the name to be `\w+`.
   - DDL did not read `string collate system.builtin.<name>`, which 
`CollationFactory.resolveFullyQualifiedName` does.
   
   PySpark's patterns are applied with `re.match`, so it also reads a type 
string with trailing text, such as `decimal(10,2)xyz`, or `interval day to 
year` as `interval day`. That is not followed: both parsers still raise for 
them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for the type strings listed above:
   
   - `DataType::from_json` no longer reads `decimal(p)`, a signed precision or 
length, an interval of several spaces between its words, or a collated string 
without a `\w+` name. It now reads an interval range of any two fields of one 
family, and `string collate <name>` of any whitespace.
   - `DataType::from_ddl` no longer reads a signed argument (a negative scale 
among them) or a collated string without a `\w+` name. It now reads a collation 
qualified as `system.builtin.<name>`.
   
   `_parse_datatype_json_string` of the Python client reads its string forms 
with `DataType::from_json`, so it changes the same way.
   
   ### How was this patch tested?
   
   A new table-driven test, 
`json_and_ddl_parsers_agree_on_the_atomic_and_parameterized_forms` in 
`crates/spark-connect/tests/types_roundtrip.rs`, feeds each form through both 
parsers. It has three tables: the forms that are the same type of both, those 
that are an error of both, and the listed differences with what each parser 
makes of them. A DDL string is read alone and as the element of an array, and 
the two have to agree. Out-of-range decimals (`decimal(39,0)`, 
`decimal(300,0)`, `decimal(5,6)`) are in the first table.
   
   - `cargo test -p apache-spark-connect --release --lib`: 599 passed.
   - `cargo test -p apache-spark-connect --release`: all pass.
   - `cargo test -p apache-spark-connect --release --no-run`, with and without 
`--features wasm-udf`.
   - `cargo fmt --all --check`.
   
   The DDL behavior was checked against `SqlBaseParser.g4` and 
`DataTypeAstBuilder.scala` of Spark v4.3.0, and the JSON behavior against 
`python/pyspark/sql/types.py` of the same tag. The server-backed tests of 
`parse_ddl`, the Python tests of the drop-in and the official-test parity gate 
were not run. `cargo clippy` reports nothing on the changed lines.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (claude-fable-5-2)
   
   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