File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed
main/scala/org/apache/spark/sql/execution/command
test/scala/org/apache/spark/sql/execution/command
hive/src/test/scala/org/apache/spark/sql/hive/execution Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -88,7 +88,11 @@ case class CreateViewCommand(
8888 qe.assertAnalyzed()
8989 val analyzedPlan = qe.analyzed
9090
91- require(tableDesc.schema == Nil || tableDesc.schema.length == analyzedPlan.output.length)
91+ if (tableDesc.schema != Nil && tableDesc.schema.length != analyzedPlan.output.length) {
92+ throw new AnalysisException (s " The number of columns produced by the SELECT clause " +
93+ s " (num: ` ${analyzedPlan.output.length}`) does not match the number of column names " +
94+ s " specified by CREATE VIEW (num: ` ${tableDesc.schema.length}`). " )
95+ }
9296 val sessionState = sparkSession.sessionState
9397
9498 if (isTemporary) {
Original file line number Diff line number Diff line change @@ -1314,6 +1314,29 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
13141314 }
13151315 }
13161316
1317+ test(" create temporary view with mismatched schema" ) {
1318+ withTable(" tab1" ) {
1319+ spark.range(10 ).write.saveAsTable(" tab1" )
1320+ withView(" view1" ) {
1321+ val e = intercept[AnalysisException ] {
1322+ sql(" CREATE TEMPORARY VIEW view1 (col1, col3) AS SELECT * FROM tab1" )
1323+ }.getMessage
1324+ assert(e.contains(" the SELECT clause (num: `1`) does not match" )
1325+ && e.contains(" CREATE VIEW (num: `2`)" ))
1326+ }
1327+ }
1328+ }
1329+
1330+ test(" create temporary view with specified schema" ) {
1331+ withView(" view1" ) {
1332+ sql(" CREATE TEMPORARY VIEW view1 (col1, col2) AS SELECT 1, 2" )
1333+ checkAnswer(
1334+ sql(" SELECT * FROM view1" ),
1335+ Row (1 , 2 ) :: Nil
1336+ )
1337+ }
1338+ }
1339+
13171340 test(" truncate table - external table, temporary table, view (not allowed)" ) {
13181341 import testImplicits ._
13191342 val path = Utils .createTempDir().getAbsolutePath
Original file line number Diff line number Diff line change @@ -391,6 +391,29 @@ class HiveDDLSuite
391391 }
392392 }
393393
394+ test(" create view with mismatched schema" ) {
395+ withTable(" tab1" ) {
396+ spark.range(10 ).write.saveAsTable(" tab1" )
397+ withView(" view1" ) {
398+ val e = intercept[AnalysisException ] {
399+ sql(" CREATE VIEW view1 (col1, col3) AS SELECT * FROM tab1" )
400+ }.getMessage
401+ assert(e.contains(" the SELECT clause (num: `1`) does not match" )
402+ && e.contains(" CREATE VIEW (num: `2`)" ))
403+ }
404+ }
405+ }
406+
407+ test(" create view with specified schema" ) {
408+ withView(" view1" ) {
409+ sql(" CREATE VIEW view1 (col1, col2) AS SELECT 1, 2" )
410+ checkAnswer(
411+ sql(" SELECT * FROM view1" ),
412+ Row (1 , 2 ) :: Nil
413+ )
414+ }
415+ }
416+
394417 test(" desc table for Hive table" ) {
395418 withTable(" tab1" ) {
396419 val tabName = " tab1"
You can’t perform that action at this time.
0 commit comments