Skip to content

Commit

Permalink
Add support for adding columns with comments in Kudu
Browse files Browse the repository at this point in the history
  • Loading branch information
findinpath authored and ebyhr committed Apr 4, 2022
1 parent ead9ec8 commit addbffa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -399,15 +407,15 @@ 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);
setTypeAttributes(columnMetadata, builder);
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();
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private ColumnMetadata getColumnMetadata(ColumnSchema column)
.setType(prestoType)
.setExtraInfo(Optional.of(extra.toString()))
.setProperties(properties)
.setComment(Optional.ofNullable(column.getComment()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit addbffa

Please sign in to comment.