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

    https://github.com/apache/spark/pull/16674#discussion_r100235360
  
    --- Diff: 
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveSQLViewSuite.scala
 ---
    @@ -0,0 +1,190 @@
    +/*
    + * 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.execution
    +
    +import org.apache.spark.sql.{AnalysisException, Row, SaveMode, 
SparkSession}
    +import org.apache.spark.sql.catalyst.TableIdentifier
    +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, 
CatalogTable, CatalogTableType}
    +import org.apache.spark.sql.execution.SQLViewSuite
    +import org.apache.spark.sql.hive.test.{TestHive, TestHiveSingleton}
    +import org.apache.spark.sql.types.StructType
    +
    +/**
    + * A test suite for Hive view related functionality.
    + */
    +class HiveSQLViewSuite extends SQLViewSuite with TestHiveSingleton {
    +  protected override val spark: SparkSession = TestHive.sparkSession
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +    // Create a simple table with two columns: id and id1
    +    spark.range(1, 10).selectExpr("id", "id 
id1").write.format("json").saveAsTable("jt")
    +  }
    +
    +  override def afterAll(): Unit = {
    +    try {
    +      spark.sql(s"DROP TABLE IF EXISTS jt")
    +    } finally {
    +      super.afterAll()
    +    }
    +  }
    +
    +  import testImplicits._
    +
    +  test("create a permanent/temp view using a hive, built-in, and permanent 
user function") {
    +    val permanentFuncName = "myUpper"
    +    val permanentFuncClass =
    +      
classOf[org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper].getCanonicalName
    +    val builtInFuncNameInLowerCase = "abs"
    +    val builtInFuncNameInMixedCase = "aBs"
    +    val hiveFuncName = "histogram_numeric"
    +
    +    withUserDefinedFunction(permanentFuncName -> false) {
    +      sql(s"CREATE FUNCTION $permanentFuncName AS '$permanentFuncClass'")
    +      withTable("tab1") {
    +        (1 to 10).map(i => (s"$i", i)).toDF("str", 
"id").write.saveAsTable("tab1")
    +        Seq("VIEW", "TEMPORARY VIEW").foreach { viewMode =>
    +          withView("view1") {
    +            sql(
    +              s"""
    +                 |CREATE $viewMode view1
    +                 |AS SELECT
    +                 |$permanentFuncName(str),
    +                 |$builtInFuncNameInLowerCase(id),
    +                 |$builtInFuncNameInMixedCase(id) as aBs,
    +                 |$hiveFuncName(id, 5) over()
    +                 |FROM tab1
    +               """.stripMargin)
    +            checkAnswer(sql("select count(*) FROM view1"), Row(10))
    +          }
    +        }
    +      }
    +    }
    +  }
    +
    +  test("create a permanent/temp view using a temporary function") {
    +    val tempFunctionName = "temp"
    +    val functionClass =
    +      
classOf[org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper].getCanonicalName
    +    withUserDefinedFunction(tempFunctionName -> true) {
    +      sql(s"CREATE TEMPORARY FUNCTION $tempFunctionName AS 
'$functionClass'")
    +      withView("view1", "tempView1") {
    +        withTable("tab1") {
    +          (1 to 10).map(i => s"$i").toDF("id").write.saveAsTable("tab1")
    +
    +          // temporary view
    +          sql(s"CREATE TEMPORARY VIEW tempView1 AS SELECT 
$tempFunctionName(id) from tab1")
    +          checkAnswer(sql("select count(*) FROM tempView1"), Row(10))
    +
    +          // permanent view
    +          val e = intercept[AnalysisException] {
    +            sql(s"CREATE VIEW view1 AS SELECT $tempFunctionName(id) from 
tab1")
    +          }.getMessage
    +          assert(e.contains("Not allowed to create a permanent view 
`view1` by referencing " +
    +            s"a temporary function `$tempFunctionName`"))
    +        }
    +      }
    +    }
    +  }
    +
    +  test("create hive view for json table") {
    +    // json table is not hive-compatible, make sure the new flag fix it.
    +    withView("testView") {
    +      sql("CREATE VIEW testView AS SELECT id FROM jt")
    +      checkAnswer(sql("SELECT * FROM testView ORDER BY id"), (1 to 
9).map(i => Row(i)))
    +    }
    +  }
    +
    +  test("create hive view for partitioned parquet table") {
    +    // partitioned parquet table is not hive-compatible, make sure the new 
flag fix it.
    +    withTable("parTable") {
    +      withView("testView") {
    +        val df = Seq(1 -> "a").toDF("i", "j")
    +        df.write.format("parquet").partitionBy("i").saveAsTable("parTable")
    +        sql("CREATE VIEW testView AS SELECT i, j FROM parTable")
    +        checkAnswer(sql("SELECT * FROM testView"), Row(1, "a"))
    +      }
    +    }
    +  }
    +
    +  test("create hive view for joined tables") {
    --- End diff --
    
    Any reason why this is Hive only?


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