You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/05/25 16:24:49 UTC

[GitHub] [ignite-3] korlov42 commented on a diff in pull request #817: IGNITE-16964 SQL API: Implement async SQL API

korlov42 commented on code in PR #817:
URL: https://github.com/apache/ignite-3/pull/817#discussion_r881655686


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {

Review Comment:
   ```suggestion
           if (cur.queryType() != SqlQueryType.DML) {
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {

Review Comment:
   ```suggestion
           if (cur.queryType() != SqlQueryType.DDL) {
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Boolean
+                && !batchPage.hasMore() : "Invalid DDL result: " + batchPage;
+
+        return (boolean) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        if (!hasRowSet()) {
+            throw new NoRowSetExpectedException("Query hasn't result set: [type=" + cur.queryType() + ']');
+        }
+
+        return page;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<? extends AsyncResultSet> fetchNextPage() {
+        if (next == null) {
+            synchronized (mux) {
+                if (next == null) {
+                    if (!hasMorePages()) {
+                        next = HAS_NO_MORE_PAGE_FUTURE;
+                    } else {
+                        next = cur.requestNextAsync(pageSize)
+                                .thenApply(batchRes -> new AsyncResultSetImpl(cur, batchRes, pageSize, closeRun));
+                    }
+                }
+            }
+        }
+
+        return next;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasMorePages() {
+        return batchPage.hasMore();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<Void> closeAsync() {
+        return cur.closeAsync().thenAccept((v) -> closeRun.run());
+    }
+
+    private class Page implements Iterable<SqlRow> {
+        /** {@inheritDoc} */
+        @NotNull
+        @Override
+        public Iterator<SqlRow> iterator() {
+            return new IteratorImpl(batchPage.items().iterator());
+        }
+    }
+
+    private class IteratorImpl implements Iterator<SqlRow> {
+        private final Iterator<List<Object>> it;
+
+        IteratorImpl(Iterator<List<Object>> it) {
+            this.it = it;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return it.hasNext();
+        }
+
+        @Override
+        public SqlRow next() {
+            return new SqlRowImpl(it.next());
+        }
+    }
+
+    private class SqlRowImpl implements SqlRow {
+        private final List<Object> row;
+
+        SqlRowImpl(List<Object> row) {
+            this.row = row;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnCount() {
+            return cur.metadata().fields().size();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public String columnName(int columnIndex) {
+            return cur.metadata().fields().get(columnIndex).name();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnIndex(@NotNull String columnName) {
+            return cur.metadata().fields().stream()
+                    .filter(fld -> fld.name().equals(columnName))
+                    .findFirst()
+                    .get()
+                    .order();
+        }
+
+        /** {@inheritDoc} */
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T valueOrDefault(@NotNull String columnName, T defaultValue) {
+            T ret = (T) row.get(columnIndex(columnName));

Review Comment:
   with that in mind it totally make sense to prepare meta...



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1

Review Comment:
   the same about validation 



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Boolean
+                && !batchPage.hasMore() : "Invalid DDL result: " + batchPage;
+
+        return (boolean) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        if (!hasRowSet()) {
+            throw new NoRowSetExpectedException("Query hasn't result set: [type=" + cur.queryType() + ']');
+        }
+
+        return page;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<? extends AsyncResultSet> fetchNextPage() {
+        if (next == null) {
+            synchronized (mux) {
+                if (next == null) {
+                    if (!hasMorePages()) {
+                        next = HAS_NO_MORE_PAGE_FUTURE;
+                    } else {
+                        next = cur.requestNextAsync(pageSize)
+                                .thenApply(batchRes -> new AsyncResultSetImpl(cur, batchRes, pageSize, closeRun));
+                    }
+                }
+            }
+        }
+
+        return next;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasMorePages() {
+        return batchPage.hasMore();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<Void> closeAsync() {
+        return cur.closeAsync().thenAccept((v) -> closeRun.run());

Review Comment:
   ```suggestion
           return cur.closeAsync().thenRun(closeRun);
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Boolean
+                && !batchPage.hasMore() : "Invalid DDL result: " + batchPage;
+
+        return (boolean) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        if (!hasRowSet()) {
+            throw new NoRowSetExpectedException("Query hasn't result set: [type=" + cur.queryType() + ']');
+        }
+
+        return page;

Review Comment:
   ```suggestion
           return () -> new TransformingIterator<>(batchPage.items().iterator(), SqlRowImpl::new);
   ```
   
   and after that we don't need neither Page nor IteratorImpl classes



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Boolean
+                && !batchPage.hasMore() : "Invalid DDL result: " + batchPage;
+
+        return (boolean) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        if (!hasRowSet()) {
+            throw new NoRowSetExpectedException("Query hasn't result set: [type=" + cur.queryType() + ']');
+        }
+
+        return page;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<? extends AsyncResultSet> fetchNextPage() {
+        if (next == null) {
+            synchronized (mux) {
+                if (next == null) {
+                    if (!hasMorePages()) {
+                        next = HAS_NO_MORE_PAGE_FUTURE;
+                    } else {
+                        next = cur.requestNextAsync(pageSize)
+                                .thenApply(batchRes -> new AsyncResultSetImpl(cur, batchRes, pageSize, closeRun));
+                    }
+                }
+            }
+        }
+
+        return next;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasMorePages() {
+        return batchPage.hasMore();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<Void> closeAsync() {
+        return cur.closeAsync().thenAccept((v) -> closeRun.run());
+    }
+
+    private class Page implements Iterable<SqlRow> {
+        /** {@inheritDoc} */
+        @NotNull
+        @Override
+        public Iterator<SqlRow> iterator() {
+            return new IteratorImpl(batchPage.items().iterator());
+        }
+    }
+
+    private class IteratorImpl implements Iterator<SqlRow> {
+        private final Iterator<List<Object>> it;
+
+        IteratorImpl(Iterator<List<Object>> it) {
+            this.it = it;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return it.hasNext();
+        }
+
+        @Override
+        public SqlRow next() {
+            return new SqlRowImpl(it.next());
+        }
+    }
+
+    private class SqlRowImpl implements SqlRow {
+        private final List<Object> row;
+
+        SqlRowImpl(List<Object> row) {
+            this.row = row;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnCount() {
+            return cur.metadata().fields().size();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public String columnName(int columnIndex) {
+            return cur.metadata().fields().get(columnIndex).name();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnIndex(@NotNull String columnName) {
+            return cur.metadata().fields().stream()

Review Comment:
   does it make sense to prepare metadata once for the whole result and then just pass it to the row?
   
   BTW, what if the column with the given name is not exists in the row?



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,424 @@
+/*
+ * 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.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1

Review Comment:
   I believe, such a validation is better to be done as soon as possible, thus let's move it to the constructor



-- 
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