Skip to content

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

[#6403] improvement(trino-connector): Support SQL UPDATE/DELETE with jdbc catalog (#8188)

qbhand184eee45e7b
diff --git a/docs/trino-connector/catalog-hive.md b/docs/trino-connector/catalog-hive.md
index 77a738f3df..1a999accfe 100644
--- a/docs/trino-connector/catalog-hive.md
+++ b/docs/trino-connector/catalog-hive.md
@@ -79,6 +79,14 @@ Support for the following alter table operations:
The Gravitino Trino connector supports most SELECT statements, allowing the execution of queries successfully.
Currently, it doesn't support certain query optimizations, such as pushdown and pruning functionalities.
+### Update
+
+Not support.
+
+### Delete
+
+Not support.
+
## Schema and table properties
You can set additional properties for tables and schemas in the Hive catalog using "WITH" keyword in the "CREATE"
diff --git a/docs/trino-connector/catalog-iceberg.md b/docs/trino-connector/catalog-iceberg.md
index ae827f17cc..13b5c9c8d3 100644
--- a/docs/trino-connector/catalog-iceberg.md
+++ b/docs/trino-connector/catalog-iceberg.md
@@ -55,11 +55,19 @@ Support for the following alter table operations:
- Change a column type
- Set a table property
-## Select
+### Select
The Apache Gravitino Trino connector supports most SELECT statements, allowing the execution of queries successfully.
Currently, it doesn't support certain query optimizations, such as pushdown and pruning functionalities.
+### Update
+
+Not support.
+
+### Delete
+
+Not support.
+
## Table and Schema properties
### Create a schema with properties
diff --git a/docs/trino-connector/catalog-mysql.md b/docs/trino-connector/catalog-mysql.md
index 616ac2380a..5a7a15528f 100644
--- a/docs/trino-connector/catalog-mysql.md
+++ b/docs/trino-connector/catalog-mysql.md
@@ -34,6 +34,14 @@ Support for the following alter table operations:
The Gravitino Trino connector supports most SELECT statements, allowing the execution of queries successfully.
Currently, it doesn't support certain query optimizations, such as indexes and pushdowns.
+## Update
+
+Only `UPDATE` statements with constant assignments and predicates are supported. See also [UPDATE limitation](https://trino.io/docs/current/connector/mysql.html#update-limitation).
+
+## Delete
+
+If the `WHERE` clause is specified, only the matching rows are deleted. Otherwise, all rows from the table are deleted. See also [DELETE limitation](https://trino.io/docs/current/connector/mysql.html#delete-limitation).
+
## Table and Schema properties
MySQL's schemas cannot support properties.
@@ -210,6 +218,19 @@ Insert data into the table `table_01` from select:
INSERT INTO mysql_test.database_01.table_01 (name, salary) SELECT * FROM "test.mysql_test".database_01.table_01;
```
+Update the table `table_01`:
+
+```sql
+UPDATE mysql_test.database_01.table_01 SET name = 'ice_update' WHERE salary = 12;
+```
+
+Delete the table `table_01`:
+
+```sql
+DELETE FROM mysql_test.database_01.table_01 WHERE salary = 12;
+DELETE FROM mysql_test.database_01.table_01;
+```
+
### Querying data
Query the `table_01` table:
diff --git a/docs/trino-connector/catalog-postgresql.md b/docs/trino-connector/catalog-postgresql.md
index 550b7f0d04..32320188b3 100644
--- a/docs/trino-connector/catalog-postgresql.md
+++ b/docs/trino-connector/catalog-postgresql.md
@@ -45,6 +45,14 @@ Gravitino Trino connector supports the following alter table operations:
The Gravitino Trino connector supports most SELECT statements, allowing the execution of queries successfully.
Currently, it doesn't support certain query optimizations, such as indexes and pushdowns.
+## Update
+
+Only `UPDATE` statements with constant assignments and predicates are supported. See also [UPDATE limitation](https://trino.io/docs/current/connector/postgresql.html#update-limitation).
+
+## Delete
+
+If the `WHERE` clause is specified, only the matching rows are deleted. Otherwise, all rows from the table are deleted. See also [DELETE limitation](https://trino.io/docs/current/connector/postgresql.html#delete-limitation).
+
## Table and Schema properties
PostgreSQL's tables and schemas cannot support properties.
@@ -140,6 +148,19 @@ Insert data into the table `table_01` from select:
INSERT INTO postgresql_test.database_01.table_01 (name, salary) SELECT * FROM postgresql_test.database_01.table_01;
```
+Update the table `table_01`:
+
+```sql
+UPDATE postgresql_test.database_01.table_01 SET name = 'ice_update' WHERE salary = 12;
+```
+
+Delete the table `table_01`:
+
+```sql
+DELETE FROM postgresql_test.database_01.table_01 WHERE salary = 12;
+DELETE FROM postgresql_test.database_01.table_01;
+```
+
### Querying data
Query the `table_01` table:
diff --git a/docs/trino-connector/sql-support.md b/docs/trino-connector/sql-support.md
index 6603abf820..d569d7eace 100644
--- a/docs/trino-connector/sql-support.md
+++ b/docs/trino-connector/sql-support.md
@@ -29,6 +29,8 @@ The connector provides read access and write access to data and metadata stored
- [INSERT](https://trino.io/docs/current/sql/insert.html)
- [INSERT INTO SELECT](https://trino.io/docs/current/sql/insert.html)
+- [UPDATE](https://trino.io/docs/current/sql/update.html)
+- [DELETE](https://trino.io/docs/current/sql/delete.html)
### Schema and table management
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.sql b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.sql
new file mode 100644
index 0000000000..a62e447155
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.sql
@@ -0,0 +1,22 @@
+CREATE SCHEMA gt_mysql.gt_db1;
+
+CREATE TABLE gt_mysql.gt_db1.tb01 (
+ name varchar(200),
+ salary int
+);
+
+INSERT INTO gt_mysql.gt_db1.tb01(name, salary) VALUES ('bob', 14), ('tom', 12);
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY name;
+
+UPDATE gt_mysql.gt_db1.tb01 SET name='bob_update' WHERE salary = 14;
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY name;
+
+UPDATE gt_mysql.gt_db1.tb01 SET name='nancy' WHERE salary < 15;
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY salary;
+
+DROP TABLE gt_mysql.gt_db1.tb01;
+
+DROP SCHEMA gt_mysql.gt_db1;
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.txt b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.txt
new file mode 100644
index 0000000000..0c0d5a2f77
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00012_update_table.txt
@@ -0,0 +1,22 @@
+CREATE SCHEMA
+
+CREATE TABLE
+
+INSERT: 2 rows
+
+"bob","14"
+"tom","12"
+
+UPDATE: 1 row
+
+"bob_update","14"
+"tom","12"
+
+UPDATE: 2 rows
+
+"nancy","12"
+"nancy","14"
+
+DROP TABLE
+
+DROP SCHEMA
\ No newline at end of file
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.sql b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.sql
new file mode 100644
index 0000000000..eb576b073d
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.sql
@@ -0,0 +1,26 @@
+CREATE SCHEMA gt_mysql.gt_db1;
+
+CREATE TABLE gt_mysql.gt_db1.tb01 (
+ name varchar(200),
+ salary int
+);
+
+INSERT INTO gt_mysql.gt_db1.tb01(name, salary) VALUES ('bob', 14), ('tom', 12), ('nancy', 15), ('hoho', 16);
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_mysql.gt_db1.tb01 WHERE salary=14;
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_mysql.gt_db1.tb01 WHERE salary<16;
+
+SELECT * FROM gt_mysql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_mysql.gt_db1.tb01;
+
+SELECT count(*) FROM gt_mysql.gt_db1.tb01;
+
+DROP TABLE gt_mysql.gt_db1.tb01;
+
+DROP SCHEMA gt_mysql.gt_db1;
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.txt b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.txt
new file mode 100644
index 0000000000..3ae0cdf73d
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-mysql/00013_delete_table.txt
@@ -0,0 +1,28 @@
+CREATE SCHEMA
+
+CREATE TABLE
+
+INSERT: 4 rows
+
+"bob","14"
+"hoho","16"
+"nancy","15"
+"tom","12"
+
+DELETE: 1 row
+
+"hoho","16"
+"nancy","15"
+"tom","12"
+
+DELETE: 2 rows
+
+"hoho","16"
+
+DELETE: 1 row
+
+"0"
+
+DROP TABLE
+
+DROP SCHEMA
\ No newline at end of file
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.sql b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.sql
new file mode 100644
index 0000000000..3234bcdffc
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.sql
@@ -0,0 +1,22 @@
+CREATE SCHEMA gt_postgresql.gt_db1;
+
+CREATE TABLE gt_postgresql.gt_db1.tb01 (
+ name varchar(200),
+ salary int
+);
+
+INSERT INTO gt_postgresql.gt_db1.tb01(name, salary) VALUES ('bob', 14), ('tom', 12);
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY name;
+
+UPDATE gt_postgresql.gt_db1.tb01 SET name='bob_update' WHERE salary=14;
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY name;
+
+UPDATE gt_postgresql.gt_db1.tb01 SET name='nancy' WHERE salary < 15;
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY salary;
+
+DROP TABLE gt_postgresql.gt_db1.tb01;
+
+DROP SCHEMA gt_postgresql.gt_db1;
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.txt b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.txt
new file mode 100644
index 0000000000..0c0d5a2f77
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00008_update_table.txt
@@ -0,0 +1,22 @@
+CREATE SCHEMA
+
+CREATE TABLE
+
+INSERT: 2 rows
+
+"bob","14"
+"tom","12"
+
+UPDATE: 1 row
+
+"bob_update","14"
+"tom","12"
+
+UPDATE: 2 rows
+
+"nancy","12"
+"nancy","14"
+
+DROP TABLE
+
+DROP SCHEMA
\ No newline at end of file
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.sql b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.sql
new file mode 100644
index 0000000000..6b85e5f5f1
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.sql
@@ -0,0 +1,26 @@
+CREATE SCHEMA gt_postgresql.gt_db1;
+
+CREATE TABLE gt_postgresql.gt_db1.tb01 (
+ name varchar(200),
+ salary int
+);
+
+INSERT INTO gt_postgresql.gt_db1.tb01(name, salary) VALUES ('bob', 14), ('tom', 12), ('nancy', 15), ('hoho', 16);
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_postgresql.gt_db1.tb01 WHERE salary=14;
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_postgresql.gt_db1.tb01 WHERE salary<16;
+
+SELECT * FROM gt_postgresql.gt_db1.tb01 ORDER BY name;
+
+DELETE FROM gt_postgresql.gt_db1.tb01;
+
+SELECT count(*) FROM gt_postgresql.gt_db1.tb01;
+
+DROP TABLE gt_postgresql.gt_db1.tb01;
+
+DROP SCHEMA gt_postgresql.gt_db1;
diff --git a/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.txt b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.txt
new file mode 100644
index 0000000000..3ae0cdf73d
--- /dev/null
+++ b/trino-connector/integration-test/src/test/resources/trino-ci-testset/testsets/jdbc-postgresql/00009_delete_table.txt
@@ -0,0 +1,28 @@
+CREATE SCHEMA
+
+CREATE TABLE
+
+INSERT: 4 rows
+
+"bob","14"
+"hoho","16"
+"nancy","15"
+"tom","12"
+
+DELETE: 1 row
+
+"hoho","16"
+"nancy","15"
+"tom","12"
+
+DELETE: 2 rows
+
+"hoho","16"
+
+DELETE: 1 row
+
+"0"
+
+DROP TABLE
+
+DROP SCHEMA
\ No newline at end of file
diff --git a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java
index 7dbce02e58..69a8319520 100644
--- a/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java
+++ b/trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/GravitinoMetadata.java
@@ -45,11 +45,13 @@ import io.trino.spi.connector.JoinType;
import io.trino.spi.connector.LimitApplicationResult;
import io.trino.spi.connector.ProjectionApplicationResult;
import io.trino.spi.connector.RetryMode;
+import io.trino.spi.connector.RowChangeParadigm;
import io.trino.spi.connector.SaveMode;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.connector.SortItem;
import io.trino.spi.connector.TopNApplicationResult;
import io.trino.spi.expression.ConnectorExpression;
+import io.trino.spi.expression.Constant;
import io.trino.spi.security.TrinoPrincipal;
import io.trino.spi.statistics.ComputedStatistics;
import io.trino.spi.statistics.TableStatistics;
@@ -58,6 +60,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.OptionalLong;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
@@ -73,6 +76,10 @@ import org.apache.gravitino.trino.connector.metadata.GravitinoTable;
* Additionally, it wraps the internal connector metadata for accessing data.
*/
public class GravitinoMetadata implements ConnectorMetadata {
+
+ // The column handle name that will generate row IDs for the merge operation.
+ public static final String MERGE_ROW_ID = "$row_id";
+
// Handling metadata operations on gravitino server
private final CatalogConnectorMetadata catalogConnectorMetadata;
@@ -433,7 +440,10 @@ public class GravitinoMetadata implements ConnectorMetadata {
@Override
public ColumnHandle getMergeRowIdColumnHandle(
ConnectorSession session, ConnectorTableHandle tableHandle) {
- return ConnectorMetadata.super.getMergeRowIdColumnHandle(session, tableHandle);
+ ColumnHandle mergeRowIdColumnHandle =
+ internalMetadata.getMergeRowIdColumnHandle(session, GravitinoHandle.unWrap(tableHandle));
+
+ return new GravitinoColumnHandle(MERGE_ROW_ID, mergeRowIdColumnHandle);
}
@Override
@@ -575,6 +585,56 @@ public class GravitinoMetadata implements ConnectorMetadata {
return internalMetadata.getTableStatistics(session, GravitinoHandle.unWrap(tableHandle));
}
+ @Override
+ public Optional<ConnectorTableHandle> applyUpdate(
+ ConnectorSession session,
+ ConnectorTableHandle tableHandle,
+ Map<ColumnHandle, Constant> assignments) {
+ return internalMetadata
+ .applyUpdate(
+ session,
+ GravitinoHandle.unWrap(tableHandle),
+ assignments.entrySet().stream()
+ .collect(
+ Collectors.toMap(
+ entry -> GravitinoHandle.unWrap(entry.getKey()), Map.Entry::getValue)))
+ .map(
+ result ->
+ new GravitinoTableHandle(
+ getTableName(tableHandle).getSchemaName(),
+ getTableName(tableHandle).getTableName(),
+ result));
+ }
+
+ @Override
+ public RowChangeParadigm getRowChangeParadigm(
+ ConnectorSession session, ConnectorTableHandle tableHandle) {
+ return internalMetadata.getRowChangeParadigm(session, GravitinoHandle.unWrap(tableHandle));
+ }
+
+ @Override
+ public OptionalLong executeUpdate(ConnectorSession session, ConnectorTableHandle tableHandle) {
+ return internalMetadata.executeUpdate(session, GravitinoHandle.unWrap(tableHandle));
+ }
+
+ @Override
+ public Optional<ConnectorTableHandle> applyDelete(
+ ConnectorSession session, ConnectorTableHandle tableHandle) {
+ return internalMetadata
+ .applyDelete(session, GravitinoHandle.unWrap(tableHandle))
+ .map(
+ result ->
+ new GravitinoTableHandle(
+ getTableName(tableHandle).getSchemaName(),
+ getTableName(tableHandle).getTableName(),
+ result));
+ }
+
+ @Override
+ public OptionalLong executeDelete(ConnectorSession session, ConnectorTableHandle tableHandle) {
+ return internalMetadata.executeDelete(session, GravitinoHandle.unWrap(tableHandle));
+ }
+
private SchemaTableName getTableName(ConnectorTableHandle tableHandle) {
return ((GravitinoTableHandle) tableHandle).toSchemaTableName();
}