Search in sources :

Example 1 with AdHocPlannedStatement

use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.

the class ProcedureRunner method voltQueueSQL.

public void voltQueueSQL(final String sql, Object... args) {
    if (sql == null || sql.isEmpty()) {
        throw new IllegalArgumentException("SQL statement '" + sql + "' is null or the empty string");
    }
    try {
        AdHocPlannedStmtBatch batch = m_csp.plan(sql, args, m_isSinglePartition);
        if (m_isReadOnly && !batch.isReadOnly()) {
            throw new VoltAbortException("Attempted to queue DML adhoc sql '" + sql + "' from read only procedure");
        }
        assert (1 == batch.plannedStatements.size());
        QueuedSQL queuedSQL = new QueuedSQL();
        AdHocPlannedStatement plannedStatement = batch.plannedStatements.get(0);
        long aggFragId = ActivePlanRepository.loadOrAddRefPlanFragment(plannedStatement.core.aggregatorHash, plannedStatement.core.aggregatorFragment, sql);
        long collectorFragId = 0;
        if (plannedStatement.core.collectorFragment != null) {
            collectorFragId = ActivePlanRepository.loadOrAddRefPlanFragment(plannedStatement.core.collectorHash, plannedStatement.core.collectorFragment, sql);
        }
        queuedSQL.stmt = SQLStmtAdHocHelper.createWithPlan(plannedStatement.sql, aggFragId, plannedStatement.core.aggregatorHash, true, collectorFragId, plannedStatement.core.collectorHash, true, plannedStatement.core.isReplicatedTableDML, plannedStatement.core.readOnly, plannedStatement.core.parameterTypes, m_site);
        Object[] argumentParams = args;
        // supporting @AdHocSpForTest with queries that contain '?' parameters.
        if (plannedStatement.hasExtractedParams()) {
            if (args.length > 0) {
                throw new VoltAbortException("Number of arguments provided was " + args.length + " where 0 were expected for statement: " + sql);
            }
            argumentParams = plannedStatement.extractedParamArray();
            if (argumentParams.length != queuedSQL.stmt.statementParamTypes.length) {
                String msg = String.format("The wrong number of arguments (" + argumentParams.length + " vs. the " + queuedSQL.stmt.statementParamTypes.length + " expected) were passed for the parameterized statement: %s", sql);
                throw new VoltAbortException(msg);
            }
        }
        queuedSQL.params = getCleanParams(queuedSQL.stmt, false, argumentParams);
        m_batch.add(queuedSQL);
    } catch (Exception e) {
        if (e instanceof ExecutionException) {
            throw new VoltAbortException(e.getCause());
        }
        if (e instanceof VoltAbortException) {
            throw (VoltAbortException) e;
        }
        throw new VoltAbortException(e);
    }
}
Also used : AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) ExecutionException(java.util.concurrent.ExecutionException) AdHocPlannedStmtBatch(org.voltdb.compiler.AdHocPlannedStmtBatch) SerializableException(org.voltdb.exceptions.SerializableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EEException(org.voltdb.exceptions.EEException) SpecifiedException(org.voltdb.exceptions.SpecifiedException) VoltAbortException(org.voltdb.VoltProcedure.VoltAbortException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) VoltAbortException(org.voltdb.VoltProcedure.VoltAbortException)

Example 2 with AdHocPlannedStatement

use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.

the class TestAdHocPlans method runQueryTest.

/**
     * For planner-only testing, most of the args are ignored.
     */
@Override
public int runQueryTest(String query, int hash, int spPartialSoFar, int expected, int validatingSPresult) throws IOException, NoConnectionsException, ProcCallException {
    AdHocPlannedStatement result = m_pt.planSqlForTest(query);
    boolean spPlan = result.toString().contains("ALL: null");
    if ((validatingSPresult == VALIDATING_SP_RESULT) != spPlan) {
        System.out.println("Missed: " + query);
        System.out.println(result);
        // for a do-over after getting an unexpected result.
        if (m_debugging_set_this_to_retry_failures) {
            result = m_pt.planSqlForTest(query);
        }
    }
    assertEquals((validatingSPresult == VALIDATING_SP_RESULT), spPlan);
    return 0;
}
Also used : AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement)

Example 3 with AdHocPlannedStatement

use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.

the class AdHocNTBase method processExplainDefaultProc.

/**
     * Explain Proc for a default proc is routed through the regular Explain
     * path using ad hoc planning and all. Take the result from that async
     * process and format it like other explains for procedures.
     */
static CompletableFuture<ClientResponse> processExplainDefaultProc(AdHocPlannedStmtBatch planBatch) {
    Database db = VoltDB.instance().getCatalogContext().database;
    // from a default procedure
    assert (planBatch.getPlannedStatementCount() == 1);
    AdHocPlannedStatement ahps = planBatch.getPlannedStatement(0);
    String sql = new String(ahps.sql, StandardCharsets.UTF_8);
    String explain = planBatch.explainStatement(0, db);
    VoltTable vt = new VoltTable(new VoltTable.ColumnInfo("SQL_STATEMENT", VoltType.STRING), new VoltTable.ColumnInfo("EXECUTION_PLAN", VoltType.STRING));
    vt.addRow(sql, explain);
    ClientResponseImpl response = new ClientResponseImpl(ClientResponseImpl.SUCCESS, ClientResponse.UNINITIALIZED_APP_STATUS_CODE, null, new VoltTable[] { vt }, null);
    CompletableFuture<ClientResponse> fut = new CompletableFuture<>();
    fut.complete(response);
    return fut;
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) CompletableFuture(java.util.concurrent.CompletableFuture) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) Database(org.voltdb.catalog.Database) ClientResponseImpl(org.voltdb.ClientResponseImpl) VoltTable(org.voltdb.VoltTable)

Example 4 with AdHocPlannedStatement

use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.

the class AdHocNTBase method runNonDDLAdHoc.

/**
     * Plan and execute a batch of DML/DQL sql. Any DDL has been filtered out at this point.
     */
protected CompletableFuture<ClientResponse> runNonDDLAdHoc(CatalogContext context, List<String> sqlStatements, boolean inferPartitioning, Object userPartitionKey, ExplainMode explainMode, boolean isSwapTables, Object[] userParamSet) {
    // catch races vs. updateApplicationCatalog.
    if (context == null) {
        context = VoltDB.instance().getCatalogContext();
    }
    List<String> errorMsgs = new ArrayList<>();
    List<AdHocPlannedStatement> stmts = new ArrayList<>();
    int partitionParamIndex = -1;
    VoltType partitionParamType = null;
    Object partitionParamValue = null;
    assert (sqlStatements != null);
    boolean inferSP = (sqlStatements.size() == 1) && inferPartitioning;
    if (userParamSet != null && userParamSet.length > 0) {
        if (sqlStatements.size() != 1) {
            return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, AdHocErrorResponseMessage);
        }
    }
    for (final String sqlStatement : sqlStatements) {
        try {
            AdHocPlannedStatement result = compileAdHocSQL(context, sqlStatement, inferSP, userPartitionKey, explainMode, isSwapTables, userParamSet);
            // and generated a partition parameter.
            if (inferSP) {
                partitionParamIndex = result.getPartitioningParameterIndex();
                partitionParamType = result.getPartitioningParameterType();
                partitionParamValue = result.getPartitioningParameterValue();
            }
            stmts.add(result);
        } catch (AdHocPlanningException e) {
            errorMsgs.add(e.getMessage());
        }
    }
    if (!errorMsgs.isEmpty()) {
        String errorSummary = StringUtils.join(errorMsgs, "\n");
        return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, errorSummary);
    }
    AdHocPlannedStmtBatch plannedStmtBatch = new AdHocPlannedStmtBatch(userParamSet, stmts, partitionParamIndex, partitionParamType, partitionParamValue, userPartitionKey == null ? null : new Object[] { userPartitionKey });
    if (adhocLog.isDebugEnabled()) {
        logBatch(context, plannedStmtBatch, userParamSet);
    }
    final VoltTrace.TraceEventBatch traceLog = VoltTrace.log(VoltTrace.Category.CI);
    if (traceLog != null) {
        traceLog.add(() -> VoltTrace.endAsync("planadhoc", getClientHandle()));
    }
    if (explainMode == ExplainMode.EXPLAIN_ADHOC) {
        return processExplainPlannedStmtBatch(plannedStmtBatch);
    } else if (explainMode == ExplainMode.EXPLAIN_DEFAULT_PROC) {
        return processExplainDefaultProc(plannedStmtBatch);
    } else {
        try {
            return createAdHocTransaction(plannedStmtBatch, isSwapTables);
        } catch (VoltTypeException vte) {
            String msg = "Unable to execute adhoc sql statement(s): " + vte.getMessage();
            return makeQuickResponse(ClientResponse.GRACEFUL_FAILURE, msg);
        }
    }
}
Also used : VoltTrace(org.voltdb.utils.VoltTrace) ArrayList(java.util.ArrayList) VoltTypeException(org.voltdb.VoltTypeException) AdHocPlannedStmtBatch(org.voltdb.compiler.AdHocPlannedStmtBatch) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) VoltType(org.voltdb.VoltType)

Example 5 with AdHocPlannedStatement

use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.

the class AdHocBase method runAdHoc.

/**
     * Call from derived class run() method for implementation.
     *
     * @param ctx
     * @param aggregatorFragments
     * @param collectorFragments
     * @param sqlStatements
     * @param replicatedTableDMLFlags
     * @return
     */
public VoltTable[] runAdHoc(SystemProcedureExecutionContext ctx, byte[] serializedBatchData) {
    Pair<Object[], AdHocPlannedStatement[]> data = decodeSerializedBatchData(serializedBatchData);
    Object[] userparams = data.getFirst();
    AdHocPlannedStatement[] statements = data.getSecond();
    if (statements.length == 0) {
        return new VoltTable[] {};
    }
    for (AdHocPlannedStatement statement : statements) {
        if (!statement.core.wasPlannedAgainstHash(ctx.getCatalogHash())) {
            @SuppressWarnings("deprecation") String msg = String.format("AdHoc transaction %d wasn't planned " + "against the current catalog version. Statement: %s", DeprecatedProcedureAPIAccess.getVoltPrivateRealTransactionId(this), new String(statement.sql, Constants.UTF8ENCODING));
            throw new VoltAbortException(msg);
        }
        // Don't cache the statement text, since ad hoc statements
        // that differ only by constants reuse the same plan, statement text may change.
        long aggFragId = ActivePlanRepository.loadOrAddRefPlanFragment(statement.core.aggregatorHash, statement.core.aggregatorFragment, null);
        long collectorFragId = 0;
        if (statement.core.collectorFragment != null) {
            collectorFragId = ActivePlanRepository.loadOrAddRefPlanFragment(statement.core.collectorHash, statement.core.collectorFragment, null);
        }
        SQLStmt stmt = SQLStmtAdHocHelper.createWithPlan(statement.sql, aggFragId, statement.core.aggregatorHash, true, collectorFragId, statement.core.collectorHash, true, statement.core.isReplicatedTableDML, statement.core.readOnly, statement.core.parameterTypes, m_site);
        Object[] params = paramsForStatement(statement, userparams);
        voltQueueSQL(stmt, params);
    }
    return voltExecuteSQL(true);
}
Also used : SQLStmt(org.voltdb.SQLStmt) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) VoltTable(org.voltdb.VoltTable)

Aggregations

AdHocPlannedStatement (org.voltdb.compiler.AdHocPlannedStatement)10 IOException (java.io.IOException)3 AdHocPlannedStmtBatch (org.voltdb.compiler.AdHocPlannedStmtBatch)3 ArrayList (java.util.ArrayList)2 VoltTable (org.voltdb.VoltTable)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 HostMessenger (org.voltcore.messaging.HostMessenger)1 Pair (org.voltcore.utils.Pair)1 CatalogContext (org.voltdb.CatalogContext)1 ClientResponseImpl (org.voltdb.ClientResponseImpl)1 DependencyPair (org.voltdb.DependencyPair)1 ParameterSet (org.voltdb.ParameterSet)1 SQLStmt (org.voltdb.SQLStmt)1 VoltAbortException (org.voltdb.VoltProcedure.VoltAbortException)1 VoltType (org.voltdb.VoltType)1 VoltTypeException (org.voltdb.VoltTypeException)1