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

    https://github.com/apache/spark/pull/15090#discussion_r79335325
  
    --- Diff: 
sql/hive/src/test/scala/org/apache/spark/sql/hive/StatisticsColumnSuite.scala 
---
    @@ -0,0 +1,228 @@
    +/*
    + * 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.sql.{Date, Timestamp}
    +
    +import org.apache.spark.sql.{AnalysisException, Row}
    +import org.apache.spark.sql.catalyst.plans.logical.BasicColStats
    +import org.apache.spark.sql.execution.command.AnalyzeColumnCommand
    +import org.apache.spark.sql.types._
    +
    +class StatisticsColumnSuite extends StatisticsTest {
    +
    +  test("parse analyze column commands") {
    +    val table = "table"
    +    assertAnalyzeCommand(
    +      s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS key, value",
    +      classOf[AnalyzeColumnCommand])
    +
    +    val noColumnError = intercept[AnalysisException] {
    +      sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS")
    +    }
    +    assert(noColumnError.message == "Need to specify the columns to 
analyze. Usage: " +
    +      "ANALYZE TABLE tbl COMPUTE STATISTICS FOR COLUMNS key, value")
    +
    +    withTable(table) {
    +      sql(s"CREATE TABLE $table (key INT, value STRING)")
    +      val invalidColError = intercept[AnalysisException] {
    +        sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS k")
    +      }
    +      assert(invalidColError.message == s"Invalid column name: k")
    +
    +      val duplicateColError = intercept[AnalysisException] {
    +        sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS key, 
value, key")
    +      }
    +      assert(duplicateColError.message == s"Duplicate column name: key")
    +
    +      withSQLConf("spark.sql.caseSensitive" -> "true") {
    +        val invalidErr = intercept[AnalysisException] {
    +          sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS keY")
    +        }
    +        assert(invalidErr.message == s"Invalid column name: keY")
    +      }
    +
    +      withSQLConf("spark.sql.caseSensitive" -> "false") {
    +        val duplicateErr = intercept[AnalysisException] {
    +          sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS key, 
value, vaLue")
    +        }
    +        assert(duplicateErr.message == s"Duplicate column name: vaLue")
    +      }
    +    }
    +  }
    +
    +  test("basic statistics for integral type columns") {
    +    val rdd = sparkContext.parallelize(Seq("1", null, "2", "3", null)).map 
{ i =>
    +      if (i != null) Row(i.toByte, i.toShort, i.toInt, i.toLong) else 
Row(i, i, i, i)
    --- End diff --
    
    @wzhfy I guess he understood `"1", null, "2", "3", null` are the actual 
values for rows. Could we maybe make this easier to read? How about the codes 
below?
    
    ```scala
    val values = (0 to 5).map { i =>
      if (i % 2 == 0) None else Some(i)
    }
    val data = values.map { i =>
      (i.map(_.toByte), i.map(_.toShort), i.map(_.toInt), i.map(_.toLong))
    }
    
    val df = data.toDF("c1", "c2", "c3", "c4")
    val statsSeq = df.schema.map { f =>
      val basicStats = BasicColStats(
        dataType = f.dataType,
        numNulls = values.count(_.isDefined),
        max = values.filter(_.isDefined).max,
        min = values.filter(_.isDefined).min,
        ndv = Some(values.distinct.length.toLong))
      (f.name, basicStats)
    }
    
    checkColStats(df, statsSeq)
    ```
    
    with importing  `import testImplicits._` right below 
`StatisticsColumnSuite` and then changing `checkColStats`
    
    as below:
    
    ```scala
    def checkColStats(
        df: DataFrame,
        expectedColStatsSeq: Seq[(String, BasicColStats)]): Unit = {
      val table = "tbl"
      withTable(table) {
        df.write.format("json").saveAsTable(table)
        val columns = expectedColStatsSeq.map(_._1).mkString(", ")
        sql(s"ANALYZE TABLE $table COMPUTE STATISTICS FOR COLUMNS $columns")
        val readback = sql(s"SELECT * FROM $table")
        val stats = readback.queryExecution.analyzed.collect {
          case rel: LogicalRelation =>
            expectedColStatsSeq.foreach { expected =>
              
assert(rel.catalogTable.get.stats.get.basicColStats.contains(expected._1))
              checkColStats(colStats = 
rel.catalogTable.get.stats.get.basicColStats(expected._1),
                expectedColStats = expected._2)
            }
        }
        assert(stats.size == 1)
      }
    }
    ```


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