Search in sources :

Example 6 with AdHocPlannedStatement

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();
}
Also used : ParameterSet(org.voltdb.ParameterSet) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement)

Example 7 with AdHocPlannedStatement

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));
        }
    }
}
Also used : AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement)

Example 8 with AdHocPlannedStatement

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);
}
Also used : AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DependencyPair(org.voltdb.DependencyPair) Pair(org.voltcore.utils.Pair)

Example 9 with AdHocPlannedStatement

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);
}
Also used : AdHocSQLMix(org.voltdb.sysprocs.AdHocNTBase.AdHocSQLMix) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) AdHocPlanningException(org.voltdb.sysprocs.AdHocNTBase.AdHocPlanningException) ArrayList(java.util.ArrayList) AdHocPlannedStmtBatch(org.voltdb.compiler.AdHocPlannedStmtBatch)

Example 10 with AdHocPlannedStatement

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);
}
Also used : DbSettings(org.voltdb.settings.DbSettings) PlannerTool(org.voltdb.compiler.PlannerTool) AdHocPlannedStatement(org.voltdb.compiler.AdHocPlannedStatement) HostMessenger(org.voltcore.messaging.HostMessenger) TPCCProjectBuilder(org.voltdb.benchmark.tpcc.TPCCProjectBuilder) File(java.io.File) Catalog(org.voltdb.catalog.Catalog) CatalogContext(org.voltdb.CatalogContext) IOException(java.io.IOException)

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