Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ALTER COLUMN SET DATA TYPE statement #11608

Merged
merged 2 commits into from
Dec 23, 2022

Conversation

ebyhr
Copy link
Member

@ebyhr ebyhr commented Mar 22, 2022

Description

Support ALTER COLUMN SET DATA TYPE statement. ANSI specification:

<alter table statement> ::=
  ALTER TABLE <table name>  <alter table action> 

<alter table action> ::=
...
  | <alter column definition> 

<alter column definition> ::=
ALTER [ COLUMN ] <column name> <alter column action>

<alter column action> ::=
...
  | <alter column data type clause> 

<alter column data type clause> ::=
  SET DATA TYPE <data type> 

Documentation

( ) No documentation is needed.
(x) Sufficient documentation is included in this PR.
( ) Documentation PR is available with #prnumber.
( ) Documentation issue #issuenumber is filed, and can be handled later.

Release notes

( ) No release notes entries required.
(x) Release notes entries required with the following suggested text:

# General
* Add support for `ALTER COLUMN ... SET DATA TYPE` statement. ({issue}`11608 `)

Fixes #10736

@ebyhr ebyhr added the enhancement New feature or request label Mar 22, 2022
@cla-bot cla-bot bot added the cla-signed label Mar 22, 2022
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch 3 times, most recently from 04a6a48 to 9344ed4 Compare March 22, 2022 09:29
*
* @throws AccessDeniedException if not allowed
*/
void checkCanAlterColumn(SecurityContext context, QualifiedObjectName tableName);
Copy link
Member Author

Choose a reason for hiding this comment

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

@martint @kokosing Do you think if we should check the permission in ALTER COLUMN level? Or it should be SET DATA TYPE (= checkCanSetColumnType) level?

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if checkCanAlterTable is not enough here. However this can be modeled by the implementation of the access control easily. So I think checkCanAlterColumn is good.

Copy link
Member

Choose a reason for hiding this comment

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

According to the SQL spec:

  1. The enabled authorization identifiers shall include the <authorization identifier> that owns the schema identified by the <schema name> of the table identified by <table name>.

@ebyhr ebyhr marked this pull request as ready for review March 28, 2022 01:48
@ebyhr ebyhr requested a review from kasiafi April 23, 2022 00:35
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch 3 times, most recently from 0d98dcc to 188323d Compare May 17, 2022 01:40
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch 3 times, most recently from 52ec290 to f92141a Compare July 7, 2022 05:29
@ebyhr ebyhr requested a review from martint July 8, 2022 03:18
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from f92141a to bcd411b Compare July 12, 2022 03:03
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from bcd411b to e4ce14c Compare July 26, 2022 05:07
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from e4ce14c to 6699757 Compare August 10, 2022 02:07
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from 6699757 to d32f968 Compare August 18, 2022 11:02
@ebyhr ebyhr requested a review from electrum August 18, 2022 11:02
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from d32f968 to 2c321a2 Compare September 13, 2022 07:17
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch 2 times, most recently from 6d7ca24 to b01be62 Compare October 6, 2022 07:40
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from b01be62 to 14a445d Compare October 14, 2022 07:27
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch 2 times, most recently from 6f137a4 to d23f22c Compare November 1, 2022 05:43
@bitsondatadev
Copy link
Member

👋 @ebyhr - this PR has become inactive. If you're still interested in working on it, please let us know.

We're working on closing out old and inactive PRs, so if you're too busy or this has too many merge conflicts to be worth picking back up, we'll be making another pass to close it out in a few weeks.

@findepi
Copy link
Member

findepi commented Dec 8, 2022

@ebyhr can you please chop it a little bit?

let's have a grammar commit (antlr grammar, astbuilder, sqlformatter)
let's have a statement commit (let's throw all the rest there for now)

let's also move mongo stuff to separate PR; i tend not to review Mongo-related stuff

@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from a305366 to 8df8da2 Compare December 9, 2022 03:13
@ebyhr
Copy link
Member Author

ebyhr commented Dec 9, 2022

Separate into two commits and removed a MongoDB commit.

@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from 8df8da2 to 34d7298 Compare December 12, 2022 02:51
@ebyhr
Copy link
Member Author

ebyhr commented Dec 12, 2022

CI hit #15367

accessControl.checkCanAlterColumn(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(qualifiedObjectName));

TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
Copy link
Member

Choose a reason for hiding this comment

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

Should you make sure the user can see the column here using accessControl.filterColumns?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure, other column tasks (e.g. DROP COLUMN) don't have such logic.

@kokosing Do you have any opinion?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that the accessControl.filterColumns check is relevant currently only within information_schema.columns metadata table.

accessControl.checkCanAlterColumn(session.toSecurityContext(), redirectionAwareTableHandle.getRedirectedTableName().orElse(qualifiedObjectName));

TableHandle tableHandle = redirectionAwareTableHandle.getTableHandle().get();
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that the accessControl.filterColumns check is relevant currently only within information_schema.columns metadata table.

List<ColumnMetadata> columns = new ArrayList<>(tableMetadata.getColumns());
ColumnMetadata columnMetadata = getColumnMetadata(session, tableHandle, column);
columns.set(columns.indexOf(columnMetadata), ColumnMetadata.builderFrom(columnMetadata).setType(type).build());
tables.put(tableName, new ConnectorTableMetadata(tableName, ImmutableList.copyOf(columns), tableMetadata.getProperties(), tableMetadata.getComment()));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Is the copy of the columns actually necessary?

}

@Override
public boolean shallowEquals(Node other)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this override needed?

Copy link
Member

Choose a reason for hiding this comment

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

hopefully not needed, please remove

(the current impl isn't correct yet, it should check all local fields)

Copy link
Member Author

@ebyhr ebyhr Dec 19, 2022

Choose a reason for hiding this comment

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

The javadoc of shallowEquals method says below. I don't think we should check all fields. I will remove either though.

Compare with another node by considering internal state excluding any Node returned by getChildren()

Copy link
Member

Choose a reason for hiding this comment

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

It seems you might be right. Still -- let's try to remove it, and avoid need for discussing it.
For example, AddColumn doesn't implement this

Copy link
Member

Choose a reason for hiding this comment

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

It seems you might be right. Still -- let's try to remove it, and avoid need for discussing it.

@ebyhr thoughts?

@ebyhr ebyhr requested a review from findepi December 16, 2022 04:47
}

private SetColumnType(Optional<NodeLocation> location, QualifiedName tableName, Identifier columnName, DataType type, boolean tableExists)
public SetColumnType(Optional<NodeLocation> location, QualifiedName tableName, Identifier columnName, DataType type, boolean tableExists)
Copy link
Member

Choose a reason for hiding this comment

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

location is always known (except for io.trino.execution.TestSetColumnTypeTask#executeSetColumnType where you can fill it with dummy value), so no need for optional?

@Test
public void testAlterColumnSetDataType()
{
assertStatement("ALTER TABLE foo.t ALTER COLUMN a SET DATA TYPE bigint",
Copy link
Member

Choose a reason for hiding this comment

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

assertStatement is deprecated (although my IDE doesn't highlight that, perhaps because this is same class)

use assertThat(statement(....)).

}

@Override
public boolean shallowEquals(Node other)
Copy link
Member

Choose a reason for hiding this comment

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

hopefully not needed, please remove

(the current impl isn't correct yet, it should check all local fields)

@ebyhr
Copy link
Member Author

ebyhr commented Dec 19, 2022

Addressed comments.

@findepi
Copy link
Member

findepi commented Dec 21, 2022

(squashed and rebased)

public SetColumnType(NodeLocation location, QualifiedName tableName, Identifier columnName, DataType type, boolean tableExists)
{
this(Optional.of(location), tableName, columnName, type, tableExists);
Copy link
Member

Choose a reason for hiding this comment

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

changes in this class should all go in the first commit

Copy link
Member

@findepi findepi left a comment

Choose a reason for hiding this comment

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

% commit boundaries alignment and shallowEquals removal

@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from e0a1f6e to 1db772f Compare December 21, 2022 13:23
@ebyhr ebyhr force-pushed the ebi/alter-table-set-data-type branch from 1db772f to b85a786 Compare December 21, 2022 23:49
@ebyhr ebyhr merged commit ef94c0d into trinodb:master Dec 23, 2022
@ebyhr ebyhr deleted the ebi/alter-table-set-data-type branch December 23, 2022 00:55
@ebyhr ebyhr mentioned this pull request Dec 23, 2022
@github-actions github-actions bot added this to the 404 milestone Dec 23, 2022
utk-spartan added a commit to utk-spartan/ranger that referenced this pull request Mar 8, 2023
Incorporate following changes

Pass properties when checking access for CREATE SCHEMA
trinodb/trino#15153
trinodb/trino@e16620f

Support ALTER COLUMN SET DATA TYPE statement
trinodb/trino#11608
trinodb/trino@ef94c0d
utk-spartan added a commit to razorpay/ranger that referenced this pull request Mar 8, 2023
Incorporate following changes

Pass properties when checking access for CREATE SCHEMA
trinodb/trino#15153
trinodb/trino@e16620f

Support ALTER COLUMN SET DATA TYPE statement
trinodb/trino#11608
trinodb/trino@ef94c0d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed docs enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Add ALTER TABLE syntax to handle column type change
7 participants