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


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PrepareServiceImpl.java:
##########
@@ -238,26 +240,90 @@ public CompletableFuture<QueryPlan> prepareAsync(
                 .parameters(Commons.arrayToMap(operationContext.parameters()))
                 .build();
 
-        result = prepareAsync0(parsedResult, planningContext);
+        QueryTransactionContext txContext = operationContext.txContext();
+
+        // Validate statement
+        CompletableFuture<ValidStatement> validFut = 
validateStatement(parsedResult, planningContext);
+
+        // Try optimize to fast
+        CompletableFuture<FastOptimizationResult> plannedOrValidated =
+                validFut.thenApply(stmt -> tryOptimizeFast(stmt, 
planningContext, txContext));
+
+        result = plannedOrValidated.thenCompose(optResult -> {
+            ValidStatement stmt = optResult.stmt;
+            QueryPlan plan = optResult.plan;
+
+            // If optimize fast returned a plan, return it.
+            if (plan != null) {
+                return CompletableFuture.completedFuture(plan);
+            } else {
+                // Otherwise, continue with the regular planning.
+
+                return prepareAsync0(stmt, planningContext, txContext);
+            }
+        });
 
         return result.exceptionally(ex -> {
-                    Throwable th = ExceptionUtils.unwrapCause(ex);
+            Throwable th = ExceptionUtils.unwrapCause(ex);
 
-                    throw new 
CompletionException(SqlExceptionMapperUtil.mapToPublicSqlException(th));
-                }
-        );
+            throw new 
CompletionException(SqlExceptionMapperUtil.mapToPublicSqlException(th));
+        });
+    }
+
+    private FastOptimizationResult tryOptimizeFast(
+            ValidStatement stmt,
+            PlanningContext planningContext,
+            @Nullable QueryTransactionContext txContext
+    ) {
+        // If fast query optimization is disabled, then proceed with the 
regular planning.
+        if (!fastQueryOptimizationEnabled()) {
+            return new FastOptimizationResult(stmt, null);
+        }
+
+        IgniteRel fastOptRel = 
PlannerHelper.tryOptimizeSelectCount(planningContext.planner(), txContext, 
stmt.value.sqlNode());
+        if (fastOptRel == null) {
+            return new FastOptimizationResult(stmt, null);

Review Comment:
   Please take a look at how explain works. 



-- 
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-unsubscr...@ignite.apache.org

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

Reply via email to