Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2357] improvement(trino): Fix varchar type mapping between Gravitino JDBC catalog and Trino (#2358)

Qi Yu77fcc1309497
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryIT.java
index faf07d037c..f0c3b50f04 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/trino/TrinoQueryIT.java
@@ -269,7 +269,8 @@ public class TrinoQueryIT extends TrinoQueryITBase {
if (expectResult.startsWith("<QUERY_FAILED>")) {
boolean match =
Pattern.compile(
- "^Query \\w+ failed.*: " + expectResult.replace("<QUERY_FAILED>", "").trim())
+ "^Query \\w+ failed.*: "
+ + Pattern.quote(expectResult.replace("<QUERY_FAILED>", "").trim()))
.matcher(result)
.find();
return match;
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.sql b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.sql
new file mode 100644
index 0000000000..a0143719f7
--- /dev/null
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.sql
@@ -0,0 +1,44 @@
+CREATE SCHEMA "test.gt_mysql".varchar_db1;
+
+USE "test.gt_mysql".varchar_db1;
+
+CREATE TABLE tb01 (id int, name char(20));
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb01;
+
+CREATE TABLE tb02 (id int, name char(255));
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb02;
+
+CREATE TABLE tb03 (id int, name char(256));
+
+CREATE TABLE tb04 (id int, name varchar(250));
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb04;
+
+CREATE TABLE tb05 (id int, name varchar(256));
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb05;
+
+CREATE TABLE tb06 (id int, name char);
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb06;
+
+CREATE TABLE tb07 (id int, name varchar);
+
+SHOW CREATE TABLE "test.gt_mysql".varchar_db1.tb07;
+
+drop table "test.gt_mysql".varchar_db1.tb01;
+
+drop table "test.gt_mysql".varchar_db1.tb02;
+
+drop table "test.gt_mysql".varchar_db1.tb04;
+
+drop table "test.gt_mysql".varchar_db1.tb05;
+
+drop table "test.gt_mysql".varchar_db1.tb06;
+
+drop table "test.gt_mysql".varchar_db1.tb07;
+
+drop schema "test.gt_mysql".varchar_db1;
+
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.txt b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.txt
new file mode 100644
index 0000000000..cbad718d69
--- /dev/null
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00007_varchar.txt
@@ -0,0 +1,85 @@
+CREATE SCHEMA
+
+USE
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb01 (
+ id integer,
+ name char(20)
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb02 (
+ id integer,
+ name char(255)
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+<QUERY_FAILED> MySQL does not support the datatype CHAR with the length greater than 255
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb04 (
+ id integer,
+ name varchar(250)
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb05 (
+ id integer,
+ name varchar(256)
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb06 (
+ id integer,
+ name char(1)
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_mysql"".varchar_db1.tb07 (
+ id integer,
+ name varchar
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP SCHEMA
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00004_query_pushdown.txt b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00004_query_pushdown.txt
index 348492934c..d5fe30a858 100644
--- a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00004_query_pushdown.txt
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00004_query_pushdown.txt
@@ -20,7 +20,7 @@ INSERT: 15000 rows
"Trino version: %
%
└─ TableScan[table = test.gt_postgresql:gt_db1.customer gt_db1.customer constraints=[ParameterizedExpression[expression=(""phone"") LIKE (?), parameters=[QueryParameter{jdbcType=Optional.empty, type=varchar(6), value=Optional[Slice{base=[B@%, baseOffset=0, length=6}]}]]] limit=10]
- Layout: [custkey:bigint, name:varchar, address:varchar, nationkey:bigint, phone:varchar, acctbal:decimal(12,2), mktsegment:varchar, comment:varchar]
+ Layout: [custkey:bigint, name:varchar(25), address:varchar(40), nationkey:bigint, phone:varchar(15), acctbal:decimal(12,2), mktsegment:varchar(10), comment:varchar(117)]
%
"
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00006_datatype.txt b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00006_datatype.txt
index 56ebe5d87c..4c3789a351 100644
--- a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00006_datatype.txt
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00006_datatype.txt
@@ -5,8 +5,8 @@ USE
CREATE TABLE
"CREATE TABLE ""test.gt_postgresql"".gt_db1.tb01 (
- f1 varchar,
- f2 varchar,
+ f1 varchar(200),
+ f2 char(20),
f3 varbinary,
f4 decimal(10, 3),
f5 real,
@@ -26,14 +26,14 @@ INSERT: 1 row
INSERT: 1 row
-"Sample text 1","Text1","65","123.456","7.89","12.34","false","100","1000","1000","100000","2024-01-01","08:00:00.000","2024-01-01 08:00:00.000"
+"Sample text 1","Text1 ","65","123.456","7.89","12.34","false","100","1000","1000","100000","2024-01-01","08:00:00.000","2024-01-01 08:00:00.000"
"","","","","","","","","","","","","",""
CREATE TABLE
"CREATE TABLE ""test.gt_postgresql"".gt_db1.tb02 (
- f1 varchar NOT NULL,
- f2 varchar NOT NULL,
+ f1 varchar(200) NOT NULL,
+ f2 char(20) NOT NULL,
f3 varbinary NOT NULL,
f4 decimal(10, 3) NOT NULL,
f5 real NOT NULL,
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.sql b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.sql
new file mode 100644
index 0000000000..b21f99f9d2
--- /dev/null
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.sql
@@ -0,0 +1,46 @@
+CREATE SCHEMA "test.gt_postgresql".varchar_db1;
+
+USE "test.gt_postgresql".varchar_db1;
+
+CREATE TABLE tb01 (id int, name char(20));
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb01;
+
+CREATE TABLE tb02 (id int, name char(65536));
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb02;
+
+CREATE TABLE tb03 (id int, name char(65537));
+
+CREATE TABLE tb04 (id int, name varchar(250));
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb04;
+
+CREATE TABLE tb05 (id int, name varchar(10485760));
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb05;
+
+CREATE TABLE tb06 (id int, name varchar(10485761));
+
+CREATE TABLE tb06 (id int, name char);
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb06;
+
+CREATE TABLE tb07 (id int, name varchar);
+
+SHOW CREATE TABLE "test.gt_postgresql".varchar_db1.tb07;
+
+drop table "test.gt_postgresql".varchar_db1.tb01;
+
+drop table "test.gt_postgresql".varchar_db1.tb02;
+
+drop table "test.gt_postgresql".varchar_db1.tb04;
+
+drop table "test.gt_postgresql".varchar_db1.tb05;
+
+drop table "test.gt_postgresql".varchar_db1.tb06;
+
+drop table "test.gt_postgresql".varchar_db1.tb07;
+
+drop schema "test.gt_postgresql".varchar_db1;
+
diff --git a/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.txt b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.txt
new file mode 100644
index 0000000000..f7776c53be
--- /dev/null
+++ b/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00007_varchar.txt
@@ -0,0 +1,69 @@
+CREATE SCHEMA
+
+USE
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb01 (
+ id integer,
+ name char(20)
+)
+COMMENT ''"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb02 (
+ id integer,
+ name char(65536)
+)
+COMMENT ''"
+
+<QUERY_FAILED> Unknown type 'char(65537)' for column 'name'
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb04 (
+ id integer,
+ name varchar(250)
+)
+COMMENT ''"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb05 (
+ id integer,
+ name varchar(10485760)
+)
+COMMENT ''"
+
+<QUERY_FAILED> length for type varchar cannot exceed 10485760
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb06 (
+ id integer,
+ name char(1)
+)
+COMMENT ''"
+
+CREATE TABLE
+
+"CREATE TABLE ""test.gt_postgresql"".varchar_db1.tb07 (
+ id integer,
+ name varchar
+)
+COMMENT ''"
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP TABLE
+
+DROP SCHEMA
diff --git a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/MySQLDataTypeTransformer.java b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/MySQLDataTypeTransformer.java
index 549898fe3c..ca742d8517 100644
--- a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/MySQLDataTypeTransformer.java
+++ b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/MySQLDataTypeTransformer.java
@@ -11,27 +11,65 @@ import com.datastrato.gravitino.rel.types.Types;
import com.datastrato.gravitino.trino.connector.GravitinoErrorCode;
import com.datastrato.gravitino.trino.connector.util.GeneralDataTypeTransformer;
import io.trino.spi.TrinoException;
+import io.trino.spi.type.CharType;
/** Type transformer between MySQL and Trino */
public class MySQLDataTypeTransformer extends GeneralDataTypeTransformer {
private static final int MYSQL_CHAR_LENGTH_LIMIT = 255;
+ // 65535 / 4 = 16383, in fact, MySQL limit the row size to 65535, and the utf8mb4 character set
+ // uses 4 bytes per character. In fact, if a row has several varchar columns, the length of each
+ // column should be less than 16383. For more details, please refer to
+ // https://dev.mysql.com/doc/refman/8.0/en/char.html
+ private static final int MYSQL_VARCHAR_LENGTH_LIMIT = 16383;
+
+ @Override
+ public io.trino.spi.type.Type getTrinoType(Type type) {
+ if (type.name() == Name.STRING) {
+ return io.trino.spi.type.VarcharType.createUnboundedVarcharType();
+ }
+
+ return super.getTrinoType(type);
+ }
@Override
public Type getGravitinoType(io.trino.spi.type.Type type) {
- Type gravitinoType = super.getGravitinoType(type);
- if (gravitinoType.name() == Name.VARCHAR) {
- if (((Types.VarCharType) gravitinoType).length() > MYSQL_CHAR_LENGTH_LIMIT) {
+ Class<? extends io.trino.spi.type.Type> typeClass = type.getClass();
+ if (typeClass == io.trino.spi.type.CharType.class) {
+ CharType charType = (CharType) type;
+ if (charType.getLength() > MYSQL_CHAR_LENGTH_LIMIT) {
+ throw new TrinoException(
+ GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
+ "MySQL does not support the datatype CHAR with the length greater than "
+ + MYSQL_CHAR_LENGTH_LIMIT);
+ }
+
+ // We do not support the CHAR without a length.
+ if (charType.getLength() == 0) {
+ throw new TrinoException(
+ GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
+ "MySQL does not support the datatype CHAR with the length 0");
+ }
+
+ return Types.FixedCharType.of(charType.getLength());
+ } else if (typeClass == io.trino.spi.type.VarcharType.class) {
+ io.trino.spi.type.VarcharType varcharType = (io.trino.spi.type.VarcharType) type;
+
+ // If the length is not specified, it is a VARCHAR without length, we convert it to a string
+ // type.
+ if (varcharType.getLength().isEmpty()) {
return Types.StringType.get();
}
- }
- if (gravitinoType.name() == Name.FIXEDCHAR) {
- if (((Types.FixedCharType) gravitinoType).length() > MYSQL_CHAR_LENGTH_LIMIT) {
+ int length = varcharType.getLength().get();
+ if (length > MYSQL_VARCHAR_LENGTH_LIMIT) {
throw new TrinoException(
GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
- "MySQL does not support the datatype CHAR with the length greater than 255");
+ "MySQL does not support the datatype VARCHAR with the length greater than "
+ + MYSQL_VARCHAR_LENGTH_LIMIT);
}
+ return Types.VarCharType.of(length);
}
- return gravitinoType;
+
+ return super.getGravitinoType(type);
}
}
diff --git a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/PostgreSQLDataTypeTransformer.java b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/PostgreSQLDataTypeTransformer.java
index fb7a99ac23..c2faaf714b 100644
--- a/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/PostgreSQLDataTypeTransformer.java
+++ b/trino-connector/src/main/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/PostgreSQLDataTypeTransformer.java
@@ -8,17 +8,55 @@ package com.datastrato.gravitino.trino.connector.catalog.jdbc.postgresql;
import com.datastrato.gravitino.rel.types.Type;
import com.datastrato.gravitino.rel.types.Type.Name;
import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.trino.connector.GravitinoErrorCode;
import com.datastrato.gravitino.trino.connector.util.GeneralDataTypeTransformer;
+import io.trino.spi.TrinoException;
+import io.trino.spi.type.CharType;
/** Type transformer between PostgreSQL and Trino */
public class PostgreSQLDataTypeTransformer extends GeneralDataTypeTransformer {
+ private static final int POSTGRESQL_CHAR_LENGTH_LIMIT = 10485760;
+ // 1 GB, please refer to
+ // https://stackoverflow.com/questions/70785582/is-a-varchar-unlimited-in-postgresql
+ private static final int POSTGRESQL_VARCHAR_LENGTH_LIMIT = 10485760;
+
+ @Override
+ public io.trino.spi.type.Type getTrinoType(Type type) {
+ if (type.name() == Name.STRING) {
+ return io.trino.spi.type.VarcharType.createUnboundedVarcharType();
+ }
+
+ return super.getTrinoType(type);
+ }
@Override
public Type getGravitinoType(io.trino.spi.type.Type type) {
- Type gravitinoType = super.getGravitinoType(type);
- if (gravitinoType.name() == Name.VARCHAR || gravitinoType.name() == Name.FIXEDCHAR) {
- return Types.StringType.get();
+ Class<? extends io.trino.spi.type.Type> typeClass = type.getClass();
+ if (typeClass == io.trino.spi.type.CharType.class) {
+ CharType charType = (CharType) type;
+
+ // Do not need to check the scenario that the length of the CHAR type is greater than
+ // POSTGRESQL_CHAR_LENGTH_LIMIT ,because the length of the CHAR type in Trino is no greater
+ // than 65536 We do not support the CHAR without a length.
+ if (charType.getLength() == 0) {
+ throw new TrinoException(
+ GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
+ "PostgreSQL does not support the datatype CHAR with the length 0");
+ }
+
+ return Types.FixedCharType.of(charType.getLength());
+ } else if (typeClass == io.trino.spi.type.VarcharType.class) {
+ io.trino.spi.type.VarcharType varcharType = (io.trino.spi.type.VarcharType) type;
+
+ // If the length is not specified, it is a VARCHAR without length, we convert it to a string
+ // type.
+ if (varcharType.getLength().isEmpty()) {
+ return Types.StringType.get();
+ }
+
+ return Types.VarCharType.of(varcharType.getLength().get());
}
- return gravitinoType;
+
+ return super.getGravitinoType(type);
}
}
diff --git a/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/TestMySQLDataTypeTransformer.java b/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/TestMySQLDataTypeTransformer.java
new file mode 100644
index 0000000000..2b75aa0f72
--- /dev/null
+++ b/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/mysql/TestMySQLDataTypeTransformer.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.trino.connector.catalog.jdbc.mysql;
+
+import com.datastrato.gravitino.rel.types.Type;
+import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.trino.connector.util.GeneralDataTypeTransformer;
+import io.trino.spi.TrinoException;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestMySQLDataTypeTransformer {
+
+ @Test
+ public void testTrinoTypeToGravitinoType() {
+ GeneralDataTypeTransformer generalDataTypeTransformer = new MySQLDataTypeTransformer();
+ io.trino.spi.type.Type charTypeWithLengthOne = io.trino.spi.type.CharType.createCharType(1);
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(charTypeWithLengthOne),
+ Types.FixedCharType.of(1));
+
+ io.trino.spi.type.Type charTypeWithLength = io.trino.spi.type.CharType.createCharType(256);
+ Exception e =
+ Assert.expectThrows(
+ TrinoException.class,
+ () -> generalDataTypeTransformer.getGravitinoType(charTypeWithLength));
+ Assert.assertTrue(
+ e.getMessage()
+ .contains("MySQL does not support the datatype CHAR with the length greater than 255"));
+
+ io.trino.spi.type.Type varcharType = io.trino.spi.type.VarcharType.createVarcharType(1);
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(varcharType), Types.VarCharType.of(1));
+
+ io.trino.spi.type.Type varcharTypeWithLength =
+ io.trino.spi.type.VarcharType.createVarcharType(16384);
+ e =
+ Assert.expectThrows(
+ TrinoException.class,
+ () -> generalDataTypeTransformer.getGravitinoType(varcharTypeWithLength));
+ Assert.assertTrue(
+ e.getMessage()
+ .contains(
+ "MySQL does not support the datatype VARCHAR with the length greater than 16383"));
+
+ io.trino.spi.type.Type varcharTypeWithLength2 =
+ io.trino.spi.type.VarcharType.createUnboundedVarcharType();
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(varcharTypeWithLength2),
+ Types.StringType.get());
+ }
+
+ @Test
+ public void testGravitinoCharToTrinoType() {
+ GeneralDataTypeTransformer generalDataTypeTransformer = new MySQLDataTypeTransformer();
+
+ Type stringType = Types.StringType.get();
+ Assert.assertEquals(
+ generalDataTypeTransformer.getTrinoType(stringType),
+ io.trino.spi.type.VarcharType.createUnboundedVarcharType());
+ }
+}
diff --git a/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/TestPostgreSQLDataTypeTransformer.java b/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/TestPostgreSQLDataTypeTransformer.java
new file mode 100644
index 0000000000..d4adcf3151
--- /dev/null
+++ b/trino-connector/src/test/java/com/datastrato/gravitino/trino/connector/catalog/jdbc/postgresql/TestPostgreSQLDataTypeTransformer.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.trino.connector.catalog.jdbc.postgresql;
+
+import com.datastrato.gravitino.rel.types.Type;
+import com.datastrato.gravitino.rel.types.Types;
+import com.datastrato.gravitino.trino.connector.util.GeneralDataTypeTransformer;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestPostgreSQLDataTypeTransformer {
+
+ @Test
+ public void testTrinoTypeToGravitinoType() {
+ GeneralDataTypeTransformer generalDataTypeTransformer = new PostgreSQLDataTypeTransformer();
+ io.trino.spi.type.Type charTypeWithLengthOne = io.trino.spi.type.CharType.createCharType(1);
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(charTypeWithLengthOne),
+ Types.FixedCharType.of(1));
+
+ io.trino.spi.type.Type charTypeWithLength = io.trino.spi.type.CharType.createCharType(65536);
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(charTypeWithLength),
+ Types.FixedCharType.of(65536));
+
+ io.trino.spi.type.Type varcharType = io.trino.spi.type.VarcharType.createVarcharType(1);
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(varcharType), Types.VarCharType.of(1));
+
+ io.trino.spi.type.Type varcharTypeWithLength =
+ io.trino.spi.type.VarcharType.createVarcharType(65536);
+
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(varcharTypeWithLength),
+ Types.VarCharType.of(65536));
+
+ io.trino.spi.type.Type varcharTypeWithLength2 =
+ io.trino.spi.type.VarcharType.createUnboundedVarcharType();
+ Assert.assertEquals(
+ generalDataTypeTransformer.getGravitinoType(varcharTypeWithLength2),
+ Types.StringType.get());
+ }
+
+ @Test
+ public void testGravitinoCharToTrinoType() {
+ GeneralDataTypeTransformer generalDataTypeTransformer = new PostgreSQLDataTypeTransformer();
+ Type stringType = Types.StringType.get();
+ Assert.assertEquals(
+ generalDataTypeTransformer.getTrinoType(stringType),
+ io.trino.spi.type.VarcharType.createUnboundedVarcharType());
+ }
+}