Skip to content

Commit 7c5b6af

Browse files
committed
code refactor.
1 parent e2ccdd5 commit 7c5b6af

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ case class CreateViewCommand(
165165
}
166166
} else {
167167
// Create the view if it doesn't exist.
168-
catalog.createTable(prepareTable(sparkSession, analyzedPlan),
169-
ignoreIfExists = false)
168+
catalog.createTable(prepareTable(sparkSession, analyzedPlan), ignoreIfExists = false)
170169
}
171170
Seq.empty[Row]
172171
}
@@ -228,14 +227,13 @@ case class CreateViewCommand(
228227
"It is not allowed to create a persisted view from the Dataset API")
229228
}
230229

231-
val aliasedPlan = aliasPlan(session, analyzedPlan)
232230
val newProperties = generateViewProperties(properties, session, analyzedPlan)
233231

234232
CatalogTable(
235233
identifier = name,
236234
tableType = CatalogTableType.VIEW,
237235
storage = CatalogStorageFormat.empty,
238-
schema = aliasedPlan.schema,
236+
schema = aliasPlan(session, analyzedPlan).schema,
239237
properties = newProperties,
240238
viewOriginalText = originalText,
241239
viewText = originalText,

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -370,35 +370,30 @@ class HiveDDLSuite
370370
spark.range(10).write.saveAsTable(tabName)
371371
val viewName = "view1"
372372
withView(viewName) {
373-
def checkProperties(
374-
properties: Map[String, String],
375-
expected: Map[String, String]): Boolean = {
373+
def checkProperties(expected: Map[String, String]): Boolean = {
374+
val properties = spark.sessionState.catalog.getTableMetadata(TableIdentifier(viewName))
375+
.properties
376376
properties.filterNot { case (key, value) =>
377377
Seq("transient_lastDdlTime", CatalogTable.VIEW_DEFAULT_DATABASE).contains(key) ||
378378
key.startsWith(CatalogTable.VIEW_QUERY_OUTPUT_PREFIX)
379379
} == expected
380380
}
381-
382-
val catalog = spark.sessionState.catalog
383381
sql(s"CREATE VIEW $viewName AS SELECT * FROM $tabName")
384382

385-
checkProperties(catalog.getTableMetadata(TableIdentifier(viewName)).properties, Map())
383+
checkProperties(Map())
386384
sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')")
387-
checkProperties(catalog.getTableMetadata(TableIdentifier(viewName)).properties,
388-
Map("p" -> "an"))
385+
checkProperties(Map("p" -> "an"))
389386

390387
// no exception or message will be issued if we set it again
391388
sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'an')")
392-
checkProperties(catalog.getTableMetadata(TableIdentifier(viewName)).properties,
393-
Map("p" -> "an"))
389+
checkProperties(Map("p" -> "an"))
394390

395391
// the value will be updated if we set the same key to a different value
396392
sql(s"ALTER VIEW $viewName SET TBLPROPERTIES ('p' = 'b')")
397-
checkProperties(catalog.getTableMetadata(TableIdentifier(viewName)).properties,
398-
Map("p" -> "b"))
393+
checkProperties(Map("p" -> "b"))
399394

400395
sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')")
401-
checkProperties(catalog.getTableMetadata(TableIdentifier(viewName)).properties, Map())
396+
checkProperties(Map())
402397

403398
val message = intercept[AnalysisException] {
404399
sql(s"ALTER VIEW $viewName UNSET TBLPROPERTIES ('p')")

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLViewSuite.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ class SQLViewSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
225225
withView("testView") {
226226
sql(
227227
"""CREATE VIEW IF NOT EXISTS
228-
|default.testView (c1 COMMENT 'blabla', c2 COMMENT 'blabla')
229-
|TBLPROPERTIES ('a' = 'b')
230-
|AS SELECT * FROM jt""".stripMargin)
228+
|default.testView (c1 COMMENT 'blabla', c2 COMMENT 'blabla')
229+
|TBLPROPERTIES ('a' = 'b')
230+
|AS SELECT * FROM jt
231+
|""".stripMargin)
231232
checkAnswer(sql("SELECT c1, c2 FROM testView ORDER BY c1"), (1 to 9).map(i => Row(i, i)))
232233
}
233234
}

0 commit comments

Comments
 (0)