Github user falaki commented on a diff in the pull request:
https://github.com/apache/spark/pull/10766#discussion_r49899163
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala
---
@@ -0,0 +1,227 @@
+/*
+ * 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.math.BigDecimal
+import java.sql.{Date, Timestamp}
+import java.text.NumberFormat
+import java.util.Locale
+
+import scala.util.control.Exception._
+import scala.util.Try
+
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.analysis.HiveTypeCoercion
+import org.apache.spark.sql.types._
+
+
+private[sql] object CSVInferSchema {
+
+ /**
+ * Similar to the JSON schema inference
+ * 1. Infer type of each row
+ * 2. Merge row types to find common type
+ * 3. Replace any null types with string type
+ * TODO(hossein): Can we reuse JSON schema inference? [SPARK-12670]
+ */
+ def apply(
+ tokenRdd: RDD[Array[String]],
+ header: Array[String],
+ nullValue: String = ""): StructType = {
+
+ val startType: Array[DataType] =
Array.fill[DataType](header.length)(NullType)
+ val rootTypes: Array[DataType] =
+ tokenRdd.aggregate(startType)(inferRowType(nullValue), mergeRowTypes)
+
+ val structFields = header.zip(rootTypes).map { case (thisHeader,
rootType) =>
+ StructField(thisHeader, rootType, nullable = true)
+ }
+
+ StructType(structFields)
+ }
+
+ private def inferRowType(nullValue: String)
+ (rowSoFar: Array[DataType], next: Array[String]): Array[DataType] = {
+ var i = 0
+ while (i < math.min(rowSoFar.length, next.length)) { // May have
columns on right missing.
+ rowSoFar(i) = inferField(rowSoFar(i), next(i), nullValue)
+ i+=1
+ }
+ rowSoFar
+ }
+
+ private[csv] def mergeRowTypes(
+ first: Array[DataType],
+ second: Array[DataType]): Array[DataType] = {
+
+ first.zipAll(second, NullType, NullType).map { case ((a, b)) =>
+ val tpe = findTightestCommonType(a, b).getOrElse(StringType)
+ tpe match {
+ case _: NullType => StringType
+ case other => other
+ }
+ }
+ }
+
+ /**
+ * Infer type of string field. Given known type Double, and a string
"1", there is no
+ * point checking if it is an Int, as the final type must be Double or
higher.
+ */
+ private[csv] def inferField(
+ typeSoFar: DataType, field: String, nullValue: String = ""):
DataType = {
+ if (field == null || field.isEmpty || field == nullValue) {
+ typeSoFar
+ } else {
+ typeSoFar match {
+ case NullType => tryParseInteger(field)
--- End diff --
No it can be of type string if all other types fail to parse. Our order of
preference is:
`Int > Long > Double > Timestamp > String`
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]