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);
}
}
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;
}
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;
}
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);
}
}
}
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);
}
Aggregations