|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.catalyst.catalog.v2 |
| 18 | + |
| 19 | +import org.scalatest.Matchers._ |
| 20 | + |
| 21 | +import org.apache.spark.sql.catalog.v2.{CatalogNotFoundException, CatalogPlugin} |
| 22 | +import org.apache.spark.sql.catalyst.TableIdentifier |
| 23 | +import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, Analyzer} |
| 24 | +import org.apache.spark.sql.catalyst.parser.CatalystSqlParser |
| 25 | +import org.apache.spark.sql.internal.SQLConf |
| 26 | +import org.apache.spark.sql.util.CaseInsensitiveStringMap |
| 27 | + |
| 28 | +private class TestCatalogPlugin(override val name: String) extends CatalogPlugin { |
| 29 | + |
| 30 | + override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = Unit |
| 31 | +} |
| 32 | + |
| 33 | +class ResolveMultipartIdentifierSuite extends AnalysisTest { |
| 34 | + import CatalystSqlParser._ |
| 35 | + |
| 36 | + private val analyzer = makeAnalyzer(caseSensitive = false) |
| 37 | + |
| 38 | + private val catalogs = Seq("prod", "test").map(name => name -> new TestCatalogPlugin(name)).toMap |
| 39 | + |
| 40 | + private def lookupCatalog(catalog: String): CatalogPlugin = |
| 41 | + catalogs.getOrElse(catalog, throw new CatalogNotFoundException("Not found")) |
| 42 | + |
| 43 | + private def makeAnalyzer(caseSensitive: Boolean) = { |
| 44 | + val conf = new SQLConf().copy(SQLConf.CASE_SENSITIVE -> caseSensitive) |
| 45 | + new Analyzer(Some(lookupCatalog _), null, conf) |
| 46 | + } |
| 47 | + |
| 48 | + override protected def getAnalyzer(caseSensitive: Boolean) = analyzer |
| 49 | + |
| 50 | + private def checkResolution(sqlText: String, expectedCatalog: Option[CatalogPlugin], |
| 51 | + expectedNamespace: Array[String], expectedName: String): Unit = { |
| 52 | + |
| 53 | + import analyzer.CatalogObjectIdentifier |
| 54 | + val CatalogObjectIdentifier(catalog, ident) = parseMultipartIdentifier(sqlText) |
| 55 | + catalog shouldEqual expectedCatalog |
| 56 | + ident.namespace shouldEqual expectedNamespace |
| 57 | + ident.name shouldEqual expectedName |
| 58 | + } |
| 59 | + |
| 60 | + private def checkTableResolution(sqlText: String, |
| 61 | + expectedIdent: Option[TableIdentifier]): Unit = { |
| 62 | + |
| 63 | + import analyzer.AsTableIdentifier |
| 64 | + parseMultipartIdentifier(sqlText) match { |
| 65 | + case AsTableIdentifier(ident) => |
| 66 | + assert(Some(ident) === expectedIdent) |
| 67 | + case _ => |
| 68 | + assert(None === expectedIdent) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + test("resolve multipart identifier") { |
| 73 | + checkResolution("tbl", None, Array.empty, "tbl") |
| 74 | + checkResolution("db.tbl", None, Array("db"), "tbl") |
| 75 | + checkResolution("prod.func", catalogs.get("prod"), Array.empty, "func") |
| 76 | + checkResolution("ns1.ns2.tbl", None, Array("ns1", "ns2"), "tbl") |
| 77 | + checkResolution("prod.db.tbl", catalogs.get("prod"), Array("db"), "tbl") |
| 78 | + checkResolution("test.db.tbl", catalogs.get("test"), Array("db"), "tbl") |
| 79 | + checkResolution("test.ns1.ns2.ns3.tbl", |
| 80 | + catalogs.get("test"), Array("ns1", "ns2", "ns3"), "tbl") |
| 81 | + checkResolution("`db.tbl`", None, Array.empty, "db.tbl") |
| 82 | + checkResolution("parquet.`file:/tmp/db.tbl`", None, Array("parquet"), "file:/tmp/db.tbl") |
| 83 | + checkResolution("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`", None, |
| 84 | + Array("org.apache.spark.sql.json"), "s3://buck/tmp/abc.json") |
| 85 | + } |
| 86 | + |
| 87 | + test("resolve table identifier") { |
| 88 | + checkTableResolution("tbl", Some(TableIdentifier("tbl"))) |
| 89 | + checkTableResolution("db.tbl", Some(TableIdentifier("tbl", Some("db")))) |
| 90 | + checkTableResolution("prod.func", None) |
| 91 | + checkTableResolution("ns1.ns2.tbl", None) |
| 92 | + checkTableResolution("prod.db.tbl", None) |
| 93 | + checkTableResolution("`db.tbl`", Some(TableIdentifier("db.tbl"))) |
| 94 | + checkTableResolution("parquet.`file:/tmp/db.tbl`", |
| 95 | + Some(TableIdentifier("file:/tmp/db.tbl", Some("parquet")))) |
| 96 | + checkTableResolution("`org.apache.spark.sql.json`.`s3://buck/tmp/abc.json`", |
| 97 | + Some(TableIdentifier("s3://buck/tmp/abc.json", Some("org.apache.spark.sql.json")))) |
| 98 | + } |
| 99 | +} |
0 commit comments