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 TABLE prefix operator #25921

Merged
merged 3 commits into from
May 30, 2023
Merged
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 @@ -24,12 +24,16 @@
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.SQLSegmentConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.from.TableConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.select.SelectStatementConverter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Subquery table converter.
Expand All @@ -42,7 +46,17 @@ public Optional<SqlNode> convert(final SubqueryTableSegment segment) {
return Optional.empty();
}
Collection<SqlNode> sqlNodes = new LinkedList<>();
sqlNodes.add(new SelectStatementConverter().convert(segment.getSubquery().getSelect()));
if (null == segment.getSubquery().getSelect().getProjections()) {
List<Optional<SqlNode>> operandList = new LinkedList<>();
operandList.add(new TableConverter().convert(segment.getSubquery().getSelect().getFrom()));
List<SqlNode> result = operandList.stream()
.flatMap(optional -> optional.map(Stream::of).orElseGet(Stream::empty))
.collect(Collectors.toList());
sqlNodes.add(new SqlBasicCall(SqlStdOperatorTable.EXPLICIT_TABLE,
result, SqlParserPos.ZERO));
} else {
sqlNodes.add(new SelectStatementConverter().convert(segment.getSubquery().getSelect()));
}
segment.getAlias().ifPresent(optional -> sqlNodes.add(new SqlIdentifier(optional, SqlParserPos.ZERO)));
return Optional.of(new SqlBasicCall(SqlStdOperatorTable.AS, new ArrayList<>(sqlNodes), SqlParserPos.ZERO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,12 @@ public ASTNode visitQuerySpecification(final QuerySpecificationContext ctx) {
@Override
public ASTNode visitTableStatement(final TableStatementContext ctx) {
MySQLSelectStatement result = new MySQLSelectStatement();
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
if (null != ctx.TABLE()) {
result.setFrom(new SimpleTableSegment(new TableNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(),
new IdentifierValue(ctx.tableName().getText()))));
} else {
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void assertConvert(final String sqlCaseId, final SQLCaseType sqlCaseType, final
return;
}
String sql = SQL_CASES.getSQL(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES.get(sqlCaseId).getParameters());
String actual = SQLNodeConverterEngine.convert(parseSQLStatement(databaseType, sql)).toSqlString(SQLDialectFactory.getSQLDialect(databaseType)).getSql().replace("\n", " ");
String actual = SQLNodeConverterEngine.convert(parseSQLStatement(databaseType, sql)).toSqlString(SQLDialectFactory.getSQLDialect(databaseType)).getSql().replace("\n", " ").replace("\r", "");
assertThat(actual, is(expected));
}

Expand Down
21 changes: 21 additions & 0 deletions test/it/optimizer/src/test/resources/converter/select-table.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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
~
~ http://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.
-->

<sql-node-converter-test-cases>
<test-cases sql-case-id="select_from_with_table" expected-sql="SELECT * FROM (TABLE `t0`) AS `dt`" db-types="MySQL" />
</sql-node-converter-test-cases>
4 changes: 3 additions & 1 deletion test/it/parser/src/main/resources/case/dal/explain.xml
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,16 @@

<describe sql-case-id="explain_table">
<select>
<simple-table name="t_order" start-index="14" stop-index="20" />
<order-by>
<column-item name="order_id" start-index="31" stop-index="38" />
</order-by>
<limit start-index="40" stop-index="55">
<offset value="2" literal-start-index="55" literal-stop-index="55" />
<row-count value="1" start-index="46" stop-index="46" />
</limit>
<from>
<simple-table name="t_order" start-index="8" stop-index="20" />
</from>
</select>
</describe>

Expand Down
20 changes: 15 additions & 5 deletions test/it/parser/src/main/resources/case/dml/table.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

<sql-parser-test-cases>
<select sql-case-id="table_with_order_by_limit_offset">
<simple-table name="t_order" start-index="6" stop-index="12" />
<from>
<simple-table name="t_order" start-index="0" stop-index="12" />
</from>
<order-by>
<column-item name="order_id" start-index="23" stop-index="30" />
</order-by>
Expand All @@ -29,13 +31,19 @@
</select>

<select sql-case-id="table_union">
<simple-table name="T1" start-index="6" stop-index="7" />
<from>
<simple-table name="T1" start-index="0" stop-index="7" />
</from>
<combine combine-type="UNION" start-index="9" stop-index="22">
<left>
<simple-table name="T1" start-index="6" stop-index="7" />
<from>
<simple-table name="T1" start-index="0" stop-index="7" />
</from>
</left>
<right>
<simple-table name="T2" start-index="21" stop-index="22" />
<from>
<simple-table name="T2" start-index="15" stop-index="22" />
</from>
</right>
</combine>
</select>
Expand All @@ -48,7 +56,9 @@
<subquery-table alias="dt">
<subquery>
<select>
<simple-table name="t0" start-index="21" stop-index="22" />
<from>
<simple-table name="t0" start-index="15" stop-index="22" />
</from>
</select>
</subquery>
</subquery-table>
Expand Down