Search in sources :

Example 36 with VoltTableRow

use of org.voltdb.VoltTableRow in project voltdb by VoltDB.

the class TestSqlAggregateSuite method testAggregatesOnEmptyTable.

public void testAggregatesOnEmptyTable() throws IOException, ProcCallException {
    String[] aggs = { "count", "sum", "min", "max" };
    String[] tables = { "P1", "R1" };
    for (String table : tables) {
        Client client = getClient();
        for (int i = 0; i < aggs.length; ++i) {
            String query = String.format("select %s(%s.NUM) from %s", aggs[i], table, table);
            VoltTable[] results = client.callProcedure("@AdHoc", query).getResults();
            if (aggs[i].equals("count")) {
                assertEquals(0, results[0].asScalarLong());
            } else {
                final VoltTableRow row = results[0].fetchRow(0);
                row.get(0, results[0].getColumnType(0));
                if (!isHSQL()) {
                    assertTrue(row.wasNull());
                }
            }
        }
        // Do avg separately since the column is a float and makes
        // asScalarLong() unhappy
        String query = String.format("select avg(%s.NUM) from %s", table, table);
        VoltTable[] results = client.callProcedure("@AdHoc", query).getResults();
        results[0].advanceRow();
        @SuppressWarnings("unused") final double value = ((Number) results[0].get(0, results[0].getColumnType(0))).doubleValue();
        if (!isHSQL()) {
            assertTrue(results[0].wasNull());
        }
    }
}
Also used : Client(org.voltdb.client.Client) VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 37 with VoltTableRow

use of org.voltdb.VoltTableRow in project voltdb by VoltDB.

the class paymentByCustomerName method run.

public VoltTable[] run(short w_id, byte d_id, double h_amount, short c_w_id, byte c_d_id, String c_last, TimestampType timestamp) throws VoltAbortException {
    voltQueueSQL(getCustomersByLastName, c_last, c_d_id, c_w_id);
    final VoltTable customers = voltExecuteSQL()[0];
    final int namecnt = customers.getRowCount();
    if (namecnt == 0) {
        throw new VoltAbortException("no customers with last name: " + c_last + " in warehouse: " + c_w_id + " and in district " + c_d_id);
    }
    final int index = (namecnt - 1) / 2;
    final VoltTableRow customer = customers.fetchRow(index);
    final int c_id = (int) customer.getLong(C_ID_IDX);
    return processPayment(w_id, d_id, c_w_id, c_d_id, c_id, h_amount, customer, timestamp);
}
Also used : VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 38 with VoltTableRow

use of org.voltdb.VoltTableRow in project voltdb by VoltDB.

the class paymentByCustomerNameC method run.

public VoltTable[] run(short w_id, byte d_id, double h_amount, short c_w_id, byte c_d_id, byte[] c_last, TimestampType timestamp) throws VoltAbortException {
    voltQueueSQL(getCustomersByLastName, c_last, c_d_id, c_w_id);
    final VoltTable customers = voltExecuteSQL()[0];
    final int namecnt = customers.getRowCount();
    if (namecnt == 0) {
        String c_lastString = null;
        try {
            c_lastString = new String(c_last, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        throw new VoltAbortException("paymentByCustomerNameC: no customers with last name: " + c_lastString + " in warehouse: " + c_w_id + " and in district " + c_d_id);
    }
    final int index = (namecnt - 1) / 2;
    final VoltTableRow customer = customers.fetchRow(index);
    final int c_id = (int) customer.getLong(C_ID_IDX);
    return processPayment(w_id, d_id, c_w_id, c_d_id, c_id, h_amount, customer, timestamp);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 39 with VoltTableRow

use of org.voltdb.VoltTableRow in project voltdb by VoltDB.

the class delivery method run.

public VoltTable run(short w_id, int o_carrier_id, TimestampType timestamp) throws VoltAbortException {
    for (long d_id = 1; d_id <= Constants.DISTRICTS_PER_WAREHOUSE; ++d_id) {
        voltQueueSQL(getNewOrder, d_id, w_id);
    }
    final VoltTable[] neworderresults = voltExecuteSQL();
    assert neworderresults.length == Constants.DISTRICTS_PER_WAREHOUSE;
    final Long[] no_o_ids = new Long[Constants.DISTRICTS_PER_WAREHOUSE];
    int valid_neworders = 0;
    int[] result_offsets = new int[Constants.DISTRICTS_PER_WAREHOUSE];
    for (long d_id = 1; d_id <= Constants.DISTRICTS_PER_WAREHOUSE; ++d_id) {
        final VoltTable newOrder = neworderresults[(int) d_id - 1];
        if (newOrder.getRowCount() == 0) {
            // No orders for this district: skip it. Note: This must be reported if > 1%
            result_offsets[(int) d_id - 1] = -1;
            continue;
        }
        assert newOrder.getRowCount() == 1;
        result_offsets[(int) d_id - 1] = valid_neworders * 2;
        ++valid_neworders;
        final Long no_o_id = newOrder.asScalarLong();
        no_o_ids[(int) d_id - 1] = no_o_id;
        voltQueueSQL(getCId, no_o_id, d_id, w_id);
        voltQueueSQL(sumOLAmount, no_o_id, d_id, w_id);
    }
    final VoltTable[] otherresults = voltExecuteSQL();
    assert otherresults.length == valid_neworders * 2;
    for (long d_id = 1; d_id <= Constants.DISTRICTS_PER_WAREHOUSE; ++d_id) {
        final VoltTable newOrder = neworderresults[(int) d_id - 1];
        if (newOrder.getRowCount() == 0) {
            // No orders for this district: skip it. Note: This must be reported if > 1%
            continue;
        }
        assert newOrder.getRowCount() == 1;
        final Long no_o_id = newOrder.asScalarLong();
        no_o_ids[(int) d_id - 1] = no_o_id;
        voltQueueSQL(deleteNewOrder, d_id, w_id, no_o_id);
        voltQueueSQL(updateOrders, o_carrier_id, no_o_id, d_id, w_id);
        voltQueueSQL(updateOrderLine, timestamp, no_o_id, d_id, w_id);
    }
    voltExecuteSQL();
    // these must be logged in the "result file" according to TPC-C 2.7.2.2 (page 39)
    // We remove the queued time, completed time, w_id, and o_carrier_id: the client can figure
    // them out
    final VoltTable result = result_template.clone(1024);
    for (long d_id = 1; d_id <= Constants.DISTRICTS_PER_WAREHOUSE; ++d_id) {
        int resultoffset = result_offsets[(int) d_id - 1];
        if (resultoffset < 0) {
            continue;
        }
        assert otherresults[resultoffset + 0].getRowCount() == 1;
        assert otherresults[resultoffset + 1].getRowCount() == 1;
        final long c_id = (otherresults[resultoffset + 0].asScalarLong());
        final VoltTableRow row = otherresults[resultoffset + 1].fetchRow(0);
        final double ol_total = row.getDouble(0);
        final boolean ol_total_wasnull = row.wasNull();
        // If there are no order lines, SUM returns null. There should always be order lines.
        if (ol_total_wasnull) {
            throw new VoltAbortException("ol_total is NULL: there are no order lines. This should not happen");
        }
        assert ol_total > 0.0;
        voltQueueSQL(updateCustomer, ol_total, c_id, d_id, w_id);
        final Long no_o_id = no_o_ids[(int) d_id - 1];
        result.addRow(d_id, no_o_id);
    }
    voltExecuteSQL();
    return result;
}
Also used : VoltTable(org.voltdb.VoltTable) VoltTableRow(org.voltdb.VoltTableRow)

Example 40 with VoltTableRow

use of org.voltdb.VoltTableRow in project voltdb by VoltDB.

the class neworder method run.

public VoltTable[] run(short w_id, byte d_id, int c_id, TimestampType timestamp, int[] item_id, short[] supware, int[] quantity) throws VoltAbortException {
    assert item_id.length > 0;
    assert item_id.length == supware.length;
    assert item_id.length == quantity.length;
    // CHEAT: Validate all items to see if we will need to abort.
    // Also determine if this is an all local order or not
    final VoltTableRow[] items = new VoltTableRow[item_id.length];
    boolean isAllLocal = true;
    for (int i = 0; i < item_id.length; ++i) {
        isAllLocal = isAllLocal && supware[i] == w_id;
        voltQueueSQL(getItemInfo, item_id[i]);
    }
    voltQueueSQL(getWarehouseTaxRate, w_id);
    voltQueueSQL(getDistrict, d_id, w_id);
    voltQueueSQL(getCustomer, w_id, d_id, c_id);
    final VoltTable[] itemresults = voltExecuteSQL();
    assert itemresults.length == item_id.length + 3;
    for (int i = 0; i < item_id.length; ++i) {
        if (itemresults[i].getRowCount() == 0) {
            // TPCC defines 1% of neworder gives a wrong itemid, causing rollback.
            throw new VoltAbortException(Constants.INVALID_ITEM_MESSAGE);
        }
        assert itemresults[i].getRowCount() == 1;
        items[i] = itemresults[i].fetchRow(0);
    }
    //final VoltTable[] backgroundInfo = executeSQL();
    VoltTable customer = itemresults[item_id.length + 2];
    final double w_tax = itemresults[item_id.length].fetchRow(0).getDouble(0);
    final int D_TAX_COL = 0, D_NEXT_O_ID = 1;
    final int C_DISCOUNT = 0;
    final VoltTableRow tempRow = itemresults[item_id.length + 1].fetchRow(0);
    final double d_tax = tempRow.getDouble(D_TAX_COL);
    final double c_discount = itemresults[item_id.length + 2].fetchRow(0).getDouble(C_DISCOUNT);
    final long d_next_o_id = tempRow.getLong(D_NEXT_O_ID);
    final long ol_cnt = item_id.length;
    final long all_local = isAllLocal ? 1 : 0;
    voltQueueSQL(incrementNextOrderId, d_next_o_id + 1, d_id, w_id);
    voltQueueSQL(createOrder, d_next_o_id, d_id, w_id, c_id, timestamp, Constants.NULL_CARRIER_ID, ol_cnt, all_local);
    voltQueueSQL(createNewOrder, d_next_o_id, d_id, w_id);
    voltExecuteSQL();
    // values the client is missing: i_name, s_quantity, brand_generic, i_price, ol_amount
    final VoltTable item_data = item_data_template.clone(2048);
    double total = 0;
    for (int i = 0; i < item_id.length; ++i) {
        final long ol_supply_w_id = supware[i];
        final long ol_i_id = item_id[i];
        // One getStockInfo SQL statement for each district
        voltQueueSQL(getStockInfo[d_id - 1], ol_i_id, ol_supply_w_id);
    }
    final VoltTable[] stockresults = voltExecuteSQL();
    assert stockresults.length == item_id.length;
    for (int i = 0; i < item_id.length; ++i) {
        final long ol_number = i + 1;
        final long ol_supply_w_id = supware[i];
        final long ol_i_id = item_id[i];
        final long ol_quantity = quantity[i];
        assert stockresults[i].getRowCount() == 1 : "Cannot find stock info for item; should not happen with valid database";
        final VoltTableRow itemInfo = items[i];
        final VoltTableRow stockInfo = stockresults[i].fetchRow(0);
        final int I_PRICE = 0, I_NAME = 1, I_DATA = 2;
        final byte[] i_name = itemInfo.getStringAsBytes(I_NAME);
        final byte[] i_data = itemInfo.getStringAsBytes(I_DATA);
        final double i_price = itemInfo.getDouble(I_PRICE);
        final int S_QUANTITY = 0, S_DATA = 1, S_YTD = 2, S_ORDER_CNT = 3, S_REMOTE_CNT = 4, S_DIST_XX = 5;
        long s_quantity = stockInfo.getLong(S_QUANTITY);
        long s_ytd = stockInfo.getLong(S_YTD);
        long s_order_cnt = stockInfo.getLong(S_ORDER_CNT);
        long s_remote_cnt = stockInfo.getLong(S_REMOTE_CNT);
        final byte[] s_data = stockInfo.getStringAsBytes(S_DATA);
        // Fetches data from the s_dist_[d_id] column
        final byte[] s_dist_xx = stockInfo.getStringAsBytes(S_DIST_XX);
        // Update stock
        s_ytd += ol_quantity;
        if (s_quantity >= ol_quantity + 10) {
            s_quantity = s_quantity - ol_quantity;
        } else {
            s_quantity = s_quantity + 91 - ol_quantity;
        }
        s_order_cnt++;
        if (ol_supply_w_id != w_id)
            s_remote_cnt++;
        // TODO(evanj): Faster to do s_ytd and s_order_cnt increment in SQL?
        // Saves fetching those columns the first time
        voltQueueSQL(updateStock, s_quantity, s_ytd, s_order_cnt, s_remote_cnt, ol_i_id, ol_supply_w_id);
        byte[] brand_generic;
        if (indexOf(i_data, Constants.ORIGINAL_BYTES) != -1 && indexOf(s_data, Constants.ORIGINAL_BYTES) != -1) {
            brand_generic = new byte[] { 'B' };
        } else {
            brand_generic = new byte[] { 'G' };
        }
        //Transaction profile states to use "ol_quantity * i_price"
        final double ol_amount = ol_quantity * i_price;
        total += ol_amount;
        voltQueueSQL(createOrderLine, d_next_o_id, d_id, w_id, ol_number, ol_i_id, ol_supply_w_id, timestamp, ol_quantity, ol_amount, s_dist_xx);
        // Add the info to be returned
        item_data.addRow(i_name, s_quantity, brand_generic, i_price, ol_amount);
    }
    voltExecuteSQL();
    // Adjust the total for the discount
    total *= (1 - c_discount) * (1 + w_tax + d_tax);
    // pack up values the client is missing (see TPC-C 2.4.3.5)
    final VoltTable misc = misc_template.clone(256);
    misc.addRow(w_tax, d_tax, d_next_o_id, total);
    return new VoltTable[] { customer, misc, item_data };
}
Also used : VoltTableRow(org.voltdb.VoltTableRow) VoltTable(org.voltdb.VoltTable)

Aggregations

VoltTableRow (org.voltdb.VoltTableRow)65 VoltTable (org.voltdb.VoltTable)57 Client (org.voltdb.client.Client)23 ProcCallException (org.voltdb.client.ProcCallException)11 TimestampType (org.voltdb.types.TimestampType)7 NoConnectionsException (org.voltdb.client.NoConnectionsException)6 IOException (java.io.IOException)5 VoltAbortException (org.voltdb.VoltProcedure.VoltAbortException)5 Date (java.util.Date)4 WorkWithBigString (org.voltdb_testprocs.regressionsuites.sqlfeatureprocs.WorkWithBigString)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 ClientResponse (org.voltdb.client.ClientResponse)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 ByteBuilder (org.voltdb.benchmark.tpcc.procedures.ByteBuilder)1