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

    https://github.com/apache/spark/pull/5526#discussion_r29015520
  
    --- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/sources/FSBasedRelationSuite.scala 
---
    @@ -0,0 +1,425 @@
    +/*
    + * 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.sources
    +
    +import java.io.IOException
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.hadoop.fs.{FileSystem, Path}
    +
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.sql.types._
    +import org.apache.spark.sql.{Row, SQLContext, SaveMode}
    +import org.apache.spark.util.Utils
    +
    +class SimpleFSBasedSource extends RelationProvider {
    +  override def createRelation(
    +      sqlContext: SQLContext,
    +      parameters: Map[String, String]): BaseRelation = {
    +    SimpleFSBasedRelation(parameters)(sqlContext)
    +  }
    +}
    +
    +case class SimpleFSBasedRelation
    +    (parameter: Map[String, String])
    +    (val sqlContext: SQLContext)
    +  extends FSBasedRelation {
    +
    +  class SimpleOutputWriter extends OutputWriter {
    +    override def write(row: Row): Unit = TestResult.writtenRows += row
    +  }
    +
    +  override val path = parameter("path")
    +
    +  override def dataSchema: StructType =
    +    DataType.fromJson(parameter("schema")).asInstanceOf[StructType]
    +
    +  override def buildScan(
    +      requiredColumns: Array[String],
    +      filters: Array[Filter],
    +      inputPaths: Array[String]): RDD[Row] = {
    +    val basePath = new Path(path)
    +    val fs = 
basePath.getFileSystem(sqlContext.sparkContext.hadoopConfiguration)
    +
    +    // Used to test queries like "INSERT OVERWRITE tbl SELECT * FROM tbl". 
Source directory
    +    // shouldn't be removed before scanning the data.
    +    assert(inputPaths.map(new Path(_)).forall(fs.exists))
    +
    +    TestResult.requiredColumns = requiredColumns
    +    TestResult.filters = filters
    +    TestResult.inputPaths = inputPaths
    +
    +    Option(TestResult.rowsToRead).getOrElse(sqlContext.emptyResult)
    +  }
    +
    +  override def outputWriterClass: Class[_ <: OutputWriter] = 
classOf[SimpleOutputWriter]
    +}
    +
    +object TestResult {
    +  var requiredColumns: Array[String] = _
    +  var filters: Array[Filter] = _
    +  var inputPaths: Array[String] = _
    +  var rowsToRead: RDD[Row] = _
    +  var writtenRows: ArrayBuffer[Row] = ArrayBuffer.empty[Row]
    +
    +  def reset(): Unit = {
    +    requiredColumns = null
    +    filters = null
    +    inputPaths = null
    +    rowsToRead = null
    +    writtenRows.clear()
    +  }
    +}
    +
    +class FSBasedRelationSuite extends DataSourceTest {
    +  import caseInsensitiveContext._
    +  import caseInsensitiveContext.implicits._
    +
    +  var basePath: Path = _
    +
    +  var fs: FileSystem = _
    +
    +  val dataSchema =
    +    StructType(
    +      Seq(
    +        StructField("a", IntegerType, nullable = false),
    +        StructField("b", StringType, nullable = false)))
    +
    +  val testDF = (for {
    +    i <- 1 to 3
    +    p <- 1 to 2
    +  } yield (i, s"val_$i", p)).toDF("a", "b", "p1")
    +
    +  before {
    +    basePath = new Path(Utils.createTempDir().getCanonicalPath)
    +    fs = basePath.getFileSystem(sparkContext.hadoopConfiguration)
    +    TestResult.reset()
    +  }
    +
    +  ignore("load() - partitioned table - partition column not included in 
data files") {
    +    fs.mkdirs(new Path(basePath, "p1=1/p2=hello"))
    +    fs.mkdirs(new Path(basePath, "p1=2/p2=world"))
    +
    +    val df = load(
    +      source = classOf[SimpleFSBasedSource].getCanonicalName,
    +      options = Map(
    +        "path" -> basePath.toString,
    +        "schema" -> dataSchema.json))
    +
    +    df.queryExecution.analyzed.collect {
    +      case LogicalRelation(relation: SimpleFSBasedRelation) =>
    +        assert(relation.dataSchema === dataSchema)
    +      case _ =>
    +        fail("Couldn't find expected SimpleFSBasedRelation instance")
    +    }
    +
    +    val expectedSchema =
    +      StructType(
    +        dataSchema ++ Seq(
    +          StructField("p1", IntegerType, nullable = true),
    +          StructField("p2", StringType, nullable = true)))
    +
    +    assert(df.schema === expectedSchema)
    +
    +    df.select("b").where($"a" > 0 && $"p1" === 1).collect()
    +
    +    // Check for column pruning, filter push-down, and partition pruning
    +    assert(TestResult.requiredColumns.toSet === Set("a", "b"))
    +    assert(TestResult.filters === Seq(GreaterThan("a", 0)))
    +    assert(TestResult.inputPaths === Seq(new Path(basePath, 
"p1=1").toString))
    +  }
    +
    +  ignore("load() - partitioned table - partition column included in data 
files") {
    +    val data = sparkContext.parallelize(Seq.empty[String])
    +    data.saveAsTextFile(new Path(basePath, "p1=1/p2=hello").toString)
    +    data.saveAsTextFile(new Path(basePath, "p1=2/p2=world").toString)
    +
    +    val dataSchema =
    +      StructType(
    +        Seq(
    +          StructField("a", IntegerType, nullable = false),
    +          StructField("p1", IntegerType, nullable = true),
    +          StructField("b", StringType, nullable = false)))
    +
    +    val df = load(
    +      source = classOf[SimpleFSBasedSource].getCanonicalName,
    +      options = Map(
    +        "path" -> basePath.toString,
    +        "schema" -> dataSchema.json))
    +
    +    df.queryExecution.analyzed.collect {
    +      case LogicalRelation(relation: SimpleFSBasedRelation) =>
    +        assert(relation.dataSchema === dataSchema)
    +      case _ =>
    +        fail("Couldn't find expected SimpleFSBasedRelation instance")
    +    }
    +
    +    val expectedSchema =
    +      StructType(
    +        Seq(
    +          StructField("a", IntegerType, nullable = false),
    +          StructField("p1", IntegerType, nullable = true),
    +          StructField("b", StringType, nullable = false),
    +          StructField("p2", StringType, nullable = true)))
    +
    +    assert(df.schema === expectedSchema)
    +
    +    df.select("b").where($"a" > 0 && $"p1" === 1).collect()
    +
    +    // Check for column pruning, filter push-down, and partition pruning
    +    assert(TestResult.requiredColumns.toSet === Set("a", "b"))
    +    assert(TestResult.filters === Seq(GreaterThan("a", 0)))
    +    assert(TestResult.inputPaths === Seq(new Path(basePath, 
"p1=1").toString))
    +  }
    +
    +  ignore("save() - partitioned table - Overwrite") {
    +    testDF.save(
    +      source = classOf[SimpleFSBasedSource].getCanonicalName,
    +      mode = SaveMode.Overwrite,
    +      options = Map(
    +        "path" -> basePath.toString,
    +        "schema" -> dataSchema.json),
    +      partitionColumns = Seq("p1"))
    +
    +    // Written rows shouldn't contain dynamic partition column
    +    val expectedRows = for (i <- 1 to 3; _ <- 1 to 2) yield Row(i, 
s"val_$i")
    +    assert(TestResult.writtenRows.sameElements(expectedRows))
    +  }
    +
    +  ignore("save() - partitioned table - Overwrite - select and overwrite 
the same table") {
    +    TestResult.rowsToRead = testDF.rdd
    +
    +    val df = load(
    +      source = classOf[SimpleFSBasedSource].getCanonicalName,
    +      options = Map(
    +        "path" -> basePath.toString,
    +        "schema" -> dataSchema.json))
    +
    +    df.save(
    +      source = classOf[SimpleFSBasedSource].getCanonicalName,
    +      mode = SaveMode.Overwrite,
    +      options = Map(
    +        "path" -> basePath.toString,
    +        "schema" -> dataSchema.json),
    +      partitionColumns = Seq("p1"))
    +
    +    // Written rows shouldn't contain dynamic partition column
    +    val expectedRows = for (i <- 1 to 3; _ <- 1 to 2) yield Row(i, 
s"val_$i")
    +    assert(TestResult.writtenRows.sameElements(expectedRows))
    +  }
    +
    +  ignore("save() - partitioned table - Append") {
    --- End diff --
    
    Also, when we append to existing dataset, what will happen if the 
partitioning columns provided in `save` does not match those encoded by the dir 
structure. For example, we say `partitionColumns = Seq("p1")`, but the existing 
dir struct is like `/p1=.../p2=...`. 
    
    Also, what will happen if the ordering of partitioning columns does not 
match? For example, we say `partitionColumns = Seq("p1", "p2")`, but the 
existing dir struct is like `/p2=.../p1=...`. 


---
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]

Reply via email to