From addbffac8aff36dffca07f19fd8282ae2354dd25 Mon Sep 17 00:00:00 2001 From: Marius Grama Date: Sat, 26 Mar 2022 21:08:13 +0100 Subject: [PATCH] Add support for adding columns with comments in Kudu --- .../trino/plugin/kudu/KuduClientSession.java | 22 ++++++++---- .../io/trino/plugin/kudu/KuduMetadata.java | 1 + .../kudu/AbstractKuduConnectorTest.java | 36 ++++++++++++++----- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduClientSession.java b/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduClientSession.java index 6bd4351020d7..87050f5fcba1 100644 --- a/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduClientSession.java +++ b/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduClientSession.java @@ -68,6 +68,9 @@ import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR; import static io.trino.spi.StandardErrorCode.QUERY_REJECTED; import static java.util.stream.Collectors.toList; +import static org.apache.kudu.ColumnSchema.ColumnSchemaBuilder; +import static org.apache.kudu.ColumnSchema.CompressionAlgorithm; +import static org.apache.kudu.ColumnSchema.Encoding; import static org.apache.kudu.client.KuduPredicate.ComparisonOp.GREATER; import static org.apache.kudu.client.KuduPredicate.ComparisonOp.GREATER_EQUAL; import static org.apache.kudu.client.KuduPredicate.ComparisonOp.LESS; @@ -312,7 +315,12 @@ public void addColumn(SchemaTableName schemaTableName, ColumnMetadata column) String rawName = schemaEmulation.toRawName(schemaTableName); AlterTableOptions alterOptions = new AlterTableOptions(); Type type = TypeHelper.toKuduClientType(column.getType()); - alterOptions.addNullableColumn(column.getName(), type); + alterOptions.addColumn( + new ColumnSchemaBuilder(column.getName(), type) + .nullable(true) + .defaultValue(null) + .comment(column.getComment()) + .build()); client.alterTable(rawName, alterOptions); } catch (KuduException e) { @@ -399,7 +407,7 @@ private ColumnSchema toColumnSchema(ColumnMetadata columnMetadata) String name = columnMetadata.getName(); ColumnDesign design = KuduTableProperties.getColumnDesign(columnMetadata.getProperties()); Type ktype = TypeHelper.toKuduClientType(columnMetadata.getType()); - ColumnSchema.ColumnSchemaBuilder builder = new ColumnSchema.ColumnSchemaBuilder(name, ktype); + ColumnSchemaBuilder builder = new ColumnSchemaBuilder(name, ktype); builder.key(design.isPrimaryKey()).nullable(design.isNullable()); setEncoding(name, builder, design); setCompression(name, builder, design); @@ -407,7 +415,7 @@ private ColumnSchema toColumnSchema(ColumnMetadata columnMetadata) return builder.build(); } - private void setTypeAttributes(ColumnMetadata columnMetadata, ColumnSchema.ColumnSchemaBuilder builder) + private void setTypeAttributes(ColumnMetadata columnMetadata, ColumnSchemaBuilder builder) { if (columnMetadata.getType() instanceof DecimalType) { DecimalType type = (DecimalType) columnMetadata.getType(); @@ -418,11 +426,11 @@ private void setTypeAttributes(ColumnMetadata columnMetadata, ColumnSchema.Colum } } - private void setCompression(String name, ColumnSchema.ColumnSchemaBuilder builder, ColumnDesign design) + private void setCompression(String name, ColumnSchemaBuilder builder, ColumnDesign design) { if (design.getCompression() != null) { try { - ColumnSchema.CompressionAlgorithm algorithm = KuduTableProperties.lookupCompression(design.getCompression()); + CompressionAlgorithm algorithm = KuduTableProperties.lookupCompression(design.getCompression()); builder.compressionAlgorithm(algorithm); } catch (IllegalArgumentException e) { @@ -431,11 +439,11 @@ private void setCompression(String name, ColumnSchema.ColumnSchemaBuilder builde } } - private void setEncoding(String name, ColumnSchema.ColumnSchemaBuilder builder, ColumnDesign design) + private void setEncoding(String name, ColumnSchemaBuilder builder, ColumnDesign design) { if (design.getEncoding() != null) { try { - ColumnSchema.Encoding encoding = KuduTableProperties.lookupEncoding(design.getEncoding()); + Encoding encoding = KuduTableProperties.lookupEncoding(design.getEncoding()); builder.encoding(encoding); } catch (IllegalArgumentException e) { diff --git a/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduMetadata.java b/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduMetadata.java index dabea75d654c..c7e37773f5d6 100755 --- a/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduMetadata.java +++ b/plugin/trino-kudu/src/main/java/io/trino/plugin/kudu/KuduMetadata.java @@ -153,6 +153,7 @@ private ColumnMetadata getColumnMetadata(ColumnSchema column) .setType(prestoType) .setExtraInfo(Optional.of(extra.toString())) .setProperties(properties) + .setComment(Optional.ofNullable(column.getComment())) .build(); } diff --git a/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/AbstractKuduConnectorTest.java b/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/AbstractKuduConnectorTest.java index e9e8865cd449..8902e49c024f 100644 --- a/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/AbstractKuduConnectorTest.java +++ b/plugin/trino-kudu/src/test/java/io/trino/plugin/kudu/AbstractKuduConnectorTest.java @@ -132,15 +132,15 @@ public void testShowCreateTable() { assertThat((String) computeActual("SHOW CREATE TABLE orders").getOnlyValue()) .matches("CREATE TABLE kudu\\.\\w+\\.orders \\Q(\n" + - " orderkey bigint WITH ( nullable = true ),\n" + - " custkey bigint WITH ( nullable = true ),\n" + - " orderstatus varchar WITH ( nullable = true ),\n" + - " totalprice double WITH ( nullable = true ),\n" + - " orderdate varchar WITH ( nullable = true ),\n" + - " orderpriority varchar WITH ( nullable = true ),\n" + - " clerk varchar WITH ( nullable = true ),\n" + - " shippriority integer WITH ( nullable = true ),\n" + - " comment varchar WITH ( nullable = true )\n" + + " orderkey bigint COMMENT '' WITH ( nullable = true ),\n" + + " custkey bigint COMMENT '' WITH ( nullable = true ),\n" + + " orderstatus varchar COMMENT '' WITH ( nullable = true ),\n" + + " totalprice double COMMENT '' WITH ( nullable = true ),\n" + + " orderdate varchar COMMENT '' WITH ( nullable = true ),\n" + + " orderpriority varchar COMMENT '' WITH ( nullable = true ),\n" + + " clerk varchar COMMENT '' WITH ( nullable = true ),\n" + + " shippriority integer COMMENT '' WITH ( nullable = true ),\n" + + " comment varchar COMMENT '' WITH ( nullable = true )\n" + ")\n" + "WITH (\n" + " number_of_replicas = 3,\n" + @@ -283,6 +283,24 @@ public void testAddColumn() throw new SkipException("TODO"); } + @Test + public void testAddColumnWithComment() + { + String tableName = "test_add_column_with_comment" + randomTableSuffix(); + + assertUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + + "id INT WITH (primary_key=true), " + + "a_varchar VARCHAR" + + ") WITH (" + + " partition_by_hash_columns = ARRAY['id'], " + + " partition_by_hash_buckets = 2" + + ")"); + + assertUpdate("ALTER TABLE " + tableName + " ADD COLUMN b_varchar varchar COMMENT 'test new column comment'"); + assertThat(getColumnComment(tableName, "b_varchar")).isEqualTo("test new column comment"); + assertUpdate("DROP TABLE " + tableName); + } + @Test @Override public void testInsert()