Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for WithInsert query #28867

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.ColumnConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.from.TableConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.with.WithConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.SQLStatementConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.select.SelectStatementConverter;

Expand All @@ -47,7 +49,8 @@ public final class InsertStatementConverter implements SQLStatementConverter<Ins

@Override
public SqlNode convert(final InsertStatement insertStatement) {
return convertInsert(insertStatement);
SqlInsert sqlInsert = convertInsert(insertStatement);
return InsertStatementHandler.getWithSegment(insertStatement).flatMap(optional -> WithConverter.convert(optional, sqlInsert)).orElse(sqlInsert);
}

private SqlInsert convertInsert(final InsertStatement insertStatement) {
Expand Down
2 changes: 2 additions & 0 deletions test/it/optimizer/src/test/resources/converter/insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@
<test-cases sql-case-id="insert_with_content_keyword" expected-sql="INSERT INTO &quot;SYS_MQ_MSG&quot; (&quot;ID&quot;, &quot;CONTENT&quot;) VALUES (1, 'test')" db-types="Oracle" sql-case-types="LITERAL" />
<test-cases sql-case-id="insert_with_connect_by_and_prior" expected-sql="INSERT INTO &quot;t&quot; (&quot;c1&quot;, &quot;c2&quot;, &quot;c3&quot;, &quot;c4&quot;, &quot;c5&quot;) SELECT &quot;c1&quot;, &quot;c2&quot;, &quot;regexp_substr&quot;(&quot;c3&quot;, '[^,]+', 1, &quot;l&quot;) &quot;c3&quot;, &quot;c4&quot;, &quot;c5&quot; FROM &quot;t&quot; WHERE &quot;id&quot; = 1" db-types="Oracle" sql-case-types="LITERAL" />
<test-cases sql-case-id="insert_with_national_character_set" expected-sql="INSERT INTO &quot;customers&quot; VALUES (1000, &quot;TO_NCHAR&quot;('John Smith'), '''500 Oracle Parkway', &quot;sysdate&quot;)" db-types="Oracle" sql-case-types="LITERAL" />
<test-cases sql-case-id="insert_with_with_clause" expected-sql="WITH cte (order_id, user_id) AS (SELECT order_id, user_id FROM t_order) INSERT INTO t_order (order_id, user_id) SELECT order_id, user_id FROM cte" db-types="SQLServer" />
<test-cases sql-case-id="insert_without_columns_with_with_clause" expected-sql="WITH cte AS (SELECT order_id, user_id FROM t_order) INSERT INTO t_order (order_id, user_id) SELECT order_id, user_id FROM cte" db-types="SQLServer" />
</sql-node-converter-test-cases>