Implement Spark-compatible `schema_of_csv`
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start by reading SchemaOfCsv in sql/catalyst/.../csvExpressions.scala and SchemaOfCsvEvaluator in CsvExpressionEvalUtils.scala, then CSVInferSchema.inferRowType/inferField/toStructFields and CSVOptions — those define the inference ladder and option parsing to mirror. The target is crates/spark/src/function/csv/, reusing the tokenizer and CsvOptions already shared with from_csv/to_csv, and reusing schema_of_json's DDL formatting if present. Done means a foldable expression returning the STRUCT<...> string with all listed error codes, verified by the new crates/sqllogictest/test_files/csv/schema_of_csv.slt cases and annotated with #[user_doc].
由索引模型根据 Issue 内容生成。
描述
Part of #272 and apache/datafusion-comet#7300.
What the function does
schema_of_csv(csv[, options]) parses one CSV record and returns the inferred schema as a DDL string such as
STRUCT<_c0: INT, _c1: STRING>. Fields are named _c0, _c1, ... by position. csv must be foldable
(DATATYPE_MISMATCH.NON_FOLDABLE_INPUT) and not NULL (DATATYPE_MISMATCH.UNEXPECTED_NULL). From 4.1, a non-string
argument fails with DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE. options is a map(...) of string literals, read
with the same CSVOptions as from_csv (sep/delimiter, quote, escape, nullValue, nanValue,
positiveInf, negativeInf, dateFormat, timestampFormat, timestampNTZFormat, preferDate, locale,
ignoreLeadingWhiteSpace, ignoreTrailingWhiteSpace, ...). The time zone is always UTC. lineSep is forced to
\uFFFF, so a newline is part of the field.
Returns a non-nullable STRING. Since 3.0.0. Because the input is foldable, Spark always constant-folds it
(in 4.x it is RuntimeReplaceable over an Invoke on a literal). Its main use is as the schema argument of
from_csv, so DataFusion can evaluate it at planning time (for example in simplify).
Examples
SELECT schema_of_csv('1,abc');
-- STRUCT<_c0: INT, _c1: STRING>
SELECT schema_of_csv('1|abc', map('delimiter', '|'));
-- STRUCT<_c0: INT, _c1: STRING>
SELECT schema_of_csv('0.1,1');
-- STRUCT<_c0: DOUBLE, _c1: INT>
SELECT schema_of_csv('0.1 1', map('sep', ' '));
-- STRUCT<_c0: DOUBLE, _c1: INT>
SELECT schema_of_csv('14:30:45');
-- STRUCT<_c0: TIMESTAMP>
SELECT schema_of_csv('14:30:45.123456');
-- STRUCT<_c0: TIMESTAMP>
SELECT schema_of_csv(null);
-- error: DATATYPE_MISMATCH.UNEXPECTED_NULL
SELECT schema_of_csv(42);
-- error: DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE (4.1+)
SELECT schema_of_csv(csvField) FROM csvTable;
-- error: DATATYPE_MISMATCH.NON_FOLDABLE_INPUT
These come from the ExpressionDescription, csv-functions.sql.out and CsvFunctionsSuite. Others, by reading
CSVInferSchema: '1,,abc' gives _c1: STRING; '12345678901234567890' gives DECIMAL(20,0);
'2015-08-26' gives DATE; 'TRUE' gives BOOLEAN; 'NaN' and 'Inf' give DOUBLE.
Edge cases to test
Inference (CSVInferSchema.inferField, starting from NullType for each field, single row so no merging):
- Empty field or a field equal to
nullValue(default"") staysNullType, which becomesSTRING. - The type ladder is tried in this order, each falling through to the next on failure:
INT(Integer.parseInt),BIGINT(Long.parseLong),DECIMAL(p, s)only when theBigDecimalhas
scale <= 0 (so integers too big for BIGINT; precision > 38 or a negative scale such as1e5throws and falls
through),DOUBLE(Double.parseDouble, or thenanValue/positiveInf/negativeInfstrings),DATE
(only whenpreferDate, default true; strict parse withdateFormat, defaultyyyy-MM-dd),TIMESTAMP_NTZ(only
whenspark.sql.timestampType=TIMESTAMP_NTZ),TIMESTAMP(withtimestampFormat, or the lenient default parser
that also accepts time-only strings, see above),BOOLEAN(case-insensitivetrue/false), elseSTRING. 1.5has scale 1, so it skips DECIMAL and becomesDOUBLE.TIMEis never inferred, even in 4.2.- Whitespace:
' 0.8'fails INT, BIGINT and BigDecimal butDouble.parseDoubletrims, so it isDOUBLE.
' 1'isDOUBLEtoo, notINT(by code reading). WithignoreLeadingWhiteSpaceit becomesINT. - Java
Double.parseDoublealso accepts1d,1f,Infinityand hex floats, all givingDOUBLE. - With a non-US
localethe decimal step usesDecimalFormatfor that locale (grouping and decimal separators).
The US default uses plainnew BigDecimal(s)(unlikefrom_csv, commas are not stripped). spark.sql.legacy.timeParserPolicy=LEGACYturnspreferDateoff and sets the default timestamp pattern to
yyyy-MM-dd'T'HH:mm:ss.SSSXXX, which changes date and timestamp inference.- A quoted field with the delimiter inside (
'"a,b",1') is one field. The number of fields equals the number of
tokens univocity returns, including a trailing empty one ('1,'gives two fields). - Output formatting is
StructType.sql:STRUCT<name: TYPE, ...>with:and,separators, upper-case types,
and field names quoted with backticks only when needed. - Bad option values give the same errors as
from_csv(OPTION_VALUE_EXCEEDS_ONE_CHARACTER,
INVALID_DELIMITER_VALUE.*,INVALID_OPTIONS.NON_MAP_FUNCTION,INVALID_OPTIONS.NON_STRING_TYPE).
Differences across Spark versions
- 3.4 / 3.5:
CodegenFallbackexpression. No explicit string type check on the argument. - 4.0: becomes
RuntimeReplaceable(SchemaOfCsvEvaluator).spark.sql.legacy.timeParserPolicydefault moves
fromEXCEPTIONtoCORRECTED. - 4.1: adds the
UNEXPECTED_INPUT_TYPEcheck (child.dataType != StringType). - 4.2:
csv-functions.sql.outadds the time-only cases, which inferTIMESTAMP.
Implementation notes
- Spark:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/csvExpressions.scala
(SchemaOfCsv),expressions/csv/CsvExpressionEvalUtils.scala(SchemaOfCsvEvaluator),
catalyst/csv/CSVInferSchema.scala(inferRowType,inferField,toStructFields),catalyst/csv/CSVOptions.scala. - DataFusion core has no equivalent. Arrow's CSV schema inference (
arrow-csvinfer_schema) uses different
rules (regex based, Int64/Float64/Boolean/Date/Timestamp) and can't be reused. - Share the CSV tokenizer and
CsvOptionswithfrom_csvandto_csv(apache/datafusion-spark#138; Comet's
nativeto_csvinnative/spark-expr/src/csv_funcs/already has write options) in
crates/spark/src/function/csv/.schema_of_jsonshould share the DDL type-string formatting if it exists. - Tests: add
crates/sqllogictest/test_files/csv/schema_of_csv.slt; document 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.
- 主要语言
- Rust
- 星标
- 7
- 派生
- 15
- 平均合并
- 16 小时 43 分钟
- 30 天内合并 PR
- 26
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/datafusion-spark 的其他 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
apache/datafusion-spark#287 ·
维护者通常 1 天内回复
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 80/100
apache/datafusion-spark#266 ·
维护者通常 1 天内回复
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the interval可能已有人在做 @Subodh-17 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apache/datafusion-spark#109 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 18/100
apache/datafusion-spark#299 ·
维护者通常 1 天内回复
-
enhancement
难度 4/5 3-5 天 新手友好度 35/100
apache/datafusion-spark#298 ·
维护者通常 1 天内回复
查看 apache/datafusion-spark 的全部 Issue
相似的 Issue
-
comp-rbac
难度 2/5 1-3 小时 新手友好度 68/100
ClickHouse/ClickHouse#131875 ·
维护者通常 1 天内回复
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indices可能已有人在做 @Borg93 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 75/100
lance-format/lance#11117 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
storytold/photocraft#15232 · 1 条评论 ·
维护者通常 1 天内回复
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)可能已有人在做 @Ynyesto 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
foundry-rs/foundry#18485 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
bondybondbond/rushcut#238 · 1 条评论 ·