/* * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) * * Licensed 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. * * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) * SPDX-License-Identifier: Apache-2.0 */ /* Generated By:JJTree: Do not edit this line. OExplainStatement.java Version 4.3 */ /* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=O,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_USERTYPE_VISIBILITY_PUBLIC=true */ package com.arcadedb.query.sql.parser; import com.arcadedb.database.Database; import com.arcadedb.exception.CommandExecutionException; import com.arcadedb.query.OperationType; import com.arcadedb.query.sql.executor.BasicCommandContext; import com.arcadedb.query.sql.executor.CommandContext; import com.arcadedb.query.sql.executor.ExecutionPlan; import com.arcadedb.query.sql.executor.InternalExecutionPlan; import com.arcadedb.query.sql.executor.ResultSet; import com.arcadedb.query.sql.executor.UpdateExecutionPlan; import java.util.Map; import java.util.Objects; import java.util.Set; public class ProfileStatement extends Statement { public Statement statement; public ProfileStatement() { } @Override public void toString(final Map params, final StringBuilder builder) { builder.append("PROFILE "); statement.toString(params, builder); } @Override public ResultSet execute(final Database db, final Object[] args, final CommandContext parentcontext, final boolean usePlanCache) { final BasicCommandContext context = new BasicCommandContext(); if (parentcontext != null) context.setParentWithoutOverridingChild(parentcontext); context.setDatabase(db); context.setInputParameters(args); context.setProfiling(true); final ExecutionPlan executionPlan = statement.createExecutionPlan(context); if (executionPlan instanceof UpdateExecutionPlan plan) plan.executeInternal(); return profile(new LocalResultSet((InternalExecutionPlan) executionPlan)); } /** Drains the rows, then the facets (a LIMIT stops the rows before every match is collected), and returns the profiled plan. */ private ExplainResultSet profile(final LocalResultSet rs) { while (rs.hasNext()) rs.next(); rs.getFacets(); final ExplainResultSet result = new ExplainResultSet( rs.getExecutionPlan().orElseThrow(() -> new CommandExecutionException("Cannot profile command: " + statement))); rs.close(); return result; } @Override public ResultSet execute(final Database db, final Map params, final CommandContext parentcontext, final boolean usePlanCache) { final BasicCommandContext context = new BasicCommandContext(); if (parentcontext != null) context.setParentWithoutOverridingChild(parentcontext); context.setDatabase(db); context.setInputParameters(params); context.setProfiling(true); final ExecutionPlan executionPlan = statement.createExecutionPlan(context); return profile(new LocalResultSet((InternalExecutionPlan) executionPlan)); } @Override public InternalExecutionPlan createExecutionPlan(final CommandContext context) { return statement.createExecutionPlan(context); } @Override public ProfileStatement copy() { final ProfileStatement result = new ProfileStatement(); result.statement = statement == null ? null : statement.copy(); return result; } @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final ProfileStatement that = (ProfileStatement) o; return Objects.equals(statement, that.statement); } @Override public int hashCode() { return statement != null ? statement.hashCode() : 0; } /** * PROFILE executes the wrapped statement, so it is exactly what that statement is (issue #9313): it writes nothing * itself and has no operation of its own, and the wrapped statement - nested in it - contributes its own (issue * #9628). EXPLAIN, which only plans, is the one that stays idempotent whatever it wraps. */ @Override protected boolean isIdempotentItself() { return true; } @Override protected Set getOperationTypesItself() { return Set.of(); } /** * Delegates to the wrapped statement. Same gap and same fix as * {@link ExplainStatement#refersToParent()}: without this override, {@code * LET $x = (PROFILE )} inside a SELECT's LET clause throws * {@code UnsupportedOperationException} out of {@code Statement.refersToParent()}'s default. */ @Override public boolean refersToParent() { return statement.refersToParent(); } /** PROFILE runs the wrapped statement, so a wrapped BEGIN, COMMIT or ROLLBACK is a transaction control (issue #12202). */ @Override public boolean isTransactionControl() { return statement.isTransactionControl(); } } /* JavaCC - OriginalChecksum=9fdd24510993cbee32e38a51c838bdb4 (do not edit this line) */