korlov42 commented on code in PR #843: URL: https://github.com/apache/ignite-3/pull/843#discussion_r893128911
########## modules/api/src/main/java/org/apache/ignite/sql/SqlBatchException.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.sql; + +import org.apache.ignite.internal.util.ArrayUtils; + +/** + * The subclass of {@link SqlException} thrown when an error occurs during a batch update operation. In addition to the + * information provided by {@link SqlException}, a <code>SqlBatchException</code> provides the update + * counts for all commands that were executed successfully during the batch update, that is, + * all commands that were executed before the error occurred. The order of elements in an array of update counts + * corresponds to the order in which commands were added to the batch. + * + */ +public class SqlBatchException extends SqlException { + private final long[] updCntrs; + + /** + * Creates a new grid exception with the given throwable as a cause and source of error message. Review Comment: let's omit the word `grid` here and below BTW, this constructor doesn't accept throwable ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/SessionImpl.java: ########## @@ -215,13 +220,68 @@ public CompletableFuture<AsyncResultSet> executeAsync( /** {@inheritDoc} */ @Override - public CompletableFuture<int[]> executeBatchAsync(@Nullable Transaction transaction, String query, BatchedArguments batch) { - throw new UnsupportedOperationException("Not implemented yet."); + public CompletableFuture<long[]> executeBatchAsync(@Nullable Transaction transaction, String query, BatchedArguments batch) { + if (!busyLock.enterBusy()) { + return CompletableFuture.failedFuture(new SqlException("Session is closed.")); + } + + try { + QueryContext ctx = QueryContext.of( + transaction, + new QueryTimeout(timeout, TimeUnit.NANOSECONDS), + (QueryValidator) plan -> { + if (plan.type() != Type.DML) { + throw new SqlException("Invalid SQL statement type in the batch [plan=" + plan + ']'); + } + } + ); + + final var counters = new LongArrayList(batch.size()); + CompletableFuture<Void> tail = CompletableFuture.completedFuture(null); + ArrayList<CompletableFuture<Void>> batchFuts = new ArrayList<>(batch.size()); + + for (int i = 0; i < batch.size(); ++i) { + Object[] args = batch.get(i).toArray(); + + tail = tail.thenCompose(v -> qryProc.querySingleAsync(ctx, schema, query, args)) + .thenCompose(cur -> cur.requestNextAsync(1)) + .thenAccept(page -> { + validateDmlResult(page); + + counters.add((long) page.items().get(0).get(0)); + }) + .whenComplete((v, ex) -> { + if (ex instanceof CancellationException) { + // TODO Review Comment: todo without ticket number -- 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]
