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


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,410 @@
+/*
+ * 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.Map;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import java.util.stream.Collectors;
+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.ResultFieldMetadata;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.internal.sql.engine.util.TransformingIterator;
+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 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;
+
+        assert cur.queryType() == SqlQueryType.QUERY
+                || ((cur.queryType() == SqlQueryType.DML || cur.queryType() == 
SqlQueryType.DDL)
+                && batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && !batchPage.hasMore()) : "Invalid query result: [type=" + 
cur.queryType() + "res=" + batchPage + ']';
+    }
+
+    /** {@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 (cur.queryType() != SqlQueryType.DML) {
+            return -1;
+        }
+
+        assert batchPage.items().get(0).get(0) instanceof Long : "Invalid DML 
result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (cur.queryType() != SqlQueryType.DDL) {
+            return false;
+        }
+
+        assert batchPage.items().get(0).get(0) instanceof Boolean : "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 () -> new TransformingIterator<>(batchPage.items().iterator(), 
SqlRowImpl::new);
+    }
+
+    /** {@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().thenRun(closeRun);
+    }
+
+    private class SqlRowImpl implements SqlRow {
+        private final List<Object> row;
+
+        private final Map<String, Integer> fields;
+
+        org.apache.ignite.internal.sql.engine.ResultSetMetadata meta = 
cur.metadata();
+
+        SqlRowImpl(List<Object> row) {
+            this.row = row;
+            fields = meta.fields().stream()
+                    .collect(Collectors.toMap(ResultFieldMetadata::name, 
ResultFieldMetadata::order));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnCount() {
+            return meta.fields().size();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public String columnName(int columnIndex) {
+            return meta.fields().get(columnIndex).name();
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int columnIndex(@NotNull String columnName) {
+            return fields.getOrDefault(columnName, -1);
+        }
+
+        private int columnIndexChecked(@NotNull String columnName) {
+            int idx = columnIndex(columnName);
+
+            if (idx == -1) {
+                throw new IllegalArgumentException("Column doesn't exist 
[name=" + columnName + ']');
+            }
+
+            return idx;
+        }
+
+        /** {@inheritDoc} */
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T valueOrDefault(@NotNull String columnName, T 
defaultValue) {
+            T ret = (T) row.get(columnIndexChecked(columnName));
+
+            return ret != null ? ret : defaultValue;
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public Tuple set(@NotNull String columnName, Object value) {
+            throw new UnsupportedOperationException("Operation not 
supported.");
+        }
+
+        /** {@inheritDoc} */
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T value(@NotNull String columnName) throws 
IllegalArgumentException {
+            return (T) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T value(int columnIndex) {
+            return (T) row.get(columnIndex);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public BinaryObject binaryObjectValue(@NotNull String columnName) {
+            return (BinaryObject) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public BinaryObject binaryObjectValue(int columnIndex) {
+            return (BinaryObject) row.get(columnIndex);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public byte byteValue(@NotNull String columnName) {
+            return (byte) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public byte byteValue(int columnIndex) {
+            return (byte) row.get(columnIndex);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public short shortValue(@NotNull String columnName) {
+            return (short) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public short shortValue(int columnIndex) {
+            return (short) row.get(columnIndex);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int intValue(@NotNull String columnName) {
+            return (int) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public int intValue(int columnIndex) {
+            return (int) row.get(columnIndex);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public long longValue(@NotNull String columnName) {
+            return (long) row.get(columnIndexChecked(columnName));
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public long longValue(int columnIndex) {
+            return 0;

Review Comment:
   did you miss this one? 



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