From 79cd8edf994b68219cbae76c6f7dceb8cc5ac9c0 Mon Sep 17 00:00:00 2001 From: softpoeter <3095717866@qq.com> Date: Thu, 28 Dec 2017 20:49:12 +0800 Subject: [PATCH 1/5] modify the format of index query to be consistent with standard SQL --- .../cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g | 2 + .../edu/tsinghua/iotdb/sql/parse/TSParser.g | 10 +-- .../tsinghua/iotdb/index/IndexManager.java | 6 +- .../index/common/IndexManagerException.java | 8 +- .../qp/exception/QueryProcessorException.java | 4 + .../qp/logical/crud/IndexQueryOperator.java | 21 ++++- .../iotdb/qp/strategy/LogicalGenerator.java | 39 +++++----- .../iotdb/qp/strategy/PhysicalGenerator.java | 8 +- .../tsinghua/iotdb/service/KVIndexTest.java | 76 ++++++++----------- .../edu/tsinghua/iotdb/sql/SQLParserTest.java | 31 ++++---- 10 files changed, 108 insertions(+), 97 deletions(-) diff --git a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g index ff4d4e3f0..0ebbd9111 100644 --- a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g +++ b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSLexer.g @@ -65,6 +65,8 @@ KW_LINK: 'LINK' ; KW_UNLINK: 'UNLINK'; KW_USING: 'USING'; +KW_KVINDEX: 'KVINDEX'; + QUOTE : '\'' ; DOT : '.'; // generated as a part of Number rule diff --git a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g index 976c2bbfa..2c188a9f8 100644 --- a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g +++ b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g @@ -584,7 +584,7 @@ indexStatement ; createIndexStatement - : KW_CREATE KW_INDEX KW_ON p=timeseries KW_USING func=Identifier indexWithClause? whereClause? + : KW_CREATE KW_INDEX KW_ON p=timeseries KW_USING func=KW_KVINDEX indexWithClause? whereClause? -> ^(TOK_CREATE ^(TOK_INDEX $p ^(TOK_FUNC $func indexWithClause? whereClause?))) ; @@ -606,7 +606,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))) ; @@ -632,10 +632,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 diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/IndexManager.java b/src/main/java/cn/edu/tsinghua/iotdb/index/IndexManager.java index 99c0189d5..13efbd4a4 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/index/IndexManager.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/index/IndexManager.java @@ -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); } } } diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/common/IndexManagerException.java b/src/main/java/cn/edu/tsinghua/iotdb/index/common/IndexManagerException.java index 1119ed8ee..747a678a3 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/index/common/IndexManagerException.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/index/common/IndexManagerException.java @@ -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); diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/exception/QueryProcessorException.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/exception/QueryProcessorException.java index dc94e9f91..fe73b63b4 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/exception/QueryProcessorException.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/exception/QueryProcessorException.java @@ -13,4 +13,8 @@ public class QueryProcessorException extends Exception { public QueryProcessorException(String msg) { super(msg); } + + public QueryProcessorException(Throwable cause) { + super(cause); + } } diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexQueryOperator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexQueryOperator.java index 6f872fca8..ebc45f410 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexQueryOperator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexQueryOperator.java @@ -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; @@ -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); + + } + } } diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java index b929d9af6..9433a9dba 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java @@ -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; @@ -177,7 +178,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); @@ -879,7 +881,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) { @@ -934,26 +936,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) { @@ -962,11 +961,11 @@ 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); } @@ -974,7 +973,7 @@ private void analyzeIndexSelect(ASTNode astNode) throws LogicalOperatorException 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)); } } diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java index 0a66e92f5..48b26e844 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java @@ -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; @@ -12,6 +13,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; @@ -114,10 +116,14 @@ public PhysicalPlan transformToPhysicalPlan(Operator operator) throws QueryProce return new IndexPlan(indexOperator.getPath(), indexOperator.getParameters(), indexOperator.getStartTime(), indexOperator.getIndexOperatorType(), indexOperator.getIndexType()); case INDEXQUERY: + List 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(queryPathList.get(0), indexQueryOperator.getPatternPath(), indexQueryOperator.getEpsilon(), indexQueryOperator.getStartTime(), indexQueryOperator.getEndTime()); indexQueryPlan.setAlpha(indexQueryOperator.getAlpha()); diff --git a/src/test/java/cn/edu/tsinghua/iotdb/service/KVIndexTest.java b/src/test/java/cn/edu/tsinghua/iotdb/service/KVIndexTest.java index 5966dde01..eb113ae14 100644 --- a/src/test/java/cn/edu/tsinghua/iotdb/service/KVIndexTest.java +++ b/src/test/java/cn/edu/tsinghua/iotdb/service/KVIndexTest.java @@ -37,74 +37,70 @@ private String count(String path) { {"SET STORAGE GROUP TO root.vehicle.d1"}, {"CREATE TIMESERIES root.vehicle.d0.s0 WITH DATATYPE=INT32, ENCODING=RLE"}, {"CREATE TIMESERIES root.vehicle.d1.s0 WITH DATATYPE=INT32, ENCODING=RLE"}, -// s0第一个文件 +// create the first file of sensor s0 {"insert into root.vehicle.d0(timestamp,s0) values(1,101)"}, {"insert into root.vehicle.d0(timestamp,s0) values(2,102)"}, {"insert into root.vehicle.d0(timestamp,s0) values(3,103)"}, {"insert into root.vehicle.d0(timestamp,s0) values(4,104)"}, {"insert into root.vehicle.d0(timestamp,s0) values(5,105)"}, -// 创建索引 +// create index for d0.s0 {"create index on root.vehicle.d0.s0 using kvindex with window_length=2, since_time=0"}, -// 强行切断d0.s0,生成d1.s0文件 +// close the first file of d1.s0 which closes d0.s0 due to the threshold number of existing Filenodes is 1 {"insert into root.vehicle.d1(timestamp,s0) values(5,102)"}, -// s0第二个文件 +// create the second file of sensor s0 {"insert into root.vehicle.d0(timestamp,s0) values(6,106)"}, {"insert into root.vehicle.d0(timestamp,s0) values(7,107)"}, {"insert into root.vehicle.d0(timestamp,s0) values(8,108)"}, {"insert into root.vehicle.d0(timestamp,s0) values(9,109)"}, {"insert into root.vehicle.d0(timestamp,s0) values(10,110)"}, -// 强行切断d0.s0,生成第二个d1.s0文件 +// close the second file of d0.s0 {"insert into root.vehicle.d1(timestamp,s0) values(6,102)"}, -// s0第三个文件,处于未关闭状态 +// create the thrid file of d0.s0 which is unclosed. {"insert into root.vehicle.d0(timestamp,s0) values(11,111)"}, {"insert into root.vehicle.d0(timestamp,s0) values(12,112)"}, {"insert into root.vehicle.d0(timestamp,s0) values(13,113)"}, {"insert into root.vehicle.d0(timestamp,s0) values(14,114)"}, {"insert into root.vehicle.d0(timestamp,s0) values(15,115)"}, -// 修改d2.s0,强行切断d0.s0,生成第三个d0.s0文件 +// update the file of d0.s0 {"update root.vehicle SET d0.s0 = 33333 WHERE time >= 6 and time <= 7"}, {"insert into root.vehicle.d0(timestamp,s0) values(7,102)"}, -// 单文件索引查询 -// { -// "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 4, 7, 0.0, 1.0, 0.0) from root" + -// ".vehicle.d0.s0", -// "0,4,7,0.0", -// }, -// { -// "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 2, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", -// "0,2,5,0.0", -// }, +// query kvindex within a single file { - "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 1, 4, 0.0, 1.0, 0.0) from root" + - ".indextest.d0.s0", + "select kvindex(root.vehicle.d0.s0, 2, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "0,2,5,0.0", + }, + { + "select kvindex(root.vehicle.d0.s0, 1, 4, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", "0,1,4,0.0", }, -// 跨文件索引,涉及到Overflow的查询 - -// merge操作 +// query kvindex across multiple files involving Overflow file. + { + "select kvindex(root.vehicle.d0.s0, 4, 7, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "0,4,7,0.0", + }, +// merge {"merge"}, -// 单文件索引查询 +// query index in a single file after merging operation { - "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 2, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "select kvindex(root.vehicle.d0.s0, 2, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", "0,2,5,0.0", }, { - "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 3, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "select kvindex(root.vehicle.d0.s0, 3, 5, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", "0,3,5,0.0", }, - -// 跨文件索引,涉及到Overflow的查询 +// query kvindex across multiple files { - "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 5, 8, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "select kvindex(root.vehicle.d0.s0, 5, 8, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", "0,5,8,0.0", }, -// 删除索引 +// drop the index {"drop index kvindex on root.vehicle.d0.s0"}, -//// 再次查询 +// query the dropped index will throw exception { - "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 6, 9, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", - "0,1,4,0.0", + "select kvindex(root.vehicle.d0.s0, 6, 9, 0.0, 1.0, 0.0) from root.vehicle.d0.s0", + "The timeseries root.vehicle.d0.s0 hasn't been indexed.", }, }; @@ -150,24 +146,17 @@ private void executeSQL() throws ClassNotFoundException, SQLException { try { for (String[] sqlRet : sqls) { String sql = sqlRet[0]; - System.out.println("testtest-sql\t" + sql); + System.out.println("test-sql\t" + sql); if ("".equals(sql)) return; -// if("select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 1, 3, 0)".equals(sql)) -// System.out.println(); if (sqlRet.length == 1) { //长度1,non-query语句 connection = DriverManager.getConnection("jdbc:tsfile://127.0.0.1:6667/", "root", "root"); Statement statement = connection.createStatement(); statement.execute(sql); - if ("merge".equals(sql)) { -// Thread.sleep(3000); - System.out.println("process merge operation"); - } statement.close(); } else { //长度2,query语句,第二项是结果 -// String[] retArray = (String[]) sqlRet[1]; query(sql, sqlRet); } } @@ -188,8 +177,6 @@ private void query(String querySQL, String[] retArray) throws ClassNotFoundExcep Statement statement = connection.createStatement(); try { boolean hasResultSet = statement.execute(querySQL); - // System.out.println(hasResultSet + "..."); - // KvMatchIndexQueryPlan planForHeader = new KvMatchIndexQueryPlan(null, null, 0,0,0); Assert.assertTrue(hasResultSet); if (hasResultSet) { ResultSet resultSet = statement.getResultSet(); @@ -198,7 +185,6 @@ private void query(String querySQL, String[] retArray) throws ClassNotFoundExcep String ans = resultSet.getString(TIMESTAMP_STR) + "," + resultSet.getString(2) + "," + resultSet.getString(3) + "," + resultSet.getString(4); - System.out.println("testtest-actual\t" + ans); if (!retArray[cnt].equals(ans)) Assert.assertEquals(retArray[cnt], ans); cnt++; @@ -209,8 +195,8 @@ private void query(String querySQL, String[] retArray) throws ClassNotFoundExcep Assert.assertEquals(retArray.length, cnt); } } catch (TsfileSQLException e) { - Assert.assertEquals(e.getMessage(),"The timeseries root.vehicle.d0.s0 hasn't been indexed."); - Assert.assertEquals(querySQL, "select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 6, 9, 0.0, 1.0, 0.0) from root.vehicle.d0.s0"); + Assert.assertEquals(retArray[1], e.getMessage()); + } } catch (Exception e) { e.printStackTrace(); diff --git a/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java b/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java index b9b8c548b..864ea5d23 100644 --- a/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java +++ b/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java @@ -955,11 +955,11 @@ public void createIndex1() throws ParseException, RecognitionException { public void createIndex2() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_CREATE", "TOK_INDEX", "TOK_PATH" ,"TOK_ROOT", "a", "b", "c", - "TOK_FUNC", "kv-match2", + "TOK_FUNC", "kvindex", "TOK_WITH", "TOK_INDEX_KV", "xxx", "50", "TOK_INDEX_KV", "xxx", "123", "TOK_WHERE", ">", "TOK_PATH", "time" ,"TOK_DATETIME", "now")); ArrayList rec = new ArrayList<>(); - ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kv-match2 with xxx=50,xxx=123 where time > now();"); + ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kvindex with xxx=50,xxx=123 where time > now();"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); @@ -973,13 +973,13 @@ public void createIndex2() throws ParseException, RecognitionException { @Test public void selectIndex1() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_QUERY", - "TOK_SELECT_INDEX", "subsequence_matching", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", + "TOK_SELECT_INDEX", "kvindex", + "TOK_PATH", "TOK_ROOT", "b", "c", "123", "132", "123.1", + "TOK_FROM", "TOK_PATH" ,"TOK_ROOT", "a", "TOK_WHERE", "<", "TOK_PATH", "time", "10")); ArrayList rec = new ArrayList<>(); - ASTNode astTree = ParseGenerator.generateAST("select index subsequence_matching(root.a.b.c, root.a.b.c, 123, 132 , 123.1) where time < 10"); + ASTNode astTree = ParseGenerator.generateAST("select kvindex(root.b.c, 123, 132, 123.1) from root.a where time < 10"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); @@ -988,19 +988,19 @@ public void selectIndex1() throws ParseException, RecognitionException { assertEquals(rec.get(i), ans.get(i)); i++; } + System.out.println(astTree.dump()); } @Test public void selectIndex2() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_QUERY", - "TOK_SELECT_INDEX", "subsequence_matching", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", + "TOK_SELECT_INDEX", "kvindex", + "TOK_PATH", "TOK_ROOT", "c", "123", "132", "123.1", "0.123", "0.5", - "TOK_FROM", "TOK_PATH", "TOK_ROOT", "a", "b")); + "TOK_FROM", "TOK_PATH", "TOK_ROOT", "a", "b", "c")); ArrayList rec = new ArrayList<>(); // ASTNode astTree = ParseGenerator.generateAST("select index kvindex(root.vehicle.d0.s0, root.vehicle.d0.s0, 1, 3, 0.0, 1.0, 0.0) from root.vehicle.d0.s0"); - ASTNode astTree = ParseGenerator.generateAST("select index subsequence_matching(root.a.b.c, root.a.b.c, 123, 132 , 123.1, 0.123, 0.5) from root.a.b;"); + ASTNode astTree = ParseGenerator.generateAST("select kvindex(root.c, 123, 132 , 123.1, 0.123, 0.5) from root.a.b;"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); @@ -1015,13 +1015,14 @@ public void selectIndex2() throws ParseException, RecognitionException { public void selectIndex3() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_QUERY", "TOK_SELECT_INDEX", "kvindex", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", - "TOK_PATH", "TOK_ROOT", "a", "b", "c", + "TOK_PATH", "TOK_ROOT", "b", "c", "TOK_DATETIME", "2016-11-16T16:22:33+08:00", "TOK_DATETIME", "now", - "123.1", "0.123", "0.5")); + "123.1", "0.123", "0.5", + "TOK_FROM", "TOK_PATH", "TOK_ROOT", "a", + "TOK_WHERE", "<", "TOK_PATH", "time", "10")); ArrayList rec = new ArrayList<>(); - ASTNode astTree = ParseGenerator.generateAST("select index kvindex(root.a.b.c, root.a.b.c, 2016-11-16T16:22:33+08:00, now() , 123.1, 0.123, 0.5)"); + ASTNode astTree = ParseGenerator.generateAST("select kvindex(root.b.c, 2016-11-16T16:22:33+08:00, now() , 123.1, 0.123, 0.5) from root.a where time < 10"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); From cf43607af5e28471739c126b128b5e533de88a56 Mon Sep 17 00:00:00 2001 From: softpoeter <3095717866@qq.com> Date: Fri, 29 Dec 2017 17:22:03 +0800 Subject: [PATCH 2/5] add JUnit Test for IndexQueryOperator --- .../edu/tsinghua/iotdb/qp/QueryProcessor.java | 2 +- .../iotdb/qp/strategy/PhysicalGenerator.java | 11 +-- .../optimizer/ConcatPathOptimizer.java | 18 +++-- .../iotdb/qp/query/KvIndexQueryTest.java | 70 +++++++++++++++++++ 4 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/test/java/cn/edu/tsinghua/iotdb/qp/query/KvIndexQueryTest.java diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java index c6d01f354..369c060e3 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/QueryProcessor.java @@ -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: diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java index 48b26e844..1ad954401 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/PhysicalGenerator.java @@ -116,14 +116,15 @@ public PhysicalPlan transformToPhysicalPlan(Operator operator) throws QueryProce return new IndexPlan(indexOperator.getPath(), indexOperator.getParameters(), indexOperator.getStartTime(), indexOperator.getIndexOperatorType(), indexOperator.getIndexType()); case INDEXQUERY: - List queryPathList = ((SFWOperator)operator).getFromOperator().getPrefixPaths(); - if(queryPathList.size() != 1) - throw new IndexManagerException("The number of index query paths must be 1, given: " + queryPathList - .size()); +// List 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(queryPathList.get(0), + KvMatchIndexQueryPlan indexQueryPlan = new KvMatchIndexQueryPlan( + indexQueryOperator.getSelectOperator().getSuffixPaths().get(0), indexQueryOperator.getPatternPath(), indexQueryOperator.getEpsilon(), indexQueryOperator.getStartTime(), indexQueryOperator.getEndTime()); indexQueryPlan.setAlpha(indexQueryOperator.getAlpha()); diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/optimizer/ConcatPathOptimizer.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/optimizer/ConcatPathOptimizer.java index b0c96e73d..c701040bf 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/optimizer/ConcatPathOptimizer.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/optimizer/ConcatPathOptimizer.java @@ -46,10 +46,8 @@ public Operator transform(Operator operator) throws LogicalOptimizeException { LOG.warn("given SFWOperator doesn't have prefix paths, cannot concat path"); return operator; } - SelectOperator select = sfwOperator.getSelectOperator(); - // concat select paths - concatSelect(prefixPaths, select); + concatSelect(prefixPaths, sfwOperator); // concat filter FilterOperator filter = sfwOperator.getFilterOperator(); @@ -60,11 +58,21 @@ public Operator transform(Operator operator) throws LogicalOptimizeException { } - private void concatSelect(List fromPaths, SelectOperator selectOperator) + private void concatSelect(List fromPaths, SFWOperator sfwOperator) throws LogicalOptimizeException { List suffixPaths; + SelectOperator selectOperator = sfwOperator.getSelectOperator(); if (selectOperator == null || (suffixPaths = selectOperator.getSuffixPaths()).isEmpty()) { - throw new LogicalOptimizeException("given SFWOperator doesn't have suffix paths, cannot concat path"); + //This branch only happens in the case of index query. + //Index query has exactly one query path occurring in FROM clause and has no select operator. + if(fromPaths.size() != 1 || !fromPaths.get(0).startWith(SQLConstant.ROOT)){ + throw new LogicalOptimizeException("For the SFWOperator without selectOperators, " + + "it should has one path starting with ROOT. The actual path list is: " + fromPaths); + } + selectOperator = new SelectOperator(SQLConstant.TOK_SELECT); + selectOperator.setSuffixPathList(fromPaths); + sfwOperator.setSelectOperator(selectOperator); + return; } if(selectOperator.getAggregations().size() != 0 && selectOperator.getSuffixPaths().size() != selectOperator.getAggregations().size()) throw new LogicalOptimizeException("Common queries and aggregated queries are not allowed to appear at the same time"); diff --git a/src/test/java/cn/edu/tsinghua/iotdb/qp/query/KvIndexQueryTest.java b/src/test/java/cn/edu/tsinghua/iotdb/qp/query/KvIndexQueryTest.java new file mode 100644 index 000000000..c24b48ddf --- /dev/null +++ b/src/test/java/cn/edu/tsinghua/iotdb/qp/query/KvIndexQueryTest.java @@ -0,0 +1,70 @@ +package cn.edu.tsinghua.iotdb.qp.query; + +import cn.edu.tsinghua.iotdb.exception.ArgsErrorException; +import cn.edu.tsinghua.iotdb.qp.QueryProcessor; +import cn.edu.tsinghua.iotdb.qp.exception.QueryProcessorException; +import cn.edu.tsinghua.iotdb.qp.logical.crud.IndexQueryOperator; +import cn.edu.tsinghua.iotdb.qp.physical.PhysicalPlan; +import cn.edu.tsinghua.iotdb.qp.physical.crud.IndexPlan; +import cn.edu.tsinghua.iotdb.qp.physical.crud.IndexQueryPlan; +import cn.edu.tsinghua.iotdb.qp.physical.index.KvMatchIndexQueryPlan; +import cn.edu.tsinghua.iotdb.qp.utils.MemIntQpExecutor; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class KvIndexQueryTest { + private static double minDoubleDelta = Double.MIN_VALUE; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testQueryIndexSingleSideWhere() throws QueryProcessorException, ArgsErrorException { + + String queryIndex = "select kvindex(root.c, 1, 2, 0.5, 3.0, 4.0) from root.a.b where time >= 5"; + QueryProcessor processor = new QueryProcessor(new MemIntQpExecutor()); + PhysicalPlan plan = processor.parseSQLToPhysicalPlan(queryIndex); + assertTrue(plan instanceof KvMatchIndexQueryPlan); + KvMatchIndexQueryPlan indexQueryPlan = (KvMatchIndexQueryPlan) plan; + assertEquals("root.c", indexQueryPlan.getPatterPath().toString()); + assertEquals("[root.a.b]", indexQueryPlan.getPaths().toString()); + assertEquals(1, indexQueryPlan.getPatternStartTime()); + assertEquals(2, indexQueryPlan.getPatternEndTime()); + assertEquals(5, indexQueryPlan.getStartTime()); + assertEquals(Long.MAX_VALUE, indexQueryPlan.getEndTime()); + assertEquals(0.5d, indexQueryPlan.getEpsilon(), minDoubleDelta); + assertEquals(3d, indexQueryPlan.getAlpha(), minDoubleDelta); + assertEquals(4d, indexQueryPlan.getBeta(), minDoubleDelta); + } + + @Test + public void testQueryIndexBothSideWhere() throws QueryProcessorException, ArgsErrorException { + String queryIndex = "select kvindex(root.c, 1, 2, 0.5, 3.0, 4.0) from root.a.b where time >= 5 and time <= 10"; + QueryProcessor processor = new QueryProcessor(new MemIntQpExecutor()); + PhysicalPlan plan = processor.parseSQLToPhysicalPlan(queryIndex); + assertTrue(plan instanceof KvMatchIndexQueryPlan); + KvMatchIndexQueryPlan indexQueryPlan = (KvMatchIndexQueryPlan) plan; + assertEquals("root.c", indexQueryPlan.getPatterPath().toString()); + assertEquals("[root.a.b]", indexQueryPlan.getPaths().toString()); + assertEquals(1, indexQueryPlan.getPatternStartTime()); + assertEquals(2, indexQueryPlan.getPatternEndTime()); + assertEquals(5, indexQueryPlan.getStartTime()); + assertEquals(10, indexQueryPlan.getEndTime()); + assertEquals(0.5d, indexQueryPlan.getEpsilon(), minDoubleDelta); + assertEquals(3d, indexQueryPlan.getAlpha(), minDoubleDelta); + assertEquals(4d, indexQueryPlan.getBeta(), minDoubleDelta); + + } + +} From 529ab5723e122c15005a8a33a794aa21fdc33850 Mon Sep 17 00:00:00 2001 From: yi xu Date: Fri, 29 Dec 2017 22:54:33 +0800 Subject: [PATCH 3/5] change create index grammar --- .../cn/edu/tsinghua/iotdb/sql/parse/TSParser.g | 4 ++-- .../cn/edu/tsinghua/iotdb/sql/SQLParserTest.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g index 0611d635e..22e55a56b 100644 --- a/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g +++ b/src/main/antlr3/cn/edu/tsinghua/iotdb/sql/parse/TSParser.g @@ -596,8 +596,8 @@ indexStatement ; createIndexStatement - : KW_CREATE KW_INDEX KW_ON p=timeseries KW_USING func=KW_KVINDEX 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?))) ; diff --git a/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java b/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java index f5e456f09..d5ce8a609 100644 --- a/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java +++ b/src/test/java/cn/edu/tsinghua/iotdb/sql/SQLParserTest.java @@ -1073,10 +1073,10 @@ public void createIndex1() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_CREATE", "TOK_INDEX", "TOK_PATH" ,"TOK_ROOT", "a", "b", "c", "TOK_FUNC", "kvindex", - "TOK_WITH", "TOK_INDEX_KV", "window_length", "50", - "TOK_WHERE", ">", "TOK_PATH", "time", "123")); + "TOK_WITH", "TOK_INDEX_KV", "window_length", "50" + )); ArrayList rec = new ArrayList<>(); - ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kvindex with window_length=50 where time > 123"); + ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kvindex with window_length=50"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); @@ -1092,10 +1092,10 @@ public void createIndex2() throws ParseException, RecognitionException { ArrayList ans = new ArrayList<>(Arrays.asList("TOK_CREATE", "TOK_INDEX", "TOK_PATH" ,"TOK_ROOT", "a", "b", "c", "TOK_FUNC", "kvindex", - "TOK_WITH", "TOK_INDEX_KV", "xxx", "50", "TOK_INDEX_KV", "xxx", "123", - "TOK_WHERE", ">", "TOK_PATH", "time" ,"TOK_DATETIME", "now")); + "TOK_WITH", "TOK_INDEX_KV", "xxx", "50", "TOK_INDEX_KV", "xxx", "123" + )); ArrayList rec = new ArrayList<>(); - ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kvindex with xxx=50,xxx=123 where time > now();"); + ASTNode astTree = ParseGenerator.generateAST("create index on root.a.b.c using kvindex with xxx=50,xxx=123;"); astTree = ParseUtils.findRootNonNullToken(astTree); recursivePrintSon(astTree, rec); From aeb5aef66cd275a4688d9ebb62cd4cfa9a29540e Mon Sep 17 00:00:00 2001 From: softpoeter <3095717866@qq.com> Date: Fri, 29 Dec 2017 23:13:00 +0800 Subject: [PATCH 4/5] Fix bug of index --- .../edu/tsinghua/iotdb/qp/cud/IndexTest.java | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java b/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java index a1b269eb5..7b6368599 100644 --- a/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java +++ b/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java @@ -1,6 +1,7 @@ package cn.edu.tsinghua.iotdb.qp.cud; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.Map; @@ -28,29 +29,17 @@ public void tearDown() throws Exception { @Test public void testCreateIndex() throws QueryProcessorException, ArgsErrorException { - String createIndex = "create index on root.laptop.d1.s1 using kvindex"; + String createIndex = "create index on root.laptop.d1.s1 using kvindex with window_length=2, since_time=0"; QueryProcessor processor = new QueryProcessor(new MemIntQpExecutor()); IndexPlan indexPlan = (IndexPlan) processor.parseSQLToPhysicalPlan(createIndex); assertEquals("root.laptop.d1.s1", indexPlan.getPaths().get(0).getFullPath()); - assertEquals(1, indexPlan.getParameters().keySet().size()); + assertEquals(2, indexPlan.getParameters().keySet().size()); + assertTrue(indexPlan.getParameters().containsKey("window_length")); + assertEquals(2, indexPlan.getParameters().get("window_length")); + assertTrue(indexPlan.getParameters().containsKey("since_time")); + assertEquals(0L, indexPlan.getParameters().get("since_time")); // assertEquals(0, indexPlan.getStartTime()); } - - @Test - public void testCreateIndex2() throws QueryProcessorException, ArgsErrorException{ - String createIndex = "create index on root.laptop.d1.s1 using kvindex with b=20,a=50 where time>=100"; - QueryProcessor processor = new QueryProcessor(new MemIntQpExecutor()); - IndexPlan indexPlan = (IndexPlan) processor.parseSQLToPhysicalPlan(createIndex); - assertEquals("root.laptop.d1.s1", indexPlan.getPaths().get(0).getFullPath()); - assertEquals(3, indexPlan.getParameters().keySet().size()); - Map map = indexPlan.getParameters(); - assertEquals(20, map.get("b")); - assertEquals(50, map.get("a")); -// assertEquals(100, indexPlan.getStartTime()); - createIndex = "create index on root.laptop.d1.s1 using kvindex with b=20,a=50 where time>100"; - processor = new QueryProcessor(new MemIntQpExecutor()); - indexPlan = (IndexPlan) processor.parseSQLToPhysicalPlan(createIndex); -// assertEquals(101, indexPlan.getStartTime()); - } + } From f725eddc98bcf3d394e369c34e1937ea97ebea26 Mon Sep 17 00:00:00 2001 From: softpoeter <3095717866@qq.com> Date: Sat, 30 Dec 2017 00:33:54 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iotdb/index/kvmatch/KvMatchIndex.java | 20 +++++++++++++++---- .../iotdb/qp/logical/crud/IndexOperator.java | 8 ++++---- .../iotdb/qp/physical/crud/IndexPlan.java | 3 +-- .../iotdb/qp/strategy/LogicalGenerator.java | 10 +++++----- .../edu/tsinghua/iotdb/qp/cud/IndexTest.java | 4 ++-- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchIndex.java b/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchIndex.java index b98c67c60..9ca984cc8 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchIndex.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/index/kvmatch/KvMatchIndex.java @@ -115,8 +115,14 @@ public boolean build(Path path, List fileList, Map 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(); @@ -232,8 +238,14 @@ public boolean build(Path path, DataFileInfo newFile, Map 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(); diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexOperator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexOperator.java index ad4287aa2..c06c33d84 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexOperator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/logical/crud/IndexOperator.java @@ -11,7 +11,7 @@ public final class IndexOperator extends SFWOperator { private Path path; - private Map parameters; + private Map parameters; private long startTime; private final IndexOperatorType indexOperatorType; @@ -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<>(); } @@ -42,11 +42,11 @@ public void setPath(Path path) { this.path = path; } - public Map getParameters() { + public Map getParameters() { return parameters; } - public void setParameters(Map parameters) { + public void setParameters(Map parameters) { this.parameters = parameters; } diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/physical/crud/IndexPlan.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/physical/crud/IndexPlan.java index 3ed5f0e88..390c0abb4 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/physical/crud/IndexPlan.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/physical/crud/IndexPlan.java @@ -23,14 +23,13 @@ public class IndexPlan extends private final IndexType indexType; - public IndexPlan(Path path, Map parameters,long startTime,IndexOperatorType indexOperatorType, IndexType indexType) { + public IndexPlan(Path path, Map 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(){ diff --git a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java index 9b54c0840..82ef1d04e 100644 --- a/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java +++ b/src/main/java/cn/edu/tsinghua/iotdb/qp/strategy/LogicalGenerator.java @@ -972,12 +972,12 @@ private void checkMetadataArgs(String dataType, String encoding) throws Metadata } - private Map parseIndexWithParameters(ASTNode astNode) { - Map indexParameters = new HashMap(); + private Map parseIndexWithParameters(ASTNode astNode) { + Map 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; @@ -1003,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 indexParameters = parseIndexWithParameters(child); + Map indexParameters = parseIndexWithParameters(child); indexOperator.setParameters(indexParameters); } else if (child.getToken().getType() == TSParser.TOK_WHERE) { analyzeWhere(child); diff --git a/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java b/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java index 7b6368599..cc9449924 100644 --- a/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java +++ b/src/test/java/cn/edu/tsinghua/iotdb/qp/cud/IndexTest.java @@ -35,9 +35,9 @@ public void testCreateIndex() throws QueryProcessorException, ArgsErrorException assertEquals("root.laptop.d1.s1", indexPlan.getPaths().get(0).getFullPath()); assertEquals(2, indexPlan.getParameters().keySet().size()); assertTrue(indexPlan.getParameters().containsKey("window_length")); - assertEquals(2, indexPlan.getParameters().get("window_length")); + assertEquals("2", indexPlan.getParameters().get("window_length")); assertTrue(indexPlan.getParameters().containsKey("since_time")); - assertEquals(0L, indexPlan.getParameters().get("since_time")); + assertEquals("0", indexPlan.getParameters().get("since_time")); // assertEquals(0, indexPlan.getStartTime()); }