From 735770ffb7d3d950a51dfbb7170026f316f64fb0 Mon Sep 17 00:00:00 2001 From: Marcin Rusek Date: Wed, 23 Aug 2023 17:34:13 +0200 Subject: [PATCH] Coerce varchar(0) to varchar(1) for Hive table --- .../io/trino/plugin/hive/HiveMetadata.java | 10 ++++++ .../plugin/hive/BaseHiveConnectorTest.java | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java index 966b4ff7c8b38..287afee216412 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java @@ -124,6 +124,7 @@ import io.trino.spi.type.TimestampType; import io.trino.spi.type.Type; import io.trino.spi.type.TypeManager; +import io.trino.spi.type.VarcharType; import org.apache.avro.Schema; import org.apache.avro.SchemaParseException; import org.apache.hadoop.fs.Path; @@ -3414,6 +3415,15 @@ public Optional getNewTableLayout(ConnectorSession session multipleWritersPerPartitionSupported)); } + @Override + public Optional getSupportedType(ConnectorSession session, Type type) + { + if (type instanceof VarcharType varcharType && !varcharType.isUnbounded() && varcharType.getBoundedLength() == 0) { + return Optional.of(VarcharType.createVarcharType(1)); + } + return Optional.empty(); + } + @Override public Optional getLayoutForTableExecute(ConnectorSession session, ConnectorTableExecuteHandle executeHandle) { diff --git a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/BaseHiveConnectorTest.java b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/BaseHiveConnectorTest.java index 20749bd40fbde..3d705556a7297 100644 --- a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/BaseHiveConnectorTest.java +++ b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/BaseHiveConnectorTest.java @@ -7987,6 +7987,41 @@ public void testWriteInvalidPrecisionTimestamp() "\\QIncorrect timestamp precision for timestamp(3); the configured precision is " + HiveTimestampPrecision.MICROSECONDS + "; column name: ts"); } + @Test + public void testCoercingVarchar0ToVarchar1() + { + try (TestTable testTable = new TestTable( + getQueryRunner()::execute, + "test_coercion_create_table_varchar", + "(var_column_0 varchar(0), var_column_1 varchar(1), var_column_10 varchar(10))")) { + assertEquals(getColumnType(testTable.getName(), "var_column_0"), "varchar(1)"); + assertEquals(getColumnType(testTable.getName(), "var_column_1"), "varchar(1)"); + assertEquals(getColumnType(testTable.getName(), "var_column_10"), "varchar(10)"); + } + } + + @Test + public void testCoercingVarchar0ToVarchar1WithCTAS() + { + try (TestTable testTable = new TestTable( + getQueryRunner()::execute, + "test_coercion_ctas_varchar", + "AS SELECT '' AS var_column")) { + assertEquals(getColumnType(testTable.getName(), "var_column"), "varchar(1)"); + } + } + + @Test + public void testCoercingVarchar0ToVarchar1WithCTASNoData() + { + try (TestTable testTable = new TestTable( + getQueryRunner()::execute, + "test_coercion_ctas_nd_varchar", + "AS SELECT '' AS var_column WITH NO DATA")) { + assertEquals(getColumnType(testTable.getName(), "var_column"), "varchar(1)"); + } + } + @Test public void testOptimize() {