You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by flyjy <gi...@git.apache.org> on 2016/02/13 19:14:50 UTC

[GitHub] spark pull request: [SPARK-9763][SQL] Minimize exposure of interna...

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

    https://github.com/apache/spark/pull/8056#discussion_r52831393
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ResolvedDataSource.scala ---
    @@ -0,0 +1,204 @@
    +/*
    +* 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
    +
    +import java.util.ServiceLoader
    +
    +import scala.collection.JavaConversions._
    +import scala.language.{existentials, implicitConversions}
    +import scala.util.{Success, Failure, Try}
    +
    +import org.apache.hadoop.fs.Path
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.deploy.SparkHadoopUtil
    +import org.apache.spark.sql.{DataFrame, SaveMode, AnalysisException, SQLContext}
    +import org.apache.spark.sql.sources._
    +import org.apache.spark.sql.types.{CalendarIntervalType, StructType}
    +import org.apache.spark.util.Utils
    +
    +
    +case class ResolvedDataSource(provider: Class[_], relation: BaseRelation)
    +
    +
    +object ResolvedDataSource extends Logging {
    +
    +  /** A map to maintain backward compatibility in case we move data sources around. */
    +  private val backwardCompatibilityMap = Map(
    +    "org.apache.spark.sql.jdbc" -> classOf[jdbc.DefaultSource].getCanonicalName,
    +    "org.apache.spark.sql.jdbc.DefaultSource" -> classOf[jdbc.DefaultSource].getCanonicalName,
    +    "org.apache.spark.sql.json" -> classOf[json.DefaultSource].getCanonicalName,
    +    "org.apache.spark.sql.json.DefaultSource" -> classOf[json.DefaultSource].getCanonicalName,
    +    "org.apache.spark.sql.parquet" -> classOf[parquet.DefaultSource].getCanonicalName,
    +    "org.apache.spark.sql.parquet.DefaultSource" -> classOf[parquet.DefaultSource].getCanonicalName
    +  )
    +
    +  /** Given a provider name, look up the data source class definition. */
    +  def lookupDataSource(provider0: String): Class[_] = {
    +    val provider = backwardCompatibilityMap.getOrElse(provider0, provider0)
    +    val provider2 = s"$provider.DefaultSource"
    +    val loader = Utils.getContextOrSparkClassLoader
    +    val serviceLoader = ServiceLoader.load(classOf[DataSourceRegister], loader)
    +
    +    serviceLoader.iterator().filter(_.shortName().equalsIgnoreCase(provider)).toList match {
    +      /** the provider format did not match any given registered aliases */
    +      case Nil => Try(loader.loadClass(provider)).orElse(Try(loader.loadClass(provider2))) match {
    +        case Success(dataSource) => dataSource
    +        case Failure(error) =>
    +          if (provider.startsWith("org.apache.spark.sql.hive.orc")) {
    +            throw new ClassNotFoundException(
    +              "The ORC data source must be used with Hive support enabled.", error)
    +          } else {
    +            throw new ClassNotFoundException(
    +              s"Failed to load class for data source: $provider.", error)
    +          }
    +      }
    +      /** there is exactly one registered alias */
    +      case head :: Nil => head.getClass
    +      /** There are multiple registered aliases for the input */
    +      case sources => sys.error(s"Multiple sources found for $provider, " +
    +        s"(${sources.map(_.getClass.getName).mkString(", ")}), " +
    +        "please specify the fully qualified class name.")
    +    }
    +  }
    +
    +  /** Create a [[ResolvedDataSource]] for reading data in. */
    +  def apply(
    +      sqlContext: SQLContext,
    +      userSpecifiedSchema: Option[StructType],
    +      partitionColumns: Array[String],
    +      provider: String,
    +      options: Map[String, String]): ResolvedDataSource = {
    +    val clazz: Class[_] = lookupDataSource(provider)
    +    def className: String = clazz.getCanonicalName
    +    val relation = userSpecifiedSchema match {
    +      case Some(schema: StructType) => clazz.newInstance() match {
    +        case dataSource: SchemaRelationProvider =>
    +          dataSource.createRelation(sqlContext, new CaseInsensitiveMap(options), schema)
    +        case dataSource: HadoopFsRelationProvider =>
    +          val maybePartitionsSchema = if (partitionColumns.isEmpty) {
    +            None
    +          } else {
    +            Some(partitionColumnsSchema(schema, partitionColumns))
    +          }
    +
    +          val caseInsensitiveOptions = new CaseInsensitiveMap(options)
    +          val paths = {
    +            val patternPath = new Path(caseInsensitiveOptions("path"))
    +            val fs = patternPath.getFileSystem(sqlContext.sparkContext.hadoopConfiguration)
    +            val qualifiedPattern = patternPath.makeQualified(fs.getUri, fs.getWorkingDirectory)
    +            SparkHadoopUtil.get.globPathIfNecessary(qualifiedPattern).map(_.toString).toArray
    +          }
    +
    +          val dataSchema =
    +            StructType(schema.filterNot(f => partitionColumns.contains(f.name))).asNullable
    --- End diff --
    
    @rxin Following the discussion on [SPARK-9763](https://issues.apache.org/jira/browse/SPARK-9763), I am actually wondering why we convert the StructType with "asNullable" which set all the contained StructField to be Nullable. This will cause problem when one StructFiled is not allowed to be nullable, but the HadoopFsRelationProvider automatically sets it to be nullable. Is it because that all the fields in HadoopFsRelationProvider have to be nullable? Thanks!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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