-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: Merge code supporting "Insert Into" and "Create View xx As Select * From xx" into master branch. Testing: New unit tests were added. I manually tested this PR in HDP cluster: Case1: sql("create table table_parquet_200 (a int) using parquet") sql("insert into table table_parquet_200 values (200)") sql("create table table_parquet_300 (a int) using parquet") sql("INSERT INTO table_parquet_300 SELECT * FROM table_parquet_200") Case2: sql("CREATE TABLE t00000(a int)") sql("INSERT INTO t00000 VALUES (1)") sql("CREATE TABLE t10000(a int)") sql("INSERT INTO t10000 SELECT * FROM t00000") Case 3: sql("CREATE TABLE sourceTable(a int)") sql("INSERT INTO sourceTable VALUES (1)") sql("CREATE view s_view1 as select * from sourceTable") * Add supports to Insert Into * Add unit tests * reorder imports * Fix failure * Fix review comments * Add unit tests for CreateViewHarvester * Fix a small issue
- Loading branch information
Showing
4 changed files
with
394 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...s-connector/src/test/scala/com/hortonworks/spark/atlas/sql/CreateViewHarvesterSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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 com.hortonworks.spark.atlas.sql | ||
|
||
import java.io.File | ||
import java.util | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.util.Random | ||
|
||
import org.apache.atlas.AtlasClient | ||
import org.apache.atlas.model.instance.AtlasEntity | ||
import org.apache.commons.io.FileUtils | ||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.execution.command.{ExecutedCommandExec, CreateViewCommand} | ||
import org.scalatest.{BeforeAndAfterAll, Matchers, FunSuite} | ||
|
||
import com.hortonworks.spark.atlas.types.external | ||
|
||
class CreateViewHarvesterSuite extends FunSuite with Matchers with BeforeAndAfterAll { | ||
private var sparkSession: SparkSession = _ | ||
private val sourceTblName = "source_" + Random.nextInt(100000) | ||
private val destinationViewName = "destination_" + Random.nextInt(100000) | ||
|
||
override def beforeAll(): Unit = { | ||
super.beforeAll() | ||
sparkSession = SparkSession.builder() | ||
.master("local") | ||
.config("spark.sql.catalogImplementation", "hive") | ||
.getOrCreate() | ||
|
||
sparkSession.sql(s"CREATE TABLE $sourceTblName (name string)") | ||
sparkSession.sql(s"INSERT INTO TABLE $sourceTblName VALUES ('lucy'), ('tom')") | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
sparkSession.stop() | ||
SparkSession.clearActiveSession() | ||
SparkSession.clearDefaultSession() | ||
sparkSession = null | ||
|
||
FileUtils.deleteDirectory(new File("metastore_db")) | ||
FileUtils.deleteDirectory(new File("spark-warehouse")) | ||
|
||
super.afterAll() | ||
} | ||
|
||
test("CREATE VIEW FROM TABLE") { | ||
val qe = sparkSession.sql(s"CREATE VIEW $destinationViewName " + | ||
s"AS SELECT * FROM $sourceTblName").queryExecution | ||
val qd = QueryDetail(qe, 0L, 0L) | ||
|
||
assert(qe.sparkPlan.isInstanceOf[ExecutedCommandExec]) | ||
val node = qe.sparkPlan.asInstanceOf[ExecutedCommandExec] | ||
assert(node.cmd.isInstanceOf[CreateViewCommand]) | ||
val cmd = node.cmd.asInstanceOf[CreateViewCommand] | ||
|
||
val entities = CommandsHarvester.CreateViewHarvester.harvest(cmd, qd) | ||
val pEntity = entities.head | ||
|
||
assert(pEntity.getAttribute("inputs").isInstanceOf[util.Collection[_]]) | ||
val inputs = pEntity.getAttribute("inputs").asInstanceOf[util.Collection[AtlasEntity]] | ||
inputs.size() should be (1) | ||
|
||
val inputTbl = inputs.asScala.head | ||
inputTbl.getTypeName should be (external.HIVE_TABLE_TYPE_STRING) | ||
inputTbl.getAttribute("name") should be (sourceTblName) | ||
inputTbl.getAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME).toString should be ( | ||
s"default.$sourceTblName@primary") | ||
|
||
assert(pEntity.getAttribute("outputs").isInstanceOf[util.Collection[_]]) | ||
val outputs = pEntity.getAttribute("outputs").asInstanceOf[util.Collection[AtlasEntity]] | ||
outputs.size() should be (1) | ||
val outputView = outputs.asScala.head | ||
outputView.getAttribute("name") should be (destinationViewName) | ||
outputView.getAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME).toString should endWith ( | ||
s"default.$destinationViewName") | ||
} | ||
} |
Oops, something went wrong.