aboutsummaryrefslogtreecommitdiff
path: root/sql/hive/src/test/scala/org/apache/spark/sql/hive/ErrorPositionSuite.scala
blob: d9664680f4a11c4a25b3316e2010e58bca43dd35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * 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 scala.util.Try

import org.scalatest.BeforeAndAfterEach

import org.apache.spark.sql.{AnalysisException, QueryTest}
import org.apache.spark.sql.catalyst.util.quietly
import org.apache.spark.sql.hive.execution.HiveSqlParser
import org.apache.spark.sql.hive.test.TestHiveSingleton

class ErrorPositionSuite extends QueryTest with TestHiveSingleton with BeforeAndAfterEach {
  import hiveContext.implicits._

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    if (sqlContext.tableNames().contains("src")) {
      sqlContext.dropTempTable("src")
    }
    Seq((1, "")).toDF("key", "value").registerTempTable("src")
    Seq((1, 1, 1)).toDF("a", "a", "b").registerTempTable("dupAttributes")
  }

  override protected def afterEach(): Unit = {
    try {
      sqlContext.dropTempTable("src")
      sqlContext.dropTempTable("dupAttributes")
    } finally {
      super.afterEach()
    }
  }

  positionTest("ambiguous attribute reference 1",
    "SELECT a from dupAttributes", "a")

  positionTest("ambiguous attribute reference 2",
    "SELECT a, b from dupAttributes", "a")

  positionTest("ambiguous attribute reference 3",
    "SELECT b, a from dupAttributes", "a")

  positionTest("unresolved attribute 1",
    "SELECT x FROM src", "x")

  positionTest("unresolved attribute 2",
    "SELECT        x FROM src", "x")

  positionTest("unresolved attribute 3",
    "SELECT key, x FROM src", "x")

  positionTest("unresolved attribute 4",
    """SELECT key,
      |x FROM src
    """.stripMargin, "x")

  positionTest("unresolved attribute 5",
    """SELECT key,
      |  x FROM src
    """.stripMargin, "x")

  positionTest("unresolved attribute 6",
    """SELECT key,
      |
      |  1 + x FROM src
    """.stripMargin, "x")

  positionTest("unresolved attribute 7",
    """SELECT key,
      |
      |  1 + x + 1 FROM src
    """.stripMargin, "x")

  positionTest("multi-char unresolved attribute",
    """SELECT key,
      |
      |  1 + abcd + 1 FROM src
    """.stripMargin, "abcd")

  positionTest("unresolved attribute group by",
    """SELECT key FROM src GROUP BY
       |x
    """.stripMargin, "x")

  positionTest("unresolved attribute order by",
    """SELECT key FROM src ORDER BY
      |x
    """.stripMargin, "x")

  positionTest("unresolved attribute where",
    """SELECT key FROM src
      |WHERE x = true
    """.stripMargin, "x")

  positionTest("unresolved attribute backticks",
    "SELECT `x` FROM src", "`x`")

  positionTest("parse error",
    "SELECT WHERE", "WHERE")

  positionTest("bad relation",
    "SELECT * FROM badTable", "badTable")

  ignore("other expressions") {
    positionTest("bad addition",
      "SELECT 1 + array(1)", "1 + array")
  }

  /**
   * Creates a test that checks to see if the error thrown when analyzing a given query includes
   * the location of the given token in the query string.
   *
   * @param name the name of the test
   * @param query the query to analyze
   * @param token a unique token in the string that should be indicated by the exception
   */
  def positionTest(name: String, query: String, token: String): Unit = {
    def ast = HiveSqlParser.parsePlan(query)
    def parseTree = Try(quietly(ast.treeString)).getOrElse("<failed to parse>")

    test(name) {
      val error = intercept[AnalysisException] {
        quietly(hiveContext.sql(query))
      }

      assert(!error.getMessage.contains("Seq("))
      assert(!error.getMessage.contains("List("))

      val (line, expectedLineNum) = query.split("\n").zipWithIndex.collect {
        case (l, i) if l.contains(token) => (l, i + 1)
      }.headOption.getOrElse(sys.error(s"Invalid test. Token $token not in $query"))
      val actualLine = error.line.getOrElse {
        fail(
          s"line not returned for error '${error.getMessage}' on token $token\n$parseTree"
        )
      }
      assert(actualLine === expectedLineNum, "wrong line")

      val expectedStart = line.indexOf(token)
      val actualStart = error.startPosition.getOrElse {
        fail(s"start not returned for error on token $token\n${ast.treeString}")
      }
      assert(expectedStart === actualStart,
       s"""Incorrect start position.
          |== QUERY ==
          |$query
          |
          |== AST ==
          |$parseTree
          |
          |Actual: $actualStart, Expected: $expectedStart
          |$line
          |${" " * actualStart}^
          |0123456789 123456789 1234567890
          |          2         3
        """.stripMargin)
    }
  }
}