Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the statement of index query to be consistent with standard SQL #249

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ KW_LINK: 'LINK' ;
KW_UNLINK: 'UNLINK';
KW_USING: 'USING';

KW_KVINDEX: 'KVINDEX';

QUOTE : '\'' ;

Expand Down
12 changes: 6 additions & 6 deletions src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ indexStatement
;

createIndexStatement
: KW_CREATE KW_INDEX KW_ON p=timeseries KW_USING func=Identifier indexWithClause? whereClause?
-> ^(TOK_CREATE ^(TOK_INDEX $p ^(TOK_FUNC $func indexWithClause? whereClause?)))
: KW_CREATE KW_INDEX KW_ON p=timeseries KW_USING func=KW_KVINDEX indexWithClause?
-> ^(TOK_CREATE ^(TOK_INDEX $p ^(TOK_FUNC $func indexWithClause?)))
;


Expand All @@ -618,7 +618,7 @@ indexWithEqualExpression


dropIndexStatement
: KW_DROP KW_INDEX func=Identifier KW_ON p=timeseries
: KW_DROP KW_INDEX func=KW_KVINDEX KW_ON p=timeseries
-> ^(TOK_DROP ^(TOK_INDEX $p ^(TOK_FUNC $func)))
;

Expand All @@ -644,10 +644,10 @@ identifier
// ;

selectClause
: KW_SELECT KW_INDEX func=Identifier LPAREN p1=timeseries COMMA p2=timeseries COMMA n1=dateFormatWithNumber COMMA n2=dateFormatWithNumber COMMA epsilon=Float (COMMA alpha=Float COMMA beta=Float)? RPAREN (fromClause)?
-> ^(TOK_SELECT_INDEX $func $p1 $p2 $n1 $n2 $epsilon ($alpha $beta)?) fromClause?
| KW_SELECT clusteredPath (COMMA clusteredPath)* fromClause
: KW_SELECT clusteredPath (COMMA clusteredPath)* fromClause
-> ^(TOK_SELECT clusteredPath+) fromClause
| KW_SELECT indexcmd = KW_KVINDEX LPAREN p=timeseries COMMA n1=dateFormatWithNumber COMMA n2=dateFormatWithNumber COMMA epsilon=Float (COMMA alpha=Float COMMA beta=Float)? RPAREN fromClause
-> ^(TOK_SELECT_INDEX $indexcmd $p $n1 $n2 $epsilon ($alpha $beta)?) fromClause
;

clusteredPath
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cn/edu/tsinghua/iotdb/index/IndexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public static IoTIndex getIndexInstance(IndexType indexType){

public enum IndexType {
KvIndex;
public static IndexType getIndexType(String indexNameString) throws IndexManagerException {
String normalized = indexNameString.toLowerCase();
public static IndexType getIndexType(String indexTypeString) throws IndexManagerException {
String normalized = indexTypeString.toLowerCase();
switch (normalized){
case "kvindex":
case "kv-match":
return KvIndex;
default:
throw new IndexManagerException("unsupport index type:" + indexNameString);
throw new IndexManagerException("unsupport index type:" + indexTypeString);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cn.edu.tsinghua.iotdb.index.common;

public class IndexManagerException extends Exception {
import cn.edu.tsinghua.iotdb.qp.exception.QueryProcessorException;

private static final long serialVersionUID = 6261687971768311032L;
public class IndexManagerException extends QueryProcessorException {

public IndexManagerException() {
super();
}
private static final long serialVersionUID = 6261687971768311032L;

public IndexManagerException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ public boolean build(Path path, List<DataFileInfo> fileList, Map<String, Object>
indexConfig = indexConfigStore.getOrDefault(path.getFullPath(), new IndexConfig());
}
else {
indexConfig.setWindowLength((int) parameters.getOrDefault(IndexConfig.PARAM_WINDOW_LENGTH, IndexConfig.DEFAULT_WINDOW_LENGTH));
indexConfig.setSinceTime((long) parameters.getOrDefault(IndexConfig.PARAM_SINCE_TIME, IndexConfig.DEFAULT_SINCE_TIME));
if(parameters.containsKey(IndexConfig.PARAM_WINDOW_LENGTH))
indexConfig.setWindowLength(Integer.valueOf((String)parameters.get(IndexConfig.PARAM_WINDOW_LENGTH)));
else
indexConfig.setWindowLength(IndexConfig.DEFAULT_WINDOW_LENGTH);
if(parameters.containsKey(IndexConfig.PARAM_SINCE_TIME))
indexConfig.setSinceTime(Long.valueOf((String)parameters.get(IndexConfig.PARAM_SINCE_TIME)));
else
indexConfig.setSinceTime(IndexConfig.DEFAULT_SINCE_TIME);
}

long startTime = indexConfig.getSinceTime();
Expand Down Expand Up @@ -232,8 +238,14 @@ public boolean build(Path path, DataFileInfo newFile, Map<String, Object> parame
indexConfig = indexConfigStore.getOrDefault(path.getFullPath(), new IndexConfig());
}
else {
indexConfig.setWindowLength((int) parameters.getOrDefault(IndexConfig.PARAM_WINDOW_LENGTH, IndexConfig.DEFAULT_WINDOW_LENGTH));
indexConfig.setSinceTime((long) parameters.getOrDefault(IndexConfig.PARAM_SINCE_TIME, IndexConfig.DEFAULT_SINCE_TIME));
if(parameters.containsKey(IndexConfig.PARAM_WINDOW_LENGTH))
indexConfig.setWindowLength(Integer.valueOf((String)parameters.get(IndexConfig.PARAM_WINDOW_LENGTH)));
else
indexConfig.setWindowLength(IndexConfig.DEFAULT_WINDOW_LENGTH);
if(parameters.containsKey(IndexConfig.PARAM_SINCE_TIME))
indexConfig.setSinceTime(Long.valueOf((String)parameters.get(IndexConfig.PARAM_SINCE_TIME)));
else
indexConfig.setSinceTime(IndexConfig.DEFAULT_SINCE_TIME);
}

long startTime = indexConfig.getSinceTime();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private Operator logicalOptimize(Operator operator, QueryProcessExecutor executo
case LOADDATA:
case INSERT:
case INDEX:
case INDEXQUERY:
return operator;
case INDEXQUERY:
case QUERY:
case UPDATE:
case DELETE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public class QueryProcessorException extends Exception {
public QueryProcessorException(String msg) {
super(msg);
}

public QueryProcessorException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public final class IndexOperator extends SFWOperator {

private Path path;
private Map<String, Integer> parameters;
private Map<String, String> parameters;
private long startTime;
private final IndexOperatorType indexOperatorType;

Expand All @@ -21,7 +21,7 @@ public IndexOperator(int tokenIntType,IndexOperatorType indexOperatorType, Index
super(tokenIntType);
this.indexOperatorType = indexOperatorType;
this.indexType = indexType;
operatorType = Operator.OperatorType.INDEX;
this.operatorType = Operator.OperatorType.INDEX;
this.parameters = new HashMap<>();
}

Expand All @@ -42,11 +42,11 @@ public void setPath(Path path) {
this.path = path;
}

public Map<String, Integer> getParameters() {
public Map<String, String> getParameters() {
return parameters;
}

public void setParameters(Map<String, Integer> parameters) {
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@


import cn.edu.tsinghua.iotdb.index.IndexManager;
import cn.edu.tsinghua.iotdb.index.common.IndexManagerException;
import cn.edu.tsinghua.iotdb.qp.constant.SQLConstant;
import cn.edu.tsinghua.iotdb.qp.logical.index.KvMatchIndexQueryOperator;
import cn.edu.tsinghua.tsfile.timeseries.read.support.Path;

import static cn.edu.tsinghua.iotdb.index.IndexManager.*;

public class IndexQueryOperator extends SFWOperator {

private final IndexManager.IndexType indexType;
private final IndexType indexType;
protected Path path;

public IndexQueryOperator(int tokenIntType, IndexManager.IndexType indexType) {
public IndexQueryOperator(int tokenIntType, IndexType indexType) {
super(tokenIntType);
this.operatorType = OperatorType.INDEXQUERY;
this.indexType = indexType;
Expand All @@ -23,7 +28,17 @@ public void setPath(Path path) {
this.path = path;
}

public IndexManager.IndexType getIndexType() {
public IndexType getIndexType() {
return indexType;
}

public static IndexQueryOperator getIndexQueryOperator(String indexTypeString) throws IndexManagerException {
switch (IndexType.getIndexType(indexTypeString)){
case KvIndex:
return new KvMatchIndexQueryOperator(SQLConstant.TOK_QUERY_INDEX);
default:
throw new IndexManagerException("unsupport index type:" + indexTypeString);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ public class IndexPlan extends

private final IndexType indexType;

public IndexPlan(Path path, Map<String, Integer> parameters,long startTime,IndexOperatorType indexOperatorType, IndexType indexType) {
public IndexPlan(Path path, Map<String, String> parameters,long startTime,IndexOperatorType indexOperatorType, IndexType indexType) {
super(false, INDEX);
this.path = path;
this.indexType = indexType;
this.indexOperatorType = indexOperatorType;
this.parameters = new HashMap<>();
this.parameters.putAll(parameters);
this.parameters.put(IndexConfig.PARAM_SINCE_TIME, startTime);
}

public IndexOperatorType getIndexOperatorType(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cn.edu.tsinghua.iotdb.qp.logical.crud.FilterOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.FromOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.IndexOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.IndexQueryOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.InsertOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.QueryOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.SFWOperator;
Expand Down Expand Up @@ -181,7 +182,8 @@ private void analyze(ASTNode astNode) throws QueryProcessorException, ArgsErrorE
// command. Thus, do
// nothing and call analyze() with children nodes recursively.
if (astNode.getChild(0).getType() == TSParser.TOK_SELECT_INDEX) {
initializedOperator = new KvMatchIndexQueryOperator(SQLConstant.TOK_QUERY_INDEX);
String indexTypeString = astNode.getChild(0).getChild(0).getText().toLowerCase();
initializedOperator = IndexQueryOperator.getIndexQueryOperator(indexTypeString);
break;
}
initializedOperator = new QueryOperator(SQLConstant.TOK_QUERY);
Expand Down Expand Up @@ -970,12 +972,12 @@ private void checkMetadataArgs(String dataType, String encoding) throws Metadata
}


private Map<String, Integer> parseIndexWithParameters(ASTNode astNode) {
Map<String, Integer> indexParameters = new HashMap<String, Integer>();
private Map<String, String> parseIndexWithParameters(ASTNode astNode) {
Map<String, String> indexParameters = new HashMap<>();
for (int i = 0; i < astNode.getChildCount(); i++) {
ASTNode child = astNode.getChild(i);
String key = child.getChild(0).getText();
Integer value = Integer.valueOf(child.getChild(1).getText());
String key = child.getChild(0).getText().toLowerCase();
String value = child.getChild(1).getText().toLowerCase();
indexParameters.put(key, value);
}
return indexParameters;
Expand All @@ -986,7 +988,7 @@ private void analyzeIndexCreate(ASTNode astNode) throws LogicalOperatorException
Path path = parsePath(indexNode.getChild(0));
ASTNode funcNode = indexNode.getChild(1);
String indexName = funcNode.getChild(0).getText();
IndexType indexType = null;
IndexType indexType;
try {
indexType = IndexType.getIndexType(indexName);
} catch (IndexManagerException e) {
Expand All @@ -1001,7 +1003,7 @@ private void analyzeIndexCreate(ASTNode astNode) throws LogicalOperatorException
for (int i = 1; i < childCount; i++) {
ASTNode child = funcNode.getChild(i);
if (child.getToken().getType() == TSParser.TOK_WITH) {
Map<String, Integer> indexParameters = parseIndexWithParameters(child);
Map<String, String> indexParameters = parseIndexWithParameters(child);
indexOperator.setParameters(indexParameters);
} else if (child.getToken().getType() == TSParser.TOK_WHERE) {
analyzeWhere(child);
Expand Down Expand Up @@ -1041,26 +1043,23 @@ private void analyzeIndexDrop(ASTNode astNode) throws LogicalOperatorException {
}

private void analyzeIndexSelect(ASTNode astNode) throws LogicalOperatorException {
// astNode = astNode.getChild(0);
String indexQueryName = astNode.getChild(0).getText().toLowerCase();
switch (indexQueryName) {
case "kvindex":
KvMatchIndexQueryOperator indexQuery = new KvMatchIndexQueryOperator(SQLConstant.TOK_QUERY_INDEX);;
IndexType indexType = ((IndexQueryOperator) initializedOperator).getIndexType();
switch (indexType) {
case KvIndex:
KvMatchIndexQueryOperator indexQuery = (KvMatchIndexQueryOperator) initializedOperator;
Path path = parsePath(astNode.getChild(1));
indexQuery.setPath(path);
path = parsePath(astNode.getChild(2));
indexQuery.setPatternPath(path);
long startTime;
long endTime;
if (astNode.getChild(3).getType() == TSParser.TOK_DATETIME) {
startTime = Long.valueOf(parseTokenTime(astNode.getChild(3)));
if (astNode.getChild(2).getType() == TSParser.TOK_DATETIME) {
startTime = Long.valueOf(parseTokenTime(astNode.getChild(2)));
} else {
startTime = Long.valueOf(astNode.getChild(3).getText());
startTime = Long.valueOf(astNode.getChild(2).getText());
}
if (astNode.getChild(4).getType() == TSParser.TOK_DATETIME) {
endTime = Long.valueOf(parseTokenTime(astNode.getChild(4)));
if (astNode.getChild(3).getType() == TSParser.TOK_DATETIME) {
endTime = Long.valueOf(parseTokenTime(astNode.getChild(3)));
} else {
endTime = Long.valueOf(astNode.getChild(4).getText());
endTime = Long.valueOf(astNode.getChild(3).getText());
}

if (startTime > endTime || startTime <= 0) {
Expand All @@ -1069,19 +1068,19 @@ private void analyzeIndexSelect(ASTNode astNode) throws LogicalOperatorException
}
indexQuery.setStartTime(startTime);
indexQuery.setEndTime(endTime);
double epsilon = Float.valueOf(astNode.getChild(5).getText());
double epsilon = Float.valueOf(astNode.getChild(4).getText());
indexQuery.setEpsilon(epsilon);
if (astNode.getChildCount() > 6) {
double alpha = Float.valueOf(astNode.getChild(6).getText());
double beta = Float.valueOf(astNode.getChild(7).getText());
if (astNode.getChildCount() > 5) {
double alpha = Float.valueOf(astNode.getChild(5).getText());
double beta = Float.valueOf(astNode.getChild(6).getText());
indexQuery.setAlpha(alpha);
indexQuery.setBeta(beta);
}
initializedOperator = indexQuery;
break;
default:
throw new LogicalOperatorException(String.format(
"Not support the index query %s, only support subsequence_matching(subm).", indexQueryName));
"Not support the index query %s, only support subsequence_matching(subm).", indexType));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.edu.tsinghua.iotdb.qp.strategy;

import cn.edu.tsinghua.iotdb.index.common.IndexManagerException;
import cn.edu.tsinghua.iotdb.qp.constant.SQLConstant;
import cn.edu.tsinghua.iotdb.qp.exception.GeneratePhysicalPlanException;
import cn.edu.tsinghua.iotdb.qp.exception.LogicalOperatorException;
Expand All @@ -13,6 +14,7 @@
import cn.edu.tsinghua.iotdb.qp.logical.crud.IndexQueryOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.InsertOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.QueryOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.SFWOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.SelectOperator;
import cn.edu.tsinghua.iotdb.qp.logical.crud.UpdateOperator;
import cn.edu.tsinghua.iotdb.qp.logical.index.KvMatchIndexQueryOperator;
Expand Down Expand Up @@ -115,10 +117,15 @@ public PhysicalPlan transformToPhysicalPlan(Operator operator) throws QueryProce
return new IndexPlan(indexOperator.getPath(), indexOperator.getParameters(),
indexOperator.getStartTime(), indexOperator.getIndexOperatorType(), indexOperator.getIndexType());
case INDEXQUERY:
// List<Path> queryPathList = ((SFWOperator)operator).getFromOperator().getPrefixPaths();
// if(queryPathList.size() != 1)
// throw new IndexManagerException("The number of index query paths must be 1, given: " + queryPathList
// .size());
switch (((IndexQueryOperator) operator).getIndexType()){
case KvIndex:
KvMatchIndexQueryOperator indexQueryOperator = (KvMatchIndexQueryOperator) operator;
KvMatchIndexQueryPlan indexQueryPlan = new KvMatchIndexQueryPlan(indexQueryOperator.getPath(),
KvMatchIndexQueryPlan indexQueryPlan = new KvMatchIndexQueryPlan(
indexQueryOperator.getSelectOperator().getSuffixPaths().get(0),
indexQueryOperator.getPatternPath(), indexQueryOperator.getEpsilon(),
indexQueryOperator.getStartTime(), indexQueryOperator.getEndTime());
indexQueryPlan.setAlpha(indexQueryOperator.getAlpha());
Expand Down
Loading