Skip to content
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
99 changes: 61 additions & 38 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,9 @@ create_stmt ::=
{:
RESULT = new CreateFileStmt(fileName, db, properties);
:}
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName KW_AS select_stmt:selectStmt opt_properties:properties
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName KW_AS query_stmt:queryStmt opt_properties:properties
{:
RESULT = new CreateMaterializedViewStmt(mvName, selectStmt, properties);
RESULT = new CreateMaterializedViewStmt(mvName, queryStmt, properties);
:}
| KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName build_mv:buildMethod
opt_mv_refersh_info:refreshInfo
Expand Down Expand Up @@ -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, null, null);
RESULT = new SelectStmt(list, 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, null, null);
RESULT = new SelectStmt(list, null, null, null, null);
:}
| KW_WARNINGS limit_clause
{:
Expand Down Expand Up @@ -3436,86 +3436,113 @@ 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) {
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, null, limitElement);
}
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) {
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, null, limitElement);
}
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) {
parser.parseError("limit", SqlParserSymbols.KW_LIMIT);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, null, limitElement);
}
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) {
parser.parseError("order", SqlParserSymbols.KW_ORDER);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
queryStmt.setOrderByElements(orderByClause);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
}
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) {
parser.parseError("order", SqlParserSymbols.KW_ORDER);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
queryStmt.setOrderByElements(orderByClause);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
}
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) {
parser.parseError("order", SqlParserSymbols.KW_ORDER);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
queryStmt.setOrderByElements(orderByClause);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
}
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) {
parser.parseError("order", SqlParserSymbols.KW_ORDER);
SetOperand setOperand = operands.get(0);
QueryStmt queryStmt = setOperand.getQueryStmt();
queryStmt.setLimitElement(limitElement);
queryStmt.setOrderByElements(orderByClause);
RESULT = queryStmt;
} else {
RESULT = new SetOperationStmt(operands, orderByClause, limitElement);
}
RESULT = new SetOperationStmt(operands, orderByClause, new LimitElement(offset.longValue(), limit.longValue()));
:}
;


set_operand ::=
select_stmt:select
{:
Expand Down Expand Up @@ -4034,23 +4061,19 @@ set_expr_or_default ::=

select_stmt ::=
select_clause:selectList
limit_clause:limitClause
{: RESULT = new SelectStmt(selectList, null, null, null, null, null, limitClause); :}
{: RESULT = new SelectStmt(selectList, null, null, null, null); :}
| 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, orderByClause,
limitClause);
groupByClause, havingPredicate);
:}
| value_clause:valueClause order_by_clause:orderByClause limit_clause:limitClause
| value_clause:valueClause
{:
RESULT = new SelectStmt(valueClause, orderByClause, limitClause);
RESULT = new SelectStmt(valueClause);
:}
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class CreateMaterializedViewStmt extends DdlStmt {
}

private String mvName;
private QueryStmt queryStmt;
private SelectStmt selectStmt;
private Map<String, String> properties;

Expand All @@ -95,9 +96,16 @@ public class CreateMaterializedViewStmt extends DdlStmt {
// only in Rollup or MaterializedIndexMeta is true
private boolean isReplay = false;

public CreateMaterializedViewStmt(String mvName, SelectStmt selectStmt, Map<String, String> properties) {
/**
* 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) {
this.mvName = mvName;
this.selectStmt = selectStmt;
this.queryStmt = queryStmt;
this.properties = properties;
}

Expand All @@ -109,10 +117,6 @@ public String getMVName() {
return mvName;
}

public SelectStmt getSelectStmt() {
return selectStmt;
}

public List<MVColumnItem> getMVColumnItemList() {
return mvColumnItemList;
}
Expand All @@ -137,6 +141,11 @@ public KeysType getMVKeysType() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ public abstract void getTables(Analyzer analyzer, Map<Long, TableIf> tables, Set

abstract List<TupleId> collectTupleIds();

public void setOrderByElements(ArrayList<OrderByElement> orderByElements) {
this.orderByElements = orderByElements;
}

public ArrayList<OrderByElement> getOrderByElements() {
return orderByElements;
}
Expand Down Expand Up @@ -615,6 +619,10 @@ public void setLimit(long limit) throws AnalysisException {
limitElement = new LimitElement(newLimit);
}

public void setLimitElement(LimitElement limitElement) {
this.limitElement = limitElement;
}

public void removeLimitElement() {
limitElement = LimitElement.NO_LIMIT;
}
Expand Down
19 changes: 11 additions & 8 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,26 @@ public class SelectStmt extends QueryStmt {
// Members that need to be reset to origin
private SelectList originSelectList;

public SelectStmt(ValueList valueList, ArrayList<OrderByElement> orderByElement, LimitElement limitElement) {
super(orderByElement, limitElement);
/**
* 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);
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,
ArrayList<OrderByElement> orderByElements,
LimitElement limitElement) {
super(orderByElements, limitElement);
Expr havingPredicate) {
super(null, LimitElement.NO_LIMIT);
this.selectList = selectList;
this.originSelectList = selectList.clone();
if (fromClause == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,18 +681,12 @@ public String toSql() {
strBuilder.append(" ");
}
Preconditions.checkState(operands.size() > 0);
strBuilder.append(operands.get(0).getQueryStmt().toSql());
strBuilder.append("(").append(operands.get(0).getQueryStmt().toSql()).append(")");
for (int i = 1; i < operands.size() - 1; ++i) {
strBuilder.append(" "
+ operands.get(i).getOperation().toString() + " "
+ ((operands.get(i).getQualifier() == Qualifier.ALL) ? "ALL " : ""));
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(")");
}
strBuilder.append("(").append(operands.get(i).getQueryStmt().toSql()).append(")");
}
// Determine whether we need parenthesis around the last Set operand.
SetOperand lastOperand = operands.get(operands.size() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public SelectStmt toSelectStmt(Analyzer analyzer) throws AnalysisException {
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
where, null, null);
analyzer.setSchemaInfo(tableName.getDb(), tableName.getTbl(), null);

return selectStmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public SelectStmt toSelectStmt(Analyzer analyzer) {
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
where, null, null);

return selectStmt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public SelectStmt toSelectStmt(Analyzer analyzer) throws AnalysisException {
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
where, null, null);
analyzer.setSchemaInfo(db, null, null);

return selectStmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public SelectStmt toSelectStmt(Analyzer analyzer) throws AnalysisException {
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
where, null, null, null, LimitElement.NO_LIMIT);
where, null, null);

analyzer.setSchemaInfo(ClusterNamespace.getNameFromFullName(db), null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public SelectStmt toSelectStmt(Analyzer analyzer) {
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(tableName, null))),
where, null, null, null, LimitElement.NO_LIMIT);
where, null, null);
LOG.debug("select stmt is {}", selectStmt.toSql());

// DB: type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ private static SelectStmt rewriteHavingClauseSubqueries(SelectStmt stmt, Analyze
List<TableRef> newTableRefList = Lists.newArrayList();
newTableRefList.add(inlineViewRef);
FromClause newFromClause = new FromClause(newTableRefList);
SelectStmt result = new SelectStmt(newSelectList, newFromClause, newWherePredicate, null, null,
newOrderByElements, limitElement);
SelectStmt result = new SelectStmt(newSelectList, newFromClause, newWherePredicate, null, null);
result.setOrderByElements(newOrderByElements);
result.setLimitElement(limitElement);
result.setTableAliasGenerator(tableAliasGenerator);
try {
result.analyze(analyzer);
Expand Down Expand Up @@ -1205,9 +1206,7 @@ public static boolean rewriteByPolicy(StatementBase statementBase, Analyzer anal
new FromClause(Lists.newArrayList(tableRef)),
matchPolicy.getWherePredicate(),
null,
null,
null,
LimitElement.NO_LIMIT);
null);
selectStmt.fromClause.set(i, new InlineViewRef(tableRef.getAliasAsName().getTbl(), stmt));
selectStmt.analyze(analyzer);
reAnalyze = true;
Expand Down
Loading