Skip to content

Commit

Permalink
Trino dialect supports insert syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
renzhimin7 committed Nov 5, 2024
1 parent aeff4a3 commit f3a79d1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String, String> properties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down

0 comments on commit f3a79d1

Please sign in to comment.