Skip to content

Commit 3e04e2b

Browse files
NivinCShantangwangd
authored andcommitted
feat(plugin-iceberg): Add support for ALTER COLUMN SET DATA TYPE in the Iceberg connector (#25418)
Add support for ALTER COLUMN SET DATA TYPE in the Iceberg connector To address the issue : #25417 <!---Describe any public API or user-facing feature change or any performance impact--> <!---Please fill in how you tested your change--> - [ ] Please make sure your submission complies with our [contributing guide](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md), in particular [code style](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#code-style) and [commit standards](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#commit-standards). - [ ] PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced. - [ ] Documented new properties (with its default value), SQL syntax, functions, or other functionality. - [ ] If release notes are required, they follow the [release notes guidelines](https://github.com/prestodb/presto/wiki/Release-Notes-Guidelines). - [ ] Adequate tests were added if applicable. - [ ] CI passed. Please follow [release notes guidelines](https://github.com/prestodb/presto/wiki/Release-Notes-Guidelines) and fill in the release notes below. ``` == RELEASE NOTES == General Changes * Add support for ALTER COLUMN SET DATA TYPE in the Iceberg connector Co-authored-by: Dong Wang <mingwbd@gmail.com>
1 parent f12815c commit 3e04e2b

83 files changed

Lines changed: 3340 additions & 86 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

presto-analyzer/src/main/java/com/facebook/presto/sql/analyzer/utils/StatementUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.facebook.presto.sql.tree.Revoke;
6363
import com.facebook.presto.sql.tree.RevokeRoles;
6464
import com.facebook.presto.sql.tree.Rollback;
65+
import com.facebook.presto.sql.tree.SetColumnType;
6566
import com.facebook.presto.sql.tree.SetProperties;
6667
import com.facebook.presto.sql.tree.SetRole;
6768
import com.facebook.presto.sql.tree.SetSession;
@@ -172,6 +173,7 @@ private StatementUtils() {}
172173
builder.put(Revoke.class, QueryType.DATA_DEFINITION);
173174
builder.put(Prepare.class, QueryType.CONTROL);
174175
builder.put(Deallocate.class, QueryType.CONTROL);
176+
builder.put(SetColumnType.class, QueryType.DATA_DEFINITION);
175177

176178
STATEMENT_QUERY_TYPES = builder.build();
177179
}

presto-blackhole/src/main/java/com/facebook/presto/plugin/blackhole/BlackHoleMetadata.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.facebook.presto.plugin.blackhole;
1515

1616
import com.facebook.airlift.units.Duration;
17+
import com.facebook.presto.common.type.Type;
1718
import com.facebook.presto.spi.ColumnHandle;
1819
import com.facebook.presto.spi.ColumnMetadata;
1920
import com.facebook.presto.spi.ConnectorInsertTableHandle;
@@ -286,4 +287,23 @@ private void checkSchemaExists(String schemaName)
286287
throw new SchemaNotFoundException(schemaName);
287288
}
288289
}
290+
291+
@Override
292+
public void setColumnType(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle, Type type)
293+
{
294+
BlackHoleTableHandle table = (BlackHoleTableHandle) tableHandle;
295+
BlackHoleColumnHandle column = (BlackHoleColumnHandle) columnHandle;
296+
List<BlackHoleColumnHandle> columns = new ArrayList<>(table.getColumnHandles());
297+
columns.set(columns.indexOf(column), new BlackHoleColumnHandle(column.getName(), type));
298+
299+
tables.put(table.toSchemaTableName(), new BlackHoleTableHandle(
300+
table.getSchemaName(),
301+
table.getTableName(),
302+
ImmutableList.copyOf(columns),
303+
table.getSplitCount(),
304+
table.getPagesPerSplit(),
305+
table.getRowsPerPage(),
306+
table.getFieldsLength(),
307+
table.getPageProcessingDelay()));
308+
}
289309
}

presto-common/src/main/java/com/facebook/presto/common/type/DoubleType.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.facebook.presto.common.block.Block;
1717
import com.facebook.presto.common.block.BlockBuilder;
1818
import com.facebook.presto.common.block.BlockBuilderStatus;
19+
import com.facebook.presto.common.block.IntArrayBlock;
1920
import com.facebook.presto.common.block.LongArrayBlockBuilder;
2021
import com.facebook.presto.common.block.PageBuilderStatus;
2122
import com.facebook.presto.common.block.UncheckedBlock;
@@ -27,6 +28,7 @@
2728
import static com.facebook.presto.common.type.TypeUtils.doubleHashCode;
2829
import static java.lang.Double.doubleToLongBits;
2930
import static java.lang.Double.longBitsToDouble;
31+
import static java.lang.Float.intBitsToFloat;
3032

3133
public final class DoubleType
3234
extends AbstractPrimitiveType
@@ -67,6 +69,9 @@ public Object getObjectValue(SqlFunctionProperties properties, Block block, int
6769
if (block.isNull(position)) {
6870
return null;
6971
}
72+
if (IntArrayBlock.class.isInstance(block)) {
73+
return intBitsToFloat(block.getInt(position));
74+
}
7075
return longBitsToDouble(block.getLong(position));
7176
}
7277

@@ -109,13 +114,22 @@ public void appendTo(Block block, int position, BlockBuilder blockBuilder)
109114
blockBuilder.appendNull();
110115
}
111116
else {
112-
blockBuilder.writeLong(block.getLong(position)).closeEntry();
117+
if (block instanceof IntArrayBlock) {
118+
float flt = intBitsToFloat(block.getInt(position));
119+
blockBuilder.writeLong(doubleToLongBits(flt)).closeEntry();
120+
}
121+
else {
122+
blockBuilder.writeLong(block.getLong(position)).closeEntry();
123+
}
113124
}
114125
}
115126

116127
@Override
117128
public double getDouble(Block block, int position)
118129
{
130+
if (block instanceof IntArrayBlock) {
131+
return intBitsToFloat(block.getInt(position));
132+
}
119133
return longBitsToDouble(block.getLong(position));
120134
}
121135

presto-common/src/main/java/com/facebook/presto/common/type/LongDecimalType.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
import com.facebook.presto.common.block.BlockBuilder;
1818
import com.facebook.presto.common.block.BlockBuilderStatus;
1919
import com.facebook.presto.common.block.Int128ArrayBlockBuilder;
20+
import com.facebook.presto.common.block.LongArrayBlock;
2021
import com.facebook.presto.common.block.PageBuilderStatus;
2122
import com.facebook.presto.common.function.SqlFunctionProperties;
2223
import io.airlift.slice.Slice;
2324
import io.airlift.slice.Slices;
2425

26+
import java.math.BigInteger;
27+
2528
import static com.facebook.presto.common.block.Int128ArrayBlock.INT128_BYTES;
2629
import static com.facebook.presto.common.type.Decimals.MAX_PRECISION;
2730
import static com.facebook.presto.common.type.Decimals.decodeUnscaledValue;
31+
import static com.facebook.presto.common.type.Decimals.encodeUnscaledValue;
2832
import static com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH;
2933
import static com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.compare;
3034
import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
3135

32-
final class LongDecimalType
36+
public final class LongDecimalType
3337
extends DecimalType
3438
{
3539
LongDecimalType(int precision, int scale)
@@ -77,6 +81,10 @@ public Object getObjectValue(SqlFunctionProperties properties, Block block, int
7781
if (block.isNull(position)) {
7882
return null;
7983
}
84+
if (block instanceof LongArrayBlock) {
85+
long unscaledValue = block.getLong(position);
86+
return new SqlDecimal(BigInteger.valueOf(unscaledValue), getPrecision(), getScale());
87+
}
8088
Slice slice = getSlice(block, position);
8189
return new SqlDecimal(decodeUnscaledValue(slice), getPrecision(), getScale());
8290
}
@@ -112,9 +120,15 @@ public void appendTo(Block block, int position, BlockBuilder blockBuilder)
112120
blockBuilder.appendNull();
113121
}
114122
else {
115-
blockBuilder.writeLong(block.getLong(position, 0));
116-
blockBuilder.writeLong(block.getLong(position, SIZE_OF_LONG));
117-
blockBuilder.closeEntry();
123+
if (block instanceof LongArrayBlock) {
124+
Slice slice = encodeUnscaledValue(block.getLong(position));
125+
writeSlice(blockBuilder, slice);
126+
}
127+
else {
128+
blockBuilder.writeLong(block.getLong(position, 0));
129+
blockBuilder.writeLong(block.getLong(position, SIZE_OF_LONG));
130+
blockBuilder.closeEntry();
131+
}
118132
}
119133
}
120134

@@ -138,6 +152,9 @@ public void writeSlice(BlockBuilder blockBuilder, Slice value, int offset, int l
138152
@Override
139153
public Slice getSlice(Block block, int position)
140154
{
155+
if (block instanceof LongArrayBlock) {
156+
return encodeUnscaledValue(block.getLong(position));
157+
}
141158
return Slices.wrappedLongArray(
142159
block.getLong(position, 0),
143160
block.getLong(position, SIZE_OF_LONG));

presto-docs/src/main/sphinx/sql/alter-table.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Synopsis
1717
ALTER TABLE [ IF EXISTS ] name SET PROPERTIES (property_name=value, [, ...])
1818
ALTER TABLE [ IF EXISTS ] name DROP BRANCH [ IF EXISTS ] branch_name
1919
ALTER TABLE [ IF EXISTS ] name DROP TAG [ IF EXISTS ] tag_name
20+
ALTER TABLE [ IF EXISTS ] name ALTER COLUMN column_name SET DATA TYPE new_type
2021
ALTER TABLE [ IF EXISTS ] name CREATE [ OR REPLACE ] BRANCH [ IF NOT EXISTS ] branch_name
2122
ALTER TABLE [ IF EXISTS ] name CREATE [ OR REPLACE ] BRANCH [ IF NOT EXISTS ] branch_name FOR SYSTEM_VERSION AS OF version
2223
ALTER TABLE [ IF EXISTS ] name CREATE [ OR REPLACE ] BRANCH [ IF NOT EXISTS ] branch_name FOR SYSTEM_TIME AS OF timestamp

presto-hive/src/main/java/com/facebook/presto/hive/security/LegacyAccessControl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public void checkCanCreateViewWithSelectFromColumns(ConnectorTransactionHandle t
241241
{
242242
}
243243

244+
@Override
245+
public void checkCanAlterColumn(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName) {}
246+
244247
@Override
245248
public void checkCanSetCatalogSessionProperty(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, String propertyName)
246249
{

presto-hive/src/main/java/com/facebook/presto/hive/security/SqlStandardAccessControl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static com.facebook.presto.hive.metastore.thrift.ThriftMetastoreUtil.listEnabledTablePrivileges;
5757
import static com.facebook.presto.spi.security.AccessDeniedException.denyAddColumn;
5858
import static com.facebook.presto.spi.security.AccessDeniedException.denyAddConstraint;
59+
import static com.facebook.presto.spi.security.AccessDeniedException.denyAlterColumn;
5960
import static com.facebook.presto.spi.security.AccessDeniedException.denyCallProcedure;
6061
import static com.facebook.presto.spi.security.AccessDeniedException.denyCreateBranch;
6162
import static com.facebook.presto.spi.security.AccessDeniedException.denyCreateRole;
@@ -405,6 +406,24 @@ public void checkCanUpdateTableColumns(ConnectorTransactionHandle transaction, C
405406
}
406407
}
407408

409+
@Override
410+
public void checkCanAlterColumn(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
411+
{
412+
MetastoreContext metastoreContext = new MetastoreContext(
413+
identity, context.getQueryId().getId(),
414+
context.getClientInfo(),
415+
context.getClientTags(),
416+
context.getSource(),
417+
Optional.empty(),
418+
false,
419+
HiveColumnConverterProvider.DEFAULT_COLUMN_CONVERTER_PROVIDER,
420+
context.getWarningCollector(),
421+
context.getRuntimeStats());
422+
if (!isTableOwner(transaction, identity, metastoreContext, tableName)) {
423+
denyAlterColumn(tableName.toString());
424+
}
425+
}
426+
408427
@Override
409428
public void checkCanCreateView(ConnectorTransactionHandle transaction, ConnectorIdentity identity, AccessControlContext context, SchemaTableName viewName)
410429
{

presto-iceberg/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,10 @@
784784
<artifactId>commons-math3</artifactId>
785785
<scope>test</scope>
786786
</dependency>
787+
<dependency>
788+
<groupId>com.facebook.presto</groupId>
789+
<artifactId>presto-parser</artifactId>
790+
</dependency>
787791
</dependencies>
788792

789793
<build>

0 commit comments

Comments
 (0)