use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.
the class AdHocBase method adHocSQLFromInvocationForDebug.
/**
* Get a string containing the SQL statements and any parameters for a given
* batch passed to an ad-hoc query. Used for debugging and logging.
*/
public static String adHocSQLFromInvocationForDebug(StoredProcedureInvocation invocation) {
assert (invocation.getProcName().startsWith("@AdHoc"));
ParameterSet params = invocation.getParams();
assert (params.size() == 2 || params.size() == 3);
// the final param is the byte array we need
byte[] serializedBatchData = (byte[]) params.getParam(params.size() - 1);
Pair<Object[], AdHocPlannedStatement[]> data = decodeSerializedBatchData(serializedBatchData);
Object[] userparams = data.getFirst();
AdHocPlannedStatement[] statements = data.getSecond();
StringBuilder sb = new StringBuilder();
if (statements.length == 0) {
sb.append("ADHOC INVOCATION HAS NO SQL");
} else if (statements.length == 1) {
sb.append(adHocSQLStringFromPlannedStatement(statements[0], userparams));
} else {
// > 1
sb.append("BEGIN ADHOC_SQL_BATCH {\n");
for (AdHocPlannedStatement stmt : statements) {
sb.append(adHocSQLStringFromPlannedStatement(stmt, userparams)).append("\n");
}
sb.append("} END ADHOC_SQL_BATCH");
}
return sb.toString();
}
use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.
the class AdHocNTBase method logBatch.
/**
* Log ad hoc batch info
* @param batch planned statement batch
*/
private void logBatch(final CatalogContext context, final AdHocPlannedStmtBatch batch, final Object[] userParams) {
final int numStmts = batch.getPlannedStatementCount();
final int numParams = userParams == null ? 0 : userParams.length;
final String readOnly = batch.readOnly ? "yes" : "no";
final String singlePartition = batch.isSinglePartitionCompatible() ? "yes" : "no";
final String user = getUsername();
final String[] groupNames = context.authSystem.getGroupNamesForUser(user);
final String groupList = StringUtils.join(groupNames, ',');
//String[] stmtArray = batch.stmts.stream().map(s -> new String(s.sql, Charsets.UTF_8)).toArray(String[]::new);
adhocLog.debug(String.format("=== statements=%d parameters=%d read-only=%s single-partition=%s user=%s groups=[%s]", numStmts, numParams, readOnly, singlePartition, user, groupList));
for (int i = 0; i < batch.getPlannedStatementCount(); i++) {
AdHocPlannedStatement stmt = batch.getPlannedStatement(i);
String sql = stmt.sql == null ? "SQL_UNKNOWN" : new String(stmt.sql, Charsets.UTF_8);
adhocLog.debug(String.format("Statement #%d: %s", i + 1, sql));
}
if (userParams != null) {
for (int i = 0; i < userParams.length; ++i) {
Object value = userParams[i];
final String valueString = (value != null ? value.toString() : "NULL");
adhocLog.debug(String.format("Parameter #%d: %s", i + 1, valueString));
}
}
}
use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.
the class AdHocBase method decodeSerializedBatchData.
/**
* Decode binary data into structures needed to process adhoc queries.
* This code was pulled out of runAdHoc so it could be shared there and with
* adHocSQLStringFromPlannedStatement.
*/
public static Pair<Object[], AdHocPlannedStatement[]> decodeSerializedBatchData(byte[] serializedBatchData) {
// Collections must be the same size since they all contain slices of the same data.
assert (serializedBatchData != null);
ByteBuffer buf = ByteBuffer.wrap(serializedBatchData);
AdHocPlannedStatement[] statements = null;
Object[] userparams = null;
try {
userparams = AdHocPlannedStmtBatch.userParamsFromBuffer(buf);
statements = AdHocPlannedStmtBatch.planArrayFromBuffer(buf);
} catch (IOException e) {
throw new VoltAbortException(e);
}
return new Pair<Object[], AdHocPlannedStatement[]>(userparams, statements);
}
use of org.voltdb.compiler.AdHocPlannedStatement 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);
}
use of org.voltdb.compiler.AdHocPlannedStatement in project voltdb by VoltDB.
the class TestPlannerTool method testSimple.
public void testSimple() throws IOException {
TPCCProjectBuilder builder = new TPCCProjectBuilder();
builder.addAllDefaults();
final File jar = new File("tpcc-oop.jar");
jar.deleteOnExit();
//long start = System.nanoTime();
//for (int i = 0; i < 10000; i++) {
builder.compile("tpcc-oop.jar");
/* long end = System.nanoTime();
System.err.printf("Took %.3f seconds to compile.\n",
(end - start) / 1000000000.0);
start = end;
}*/
byte[] bytes = MiscUtils.fileToBytes(new File("tpcc-oop.jar"));
String serializedCatalog = CatalogUtil.getSerializedCatalogStringFromJar(CatalogUtil.loadAndUpgradeCatalogFromJar(bytes, false).getFirst());
Catalog catalog = new Catalog();
catalog.execute(serializedCatalog);
DbSettings settings = new DbSettings(ClusterSettings.create().asSupplier(), NodeSettings.create());
CatalogContext context = new CatalogContext(0, 0, catalog, settings, bytes, null, new byte[] {}, 0, mock(HostMessenger.class));
m_pt = new PlannerTool(context.database, context.getCatalogHash());
AdHocPlannedStatement result = null;
result = m_pt.planSqlForTest("select * from warehouse;");
System.out.println(result);
// try many tables joins
try {
result = m_pt.planSqlForTest("select * from WAREHOUSE, DISTRICT, CUSTOMER, CUSTOMER_NAME, HISTORY, STOCK, ORDERS, NEW_ORDER, ORDER_LINE where " + "WAREHOUSE.W_ID = DISTRICT.D_W_ID and " + "WAREHOUSE.W_ID = CUSTOMER.C_W_ID and " + "WAREHOUSE.W_ID = CUSTOMER_NAME.C_W_ID and " + "WAREHOUSE.W_ID = HISTORY.H_W_ID and " + "WAREHOUSE.W_ID = STOCK.S_W_ID and " + "WAREHOUSE.W_ID = ORDERS.O_W_ID and " + "WAREHOUSE.W_ID = NEW_ORDER.NO_W_ID and " + "WAREHOUSE.W_ID = ORDER_LINE.OL_W_ID and " + "WAREHOUSE.W_ID = 0");
} catch (Exception e) {
// V4.5 supports multiple table joins
fail();
}
// try just the right amount of tables
try {
result = m_pt.planSqlForTest("select * from CUSTOMER, STOCK, ORDERS, ORDER_LINE, NEW_ORDER where " + "CUSTOMER.C_W_ID = CUSTOMER.C_W_ID and " + "CUSTOMER.C_W_ID = STOCK.S_W_ID and " + "CUSTOMER.C_W_ID = ORDERS.O_W_ID and " + "CUSTOMER.C_W_ID = ORDER_LINE.OL_W_ID and " + "CUSTOMER.C_W_ID = NEW_ORDER.NO_W_ID and " + "CUSTOMER.C_W_ID = 0");
} catch (Exception e) {
e.printStackTrace();
fail();
}
// try garbage
try {
result = m_pt.planSqlForTest("ryan likes the yankees");
fail();
} catch (Exception e) {
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
result = m_pt.planSqlForTest("ryan likes the yankees");
fail();
} catch (Exception e) {
}
result = m_pt.planSqlForTest("select * from warehouse;");
System.out.println(result);
}
Aggregations