Implement Spark-compatible `to_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ó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
Hướng nghiên cứu
Start from the crate's existing to_json expression: it is the closest structural template and the issue says to reuse its datetime formatting helpers. Then read Spark's StructsToXml and StaxXmlGenerator at the paths listed here for the exact serialization and indentation rules, and build the writer plus the registered function documented with #[user_doc]. Done means a new crates/sqllogictest/test_files/xml/to_xml.slt passes the examples and edge cases in this issue.
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
to_xml(expr[, options]) serializes a STRUCT (or a VARIANT, from 4.1) to an XML string. It was added in
Spark 4.0.0.
- Arguments. Any other input type fails analysis with
DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
(requiredType"STRUCT").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(expectedNum[1, 2]). - Result. The result is a nullable
STRING(the session default string type). The function is
NULL-intolerant and time zone aware.
Output comes from StaxXmlGenerator, which wraps a Woodstox XMLStreamWriter with structure validation off.
- Row element. The struct is written as one element named
rowTag(defaultROW). No XML declaration and
norootTagwrapper are written; those are file-writer only. - Indentation. The default
indentis four spaces, which gives newline-separated, indented output.
indentset to''gives compact output. - Fields. A field whose name starts with
attributePrefix(default_), and is notvalueTag(default
_VALUE), becomes an attribute without the prefix. Attributes are written before child elements. The
valueTagfield is written as the element's text. - NULL values. When
nullValueis not set (the default), NULL fields are omitted. Otherwise the
nullValuetext is written. - Arrays. An array field becomes repeated elements with the field's name. An array nested directly in an
array usesarrayElementName(defaultitem). - Maps. A map becomes child elements keyed by the map key. Keys with the attribute prefix become
attributes.
Examples
SELECT to_xml(named_struct('a', 1, 'b', 2));
-- <ROW>
-- <a>1</a>
-- <b>2</b>
-- </ROW>
SELECT to_xml(named_struct('a', 1, 'b', 2), map('indent', ''));
-- <ROW><a>1</a><b>2</b></ROW>
SELECT to_xml(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy', 'indent', ''));
-- <ROW><time>26/08/2015</time></ROW>
SELECT to_xml(named_struct('time', TIME'14:30:45.123456'), map('rowTag', 'record', 'indent', ''));
-- <record><time>14:30:45.123456</time></record>
SELECT to_xml(array(named_struct('a', 1, 'b', 2)));
-- error: DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
SELECT to_xml(map('a', 1));
-- error: DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
These come from the ExpressionDescription and xml-functions.sql.out (4.2). XmlFunctionsSuite also has
these cases:
- with session time zone
America/Los_Angeles, the timestamp1800-01-01 00:00:00is written as
1800-01-01T00:00:00.000-07:52:58; - with
timestampFormatyyyy-MM-dd HH:mm:ss.SSSSSS, it writes2019-11-18 11:56:00.123456.
Edge cases to test
- Value formatting.
- Numbers use Java
toString, soDOUBLEandFLOATprint as1.0E10,NaNandInfinity, and
DECIMALusesDecimal.toString. Booleans print astrueorfalse. DATEusesdateFormat(defaultyyyy-MM-dd).TIMESTAMPusestimestampFormat. The default isyyyy-MM-dd'T'HH:mm:ss[.SSS][XXX];
spark.sql.legacy.timeParserPolicy=LEGACYchanges it to...ss.SSSXXX, and the internal
supportSecondOffsetFormatsetting changes the offset to[XXXXX]. The value is rendered in the session
time zone or thetimeZoneoption. Test DST and historical (LMT) offsets, as in the 1800 case above.TIMESTAMP_NTZusestimestampNTZFormat(defaultyyyy-MM-dd'T'HH:mm:ss[.SSS]), andTIMEuses
timeFormat(4.2).BINARYfollowsspark.sql.binaryOutputStyle.
- Numbers use Java
- Escaping and names.
<,&and>in text, and quotes in attributes, are escaped by the writer.- Field names that aren't valid XML names (
'1a', a name with a space) fail whenvalidateName=true
(default). Check the exact error against Spark.
- Empty struct values. Nested NULL structs are omitted. A struct whose fields are all NULL writes an empty
element. Compare the exact output (<ROW></ROW>or<ROW/>) against Spark. - Attributes and
valueTag. Fornamed_struct('_id', 1, '_VALUE', 'x'), attribute fields are written
first, whatever the field order. - Arrays. Arrays of primitives, arrays of structs, and NULL array elements (omitted unless
nullValueis
set). Nested arrays usearrayElementName. - Option validation.
attributePrefixmust be non-empty (requirefails otherwise), androwTagmust
not contain angle brackets. - Round-trips.
to_xmlfollowed byfrom_xmlwith the same schema should round-trip. - NULL input gives NULL.
Differences across Spark versions
- Not available in 3.4 or 3.5.
- 4.1: accepts
VARIANTinput, and adds thesingleVariantColumnhandling inStaxXmlGenerator.write. - 4.2: adds
TIMEand thetimeFormatoption.
Implementation notes
- Spark:
StructsToXmlinsql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xmlExpressions.scala,
StructsToXmlEvaluatorin.../expressions/xml/XmlExpressionEvalUtils.scala, and the writer in
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/StaxXmlGenerator.scalawith
IndentingXMLStreamWriter.scala. Options are inXmlOptions.scala. - DataFusion core has no XML functions.
- A writer can be hand-rolled, or built on
quick-xml'sWriter. The crate has no XML dependency today.
Match the indentation rules ofIndentingXMLStreamWriterexactly. Reuse the datetime formatting helpers
thatto_jsonanddate_formatuse, if they exist by then. - Tests: add
crates/sqllogictest/test_files/xml/to_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 ·