@@ -35,39 +35,47 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {
3535 private var globalTempDB : String = _
3636
3737 test(" basic semantic" ) {
38- sql(" CREATE GLOBAL TEMP VIEW src AS SELECT 1, 'a'" )
38+ try {
39+ sql(" CREATE GLOBAL TEMP VIEW src AS SELECT 1, 'a'" )
40+
41+ // If there is no database in table name, we should try local temp view first, if not found,
42+ // try table/view in current database, which is "default" in this case. So we expect
43+ // NoSuchTableException here.
44+ intercept[NoSuchTableException ](spark.table(" src" ))
3945
40- // If there is no database in table name, we should try local temp view first, if not found,
41- // try table/view in current database, which is "default" in this case. So we expect
42- // NoSuchTableException here.
43- intercept[NoSuchTableException ](spark.table(" src" ))
46+ // Use qualified name to refer to the global temp view explicitly.
47+ checkAnswer(spark.table(s " $globalTempDB.src " ), Row (1 , " a" ))
4448
45- // Use qualified name to refer to the global temp view explicitly .
46- checkAnswer(spark.table( s " $globalTempDB .src " ), Row ( 1 , " a " ))
49+ // Table name without database will never refer to a global temp view.
50+ intercept[ NoSuchTableException ](sql( " DROP VIEW src " ))
4751
48- // Table name without database will never refer to a global temp view.
49- intercept[NoSuchTableException ](sql(" DROP VIEW src" ))
52+ sql(s " DROP VIEW $globalTempDB.src " )
53+ // The global temp view should be dropped successfully.
54+ intercept[NoSuchTableException ](spark.table(s " $globalTempDB.src " ))
5055
51- sql( s " DROP VIEW $globalTempDB .src " )
52- // The global temp view should be dropped successfully.
53- intercept[ NoSuchTableException ] (spark.table(s " $globalTempDB.src " ))
56+ // We can also use Dataset API to create global temp view
57+ Seq ( 1 -> " a " ).toDF( " i " , " j " ).createGlobalTempView( " src " )
58+ checkAnswer (spark.table(s " $globalTempDB.src " ), Row ( 1 , " a " ))
5459
55- // We can also use Dataset API to create global temp view
56- Seq (1 -> " a" ).toDF(" i" , " j" ).createGlobalTempView(" src" )
57- checkAnswer(spark.table(s " $globalTempDB.src " ), Row (1 , " a" ))
60+ // Use qualified name to rename a global temp view.
61+ sql(s " ALTER VIEW $globalTempDB.src RENAME TO src2 " )
62+ intercept[NoSuchTableException ](spark.table(s " $globalTempDB.src " ))
63+ checkAnswer(spark.table(s " $globalTempDB.src2 " ), Row (1 , " a" ))
5864
59- // Use qualified name to rename a global temp view.
60- sql(s " ALTER VIEW $globalTempDB.src RENAME TO src2 " )
61- intercept[NoSuchTableException ](spark.table(s " $globalTempDB.src " ))
62- checkAnswer(spark.table(s " $globalTempDB.src2 " ), Row (1 , " a" ))
65+ // Use qualified name to alter a global temp view.
66+ sql(s " ALTER VIEW $globalTempDB.src2 AS SELECT 2, 'b' " )
67+ checkAnswer(spark.table(s " $globalTempDB.src2 " ), Row (2 , " b" ))
6368
64- // Use qualified name to alter a global temp view.
65- sql( s " ALTER VIEW $globalTempDB . src2 AS SELECT 2, 'b' " )
66- checkAnswer (spark.table(s " $globalTempDB.src2 " ), Row ( 2 , " b " ))
69+ // We can also use Catalog API to drop global temp view
70+ spark.catalog.dropGlobalTempView( " src2" )
71+ intercept[ NoSuchTableException ] (spark.table(s " $globalTempDB.src2 " ))
6772
68- // We can also use Catalog API to drop global temp view
69- spark.catalog.dropGlobalTempView(" src2" )
70- intercept[NoSuchTableException ](spark.table(s " $globalTempDB.src2 " ))
73+ // We can also use Dataset API to replace global temp view
74+ Seq (2 -> " b" ).toDF(" i" , " j" ).createOrReplaceGlobalTempView(" src" )
75+ checkAnswer(spark.table(s " $globalTempDB.src " ), Row (2 , " b" ))
76+ } finally {
77+ spark.catalog.dropGlobalTempView(" src" )
78+ }
7179 }
7280
7381 test(" global temp view is shared among all sessions" ) {
@@ -106,7 +114,7 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {
106114 test(" CREATE TABLE LIKE should work for global temp view" ) {
107115 try {
108116 sql(" CREATE GLOBAL TEMP VIEW src AS SELECT 1 AS a, '2' AS b" )
109- sql(s " CREATE TABLE cloned LIKE ${ globalTempDB} .src " )
117+ sql(s " CREATE TABLE cloned LIKE $globalTempDB.src " )
110118 val tableMeta = spark.sessionState.catalog.getTableMetadata(TableIdentifier (" cloned" ))
111119 assert(tableMeta.schema == new StructType ().add(" a" , " int" , false ).add(" b" , " string" , false ))
112120 } finally {
0 commit comments