This is an automated email from the ASF dual-hosted git repository.
diqiu50 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 572a11af75 [#7848] feat(trino-connector): Support create table with
column properties in MySQL (#7852)
572a11af75 is described below
commit 572a11af75f3c88c9e5f11beb78a0651da267971
Author: qbhan <[email protected]>
AuthorDate: Tue Aug 12 09:26:33 2025 +0800
[#7848] feat(trino-connector): Support create table with column properties
in MySQL (#7852)
### What changes were proposed in this pull request?
Support create MySQL table with default value and auto-increment
### Why are the changes needed?
Fix: #7848
### Does this PR introduce _any_ user-facing change?
no
### How was this patch tested?
local tests
---
docs/trino-connector/catalog-mysql.md | 54 ++++++-
.../docker-script/init/mysql/init.sql | 107 ++++++++++++++
.../jdbc-mysql/00011_column_properties.sql | 159 ++++++++++++++++++++
.../jdbc-mysql/00011_column_properties.txt | 161 +++++++++++++++++++++
.../jdbc/JdbcColumnDefaultValueConverter.java | 85 +++++++++++
.../catalog/jdbc/mysql/MySQLConnectorAdapter.java | 5 +-
.../catalog/jdbc/mysql/MySQLMetadataAdapter.java | 90 +++++++++++-
.../catalog/jdbc/mysql/MySQLPropertyMeta.java | 45 +++++-
.../mysql/MysqlColumnDefaultValueConverter.java | 105 ++++++++++++++
.../trino/connector/metadata/GravitinoColumn.java | 43 +++++-
.../trino/connector/metadata/GravitinoTable.java | 2 +-
.../connector/metadata/TestGravitinoColumn.java | 8 +-
12 files changed, 851 insertions(+), 13 deletions(-)
diff --git a/docs/trino-connector/catalog-mysql.md
b/docs/trino-connector/catalog-mysql.md
index 1dd6ea869b..616ac2380a 100644
--- a/docs/trino-connector/catalog-mysql.md
+++ b/docs/trino-connector/catalog-mysql.md
@@ -16,7 +16,7 @@ To connect to MySQL, you need:
## Create table
-At present, the Apache Gravitino Trino connector only supports basic MySQL
table creation statements, which involve fields, null allowances, comments,
primary keys and indexes. However, it does not support advanced features like
default values and auto-increment.
+At present, the Apache Gravitino Trino connector only supports basic MySQL
table creation statements, which involve fields, null allowances, comments,
primary keys, indexes, default values and auto-increment.
The Gravitino Trino connector does not support `CREATE TABLE AS SELECT`.
@@ -47,6 +47,32 @@ The following are supported MySQL table properties:
| primary_key | list | (none) | The primary
keys for the table, can choose multi columns as the table primary key. All key
columns must be defined as `NOT NULL`. | No | 1.0.0 |
| unique_key | list | (none) | The unique
keys for the table, can choose multi columns for multi unique key. Each unique
key should be defined as `keyName:col1,col2`. | No | 1.0.0 |
+The following are supported MySQL column properties:
+
+| Property name | Type | Default Value | Description
| Required | Since Version |
+|------------------------------------|---------|---------------|---------------------------------------------------|----------|---------------|
+| auto_increment | boolean | false | The auto
increment column. | No | 1.0.0 |
+| default | string | (none) | The default
value for column. | No | 1.0.0 |
+
+**Note:** Currently, creating tables only supports constant default values and
does not support expression default values,
+and `show create table` also exclusively renders constant default values in
its output.
+The following are Trino type which support configuration of default values:
+
+| Type name | Default Value example |
+|-----------|-----------------------------------------|
+| TINYINT | 1 |
+| SMALLINT | 1 |
+| INT | 1 |
+| BIGINT | 1 |
+| REAL | 1.0 |
+| DOUBLE | 1.0 |
+| DECIMAL | 1.0 |
+| VARCHAR | abc |
+| CHAR | abc |
+| DATE | 2025-08-07 |
+| TIME | 01:01:01 |
+| TIMESTAMP | 2025-08-07 01:01:01 (CURRENT_TIMESTAMP) |
+
## Basic usage examples
You need to do the following steps before you can use the MySQL catalog in
Trino through Gravitino.
@@ -144,6 +170,32 @@ WITH (
);
```
+Create a new table named `table_column_properties` in schema
`mysql_test.database_01` with auto_increment and default.
+
+```sql
+CREATE TABLE mysql_test.database_01.table_column_properties(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ f1 VARCHAR(200) WITH (default='VARCHAR'),
+ f2 CHAR(20) WITH (default='CHAR') ,
+ f4 DECIMAL(10, 3) WITH (default='0.3') ,
+ f5 REAL WITH (default='0.3') ,
+ f6 DOUBLE WITH (default='0.3') ,
+ f8 TINYINT WITH (default='1') ,
+ f9 SMALLINT WITH (default='1') ,
+ f10 INT WITH (default='1') ,
+ f11 INTEGER WITH (default='1') ,
+ f12 BIGINT WITH (default='1'),
+ f13 DATE WITH (default='2024-04-01'),
+ f14 TIME WITH (default='08:00:00'),
+ f15 TIMESTAMP WITH (default='2012-12-31 11:30:45'),
+ f16 TIMESTAMP WITH TIME ZONE WITH (default='2012-12-31 11:30:45'),
+ f17 TIMESTAMP WITH TIME ZONE WITH (default='CURRENT_TIMESTAMP')
+)
+WITH (
+ primary_key = ARRAY['key1']
+);
+```
+
### Writing data
Insert data into the table `table_01`:
diff --git a/integration-test-common/docker-script/init/mysql/init.sql
b/integration-test-common/docker-script/init/mysql/init.sql
index 8848b0f23d..718ba3b64f 100644
--- a/integration-test-common/docker-script/init/mysql/init.sql
+++ b/integration-test-common/docker-script/init/mysql/init.sql
@@ -118,3 +118,110 @@ INSERT INTO gt_mysql_test_all_type.demo (
ST_GeomFromText('POINT(10 20)'), -- point_col
ST_GeomFromText('LINESTRING(0 0, 10 10)') -- geometry_col
);
+CREATE DATABASE gt_mysql_test_column_properties;
+/*
++----------------+-------------------------------------+------+-----+---------------------+-------------------+
+| Field | Type | Null | Key | Default
| Extra |
++----------------+-------------------------------------+------+-----+---------------------+-------------------+
+| col_bit | bit(1) | YES | | b'1'
| |
+| col_tinyint | tinyint | YES | | 0
| |
+| col_smallint | smallint | YES | | -1
| |
+| col_mediumint | mediumint | YES | | 100
| |
+| col_int | int | YES | | 1
| |
+| col_bigint | bigint | YES | | 9999999
| |
+| col_float | float | YES | | 0
| |
+| col_double | double | YES | | 1.23456
| |
+| col_decimal | decimal(10,2) | YES | | 100.50
| |
+| col_date | date | YES | |
2000-01-01 | |
+| col_time | time | YES | | 12:30:01
| |
+| col_datetime | datetime | YES | |
2025-01-01 00:00:01 | |
+| col_timestamp | timestamp | NO | |
2025-01-01 00:00:01 | |
+| col_year | year | YES | | 2025
| |
+| col_char | char(10) | YES | | abc
| |
+| col_varchar | varchar(100) | YES | | Hello
| |
+| col_tinytext | tinytext | YES | | NULL
| |
+| col_text | text | YES | | NULL
| |
+| col_mediumtext | mediumtext | YES | | NULL
| |
+| col_longtext | longtext | YES | | NULL
| |
+| col_binary | binary(5) | YES | | 0x61
| |
+| col_varbinary | varbinary(100) | YES | |
0x62696E617279 | |
+| col_blob | blob | YES | | NULL
| |
+| col_mediumblob | mediumblob | YES | | NULL
| |
+| col_longblob | longblob | YES | | NULL
| |
+| col_enum | enum('active','inactive','pending') | YES | | pending
| |
+| col_set | set('red','green','blue') | YES | |
green,blue | |
+| col_json | json | YES | | NULL
| |
+| col_boolean | tinyint(1) | YES | | 1
| |
+| col_geometry | geometry | YES | | NULL
| |
+| col_point | point | YES | |
point(0,0) | DEFAULT_GENERATED |
++----------------+-------------------------------------+------+-----+---------------------+-------------------+
+*/
+CREATE TABLE gt_mysql_test_column_properties.demo_all_data_types_default_value
(
+ -- 整数类型
+ col_bit BIT DEFAULT b'1',
+ col_tinyint TINYINT DEFAULT 0,
+ col_smallint SMALLINT DEFAULT -1,
+ col_mediumint MEDIUMINT DEFAULT 100,
+ col_int INT DEFAULT 1,
+ col_bigint BIGINT DEFAULT 9999999,
+ -- 浮点与精确小数
+ col_float FLOAT DEFAULT 0.0,
+ col_double DOUBLE DEFAULT 1.23456,
+ col_decimal DECIMAL(10, 2) DEFAULT 100.50,
+ -- 日期与时间
+ col_date DATE DEFAULT '2000-01-01',
+ col_time TIME DEFAULT '12:30:01',
+ col_datetime DATETIME DEFAULT '2025-01-01 00:00:01',
+ col_timestamp TIMESTAMP DEFAULT '2025-01-01 00:00:01',
+ col_year YEAR DEFAULT 2025,
+ -- 字符串类型
+ col_char CHAR(10) DEFAULT 'abc',
+ col_varchar VARCHAR(100) DEFAULT 'Hello',
+ col_tinytext TINYTEXT DEFAULT NULL,
+ col_text TEXT DEFAULT NULL,
+ col_mediumtext MEDIUMTEXT DEFAULT NULL,
+ col_longtext LONGTEXT DEFAULT NULL,
+ -- 二进制数据
+ col_binary BINARY(5) DEFAULT 'a',
+ col_varbinary VARBINARY(100) DEFAULT 'binary',
+ col_blob BLOB DEFAULT NULL,
+ col_mediumblob MEDIUMBLOB DEFAULT NULL,
+ col_longblob LONGBLOB DEFAULT NULL,
+ -- 特殊类型
+ col_enum ENUM('active', 'inactive', 'pending') DEFAULT 'pending',
+ col_set SET('red', 'green', 'blue') DEFAULT 'green,blue',
+ col_json JSON DEFAULT NULL,
+ col_boolean BOOLEAN DEFAULT TRUE,
+ -- 空间数据类型
+ col_geometry GEOMETRY DEFAULT NULL,
+ col_point POINT DEFAULT (POINT(0, 0))
+);
+/*
++------------------+--------------+------+-----+-------------------------------+-------------------+
+| Field | Type | Null | Key | Default
| Extra |
++------------------+--------------+------+-----+-------------------------------+-------------------+
+| int_col_2 | int | YES | | rand()
| DEFAULT_GENERATED |
+| varchar200_col_1 | varchar(200) | YES | | curdate()
| DEFAULT_GENERATED |
+| varchar200_col_2 | varchar(200) | YES | | now()
| DEFAULT_GENERATED |
+| datetime_col_1 | datetime | YES | | CURRENT_TIMESTAMP
| DEFAULT_GENERATED |
+| datetime_col_2 | datetime | YES | | CURRENT_TIMESTAMP
| DEFAULT_GENERATED |
+| date_col_1 | date | YES | | curdate()
| DEFAULT_GENERATED |
+| date_col_2 | date | YES | | (curdate() + interval 1 year)
| DEFAULT_GENERATED |
+| date_col_3 | date | YES | | curdate()
| DEFAULT_GENERATED |
+| timestamp_col_1 | timestamp | NO | | CURRENT_TIMESTAMP
| DEFAULT_GENERATED |
+| timestamp_col_2 | timestamp(6) | NO | | CURRENT_TIMESTAMP(6)
| DEFAULT_GENERATED |
++------------------+--------------+------+-----+-------------------------------+-------------------+
+*/
+CREATE TABLE gt_mysql_test_column_properties.demo_default_value_with_expression
+(
+ int_col_2 int default (rand()),
+ varchar200_col_1 varchar(200) default (curdate()),
+ varchar200_col_2 varchar(200) default (CURRENT_TIMESTAMP),
+ datetime_col_1 datetime default CURRENT_TIMESTAMP,
+ datetime_col_2 datetime default current_timestamp,
+ date_col_1 date default (CURRENT_DATE),
+ date_col_2 date DEFAULT (CURRENT_DATE + INTERVAL 1 YEAR),
+ date_col_3 date DEFAULT (CURRENT_DATE),
+ timestamp_col_1 timestamp default CURRENT_TIMESTAMP,
+ timestamp_col_2 timestamp(6) default CURRENT_TIMESTAMP(6)
+);
diff --git
a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.sql
b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.sql
new file mode 100644
index 0000000000..417d77f141
--- /dev/null
+++
b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.sql
@@ -0,0 +1,159 @@
+-- Table create by integration-test-common/docker-script/init/mysql/init.sql
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties.demo_all_data_types_default_value;
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties.demo_default_value_with_expression;
+
+CREATE SCHEMA gt_mysql.gt_mysql_test_column_properties_v2;
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_default_value(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ f1 VARCHAR(200) WITH (default='VARCHAR'),
+ f2 CHAR(20) WITH (default='CHAR') ,
+ f4 DECIMAL(10, 3) WITH (default='0.3') ,
+ f5 REAL WITH (default='0.3') ,
+ f6 DOUBLE WITH (default='0.3') ,
+ f8 TINYINT WITH (default='1') ,
+ f9 SMALLINT WITH (default='1') ,
+ f10 INT WITH (default='1') ,
+ f11 INTEGER WITH (default='1') ,
+ f12 BIGINT WITH (default='1'),
+ f13 DATE WITH (default='2024-04-01'),
+ f14 TIME WITH (default='08:00:00'),
+ f15 TIMESTAMP WITH (default='2012-12-31 11:30:45'),
+ f16 TIMESTAMP WITH TIME ZONE WITH (default='2012-12-31 11:30:45'),
+ f17 TIMESTAMP WITH TIME ZONE WITH (default='CURRENT_TIMESTAMP')
+)
+WITH (
+ primary_key = ARRAY['key1']
+);
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_default_value;
+
+DROP TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_default_value;
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_default_value(
+ key1 INT NOT NULL,
+ f2 DATE WITH (default='1')
+)
+WITH (
+ primary_key = ARRAY['key1']
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_default_value_2(
+ key1 INT NOT NULL,
+ f2 UUID WITH (default='1')
+)
+WITH (
+ primary_key = ARRAY['key1']
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_1(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ f1 TIMESTAMP ,
+ f2 DATE
+)
+WITH (
+ primary_key = ARRAY['key1']
+);
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_1;
+
+DROP TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_1;
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_2(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ key2 INT NOT NULL,
+ f1 TIMESTAMP ,
+ f2 DATE
+)
+WITH (
+ primary_key = ARRAY['key1', 'key2']
+);
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_2;
+
+DROP TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_2;
+
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_3 (
+ key1 INT NOT NULL WITH (auto_increment=true),
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ unique_key = ARRAY['unique_key1:key1']
+);
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_3;
+
+DROP TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_3;
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_4 (
+ key1 INT NOT NULL WITH (auto_increment=true),
+ key2 INT,
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key1'],
+ unique_key = ARRAY['unique_key1:key1,key2']
+);
+
+SHOW CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_4;
+
+DROP TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_4;
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_auto_increment_1
(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ key2 INT,
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_auto_increment_2
(
+ key1 INT NOT NULL WITH (auto_increment=true),
+ key2 INT NOT NULL WITH (auto_increment=true),
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key2','key1']
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_auto_increment_3
(
+ key1 INT WITH (auto_increment=true),
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ unique_key = ARRAY['unique_key1:key1']
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_auto_increment_4
(
+ key1 INT NOT NULL WITH (auto_increment=true, default='1'),
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ unique_key = ARRAY['unique_key1:key1']
+);
+
+CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_invalid_auto_increment_5
(
+ key1 VARCHAR NOT NULL WITH (auto_increment=true),
+ col1 INT
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ unique_key = ARRAY['unique_key1:key1']
+);
+
+DROP SCHEMA gt_mysql.gt_mysql_test_column_properties_v2;
\ No newline at end of file
diff --git
a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.txt
b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.txt
new file mode 100644
index 0000000000..c3e55683f7
--- /dev/null
+++
b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00011_column_properties.txt
@@ -0,0 +1,161 @@
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties.demo_all_data_types_default_value (
+ col_bit boolean,
+ col_tinyint tinyint WITH ( default = '0' ),
+ col_smallint smallint WITH ( default = '-1' ),
+ col_mediumint integer,
+ col_int integer WITH ( default = '1' ),
+ col_bigint bigint WITH ( default = '9999999' ),
+ col_float real WITH ( default = '0.0' ),
+ col_double double WITH ( default = '1.23456' ),
+ col_decimal decimal(10, 2) WITH ( default = '100.50' ),
+ col_date date WITH ( default = '2000-01-01' ),
+ col_time time(0) WITH ( default = '12:30:01' ),
+ col_datetime timestamp(0) WITH ( default = '2025-01-01 00:00:01' ),
+ col_timestamp timestamp(0) with time zone WITH ( default = '2025-01-01
00:00:01' ),
+ col_year date,
+ col_char char(10) WITH ( default = 'abc' ),
+ col_varchar varchar(100) WITH ( default = 'Hello' ),
+ col_tinytext varchar,
+ col_text varchar,
+ col_mediumtext varchar,
+ col_longtext varchar,
+ col_binary varbinary,
+ col_varbinary varbinary,
+ col_blob varbinary,
+ col_mediumblob varbinary,
+ col_longblob varbinary,
+ col_enum varchar,
+ col_set varchar,
+ col_json json,
+ col_boolean boolean,
+ col_geometry varbinary,
+ col_point varbinary
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties.demo_default_value_with_expression (
+ int_col_2 integer,
+ varchar200_col_1 varchar(200),
+ varchar200_col_2 varchar(200),
+ datetime_col_1 timestamp(0) WITH ( default = 'CURRENT_TIMESTAMP' ),
+ datetime_col_2 timestamp(0) WITH ( default = 'CURRENT_TIMESTAMP' ),
+ date_col_1 date,
+ date_col_2 date,
+ date_col_3 date,
+ timestamp_col_1 timestamp(0) with time zone WITH ( default =
'CURRENT_TIMESTAMP' ),
+ timestamp_col_2 timestamp(0) with time zone WITH ( default =
'CURRENT_TIMESTAMP' )
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB'
+)"
+
+CREATE SCHEMA
+
+CREATE TABLE
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_default_value (
+ key1 integer NOT NULL WITH ( auto_increment = true ),
+ f1 varchar(200) WITH ( default = 'VARCHAR' ),
+ f2 char(20) WITH ( default = 'CHAR' ),
+ f4 decimal(10, 3) WITH ( default = '0.300' ),
+ f5 real WITH ( default = '0.3' ),
+ f6 double WITH ( default = '0.3' ),
+ f8 tinyint WITH ( default = '1' ),
+ f9 smallint WITH ( default = '1' ),
+ f10 integer WITH ( default = '1' ),
+ f11 integer WITH ( default = '1' ),
+ f12 bigint WITH ( default = '1' ),
+ f13 date WITH ( default = '2024-04-01' ),
+ f14 time(0) WITH ( default = '08:00' ),
+ f15 timestamp(0) WITH ( default = '2012-12-31 11:30:45' ),
+ f16 timestamp(0) with time zone WITH ( default = '2012-12-31 11:30:45' ),
+ f17 timestamp(0) with time zone WITH ( default = 'CURRENT_TIMESTAMP' )
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key1']
+)"
+
+DROP TABLE
+
+<QUERY_FAILED> Invalidate default value 1 for DATE type. Text '1' could not be
parsed at index 0
+
+<QUERY_FAILED> Invalidate default value 1 for UUID type. This Type not support
default value
+
+CREATE TABLE
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_1 (
+ key1 integer NOT NULL WITH ( auto_increment = true ),
+ f1 timestamp(0),
+ f2 date
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key1']
+)"
+
+DROP TABLE
+
+CREATE TABLE
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_2 (
+ key1 integer NOT NULL WITH ( auto_increment = true ),
+ key2 integer NOT NULL,
+ f1 timestamp(0),
+ f2 date
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key1','key2']
+)"
+
+DROP TABLE
+
+CREATE TABLE
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_3 (
+ key1 integer NOT NULL WITH ( auto_increment = true ),
+ col1 integer
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ unique_key = ARRAY['unique_key1:key1']
+)"
+
+DROP TABLE
+
+CREATE TABLE
+
+"CREATE TABLE
gt_mysql.gt_mysql_test_column_properties_v2.test_create_with_auto_increment_4 (
+ key1 integer NOT NULL WITH ( auto_increment = true ),
+ key2 integer,
+ col1 integer
+)
+COMMENT ''
+WITH (
+ engine = 'InnoDB',
+ primary_key = ARRAY['key1'],
+ unique_key = ARRAY['unique_key1:key1,key2']
+)"
+
+DROP TABLE
+
+<QUERY_FAILED> Auto increment column must be defined as a key in MySQL
+
+<QUERY_FAILED> Only one column can be auto-incremented in MySQL
+
+<QUERY_FAILED> Auto increment column must be not null in MySQL
+
+<QUERY_FAILED> Auto increment column cannot have a default value in MySQL
+
+<QUERY_FAILED> Auto increment column must be integer type in MySQL
+
+DROP SCHEMA
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/JdbcColumnDefaultValueConverter.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/JdbcColumnDefaultValueConverter.java
new file mode 100644
index 0000000000..abd94b4432
--- /dev/null
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/JdbcColumnDefaultValueConverter.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.trino.connector.catalog.jdbc;
+
+import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import org.apache.gravitino.rel.expressions.Expression;
+import org.apache.gravitino.rel.expressions.FunctionExpression;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.types.Type;
+import org.apache.gravitino.rel.types.Types;
+
+/**
+ * Column default value converter
+ *
+ * <p>Referred from
org/apache/gravitino/catalog/jdbc/converter/JdbcColumnDefaultValueConverter.java
+ */
+public class JdbcColumnDefaultValueConverter {
+
+ protected static final String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
+ protected static final String NULL = "NULL";
+ protected static final DateTimeFormatter DATE_TIME_FORMATTER =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss[.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]");
+ protected static final DateTimeFormatter DATE_FORMATTER =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ protected static final DateTimeFormatter TIME_FORMATTER =
+
DateTimeFormatter.ofPattern("HH:mm:ss[.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]");
+
+ public String fromGravitino(Expression defaultValue) {
+ if (DEFAULT_VALUE_NOT_SET.equals(defaultValue)) {
+ return null;
+ }
+
+ if (defaultValue instanceof FunctionExpression) {
+ FunctionExpression functionExpression = (FunctionExpression)
defaultValue;
+ if
(functionExpression.functionName().equalsIgnoreCase(CURRENT_TIMESTAMP)) {
+ // CURRENT_TIMESTAMP is a special case(key word), it should not be
wrapped in parentheses
+ return CURRENT_TIMESTAMP;
+ }
+ }
+
+ if (defaultValue instanceof Literal) {
+ Literal<?> literal = (Literal<?>) defaultValue;
+ Type type = literal.dataType();
+ if (defaultValue.equals(Literals.NULL)) {
+ // Not display default value if default value is null
+ return null;
+ } else if (type instanceof Types.TimestampType) {
+ /** @see LocalDateTime#toString() would return like
'yyyy-MM-ddTHH:mm:ss' */
+ if (literal.value() instanceof String || literal.value() instanceof
LocalDateTime) {
+ return literal.value().toString().replace("T", " ");
+ }
+ return literal.value().toString();
+ } else {
+ return literal.value().toString();
+ }
+ }
+
+ // todo support UnparseExpression default value convert
+ return null;
+ }
+
+ public Expression toGravitino(Type columnType, String columnDefaultValue,
boolean nullable) {
+ return DEFAULT_VALUE_NOT_SET;
+ }
+}
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLConnectorAdapter.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLConnectorAdapter.java
index bd002ef4c0..f646297457 100644
---
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLConnectorAdapter.java
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLConnectorAdapter.java
@@ -18,8 +18,6 @@
*/
package org.apache.gravitino.trino.connector.catalog.jdbc.mysql;
-import static java.util.Collections.emptyList;
-
import io.trino.spi.session.PropertyMetadata;
import java.util.List;
import java.util.Map;
@@ -60,7 +58,8 @@ public class MySQLConnectorAdapter implements
CatalogConnectorAdapter {
@Override
public CatalogConnectorMetadataAdapter getMetadataAdapter() {
// TODO yuhui Need to improve schema table and column properties
- return new MySQLMetadataAdapter(getSchemaProperties(),
getTableProperties(), emptyList());
+ return new MySQLMetadataAdapter(
+ getSchemaProperties(), getTableProperties(), getColumnProperties());
}
@Override
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLMetadataAdapter.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLMetadataAdapter.java
index 614600889c..1c8d051cd5 100644
---
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLMetadataAdapter.java
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLMetadataAdapter.java
@@ -21,10 +21,14 @@ package
org.apache.gravitino.trino.connector.catalog.jdbc.mysql;
import static io.trino.spi.StandardErrorCode.INVALID_TABLE_PROPERTY;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static java.lang.String.format;
+import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;
import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.TABLE_PRIMARY_KEY;
import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.TABLE_UNIQUE_KEY;
+import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.filterColumnProperties;
+import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.getDefaultValue;
import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.getPrimaryKey;
import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.getUniqueKey;
+import static
org.apache.gravitino.trino.connector.catalog.jdbc.mysql.MySQLPropertyMeta.isAutoIncrement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -43,10 +47,13 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.catalog.property.PropertyConverter;
import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.rel.indexes.Indexes;
+import org.apache.gravitino.rel.types.Type;
import
org.apache.gravitino.trino.connector.catalog.CatalogConnectorMetadataAdapter;
+import
org.apache.gravitino.trino.connector.catalog.jdbc.JdbcColumnDefaultValueConverter;
import org.apache.gravitino.trino.connector.metadata.GravitinoColumn;
import org.apache.gravitino.trino.connector.metadata.GravitinoTable;
import org.apache.logging.log4j.util.Strings;
@@ -55,6 +62,7 @@ import org.apache.logging.log4j.util.Strings;
public class MySQLMetadataAdapter extends CatalogConnectorMetadataAdapter {
private final PropertyConverter tableConverter;
+ private final JdbcColumnDefaultValueConverter columnDefaultValueConverter;
/**
* Constructs a new MySQLMetadataAdapter.
@@ -70,6 +78,7 @@ public class MySQLMetadataAdapter extends
CatalogConnectorMetadataAdapter {
super(schemaProperties, tableProperties, columnProperties, new
MySQLDataTypeTransformer());
this.tableConverter = new MySQLTablePropertyConverter();
+ this.columnDefaultValueConverter = new MysqlColumnDefaultValueConverter();
}
@Override
@@ -97,29 +106,44 @@ public class MySQLMetadataAdapter extends
CatalogConnectorMetadataAdapter {
Map<String, Set<String>> uniqueKeyMap = getUniqueKey(tableProperties);
List<GravitinoColumn> columns = new ArrayList<>();
+ ImmutableList.Builder<GravitinoColumn> incrementColumnListBuilder =
ImmutableList.builder();
ImmutableSet.Builder<String> columnNamesBuilder = ImmutableSet.builder();
for (int i = 0; i < tableMetadata.getColumns().size(); i++) {
ColumnMetadata column = tableMetadata.getColumns().get(i);
if (primaryKeyList.contains(column.getName()) && column.isNullable()) {
throw new TrinoException(NOT_SUPPORTED, "Primary key must be NOT NULL
in MySQL");
}
- boolean autoIncrement =
- (boolean)
column.getProperties().getOrDefault(MySQLPropertyMeta.AUTO_INCREMENT, false);
- columns.add(
+ Map<String, Object> columnProperties = column.getProperties();
+ boolean autoIncrement = isAutoIncrement(columnProperties);
+ String defaultValue = getDefaultValue(columnProperties);
+
+ Type gravitinoType =
dataTypeTransformer.getGravitinoType(column.getType());
+ GravitinoColumn gravitinoColumn =
new GravitinoColumn(
column.getName(),
- dataTypeTransformer.getGravitinoType(column.getType()),
+ gravitinoType,
i,
column.getComment(),
column.isNullable(),
autoIncrement,
- column.getProperties()));
+ columnDefaultValueConverter.toGravitino(
+ gravitinoType, defaultValue, column.isNullable()),
+ filterColumnProperties(columnProperties));
+
+ columns.add(gravitinoColumn);
+
+ if (autoIncrement) {
+ incrementColumnListBuilder.add(gravitinoColumn);
+ }
+
columnNamesBuilder.add(column.getName());
}
Index[] indexes = buildIndexes(primaryKeyList, uniqueKeyMap,
columnNamesBuilder.build());
+ validateIncrementCol(incrementColumnListBuilder.build(), indexes);
+
return new GravitinoTable(schemaName, tableName, columns, comment,
properties, indexes);
}
@@ -174,6 +198,57 @@ public class MySQLMetadataAdapter extends
CatalogConnectorMetadataAdapter {
return fieldNames.stream().map(colName -> new String[]
{colName}).toArray(String[][]::new);
}
+ /**
+ * The auto-increment column will be verified. There can only be one
auto-increment column and it
+ * must be the primary key or unique index.
+ *
+ * @param columns table auto increment columns
+ * @param indexes table indexes
+ */
+ protected static void validateIncrementCol(List<GravitinoColumn> columns,
Index[] indexes) {
+ // check not exists multi auto-increment column
+ if (columns.size() > 1) {
+ throw new TrinoException(NOT_SUPPORTED, "Only one column can be
auto-incremented in MySQL");
+ }
+ if (!columns.isEmpty()) {
+ GravitinoColumn incrementColumn = columns.get(0);
+ // check auto-increment column must be integer type
+ if (!(incrementColumn.getType() instanceof Type.IntegralType)) {
+ throw new TrinoException(
+ NOT_SUPPORTED, "Auto increment column must be integer type in
MySQL");
+ }
+ // check auto-increment column must be not null
+ if (incrementColumn.isNullable()) {
+ throw new TrinoException(NOT_SUPPORTED, "Auto increment column must be
not null in MySQL");
+ }
+ // check auto-increment column have no default value
+ if (DEFAULT_VALUE_NOT_SET != incrementColumn.getDefaultValue()) {
+ throw new TrinoException(
+ NOT_SUPPORTED, "Auto increment column cannot have a default value
in MySQL");
+ }
+ // check the only one auto-increment column can only be defined in
primary key or unique
+ // key.
+ // auto-increment column can exist in both primary key and unique key
+ String colName = incrementColumn.getName();
+ Optional<Index> existAutoIncrementColIndexOptional =
+ Arrays.stream(indexes)
+ .filter(
+ index ->
+ Arrays.stream(index.fieldNames())
+ .flatMap(Arrays::stream)
+ .anyMatch(s -> StringUtils.equalsIgnoreCase(colName,
s)))
+ .filter(
+ index ->
+ index.type() == Index.IndexType.PRIMARY_KEY
+ || index.type() == Index.IndexType.UNIQUE_KEY)
+ .findAny();
+ if (!existAutoIncrementColIndexOptional.isPresent()) {
+ throw new TrinoException(
+ NOT_SUPPORTED, "Auto increment column must be defined as a key in
MySQL");
+ }
+ }
+ }
+
@Override
public ConnectorTableMetadata getTableMetadata(GravitinoTable
gravitinoTable) {
SchemaTableName schemaTableName =
@@ -231,6 +306,11 @@ public class MySQLMetadataAdapter extends
CatalogConnectorMetadataAdapter {
propertyMap.put(MySQLPropertyMeta.AUTO_INCREMENT, true);
}
+ String defaultValue =
columnDefaultValueConverter.fromGravitino(column.getDefaultValue());
+ if (defaultValue != null) {
+ propertyMap.put(MySQLPropertyMeta.DEFAULT, defaultValue);
+ }
+
return ColumnMetadata.builder()
.setName(column.getName())
.setType(dataTypeTransformer.getTrinoType(column.getType()))
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLPropertyMeta.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLPropertyMeta.java
index 8ebe1dd7db..69bb50a0f5 100644
---
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLPropertyMeta.java
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLPropertyMeta.java
@@ -33,6 +33,7 @@ import io.trino.spi.type.ArrayType;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.stream.Collectors;
import org.apache.gravitino.trino.connector.catalog.HasPropertyMeta;
/**
@@ -72,9 +73,16 @@ public class MySQLPropertyMeta implements HasPropertyMeta {
/** Property name for auto-incrementing columns. */
public static final String AUTO_INCREMENT = "auto_increment";
+ /** Property name for the default value of columns. */
+ public static final String DEFAULT = "default";
private static final List<PropertyMetadata<?>> COLUMN_PROPERTY_META =
- ImmutableList.of(booleanProperty(AUTO_INCREMENT, "The auto increment
column", false, false));
+ ImmutableList.of(
+ booleanProperty(AUTO_INCREMENT, "The auto increment column", false,
false),
+ stringProperty(DEFAULT, "The default value of column", null, false));
+
+ private static final List<String> filterColumnProperties =
+ ImmutableList.of(AUTO_INCREMENT, DEFAULT);
@Override
public List<PropertyMetadata<?>> getTablePropertyMetadata() {
@@ -130,4 +138,39 @@ public class MySQLPropertyMeta implements HasPropertyMeta {
return uniqueKeyMapBuilder.build();
}
+
+ /**
+ * Extract auto increment property from column properties
+ *
+ * @param columnProperties column properties
+ * @return auto increment property
+ */
+ public static boolean isAutoIncrement(Map<String, Object> columnProperties) {
+ Preconditions.checkArgument(columnProperties != null, "columnProperties is
null");
+ return (boolean)
columnProperties.getOrDefault(MySQLPropertyMeta.AUTO_INCREMENT, false);
+ }
+
+ /**
+ * Extract default value property from column properties
+ *
+ * @param columnProperties column properties
+ * @return default value property
+ */
+ public static String getDefaultValue(Map<String, Object> columnProperties) {
+ Preconditions.checkArgument(columnProperties != null, "columnProperties is
null");
+ return (String) columnProperties.get(MySQLPropertyMeta.DEFAULT);
+ }
+
+ /**
+ * Filter specified property from column properties
+ *
+ * @param columnProperties column properties
+ * @return column properties
+ */
+ public static Map<String, Object> filterColumnProperties(Map<String, Object>
columnProperties) {
+ Preconditions.checkArgument(columnProperties != null, "columnProperties is
null");
+ return columnProperties.entrySet().stream()
+ .filter(entry -> !filterColumnProperties.contains(entry.getKey()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ }
}
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MysqlColumnDefaultValueConverter.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MysqlColumnDefaultValueConverter.java
new file mode 100644
index 0000000000..0dc6238fb9
--- /dev/null
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MysqlColumnDefaultValueConverter.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.trino.connector.catalog.jdbc.mysql;
+
+import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
+import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;
+import static
org.apache.gravitino.rel.Column.DEFAULT_VALUE_OF_CURRENT_TIMESTAMP;
+
+import io.trino.spi.TrinoException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeParseException;
+import org.apache.gravitino.rel.expressions.Expression;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.types.Decimal;
+import org.apache.gravitino.rel.types.Type;
+import org.apache.gravitino.rel.types.Types;
+import
org.apache.gravitino.trino.connector.catalog.jdbc.JdbcColumnDefaultValueConverter;
+
+/**
+ * Column default value converter for MySQL
+ *
+ * <p>Referred from
+ *
org/apache/gravitino/catalog/mysql/converter/MysqlColumnDefaultValueConverter.java
+ */
+public class MysqlColumnDefaultValueConverter extends
JdbcColumnDefaultValueConverter {
+
+ @Override
+ public Expression toGravitino(Type type, String columnDefaultValue, boolean
nullable) {
+ if (columnDefaultValue == null) {
+ return nullable ? Literals.NULL : DEFAULT_VALUE_NOT_SET;
+ }
+
+ if (columnDefaultValue.equalsIgnoreCase(NULL)) {
+ return Literals.NULL;
+ }
+
+ try {
+ switch (type.name()) {
+ case BYTE:
+ return Literals.byteLiteral(Byte.valueOf(columnDefaultValue));
+ case SHORT:
+ return Literals.shortLiteral(Short.valueOf(columnDefaultValue));
+ case INTEGER:
+ return Literals.integerLiteral(Integer.valueOf(columnDefaultValue));
+ case LONG:
+ return Literals.longLiteral(Long.valueOf(columnDefaultValue));
+ case FLOAT:
+ return Literals.floatLiteral(Float.valueOf(columnDefaultValue));
+ case DOUBLE:
+ return Literals.doubleLiteral(Double.valueOf(columnDefaultValue));
+ case DECIMAL:
+ Types.DecimalType decimalType = (Types.DecimalType) type;
+ return Literals.decimalLiteral(
+ Decimal.of(columnDefaultValue, decimalType.precision(),
decimalType.scale()));
+ case DATE:
+ return Literals.dateLiteral(LocalDate.parse(columnDefaultValue,
DATE_FORMATTER));
+ case TIME:
+ return Literals.timeLiteral(LocalTime.parse(columnDefaultValue,
TIME_FORMATTER));
+ case TIMESTAMP:
+ if (CURRENT_TIMESTAMP.equalsIgnoreCase(columnDefaultValue)) {
+ return DEFAULT_VALUE_OF_CURRENT_TIMESTAMP;
+ }
+ try {
+ return Literals.timestampLiteral(
+ LocalDateTime.parse(columnDefaultValue, DATE_TIME_FORMATTER));
+ } catch (DateTimeParseException e) {
+ throw new IllegalArgumentException(
+ String.format("Unable to parse datetime value: %s",
columnDefaultValue));
+ }
+ case FIXEDCHAR:
+ Types.FixedCharType fixedCharType = (Types.FixedCharType) type;
+ return Literals.of(columnDefaultValue,
Types.FixedCharType.of(fixedCharType.length()));
+ case VARCHAR:
+ case STRING:
+ return Literals.stringLiteral(columnDefaultValue);
+ default:
+ throw new IllegalArgumentException("This Type not support default
value");
+ }
+ } catch (Exception e) {
+ throw new TrinoException(
+ NOT_SUPPORTED,
+ String.format(
+ "Invalidate default value %s for %s type. %s",
+ columnDefaultValue, type.name(), e.getMessage()));
+ }
+ }
+}
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoColumn.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoColumn.java
index 5c618656ff..4584b07bb6 100644
---
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoColumn.java
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoColumn.java
@@ -18,9 +18,12 @@
*/
package org.apache.gravitino.trino.connector.metadata;
+import static org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET;
+
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.apache.gravitino.rel.Column;
+import org.apache.gravitino.rel.expressions.Expression;
import org.apache.gravitino.rel.types.Type;
/** Help Gravitino connector access ColumnMetadata from gravitino client. */
@@ -31,6 +34,7 @@ public final class GravitinoColumn {
private final String comment;
private final boolean nullable;
private final boolean autoIncrement;
+ private final Expression defaultValue;
// Column properties
private final Map<String, Object> properties;
@@ -49,12 +53,13 @@ public final class GravitinoColumn {
column.comment(),
column.nullable(),
column.autoIncrement(),
+ column.defaultValue(),
ImmutableMap.of());
}
/**
* Constructs a new GravitinoColumn with the specified name, data type,
index, comment, nullable,
- * autoIncrement, and properties.
+ * autoIncrement, defaultValue and properties.
*
* @param name the name of the column
* @param dataType the data type of the column
@@ -62,6 +67,7 @@ public final class GravitinoColumn {
* @param comment the comment of the column
* @param nullable whether the column is nullable
* @param autoIncrement whether the column is auto increment
+ * @param defaultValue the default value of the column
* @param properties the properties of the column
*/
public GravitinoColumn(
@@ -71,6 +77,7 @@ public final class GravitinoColumn {
String comment,
boolean nullable,
boolean autoIncrement,
+ Expression defaultValue,
Map<String, Object> properties) {
this.name = name;
this.dataType = dataType;
@@ -78,9 +85,34 @@ public final class GravitinoColumn {
this.comment = comment;
this.nullable = nullable;
this.autoIncrement = autoIncrement;
+ this.defaultValue = defaultValue;
this.properties = properties;
}
+ /**
+ * Constructs a new GravitinoColumn with the specified name, data type,
index, comment, nullable,
+ * autoIncrement, and properties.
+ *
+ * @param name the name of the column
+ * @param dataType the data type of the column
+ * @param index the index of the column
+ * @param comment the comment of the column
+ * @param nullable whether the column is nullable
+ * @param autoIncrement whether the column is auto increment
+ * @param properties the properties of the column
+ */
+ public GravitinoColumn(
+ String name,
+ Type dataType,
+ int index,
+ String comment,
+ boolean nullable,
+ boolean autoIncrement,
+ Map<String, Object> properties) {
+ this(
+ name, dataType, index, comment, nullable, autoIncrement,
DEFAULT_VALUE_NOT_SET, properties);
+ }
+
/**
* Constructs a new GravitinoColumn with the specified name, data type,
index, comment, and
* nullable.
@@ -166,4 +198,13 @@ public final class GravitinoColumn {
public boolean isAutoIncrement() {
return autoIncrement;
}
+
+ /**
+ * Retrieves the default value of the column.
+ *
+ * @return the default value of the column
+ */
+ public Expression getDefaultValue() {
+ return defaultValue;
+ }
}
diff --git
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoTable.java
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoTable.java
index 6b4869f174..8542fe3440 100644
---
a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoTable.java
+++
b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/metadata/GravitinoTable.java
@@ -180,7 +180,7 @@ public class GravitinoTable {
column.getComment(),
column.isNullable(),
column.isAutoIncrement(),
- null);
+ column.getDefaultValue());
}
return gravitinoColumns;
}
diff --git
a/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/metadata/TestGravitinoColumn.java
b/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/metadata/TestGravitinoColumn.java
index f2bb213704..2c3628503b 100644
---
a/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/metadata/TestGravitinoColumn.java
+++
b/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/metadata/TestGravitinoColumn.java
@@ -21,6 +21,7 @@ package org.apache.gravitino.trino.connector.metadata;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.gravitino.rel.Column;
+import org.apache.gravitino.rel.expressions.literals.Literals;
import org.apache.gravitino.rel.types.Types;
import org.junit.jupiter.api.Test;
@@ -28,12 +29,17 @@ public class TestGravitinoColumn {
@Test
public void testGravitinoColumn() {
- Column column = Column.of("f1", Types.StringType.get(), "test column");
+ Column column =
+ Column.of(
+ "f1", Types.StringType.get(), "test column", false, true,
Literals.stringLiteral("1"));
GravitinoColumn gravitinoColumn = new GravitinoColumn(column, 0);
assertEquals(gravitinoColumn.getName(), column.name());
assertEquals(gravitinoColumn.getIndex(), 0);
assertEquals(gravitinoColumn.getComment(), column.comment());
assertEquals(gravitinoColumn.getType(), column.dataType());
+ assertEquals(gravitinoColumn.isNullable(), column.nullable());
+ assertEquals(gravitinoColumn.isAutoIncrement(), column.autoIncrement());
+ assertEquals(gravitinoColumn.getDefaultValue(), column.defaultValue());
}
}