Implement Spark-compatible `from_xml`
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 20/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- rust, xml
- Lĩnh vực
- data-engineering
Hướng nghiên cứu
Start by reading the Spark implementation named in the issue (XmlToStructs, StaxXmlParser, XmlOptions, FailureSafeParser) alongside this crate's from_json work to settle how Spark DDL schemas are parsed. Then add a quick-xml-based parser and wire the expression, using crates/sqllogictest/test_files/xml/from_xml.slt as the acceptance test and #[user_doc] for docs. A first version should cover PERMISSIVE/FAILFAST, primitive/struct/array/string-keyed map, attributes and valueTag; VARIANT, XSD and datetime patterns are follow-ups.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Part of #271 and apache/datafusion-comet#7300.
What the function does
from_xml(xmlStr, schema[, options]) parses one XML record into a STRUCT. It was added in Spark 4.0.0.
- Schema.
schemamust be a foldable string: a DDL string ('a INT, b DOUBLE'or'STRUCT<...>') or
schema_of_xml(...). A non-literal schema fails withINVALID_SCHEMA.NON_STRING_LITERAL. A bad DDL string
fails withPARSE_SYNTAX_ERROR. The top level must beSTRUCT, orVARIANTfrom 4.1. Otherwise analysis
fails withDATATYPE_MISMATCH.INVALID_XML_SCHEMA. AMAPwith a non-STRINGkey anywhere in the schema
givesDATATYPE_MISMATCH.INVALID_XML_MAP_KEY_TYPE. - Options.
optionsmust be amap(...)of string literals. Otherwise the error is
INVALID_OPTIONS.NON_MAP_FUNCTIONorINVALID_OPTIONS.NON_STRING_TYPE. Wrong arity gives
WRONG_NUM_ARGS.WITHOUT_SUGGESTION. - Result. The result type is the schema with nullability forced everywhere (
asNullable). It is nullable
and NULL-intolerant (NULL in, NULL out). The input is a string (collation-aware), and the function is time
zone aware. - Root element. The root element's name is ignored. Its attributes become fields and its children are
matched against the schema, sorowTaghas no effect:XmlFunctionsSuiteshows<foo>,<ROW>and<p>
all parsing.
Parsing uses StaxXmlParser over a StAX reader. The reader is not namespace-aware and coalesces text. It does
not support DTDs or external entities, and skips comments, processing instructions and DTD events. Records go
through FailureSafeParser with options from XmlOptions.
Examples
SELECT from_xml('<p><a>1</a><b>0.8</b></p>', 'a INT, b DOUBLE');
-- {"a":1,"b":0.8}
SELECT from_xml('<p><time>26/08/2015</time></p>', 'time Timestamp', map('timestampFormat', 'dd/MM/yyyy'));
-- {"time":2015-08-26 00:00:00}
SELECT from_xml('<p><teacher>Alice</teacher><student><name>Bob</name><rank>1</rank></student><student><name>Charlie</name><rank>2</rank></student></p>', 'STRUCT<teacher: STRING, student: ARRAY<STRUCT<name: STRING, rank: INT>>>');
-- {"teacher":"Alice","student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}
SELECT from_xml('<p><a>1</a></p>', 'struct<a:array<int>>');
-- {"a":[1]}
SELECT from_xml('<p><a>1</a><b>"2"</b></p>', 'struct<a:int,b:string>');
-- {"a":1,"b":""2""}
SELECT from_xml('<p><a>1</a><a>"2"</a></p>', 'struct<a:array<int>>');
-- {"a":[1]}
SELECT from_xml('<p><a>1</a><a></a></p>', 'struct<a:array<int>>');
-- {"a":[1,null]}
SELECT from_xml('<p><a attr="1"><b>2</b></a></p>', 'struct<a:map<string,int>>');
-- {"a":{"_attr":1,"b":2}}
SELECT from_xml('<p><d>12/15 2012</d><t>12/15 2012 15:15:15</t>}</p>', 'd date, t timestamp', map('dateFormat', 'MM/dd yyyy', 'timestampFormat', 'MM/dd yyyy HH:mm:ss'));
-- {"d":2012-12-15,"t":2012-12-15 15:15:15}
SELECT from_xml('<p><d>02-29</d></p>', 'd date', map('dateFormat', 'MM-dd'));
-- {"d":null}
SELECT from_xml('<p><a>1</a><a>2</a><a>3</a></p>', schema_of_xml('<p><a>1</a><a>2</a></p>'));
-- {"a":[1,2,3]}
These come from the ExpressionDescription and xml-functions.sql.out (4.2).
Edge cases to test
- Modes (
mode, case-insensitive; an unknown value falls back toPERMISSIVEwith a warning):PERMISSIVE(the default). A malformed record gives a struct of NULLs, not a NULL struct.<ROW><a>1</ROW>
witha INTgives{"a":null}.- A conversion failure inside an element keeps the partial row (the
[1]example above). - If the schema contains the corrupt-record column (
spark.sql.columnNameOfCorruptRecord, default
_corrupt_record, or thecolumnNameOfCorruptRecordoption), that column gets the raw input string. FAILFASTraisesMALFORMED_RECORD_IN_PARSING.WITHOUT_SUGGESTION.DROPMALFORMEDraisesPARSE_MODE_UNSUPPORTED("doesn't support the DROPMALFORMED mode").
- Corrupt column in the middle.
XmlFunctionsSuite("corrupt record column in the middle") asserts that
schemaa INT, _unparsed STRING, b INTon the valid<ROW><a>1</a><b>12</b></ROW>gives(1, null, null).
FailureSafeParserreads the parsed row by its index in the schema without the corrupt column, so
every field after the corrupt column is shifted. Decide whether to copy this bug and document the decision. - Type conversion (
convertTo/castTo, after trimming whenignoreSurroundingSpaces=true, the default):INT,BIGINTandFLOAT/DOUBLEstrip a leading+or-, parse the rest with Java
toInt/toLong/toDouble, and then negate. By code reading this means-2147483648and
-9223372036854775808overflow and become NULL.FLOAT/DOUBLEfall back to a localeNumberFormat.TINYINTandSMALLINTusetoByte/toShortdirectly, andDECIMALuses the locale decimal parser.BOOLEANacceptstrue,false,1and0, case-insensitive.- Any failure nulls the field, or raises in
FAILFAST.
- Empty values. An empty element (
<a></a>or<a/>) is NULL for non-string types. ForSTRINGit is
""unlessnullValueis"". A text value equal tonullValue(default: none) becomes NULL. STRINGfield with child elements. The field gets the inner markup as a string
(currentStructureAsString).- Attributes. Attributes become fields named
attributePrefix + name(default_). With
excludeAttribute=truethey are dropped. An element that has both attributes and text maps the text to
valueTag(default_VALUE):<a x="1">5</a>witha STRUCT<_VALUE: INT, _x: INT>. Option checks:
valueTagmust be non-empty and different fromattributePrefix. - Arrays. Repeated elements fill an
ARRAYfield. A single occurrence gives a one-element array.
wildcardColName(defaultxs_any) collects unmatched elements asSTRINGorARRAY<STRING>. - Field names. Matching follows
spark.sql.caseSensitive(default false, so it is case-insensitive).
Unknown elements are skipped and missing ones are NULL.ignoreNamespace=truestrips theprefix:. - Datetimes.
timestampFormathas no default in read mode unless the legacy parser policy is on. Also
covertimestampNTZFormat,dateFormat(an invalid date such as02-29without a year gives NULL),
timeFormat(4.2,TIME(p)),locale,timeZone,spark.sql.session.timeZone, DST transitions, and
spark.sql.legacy.timeParserPolicy. - Binary.
BINARYfields (4.1+) are parsed according tospark.sql.binaryOutputStyle. - Bad options. Validation errors from
require(IllegalArgumentException) cover an emptyrowTag, a
rowTagwith<or>, an emptyvalueTag, and a non-boolean value forexcludeAttributeand other
boolean options. - Inputs. NULL input gives NULL. Also test the empty string, whitespace-only input, multiple root elements,
trailing content after the root, and Unicode in names and text. - Round-trip.
from_xml(to_xml(s), schema)should round-trip structs and arrays (XmlFunctionsSuite).
Differences across Spark versions
- Not available in 3.4 or 3.5.
- 4.1: adds the
VARIANTschema and thesingleVariantColumnoption,BINARYparsing, and the
useLegacyXMLParseroption andspark.sql.legacy.useLegacyXMLParserconfig. These only affect the file
reader. - 4.2: adds the
TIMEtype and thetimeFormatoption.
Implementation notes
- Spark:
XmlToStructsinsql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xmlExpressions.scala
andXmlToStructsEvaluatorin.../expressions/xml/XmlExpressionEvalUtils.scala. The parser is
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/StaxXmlParser.scala, with
StaxXmlParserUtils.scalaandXmlOptions.scala. Modes are incatalyst/util/FailureSafeParser.scala. - DataFusion core has no XML functions.
- Add a streaming XML parser dependency.
quick-xmlmaps closely to the StAX event model; it doesn't process
DTDs, and namespaces are opt-in. This crate has no XML dependency today. Thexpath*family needs a DOM and
an XPath engine instead (see thexpathissue), so the two may end up with different crates. - This crate has no Spark DDL schema parser. Share the approach chosen for
from_json: either parse the
literal at planning time, or let callers such as Comet pass the Arrow type directly. - A first version could support
PERMISSIVEandFAILFAST, primitive, struct, array and string-keyed map
types, attributes andvalueTag, and leaveVARIANT, XSD validation (rowValidationXSDPath) and datetime
patterns for follow-ups. - Tests: add
crates/sqllogictest/test_files/xml/from_xml.sltand document the function with#[user_doc].
This issue was generated by an LLM (Claude) from the Apache Spark source code and tests. Examples come from Spark's ExpressionDescription examples, test suites and .sql.out golden files, but please verify them against Spark before relying on them.
- Ngôn ngữ chính
- Rust
- Star
- 7
- Fork
- 15
- Merge trung bình
- 16 giờ 43 phút
- Pull request đã merge (30 ngày)
- 26
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/datafusion-spark
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/datafusion-spark#287 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
apache/datafusion-spark#266 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the intervalCó thể đã có người làm @Subodh-17 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-spark#109 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
apache/datafusion-spark#299 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
apache/datafusion-spark#298 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của apache/datafusion-spark
Issue tương tự
-
comp-rbac
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ClickHouse/ClickHouse#131875 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indicesCó thể đã có người làm @Borg93 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
lance-format/lance#11117 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
storytold/photocraft#15232 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)Có thể đã có người làm @Ynyesto đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
foundry-rs/foundry#18485 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
bondybondbond/rushcut#238 · 1 bình luận ·