korlov42 commented on code in PR #4653: URL: https://github.com/apache/ignite-3/pull/4653#discussion_r1824304877
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/fsm/ParsingPhaseHandler.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.exec.fsm; + +import static org.apache.ignite.internal.sql.engine.QueryProperty.ALLOWED_QUERY_TYPES; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.sql.engine.SqlQueryType; +import org.apache.ignite.internal.sql.engine.property.SqlProperties; +import org.apache.ignite.internal.sql.engine.sql.ParsedResult; + +/** Parses the query string and populate {@link Query query state} with results. */ +class ParsingPhaseHandler implements ExecutionPhaseHandler { + static final ExecutionPhaseHandler INSTANCE = new ParsingPhaseHandler(); + + private ParsingPhaseHandler() { } + + @Override + public Result handle(Query query) { + ParsedResult parsedResult = query.executor.lookupParsedResultInCache(query.sql); + + if (parsedResult != null) { + query.parsedResult = parsedResult; + + query.moveTo(ExecutionPhase.OPTIMIZING); + + return Result.proceedImmediately(); + } + + CompletableFuture<Void> awaitFuture = new CompletableFuture<>(); + query.executor.execute(() -> { + try { + ParsedResult result; + if (multiStatementQueryAllowed(query.properties)) { + List<ParsedResult> results = query.executor.parseScript(query.sql); + + if (results.size() != 1 || results.get(0).queryType() == SqlQueryType.TX_CONTROL) { + // the query is script indeed, need different execution path + query.parsedScript = results; + + query.moveTo(ExecutionPhase.SCRIPT_INITIALIZATION); + awaitFuture.complete(null); + + return; + } + + result = results.get(0); + } else { + result = query.executor.parse(query.sql); + } + + if (shouldBeCached(result.queryType())) { + query.executor.updateParsedResultCache(query.sql, result); + } + + query.parsedResult = result; + + query.moveTo(ExecutionPhase.OPTIMIZING); + awaitFuture.complete(null); + } catch (Throwable th) { + awaitFuture.completeExceptionally(th); + } + }); + + return Result.proceedAfter(awaitFuture); + } + + private static boolean shouldBeCached(SqlQueryType queryType) { + return queryType == SqlQueryType.QUERY || queryType == SqlQueryType.DML; + } + + /** Returns {@code true} if the specified properties allow multi-statement query execution. */ + private static boolean multiStatementQueryAllowed(SqlProperties properties) { Review Comment: good catch! Replaced ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/fsm/OptimizingPhaseHandler.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.exec.fsm; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.lang.ErrorGroups.Sql.RUNTIME_ERR; +import static org.apache.ignite.lang.ErrorGroups.Sql.STMT_VALIDATION_ERR; + +import java.time.ZoneId; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.sql.engine.QueryProperty; +import org.apache.ignite.internal.sql.engine.SqlOperationContext; +import org.apache.ignite.internal.sql.engine.SqlQueryProcessor.PrefetchCallback; +import org.apache.ignite.internal.sql.engine.SqlQueryType; +import org.apache.ignite.internal.sql.engine.prepare.PrepareService; +import org.apache.ignite.internal.sql.engine.property.SqlProperties; +import org.apache.ignite.internal.sql.engine.sql.ParsedResult; +import org.apache.ignite.internal.sql.engine.tx.QueryTransactionContext; +import org.apache.ignite.internal.sql.engine.tx.QueryTransactionWrapper; +import org.apache.ignite.internal.sql.engine.util.TypeUtils; +import org.apache.ignite.sql.SqlException; + +/** Validates parsed AST acquired on the previous phase and submit optimization task to {@link PrepareService}. */ +class OptimizingPhaseHandler implements ExecutionPhaseHandler { + static final ExecutionPhaseHandler INSTANCE = new OptimizingPhaseHandler(); + + private OptimizingPhaseHandler() { + } + + @Override + public Result handle(Query query) { + ParsedResult result = query.parsedResult; + + assert result != null : "Query is expected to be parsed at this phase"; + + validateParsedStatement(query.properties, result); + validateDynamicParameters(result.dynamicParamsCount(), query.params, true); + ensureStatementMatchesTx(result.queryType(), query.txContext); + + HybridTimestamp operationTime = query.executor.deriveOperationTime(query.txContext); + + String schemaName = query.properties.get(QueryProperty.DEFAULT_SCHEMA); + ZoneId timeZoneId = query.properties.get(QueryProperty.TIME_ZONE_ID); + + SqlOperationContext operationContext = SqlOperationContext.builder() + .queryId(query.id) + .cancel(query.cancel) + .prefetchCallback(new PrefetchCallback()) + .parameters(query.params) + .timeZoneId(timeZoneId) + .defaultSchemaName(schemaName) + .operationTime(operationTime) + .txContext(query.txContext) + .txUsedListener(tx -> query.usedTransaction = tx) + .build(); + + query.operationContext = operationContext; + + CompletableFuture<Void> awaitFuture = query.executor.waitForMetadata(operationTime) + .thenCompose(none -> query.executor.prepare(result, operationContext) + .thenAccept(plan -> { + if (query.txContext.explicitTx() == null) { + // in case of implicit tx we have to update observable time to prevent tx manager to start + // implicit transaction too much in the past where version of catalog we used to prepare the + // plan was not yet available + query.txContext.updateObservableTime(query.executor.deriveMinimalRequiredTime(plan)); + } + + query.plan = plan; + + query.moveTo(ExecutionPhase.CURSOR_INITIALIZATION); + })); + + return Result.proceedAfter(awaitFuture); + } + + /** Performs additional validation of a parsed statement. **/ + private static void validateParsedStatement( + SqlProperties properties, + ParsedResult parsedResult + ) { + Set<SqlQueryType> allowedTypes = properties.get(QueryProperty.ALLOWED_QUERY_TYPES); + SqlQueryType queryType = parsedResult.queryType(); + + if (parsedResult.queryType() == SqlQueryType.TX_CONTROL) { + String message = "Transaction control statement can not be executed as an independent statement"; + + throw new SqlException(STMT_VALIDATION_ERR, message); + } + + if (!allowedTypes.contains(queryType)) { + String message = format("Invalid SQL statement type. Expected {} but got {}", allowedTypes, queryType); + + throw new SqlException(STMT_VALIDATION_ERR, message); + } + } + + private static void validateDynamicParameters(int expectedParamsCount, Object[] params, boolean exactMatch) throws SqlException { Review Comment: good catch! Moved all such validation to `ValidationHelper` in order to reuse it -- 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