From 5a7455721f8b0999478906a4bcbbd9b917c1b852 Mon Sep 17 00:00:00 2001 From: "zhiminr.ren" <1240388654@qq.com> Date: Thu, 7 Nov 2024 22:57:07 +0800 Subject: [PATCH 1/3] Trino dialect supports insert into syntax Signed-off-by: zhiminr.ren <1240388654@qq.com> --- .../connector/parser/trino/AstBuilder.java | 13 ++++++ .../parser/trino/TrinoInsertTest.java | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoInsertTest.java diff --git a/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java b/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java index 4b57db8e3a865..83e413e2f62d0 100644 --- a/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java +++ b/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java @@ -71,6 +71,7 @@ import com.starrocks.sql.ast.CreateTableAsSelectStmt; import com.starrocks.sql.ast.CreateTableStmt; import com.starrocks.sql.ast.ExceptRelation; +import com.starrocks.sql.ast.InsertStmt; import com.starrocks.sql.ast.IntersectRelation; import com.starrocks.sql.ast.JoinRelation; import com.starrocks.sql.ast.LambdaArgument; @@ -92,6 +93,7 @@ import com.starrocks.sql.ast.UnitIdentifier; import com.starrocks.sql.ast.ValueList; import com.starrocks.sql.ast.ValuesRelation; +import com.starrocks.sql.parser.NodePosition; import com.starrocks.sql.parser.ParsingException; import io.trino.sql.tree.AliasedRelation; import io.trino.sql.tree.AllColumns; @@ -134,6 +136,7 @@ import io.trino.sql.tree.IfExpression; import io.trino.sql.tree.InListExpression; import io.trino.sql.tree.InPredicate; +import io.trino.sql.tree.Insert; import io.trino.sql.tree.Intersect; import io.trino.sql.tree.IntervalLiteral; import io.trino.sql.tree.IsNotNullPredicate; @@ -195,6 +198,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.UUID; import java.util.stream.Collectors; import static com.starrocks.analysis.AnalyticWindow.BoundaryType.CURRENT_ROW; @@ -1244,6 +1248,15 @@ protected ParseNode visitCast(Cast node, ParseTreeContext context) { return new CastExpr(new TypeDef(getType(node.getType())), (Expr) visit(node.getExpression(), context)); } + @Override + protected ParseNode visitInsert(Insert node, ParseTreeContext context) { + List parts = node.getTarget().getParts(); + String tableName = parts.get(parts.size() - 1); + return new InsertStmt(qualifiedNameToTableName(convertQualifiedName(node.getTarget())), null, + tableName.concat(UUID.randomUUID().toString()), null, + (QueryStatement) visit(node.getQuery(), context), true, new HashMap<>(0), NodePosition.ZERO); + } + @Override protected ParseNode visitCreateTableAsSelect(CreateTableAsSelect node, ParseTreeContext context) { Map properties = new HashMap<>(); diff --git a/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoInsertTest.java b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoInsertTest.java new file mode 100644 index 0000000000000..4dc92ed9d48de --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoInsertTest.java @@ -0,0 +1,45 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 com.starrocks.connector.parser.trino; + +import com.starrocks.sql.ast.InsertStmt; +import com.starrocks.sql.ast.QueryStatement; +import com.starrocks.sql.parser.SqlParser; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TrinoInsertTest extends TrinoTestBase { + @BeforeClass + public static void beforeClass() throws Exception { + TrinoTestBase.beforeClass(); + } + + @Test + public void testInsertTrinoDialect() throws Exception { + String insertSql = "insert into t3 select doy(date '2022-03-06')"; + try { + connectContext.getSessionVariable().setSqlDialect("trino"); + InsertStmt ctasStmt = + (InsertStmt) SqlParser.parse(insertSql, connectContext.getSessionVariable()).get(0); + QueryStatement queryStmt = ctasStmt.getQueryStatement(); + assertPlanContains(queryStmt, "dayofyear('2022-03-06 00:00:00')"); + + connectContext.getSessionVariable().setSqlDialect("starrocks"); + analyzeFail(insertSql, "No matching function with signature: doy(date)"); + } finally { + connectContext.getSessionVariable().setSqlDialect("trino"); + } + } +} \ No newline at end of file From eda95a22bec571668cee8e052cff3552629b5c74 Mon Sep 17 00:00:00 2001 From: "zhiminr.ren" <1240388654@qq.com> Date: Fri, 8 Nov 2024 10:16:07 +0800 Subject: [PATCH 2/3] Trino dialect supports insert into syntax Signed-off-by: zhiminr.ren <1240388654@qq.com> --- .../com/starrocks/connector/parser/trino/TrinoParserUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/TrinoParserUtils.java b/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/TrinoParserUtils.java index 6ae3baee23ab4..21f17f260ab71 100644 --- a/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/TrinoParserUtils.java +++ b/fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/TrinoParserUtils.java @@ -26,6 +26,7 @@ import io.trino.sql.tree.CreateTableAsSelect; import io.trino.sql.tree.Explain; import io.trino.sql.tree.ExplainAnalyze; +import io.trino.sql.tree.Insert; import io.trino.sql.tree.Query; import io.trino.sql.tree.Statement; @@ -40,7 +41,7 @@ public static StatementBase toStatement(String query, long sqlMode) { String trimmedQuery = query.trim(); Statement statement = TrinoParser.parse(trimmedQuery); if (statement instanceof Query || statement instanceof Explain || statement instanceof ExplainAnalyze - || statement instanceof CreateTableAsSelect) { + || statement instanceof CreateTableAsSelect || statement instanceof Insert) { return (StatementBase) statement.accept(new AstBuilder(sqlMode), new ParseTreeContext()); } else { throw trinoParserUnsupportedException("Unsupported statement type: " + statement.getClass().getName()); From 8a39c7bc612cf304dddcdc92647be83d48cb9354 Mon Sep 17 00:00:00 2001 From: "zhiminr.ren" <1240388654@qq.com> Date: Fri, 8 Nov 2024 11:06:56 +0800 Subject: [PATCH 3/3] Trino dialect supports insert into syntax Signed-off-by: zhiminr.ren <1240388654@qq.com> --- .../com/starrocks/connector/parser/trino/TrinoTestBase.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoTestBase.java b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoTestBase.java index e9642b7387ac3..62a7f4f09aac4 100644 --- a/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoTestBase.java +++ b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoTestBase.java @@ -94,6 +94,10 @@ public static void beforeClass() throws Exception { "\"in_memory\" = \"false\"\n" + ");"); + starRocksAssert.withTable("CREATE TABLE `t3` (\n" + + "`day` int NULL COMMENT \"\") \n" + + "PROPERTIES ('replication_num' = '1')"); + starRocksAssert.withTable("CREATE TABLE `tall` (\n" + " `ta` varchar(20) NULL COMMENT \"\",\n" + " `tb` smallint(6) NULL COMMENT \"\",\n" +