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

   ### What changes were proposed in this pull request?
   
   [SPARK-61242](https://issues.apache.org/jira/browse/SPARK-61242): 
`DataType::from_ddl` reads the `NOT NULL` and `COMMENT 'string'` clauses of a 
field as Spark's grammar does, and `DataType::type_name` gives PySpark's names 
for day-time intervals, year-month intervals and UDTs.
   
   - Field parsing (`parse_struct_field` and its helpers) takes `name [:] type 
[NOT NULL] [COMMENT 'string']`, both for the top-level form (`a INT NOT NULL, b 
STRING COMMENT 'x'`) and inside `struct<...>`, where the colon is optional as 
in Spark's `complexColType`. Keywords are case-insensitive, `!NULL` is `NOT 
NULL`, and the comment is one or more adjacent string literals. A `NOT NULL` 
field is non-nullable, and the comment goes in the field metadata under 
`comment`.
   - Quoted text is scanned as Spark's lexer does, so a comma, `<`, `(` or 
colon in a comment or a backquoted name does not split or nest a field. 
Comments are unescaped as `unescapeSQLString` does (`\\`, `\'`, `''`, `\n`, 
`\uXXXX`, octal, and so on), and backquoted names strip their quotes and 
undouble backquotes.
   - Empty fields (`a INT,,b INT`, `struct<a:int,>`) and unquoted names that 
are not ASCII letters, digits or underscores are errors, as in Spark. An empty 
schema (`""`, blank) is an empty struct, as PySpark's `fromDDL` gives it.
   - `type_name()` returns `daytimeinterval` and `yearmonthinterval` for the 
two interval types (`interval` stays for `CalendarIntervalType`). For 
`DataType::Udt` it returns the lower-cased class name of `python_class`, as 
`UserDefinedType.typeName()` does, and `udt` when there is no class. The tree 
string already used this rule, so its UDT arm now goes through `type_name()`. 
`simple_string()`, `json()` and the proto are unchanged.
   
   Official PySpark 4.3.0 (through the server's `DataType.fromDDL`) gives the 
same results for the cases of the tests below; I compared 83 DDL strings (NOT 
NULL, COMMENT with escapes, nesting, backquoted names, invalid forms) against 
the Rust parser, with no differences after this change. `typeName()` of 
`DayTimeIntervalType` and `YearMonthIntervalType` is `daytimeinterval` and 
`yearmonthinterval`, and of a UDT subclass `PointUDT` it is `pointudt`.
   
   ### Why are the changes needed?
   
   `DataType::to_ddl` writes ` NOT NULL` and ` COMMENT '...'` for a 
non-nullable or commented field, but `DataType::from_ddl` rejects both. So 
`from_ddl(to_ddl(x))` fails for any struct with such a field, and `from_ddl("a 
INT NOT NULL")` fails with `INVALID_DATATYPE_FORMAT`, while the server's parser 
accepts it. Separately, `type_name()` returned `interval` for the day-time and 
year-month interval types, so two different types had the same name.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for the Rust API. `DataType::from_ddl` now accepts `NOT NULL` and 
`COMMENT` clauses and rejects empty fields and non-identifier unquoted names as 
Spark does. `type_name()` changes for day-time and year-month intervals (to 
`daytimeinterval` and `yearmonthinterval`) and for UDTs (to the lower-cased 
class name). The Python drop-in is rebuilt from this crate, and its 
`StructType` and type-string coercions pick up the new `from_ddl`. Its 
`typeName()` is class-based and is unchanged.
   
   ### How was this patch tested?
   
   - `from_ddl_reads_not_null_and_comment_as_spark` (new, 
`tests/types_roundtrip.rs`): NOT NULL and COMMENT at top level and nested, in 
any keyword case, the comment escapes, adjacent literals, `!NULL`, backquoted 
names, and the error cases.
   - `to_ddl_of_non_nullable_and_commented_fields_round_trips` (new): 
`from_ddl(to_ddl(x)) == x` for structs with non-nullable and commented fields 
at several levels, in an array and a map, with names that need backquotes.
   - `type_name_of_intervals_and_udts_as_pyspark` (new): the type names of both 
intervals, `CalendarInterval` and UDTs with and without a class.
   - `from_ddl_not_null_and_comment_agree_with_the_server_parser` (new, gated 
on `SPARK_REMOTE` as `to_ddl_comments_round_trip_through_the_server_parser`): 
the same parse as the server's DDL parser for 18 strings.
   - The new tests fail on the base code (`INVALID_DATATYPE_FORMAT` and the 
wrong names) and pass with the fix. The existing 
`test_golden_values_simple_strings` expectation for the two interval types is 
updated to the PySpark names.
   - The lib tests (522 passed), the build of all tests, the offline Python 
suites (2819 passed, 8 skipped) with the rebuilt extension, and 
`types_roundtrip` (16 passed, with the comparison against the server's parser), 
`types_behavior` (15 passed), `coverage_types_extra` (10 passed) and 
`coverage_plan_map_udtf` (6 passed) against a local Spark 4.3.0 server. Clippy 
reports no diagnostics in the changed lines.
   - Checked with official PySpark 4.3.0 on a Connect server: the 83 DDL 
strings above give identical schemas in Rust and on the server.
   
   ### 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