forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-50067][SQL] Codegen Support for SchemaOfCsv(by Invoke & Runtim…
…eReplaceable) ### What changes were proposed in this pull request? The pr aims to add `Codegen` Support for `schema_of_csv`. ### Why are the changes needed? - improve codegen coverage. - simplified code. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Pass GA & Existed UT (eg: CsvFunctionsSuite#`*schema_of_csv*`) ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#48595 from panbingkun/SPARK-50067. Authored-by: panbingkun <[email protected]> Signed-off-by: Max Gekk <[email protected]>
- Loading branch information
1 parent
2cb7a16
commit 369c40c
Showing
4 changed files
with
69 additions
and
26 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...src/main/scala/org/apache/spark/sql/catalyst/expressions/csv/CsvExpressionEvalUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.sql.catalyst.expressions.csv | ||
|
||
import com.univocity.parsers.csv.CsvParser | ||
|
||
import org.apache.spark.sql.catalyst.csv.{CSVInferSchema, CSVOptions} | ||
import org.apache.spark.sql.types.{DataType, NullType, StructType} | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
case class SchemaOfCsvEvaluator(options: Map[String, String]) { | ||
|
||
@transient | ||
private lazy val csvOptions: CSVOptions = { | ||
// 'lineSep' is a plan-wise option so we set a noncharacter, according to | ||
// the unicode specification, which should not appear in Java's strings. | ||
// See also SPARK-38955 and https://www.unicode.org/charts/PDF/UFFF0.pdf. | ||
// scalastyle:off nonascii | ||
val exprOptions = options ++ Map("lineSep" -> '\uFFFF'.toString) | ||
// scalastyle:on nonascii | ||
new CSVOptions(exprOptions, true, "UTC") | ||
} | ||
|
||
@transient | ||
private lazy val csvParser: CsvParser = new CsvParser(csvOptions.asParserSettings) | ||
|
||
@transient | ||
private lazy val csvInferSchema = new CSVInferSchema(csvOptions) | ||
|
||
final def evaluate(csv: UTF8String): Any = { | ||
val row = csvParser.parseLine(csv.toString) | ||
assert(row != null, "Parsed CSV record should not be null.") | ||
val header = row.zipWithIndex.map { case (_, index) => s"_c$index" } | ||
val startType: Array[DataType] = Array.fill[DataType](header.length)(NullType) | ||
val fieldTypes = csvInferSchema.inferRowType(startType, row) | ||
val st = StructType(csvInferSchema.toStructFields(fieldTypes, header)) | ||
UTF8String.fromString(st.sql) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nect/common/src/test/resources/query-tests/explain-results/function_schema_of_csv.explain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [schema_of_csv(1|abc, (sep,|)) AS schema_of_csv(1|abc)#0] | ||
Project [invoke(SchemaOfCsvEvaluator(Map(sep -> |)).evaluate(1|abc)) AS schema_of_csv(1|abc)#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |