You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "lowka (via GitHub)" <gi...@apache.org> on 2023/06/26 06:46:09 UTC

[GitHub] [ignite-3] lowka commented on a diff in pull request #2226: IGNITE-17765 Sql. Introduce cache for parsed statements

lowka commented on code in PR #2226:
URL: https://github.com/apache/ignite-3/pull/2226#discussion_r1241659499


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java:
##########
@@ -141,8 +164,59 @@ public void stop() throws Exception {
         planningPool.shutdownNow();
     }
 
+    /** Drop cached query plans. */
+    public void invalidateCachedPlans() {
+        planCache.clear();
+    }
+
+    @TestOnly
+    public void invalidateParserCache() {
+        normalizedQueryCache.clear();
+    }
+
+    @TestOnly
+    public int planCacheSize() {
+        return planCache.size();
+    }
+
+    @TestOnly
+    public int parseCacheSize() {
+        return normalizedQueryCache.size();
+    }
+
     /** {@inheritDoc} */
     @Override
+    public CompletableFuture<QueryPlan> prepareAsync(String query, QueryContext queryContext, BaseQueryContext ctx) {
+        CacheKey cacheKey = createCacheKey(query, ctx);
+
+        String normalizedQuery = normalizedQueryCache.get(cacheKey);
+
+        if (normalizedQuery == null) {
+            SqlNode sqlNode;
+            try {
+                sqlNode = parse(query, queryContext, ctx);
+            } catch (Exception ex) {
+                return failedFuture(ex);
+            }
+
+            if (skipCache(sqlNode)) {
+                return prepareAsync(sqlNode, ctx);
+            }
+
+            normalizedQueryCache.putIfAbsent(cacheKey, sqlNode.toString());
+
+            return planCache.computeIfAbsent(createCacheKey(sqlNode.toString(), ctx), k -> prepareAsync(sqlNode, ctx))
+                    .thenApply(QueryPlan::copy);
+        }
+
+        return planCache.computeIfAbsent(createCacheKey(normalizedQuery, ctx), k ->
+                        supplyAsync(() -> parse(query, queryContext, ctx)).thenCompose(sqlNode -> prepareAsync(sqlNode, ctx)))

Review Comment:
   Here `supplyAsync` is called w/o an executor so it uses common ForkJoinPool. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org