Revert "[behavior change](planner)change Doris's query organization syntax to standard sql (#9745)" (#12135)
Reverts apache/doris#9745 This may cause NPE when calling `parseDefineExprWithoutAnalyze()`
This commit is contained in:
@ -1455,9 +1455,9 @@ create_stmt ::=
|
||||
{:
|
||||
RESULT = new CreateFileStmt(fileName, db, properties);
|
||||
:}
|
||||
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName KW_AS query_stmt:queryStmt opt_properties:properties
|
||||
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName KW_AS select_stmt:selectStmt opt_properties:properties
|
||||
{:
|
||||
RESULT = new CreateMaterializedViewStmt(mvName, queryStmt, properties);
|
||||
RESULT = new CreateMaterializedViewStmt(mvName, selectStmt, properties);
|
||||
:}
|
||||
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName build_mv:buildMethod
|
||||
opt_mv_refersh_info:refreshInfo
|
||||
@ -2903,13 +2903,13 @@ show_param ::=
|
||||
{:
|
||||
SelectList list = new SelectList();
|
||||
list.addItem(new SelectListItem(new IntLiteral((long)0), null));
|
||||
RESULT = new SelectStmt(list, null, null, null, null);
|
||||
RESULT = new SelectStmt(list, null, null, null, null, null, null);
|
||||
:}
|
||||
| KW_COUNT LPAREN STAR RPAREN KW_ERRORS
|
||||
{:
|
||||
SelectList list = new SelectList();
|
||||
list.addItem(new SelectListItem(new IntLiteral((long)0), null));
|
||||
RESULT = new SelectStmt(list, null, null, null, null);
|
||||
RESULT = new SelectStmt(list, null, null, null, null, null, null);
|
||||
:}
|
||||
| KW_WARNINGS limit_clause
|
||||
{:
|
||||
@ -3436,113 +3436,86 @@ with_view_def_list ::=
|
||||
:}
|
||||
;
|
||||
|
||||
// We must have a non-empty order by or limit for them to bind to the union.
|
||||
// We cannot reuse the existing order_by_clause or
|
||||
// limit_clause because they would introduce conflicts with EOF,
|
||||
// which, unfortunately, cannot be accessed in the parser as a nonterminal
|
||||
// making this issue unresolvable.
|
||||
// We rely on the left precedence of KW_ORDER, KW_BY, and KW_LIMIT,
|
||||
// to resolve the ambiguity with select_stmt in favor of select_stmt
|
||||
// (i.e., ORDER BY and LIMIT bind to the select_stmt by default, and not the set operation).
|
||||
// There must be at least two set operands for ORDER BY or LIMIT to bind to a set operation,
|
||||
// and we manually throw a parse error if we reach this production
|
||||
// with only a single operand.
|
||||
set_operation_with_order_by_or_limit ::=
|
||||
set_operand_list:operands
|
||||
KW_LIMIT INTEGER_LITERAL:limit
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, null, limitElement);
|
||||
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, null, new LimitElement(limit.longValue()));
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_LIMIT INTEGER_LITERAL:offset COMMA INTEGER_LITERAL:limit
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(offset.longValue(), limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, null, limitElement);
|
||||
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, null, new LimitElement(offset.longValue(), limit.longValue()));
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_LIMIT INTEGER_LITERAL:limit KW_OFFSET INTEGER_LITERAL:offset
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(offset.longValue(), limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, null, limitElement);
|
||||
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, null, new LimitElement(offset.longValue(), limit.longValue()));
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_ORDER KW_BY order_by_elements:orderByClause
|
||||
{:
|
||||
LimitElement limitElement = LimitElement.NO_LIMIT;
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
queryStmt.setOrderByElements(orderByClause);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
|
||||
parser.parseError("order", SqlParserSymbols.KW_ORDER);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, LimitElement.NO_LIMIT);
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_ORDER KW_BY order_by_elements:orderByClause
|
||||
KW_LIMIT INTEGER_LITERAL:limit
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
queryStmt.setOrderByElements(orderByClause);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
|
||||
parser.parseError("order", SqlParserSymbols.KW_ORDER);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, new LimitElement(limit.longValue()));
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_ORDER KW_BY order_by_elements:orderByClause
|
||||
KW_LIMIT INTEGER_LITERAL:offset COMMA INTEGER_LITERAL:limit
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(offset.longValue(), limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
queryStmt.setOrderByElements(orderByClause);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
|
||||
parser.parseError("order", SqlParserSymbols.KW_ORDER);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, new LimitElement(offset.longValue(), limit.longValue()));
|
||||
:}
|
||||
|
|
||||
set_operand_list:operands
|
||||
KW_ORDER KW_BY order_by_elements:orderByClause
|
||||
KW_LIMIT INTEGER_LITERAL:limit KW_OFFSET INTEGER_LITERAL:offset
|
||||
{:
|
||||
LimitElement limitElement = new LimitElement(offset.longValue(), limit.longValue());
|
||||
if (operands.size() == 1) {
|
||||
SetOperand setOperand = operands.get(0);
|
||||
QueryStmt queryStmt = setOperand.getQueryStmt();
|
||||
queryStmt.setLimitElement(limitElement);
|
||||
queryStmt.setOrderByElements(orderByClause);
|
||||
RESULT = queryStmt;
|
||||
} else {
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
|
||||
parser.parseError("order", SqlParserSymbols.KW_ORDER);
|
||||
}
|
||||
RESULT = new SetOperationStmt(operands, orderByClause, new LimitElement(offset.longValue(), limit.longValue()));
|
||||
:}
|
||||
;
|
||||
|
||||
|
||||
set_operand ::=
|
||||
select_stmt:select
|
||||
{:
|
||||
@ -4061,19 +4034,23 @@ set_expr_or_default ::=
|
||||
|
||||
select_stmt ::=
|
||||
select_clause:selectList
|
||||
{: RESULT = new SelectStmt(selectList, null, null, null, null); :}
|
||||
limit_clause:limitClause
|
||||
{: RESULT = new SelectStmt(selectList, null, null, null, null, null, limitClause); :}
|
||||
| select_clause:selectList
|
||||
from_clause:fromClause
|
||||
where_clause:wherePredicate
|
||||
group_by_clause:groupByClause
|
||||
having_clause:havingPredicate
|
||||
order_by_clause:orderByClause
|
||||
limit_clause:limitClause
|
||||
{:
|
||||
RESULT = new SelectStmt(selectList, fromClause, wherePredicate,
|
||||
groupByClause, havingPredicate);
|
||||
groupByClause, havingPredicate, orderByClause,
|
||||
limitClause);
|
||||
:}
|
||||
| value_clause:valueClause
|
||||
| value_clause:valueClause order_by_clause:orderByClause limit_clause:limitClause
|
||||
{:
|
||||
RESULT = new SelectStmt(valueClause);
|
||||
RESULT = new SelectStmt(valueClause, orderByClause, limitClause);
|
||||
:}
|
||||
;
|
||||
|
||||
|
||||
@ -75,7 +75,6 @@ public class CreateMaterializedViewStmt extends DdlStmt {
|
||||
}
|
||||
|
||||
private String mvName;
|
||||
private QueryStmt queryStmt;
|
||||
private SelectStmt selectStmt;
|
||||
private Map<String, String> properties;
|
||||
|
||||
@ -96,16 +95,9 @@ public class CreateMaterializedViewStmt extends DdlStmt {
|
||||
// only in Rollup or MaterializedIndexMeta is true
|
||||
private boolean isReplay = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param mvName materialized view name
|
||||
* @param queryStmt query stmt for construct materialized view. Must be SelectStmt
|
||||
* @param properties properties for materialized view
|
||||
*/
|
||||
public CreateMaterializedViewStmt(String mvName, QueryStmt queryStmt, Map<String, String> properties) {
|
||||
public CreateMaterializedViewStmt(String mvName, SelectStmt selectStmt, Map<String, String> properties) {
|
||||
this.mvName = mvName;
|
||||
this.queryStmt = queryStmt;
|
||||
this.selectStmt = selectStmt;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@ -117,6 +109,10 @@ public class CreateMaterializedViewStmt extends DdlStmt {
|
||||
return mvName;
|
||||
}
|
||||
|
||||
public SelectStmt getSelectStmt() {
|
||||
return selectStmt;
|
||||
}
|
||||
|
||||
public List<MVColumnItem> getMVColumnItemList() {
|
||||
return mvColumnItemList;
|
||||
}
|
||||
@ -141,11 +137,6 @@ public class CreateMaterializedViewStmt extends DdlStmt {
|
||||
public void analyze(Analyzer analyzer) throws UserException {
|
||||
super.analyze(analyzer);
|
||||
FeNameFormat.checkTableName(mvName);
|
||||
if (!(queryStmt instanceof SelectStmt)) {
|
||||
throw new AnalysisException("Set operation is not supported in add materialized view clause, statement.");
|
||||
}
|
||||
selectStmt = (SelectStmt) queryStmt;
|
||||
|
||||
// TODO(ml): The mv name in from clause should pass the analyze without error.
|
||||
selectStmt.forbiddenMVRewrite();
|
||||
selectStmt.analyze(analyzer);
|
||||
|
||||
@ -568,10 +568,6 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
|
||||
|
||||
abstract List<TupleId> collectTupleIds();
|
||||
|
||||
public void setOrderByElements(ArrayList<OrderByElement> orderByElements) {
|
||||
this.orderByElements = orderByElements;
|
||||
}
|
||||
|
||||
public ArrayList<OrderByElement> getOrderByElements() {
|
||||
return orderByElements;
|
||||
}
|
||||
@ -618,10 +614,6 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
|
||||
limitElement = new LimitElement(newLimit);
|
||||
}
|
||||
|
||||
public void setLimitElement(LimitElement limitElement) {
|
||||
this.limitElement = limitElement;
|
||||
}
|
||||
|
||||
public void removeLimitElement() {
|
||||
limitElement = LimitElement.NO_LIMIT;
|
||||
}
|
||||
|
||||
@ -118,26 +118,23 @@ public class SelectStmt extends QueryStmt {
|
||||
// Members that need to be reset to origin
|
||||
private SelectList originSelectList;
|
||||
|
||||
/**
|
||||
* Constructor for SelectStmt. This is used for the following cases.
|
||||
* 1. SELECT * FROM VALUES(a1, b1, c1), (a2, b2, c2);
|
||||
*
|
||||
* @param valueList value list
|
||||
*/
|
||||
public SelectStmt(ValueList valueList) {
|
||||
super(null, LimitElement.NO_LIMIT);
|
||||
public SelectStmt(ValueList valueList, ArrayList<OrderByElement> orderByElement, LimitElement limitElement) {
|
||||
super(orderByElement, limitElement);
|
||||
this.valueList = valueList;
|
||||
this.selectList = new SelectList();
|
||||
this.fromClause = new FromClause();
|
||||
this.colLabels = Lists.newArrayList();
|
||||
}
|
||||
|
||||
SelectStmt(SelectList selectList,
|
||||
SelectStmt(
|
||||
SelectList selectList,
|
||||
FromClause fromClause,
|
||||
Expr wherePredicate,
|
||||
GroupByClause groupByClause,
|
||||
Expr havingPredicate) {
|
||||
super(null, LimitElement.NO_LIMIT);
|
||||
Expr havingPredicate,
|
||||
ArrayList<OrderByElement> orderByElements,
|
||||
LimitElement limitElement) {
|
||||
super(orderByElements, limitElement);
|
||||
this.selectList = selectList;
|
||||
this.originSelectList = selectList.clone();
|
||||
if (fromClause == null) {
|
||||
|
||||
@ -681,12 +681,18 @@ public class SetOperationStmt extends QueryStmt {
|
||||
strBuilder.append(" ");
|
||||
}
|
||||
Preconditions.checkState(operands.size() > 0);
|
||||
strBuilder.append("(").append(operands.get(0).getQueryStmt().toSql()).append(")");
|
||||
strBuilder.append(operands.get(0).getQueryStmt().toSql());
|
||||
for (int i = 1; i < operands.size() - 1; ++i) {
|
||||
strBuilder.append(" "
|
||||
+ operands.get(i).getOperation().toString() + " "
|
||||
+ ((operands.get(i).getQualifier() == Qualifier.ALL) ? "ALL " : ""));
|
||||
strBuilder.append("(").append(operands.get(i).getQueryStmt().toSql()).append(")");
|
||||
if (operands.get(i).getQueryStmt() instanceof SetOperationStmt) {
|
||||
strBuilder.append("(");
|
||||
}
|
||||
strBuilder.append(operands.get(i).getQueryStmt().toSql());
|
||||
if (operands.get(i).getQueryStmt() instanceof SetOperationStmt) {
|
||||
strBuilder.append(")");
|
||||
}
|
||||
}
|
||||
// Determine whether we need parenthesis around the last Set operand.
|
||||
SetOperand lastOperand = operands.get(operands.size() - 1);
|
||||
|
||||
@ -163,7 +163,7 @@ public class ShowColumnStmt extends ShowStmt {
|
||||
where = where.substitute(aliasMap);
|
||||
selectStmt = new SelectStmt(selectList,
|
||||
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
|
||||
where, null, null);
|
||||
where, null, null, null, LimitElement.NO_LIMIT);
|
||||
analyzer.setSchemaInfo(tableName.getDb(), tableName.getTbl(), null);
|
||||
|
||||
return selectStmt;
|
||||
|
||||
@ -87,7 +87,7 @@ public class ShowDbStmt extends ShowStmt {
|
||||
where = where.substitute(aliasMap);
|
||||
selectStmt = new SelectStmt(selectList,
|
||||
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
|
||||
where, null, null);
|
||||
where, null, null, null, LimitElement.NO_LIMIT);
|
||||
|
||||
return selectStmt;
|
||||
}
|
||||
|
||||
@ -182,7 +182,7 @@ public class ShowTableStatusStmt extends ShowStmt {
|
||||
where = where.substitute(aliasMap);
|
||||
selectStmt = new SelectStmt(selectList,
|
||||
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
|
||||
where, null, null);
|
||||
where, null, null, null, LimitElement.NO_LIMIT);
|
||||
analyzer.setSchemaInfo(db, null, null);
|
||||
|
||||
return selectStmt;
|
||||
|
||||
@ -112,7 +112,7 @@ public class ShowTableStmt extends ShowStmt {
|
||||
where = where.substitute(aliasMap);
|
||||
selectStmt = new SelectStmt(selectList,
|
||||
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
|
||||
where, null, null);
|
||||
where, null, null, null, LimitElement.NO_LIMIT);
|
||||
|
||||
analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null);
|
||||
|
||||
|
||||
@ -101,7 +101,7 @@ public class ShowVariablesStmt extends ShowStmt {
|
||||
where = where.substitute(aliasMap);
|
||||
selectStmt = new SelectStmt(selectList,
|
||||
new FromClause(Lists.newArrayList(new TableRef(tableName, null))),
|
||||
where, null, null);
|
||||
where, null, null, null, LimitElement.NO_LIMIT);
|
||||
LOG.debug("select stmt is {}", selectStmt.toSql());
|
||||
|
||||
// DB: type
|
||||
|
||||
@ -273,9 +273,8 @@ public class StmtRewriter {
|
||||
List<TableRef> newTableRefList = Lists.newArrayList();
|
||||
newTableRefList.add(inlineViewRef);
|
||||
FromClause newFromClause = new FromClause(newTableRefList);
|
||||
SelectStmt result = new SelectStmt(newSelectList, newFromClause, newWherePredicate, null, null);
|
||||
result.setOrderByElements(newOrderByElements);
|
||||
result.setLimitElement(limitElement);
|
||||
SelectStmt result = new SelectStmt(newSelectList, newFromClause, newWherePredicate, null, null,
|
||||
newOrderByElements, limitElement);
|
||||
result.setTableAliasGenerator(tableAliasGenerator);
|
||||
try {
|
||||
result.analyze(analyzer);
|
||||
@ -1206,7 +1205,9 @@ public class StmtRewriter {
|
||||
new FromClause(Lists.newArrayList(tableRef)),
|
||||
matchPolicy.getWherePredicate(),
|
||||
null,
|
||||
null);
|
||||
null,
|
||||
null,
|
||||
LimitElement.NO_LIMIT);
|
||||
selectStmt.fromClause.set(i, new InlineViewRef(tableRef.getAliasAsName().getTbl(), stmt));
|
||||
selectStmt.analyze(analyzer);
|
||||
reAnalyze = true;
|
||||
|
||||
@ -66,7 +66,7 @@ public class CreateMaterializedViewStmtTest {
|
||||
SelectListItem selectListItem = new SelectListItem(arithmeticExpr, null);
|
||||
selectList.addItem(selectListItem);
|
||||
FromClause fromClause = new FromClause();
|
||||
SelectStmt selectStmt = new SelectStmt(selectList, fromClause, null, null, null);
|
||||
SelectStmt selectStmt = new SelectStmt(selectList, fromClause, null, null, null, null, LimitElement.NO_LIMIT);
|
||||
|
||||
new Expectations() {
|
||||
{
|
||||
|
||||
@ -1,116 +0,0 @@
|
||||
// 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.doris.analysis;
|
||||
|
||||
import org.apache.doris.common.UserException;
|
||||
import org.apache.doris.common.util.SqlParserUtils;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
public class QueryOrganizationTest {
|
||||
@Test
|
||||
public void testOneSetOperand() throws Exception {
|
||||
String sql = "SELECT * FROM tbl WHERE a = 1 ORDER BY b LIMIT 10";
|
||||
org.apache.doris.analysis.SqlScanner input = new org.apache.doris.analysis.SqlScanner(
|
||||
new StringReader(sql), 0L);
|
||||
org.apache.doris.analysis.SqlParser parser = new org.apache.doris.analysis.SqlParser(input);
|
||||
StatementBase statementBase = SqlParserUtils.getFirstStmt(parser);
|
||||
|
||||
Assertions.assertTrue(statementBase instanceof SelectStmt);
|
||||
SelectStmt selectStmt = (SelectStmt) statementBase;
|
||||
Assertions.assertEquals(10, selectStmt.getLimit());
|
||||
Assertions.assertEquals(1, selectStmt.getOrderByElements().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoSetOperandWithSecondInnerOrganization() throws Exception {
|
||||
String sql = "SELECT * FROM tbl WHERE a = 1 UNION (SELECT * FROM tbl WHERE a = 2 ORDER BY b LIMIT 10)";
|
||||
org.apache.doris.analysis.SqlScanner input = new org.apache.doris.analysis.SqlScanner(
|
||||
new StringReader(sql), 0L);
|
||||
org.apache.doris.analysis.SqlParser parser = new org.apache.doris.analysis.SqlParser(input);
|
||||
StatementBase statementBase = SqlParserUtils.getFirstStmt(parser);
|
||||
|
||||
Assertions.assertTrue(statementBase instanceof SetOperationStmt);
|
||||
SetOperationStmt setOperationStmt = (SetOperationStmt) statementBase;
|
||||
Assertions.assertEquals(-1, setOperationStmt.getLimit());
|
||||
Assertions.assertNull(setOperationStmt.getOrderByElements());
|
||||
Assertions.assertEquals(2, setOperationStmt.getOperands().size());
|
||||
|
||||
QueryStmt secondQueryStmt = setOperationStmt.getOperands().get(1).getQueryStmt();
|
||||
Assertions.assertEquals(10, secondQueryStmt.getLimit());
|
||||
Assertions.assertEquals(1, secondQueryStmt.getOrderByElements().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoSetOperandWithFirstInnerOrganization() throws Exception {
|
||||
String sql = "(SELECT * FROM tbl WHERE a = 1 ORDER BY b LIMIT 10) UNION SELECT * FROM tbl WHERE a = 2";
|
||||
org.apache.doris.analysis.SqlScanner input = new org.apache.doris.analysis.SqlScanner(
|
||||
new StringReader(sql), 0L);
|
||||
org.apache.doris.analysis.SqlParser parser = new org.apache.doris.analysis.SqlParser(input);
|
||||
StatementBase statementBase = SqlParserUtils.getFirstStmt(parser);
|
||||
|
||||
Assertions.assertTrue(statementBase instanceof SetOperationStmt);
|
||||
SetOperationStmt setOperationStmt = (SetOperationStmt) statementBase;
|
||||
Assertions.assertEquals(-1, setOperationStmt.getLimit());
|
||||
Assertions.assertNull(setOperationStmt.getOrderByElements());
|
||||
Assertions.assertEquals(2, setOperationStmt.getOperands().size());
|
||||
|
||||
QueryStmt secondQueryStmt = setOperationStmt.getOperands().get(0).getQueryStmt();
|
||||
Assertions.assertEquals(10, secondQueryStmt.getLimit());
|
||||
Assertions.assertEquals(1, secondQueryStmt.getOrderByElements().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoSetOperandWithOuterOrganization() throws Exception {
|
||||
String sql = "SELECT * FROM tbl WHERE a = 1 UNION SELECT * FROM tbl WHERE a = 2 ORDER BY b LIMIT 10";
|
||||
org.apache.doris.analysis.SqlScanner input = new org.apache.doris.analysis.SqlScanner(
|
||||
new StringReader(sql), 0L);
|
||||
org.apache.doris.analysis.SqlParser parser = new org.apache.doris.analysis.SqlParser(input);
|
||||
StatementBase statementBase = SqlParserUtils.getFirstStmt(parser);
|
||||
|
||||
Assertions.assertTrue(statementBase instanceof SetOperationStmt);
|
||||
SetOperationStmt setOperationStmt = (SetOperationStmt) statementBase;
|
||||
Assertions.assertEquals(10, setOperationStmt.getLimit());
|
||||
Assertions.assertEquals(1, setOperationStmt.getOrderByElements().size());
|
||||
Assertions.assertEquals(2, setOperationStmt.getOperands().size());
|
||||
|
||||
QueryStmt secondQueryStmt = setOperationStmt.getOperands().get(1).getQueryStmt();
|
||||
Assertions.assertEquals(-1, secondQueryStmt.getLimit());
|
||||
Assertions.assertNull(secondQueryStmt.getOrderByElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidSyntax() throws Exception {
|
||||
String sql = "SELECT * FROM tbl WHERE a = 1 ORDER BY b LIMIT 10 "
|
||||
+ "UNION SELECT * FROM tbl WHERE a = 2 ORDER BY b LIMIT 10";
|
||||
org.apache.doris.analysis.SqlScanner input = new org.apache.doris.analysis.SqlScanner(
|
||||
new StringReader(sql), 0L);
|
||||
org.apache.doris.analysis.SqlParser parser = new org.apache.doris.analysis.SqlParser(input);
|
||||
|
||||
UserException thrown = Assertions.assertThrows(
|
||||
UserException.class,
|
||||
() -> SqlParserUtils.getFirstStmt(parser),
|
||||
"Expected parser throw exception, but it didn't"
|
||||
);
|
||||
|
||||
Assertions.assertTrue(thrown.getMessage().contains("Syntax error"));
|
||||
}
|
||||
}
|
||||
@ -780,7 +780,7 @@ public class SelectStmtTest {
|
||||
Assert.assertEquals("SELECT `t`.`k1` AS `k1` "
|
||||
+ "FROM (WITH v1 AS (SELECT `t1`.`k1` AS `k1` FROM `default_cluster:db1`.`tbl1` t1),"
|
||||
+ "v2 AS (SELECT `t2`.`k1` AS `k1` FROM `default_cluster:db1`.`tbl1` t2) "
|
||||
+ "(SELECT `v1`.`k1` AS `k1` FROM `v1`) UNION SELECT `v2`.`k1` AS `k1` FROM `v2`) t", stmt1.toSql());
|
||||
+ "SELECT `v1`.`k1` AS `k1` FROM `v1` UNION SELECT `v2`.`k1` AS `k1` FROM `v2`) t", stmt1.toSql());
|
||||
|
||||
String sql2 =
|
||||
"with\n"
|
||||
|
||||
@ -91,14 +91,14 @@ public class CreateViewTest {
|
||||
// test union all
|
||||
ExceptionChecker.expectThrowsNoException(
|
||||
() -> createView("create view test.view6 as "
|
||||
+ "(select * from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10) "
|
||||
+ "select * from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10 "
|
||||
+ "union all "
|
||||
+ "(select * from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50);"));
|
||||
+ "select * from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50;"));
|
||||
ExceptionChecker.expectThrowsNoException(
|
||||
() -> createView("create view test.view7 (k1, k2) as "
|
||||
+ "(select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10) "
|
||||
+ "select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10 "
|
||||
+ "union all "
|
||||
+ "(select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50);"));
|
||||
+ "select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50;"));
|
||||
|
||||
Database db = Env.getCurrentInternalCatalog().getDbOrDdlException("default_cluster:test");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user