use of org.voltdb.compiler.AdHocPlannedStmtBatch 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.AdHocPlannedStmtBatch 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.AdHocPlannedStmtBatch in project voltdb by VoltDB.
the class CatalogSpecificPlanner method plan.
public AdHocPlannedStmtBatch plan(String sql, Object[] userParams, boolean singlePartition) throws AdHocPlanningException {
List<String> sqlStatements = new ArrayList<>();
AdHocSQLMix mix = AdHocNTBase.processAdHocSQLStmtTypes(sql, sqlStatements);
switch(mix) {
case EMPTY:
throw new AdHocPlanningException("No valid SQL found.");
case ALL_DDL:
case MIXED:
throw new AdHocPlanningException("DDL not supported in stored procedures.");
default:
break;
}
if (sqlStatements.size() != 1) {
throw new AdHocPlanningException("Only one statement is allowed in stored procedure, but received " + sqlStatements.size());
}
sql = sqlStatements.get(0);
// any object will signify SP
Object partitionKey = singlePartition ? "1" : null;
List<AdHocPlannedStatement> stmts = new ArrayList<>();
AdHocPlannedStatement result = null;
result = AdHocNTBase.compileAdHocSQL(m_catalogContext, sql, false, partitionKey, ExplainMode.NONE, false, userParams);
stmts.add(result);
return new AdHocPlannedStmtBatch(userParams, stmts, -1, null, null, userParams);
}
Aggregations