Skip to content

Conversation

@imback82
Copy link
Contributor

@imback82 imback82 commented Nov 1, 2019

What changes were proposed in this pull request?

Disallow creating a permanent view that references a temporary view in expressions.

Why are the changes needed?

Creating a permanent view that references a temporary view is currently disallowed. For example,

# The following throws org.apache.spark.sql.AnalysisException
# Not allowed to create a permanent view `per_view` by referencing a temporary view `tmp`;
CREATE VIEW per_view AS SELECT t1.a, t2.b FROM base_table t1, (SELECT * FROM tmp) t2"

However, the following is allowed.

CREATE VIEW per_view AS SELECT * FROM base_table WHERE EXISTS (SELECT * FROM tmp);

This PR fixes the bug where temporary views used inside expressions are not checked.

Does this PR introduce any user-facing change?

Yes. Now the following SQL query throws an exception as expected:

# The following throws org.apache.spark.sql.AnalysisException
# Not allowed to create a permanent view `per_view` by referencing a temporary view `tmp`;
CREATE VIEW per_view AS SELECT * FROM base_table WHERE EXISTS (SELECT * FROM tmp);

How was this patch tested?

Added new unit tests.

@imback82
Copy link
Contributor Author

imback82 commented Nov 1, 2019

cc: @cloud-fan

@SparkQA
Copy link

SparkQA commented Nov 1, 2019

Test build #113102 has finished for PR 26361 at commit 44d3d08.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@imback82
Copy link
Contributor Author

imback82 commented Nov 1, 2019

Looking at #26317, a "temporary" view will be automatically created. I can wait for that to be merged first. (This PR makes searching for temporary views more exhaustive).

cc: @amanomer

@SparkQA
Copy link

SparkQA commented Nov 1, 2019

Test build #113110 has finished for PR 26361 at commit 80ab9ad.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@maropu maropu changed the title [Spark-29630][SQL] Disallow creating a permanent view that references a temporary view in an expression [SPARK-29630][SQL] Disallow creating a permanent view that references a temporary view in an expression Nov 1, 2019
-- [SPARK-29628] Forcibly create a temporary view in CREATE VIEW if referencing a temporary view
CREATE VIEW v6_temp AS SELECT * FROM base_table WHERE id IN (SELECT id FROM temp_table);
CREATE VIEW v7_temp AS SELECT t1.id, t2.a FROM base_table t1, (SELECT * FROM temp_table) t2;
-- [SPARK-29630] Not allowed to create a permanent view by referencing a temporary view in EXISTS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't SPARK-29630 the JIRA ticket for this PR? Why it was there before this?

@SparkQA
Copy link

SparkQA commented Nov 2, 2019

Test build #113121 has finished for PR 26361 at commit c9ddf81.

  • This patch fails due to an unknown error code, -9.
  • This patch merges cleanly.
  • This patch adds no public classes.

@imback82
Copy link
Contributor Author

imback82 commented Nov 2, 2019

retest this please

@SparkQA
Copy link

SparkQA commented Nov 2, 2019

Test build #113131 has finished for PR 26361 at commit c9ddf81.

  • This patch fails SparkR unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@imback82
Copy link
Contributor Author

imback82 commented Nov 2, 2019

retest this please

@SparkQA
Copy link

SparkQA commented Nov 2, 2019

Test build #113136 has finished for PR 26361 at commit c9ddf81.

  • This patch fails SparkR unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@maropu
Copy link
Member

maropu commented Nov 2, 2019

retest this please

@SparkQA
Copy link

SparkQA commented Nov 3, 2019

Test build #113143 has finished for PR 26361 at commit c9ddf81.

  • This patch fails SparkR unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

CREATE TABLE tbl2 (c int, d int) using parquet;
CREATE TABLE tbl3 (e int, f int) using parquet;
CREATE TABLE tbl4 (g int, h int) using parquet;
-- Since Spark doesn't support CREATE TEMPORARY TABLE, we used CREATE TEMPORARY VIEW instead
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we update this comment? Now we use table as temp table in this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

"CREATE VIEW v AS SELECT * FROM base_table WHERE EXISTS (SELECT * FROM temp_view)",
"CREATE VIEW v AS SELECT id, (SELECT count(*) FROM temp_view) FROM base_table",
"CREATE VIEW v AS SELECT * FROM base_table " +
"WHERE id IN (SELECT id FROM base_table WHERE EXISTS (SELECT 1 FROM temp_view))"
Copy link
Contributor

@cloud-fan cloud-fan Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move them to create_view.sql? That's a better place for end-to-end tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these to create_view.sql. The first two scenarios were already there, so I just moved the last one. thanks for the feedback.

@SparkQA
Copy link

SparkQA commented Nov 4, 2019

Test build #113190 has finished for PR 26361 at commit 8ca1f4a.

  • This patch fails due to an unknown error code, -9.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Nov 4, 2019

Test build #113213 has finished for PR 26361 at commit 24f15f4.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 66619b8 Nov 5, 2019
-- !query 71 output

org.apache.spark.sql.AnalysisException
Not allowed to create a permanent view `temporal5` by referencing a temporary view `tt`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also provide the workaround in the error message. Instead just telling the users temp views are not allowed, we can say users can use permanent views in these cases?

cc @imback82 @cloud-fan

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add something like Create a temporary view to reference temporary views; a permanent view can only reference permanent views, but sounds a bit redundant with the error message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Permanent views can also reference to tables.

There exists a general issue in our existing error messages. We do not have a message reference book. When users hit the unclear error messages, what they can do is to read the source code or try different options until it works. This is not natural to end users who have zero background about Spark SQL. We need to correct them one by one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will do a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is the existing error message. This PR just extends the temp view check to subqueries. Agree that we can still improve the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is already addressed by #26731.

@imback82 imback82 deleted the spark-29630 branch January 6, 2020 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants