Commit 9d760a9
committed
[SPARK-57884][SQL] Make XML schema inference honor preferDate, consistent with CSV
### What changes were proposed in this pull request?
Make XML schema inference honor the `preferDate` option as a gate on whether date inference is attempted, matching `CSVInferSchema`.
Currently, `XmlInferSchema.tryParseDouble` and `tryParseTime` fall through to `tryParseDate(field)` **unconditionally**, ignoring `options.preferDate`. As a result, with `preferDate=false`, a value that matches the (default) date format is still inferred as `DateType`. This contradicts the option's purpose — `preferDate=false` is meant to disable date inference (date/timestamp inference is ambiguous, and the option lets users opt out) — and diverges from the CSV datasource, whose `tryParseDouble`/`tryParseTime` gate the date attempt on `options.preferDate`.
This change routes the date attempt through `preferDate`: when `preferDate` is true, a bare date still infers as `DateType`; when false, date inference is skipped and the value falls through to timestamp inference. TIME inference is unaffected (it remains tried ahead of date/timestamp regardless of `preferDate`, unchanged from before).
### Why are the changes needed?
`preferDate` is documented and structured (see `XmlOptions.dateFormatInRead`, which is conditioned on `preferDate`) as controlling whether date inference happens, but the `tryParse*` control flow does not honor it, so a bare ISO date leaks through as `DateType` regardless. The CSV datasource — which XML's inference cascade mirrors — already gates date inference on `preferDate`. This aligns XML with CSV so `preferDate=false` behaves consistently across the two text datasources.
### Does this PR introduce _any_ user-facing change?
Yes. With `preferDate=false`, XML schema inference no longer infers `DateType` for date-shaped values; such values now fall through to timestamp inference (as they already do in CSV). With the default `preferDate=true`, behavior is unchanged.
### How was this patch tested?
Updated the `preferDate` tests in `XmlInferSchemaSuite` (core) and `XmlInferSchemaTypeCastingSuite` (catalyst) to assert the gated behavior: `preferDate=true` -> `DateType`, `preferDate=false` -> `TimestampType`. Existing XML inference suites pass.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Anthropic Claude Opus)
Closes #56966 from cloud-fan/SPARK-preferDate-xml-followup.
Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>1 parent 41b2739 commit 9d760a9
4 files changed
Lines changed: 76 additions & 19 deletions
File tree
- sql
- catalyst/src
- main/scala/org/apache/spark/sql/catalyst/xml
- test/scala/org/apache/spark/sql/catalyst/xml
- core/src/test/scala/org/apache/spark/sql/execution/datasources/xml
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
23 | 32 | | |
24 | 33 | | |
25 | 34 | | |
| |||
147 | 156 | | |
148 | 157 | | |
149 | 158 | | |
150 | | - | |
| 159 | + | |
151 | 160 | | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
156 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
157 | 175 | | |
158 | | - | |
159 | | - | |
| 176 | + | |
160 | 177 | | |
161 | | - | |
| 178 | + | |
162 | 179 | | |
163 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
164 | 183 | | |
165 | | - | |
| 184 | + | |
166 | 185 | | |
167 | 186 | | |
168 | 187 | | |
| |||
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
682 | 682 | | |
683 | 683 | | |
684 | 684 | | |
685 | | - | |
| 685 | + | |
686 | 686 | | |
| 687 | + | |
| 688 | + | |
687 | 689 | | |
688 | 690 | | |
689 | 691 | | |
690 | 692 | | |
691 | 693 | | |
692 | 694 | | |
693 | | - | |
| 695 | + | |
694 | 696 | | |
| 697 | + | |
| 698 | + | |
695 | 699 | | |
696 | 700 | | |
697 | 701 | | |
| |||
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/xml/XmlInferSchemaTypeCastingSuite.scala
Lines changed: 36 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
159 | 188 | | |
160 | 189 | | |
161 | | - | |
162 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
163 | 196 | | |
164 | 197 | | |
165 | 198 | | |
Lines changed: 8 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
722 | 722 | | |
723 | 723 | | |
724 | 724 | | |
725 | | - | |
| 725 | + | |
726 | 726 | | |
727 | | - | |
728 | | - | |
729 | | - | |
730 | | - | |
731 | | - | |
732 | | - | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
733 | 734 | | |
734 | 735 | | |
735 | 736 | | |
| |||
0 commit comments