Github user HyukjinKwon commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10615#discussion_r48933011
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVRelation.scala
 ---
    @@ -0,0 +1,305 @@
    +/*
    + * 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.{FileStatus, Path}
    +import org.apache.hadoop.io.{Text, NullWritable, LongWritable}
    +import org.apache.hadoop.mapred.TextInputFormat
    +import org.apache.hadoop.mapreduce.RecordWriter
    +import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat
    +
    +import com.google.common.base.Objects
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql._
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.sources._
    +import org.apache.hadoop.mapreduce.{TaskAttemptContext, Job}
    +import org.apache.spark.sql.types._
    +
    +private[csv] class CSVRelation(
    +    private val inputRDD: Option[RDD[String]],
    +    override val paths: Array[String],
    +    private val maybeDataSchema: Option[StructType],
    +    override val userDefinedPartitionColumns: Option[StructType],
    +    private val parameters: Map[String, String])
    +    (@transient val sqlContext: SQLContext) extends HadoopFsRelation with 
Serializable {
    +
    +
    +  override lazy val dataSchema: StructType = maybeDataSchema match {
    +    case Some(structType) => structType
    +    case None => inferSchema(paths)
    +  }
    +
    +  private val params = new CSVParameters(parameters)
    +
    +  @transient
    +  private var cachedRDD: Option[RDD[String]] = None
    +
    +  private def readText(location: String): RDD[String] = {
    +    if (Charset.forName(params.charset) == Charset.forName("UTF-8")) {
    +      sqlContext.sparkContext.textFile(location)
    +    } else {
    +      sqlContext.sparkContext.hadoopFile[LongWritable, Text, 
TextInputFormat](location)
    +        .mapPartitions { _.map { pair =>
    +            new String(pair._2.getBytes, 0, pair._2.getLength, 
params.charset)
    +          }
    +        }
    +    }
    +  }
    +
    +  private def baseRdd(inputPaths: Array[String]): RDD[String] = {
    +    inputRDD.getOrElse {
    +      cachedRDD.getOrElse {
    +        val rdd = readText(inputPaths.mkString(","))
    +        cachedRDD = Some(rdd)
    +        rdd
    +      }
    +    }
    +  }
    +
    +  private def tokenRdd(header: Array[String], inputPaths: Array[String]): 
RDD[Array[String]] = {
    +    val rdd = baseRdd(inputPaths)
    +    // Make sure firstLine is materialized before sending to executors
    +    val firstLine = if (params.headerFlag) findFirstLine(rdd) else null
    +    CSVRelation.univocityTokenizer(rdd, header, firstLine, params)
    +  }
    +
    +  /**
    +    * This supports to eliminate unneeded columns before producing an RDD
    +    * containing all of its tuples as Row objects. This reads all the 
tokens of each line
    +    * and then drop unneeded tokens without casting and type-checking by 
mapping
    +    * both the indices produced by `requiredColumns` and the ones of 
tokens.
    +    * TODO: Switch to using buildInternalScan
    +    */
    +  override def buildScan(requiredColumns: Array[String], inputs: 
Array[FileStatus]): RDD[Row] = {
    +    val pathsString = inputs.map(_.getPath.toUri.toString)
    +    val header = schema.fields.map(_.name)
    +    val tokenizedRdd = tokenRdd(header, pathsString)
    +    CSVRelation.parseCsv(tokenizedRdd, schema, requiredColumns, inputs, 
sqlContext, params)
    +  }
    +
    +  override def prepareJobForWrite(job: Job): OutputWriterFactory = {
    +    new CSVOutputWriterFactory(params)
    +  }
    +
    +  override def hashCode(): Int = Objects.hashCode(paths.toSet, dataSchema, 
schema, partitionColumns)
    +
    +  override def equals(other: Any): Boolean = other match {
    +    case that: CSVRelation => {
    +      val equalPath = paths.toSet == that.paths.toSet
    +      val equalDataSchema = dataSchema == that.dataSchema
    +      val equalSchema = schema == that.schema
    +      val equalPartitionColums = partitionColumns == that.partitionColumns
    +
    +      equalPath && equalDataSchema && equalSchema && equalPartitionColums
    +    }
    +    case _ => false
    +  }
    +
    +  private def inferSchema(paths: Array[String]): StructType = {
    +    val rdd = baseRdd(Array(paths.head))
    +    val firstLine = findFirstLine(rdd)
    +    val firstRow = new LineCsvReader(params).parseLine(firstLine)
    +
    +    val header = if (params.headerFlag) {
    +      firstRow
    +    } else {
    +      firstRow.zipWithIndex.map { case (value, index) => s"C$index" }
    +    }
    +
    +    val parsedRdd = tokenRdd(header, paths)
    +    if (params.inferSchemaFlag) {
    +      CSVInferSchema(parsedRdd, header)
    +    } else {
    +      // By default fields are assumed to be StringType
    +      val schemaFields = header.map { fieldName =>
    +        StructField(fieldName.toString, StringType, nullable = true)
    +      }
    +      StructType(schemaFields)
    +    }
    +  }
    +
    +  /**
    +    * Returns the first line of the first non-empty file in path
    +    */
    +  private def findFirstLine(rdd: RDD[String]): String = {
    +    if (params.isCommentSet) {
    +      rdd.take(params.MAX_COMMENT_LINES_IN_HEADER)
    +        .find(!_.startsWith(params.comment.toString))
    +        .getOrElse(sys.error(s"No uncommented header line in " +
    +          s"first ${params.MAX_COMMENT_LINES_IN_HEADER} lines"))
    +    } else {
    +      rdd.first()
    +    }
    +  }
    +}
    +
    +object CSVRelation extends Logging {
    +
    +  def univocityTokenizer(
    +      file: RDD[String],
    +      header: Seq[String],
    +      firstLine: String,
    +      params: CSVParameters): RDD[Array[String]] = {
    +    // If header is set, make sure firstLine is materialized before 
sending to executors.
    +    file.mapPartitionsWithIndex({
    +      case (split, iter) => new BulkCsvReader(
    +        if (params.headerFlag) iter.filterNot(_ == firstLine) else iter,
    +        params,
    +        headers = header)
    +    }, true)
    +  }
    +
    +
    +  def parseCsv(
    +      tokenizedRDD: RDD[Array[String]],
    +      schema: StructType,
    +      requiredColumns: Array[String],
    +      inputs: Array[FileStatus],
    +      sqlContext: SQLContext,
    +      params: CSVParameters): RDD[Row] = {
    +
    +    val schemaFields = schema.fields
    +    val requiredFields = StructType(requiredColumns.map(schema(_))).fields
    +    val safeRequiredFields = if (params.dropMalformed) {
    +      // If `dropMalformed` is enabled, then it needs to parse all the 
values
    +      // so that we can decide which row is malformed.
    +      requiredFields ++ schemaFields.filterNot(requiredFields.contains(_))
    +    } else {
    +      requiredFields
    +    }
    +    //println("safeRequiredFields: " + safeRequiredFields.mkString("\n\t"))
    --- End diff --
    
    Lastly, here I believe this comment is meant to be removed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to