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 3bb6e8e3a7e35a..1c0a914d402f2d 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 @@ -70,6 +70,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; @@ -133,6 +134,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; @@ -193,6 +195,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; @@ -1222,6 +1225,16 @@ 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); + } + + @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 00000000000000..7b154f73d0ad50 --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoInsertTest.java @@ -0,0 +1,47 @@ +// 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 testCtasTrinoDialect() 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"); + } + } + +} + 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 e9642b7387ac35..62a7f4f09aac41 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" +