Skip to content

Commit

Permalink
Coerce varchar(0) to varchar(1) for Hive table
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinsbd authored and kokosing committed Sep 15, 2023
1 parent 7d36ed5 commit 735770f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3414,6 +3415,15 @@ public Optional<ConnectorTableLayout> getNewTableLayout(ConnectorSession session
multipleWritersPerPartitionSupported));
}

@Override
public Optional<Type> 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<ConnectorTableLayout> getLayoutForTableExecute(ConnectorSession session, ConnectorTableExecuteHandle executeHandle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 735770f

Please sign in to comment.