Skip to content

Commit

Permalink
[Feature]Trino dialect supports insert into syntax (#52712)
Browse files Browse the repository at this point in the history
Signed-off-by: zhiminr.ren <[email protected]>
  • Loading branch information
renzhimin7 authored Nov 8, 2024
1 parent f73f983 commit 39d46a4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<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, new HashMap<>(0), NodePosition.ZERO);
}

@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
Expand Up @@ -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;

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
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 39d46a4

Please sign in to comment.