GitHub user jiangxb1987 opened a pull request:

    https://github.com/apache/spark/pull/17125

    [SPARK-19211][SQL] Explicitly prevent Insert into View or Create View As 
Insert

    ## What changes were proposed in this pull request?
    
    Currently we don't explicitly forbid the following behaviors:
    1. The statement CREATE VIEW AS INSERT INTO throws the following exception:
    ```
    scala> spark.sql("CREATE VIEW testView AS INSERT INTO tab VALUES (1, 
\"a\")")
    org.apache.spark.sql.AnalysisException: 
org.apache.hadoop.hive.ql.metadata.HiveException: 
org.apache.hadoop.hive.ql.metadata.HiveException: at least one column must be 
specified for the table;
     scala> spark.sql("CREATE VIEW testView(a, b) AS INSERT INTO tab VALUES (1, 
\"a\")")
    org.apache.spark.sql.AnalysisException: The number of columns produced by 
the SELECT clause (num: `0`) does not match the number of column names 
specified by CREATE VIEW (num: `2`).;
    ```
    
    2. The statement INSERT INTO view VALUES throws the following exception 
from checkAnalysis:
    ```
    scala> spark.sql("INSERT INTO testView VALUES (1, \"a\")")
    org.apache.spark.sql.AnalysisException: Inserting into an RDD-based table 
is not allowed.;;
    'InsertIntoTable View (`default`.`testView`, [a#16,b#17]), false, false
    +- LocalRelation [col1#14, col2#15]
    ```
    
    After this PR, the behavior changes to:
    ```
    scala> spark.sql("CREATE VIEW testView AS INSERT INTO tab VALUES (1, 
\"a\")")
    org.apache.spark.sql.AnalysisException: Creating a view as insert into a 
table is not allowed;
    
    scala> spark.sql("CREATE VIEW testView(a, b) AS INSERT INTO tab VALUES (1, 
\"a\")")
    org.apache.spark.sql.AnalysisException: Creating a view as insert into a 
table is not allowed;
    
    scala> spark.sql("INSERT INTO testView VALUES (1, \"a\")")
    org.apache.spark.sql.AnalysisException: `default`.`testView` is a view, 
inserting into a view is not allowed;
    ```
    
    ## How was this patch tested?
    
    Add a new test case in `SQLViewSuite`;
    Update the corresponding test case in `SQLViewSuite`.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/jiangxb1987/spark insert-with-view

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/17125.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #17125
    
----
commit d1484f9a207f258fc4e9d6f7527cc3b6aa4b9500
Author: jiangxingbo <[email protected]>
Date:   2017-03-01T15:51:11Z

    explictly prevent insert into view or create view as insert into table.

commit ae28e4bb66ce76f463975d1041b260e53c792b7b
Author: jiangxingbo <[email protected]>
Date:   2017-03-01T16:55:25Z

    update error message.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to