Skip to content

Commit

Permalink
Merge b3f035b into c00b63f
Browse files Browse the repository at this point in the history
  • Loading branch information
JackieTien97 committed Mar 3, 2020
2 parents c00b63f + b3f035b commit c7e222f
Show file tree
Hide file tree
Showing 23 changed files with 1,054 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,30 @@ select count(status), max_value(temperature) from root.ln.wf01.wt01 where time >

<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116088-001e2780-0ac6-11ea-9a01-dc45271d1dad.png"></center>

#### 带Fill子句的降频聚合查询

带Fill子句的降频聚合查询中,Group by子句不支持滑动步长

Fill子句中仅能使用Previous和PREVIOUSUNTILLAST这两种插值方式,Linear不支持

Previous和PREVIOUSUNTILLAST对fill的时间不做限制

填充只针对last_value这一聚合函数,其他的函数不支持,如果其他函数的聚合值查询结果为null,依旧为null,不进行填充

##### PREVIOUSUNTILLAST与PREVIOUS填充的区别

* Previous填充方式的语意没有变,只要前面有值,就可以拿过来填充;
* PREVIOUSUNTILLAST考虑到在某些业务场景下,所填充的值的时间不能大于该时间序列last的时间戳(从业务角度考虑,取历史数据不能取未来历史数据)

对应的SQL语句是

```
SELECT last_value(temperature) as last_temperature FROM root.ln.wf01.wt01 GROUP BY([8, 39), 5m) FILL (int32[PREVIOUSUNTILLAST])
```
这条查询的含义是用PREVIOUSUNTILLAST的Fill方式填充原来的聚合查询结果



GROUP BY的SELECT子句里的查询路径必须是聚合函数,否则系统将会抛出如下对应的错误。

<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116099-0b715300-0ac6-11ea-8074-84e04797b8c7.png"></center>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ Note: the statement needs to satisfy this constraint: <PrefixPath>(FromClause) +
Note: Integer in <TimeUnit> needs to be greater than 0
```

* Group By Fill语句

```
SELECT <SelectClause> FROM <FromClause> WHERE <WhereClause> GROUP BY <GroupByClause> (FILL <GROUPBYFillClause>)?
GroupByClause : LPAREN <TimeInterval> COMMA <TimeUnit> RPAREN
GROUPBYFillClause : LPAREN <TypeClause> RPAREN
TypeClause : <AllClause> | <Int32Clause> | <Int64Clause> | <FloatClause> | <DoubleClause> | <BoolClause> | <TextClause>
AllClause: ALL LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
Int32Clause: INT32 LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
Int64Clause: INT64 LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
FloatClause: FLOAT LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
DoubleClause: DOUBLE LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
BoolClause: BOOLEAN LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
TextClause: TEXT LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
PreviousClause : PREVIOUS
PreviousUntilLastClause : PREVIOUSUNTILLAST
Eg: SELECT last_value(temperature) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (float[PREVIOUS])
Eg: SELECT last_value(power) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (int32[PREVIOUSUNTILLAST])
Eg: SELECT last_value(temperature), last_value(power) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (ALL[PREVIOUS])
Note: In group by fill, sliding step is not supported in group by clause
Note: Now, only last_value aggregation function is supported in group by fill.
Note: Linear fill is not supported in group by fill.
```

* Limit & SLimit 语句

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,33 @@ Then the system will use the time and value filtering condition in the WHERE cla

<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116088-001e2780-0ac6-11ea-9a01-dc45271d1dad.png"></center>

#### Down-Frequency Aggregate Query with Fill Clause

In group by fill, sliding step is not supported in group by clause

Now, only last_value aggregation function is supported in group by fill.

There is no limit about time in group by fill.

Linear fill is not supported in group by fill.


##### Difference Between PREVIOUSUNTILLAST And PREVIOUS

* PREVIOUS will fill any null value as long as there exist value is not null before it.
* PREVIOUSUNTILLAST won't fill the result whose time is after the last time of that time series.

The SQL statement is:

```
SELECT last_value(temperature) as last_temperature FROM root.ln.wf01.wt01 GROUP BY([8, 39), 5m) FILL (int32[PREVIOUSUNTILLAST])
```
which means:

using PREVIOUSUNTILLAST Fill way to fill the origin down-frequency aggregate query result.



The path after SELECT in GROUP BY statement must be aggregate function, otherwise the system will give the corresponding error prompt, as shown below:

<center><img style="width:100%; max-width:800px; max-height:600px; margin-left:auto; margin-right:auto; display:block;" src="https://user-images.githubusercontent.com/16079446/69116099-0b715300-0ac6-11ea-8074-84e04797b8c7.png"></center>
Expand Down
24 changes: 24 additions & 0 deletions docs/Documentation/UserGuide/5-Operation Manual/4-SQL Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,30 @@ Note: the statement needs to satisfy this constraint: <PrefixPath>(FromClause) +
Note: Integer in <TimeUnit> needs to be greater than 0
```

* Group By Fill Statement

```
SELECT <SelectClause> FROM <FromClause> WHERE <WhereClause> GROUP BY <GroupByClause> (FILL <GROUPBYFillClause>)?
GroupByClause : LPAREN <TimeInterval> COMMA <TimeUnit> RPAREN
GROUPBYFillClause : LPAREN <TypeClause> RPAREN
TypeClause : <AllClause> | <Int32Clause> | <Int64Clause> | <FloatClause> | <DoubleClause> | <BoolClause> | <TextClause>
AllClause: ALL LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
Int32Clause: INT32 LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
Int64Clause: INT64 LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
FloatClause: FLOAT LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
DoubleClause: DOUBLE LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
BoolClause: BOOLEAN LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
TextClause: TEXT LBRACKET (<PreviousUntilLastClause> | <PreviousClause>) RBRACKET
PreviousClause : PREVIOUS
PreviousUntilLastClause : PREVIOUSUNTILLAST
Eg: SELECT last_value(temperature) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (float[PREVIOUS])
Eg: SELECT last_value(power) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (int32[PREVIOUSUNTILLAST])
Eg: SELECT last_value(temperature), last_value(power) FROM root.ln.wf01.wt01 GROUP BY([20, 100), 5m) FILL (ALL[PREVIOUS])
Note: In group by fill, sliding step is not supported in group by clause
Note: Now, only last_value aggregation function is supported in group by fill.
Note: Linear fill is not supported in group by fill.
```

* Limit Statement

```
Expand Down
22 changes: 20 additions & 2 deletions server/src/main/antlr4/org/apache/iotdb/db/qp/strategy/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fromClause
specialClause
: specialLimit
| groupByClause specialLimit?
| groupByFillClause
| fillClause slimitClause? alignByDeviceClauseOrDisableAlign?
| alignByDeviceClauseOrDisableAlign?
;
Expand Down Expand Up @@ -205,9 +206,18 @@ groupByClause
RR_BRACKET
;

groupByFillClause
: GROUP BY LR_BRACKET
timeInterval
COMMA DURATION
RR_BRACKET
FILL LR_BRACKET typeClause (COMMA typeClause)* RR_BRACKET
;

typeClause
: dataType LS_BRACKET linearClause RS_BRACKET
| dataType LS_BRACKET previousClause RS_BRACKET
| dataType LS_BRACKET previousClause RS_BRACKET
| dataType LS_BRACKET previousUntilLastClause RS_BRACKET
;

linearClause
Expand All @@ -218,6 +228,10 @@ previousClause
: PREVIOUS (COMMA DURATION)?
;

previousUntilLastClause
: PREVIOUSUNTILLAST (COMMA DURATION)?
;

indexWithClause
: WITH indexValue (COMMA indexValue)?
;
Expand Down Expand Up @@ -307,7 +321,7 @@ nodeNameWithoutStar
;

dataType
: INT32 | INT64 | FLOAT | DOUBLE | BOOLEAN | TEXT
: INT32 | INT64 | FLOAT | DOUBLE | BOOLEAN | TEXT | ALL
;

dateFormat
Expand Down Expand Up @@ -445,6 +459,10 @@ PREVIOUS
: P R E V I O U S
;

PREVIOUSUNTILLAST
: P R E V I O U S U N T I L L A S T
;

METADATA
: M E T A D A T A
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
*/
package org.apache.iotdb.db.qp.constant;

import org.apache.iotdb.db.qp.strategy.SqlBaseLexer;
import org.apache.iotdb.tsfile.read.common.Path;

import java.util.HashMap;
import java.util.Map;

import org.apache.iotdb.db.qp.strategy.SqlBaseLexer;
import org.apache.iotdb.tsfile.read.common.Path;

/**
* this class contains several constants used in SQL.
*/
Expand Down Expand Up @@ -68,6 +66,8 @@ private SQLConstant() {
public static final String AVG = "avg";
public static final String SUM = "sum";

public static final String ALL = "all";

public static final int KW_AND = 1;
public static final int KW_OR = 2;
public static final int KW_NOT = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,7 @@
import org.apache.iotdb.db.qp.logical.sys.AuthorOperator;
import org.apache.iotdb.db.qp.logical.sys.AuthorOperator.AuthorType;
import org.apache.iotdb.db.qp.physical.PhysicalPlan;
import org.apache.iotdb.db.qp.physical.crud.AggregationPlan;
import org.apache.iotdb.db.qp.physical.crud.AlignByDevicePlan;
import org.apache.iotdb.db.qp.physical.crud.BatchInsertPlan;
import org.apache.iotdb.db.qp.physical.crud.DeletePlan;
import org.apache.iotdb.db.qp.physical.crud.FillQueryPlan;
import org.apache.iotdb.db.qp.physical.crud.GroupByPlan;
import org.apache.iotdb.db.qp.physical.crud.InsertPlan;
import org.apache.iotdb.db.qp.physical.crud.QueryPlan;
import org.apache.iotdb.db.qp.physical.crud.RawDataQueryPlan;
import org.apache.iotdb.db.qp.physical.crud.LastQueryPlan;
import org.apache.iotdb.db.qp.physical.crud.UpdatePlan;
import org.apache.iotdb.db.qp.physical.crud.*;
import org.apache.iotdb.db.qp.physical.sys.AuthorPlan;
import org.apache.iotdb.db.qp.physical.sys.CountPlan;
import org.apache.iotdb.db.qp.physical.sys.CreateTimeSeriesPlan;
Expand Down Expand Up @@ -230,9 +220,13 @@ private QueryDataSet processDataQuery(QueryPlan queryPlan, QueryContext context)
if (queryPlan instanceof AlignByDevicePlan) {
queryDataSet = new AlignByDeviceDataSet((AlignByDevicePlan) queryPlan, context, queryRouter);
} else {

if (queryPlan.getPaths() == null || queryPlan.getPaths().isEmpty()) {
// no time series are selected, return EmptyDataSet
return new EmptyDataSet();
} else if (queryPlan instanceof GroupByFillPlan) {
GroupByFillPlan groupByFillPlan = (GroupByFillPlan) queryPlan;
return queryRouter.groupByFill(groupByFillPlan, context);
} else if (queryPlan instanceof GroupByPlan) {
GroupByPlan groupByPlan = (GroupByPlan) queryPlan;
return queryRouter.groupBy(groupByPlan, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public enum OperatorType {
DELETE_ROLE, GRANT_ROLE_PRIVILEGE, REVOKE_ROLE_PRIVILEGE, LIST_USER, LIST_ROLE,
LIST_USER_PRIVILEGE, LIST_ROLE_PRIVILEGE, LIST_USER_ROLES, LIST_ROLE_USERS,
GRANT_WATERMARK_EMBEDDING, REVOKE_WATERMARK_EMBEDDING,
TTL, DELETE_STORAGE_GROUP, LOAD_CONFIGURATION, SHOW, LOAD_FILES, REMOVE_FILE, MOVE_FILE, LAST
TTL, DELETE_STORAGE_GROUP, LOAD_CONFIGURATION, SHOW, LOAD_FILES, REMOVE_FILE, MOVE_FILE, LAST, GROUP_BY_FILL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/
package org.apache.iotdb.db.qp.logical.crud;

import java.util.ArrayList;
import java.util.List;
import org.apache.iotdb.db.qp.logical.Operator;
import org.apache.iotdb.tsfile.read.common.Path;

import java.util.ArrayList;
import java.util.List;

/**
* this class maintains information from select clause.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.apache.iotdb.db.qp.physical.crud;

import org.apache.iotdb.db.qp.logical.Operator;
import org.apache.iotdb.db.query.fill.IFill;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;

import java.util.Map;

public class GroupByFillPlan extends GroupByPlan {

private Map<TSDataType, IFill> fillTypes;

public GroupByFillPlan() {
super();
setOperatorType(Operator.OperatorType.GROUP_BY_FILL);
}

public Map<TSDataType, IFill> getFillType() {
return fillTypes;
}

public void setFillType(Map<TSDataType, IFill> fillTypes) {
this.fillTypes = fillTypes;
}
}

0 comments on commit c7e222f

Please sign in to comment.