Skip to content

Commit

Permalink
Fix Object 'DUAL' not found exception when execute select 1 from dual…
Browse files Browse the repository at this point in the history
… with sql federation
  • Loading branch information
strongduanmu committed Oct 19, 2023
1 parent 2d549ac commit 8cd32ac
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.ExplainStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
Expand Down Expand Up @@ -57,8 +56,6 @@ public static SqlNode convert(final SQLStatement sqlStatement) {
Optional<SqlNode> result = Optional.empty();
if (sqlStatement instanceof DMLStatement) {
result = convert((DMLStatement) sqlStatement);
} else if (sqlStatement instanceof DDLStatement) {
result = convert((DDLStatement) sqlStatement);
} else if (sqlStatement instanceof DALStatement) {
result = convert((DALStatement) sqlStatement);
}
Expand All @@ -72,10 +69,6 @@ private static Optional<SqlNode> convert(final DMLStatement sqlStatement) {
if (sqlStatement instanceof DeleteStatement) {
return Optional.of(new DeleteStatementConverter().convert((DeleteStatement) sqlStatement));
}
return Optional.empty();
}

private static Optional<SqlNode> convert(final DDLStatement sqlStatement) {
if (sqlStatement instanceof UpdateStatement) {
return Optional.of(new UpdateStatementConverter().convert((UpdateStatement) sqlStatement));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public final class SimpleTableConverter {
* @return sql node
*/
public static Optional<SqlNode> convert(final SimpleTableSegment segment) {
if ("DUAL".equalsIgnoreCase(segment.getTableName().getIdentifier().getValue())) {
return Optional.empty();
}
TableNameSegment tableName = segment.getTableName();
List<String> names = new ArrayList<>();
if (segment.getOwner().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.ParameterMarkerExpressionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.AggregationProjectionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.ColumnProjectionConverter;
import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.projection.impl.ExpressionProjectionConverter;
Expand Down Expand Up @@ -75,6 +77,9 @@ private static Optional<SqlNode> getProjectionSQLNode(final ProjectionSegment se
if (segment instanceof AggregationProjectionSegment) {
return AggregationProjectionConverter.convert((AggregationProjectionSegment) segment);
}
if (segment instanceof ParameterMarkerExpressionSegment) {
return ParameterMarkerExpressionConverter.convert((ParameterMarkerExpressionSegment) segment);
}
// TODO process other projection
return Optional.empty();
}
Expand Down
4 changes: 3 additions & 1 deletion test/it/optimizer/src/test/resources/converter/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-->

<sql-node-converter-test-cases>
<test-cases sql-case-id="select_string_constant_type_cast" expected-sql="SELECT CAST('1' AS INTEGER), CAST('2' AS DECIMAL)" dbtypes="openGauss,PostgreSQL" />
<test-cases sql-case-id="select_string_constant_type_cast" expected-sql="SELECT CAST('1' AS INTEGER), CAST('2' AS DECIMAL)" db-types="openGauss,PostgreSQL" />
<test-cases sql-case-id="select_with_database_name_and_schema_name_in_table" expected-sql="SELECT &quot;order_id&quot; FROM &quot;sharding_db&quot;.&quot;public&quot;.&quot;t_order&quot; WHERE &quot;user_id&quot; = ? AND &quot;order_id&quot; = ?" db-types="PostgreSQL,openGauss" sql-case-types="PLACEHOLDER" />
<test-cases sql-case-id="select_with_database_name_and_schema_name_in_table" expected-sql="SELECT &quot;order_id&quot; FROM &quot;sharding_db&quot;.&quot;public&quot;.&quot;t_order&quot; WHERE &quot;user_id&quot; = 1 AND &quot;order_id&quot; = 1" db-types="PostgreSQL,openGauss" sql-case-types="LITERAL" />
<test-cases sql-case-id="select_with_spatial_function" expected-sql="SELECT * FROM `t_order` WHERE `ST_DISTANCE_SPHERE`(`POINT`(113.358772, 23.1273723), `POINT`(`user_id`, `order_id`)) &lt;&gt; 0" db-types="MySQL" />
Expand All @@ -33,4 +33,6 @@
<test-cases sql-case-id="select_with_assignment_operator" expected-sql="SELECT `rn` := 1, `now_code` := '' FROM `t_order`" db-types="MySQL" />
<test-cases sql-case-id="select_with_assignment_operator_and_keyword" expected-sql="SELECT `KEY` := '', `num` := 123 FROM `t_order`" db-types="MySQL" />
<test-cases sql-case-id="select_with_json_value_return_type" expected-sql="SELECT * FROM `t_order` WHERE JSON_VALUE(`items`, '''$.name''' 'RETURNING' VARCHAR(100)) = 'jack'" db-types="MySQL" />
<test-cases sql-case-id="select_projection_with_parameter" expected-sql="SELECT 1 &quot;id&quot;, ?, &quot;SYSDATE&quot; &quot;create_time&quot;, &quot;TRUNC&quot;(&quot;SYSDATE&quot;) &quot;create_date&quot;" db-types="Oracle" sql-case-types="PLACEHOLDER" />
<test-cases sql-case-id="select_projection_with_parameter" expected-sql="SELECT 1 &quot;id&quot;, 'OK' &quot;status&quot;, &quot;SYSDATE&quot; &quot;create_time&quot;, &quot;TRUNC&quot;(&quot;SYSDATE&quot;) &quot;create_date&quot;" db-types="Oracle" sql-case-types="LITERAL" />
</sql-node-converter-test-cases>

0 comments on commit 8cd32ac

Please sign in to comment.