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 setting column types in PostgreSQL connector #15591

Merged
merged 1 commit into from
Jan 10, 2023

Conversation

ebyhr
Copy link
Member

@ebyhr ebyhr commented Jan 4, 2023

Description

Relates to #15515

Additional context and related issues

Release notes

(x) Release notes are required, with the following suggested text:

# PostgreSQL
* Add support for changing column types. ({issue}`15515`)

@cla-bot cla-bot bot added the cla-signed label Jan 4, 2023
@ebyhr ebyhr force-pushed the ebi/jdbc-change-column-type branch from abf6c4e to 433df5f Compare January 4, 2023 09:44
@ebyhr ebyhr marked this pull request as ready for review January 5, 2023 01:11
@ebyhr ebyhr force-pushed the ebi/jdbc-change-column-type branch 2 times, most recently from 976659d to 7458bc8 Compare January 5, 2023 03:48
@ebyhr ebyhr requested review from findepi and hashhar January 5, 2023 03:48
JdbcTableHandle tableHandle = (JdbcTableHandle) table;
JdbcColumnHandle columnHandle = (JdbcColumnHandle) column;
verify(!tableHandle.isSynthetic(), "Not a table reference: %s", tableHandle);
jdbcClient.setColumnType(session, tableHandle, columnHandle, type);
Copy link
Member

Choose a reason for hiding this comment

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

jdbcClient can be given the table name (JdbcNamedRelationHandle), not the full handle

Copy link
Member Author

Choose a reason for hiding this comment

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

The context of existing full handle seems that third-party developers may use authorization field. Let me keep as-is in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

cc: @Praveen2112 It seems if authorization field is non-empty we should fail all operations?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.

protected SetColumnTypeSetup applyColumnMapping(SetColumnTypeSetup setup)
{
if (setup.oldColumnType().equals("timestamp(3) with time zone")) {
return new SetColumnTypeSetup(setup.oldColumnType(), setup.newColumnType(), setup.oldValueLiteral(), "timestamp '2020-02-12 14:03:00.123000 +00:00'");
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment

.build();
}

public record SetColumnTypeSetup(String oldColumnType, String newColumnType, String oldValueLiteral, String newValueLiteral) {}
Copy link
Member

Choose a reason for hiding this comment

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

add null checks

public SetColumnTypeSetup
{
    requireNonNull(oldColumnType, "oldColumnType is null");
    requireNonNull(newColumnType, "newColumnType is null");
    requireNonNull(oldValueLiteral, "oldValueLiteral is null");
    requireNonNull(newValueLiteral, "newValueLiteral is null");
}

public void testSetColumnType(SetColumnTypeSetup setup)
{
if (!hasBehavior(SUPPORTS_SET_COLUMN_TYPE)) {
assertQueryFails("ALTER TABLE nation ALTER COLUMN nationkey SET DATA TYPE bigint", "This connector does not support setting column types");
Copy link
Member

Choose a reason for hiding this comment

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

the check ignores @DataProvider (and that's fine), so it should be invoked once
not @DataProvider's element count times

.matches("VALUES " + setup.newValueLiteral);
}
finally {
assertUpdate("DROP TABLE IF EXISTS " + tableName);
Copy link
Member

Choose a reason for hiding this comment

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

Use TestTable please

.add(new SetColumnTypeSetup("smallint", "integer", "32767", "32767"))
.add(new SetColumnTypeSetup("integer", "bigint", "2147483647", "bigint '2147483647'"))
.add(new SetColumnTypeSetup("bigint", "integer", "-2147483648", "-2147483648"))
.add(new SetColumnTypeSetup("decimal(5,3)", "decimal(38,3)", "12.345", "12.345"))
Copy link
Member

Choose a reason for hiding this comment

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

for decimals, let's exercise
short -> short
long -> long
short -> long

also, we should exercise precision reduction

  • when actual values can be converted without loss
  • when actual values need to be rounded (i think they should round, instead of faliing)

.add(new SetColumnTypeSetup("decimal(5,3)", "decimal(38,3)", "12.345", "12.345"))
.add(new SetColumnTypeSetup("date", "date", "date '2023-01-04'", "date '2023-01-04'"))
.add(new SetColumnTypeSetup("time(3)", "time(6)", "time '15:03:00.123'", "time '15:03:00.123000'"))
.add(new SetColumnTypeSetup("timestamp(3)", "timestamp(6)", "timestamp '2020-02-12 15:03:00.123'", "timestamp '2020-02-12 15:03:00.123000'"))
Copy link
Member

Choose a reason for hiding this comment

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

Add tests with time/timestamp precision reduction

  • when actual values can be converted without loss
  • when actual values need to be rounded (i think they should round, instead of faliing)

.add(new SetColumnTypeSetup("timestamp(3)", "timestamp(6)", "timestamp '2020-02-12 15:03:00.123'", "timestamp '2020-02-12 15:03:00.123000'"))
.add(new SetColumnTypeSetup("timestamp(3) with time zone", "timestamp(6) with time zone", "timestamp '2020-02-12 15:03:00.123 +01:00'", "timestamp '2020-02-12 15:03:00.123000 +01:00'"))
.add(new SetColumnTypeSetup("char(100)", "varchar", "'char-to-varchar'", "'char-to-varchar'"))
.add(new SetColumnTypeSetup("varchar", "char(100)", "'varchar-to-char'", "cast('varchar-to-char' as char(100))"))
Copy link
Member

Choose a reason for hiding this comment

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

add char & varchar tests with length reduction

.collect(toDataProvider());
}

protected SetColumnTypeSetup applyColumnMapping(SetColumnTypeSetup setup)
Copy link
Member

Choose a reason for hiding this comment

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

Can we give this method a better name?

also

  • how a connector can say this particular conversion isn't support in this connector case? (ie turn positive test case into a negative one)
    • see io.trino.testing.BaseConnectorTest.DataMappingTestSetup#asUnsupported
  • how a connector can say this particular conversion is problematic, needs to be skipped (TODO + issue)?
    • see io.trino.testing.BaseConnectorTest#filterDataMappingSmokeTestData, it returns Optional for this reason

Copy link
Member Author

Choose a reason for hiding this comment

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

how a connector can say this particular conversion isn't support in this connector case? (ie turn positive test case into a negative one)

Let me handle this in follow-up as the logic is unreachable in this PR.

how a connector can say this particular conversion is problematic, needs to be skipped (TODO + issue)?

Changed to optional type.

}

@DataProvider
public Object[][] setColumnTypesProvider()
Copy link
Member

Choose a reason for hiding this comment

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

Provider -> DataProvider (at least that's my convention)

@ebyhr ebyhr force-pushed the ebi/jdbc-change-column-type branch 2 times, most recently from 2a73a62 to b60541d Compare January 6, 2023 02:17
@ebyhr ebyhr self-assigned this Jan 6, 2023
Copy link
Member

@hashhar hashhar left a comment

Choose a reason for hiding this comment

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

I don't have useful comments - just about enum ordering. Sorry.

Looks good otherwise.

Comment on lines 73 to 75
case SUPPORTS_SET_COLUMN_TYPE:
return false;

Copy link
Member

Choose a reason for hiding this comment

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

above SUPPORTS_DELETE?

@@ -87,6 +87,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_COMMENT_ON_COLUMN:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT?

@@ -96,6 +96,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_COMMENT_ON_COLUMN:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT

@@ -70,6 +70,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_CREATE_TABLE_WITH_COLUMN_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT?

@@ -86,6 +86,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT

@@ -105,6 +105,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT?

@@ -76,6 +76,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT

@@ -78,6 +78,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT

@@ -66,6 +66,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
Copy link
Member

Choose a reason for hiding this comment

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

together with ADD_COLUMN_WITH_COMMENT

@ebyhr ebyhr force-pushed the ebi/jdbc-change-column-type branch 2 times, most recently from d657f05 to e217f11 Compare January 6, 2023 07:28
@ebyhr ebyhr force-pushed the ebi/jdbc-change-column-type branch from e217f11 to 5575667 Compare January 6, 2023 12:32
@ebyhr ebyhr merged commit bbfa269 into trinodb:master Jan 10, 2023
@ebyhr ebyhr deleted the ebi/jdbc-change-column-type branch January 10, 2023 01:04
@github-actions github-actions bot added this to the 406 milestone Jan 10, 2023
@ebyhr ebyhr mentioned this pull request Jan 10, 2023
@colebow
Copy link
Member

colebow commented Jan 10, 2023

@ebyhr should we add any docs for this?

@ebyhr
Copy link
Member Author

ebyhr commented Jan 10, 2023

@colebow We need to refactor the docs first as the current granularity is ALTER TABLE.

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.

5 participants