Skip to content

Commit

Permalink
Fix result when complex type contains user defined type in Cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Jan 20, 2023
1 parent d8b4817 commit 6acfc5f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ private String objectToJson(Object cassandraValue, DataType dataType)
case INET:
case VARINT:
case TUPLE:
case UDT:
return quoteStringLiteralForJson(cassandraValue.toString());
case UDT:
return quoteStringLiteralForJson(((UdtValue) cassandraValue).getFormattedContents());

case BLOB:
case CUSTOM:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static io.trino.testing.MaterializedResult.resultBuilder;
import static io.trino.testing.QueryAssertions.assertContains;
import static io.trino.testing.QueryAssertions.assertContainsEventually;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static io.trino.type.IpAddressType.IPADDRESS;
import static java.lang.String.format;
import static java.util.Comparator.comparing;
Expand Down Expand Up @@ -993,6 +994,54 @@ public void testColumnNameAmbiguity()
session.execute("DROP KEYSPACE keyspace_5");
}

@Test
public void testUserDefinedTypeInArray()
{
String tableName = "test_udt_in_array" + randomNameSuffix();
String userDefinedTypeName = "test_udt" + randomNameSuffix();

session.execute("CREATE TYPE tpch." + userDefinedTypeName + "(udt_field bigint)");
session.execute("CREATE TABLE tpch." + tableName + "(id bigint, col list<frozen<tpch." + userDefinedTypeName + ">>, primary key (id))");
session.execute("INSERT INTO tpch." + tableName + "(id, col) values (1, [{udt_field: 10}])");

assertQuery("SELECT * FROM " + tableName, "VALUES (1, '[\"{udt_field:10}\"]')");

session.execute("DROP TABLE tpch." + tableName);
session.execute("DROP TYPE tpch." + userDefinedTypeName);
}

@Test
public void testUserDefinedTypeInMap()
{
String tableName = "test_udt_in_map" + randomNameSuffix();
String userDefinedTypeName = "test_udt" + randomNameSuffix();

session.execute("CREATE TYPE tpch." + userDefinedTypeName + "(udt_field bigint)");
session.execute("CREATE TABLE tpch." + tableName + "(id bigint, col map<frozen<tpch." + userDefinedTypeName + ">, frozen<tpch." + userDefinedTypeName + ">>, primary key (id))");
session.execute("INSERT INTO tpch." + tableName + "(id, col) values (1, {{udt_field: 10}: {udt_field: -10}})");

assertQuery("SELECT * FROM " + tableName, "VALUES (1, '{\"{udt_field:10}\":\"{udt_field:-10}\"}')");

session.execute("DROP TABLE tpch." + tableName);
session.execute("DROP TYPE tpch." + userDefinedTypeName);
}

@Test
public void testUserDefinedTypeInSet()
{
String tableName = "test_udt_in_set" + randomNameSuffix();
String userDefinedTypeName = "test_udt" + randomNameSuffix();

session.execute("CREATE TYPE tpch." + userDefinedTypeName + "(udt_field bigint)");
session.execute("CREATE TABLE tpch." + tableName + "(id bigint, col set<frozen<tpch." + userDefinedTypeName + ">>, primary key (id))");
session.execute("INSERT INTO tpch." + tableName + "(id, col) values (1, {{udt_field: 10}})");

assertQuery("SELECT * FROM " + tableName, "VALUES (1, '[\"{udt_field:10}\"]')");

session.execute("DROP TABLE tpch." + tableName);
session.execute("DROP TYPE tpch." + userDefinedTypeName);
}

@Test
public void testUnsupportedColumnType()
{
Expand Down

0 comments on commit 6acfc5f

Please sign in to comment.