-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27085][SQL] Migrate CSV to File Data Source V2 #24005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce1a4e4
csv data source V2
gengliangwang b363a00
disable csv write path
gengliangwang 5288011
fix
gengliangwang 3f6df2d
address comments
gengliangwang 256e177
use caseSensitive Map for newHadoopConfWithOptions
gengliangwang 2be5277
fix test
gengliangwang 7eb54a3
address comments
gengliangwang 689f17e
revise
gengliangwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
...core/src/main/resources/META-INF/services/org.apache.spark.sql.sources.DataSourceRegister
This file contains hidden or 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 hidden or 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 hidden or 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
57 changes: 57 additions & 0 deletions
57
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CsvOutputWriter.scala
This file contains hidden or 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,57 @@ | ||
| /* | ||
| * 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.execution.datasources.csv | ||
|
|
||
| import java.nio.charset.Charset | ||
|
|
||
| import org.apache.hadoop.fs.Path | ||
| import org.apache.hadoop.mapreduce.TaskAttemptContext | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.csv.{CSVOptions, UnivocityGenerator} | ||
| import org.apache.spark.sql.execution.datasources.{CodecStreams, OutputWriter} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| class CsvOutputWriter( | ||
| path: String, | ||
| dataSchema: StructType, | ||
| context: TaskAttemptContext, | ||
| params: CSVOptions) extends OutputWriter with Logging { | ||
|
|
||
| private var univocityGenerator: Option[UnivocityGenerator] = None | ||
|
|
||
| if (params.headerFlag) { | ||
| val gen = getGen() | ||
| gen.writeHeaders() | ||
| } | ||
|
|
||
| private def getGen(): UnivocityGenerator = univocityGenerator.getOrElse { | ||
| val charset = Charset.forName(params.charset) | ||
| val os = CodecStreams.createOutputStreamWriter(context, new Path(path), charset) | ||
| val newGen = new UnivocityGenerator(dataSchema, os, params) | ||
| univocityGenerator = Some(newGen) | ||
| newGen | ||
| } | ||
|
|
||
| override def write(row: InternalRow): Unit = { | ||
| val gen = getGen() | ||
| gen.write(row) | ||
| } | ||
|
|
||
| override def close(): Unit = univocityGenerator.foreach(_.close()) | ||
| } |
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
...ain/scala/org/apache/spark/sql/execution/datasources/v2/PartitionReaderFromIterator.scala
This file contains hidden or 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,37 @@ | ||
| /* | ||
| * 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.execution.datasources.v2 | ||
|
|
||
| import org.apache.spark.sql.sources.v2.reader.PartitionReader | ||
|
|
||
| class PartitionReaderFromIterator[InternalRow]( | ||
| iter: Iterator[InternalRow]) extends PartitionReader[InternalRow] { | ||
| private var currentValue: InternalRow = _ | ||
|
|
||
| override def next(): Boolean = { | ||
| if (iter.hasNext) { | ||
| currentValue = iter.next() | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| override def get(): InternalRow = currentValue | ||
|
|
||
| override def close(): Unit = {} | ||
| } |
45 changes: 45 additions & 0 deletions
45
...core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/TextBasedFileScan.scala
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * 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.execution.datasources.v2 | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| import org.apache.hadoop.fs.Path | ||
| import org.apache.hadoop.io.compress.{CompressionCodecFactory, SplittableCompressionCodec} | ||
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| abstract class TextBasedFileScan( | ||
| sparkSession: SparkSession, | ||
| fileIndex: PartitioningAwareFileIndex, | ||
| readSchema: StructType, | ||
| options: CaseInsensitiveStringMap) | ||
| extends FileScan(sparkSession, fileIndex, readSchema, options) { | ||
| private var codecFactory: CompressionCodecFactory = _ | ||
|
|
||
| override def isSplitable(path: Path): Boolean = { | ||
| if (codecFactory == null) { | ||
| codecFactory = new CompressionCodecFactory( | ||
| sparkSession.sessionState.newHadoopConfWithOptions(options.asScala.toMap)) | ||
| } | ||
| val codec = codecFactory.getCodec(path) | ||
| codec == null || codec.isInstanceOf[SplittableCompressionCodec] | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
...re/src/main/scala/org/apache/spark/sql/execution/datasources/v2/csv/CSVDataSourceV2.scala
This file contains hidden or 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.execution.datasources.v2.csv | ||
|
|
||
| import org.apache.spark.sql.execution.datasources.FileFormat | ||
| import org.apache.spark.sql.execution.datasources.csv.CSVFileFormat | ||
| import org.apache.spark.sql.execution.datasources.v2.FileDataSourceV2 | ||
| import org.apache.spark.sql.sources.v2.Table | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| class CSVDataSourceV2 extends FileDataSourceV2 { | ||
|
|
||
| override def fallBackFileFormat: Class[_ <: FileFormat] = classOf[CSVFileFormat] | ||
|
|
||
| override def shortName(): String = "csv" | ||
|
|
||
| override def getTable(options: CaseInsensitiveStringMap): Table = { | ||
| val paths = getPaths(options) | ||
| val tableName = getTableName(paths) | ||
| CSVTable(tableName, sparkSession, options, paths, None) | ||
| } | ||
|
|
||
| override def getTable(options: CaseInsensitiveStringMap, schema: StructType): Table = { | ||
| val paths = getPaths(options) | ||
| val tableName = getTableName(paths) | ||
| CSVTable(tableName, sparkSession, options, paths, Some(schema)) | ||
| } | ||
| } | ||
|
|
||
| object CSVDataSourceV2 { | ||
| def supportsDataType(dataType: DataType): Boolean = dataType match { | ||
| case _: AtomicType => true | ||
|
|
||
| case udt: UserDefinedType[_] => supportsDataType(udt.sqlType) | ||
|
|
||
| case _ => false | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
...n/scala/org/apache/spark/sql/execution/datasources/v2/csv/CSVPartitionReaderFactory.scala
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * 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.execution.datasources.v2.csv | ||
|
|
||
| import org.apache.spark.broadcast.Broadcast | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.csv.{CSVHeaderChecker, CSVOptions, UnivocityParser} | ||
| import org.apache.spark.sql.execution.datasources.PartitionedFile | ||
| import org.apache.spark.sql.execution.datasources.csv.CSVDataSource | ||
| import org.apache.spark.sql.execution.datasources.v2._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.sources.v2.reader.PartitionReader | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.util.SerializableConfiguration | ||
|
|
||
| /** | ||
| * A factory used to create CSV readers. | ||
| * | ||
| * @param sqlConf SQL configuration. | ||
| * @param broadcastedConf Broadcasted serializable Hadoop Configuration. | ||
| * @param dataSchema Schema of CSV files. | ||
| * @param partitionSchema Schema of partitions. | ||
| * @param readSchema Required schema in the batch scan. | ||
| * @param parsedOptions Options for parsing CSV files. | ||
| */ | ||
| case class CSVPartitionReaderFactory( | ||
| sqlConf: SQLConf, | ||
| broadcastedConf: Broadcast[SerializableConfiguration], | ||
| dataSchema: StructType, | ||
| partitionSchema: StructType, | ||
| readSchema: StructType, | ||
| parsedOptions: CSVOptions) extends FilePartitionReaderFactory { | ||
| private val columnPruning = sqlConf.csvColumnPruning | ||
| private val readDataSchema = | ||
| getReadDataSchema(readSchema, partitionSchema, sqlConf.caseSensitiveAnalysis) | ||
|
|
||
| override def buildReader(file: PartitionedFile): PartitionReader[InternalRow] = { | ||
| val conf = broadcastedConf.value.value | ||
|
|
||
| val parser = new UnivocityParser( | ||
| StructType(dataSchema.filterNot(_.name == parsedOptions.columnNameOfCorruptRecord)), | ||
| StructType(readDataSchema.filterNot(_.name == parsedOptions.columnNameOfCorruptRecord)), | ||
| parsedOptions) | ||
| val schema = if (columnPruning) readDataSchema else dataSchema | ||
| val isStartOfFile = file.start == 0 | ||
| val headerChecker = new CSVHeaderChecker( | ||
| schema, parsedOptions, source = s"CSV file: ${file.filePath}", isStartOfFile) | ||
| val iter = CSVDataSource(parsedOptions).readFile( | ||
| conf, | ||
| file, | ||
| parser, | ||
| headerChecker, | ||
| readDataSchema) | ||
| val fileReader = new PartitionReaderFromIterator[InternalRow](iter) | ||
| new PartitionReaderWithPartitionValues(fileReader, readDataSchema, | ||
| partitionSchema, file.partitionValues) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.