This is an automated email from the ASF dual-hosted git repository.

ycycse pushed a commit to branch ycy/planCacheDemo
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/ycy/planCacheDemo by this push:
     new 77892a4961a add cache key calculation and PlanCache
77892a4961a is described below

commit 77892a4961a6ad0da391aac8c9585c11c9c0ac7f
Author: YangCaiyin <[email protected]>
AuthorDate: Thu Feb 20 13:40:43 2025 +0800

    add cache key calculation and PlanCache
---
 .../relational/planner/TableLogicalPlanner.java    | 32 ++++++++++++++--------
 .../plan/relational/planner/PlanCacheTest.java     |  4 ++-
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/TableLogicalPlanner.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/TableLogicalPlanner.java
index ebfcf0268f5..0e2c877ae5b 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/TableLogicalPlanner.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/TableLogicalPlanner.java
@@ -68,6 +68,7 @@ import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Statement;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Table;
 import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.Update;
 import 
org.apache.iotdb.db.queryengine.plan.relational.sql.ast.WrappedStatement;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.util.SqlFormatter;
 import 
org.apache.iotdb.db.queryengine.plan.relational.type.InternalTypeManager;
 
 import com.google.common.collect.ImmutableList;
@@ -80,6 +81,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
@@ -100,6 +102,7 @@ public class TableLogicalPlanner {
   private final Metadata metadata;
   private final WarningCollector warningCollector;
   private final Logger logger = 
LoggerFactory.getLogger(TableLogicalPlanner.class);
+  private final HashMap<String, PlanNode> planCache = new HashMap<String, 
PlanNode>();
 
   @TestOnly
   public TableLogicalPlanner(
@@ -138,24 +141,29 @@ public class TableLogicalPlanner {
     literalMarkerReplacer.process(query);
   }
 
+  private String calculateCacheKey(Statement statement, Analysis analysis) {
+    StringBuilder sb = new StringBuilder();
+    sb.append(analysis.getDatabaseName());
+    sb.append(SqlFormatter.formatSql(statement));
+    sb.append(queryContext.getZoneId());
+    return sb.toString();
+  }
+
   public LogicalQueryPlan plan(final Analysis analysis) {
     long startTime = System.nanoTime();
     Statement statement = analysis.getStatement();
 
     // Try to use plan cache
     // We should check if statement is Query in enablePlanCache() method
-    //    if(analysis.enablePlanCache()){
-    //      generalizeStatement((Query) statement);
-    //
-    //      String cachedKey = calculateCacheKey(generalizedStatement, 
analysis);
-    //      PlanNode cachedPlan = queryContext.getPlanCache().get(cachedKey);
-    //      if (cachedPlan != null) {
-    //          // deal with the device stuff
-    //          return new LogicalQueryPlan(queryContext, cachedPlan);
-    //      }
-    //      // Following implementation of plan should be based on the 
generalizedStatement
-    //      statement = generalizedStatement;
-    //    }
+    generalizeStatement((Query) statement);
+
+    String cachedKey = calculateCacheKey(statement, analysis);
+    PlanNode cachedPlan = planCache.get(cachedKey);
+    if (cachedPlan != null) {
+      // deal with the device stuff
+      return new LogicalQueryPlan(queryContext, cachedPlan);
+    }
+    // Following implementation of plan should be based on the 
generalizedStatement
 
     PlanNode planNode = planStatement(analysis, statement);
 
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/PlanCacheTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/PlanCacheTest.java
index 4e160814b2e..de66f18925f 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/PlanCacheTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/PlanCacheTest.java
@@ -40,7 +40,8 @@ public class PlanCacheTest {
     Mockito.when(clientSession.getDatabaseName()).thenReturn(databaseName);
 
     SqlParser sqlParser = new SqlParser();
-    String sql = "select id + 1 from table1 where id > 10 and time=13289078 
order by rank+1";
+    String sql =
+        "select id + 1 from table1 where id > 10 and time=13289078 and 
deviceId = 'test' order by rank+1";
     long startTime = System.nanoTime();
     Statement originalStatement =
         sqlParser.createStatement(sql, ZoneId.systemDefault(), clientSession);
@@ -53,5 +54,6 @@ public class PlanCacheTest {
     String newSql = SqlFormatter.formatSql(originalStatement);
     System.out.println("Time to replace: " + (System.nanoTime() - startTime));
     System.out.println(newSql);
+    System.out.println(literalMarkerReplacer.getLiteralList());
   }
 }

Reply via email to