Skip to content

fix create view on exist table throw view exist exception#10186

Merged
sopel39 merged 1 commit intotrinodb:masterfrom
kyo-tom:create_view_message
Dec 22, 2021
Merged

fix create view on exist table throw view exist exception#10186
sopel39 merged 1 commit intotrinodb:masterfrom
kyo-tom:create_view_message

Conversation

@kyo-tom
Copy link
Copy Markdown
Contributor

@kyo-tom kyo-tom commented Dec 6, 2021

fix hive connector create view on exist table throw view exist exception. #10037
before:

trino:default> CREATE VIEW one AS SELECT * FROM one;
Query 20211206_045554_00007_33tp9 failed: View already exists: 'default.one'

after:

trino:default> CREATE VIEW one AS SELECT * FROM one;
Query 20211206_045554_00007_33tp9 failed: Table already exists: 'default.one'

Fixes #10186

@cla-bot cla-bot bot added the cla-signed label Dec 6, 2021
@findepi findepi requested a review from sopel39 December 6, 2021 13:06
@sopel39 sopel39 requested a review from lukasz-stec December 7, 2021 12:41
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO we should move this logic to the CreateViewTask and handle it in a similar way e.g. RenameViewTask handles it (including a check for materialized view). It would work then for all connectors (looks like iceberg connector may have the same issue) + would support MVs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I also think it should work for all connectors, so the one that really needs to be enhanced is CreateViewTask. So I will resubmit a pr to make an enhancement to CreateViewTask, can I close this pr now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's ok to make changes in this PR (you can squash commits if you need to)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok i will do it in the next few days

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

cc @lukasz-stec , I am very confused about the function of ConnectorMetadata#getTableHandle in hive.it should return null when the relation is views (just like the comment says in this function). But if I create a view, it will not return null in hive because of tableActions will add a table when view create

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, hive connector implementation of ConnectorMetadata#getTableHandle breaks the contract. I don't think it's that easy to fix though (some features may depend on this behavior)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IMO we can first judge whether the view exists according to Metadata#isView, if the view does not exist, then judge whether the table exists, we also need to verify whether this is feasible on other connectors. Do you think we need open other issus about hive connector implementation of ConnectorMetadata#getTableHandle breaks the contract.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO we can first judge whether the view exists according to Metadata#isView

yes + metadata.isMaterializedView to check if its MV

Do you think we need open other issus about hive connector implementation of ConnectorMetadata#getTableHandle breaks the contract.

Good question. There is this ticket #8349 which is a kind of prerequisite for fixing the hive connector but maybe we need another one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hello, @lukasz-stec .Could you review my code again? Thank you!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the tests above are mostly checking this (mock) logic. If the checks are moved to CreateTableTask (see comment below) this will be unnecessary and you can extend the test class from BaseDataDefinitionTaskTest.

@kyo-tom kyo-tom force-pushed the create_view_message branch from 047fc0a to 8f8fb37 Compare December 13, 2021 11:36
@kyo-tom kyo-tom requested a review from lukasz-stec December 13, 2021 11:45
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about using assertThat(metadata.isView(testSession, viewName)).isTrue(); instead of checcking getCreateViewCallCount?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good idea

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would revert this and use metadata.isView instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes,it can be revert if we use metadata.isView to check create view

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this make break for our case of hive connector returning tableHandle for views when replace=true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry, @lukasz-stec . I don't understand what you mean, , can you elaborate more?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I should say may break...

So because getTableHandle will return not empty TableHandle for a view in hive connector, when statement.isReplace() == true (i.e. CREATE OR REPLACE VIEW was used) the method will trow
throw semanticException(TABLE_ALREADY_EXISTS, statement, "Table '%s' already exists", name); and it should replace the view definition successfuly.

Copy link
Copy Markdown
Contributor Author

@kyo-tom kyo-tom Dec 16, 2021

Choose a reason for hiding this comment

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

Thank you for your reminder, I still need to check if it is view when statement.isReplace() == true.Sorry ,can you review again.

@kyo-tom kyo-tom force-pushed the create_view_message branch 6 times, most recently from a546725 to 478045c Compare December 16, 2021 03:51
@kyo-tom kyo-tom requested a review from lukasz-stec December 16, 2021 05:17
Copy link
Copy Markdown
Member

@lukasz-stec lukasz-stec left a comment

Choose a reason for hiding this comment

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

lgtm % comments

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is this change needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because the view named test_view is add to MockConnectorMetadata by withGetViews in TestEventListenerBasic, so metadata.isViews will always return true and throw a exception

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Because the view named test_view is add to MockConnectorMetadata by withGetViews in TestEventListenerBasic, so metadata.isViews will always return true and throw a exception

Should use use different view name here then?

@kyo-tom kyo-tom force-pushed the create_view_message branch 3 times, most recently from c5b4d9c to 7b98bd0 Compare December 17, 2021 02:15
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change the order:

  • MV
  • view
  • table
    This is the metadata check order that we use in other places. Let's be consistent here

Copy link
Copy Markdown
Contributor Author

@kyo-tom kyo-tom Dec 19, 2021

Choose a reason for hiding this comment

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

@sopel39 I had changed the order,can you reviewer again,thank you

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

else is not needed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Check isView only after isMaterializedView (see other comment below)

@kyo-tom kyo-tom force-pushed the create_view_message branch from 7b98bd0 to 08d8b09 Compare December 18, 2021 07:09
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use TABLE_ALREADY_EXISTS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use TABLE_ALREADY_EXISTS

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Because the view named test_view is add to MockConnectorMetadata by withGetViews in TestEventListenerBasic, so metadata.isViews will always return true and throw a exception

Should use use different view name here then?

@kyo-tom kyo-tom force-pushed the create_view_message branch 3 times, most recently from 50ffb45 to 5e3c356 Compare December 21, 2021 09:15
@kyo-tom kyo-tom force-pushed the create_view_message branch from 5e3c356 to 5897e63 Compare December 22, 2021 01:56
@sopel39
Copy link
Copy Markdown
Member

sopel39 commented Dec 22, 2021

Fixes #10186

@sopel39 sopel39 merged commit 7053fb1 into trinodb:master Dec 22, 2021
@sopel39
Copy link
Copy Markdown
Member

sopel39 commented Dec 22, 2021

merged thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants