spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cloud-fan <...@git.apache.org>
Subject [GitHub] spark pull request #16944: [SPARK-19611][SQL] Introduce configurable table s...
Date Thu, 09 Mar 2017 22:25:54 GMT
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16944#discussion_r105286553
  
    --- Diff: sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveSchemaInferenceSuite.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.hive
    +
    +import java.io.File
    +import java.util.concurrent.{Executors, TimeUnit}
    +
    +import scala.util.Random
    +
    +import org.scalatest.BeforeAndAfterEach
    +
    +import org.apache.spark.metrics.source.HiveCatalogMetrics
    +import org.apache.spark.sql.catalyst.TableIdentifier
    +import org.apache.spark.sql.catalyst.catalog._
    +import org.apache.spark.sql.execution.datasources.FileStatusCache
    +import org.apache.spark.sql.QueryTest
    +import org.apache.spark.sql.hive.client.HiveClient
    +import org.apache.spark.sql.hive.test.TestHiveSingleton
    +import org.apache.spark.sql.internal.{HiveSerDe, SQLConf}
    +import org.apache.spark.sql.internal.SQLConf.HiveCaseSensitiveInferenceMode.{Value =>
InferenceMode, _}
    +import org.apache.spark.sql.test.SQLTestUtils
    +import org.apache.spark.sql.types._
    +
    +class HiveSchemaInferenceSuite
    +  extends QueryTest with TestHiveSingleton with SQLTestUtils with BeforeAndAfterEach
{
    +
    +  import HiveSchemaInferenceSuite._
    +  import HiveExternalCatalog.DATASOURCE_SCHEMA_PREFIX
    +
    +  override def beforeEach(): Unit = {
    +    super.beforeEach()
    +    FileStatusCache.resetForTesting()
    +  }
    +
    +  override def afterEach(): Unit = {
    +    super.afterEach()
    +    spark.sessionState.catalog.tableRelationCache.invalidateAll()
    +    FileStatusCache.resetForTesting()
    +  }
    +
    +  private val externalCatalog = spark.sharedState.externalCatalog.asInstanceOf[HiveExternalCatalog]
    +  private val client = externalCatalog.client
    +
    +  // Return a copy of the given schema with all field names converted to lower case.
    +  private def lowerCaseSchema(schema: StructType): StructType = {
    +    StructType(schema.map(f => f.copy(name = f.name.toLowerCase)))
    +  }
    +
    +  // Create a Hive external test table containing the given field and partition column
names.
    +  // Returns a case-sensitive schema for the table.
    +  private def setupExternalTable(
    +      fileType: String,
    +      fields: Seq[String],
    +      partitionCols: Seq[String],
    +      dir: File): StructType = {
    +    // Treat all table fields as bigints...
    +    val structFields = fields.map { field =>
    +      StructField(
    +        name = field,
    +        dataType = LongType,
    +        nullable = true,
    +        metadata = new MetadataBuilder().putString(HIVE_TYPE_STRING, "bigint").build())
    +    }
    +    // and all partition columns as ints
    +    val partitionStructFields = partitionCols.map { field =>
    +      StructField(
    +        // Partition column case isn't preserved
    +        name = field.toLowerCase,
    +        dataType = IntegerType,
    +        nullable = true,
    +        metadata = new MetadataBuilder().putString(HIVE_TYPE_STRING, "int").build())
    +    }
    +    val schema = StructType(structFields ++ partitionStructFields)
    +
    +    // Write some test data (partitioned if specified)
    +    val writer = spark.range(NUM_RECORDS)
    +      .selectExpr((fields ++ partitionCols).map("id as " + _): _*)
    +      .write
    +      .partitionBy(partitionCols: _*)
    +      .mode("overwrite")
    +    fileType match {
    +      case ORC_FILE_TYPE =>
    +       writer.orc(dir.getAbsolutePath)
    +      case PARQUET_FILE_TYPE =>
    +       writer.parquet(dir.getAbsolutePath)
    +    }
    +
    +    // Create Hive external table with lowercased schema
    +    val serde = HiveSerDe.serdeMap(fileType)
    +    client.createTable(
    +      CatalogTable(
    +        identifier = TableIdentifier(table = TEST_TABLE_NAME, database = Option(DATABASE)),
    +        tableType = CatalogTableType.EXTERNAL,
    +        storage = CatalogStorageFormat(
    +          locationUri = Option(new java.net.URI(dir.getAbsolutePath)),
    +          inputFormat = serde.inputFormat,
    +          outputFormat = serde.outputFormat,
    +          serde = serde.serde,
    +          compressed = false,
    +          properties = Map("serialization.format" -> "1")),
    +        schema = schema,
    +        provider = Option("hive"),
    +        partitionColumnNames = partitionCols.map(_.toLowerCase),
    +        properties = Map.empty),
    +      true)
    +
    +    // Add partition records (if specified)
    +    if (!partitionCols.isEmpty) {
    +      spark.catalog.recoverPartitions(TEST_TABLE_NAME)
    +    }
    +
    +    // Check that the table returned by HiveExternalCatalog has schemaPreservesCase set
to false
    +    // and that the raw table returned by the Hive client doesn't have any Spark SQL
properties
    +    // set (table needs to be obtained from client since HiveExternalCatalog filters
these
    +    // properties out).
    +    assert(!externalCatalog.getTable(DATABASE, TEST_TABLE_NAME).schemaPreservesCase)
    +    val rawTable = client.getTable(DATABASE, TEST_TABLE_NAME)
    +    assert(rawTable.properties.filterKeys(_.startsWith(DATASOURCE_SCHEMA_PREFIX)) ==
Map.empty)
    +    schema
    +  }
    +
    +  private def withTestTables(
    +    fileType: String)(f: (Seq[String], Seq[String], StructType) => Unit): Unit = {
    +    // Test both a partitioned and unpartitioned Hive table
    +    val tableFields = Seq(
    +      (Seq("fieldOne"), Seq("partCol1", "partCol2")),
    +      (Seq("fieldOne", "fieldTwo"), Seq.empty[String]))
    +
    +    tableFields.foreach { case (fields, partCols) =>
    +      withTempDir { dir =>
    +        val schema = setupExternalTable(fileType, fields, partCols, dir)
    +        withTable(TEST_TABLE_NAME) { f(fields, partCols, schema) }
    +      }
    +    }
    +  }
    +
    +  private def withFileTypes(f: (String) => Unit): Unit
    +    = Seq(ORC_FILE_TYPE, PARQUET_FILE_TYPE).foreach(f)
    +
    +  private def withInferenceMode(mode: InferenceMode)(f: => Unit): Unit = {
    +    withSQLConf(
    +      HiveUtils.CONVERT_METASTORE_ORC.key -> "true",
    +      SQLConf.HIVE_CASE_SENSITIVE_INFERENCE.key -> mode.toString)(f)
    +  }
    +
    +  private val inferenceKey = SQLConf.HIVE_CASE_SENSITIVE_INFERENCE.key
    +
    +  private def testFieldQuery(fields: Seq[String]): Unit = {
    +    if (!fields.isEmpty) {
    +      val query = s"SELECT * FROM ${TEST_TABLE_NAME} WHERE ${Random.shuffle(fields).head}
>= 0"
    +      assert(spark.sql(query).count == NUM_RECORDS)
    +    }
    +  }
    +
    +  private def testTableSchema(expectedSchema: StructType): Unit
    +    = assert(spark.table(TEST_TABLE_NAME).schema == expectedSchema)
    +
    +  withFileTypes { fileType =>
    +    test(s"$fileType: schema should be inferred and saved when INFER_AND_SAVE is specified")
{
    +      withInferenceMode(INFER_AND_SAVE) {
    +        withTestTables(fileType) { (fields, partCols, schema) =>
    +          testFieldQuery(fields)
    +          testFieldQuery(partCols)
    +          testTableSchema(schema)
    +
    +          // Verify the catalog table now contains the updated schema and properties
    +          val catalogTable = externalCatalog.getTable(DATABASE, TEST_TABLE_NAME)
    +          assert(catalogTable.schemaPreservesCase)
    +          assert(catalogTable.schema == schema)
    +          assert(catalogTable.partitionColumnNames == partCols.map(_.toLowerCase))
    --- End diff --
    
    When we infer the data schema, we have to list all the files first, which means we already
know all the partition paths.


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


Mime
View raw message