korlov42 commented on a change in pull request #712:
URL: https://github.com/apache/ignite-3/pull/712#discussion_r834347828



##########
File path: 
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/AsyncSqlCursor.java
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine;
+
+import java.util.List;
+
+/**
+ * Sql query cursor.
+ *
+ * @param <T> Type of elements.
+ */
+public interface AsyncSqlCursor<T> extends AsyncCursor<T> {
+    /**
+     * Get query type.
+     */
+    SqlQueryType queryType();
+
+    /**
+     * Get column metadata.
+     */
+    default ResultSetMetadata metadata() {

Review comment:
       Yes. Now it's fixed

##########
File path: 
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/SqlQueryProcessor.java
##########
@@ -216,106 +214,62 @@ public synchronized void stop() throws Exception {
         }
     }
 
+    /**
+     * To be removed.
+     *
+     * @return Always return null.
+     */
     public QueryRegistry queryRegistry() {
-        return queryRegistry;
+        return null;
     }
 
-    private List<SqlCursor<List<?>>> query0(QueryContext context, String 
schemaName, String sql, Object... params) {
+    private List<CompletableFuture<AsyncSqlCursor<List<?>>>> 
query0(QueryContext context, String schemaName, String sql, Object... params) {
         SchemaPlus schema = schemaManager.schema(schemaName);
 
-        assert schema != null : "Schema not found: " + schemaName;
-
-        QueryPlan plan = planCache.queryPlan(new CacheKey(schema.getName(), 
sql));
-
-        if (plan != null) {
-            final QueryPlan finalPlan = plan;
-
-            context.maybeUnwrap(QueryValidator.class)
-                    .ifPresent(queryValidator -> 
queryValidator.validatePlan(finalPlan));
-
-            RootQuery<Object[]> qry = new RootQuery<>(
-                    sql,
-                    schema,
-                    params,
-                    exchangeService,
-                    (q) -> queryRegistry.unregister(q.id()),
-                    LOG
-            );
-
-            queryRegistry.register(qry);
-
-            try {
-                return Collections.singletonList(executionSrvc.executePlan(
-                        qry,
-                        plan
-                ));
-            } catch (Exception e) {
-                boolean isCanceled = qry.isCancelled();
-
-                qry.cancel();
-
-                queryRegistry.unregister(qry.id());
-
-                if (isCanceled) {
-                    throw new IgniteInternalException("The query was cancelled 
while planning", e);
-                } else {
-                    throw e;
-                }
-            }
+        if (schema == null) {
+            throw new IgniteInternalException(format("Schema not found 
[schemaName={}]", schemaName));
         }
 
-        SqlNodeList qryList = Commons.parse(sql, 
FRAMEWORK_CONFIG.getParserConfig());
-        List<SqlCursor<List<?>>> cursors = new ArrayList<>(qryList.size());
-
-        List<RootQuery<Object[]>> qrys = new ArrayList<>(qryList.size());
-
-        for (final SqlNode sqlNode : qryList) {
-            RootQuery<Object[]> qry = new RootQuery<>(
-                    sqlNode.toString(),
-                    schemaManager.schema(schemaName), // Update schema for 
each query in multiple statements.
-                    params,
-                    exchangeService,
-                    (q) -> queryRegistry.unregister(q.id()),
-                    LOG
-            );
-
-            qrys.add(qry);
+        SqlNodeList nodes = parsingCache.computeIfAbsent(sql, key -> 
Commons.parse(key, FRAMEWORK_CONFIG.getParserConfig()));
 
-            queryRegistry.register(qry);
+        List<CompletableFuture<AsyncSqlCursor<List<?>>>> res = new 
ArrayList<>(nodes.size());
 
-            try {
-                if (qryList.size() == 1) {
-                    plan = planCache.queryPlan(
-                            new CacheKey(schemaName, qry.sql()),
-                            () -> prepareSvc.prepareSingle(sqlNode, 
qry.planningContext()));
-                } else {
-                    plan = prepareSvc.prepareSingle(sqlNode, 
qry.planningContext());
-                }
+        CompletableFuture<Void> start = new CompletableFuture<>();
 
-                final QueryPlan finalPlan = plan;
+        for (SqlNode sqlNode : nodes) {
+            BaseQueryContext ctx = BaseQueryContext.builder()
+                    .cancel(new QueryCancel())
+                    .frameworkConfig(
+                            Frameworks.newConfigBuilder(FRAMEWORK_CONFIG)
+                                    .defaultSchema(schema)
+                                    .build()
+                    )
+                    .logger(LOG)
+                    .parameters(params)
+                    .build();
 
-                context.maybeUnwrap(QueryValidator.class)
-                        .ifPresent(queryValidator -> 
queryValidator.validatePlan(finalPlan));
+            CompletableFuture<AsyncSqlCursor<List<?>>> stage = 
start.thenCompose(voidArg -> prepareSvc.prepare(sqlNode, ctx))
+                    .thenApply(plan -> {
+                        context.maybeUnwrap(QueryValidator.class)
+                                .ifPresent(queryValidator -> 
queryValidator.validatePlan(plan));
 
-                cursors.add(executionSrvc.executePlan(qry, plan));
-            } catch (Exception e) {
-                boolean isCanceled = qry.isCancelled();
+                        return new AsyncSqlCursorImpl<>(
+                                SqlQueryType.mapPlanTypeToSqlType(plan.type()),
+                                executionSrvc.executePlan(plan, ctx)
+                        );
+                    });
 
-                qrys.forEach(RootQuery::cancel);
-
-                queryRegistry.unregister(qry.id());
-
-                if (isCanceled) {
-                    throw new IgniteInternalException("The query was cancelled 
while planning", e);
-                } else {
-                    throw e;
-                }
-            }
+            res.add(stage);
         }
 
-        return cursors;
+        // TODO: pass to a particular executor
+        start.completeAsync(() -> null);
+
+        return res;
     }
 
+    Map<String, SqlNodeList> parsingCache = new HashMap<>();

Review comment:
       done




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to