未验证 提交 f2575bd3 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

support sql with sub query projection (#4845)

上级 fa3b9e10
......@@ -17,10 +17,7 @@
-->
<sql-cases>
<!-- TODO Can not support subquery at current-->
<!-- <sql-case id="select_sub_query_with_multiple_tables" value="SELECT t.* FROM (SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id and o.order_id IN (?, ?)) t ORDER BY t.item_id" db-types="SQLServer,Oracle" />-->
<!-- <sql-case id="select_sub_query_with_order_by" value="SELECT COUNT(1) as orders_count FROM (SELECT * FROM t_order ORDER BY order_id desc) t" db-types="H2,MySQL,PostgreSQL,Oracle" />-->
<!-- <sql-case id="select_sub_query_with_group_by" value="SELECT COUNT(1) as order_items_count FROM (SELECT order_id FROM t_order_item GROUP BY order_id) t" db-types="H2,MySQL,PostgreSQL,Oracle" />-->
<sql-case id="select_sub_query_with_project" value="SELECT order_id, (select 1) as num FROM t_order" />
<sql-case id="select_sub_query_with_table" value="SELECT t.* FROM (SELECT * FROM t_order WHERE order_id IN (?, ?)) t" />
<sql-case id="select_with_equal_subquery" value="SELECT * FROM t_order WHERE user_id = (SELECT user_id FROM t_order_item WHERE id = 10)" db-types="MySQL" />
<sql-case id="select_with_in_subquery" value="SELECT * FROM t_order WHERE user_id in (SELECT user_id FROM t_order_item WHERE id in (10, 11))" db-types="MySQL" />
......
......@@ -72,6 +72,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.OnDuplicateKe
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
......@@ -444,8 +445,8 @@ public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
result.setAlias(alias);
return result;
}
if (projection instanceof SubquerySegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment((SubquerySegment) projection);
if (projection instanceof SubqueryExpressionSegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment(((SubqueryExpressionSegment) projection).getSubquery());
result.setAlias(alias);
return result;
}
......
......@@ -59,6 +59,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.InsertColumns
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
......@@ -352,8 +353,8 @@ public final class OracleDMLVisitor extends OracleVisitor implements DMLVisitor
result.setAlias(alias);
return result;
}
if (projection instanceof SubquerySegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment((SubquerySegment) projection);
if (projection instanceof SubqueryExpressionSegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment(((SubqueryExpressionSegment) projection).getSubquery());
result.setAlias(alias);
return result;
}
......
......@@ -62,6 +62,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.InsertColumns
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
......@@ -361,8 +362,8 @@ public final class PostgreSQLDMLVisitor extends PostgreSQLVisitor implements DML
result.setAlias(alias);
return result;
}
if (projection instanceof SubquerySegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment((SubquerySegment) projection);
if (projection instanceof SubqueryExpressionSegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment(((SubqueryExpressionSegment) projection).getSubquery());
result.setAlias(alias);
return result;
}
......
......@@ -57,6 +57,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.InsertColumns
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
......@@ -324,8 +325,8 @@ public final class SQL92DMLVisitor extends SQL92Visitor implements DMLVisitor {
result.setAlias(alias);
return result;
}
if (projection instanceof SubquerySegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment((SubquerySegment) projection);
if (projection instanceof SubqueryExpressionSegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment(((SubqueryExpressionSegment) projection).getSubquery());
result.setAlias(alias);
return result;
}
......
......@@ -58,6 +58,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.InsertColumns
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.expr.subquery.SubquerySegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.AggregationProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
......@@ -347,8 +348,8 @@ public final class SQLServerDMLVisitor extends SQLServerVisitor implements DMLVi
result.setAlias(alias);
return result;
}
if (projection instanceof SubquerySegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment((SubquerySegment) projection);
if (projection instanceof SubqueryExpressionSegment) {
SubqueryProjectionSegment result = new SubqueryProjectionSegment(((SubqueryExpressionSegment) projection).getSubquery());
result.setAlias(alias);
return result;
}
......
......@@ -26,6 +26,7 @@ import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.p
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.column.ExpectedColumnProjection;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.expression.ExpectedExpressionProjection;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.shorthand.ExpectedShorthandProjection;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.subquery.ExpectedSubqueryProjection;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.top.ExpectedTopProjection;
import javax.xml.bind.annotation.XmlAttribute;
......@@ -60,13 +61,17 @@ public final class ExpectedProjections extends AbstractExpectedSQLSegment {
@XmlElement(name = "top-projection")
private final Collection<ExpectedTopProjection> topProjections = new LinkedList<>();
@XmlElement(name = "subquery-projection")
private final Collection<ExpectedSubqueryProjection> subqueryProjections = new LinkedList<>();
/**
* Get size.
*
* @return size
*/
public int getSize() {
return shorthandProjections.size() + columnProjections.size() + aggregationProjections.size() + aggregationDistinctProjections.size() + expressionProjections.size() + topProjections.size();
return shorthandProjections.size() + columnProjections.size() + aggregationProjections.size() + aggregationDistinctProjections.size()
+ expressionProjections.size() + topProjections.size() + subqueryProjections.size();
}
/**
......@@ -82,6 +87,7 @@ public final class ExpectedProjections extends AbstractExpectedSQLSegment {
result.addAll(aggregationDistinctProjections);
result.addAll(expressionProjections);
result.addAll(topProjections);
result.addAll(subqueryProjections);
result.sort(Comparator.comparingInt(ExpectedSQLSegment::getStartIndex));
return result;
}
......
/*
* 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.
*/
package org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.impl.subquery;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.AbstractExpectedDelimiterSQLSegment;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.expr.simple.ExpectedSubquery;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.segment.impl.projection.ExpectedProjection;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
/**
* Expected subquery projection.
*/
@Getter
@Setter
public final class ExpectedSubqueryProjection extends AbstractExpectedDelimiterSQLSegment implements ExpectedProjection {
@XmlAttribute
private String alias;
@XmlElement
private ExpectedSubquery subquery;
}
......@@ -17,35 +17,23 @@
-->
<sql-parser-test-cases>
<!-- TODO cannot pass,add later
<select sql-case-id="select_sub_query_with_multiple_tables" parameters="1, 2">
<simple-table name="t_order" alias="o" start-index="33" stop-index="39" />
<simple-table name="t_order_item" alias="i" start-index="44" stop-index="55" />
<conditions>
<condition column-name="order_id" table-name="t_order" operator="IN">
<value index="0" literal="1" type="int" />
<value index="1" literal="2" type="int" />
</condition>
</conditions>
</select>
-->
<!-- TODO Can not support subquery at current
<select sql-case-id="select_sub_query_with_order_by">
<simple-table name="t_order" start-index="52" stop-index="58" />
<projections start-index="45" stop-index="45">
<aggregation-projection type="COUNT" alias="orders_count" />
<shorthand-projection start-index="45" stop-index="45" />
</projections>
</select>
<select sql-case-id="select_sub_query_with_group_by">
<simple-table name="t_order_item" start-index="64" stop-index="75" />
<projections start-index="50" stop-index="57">
<column-projection name="order_id" start-index="50" stop-index="57" />
<select sql-case-id="select_sub_query_with_project">
<tables>
<simple-table name="t_order" start-index="40" stop-index="46" />
</tables>
<projections start-index="7" stop-index="33">
<column-projection name="order_id" start-index="7" stop-index="14" />
<subquery-projection start-index="17" stop-index="26" alias="num">
<subquery>
<select>
<projections start-index="25" stop-index="25">
<expression-projection start-index="25" stop-index="25" />
</projections>
</select>
</subquery>
</subquery-projection>
</projections>
</select>
-->
<select sql-case-id="select_sub_query_with_table" parameters="3, 4">
<tables>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册