org.voltdb.VoltType - java examples

Here are the examples of the java api org.voltdb.VoltType taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

77 Examples 7

19 View Complete Implementation : Utils.java
Copyright GNU General Public License v3.0
Author : apavlo
public static String voltTypeToJavaType(VoltType voltType) {
    switch(voltType) {
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case DECIMAL:
            return "long";
        case FLOAT:
            return "double";
        case STRING:
            return "String";
        case TIMESTAMP:
            return "Date";
        default:
            throw new RuntimeException("Invalid Volttype: " + voltType);
    }
}

19 View Complete Implementation : TestVoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public void testDetermineImplicitCastingNullWins() {
    VoltType[] types = { VoltType.BIGINT, VoltType.DECIMAL, VoltType.FLOAT, VoltType.INTEGER, VoltType.SMALLINT, VoltType.STRING, VoltType.TIMESTAMP, VoltType.TINYINT };
    for (VoltType right : types) {
        replacedertEquals(VoltTypeUtil.determineImplicitCasting(VoltType.NULL, right), VoltType.NULL);
    }
}

19 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public static Object getRandomValue(VoltType type) {
    return getRandomValue(type, VoltTypeUtil.rand);
}

19 View Complete Implementation : TestTransactionTrace.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * testSetOutputObjectArrays
 */
public void testSetOutputObjectArrays() throws Exception {
    Object[][] output = new Object[][] { { new Long(1), new Long(2), new Float(2.0f) }, { new Long(3), new Long(4), new Float(6.0f) } };
    replacedertFalse(xact.hasOutput());
    xact.setOutput(output);
    replacedert (xact.hasOutput());
    VoltType[] expected = { VoltType.BIGINT, VoltType.BIGINT, VoltType.FLOAT };
    VoltType[] actual = xact.getOutputTypes(0);
    replacedertNotNull(actual);
    replacedertEquals(expected.length, actual.length);
    for (int i = 0; i < expected.length; i++) {
        replacedertEquals(Integer.toString(i), expected[i], actual[i]);
    }
// FOR
}

19 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/*
     * Determine if a cast is allowable w/o loss of precision
     * for index key comparison. This is probably overly strict.
     */
public static boolean isAllowableCastForKeyComparator(VoltType from, VoltType to) {
    // self to self cast is obviously fine.
    if (from == to)
        return true;
    // allow only float to float
    // allow only decimal to decimal
    // allow only string to string
    if (to == VoltType.STRING || from == VoltType.STRING || to == VoltType.DECIMAL || from == VoltType.DECIMAL || to == VoltType.FLOAT || from == VoltType.FLOAT) {
        return from == to;
    }
    // disallow integers getting smaller
    if (from == VoltType.BIGINT) {
        if (to == VoltType.SMALLINT || to == VoltType.TINYINT || to == VoltType.INTEGER)
            return false;
    } else if (from == VoltType.INTEGER) {
        if (to == VoltType.SMALLINT || to == VoltType.TINYINT)
            return false;
    } else if (from == VoltType.SMALLINT) {
        if (to == VoltType.TINYINT)
            return false;
    }
    return true;
}

19 View Complete Implementation : ProcEnv.java
Copyright GNU General Public License v3.0
Author : apavlo
public static String buildVmParaList(Procedure proc) {
    String ret = "";
    VoltType[] voltTypes = getParaVoltTypes(proc);
    String[] javaTypes = new String[voltTypes.length];
    for (int i = 0; i < voltTypes.length; i++) {
        javaTypes[i] = Utils.voltTypeToJavaType(voltTypes[i]);
    }
    if (javaTypes.length > 0) {
        int i = 0;
        for (; i < javaTypes.length - 1; i++) {
            ret = ret + (javaTypes[i] + " para" + i + ", ");
        }
        ret = ret + (javaTypes[i] + " para" + i);
    }
    return ret;
}

19 View Complete Implementation : AbstractExpression.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * @param type The type of this expression's value.
 */
public void setValueType(VoltType type) {
    m_valueType = type;
}

19 View Complete Implementation : ProcEnv.java
Copyright GNU General Public License v3.0
Author : apavlo
public static String[] getParaVoltTypeNames(Procedure proc) {
    VoltType[] types = getParaVoltTypes(proc);
    String[] ret = new String[types.length];
    for (int i = 0; i < ret.length; i++) {
        ret[i] = types[i].toString();
        // VoltType.xxx
        int posDot = ret[i].indexOf(".");
        ret[i] = ret[i].substring(posDot + 1);
    }
    return ret;
}

19 View Complete Implementation : TestVoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public void testGetObjectFromString() throws Exception {
    VoltType[] types = { VoltType.BIGINT, VoltType.DECIMAL, VoltType.FLOAT, VoltType.INTEGER, VoltType.SMALLINT, VoltType.STRING, VoltType.TIMESTAMP, VoltType.TINYINT };
    for (VoltType vt : types) {
        Object o = VoltTypeUtil.getRandomValue(vt);
        replacedertNotNull(o);
        String s = o.toString();
        replacedertFalse(s.isEmpty());
        replacedertEquals("Failed to get matching " + vt, o, VoltTypeUtil.getObjectFromString(vt, s));
    }
// FOR
}

19 View Complete Implementation : TestTransactionTrace.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * testSetOutputVoltTable
 */
public void testSetOutputVoltTable() throws Exception {
    int num_rows = 20;
    int num_cols = 4;
    VoltTable vt = new VoltTable(new ColumnInfo("col0", VoltType.BIGINT), new ColumnInfo("col1", VoltType.BIGINT), new ColumnInfo("col2", VoltType.TIMESTAMP), new ColumnInfo("col3", VoltType.FLOAT));
    replacedertEquals(num_cols, vt.getColumnCount());
    for (int i = 0; i < num_rows; i++) {
        Object[] row = new Object[num_cols];
        for (int j = 0; j < num_cols; j++) {
            row[j] = VoltTypeUtil.getRandomValue(vt.getColumnType(j));
        }
        // FOR
        vt.addRow(row);
    }
    // FOR
    replacedertEquals(num_rows, vt.getRowCount());
    replacedertFalse(xact.hasOutput());
    xact.setOutput(vt);
    replacedert (xact.hasOutput());
    VoltType[] types = xact.getOutputTypes(0);
    replacedertNotNull(types);
    Object[][] output = xact.getOutput(0);
    replacedertNotNull(output);
    for (int j = 0; j < num_cols; j++) {
        replacedertEquals(vt.getColumnName(j), vt.getColumnType(j), types[j]);
        replacedertNotNull(vt.getColumnName(j), output[0][j]);
    }
// String json = xact.toJSONString(catalog_db);
// System.err.println(JSONUtil.format(json));
}

19 View Complete Implementation : SavedTableConverter.java
Copyright GNU General Public License v3.0
Author : apavlo
public static Boolean needsConversion(VoltTable inputTable, Table outputTableSchema) {
    for (int ii = 0; ii < inputTable.getColumnCount(); ii++) {
        final String name = inputTable.getColumnName(ii);
        final VoltType type = inputTable.getColumnType(ii);
        final Column column = outputTableSchema.getColumns().get(name);
        if (column == null) {
            return true;
        }
        if (column.getIndex() != ii) {
            return true;
        }
        if (column.getType() != type.getValue()) {
            return true;
        }
    }
    return false;
}

19 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public static VoltType determineImplicitCasting(VoltType left, VoltType right) {
    // 
    // Make sure both are valid
    // 
    if (left == VoltType.INVALID || right == VoltType.INVALID) {
        throw new VoltTypeException("ERROR: Unable to determine cast type for '" + left + "' and '" + right + "' types");
    } else // Check for NULL first, if either type is NULL the output is always NULL
    // XXX do we need to actually check for all NULL_foo types here?
    if (left == VoltType.NULL || right == VoltType.NULL) {
        return VoltType.NULL;
    } else // 
    // No mixing of strings and numbers
    // 
    if ((left == VoltType.STRING && right != VoltType.STRING) || (left != VoltType.STRING && right == VoltType.STRING)) {
        throw new VoltTypeException("ERROR: Unable to determine cast type for '" + left + "' and '" + right + "' types");
    } else // Allow promoting INTEGER types to DECIMAL.
    if ((left == VoltType.DECIMAL || right == VoltType.DECIMAL) && !(left.isExactNumeric() && right.isExactNumeric())) {
        throw new VoltTypeException("ERROR: Unable to determine cast type for '" + left + "' and '" + right + "' types");
    }
    // 
    // The following list contains the rules that use for casting:
    // 
    // (1) If both types are a STRING, the output is always a STRING
    // Note that up above we made sure that they do not mix strings and numbers
    // Example: STRING + STRING -> STRING
    // (2) If one type is a DECIMAL, the output is always a DECIMAL
    // Note that above we made sure that DECIMAL only mixes with
    // allowed types
    // (3) Floating-point types take precedence over integers
    // Example: FLOAT + INTEGER -> FLOAT
    // (4) Specific types for floating-point and integer types take precedence
    // over the more general types
    // Example: MONEY + FLOAT -> MONEY
    // Example: TIMESTAMP + INTEGER -> TIMESTAMP
    VoltType[] cast_order = { VoltType.STRING, VoltType.DECIMAL, VoltType.FLOAT, VoltType.TIMESTAMP, VoltType.BIGINT };
    for (VoltType cast_type : cast_order) {
        // 
        // If any one of the types is the current cast type, we'll use that
        // 
        if (left == cast_type || right == cast_type) {
            return cast_type;
        }
    }
    // If we have INT types smaller than BIGINT
    // promote the output up to BIGINT
    if ((left == VoltType.INTEGER || left == VoltType.SMALLINT || left == VoltType.TINYINT) && (right == VoltType.INTEGER || right == VoltType.SMALLINT || right == VoltType.TINYINT)) {
        return VoltType.BIGINT;
    }
    // If we get here, we couldn't figure out what to do
    throw new VoltTypeException("ERROR: Unable to determine cast type for '" + left + "' and '" + right + "' types");
}

19 View Complete Implementation : ParameterInfo.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 */
public clreplaced ParameterInfo implements FastSerializable {

    public int index;

    public VoltType type;

    @Override
    public String toString() {
        return "P" + String.valueOf(index) + ":" + type.name();
    }

    @Override
    public void readExternal(FastDeserializer in) throws IOException {
        index = in.readInt();
        type = VoltType.get(in.readByte());
    }

    @Override
    public void writeExternal(FastSerializer out) throws IOException {
        out.writeInt(index);
        out.writeByte(type.getValue());
    }
}

19 View Complete Implementation : VoltTableComparator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * @author pavlo
 */
public clreplaced VoltTableComparator implements Comparator<Object[]> {

    private final VoltTable table;

    private final Pair<Integer, SortDirectionType>[] cols;

    private final VoltType[] types;

    public VoltTableComparator(VoltTable table, Pair<Integer, SortDirectionType>[] cols) {
        this.table = table;
        this.cols = cols;
        this.types = new VoltType[this.cols.length];
        for (int i = 0; i < this.types.length; i++) {
            this.types[i] = this.table.getColumnType(cols[i].getFirst());
        }
    // FOR
    }

    @Override
    public int compare(Object[] o1, Object[] o2) {
        replacedert (o1 != null);
        replacedert (o2 != null);
        replacedert (o1.length == o2.length);
        int cmp = 0;
        int sort_idx = 0;
        for (Pair<Integer, SortDirectionType> p : this.cols) {
            int col_idx = p.getFirst();
            replacedert (col_idx < o1.length);
            SortDirectionType dir = p.getSecond();
            replacedert (dir != SortDirectionType.INVALID);
            switch(this.types[sort_idx]) {
                case TINYINT:
                    cmp = ((Byte) o1[col_idx]) - ((Byte) o2[col_idx]);
                    break;
                case SMALLINT:
                    cmp = ((Short) o1[col_idx]) - ((Short) o2[col_idx]);
                    break;
                case INTEGER:
                    cmp = ((Integer) o1[col_idx]) - ((Integer) o2[col_idx]);
                    break;
                case BIGINT:
                    cmp = (int) (((Long) o1[col_idx]) - ((Long) o2[col_idx]));
                    break;
                case FLOAT:
                    cmp = (int) (((Float) o1[col_idx]) - ((Float) o2[col_idx]));
                    break;
                case DECIMAL:
                    cmp = (int) (((Double) o1[col_idx]) - ((Double) o2[col_idx]));
                    break;
                case STRING:
                    cmp = ((String) o1[col_idx]).compareTo((String) o2[col_idx]);
                    break;
                case TIMESTAMP:
                    cmp = ((TimestampType) o1[col_idx]).compareTo((TimestampType) o2[col_idx]);
                    break;
                case BOOLEAN:
                    cmp = ((Boolean) o1[col_idx]).compareTo((Boolean) o2[col_idx]);
                    break;
                default:
                    replacedert (false) : "Unsupported sorting column type " + this.types[sort_idx];
            }
            // SWITCH
            if (cmp != 0)
                break;
            sort_idx++;
        }
        // FOR
        // TODO: Handle duplicates!
        return (cmp);
    }
}

19 View Complete Implementation : ExpressionUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public static void replacedignLiteralConstantTypesRecursively(AbstractExpression exp, VoltType columnType) {
    if (exp == null)
        return;
    if ((exp instanceof ConstantValueExpression) && (exp.m_valueType == VoltType.NUMERIC)) {
        replacedert (columnType != VoltType.INVALID);
        exp.m_valueType = columnType;
        exp.m_valueSize = columnType.getLengthInBytesForFixedTypes();
        return;
    }
    replacedignLiteralConstantTypes_recurse(exp);
}

19 View Complete Implementation : VoltTableUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns a row with random data that can be added to this VoltTable
 * @param table
 * @return
 */
public static Object[] getRandomRow(Table catalog_tbl) {
    Object[] row = new Object[catalog_tbl.getColumns().size()];
    for (Column catalog_col : catalog_tbl.getColumns()) {
        int i = catalog_col.getIndex();
        VoltType vtype = VoltType.get(catalog_col.getType());
        row[i] = VoltTypeUtil.getRandomValue(vtype);
        if (vtype == VoltType.STRING) {
            row[i] = StringUtil.abbrv(row[i].toString(), catalog_col.getSize(), false);
        }
    }
    // FOR
    return (row);
}

19 View Complete Implementation : RandomDistributionEnv.java
Copyright GNU General Public License v3.0
Author : apavlo
private static RandomDistribution getDefaultProperty(VoltType type) {
    switch(type) {
        case STRING:
            return getDefaultString();
        case TIMESTAMP:
            return getDefaultDate();
        default:
            return getDefaultNumber();
    }
}

19 View Complete Implementation : TestSQLTypesSuite.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Utility to compare two instances of a VoltType for equality
 */
private boolean comparisonHelper(final Object lhs, final Object rhs, final VoltType vt) {
    switch(vt) {
        case TINYINT:
            final Byte b1 = (Byte) lhs;
            final Byte b2 = (Byte) rhs;
            // System.out.println("\tComparing " + b1 + " == " + b2);
            return b1.byteValue() == b2.byteValue();
        case SMALLINT:
            final Short s1 = (Short) lhs;
            final Short s2 = (Short) rhs;
            // System.out.println("\tComparing " + s1 + " == " + s2);
            return s1.shortValue() == s2.shortValue();
        case INTEGER:
            final Integer i1 = (Integer) lhs;
            final Integer i2 = (Integer) rhs;
            // System.out.println("\tComparing " + i1 + " == " + i2);
            return i1.intValue() == i2.intValue();
        case BIGINT:
            final Long l1 = (Long) lhs;
            final Long l2 = (Long) rhs;
            // System.out.println("\tComparing " + l1 + " == " + l2);
            return l1.longValue() == l2.longValue();
        case FLOAT:
            final Double d1 = (Double) lhs;
            final Double d2 = (Double) rhs;
            // System.out.println("\tComparing " + d1 + " == " + d2);
            // Handle the screwy null double value (isn't quite min double)
            if (((d1 == VoltType.NULL_FLOAT) && (d2 <= d1)) || ((d2 == VoltType.NULL_FLOAT) && (d1 <= d2))) {
                return true;
            }
            return (d1.compareTo(d2) == 0);
        case STRING:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_STRING) && (rhs == null || rhs == VoltType.NULL_STRING)) {
                return true;
            }
            return ((String) lhs).equals(rhs);
        case TIMESTAMP:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_TIMESTAMP) && (rhs == null || rhs == VoltType.NULL_TIMESTAMP)) {
                return true;
            }
            return ((TimestampType) lhs).equals(rhs);
        case DECIMAL:
            // System.out.println("\tComparing " + lhs + " == " + rhs);
            if ((lhs == null || lhs == VoltType.NULL_DECIMAL) && (rhs == null || rhs == VoltType.NULL_DECIMAL)) {
                return true;
            }
            return ((BigDecimal) lhs).equals(rhs);
    }
    return false;
}

19 View Complete Implementation : VoltTableUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns a row with random data that can be added to this VoltTable
 * @param table
 * @return
 */
public static Object[] getRandomRow(VoltTable volt_tbl) {
    Object[] row = new Object[volt_tbl.getColumnCount()];
    for (int i = 0; i < row.length; i++) {
        VoltType vtype = volt_tbl.getColumnType(i);
        row[i] = VoltTypeUtil.getRandomValue(vtype);
        // HACK: We don't actually now the VARCHAR length here,
        // so we'll just leave it at 8
        if (vtype == VoltType.STRING) {
            row[i] = StringUtil.abbrv(row[i].toString(), 8, false);
        }
    }
    // FOR
    return (row);
}

19 View Complete Implementation : TestVoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public void testDetermineImplicitCastingOrder() {
    // Check that STRING + STRING -> STRING
    replacedertEquals(VoltTypeUtil.determineImplicitCasting(VoltType.STRING, VoltType.STRING), VoltType.STRING);
    // Check the easy non-coerced order
    VoltType[] winning_types = { VoltType.FLOAT, VoltType.TIMESTAMP, VoltType.BIGINT };
    VoltType[] losing_types = { VoltType.FLOAT, VoltType.TIMESTAMP, VoltType.BIGINT, VoltType.INTEGER, VoltType.SMALLINT, VoltType.TINYINT };
    for (int i = 0; i < winning_types.length; ++i) {
        for (int j = i; j < losing_types.length; ++j) {
            replacedertEquals(winning_types[i], VoltTypeUtil.determineImplicitCasting(winning_types[i], losing_types[j]));
        }
    }
    // Finally, check the promotion of INT types if none of the winning types
    // was present
    VoltType[] promoted_types = { VoltType.INTEGER, VoltType.SMALLINT, VoltType.TINYINT };
    for (int i = 0; i < promoted_types.length; ++i) {
        for (int j = i; j < promoted_types.length; ++j) {
            replacedertEquals(VoltType.BIGINT, VoltTypeUtil.determineImplicitCasting(promoted_types[i], promoted_types[j]));
        }
    }
}

19 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns a casted object of the input value string based on the given type
 * You can preplaced the current thread to get a faster access for the cached SimpleDateFormats
 * @param type
 * @param value
 * @param self
 * @return
 * @throws ParseException
 */
public static Object getObjectFromString(VoltType type, String value, Thread self) throws ParseException {
    Object ret = null;
    switch(type) {
        // NOTE: All runtime integer parameters are actually Longs,so we will have problems
        // if we actually try to convert the object to one of the smaller numeric sizes
        // --------------------------------
        // INTEGERS
        // --------------------------------
        case TINYINT:
        // ret = Byte.valueOf(value);
        // break;
        case SMALLINT:
        // ret = Short.valueOf(value);
        // break;
        case INTEGER:
            ret = Integer.valueOf(value);
            break;
        case BIGINT:
            try {
                ret = Long.valueOf(value);
            } catch (NumberFormatException ex) {
                ret = Long.valueOf(cleanNumberString(value));
            }
            break;
        // --------------------------------
        // FLOATS
        // --------------------------------
        case FLOAT:
        case DECIMAL:
            try {
                ret = Double.valueOf(value);
            } catch (NumberFormatException ex) {
                ret = Double.valueOf(cleanNumberString(value));
            }
            break;
        // --------------------------------
        // STRINGS
        // --------------------------------
        case STRING:
            ret = value;
            break;
        // --------------------------------
        // TIMESTAMP
        // --------------------------------
        case TIMESTAMP:
            {
                Date date = null;
                int usecs = 0;
                if (value.isEmpty())
                    throw new RuntimeException("Empty " + type + " parameter value");
                // We have to do this because apparently SimpleDateFormat isn't thread safe
                SimpleDateFormat[] formats = VoltTypeUtil.CACHED_DATE_FORMATS.get(self);
                if (formats == null) {
                    formats = new SimpleDateFormat[VoltTypeUtil.DATE_FORMAT_PATTERNS.length];
                    for (int i = 0; i < formats.length; i++) {
                        formats[i] = new SimpleDateFormat(VoltTypeUtil.DATE_FORMAT_PATTERNS[i]);
                    }
                    // FOR
                    VoltTypeUtil.CACHED_DATE_FORMATS.put(self, formats);
                }
                ParseException last_ex = null;
                for (int i = 0; i < formats.length; i++) {
                    try {
                        date = formats[i].parse(value);
                        // We need to get the last microseconds
                        if (i == 0)
                            usecs = Integer.parseInt(value.substring(value.lastIndexOf('.') + 1));
                    } catch (ParseException ex) {
                        last_ex = ex;
                    }
                    if (date != null)
                        break;
                }
                // FOR
                if (date == null)
                    throw last_ex;
                ret = new TimestampType((date.getTime() * 1000) + usecs);
                break;
            }
        // --------------------------------
        // BOOLEAN
        // --------------------------------
        case BOOLEAN:
            ret = Boolean.parseBoolean(value);
            break;
        // --------------------------------
        // NULL
        // --------------------------------
        case NULL:
            ret = null;
            break;
        // --------------------------------
        // INVALID
        // --------------------------------
        default:
            {
                String msg = "Unable to get object for value with invalid ValueType '" + type + "'";
                throw new ParseException(msg, 0);
            // LOG.severe(msg);
            }
    }
    return (ret);
}

19 View Complete Implementation : ProcedureUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Store
 *
 * @param map
 * @param vt
 */
public static void storeTableInMap(final Map<String, Object[]> map, final VoltTable vt) {
    replacedert (vt != null);
    replacedert (map != null);
    int num_rows = vt.getRowCount();
    while (vt.advanceRow()) {
        int row_idx = vt.getActiveRowIndex();
        for (int i = 0, cnt = vt.getColumnCount(); i < cnt; i++) {
            String col_name = vt.getColumnName(i);
            VoltType vtype = vt.getColumnType(i);
            if (row_idx == 0) {
                map.put(col_name, new Object[num_rows]);
            }
            map.get(col_name)[row_idx] = vt.get(col_name, vtype);
        }
    // FOR
    }
// WHILE
}

19 View Complete Implementation : MappingCalculator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Helper function to cast the raw Object of a parameter into the proper Number
 * This will convert Dates to longs representing their time in milliseconds
 * @param type
 * @param raw_value
 * @return
 * @throws ParseException
 */
protected Number getParamAsNumber(VoltType type, Object raw_value) throws ParseException {
    if (raw_value == null)
        return (null);
    replacedert (type != VoltType.INVALID);
    Number ret = null;
    switch(type) {
        case TIMESTAMP:
            {
                Object param_obj = VoltTypeUtil.getObjectFromString(type, raw_value.toString());
                ret = ((TimestampType) param_obj).getTime();
                break;
            }
        case STRING:
            ret = raw_value.hashCode();
            break;
        case BOOLEAN:
            ret = ((Boolean) raw_value ? 1 : 0);
            break;
        default:
            {
                Object param_obj = VoltTypeUtil.getObjectFromString(type, raw_value.toString());
                ret = (Number) param_obj;
                break;
            }
    }
    // SWITCH
    return (ret);
}

19 View Complete Implementation : ObjectHistogram.java
Copyright GNU General Public License v3.0
Author : apavlo
@Override
public void toJSON(JSONStringer stringer) throws JSONException {
    for (Members element : ObjectHistogram.Members.values()) {
        try {
            Field field = ObjectHistogram.clreplaced.getDeclaredField(element.toString().toLowerCase());
            switch(element) {
                case HISTOGRAM:
                    {
                        if (this.histogram.isEmpty() == false) {
                            stringer.key(element.name()).object();
                            synchronized (this) {
                                for (Object value : this.histogram.keySet()) {
                                    stringer.key(value.toString()).value(this.histogram.get(value));
                                }
                            // FOR
                            }
                            // SYNCH
                            stringer.endObject();
                        }
                        break;
                    }
                case KEEP_ZERO_ENTRIES:
                    {
                        if (this.keep_zero_entries) {
                            stringer.key(element.name()).value(this.keep_zero_entries);
                        }
                        break;
                    }
                case VALUE_TYPE:
                    {
                        VoltType vtype = (VoltType) field.get(this);
                        stringer.key(element.name()).value(vtype.name());
                        break;
                    }
                default:
                    stringer.key(element.name()).value(field.get(this));
            }
        // SWITCH
        } catch (Exception ex) {
            throw new RuntimeException("Failed to serialize '" + element + "'", ex);
        }
    }
// FOR
}

19 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns a casted object of the input value string based on the given type
 * @throws ParseException
 */
public static Object getObjectFromString(VoltType type, String value) throws ParseException {
    return (VoltTypeUtil.getObjectFromString(type, value, Thread.currentThread()));
}

18 View Complete Implementation : AbstractTraceElement.java
Copyright GNU General Public License v3.0
Author : apavlo
public void toJSONString(JSONStringer stringer, Database catalog_db) throws JSONException {
    stringer.key(Members.NAME.name()).value(this.catalog_item_name);
    stringer.key(Members.START.name()).value(this.start_timestamp);
    stringer.key(Members.STOP.name()).value(this.stop_timestamp);
    stringer.key(Members.ABORTED.name()).value(this.aborted);
    // WEIGHT
    if (this.weight > 1) {
        stringer.key(Members.WEIGHT.name()).value(this.weight);
    }
    // Output Tables
    stringer.key(Members.OUTPUT.name()).array();
    if (this.output != null) {
        for (int i = 0; i < this.output.length; i++) {
            stringer.object();
            // COLUMN TYPES
            stringer.key("TYPES").array();
            VoltType[] types = this.output_types[i];
            if (types != null) {
                for (int j = 0; j < types.length; j++) {
                    stringer.value((types[j] == null ? VoltType.NULL : types[j]).name());
                }
            // FOR
            }
            stringer.endArray();
            // DATA
            stringer.key("DATA").array();
            Object[][] data = this.output[i];
            if (data != null) {
                for (int j = 0; j < data.length; j++) {
                    Object[] row = data[j];
                    stringer.array();
                    for (int k = 0; k < row.length; k++) {
                        stringer.value(data[j][k]);
                    }
                    // FOR
                    stringer.endArray();
                }
            // FOR
            }
            stringer.endArray();
            stringer.endObject();
        }
    // FOR
    }
    stringer.endArray();
    // This doesn't work because CatalogType doesn't have have we need
    // replacedert(this.catalog_item.getField("parameters") != null);
    // CatalogMap<CatalogType> param_map = (CatalogMap<CatalogType>)this.catalog_item.getField("parameters");
    CatalogMap<? extends CatalogType> param_map = null;
    T catalog_item = this.getCatalogItem(catalog_db);
    if (catalog_item instanceof Procedure) {
        param_map = (CatalogMap<? extends CatalogType>) ((Procedure) catalog_item).getParameters();
    } else {
        param_map = (CatalogMap<? extends CatalogType>) ((Statement) catalog_item).getParameters();
    }
    stringer.key(Members.PARAMS.name()).array();
    for (int i = 0; i < this.params.length; i++) {
        CatalogType catalog_param = param_map.get(i);
        Object param_isarray = catalog_param.getField("isarray");
        if (param_isarray != null && (Boolean) param_isarray) {
            stringer.array();
            Clreplaced<?> type = params[i].getClreplaced();
            replacedert (type.isArray());
            Clreplaced<?> dataType = type.getComponentType();
            // I couldn't think of a better way to do this dynamically..
            // It's not trivial to go from primitive arrays to object arrays
            if (dataType == Long.TYPE) {
                for (Object value : (long[]) this.params[i]) {
                    stringer.value(value);
                }
            // FOR
            } else if (dataType == Integer.TYPE) {
                for (Object value : (int[]) this.params[i]) {
                    stringer.value(value);
                }
            // FOR
            } else if (dataType == Short.TYPE) {
                for (Object value : (short[]) this.params[i]) {
                    stringer.value(value);
                }
            // FOR
            } else if (dataType == Byte.TYPE) {
                for (Object value : (byte[]) this.params[i]) {
                    stringer.value(value);
                }
            // FOR
            } else {
                for (Object value : (Object[]) this.params[i]) {
                    stringer.value(value);
                }
            // FOR
            }
            stringer.endArray();
        } else {
            stringer.value(this.params[i]);
        }
    }
    // FOR
    stringer.endArray();
}

18 View Complete Implementation : ParameterMangler.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Hackishly convert primitive arrays into object arrays
 *
 * @author pavlo
 */
public clreplaced ParameterMangler {

    private final Procedure catalog_proc;

    private final boolean has_arrays;

    private final VoltType[] param_types;

    private final ProcParameter[] params;

    private final boolean[] param_isarray;

    private static final Map<Procedure, ParameterMangler> singletons = new HashMap<Procedure, ParameterMangler>();

    public static ParameterMangler singleton(Procedure catalog_proc) {
        ParameterMangler mangler = singletons.get(catalog_proc);
        if (mangler == null) {
            synchronized (ParameterMangler.clreplaced) {
                mangler = singletons.get(catalog_proc);
                if (mangler == null) {
                    mangler = new ParameterMangler(catalog_proc);
                    singletons.put(catalog_proc, mangler);
                }
            }
        // SYNCH
        }
        return (mangler);
    }

    private ParameterMangler(Procedure catalog_proc) {
        this.catalog_proc = catalog_proc;
        List<ProcParameter> catalog_params = CatalogUtil.getRegularProcParameters(catalog_proc);
        int num_params = catalog_params.size();
        this.params = new ProcParameter[num_params];
        this.param_isarray = new boolean[num_params];
        this.param_types = new VoltType[num_params];
        boolean found_array = false;
        for (int i = 0, cnt = catalog_params.size(); i < cnt; i++) {
            ProcParameter catalog_param = catalog_params.get(i);
            this.params[i] = catalog_param;
            this.param_isarray[i] = catalog_param.getIsarray();
            this.param_types[i] = VoltType.get(catalog_param.getType());
            found_array = found_array || this.param_isarray[i];
        }
        // FOR
        this.has_arrays = found_array;
    }

    public static String toString(Object[] mangled, boolean[] is_array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mangled.length; i++) {
            sb.append(String.format("  [%02d] ", i));
            if (is_array[i]) {
                Object[] inner = (Object[]) mangled[i];
                sb.append(String.format("%s / length=%d", Arrays.toString(inner), inner.length));
            } else {
                sb.append(mangled[i]);
            }
            sb.append("\n");
        }
        // FOR
        return (sb.toString());
    }

    public String toString(Object[] mangled) {
        return ParameterMangler.toString(mangled, this.param_isarray);
    }

    /**
     * Thread-safe
     * @param orig
     * @return
     */
    public Object[] convert(Object[] orig) {
        // Nothing!
        if (this.has_arrays == false)
            return (orig);
        Object[] cast_args = new Object[this.params.length];
        for (int i = 0; i < this.params.length; i++) {
            // Primitive Arrays! This is messed up in Java and why we're even here!
            VoltType vtype = this.param_types[i];
            if (this.param_isarray[i] && vtype != VoltType.STRING && vtype != VoltType.TIMESTAMP) {
                Object[] inner = null;
                try {
                    switch(this.param_types[i]) {
                        case TINYINT:
                            {
                                if (orig[i] instanceof byte[]) {
                                    byte[] arr = (byte[]) orig[i];
                                    inner = new Byte[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (byte) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof short[]) {
                                    short[] arr = (short[]) orig[i];
                                    inner = new Byte[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (byte) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof int[]) {
                                    int[] arr = (int[]) orig[i];
                                    inner = new Byte[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (byte) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof long[]) {
                                    long[] arr = (long[]) orig[i];
                                    inner = new Byte[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (byte) arr[j];
                                    }
                                // FOR
                                } else {
                                    inner = new Byte[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).byteValue();
                                    }
                                // FOR
                                }
                                break;
                            }
                        case SMALLINT:
                            {
                                if (orig[i] instanceof byte[]) {
                                    byte[] arr = (byte[]) orig[i];
                                    inner = new Short[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (short) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof short[]) {
                                    short[] arr = (short[]) orig[i];
                                    inner = new Short[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (short) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof int[]) {
                                    int[] arr = (int[]) orig[i];
                                    inner = new Short[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (short) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof long[]) {
                                    long[] arr = (long[]) orig[i];
                                    inner = new Short[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (short) arr[j];
                                    }
                                // FOR
                                } else {
                                    inner = new Short[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).shortValue();
                                    }
                                // FOR
                                }
                                break;
                            }
                        case INTEGER:
                            {
                                if (orig[i] instanceof byte[]) {
                                    byte[] arr = (byte[]) orig[i];
                                    inner = new Integer[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (int) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof short[]) {
                                    short[] arr = (short[]) orig[i];
                                    inner = new Integer[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (int) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof int[]) {
                                    int[] arr = (int[]) orig[i];
                                    inner = new Integer[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (int) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof long[]) {
                                    long[] arr = (long[]) orig[i];
                                    inner = new Integer[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (int) arr[j];
                                    }
                                // FOR
                                } else {
                                    inner = new Integer[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).intValue();
                                    }
                                // FOR
                                }
                                break;
                            }
                        case BIGINT:
                            {
                                if (orig[i] instanceof byte[]) {
                                    byte[] arr = (byte[]) orig[i];
                                    inner = new Long[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (long) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof short[]) {
                                    short[] arr = (short[]) orig[i];
                                    inner = new Long[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (long) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof int[]) {
                                    int[] arr = (int[]) orig[i];
                                    inner = new Long[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (long) arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof long[]) {
                                    long[] arr = (long[]) orig[i];
                                    inner = new Long[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = (long) arr[j];
                                    }
                                // FOR
                                } else {
                                    inner = new Long[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).longValue();
                                    }
                                // FOR
                                }
                                break;
                            }
                        case FLOAT:
                            {
                                if (orig[i] instanceof float[]) {
                                    float[] arr = (float[]) orig[i];
                                    inner = new Double[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = Double.valueOf(arr[j]);
                                    }
                                // FOR
                                } else if (orig[i] instanceof double[]) {
                                    double[] arr = (double[]) orig[i];
                                    inner = new Double[arr.length];
                                    for (int j = 0; j < arr.length; j++) {
                                        inner[j] = arr[j];
                                    }
                                // FOR
                                } else if (orig[i] instanceof Float[]) {
                                    inner = new Double[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).doubleValue();
                                    }
                                // FOR
                                } else {
                                    inner = new Double[((Object[]) orig[i]).length];
                                    for (int j = 0; j < inner.length; j++) {
                                        inner[j] = ((Number) ((Object[]) orig[i])[j]).doubleValue();
                                    }
                                // FOR
                                }
                                break;
                            }
                        default:
                            replacedert (false) : "Unhandled type " + this.param_types[i];
                    }
                // SWITCH
                } catch (Exception ex) {
                    throw new RuntimeException("Failed to properly convert " + this.params[i].fullName(), ex);
                }
                cast_args[i] = inner;
            } else {
                replacedert (cast_args.length == orig.length) : String.format("%s #%d :: cast[%d] != orig[%d]\nCAST:%s\nORIG:%s\nPARAMS:%s", catalog_proc, i, cast_args.length, orig.length, Arrays.toString(cast_args), Arrays.toString(orig), Arrays.toString(this.params));
                cast_args[i] = orig[i];
            }
        }
        // FOR
        return (cast_args);
    }
}

18 View Complete Implementation : AbstractTraceElement.java
Copyright GNU General Public License v3.0
Author : apavlo
protected <U extends CatalogType> void paramsFromJSONObject(JSONObject object, CatalogMap<U> catalog_params, String param_field) throws Exception {
    replacedert (catalog_params != null);
    final Thread self = Thread.currentThread();
    JSONArray jsonParams = object.getJSONArray(Members.PARAMS.name());
    int num_params = catalog_params.size();
    this.params = new Object[num_params];
    for (int i = 0; i < num_params; i++) {
        // If we ran out of JSON parameters, then just throw in null
        if (i > jsonParams.length()) {
            this.params[i] = null;
            continue;
        }
        U catalog_param = catalog_params.get(i);
        VoltType param_type = VoltType.get(((Integer) catalog_param.getField(param_field)).byteValue());
        // if (param_type == VoltType.TINYINT) param_type = VoltType.INTEGER;
        if (param_type == null) {
            throw new Exception("Parameter type is null for " + catalog_param);
        }
        // We don't know whether we have a StmtParameter or a ProcParameter
        Object _isarray = catalog_param.getField("isarray");
        boolean param_isarray = (_isarray != null && (Boolean) _isarray);
        // HACK: If parameter says its an array, but JSON disagrees, then just
        // treat it is as a String. The Volt guys started using byte arrays instead of
        // strings but didn't add a byte VoltType
        JSONArray jsonInner = null;
        if (param_isarray) {
            if (jsonParams.isNull(i)) {
                this.params[i] = new Object[0];
                continue;
            } else {
                try {
                    jsonInner = jsonParams.getJSONArray(i);
                } catch (JSONException ex) {
                    if (param_type == VoltType.TINYINT) {
                        param_isarray = false;
                        param_type = VoltType.STRING;
                    } else {
                        LOG.error("Failed to deserialize " + CatalogUtil.getDisplayName(catalog_param) + " [type=" + param_type + ", isarray=" + param_isarray + "]", ex);
                        throw ex;
                    }
                }
            }
        }
        if (param_isarray) {
            Object[] inner = new Object[jsonInner.length()];
            for (int j = 0; j < jsonInner.length(); j++) {
                inner[j] = VoltTypeUtil.getObjectFromString(param_type, jsonInner.getString(j), self);
                if (inner[j] == null) {
                    throw new RuntimeException("Array parameter " + j + " for " + catalog_param + " is null");
                }
            }
            // FOR
            this.params[i] = VoltTypeUtil.getPrimitiveArray(param_type, inner);
        } else if (jsonParams.isNull(i)) {
            this.params[i] = null;
        } else {
            // System.err.println("[" + i + "] " + jsonParams.getString(i) + " (" + param_type + ")");
            try {
                this.params[i] = VoltTypeUtil.getObjectFromString(param_type, jsonParams.getString(i), self);
                if (this.params[i] == null) {
                    throw new Exception(catalog_param + " is null [" + param_type + "]");
                }
            } catch (Exception ex) {
                LOG.fatal("Failed to convert param '" + jsonParams.getString(i) + "' to " + param_type + " for " + catalog_param + " in " + CatalogUtil.getDisplayName(catalog_param.getParent()), ex);
                throw ex;
            }
        }
    }
// FOR
}

18 View Complete Implementation : AbstractTraceElement.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * @author Andy Pavlo <[email protected]>
 */
public abstract clreplaced AbstractTraceElement<T extends CatalogType> implements JSONString, Cloneable {

    /**
     * java.util.logging logger.
     */
    protected static final Logger LOG = Logger.getLogger(AbstractTraceElement.clreplaced);

    public enum Members {

        // Catalog Name
        NAME,
        // Start Time (relative to start of trace)
        START,
        // Stop Time (relative to start of trace)
        STOP,
        // Element Parameters
        PARAMS,
        // Aborted (true/false)
        ABORTED,
        // Output (optional)
        OUTPUT,
        // Weight (optional)
        WEIGHT
    }

    protected Long start_timestamp;

    protected Long stop_timestamp;

    protected Object[] params;

    protected String catalog_item_name;

    protected boolean aborted = false;

    protected Object[][][] output;

    protected VoltType[][] output_types;

    protected short weight = 1;

    public AbstractTraceElement() {
    // Nothing to do...
    }

    public AbstractTraceElement(String catalog_item_name, Object[] params) {
        this.params = params;
        this.catalog_item_name = catalog_item_name;
        this.start_timestamp = System.nanoTime();
    // this.stop_timestamp = -1l;
    }

    public AbstractTraceElement(T catalog_item, Object[] params) {
        this(catalog_item.getName(), params);
    }

    @Override
    public AbstractTraceElement<T> clone() {
        AbstractTraceElement<T> clone = this.cloneImpl();
        clone.start_timestamp = this.start_timestamp;
        clone.stop_timestamp = this.stop_timestamp;
        clone.aborted = this.aborted;
        clone.output = this.output;
        clone.output_types = this.output_types;
        clone.weight = this.weight;
        return (clone);
    }

    protected abstract <X> X cloneImpl();

    public void setWeight(int weight) {
        this.weight = (short) weight;
    }

    public void incrementWeight(int delta) {
        this.weight += delta;
    }

    public int getWeight() {
        return (this.weight);
    }

    @Override
    public String toString() {
        return (this.getClreplaced().getSimpleName() + "[" + this.catalog_item_name + "]");
    }

    public void stop() {
        this.stop_timestamp = System.nanoTime();
    }

    public void abort() {
        this.stop();
        this.aborted = true;
    }

    public boolean isStopped() {
        return (this.stop_timestamp != null);
    }

    public boolean isAborted() {
        return (this.aborted);
    }

    public void setTimestamps(Long start, Long stop) {
        this.start_timestamp = start;
        this.stop_timestamp = stop;
    }

    /**
     * @return the start_timestamp
     */
    public Long getStartTimestamp() {
        return this.start_timestamp;
    }

    /**
     * @return the stop_timestamp
     */
    public Long getStopTimestamp() {
        return this.stop_timestamp;
    }

    public String getCatalogItemName() {
        return this.catalog_item_name;
    }

    public abstract T getCatalogItem(Database catalog_db);

    public Boolean getAborted() {
        return aborted;
    }

    /**
     * Get the number of parameters that this trace element has
     * @return
     */
    public int getParamCount() {
        return (this.params.length);
    }

    /**
     * Get the parameters object array for this trace element
     * @return
     */
    public Object[] getParams() {
        return this.params;
    }

    /**
     * @return the params
     */
    @SuppressWarnings("unchecked")
    public <U> U getParam(int i) {
        return (U) this.params[i];
    }

    public void setParam(int i, Object value) {
        this.params[i] = value;
    }

    public boolean hasOutput() {
        return (this.output != null);
    }

    public void setOutput(Object[][]... output) {
        if (output == null || output.length == 0)
            return;
        this.output = output;
        // Build Output Types
        this.output_types = new VoltType[this.output.length][];
        for (int i = 0; i < this.output.length; i++) {
            Object[][] data = this.output[i];
            if (data == null)
                continue;
            Integer missing = null;
            for (int j = 0; j < data.length; j++) {
                Object[] row = data[j];
                if (row == null)
                    continue;
                if (missing == null) {
                    missing = row.length;
                    this.output_types[i] = new VoltType[row.length];
                }
                replacedert (missing != null);
                for (int k = 0; k < row.length; k++) {
                    if (this.output_types[i][k] != null)
                        continue;
                    Object val = row[k];
                    if (val == null)
                        continue;
                    this.output_types[i][k] = VoltType.typeFromClreplaced(val.getClreplaced());
                    missing--;
                }
                // FOR (columns)
                if (missing == 0)
                    break;
            }
        // FOR (rows)
        }
    // FOR
    }

    public void setOutput(VoltTable... output) {
        if (output == null || output.length == 0)
            return;
        this.output = new Object[output.length][][];
        this.output_types = new VoltType[output.length][];
        for (int i = 0; i < this.output.length; i++) {
            VoltTable vt = output[i];
            if (vt == null)
                continue;
            // TYPES
            this.output_types[i] = new VoltType[vt.getColumnCount()];
            for (int k = 0; k < this.output_types[i].length; k++) {
                this.output_types[i][k] = vt.getColumnType(k);
            }
            // FOR
            // DATA
            this.output[i] = new Object[vt.getRowCount()][vt.getColumnCount()];
            int j = 0;
            while (vt.advanceRow()) {
                VoltTableRow row = vt.getRow();
                for (int k = 0; k < this.output[i][j].length; k++) {
                    this.output[i][j][k] = row.get(k);
                }
                // FOR (columns)
                j++;
            }
        // WHILE (rows)
        }
    // FOR (tables)
    }

    public Object[][][] getOutput() {
        return (this.output);
    }

    public Object[][] getOutput(int idx) {
        return (this.output[idx]);
    }

    public VoltType[] getOutputTypes(int idx) {
        return (this.output_types[idx]);
    }

    /**
     * @param catalog_db
     * @return
     */
    public abstract String debug(Database catalog_db);

    @Override
    public String toJSONString() {
        replacedert (false);
        // Can't be implemented since we always need to have a Database catalog object
        return null;
    }

    public String toJSONString(Database catalog_db) {
        JSONStringer stringer = new JSONStringer();
        try {
            stringer.object();
            this.toJSONString(stringer, catalog_db);
            stringer.endObject();
        } catch (JSONException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return stringer.toString();
    }

    public void toJSONString(JSONStringer stringer, Database catalog_db) throws JSONException {
        stringer.key(Members.NAME.name()).value(this.catalog_item_name);
        stringer.key(Members.START.name()).value(this.start_timestamp);
        stringer.key(Members.STOP.name()).value(this.stop_timestamp);
        stringer.key(Members.ABORTED.name()).value(this.aborted);
        // WEIGHT
        if (this.weight > 1) {
            stringer.key(Members.WEIGHT.name()).value(this.weight);
        }
        // Output Tables
        stringer.key(Members.OUTPUT.name()).array();
        if (this.output != null) {
            for (int i = 0; i < this.output.length; i++) {
                stringer.object();
                // COLUMN TYPES
                stringer.key("TYPES").array();
                VoltType[] types = this.output_types[i];
                if (types != null) {
                    for (int j = 0; j < types.length; j++) {
                        stringer.value((types[j] == null ? VoltType.NULL : types[j]).name());
                    }
                // FOR
                }
                stringer.endArray();
                // DATA
                stringer.key("DATA").array();
                Object[][] data = this.output[i];
                if (data != null) {
                    for (int j = 0; j < data.length; j++) {
                        Object[] row = data[j];
                        stringer.array();
                        for (int k = 0; k < row.length; k++) {
                            stringer.value(data[j][k]);
                        }
                        // FOR
                        stringer.endArray();
                    }
                // FOR
                }
                stringer.endArray();
                stringer.endObject();
            }
        // FOR
        }
        stringer.endArray();
        // This doesn't work because CatalogType doesn't have have we need
        // replacedert(this.catalog_item.getField("parameters") != null);
        // CatalogMap<CatalogType> param_map = (CatalogMap<CatalogType>)this.catalog_item.getField("parameters");
        CatalogMap<? extends CatalogType> param_map = null;
        T catalog_item = this.getCatalogItem(catalog_db);
        if (catalog_item instanceof Procedure) {
            param_map = (CatalogMap<? extends CatalogType>) ((Procedure) catalog_item).getParameters();
        } else {
            param_map = (CatalogMap<? extends CatalogType>) ((Statement) catalog_item).getParameters();
        }
        stringer.key(Members.PARAMS.name()).array();
        for (int i = 0; i < this.params.length; i++) {
            CatalogType catalog_param = param_map.get(i);
            Object param_isarray = catalog_param.getField("isarray");
            if (param_isarray != null && (Boolean) param_isarray) {
                stringer.array();
                Clreplaced<?> type = params[i].getClreplaced();
                replacedert (type.isArray());
                Clreplaced<?> dataType = type.getComponentType();
                // I couldn't think of a better way to do this dynamically..
                // It's not trivial to go from primitive arrays to object arrays
                if (dataType == Long.TYPE) {
                    for (Object value : (long[]) this.params[i]) {
                        stringer.value(value);
                    }
                // FOR
                } else if (dataType == Integer.TYPE) {
                    for (Object value : (int[]) this.params[i]) {
                        stringer.value(value);
                    }
                // FOR
                } else if (dataType == Short.TYPE) {
                    for (Object value : (short[]) this.params[i]) {
                        stringer.value(value);
                    }
                // FOR
                } else if (dataType == Byte.TYPE) {
                    for (Object value : (byte[]) this.params[i]) {
                        stringer.value(value);
                    }
                // FOR
                } else {
                    for (Object value : (Object[]) this.params[i]) {
                        stringer.value(value);
                    }
                // FOR
                }
                stringer.endArray();
            } else {
                stringer.value(this.params[i]);
            }
        }
        // FOR
        stringer.endArray();
    }

    protected <U extends CatalogType> void paramsFromJSONObject(JSONObject object, CatalogMap<U> catalog_params, String param_field) throws Exception {
        replacedert (catalog_params != null);
        final Thread self = Thread.currentThread();
        JSONArray jsonParams = object.getJSONArray(Members.PARAMS.name());
        int num_params = catalog_params.size();
        this.params = new Object[num_params];
        for (int i = 0; i < num_params; i++) {
            // If we ran out of JSON parameters, then just throw in null
            if (i > jsonParams.length()) {
                this.params[i] = null;
                continue;
            }
            U catalog_param = catalog_params.get(i);
            VoltType param_type = VoltType.get(((Integer) catalog_param.getField(param_field)).byteValue());
            // if (param_type == VoltType.TINYINT) param_type = VoltType.INTEGER;
            if (param_type == null) {
                throw new Exception("Parameter type is null for " + catalog_param);
            }
            // We don't know whether we have a StmtParameter or a ProcParameter
            Object _isarray = catalog_param.getField("isarray");
            boolean param_isarray = (_isarray != null && (Boolean) _isarray);
            // HACK: If parameter says its an array, but JSON disagrees, then just
            // treat it is as a String. The Volt guys started using byte arrays instead of
            // strings but didn't add a byte VoltType
            JSONArray jsonInner = null;
            if (param_isarray) {
                if (jsonParams.isNull(i)) {
                    this.params[i] = new Object[0];
                    continue;
                } else {
                    try {
                        jsonInner = jsonParams.getJSONArray(i);
                    } catch (JSONException ex) {
                        if (param_type == VoltType.TINYINT) {
                            param_isarray = false;
                            param_type = VoltType.STRING;
                        } else {
                            LOG.error("Failed to deserialize " + CatalogUtil.getDisplayName(catalog_param) + " [type=" + param_type + ", isarray=" + param_isarray + "]", ex);
                            throw ex;
                        }
                    }
                }
            }
            if (param_isarray) {
                Object[] inner = new Object[jsonInner.length()];
                for (int j = 0; j < jsonInner.length(); j++) {
                    inner[j] = VoltTypeUtil.getObjectFromString(param_type, jsonInner.getString(j), self);
                    if (inner[j] == null) {
                        throw new RuntimeException("Array parameter " + j + " for " + catalog_param + " is null");
                    }
                }
                // FOR
                this.params[i] = VoltTypeUtil.getPrimitiveArray(param_type, inner);
            } else if (jsonParams.isNull(i)) {
                this.params[i] = null;
            } else {
                // System.err.println("[" + i + "] " + jsonParams.getString(i) + " (" + param_type + ")");
                try {
                    this.params[i] = VoltTypeUtil.getObjectFromString(param_type, jsonParams.getString(i), self);
                    if (this.params[i] == null) {
                        throw new Exception(catalog_param + " is null [" + param_type + "]");
                    }
                } catch (Exception ex) {
                    LOG.fatal("Failed to convert param '" + jsonParams.getString(i) + "' to " + param_type + " for " + catalog_param + " in " + CatalogUtil.getDisplayName(catalog_param.getParent()), ex);
                    throw ex;
                }
            }
        }
    // FOR
    }

    protected void fromJSONObject(JSONObject object, Database db) throws JSONException {
        this.start_timestamp = object.getLong(Members.START.name());
        if (!object.isNull(Members.STOP.name())) {
            this.stop_timestamp = object.getLong(Members.STOP.name());
        }
        this.catalog_item_name = object.getString(Members.NAME.name());
        this.aborted = object.getBoolean(Members.ABORTED.name());
        // WEIGHT
        if (object.has(Members.WEIGHT.name())) {
            this.weight = (short) object.getInt(Members.WEIGHT.name());
        }
        // OUTPUT
        JSONArray output_arr = null;
        try {
            output_arr = object.getJSONArray(Members.OUTPUT.name());
        } catch (JSONException ex) {
        // IGNORE
        }
        if (output_arr != null) {
            for (int i = 0, cnt = output_arr.length(); i < cnt; i++) {
                if (i == 0) {
                    this.output = new Object[cnt][][];
                    this.output_types = new VoltType[cnt][];
                }
                JSONObject inner = output_arr.getJSONObject(i);
                replacedert (inner != null);
                // TYPES
                JSONArray types_arr = inner.getJSONArray("TYPES");
                this.output_types[i] = new VoltType[types_arr.length()];
                for (int j = 0; j < this.output_types[i].length; j++) {
                    this.output_types[i][j] = VoltType.typeFromString(types_arr.getString(j));
                }
                // FOR
                // DATA
                JSONArray data_arr = inner.getJSONArray("DATA");
                this.output[i] = new Object[data_arr.length()][this.output_types[i].length];
                for (int j = 0; j < this.output[i].length; j++) {
                    JSONArray row_arr = data_arr.getJSONArray(j);
                    if (row_arr == null)
                        continue;
                    for (int k = 0; k < this.output[i][j].length; k++) {
                        String val_str = row_arr.getString(k);
                        try {
                            this.output[i][j][k] = VoltTypeUtil.getObjectFromString(this.output_types[i][k], val_str);
                        } catch (ParseException ex) {
                            throw new RuntimeException(String.format("Failed to deserialize output %s [%d][%d][%d]", i, j, k));
                        }
                    }
                // FOR (columns)
                }
            // FOR (rows)
            }
        // FOR ( tables)
        }
    }
}

18 View Complete Implementation : SubPlanAssembler.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * For a given filter expression, get the column involved that is part of the table
 * specified. For example, "WHERE F_ID = 2" would return F_ID if F_ID is in the table
 * preplaceded in. For join expressions like, "WHERE F_ID = Q_ID", this returns the column
 * that is in the table preplaceded in.
 *
 * This method just sanity-checks some conditions under which using an index
 * for the given expression would actually make sense and then hands off to
 * getColumnForFilterExpressionRecursive to do the real work.
 *
 * @param table The table we want the column from.
 * @param expr The comparison expression to search.
 * @return The column found or null if none found.
 */
protected Column getColumnForFilterExpression(Table table, AbstractExpression expr) {
    if (expr == null)
        return null;
    // Expression type must be resolvable by an index scan
    if ((expr.getExpressionType() != ExpressionType.COMPARE_EQUAL) && (expr.getExpressionType() != ExpressionType.COMPARE_GREATERTHAN) && (expr.getExpressionType() != ExpressionType.COMPARE_GREATERTHANOREQUALTO) && (expr.getExpressionType() != ExpressionType.COMPARE_LESSTHAN) && (expr.getExpressionType() != ExpressionType.COMPARE_LESSTHANOREQUALTO)) {
        return null;
    }
    Column indexedColumn = getColumnForFilterExpressionRecursive(table, expr);
    if (indexedColumn == null)
        return indexedColumn;
    // EE index comparators do not support expressions on the key.
    // The indexed column must not be part of a sub-expression.
    boolean keyIsExpression = true;
    // Also remember which side contains the key - need this later
    boolean keyIsLeft = false;
    // Already know that the column appears on at most one side of the
    // expression. Can naively check both sides.
    if (expr.getLeft().getExpressionType() == ExpressionType.VALUE_TUPLE) {
        TupleValueExpression tve = (TupleValueExpression) (expr.getLeft());
        if (getTableColumn(table, tve.getColumnName()) == indexedColumn) {
            keyIsExpression = false;
            keyIsLeft = true;
        }
    }
    if (expr.getRight().getExpressionType() == ExpressionType.VALUE_TUPLE) {
        TupleValueExpression tve = (TupleValueExpression) (expr.getRight());
        if (getTableColumn(table, tve.getColumnName()) == indexedColumn) {
            keyIsExpression = false;
            keyIsLeft = false;
        }
    }
    if (keyIsExpression)
        return null;
    // EE index key comparator can not cast keys to the RHS type.
    // Do not choose an index that requires such a cast.
    // Sadly, this restriction is not globally true but I don't
    // think the planner can really predict the index type that
    // the EE's index factory might construct.
    VoltType keyType = keyIsLeft ? expr.getLeft().getValueType() : expr.getRight().getValueType();
    VoltType exprType = keyIsLeft ? expr.getRight().getValueType() : expr.getLeft().getValueType();
    if (!VoltTypeUtil.isAllowableCastForKeyComparator(exprType, keyType)) {
        return null;
    }
    return indexedColumn;
}

18 View Complete Implementation : SavedTableConverter.java
Copyright GNU General Public License v3.0
Author : apavlo
public static VoltTable convertTable(VoltTable inputTable, Table outputTableSchema) throws VoltTypeException {
    VoltTable new_table = CatalogUtil.getVoltTable(outputTableSchema);
    Map<Integer, Integer> column_copy_index_map = computeColumnCopyIndexMap(inputTable, new_table);
    // Copy all the old tuples into the new table
    while (inputTable.advanceRow()) {
        Object[] coerced_values = new Object[new_table.getColumnCount()];
        for (int i = 0; i < new_table.getColumnCount(); i++) {
            if (column_copy_index_map.containsKey(i)) {
                int orig_column_index = column_copy_index_map.get(i);
                coerced_values[i] = inputTable.get(orig_column_index, inputTable.getColumnType(orig_column_index));
            } else {
                // otherwise if it's nullable, insert null,
                Column catalog_column = outputTableSchema.getColumns().get(new_table.getColumnName(i));
                VoltType default_type = VoltType.get((byte) catalog_column.getDefaulttype());
                if (default_type != VoltType.INVALID) {
                    // if there is a default value for this table/column
                    // insert the default value
                    try {
                        coerced_values[i] = VoltTypeUtil.getObjectFromString(default_type, catalog_column.getDefaultvalue());
                    } catch (ParseException e) {
                        String message = "Column: ";
                        message += new_table.getColumnName(i);
                        message += " has an unparseable default: ";
                        message += catalog_column.getDefaultvalue();
                        message += " for VoltType: ";
                        message += default_type.toString();
                        throw new VoltTypeException(message);
                    }
                } else if (catalog_column.getNullable()) {
                    coerced_values[i] = null;
                } else {
                    throw new VoltTypeException("Column: " + new_table.getColumnName(i) + " has no default " + "and null is not permitted");
                }
            }
        }
        new_table.addRow(coerced_values);
    }
    return new_table;
}

18 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public static Object getRandomValue(VoltType type, Random rand) {
    Object ret = null;
    switch(type) {
        // --------------------------------
        // INTEGERS
        // --------------------------------
        case TINYINT:
            ret = Integer.valueOf((byte) Math.abs(rand.nextInt() % 128));
            break;
        case SMALLINT:
            ret = Integer.valueOf((short) Math.abs(rand.nextInt() % 32768));
            break;
        case INTEGER:
            ret = Integer.valueOf(Math.abs(rand.nextInt() % 100000));
            break;
        case BIGINT:
            ret = Long.valueOf(Math.abs(rand.nextInt() % 100000));
            break;
        // --------------------------------
        // FLOATS
        // --------------------------------
        case FLOAT:
        case DECIMAL:
            ret = Double.valueOf(Math.abs(rand.nextDouble()));
            break;
        // --------------------------------
        // STRINGS
        // --------------------------------
        case STRING:
            // int size = rand.nextInt(31) + 1;
            int size = 124;
            String ret_str = "";
            for (int ctr = 0; ctr < size; ctr++) {
                char data = (char) (Math.abs(rand.nextInt()) % 128);
                // 
                // Skip quotation marks
                // 
                if (Character.isLetter(data) == false) {
                    ctr--;
                } else {
                    ret_str += String.valueOf(data);
                }
            }
            ret = ret_str;
            break;
        // --------------------------------
        // TIMESTAMP
        // --------------------------------
        case TIMESTAMP:
            {
                int timestamp = rand.nextInt(VoltTypeUtil.DATE_STOP - VoltTypeUtil.DATE_START) + VoltTypeUtil.DATE_START;
                ret = new TimestampType(Long.valueOf(timestamp * 1000));
                break;
            }
        // --------------------------------
        // INVALID
        // --------------------------------
        default:
            LOG.severe("ERROR: Unable to generate random value for invalid ValueType '" + type + "'");
    }
    // SWITCH
    return (ret);
}

18 View Complete Implementation : AbstractExpression.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 */
public abstract clreplaced AbstractExpression implements JSONString, Cloneable {

    public enum Members {

        TYPE, LEFT, RIGHT, VALUE_TYPE, VALUE_SIZE
    }

    protected String m_id;

    protected ExpressionType m_type;

    protected AbstractExpression m_left = null;

    protected AbstractExpression m_right = null;

    protected VoltType m_valueType = null;

    protected int m_valueSize = 0;

    // used by the planner internally (not needed in the EE)
    public boolean m_isJoiningClause = false;

    public AbstractExpression(ExpressionType type) {
        m_type = type;
    }

    public AbstractExpression(ExpressionType type, AbstractExpression left, AbstractExpression right) {
        this(type);
        m_left = left;
        m_right = right;
    }

    public AbstractExpression() {
    // This is needed for serialization
    }

    public void validate() throws Exception {
        // 
        // Validate our children first
        // 
        if (m_left != null) {
            m_left.validate();
        }
        if (m_right != null) {
            m_right.validate();
        }
        // 
        // Expression Type
        // 
        if (m_type == null) {
            throw new Exception("ERROR: The ExpressionType for '" + this + "' is NULL");
        } else if (m_type == ExpressionType.INVALID) {
            throw new Exception("ERROR: The ExpressionType for '" + this + "' is " + m_type);
        // 
        // Output Type
        // 
        } else if (m_valueType == null) {
            throw new Exception("ERROR: The output VoltType for '" + this + "' is NULL");
        } else if (m_valueType == VoltType.INVALID) {
            throw new Exception("ERROR: The output VoltType for '" + this + "' is " + m_valueType);
        }
        // 
        // Since it is possible for an AbstractExpression to be stored with
        // any ExpressionType, we do a simple check to make sure that it is the right clreplaced
        // 
        Clreplaced<?> check_clreplaced = m_type.getExpressionClreplaced();
        if (!check_clreplaced.isInstance(this)) {
            throw new Exception("ERROR: Expression '" + this + "' is clreplaced type '" + getClreplaced().getSimpleName() + "' but needs to be '" + check_clreplaced.getSimpleName() + "'");
        }
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        AbstractExpression clone = (AbstractExpression) super.clone();
        // clone.m_id = m_id;
        // clone.m_isJoiningClause = m_isJoiningClause;
        // clone.m_type = m_type;
        // clone.m_valueType = m_valueType;
        // clone.m_valueSize = m_valueSize;
        if (m_left != null) {
            AbstractExpression left_clone = (AbstractExpression) m_left.clone();
            clone.m_left = left_clone;
        }
        if (m_right != null) {
            AbstractExpression right_clone = (AbstractExpression) m_right.clone();
            clone.m_right = right_clone;
        }
        return clone;
    }

    /**
     * @return the id
     */
    public String getId() {
        return m_id;
    }

    public boolean isJoiningClause() {
        return m_isJoiningClause;
    }

    /**
     * @param id the id to set
     */
    /*public void setId(String id) {
        m_id = id;
    }*/
    /**
     * @return the type
     */
    public ExpressionType getExpressionType() {
        return m_type;
    }

    /**
     * @param type
     */
    public void setExpressionType(ExpressionType type) {
        m_type = type;
    }

    /**
     * @return the left
     */
    public AbstractExpression getLeft() {
        return m_left;
    }

    /**
     * @param left the left to set
     */
    public void setLeft(AbstractExpression left) {
        m_left = left;
    }

    /**
     * @return the right
     */
    public AbstractExpression getRight() {
        return m_right;
    }

    /**
     * @param right the right to set
     */
    public void setRight(AbstractExpression right) {
        m_right = right;
    }

    /**
     * @return The type of this expression's value.
     */
    public VoltType getValueType() {
        return m_valueType;
    }

    /**
     * @param type The type of this expression's value.
     */
    public void setValueType(VoltType type) {
        m_valueType = type;
    }

    /**
     * @return The size of this expression's value in bytes.
     */
    public int getValueSize() {
        return m_valueSize;
    }

    /**
     * @param size The size of this expression's value in bytes.
     */
    public void setValueSize(int size) {
        replacedert (size >= 0);
        replacedert (size <= 10000000);
        m_valueSize = size;
    }

    @Override
    public String toString() {
        return "Expression: " + toJSONString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof AbstractExpression == false)
            return false;
        AbstractExpression expr = (AbstractExpression) obj;
        // check that the presence, or lack thereof, of children is the same
        if ((expr.m_left == null) != (m_left == null))
            return false;
        if ((expr.m_right == null) != (m_right == null))
            return false;
        // check that the children identify themselves as the same
        if (expr.m_left != null)
            if (expr.m_left.equals(m_left) == false)
                return false;
        if (expr.m_right != null)
            if (expr.m_right.equals(m_right) == false)
                return false;
        if (m_type != expr.m_type)
            return false;
        if (m_valueSize != expr.m_valueSize)
            return false;
        if (m_isJoiningClause != expr.m_isJoiningClause)
            return false;
        if (m_valueType != expr.m_valueType)
            return false;
        // this abstract base clreplaced gets here if the children verify local members
        return true;
    }

    public String toJSONString() {
        JSONStringer stringer = new JSONStringer();
        try {
            stringer.object();
            toJSONString(stringer);
            stringer.endObject();
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return stringer.toString();
    }

    public void toJSONString(JSONStringer stringer) throws JSONException {
        stringer.key(Members.TYPE.name()).value(m_type.toString());
        stringer.key(Members.VALUE_TYPE.name()).value(m_valueType == null ? null : m_valueType.name());
        stringer.key(Members.VALUE_SIZE.name()).value(m_valueSize);
        if (m_left != null) {
            replacedert (m_left instanceof JSONString);
            stringer.key(Members.LEFT.name()).value(m_left);
        }
        if (m_right != null) {
            replacedert (m_right instanceof JSONString);
            stringer.key(Members.RIGHT.name()).value(m_right);
        }
    }

    abstract protected void loadFromJSONObject(JSONObject obj, Database db) throws JSONException;

    public static AbstractExpression fromJSONObject(JSONObject obj, Database db) throws JSONException {
        ExpressionType type = ExpressionType.valueOf(obj.getString(Members.TYPE.name()));
        AbstractExpression expr;
        try {
            expr = type.getExpressionClreplaced().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        expr.m_type = type;
        expr.m_valueType = VoltType.typeFromString(obj.getString(Members.VALUE_TYPE.name()));
        expr.m_valueSize = obj.getInt(Members.VALUE_SIZE.name());
        JSONObject leftObject = null;
        if (!obj.isNull(Members.LEFT.name())) {
            try {
                leftObject = obj.getJSONObject(Members.LEFT.name());
            } catch (JSONException e) {
            // ok for it not to be there.
            }
        }
        if (leftObject != null) {
            expr.m_left = AbstractExpression.fromJSONObject(obj.getJSONObject(Members.LEFT.name()), db);
        }
        JSONObject rightObject = null;
        if (!obj.isNull(Members.RIGHT.name())) {
            try {
                rightObject = obj.getJSONObject(Members.RIGHT.name());
            } catch (JSONException e) {
            // ok for it not to be there.
            }
        }
        if (rightObject != null) {
            expr.m_right = AbstractExpression.fromJSONObject(obj.getJSONObject(Members.RIGHT.name()), db);
        }
        expr.loadFromJSONObject(obj, db);
        return expr;
    }
}

18 View Complete Implementation : ProcEnv.java
Copyright GNU General Public License v3.0
Author : apavlo
public static VoltType[] getParaVoltTypes(Procedure proc) {
    CatalogMap<ProcParameter> procParams = proc.getParameters();
    VoltType[] ret = new VoltType[procParams.size()];
    Iterator<ProcParameter> iter = procParams.iterator();
    int i = 0;
    while (iter.hasNext()) {
        ret[i++] = VoltType.get(iter.next().getType());
    }
    return ret;
}

18 View Complete Implementation : VoltTypeUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
public static Object getPrimitiveArray(VoltType type, Object[] objArray) {
    Object ret = null;
    switch(type) {
        // --------------------------------
        // INTEGERS
        // --------------------------------
        case TINYINT:
            {
                Byte[] valArray = new Byte[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = ((Integer) objArray[i]).byteValue();
                }
                // FOR
                ret = valArray;
                break;
            }
        case SMALLINT:
            {
                Short[] valArray = new Short[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = ((Integer) objArray[i]).shortValue();
                }
                // FOR
                ret = valArray;
                break;
            }
        case INTEGER:
            {
                Integer[] valArray = new Integer[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (Integer) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        case BIGINT:
            {
                Long[] valArray = new Long[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (Long) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        // --------------------------------
        // FLOATS
        // --------------------------------
        case FLOAT:
        case DECIMAL:
            {
                Double[] valArray = new Double[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (Double) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        // --------------------------------
        // STRINGS
        // --------------------------------
        case STRING:
            {
                String[] valArray = new String[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (String) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        // --------------------------------
        // TIMESTAMP
        // --------------------------------
        case TIMESTAMP:
            {
                TimestampType[] valArray = new TimestampType[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (TimestampType) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        // --------------------------------
        // BOOLEAN
        // --------------------------------
        case BOOLEAN:
            {
                Boolean[] valArray = new Boolean[objArray.length];
                for (int i = 0; i < objArray.length; i++) {
                    valArray[i] = (Boolean) objArray[i];
                }
                // FOR
                ret = valArray;
                break;
            }
        // --------------------------------
        // NULL
        // --------------------------------
        case NULL:
            ret = null;
            break;
        // --------------------------------
        // INVALID
        // --------------------------------
        default:
            {
                String msg = "Unable to get object for value with invalid ValueType '" + type + "'";
                throw new RuntimeException(msg);
            }
    }
    // SWITCH
    return (ret);
}

18 View Complete Implementation : TestPartitionEstimator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * testGetParreplacedionsInsert
 */
public void testInsert() throws Exception {
    Table catalog_tbl = this.getTable(TPCCConstants.TABLENAME_ORDERS);
    replacedertNotNull(catalog_tbl);
    Procedure catalog_proc = this.getProcedure("neworder");
    replacedertNotNull(catalog_proc);
    Statement catalog_stmt = catalog_proc.getStatements().get("createOrder");
    replacedertNotNull(catalog_stmt);
    ParreplacedionEstimator estimator = new ParreplacedionEstimator(catalogContext, hasher);
    Object[] params = new Object[catalog_stmt.getParameters().size()];
    int w_id = 9;
    // 3001, 1, 9, 376, Mon Aug 10 00:28:54 EDT 2009, 0, 13, 1]
    for (int i = 0; i < params.length; i++) {
        StmtParameter catalog_param = catalog_stmt.getParameters().get(i);
        VoltType type = VoltType.get((byte) catalog_param.getJavatype());
        if (i == 2) {
            params[i] = w_id;
        } else {
            params[i] = VoltTypeUtil.getRandomValue(type);
        }
    // System.out.print((i != 0 ? ", " : "[") + params[i].toString() + (i + 1 == params.length ? "\n" : ""));
    }
    // FOR
    estimator.getAllParreplacedions(parreplacedions, catalog_stmt, params, BASE_PARreplacedION);
    // System.out.println(catalog_stmt.getName() + " Parreplacedions: " + parreplacedions);
    replacedertFalse(parreplacedions.isEmpty());
    replacedertEquals(1, parreplacedions.size());
    replacedertEquals(w_id, (int) CollectionUtil.first(parreplacedions));
}

18 View Complete Implementation : TestTransactionTrace.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * testFromJSONString
 */
public void testFromJSONString() throws Exception {
    Object[][] output = new Object[][] { { new Long(1), new String("ABC"), new Long(2), new Float(2.0f) }, { new Long(3), new String("XYZ"), new Long(4), new Float(6.0f) }, { new Long(5), new String("123"), new Long(2), new Float(18.0f) } };
    replacedertFalse(xact.hasOutput());
    xact.setOutput(output);
    replacedert (xact.hasOutput());
    String json = xact.toJSONString(catalog_db);
    replacedertNotNull(json);
    JSONObject jsonObject = new JSONObject(json);
    // System.err.println(JSONUtil.format(jsonObject));
    TransactionTrace copy = TransactionTrace.loadFromJSONObject(jsonObject, catalog_db);
    replacedertEquals(xact.catalog_item_name, copy.catalog_item_name);
    replacedertEquals(xact.getTransactionId(), copy.getTransactionId());
    replacedertEquals(xact.start_timestamp, copy.start_timestamp);
    replacedertEquals(xact.getQueryCount(), copy.getQueryCount());
    replacedertEquals(xact.getBatchCount(), copy.getBatchCount());
    // OUTPUT
    replacedertEquals(xact.hasOutput(), copy.hasOutput());
    Object[][] copy_output = copy.getOutput(0);
    replacedertNotNull(copy_output);
    replacedertEquals(output.length, copy_output.length);
    for (int i = 0; i < output.length; i++) {
        Object[] orig_row = output[i];
        replacedertNotNull(orig_row);
        Object[] copy_row = copy_output[i];
        replacedertNotNull(copy_row);
        replacedertEquals(orig_row.length, copy_row.length);
        for (int j = 0; j < orig_row.length; j++) {
            replacedertEquals(String.format("[%d, %d]", i, j), orig_row[j].toString(), copy_row[j].toString());
        }
    // FOR
    }
    // FOR
    // OUTPUT TYPES
    VoltType[] expected_types = xact.getOutputTypes(0);
    replacedertNotNull(expected_types);
    VoltType[] actual_types = xact.getOutputTypes(0);
    replacedertNotNull(actual_types);
    replacedertEquals(expected_types.length, actual_types.length);
    for (int i = 0; i < expected_types.length; i++) {
        replacedertEquals(Integer.toString(i), expected_types[i], actual_types[i]);
    }
    // FOR
    // PARAMS
    replacedertEquals(xact.params.length, copy.params.length);
    for (int i = 0; i < xact.params.length; i++) {
        if (is_array[i]) {
            Object[] inner0 = (Object[]) xact.params[i];
            Object[] inner1 = (Object[]) copy.params[i];
            for (int j = 0; j < inner0.length; j++) {
                this.compare(inner0[j], inner1[j]);
            }
        // FOR
        } else {
            this.compare(xact.params[i], copy.params[i]);
        }
    }
// FOR
}

18 View Complete Implementation : TestTransactionTrace.java
Copyright GNU General Public License v3.0
Author : apavlo
protected <T extends CatalogType> Pair<Object[], boolean[]> makeParams(List<T> catalog_params, String type_name) {
    Object[] params = new Object[catalog_params.size()];
    boolean[] is_array = new boolean[catalog_params.size()];
    int array_size = rand.nextInt(10);
    for (int i = 0; i < params.length; i++) {
        VoltType type = VoltType.get(((Integer) catalog_params.get(i).getField(type_name)).byteValue());
        // System.out.println(i + "[" + type_name + "-" + catalog_params.get(i).getField(type_name) + "]: " + type);
        Object param_is_array = catalog_params.get(i).getField("isarray");
        if (param_is_array != null && (Boolean) param_is_array) {
            is_array[i] = true;
            Object[] inner = new Object[array_size];
            for (int j = 0; j < inner.length; j++) {
                inner[j] = VoltTypeUtil.getRandomValue(type);
            }
            // FOR
            params[i] = inner;
        } else {
            is_array[i] = false;
            params[i] = VoltTypeUtil.getRandomValue(type);
        }
    }
    // FOR
    return (new Pair<Object[], boolean[]>(params, is_array));
}

18 View Complete Implementation : VoltDBTableSortedMergeWrangler.java
Copyright Apache License 2.0
Author : brianfrankcooper
/**
 * Get the value we're working with as a Comparable.
 *
 * @param theTable
 * @param columnId
 * @return a Comparable.
 * @throws NeedsToBeComparableException
 */
@SuppressWarnings("rawtypes")
private Comparable getComparable(VoltTable theTable, int columnId) throws NeedsToBeComparableException {
    Comparable c = null;
    VoltType vt = theTable.getColumnType(columnId);
    Object theValue = theTable.get(columnId, vt);
    if (theValue instanceof Comparable) {
        c = (Comparable) theValue;
    } else {
        throw new NeedsToBeComparableException(theValue + ": Only Comparables are supported by VoltDBTableSortedMergeWrangler");
    }
    return c;
}

18 View Complete Implementation : Busted.java
Copyright GNU General Public License v3.0
Author : apavlo
public VoltTable[] run(long d_id) throws VoltAbortException {
    voltQueueSQL(FAIL_SQL, d_id);
    final VoltTable[] d_results = voltExecuteSQL();
    replacedert (d_results.length == 1);
    while (d_results[0].advanceRow()) {
        System.err.print("RESULT (");
        String add = "";
        for (int i = 0, cnt = d_results[0].getColumnCount(); i < cnt; i++) {
            VoltType col_type = d_results[0].getColumnType(i);
            System.err.print(add + col_type.name() + "[");
            System.err.print(d_results[0].get(i, col_type));
            System.err.print("]");
            add = ", ";
        }
        // FOR
        System.err.println(")");
    }
    // WHILE
    return (d_results);
}

18 View Complete Implementation : GetDRecord.java
Copyright GNU General Public License v3.0
Author : apavlo
public VoltTable[] run(long d_id) throws VoltAbortException {
    voltQueueSQL(GET_D, d_id);
    final VoltTable[] d_results = voltExecuteSQL();
    replacedert (d_results.length == 1);
    while (d_results[0].advanceRow()) {
        System.err.print("RESULT (");
        String add = "";
        for (int i = 0, cnt = d_results[0].getColumnCount(); i < cnt; i++) {
            System.err.print(add + d_results[0].getLong(i));
            add = ", ";
        }
        // FOR
        long b_id = d_results[0].getLong(1);
        long c_id = d_results[0].getLong(2);
        if (b_id > c_id) {
            voltQueueSQL(GET_B, b_id);
            System.err.print(add + " GET_B(");
        } else {
            voltQueueSQL(GET_C, c_id);
            System.err.print(add + " GET_C(");
        }
        add = "";
        final VoltTable[] inner_results = voltExecuteSQL();
        replacedert (inner_results.length == 0);
        while (inner_results[0].advanceRow()) {
            for (int i = 0, cnt = inner_results[0].getColumnCount(); i < cnt; i++) {
                VoltType col_type = inner_results[0].getColumnType(i);
                System.err.print(add + col_type.name() + "[");
                System.err.print(inner_results[0].get(i, col_type));
                System.err.print("]");
                add = ", ";
            }
        // FOR
        }
        // WHILE
        System.err.println("))");
        return (inner_results);
    }
    // WHILE
    return (null);
}

18 View Complete Implementation : MemoryEstimator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns the estimate size of a tuple in bytes
 *
 * @param catalog_tbl
 * @return
 */
public static Long estimateTupleSize(Table catalog_tbl, Statement catalog_stmt, Object[] params) throws Exception {
    Long bytes = null;
    // If the table contains nothing but numeral values, then we don't need
    // to loop through and calculate the estimated tuple size each time
    // around,
    // since it's always going to be the same
    bytes = TABLE_TUPLE_SIZE.get(catalog_tbl);
    if (bytes != null)
        return (bytes);
    // Otherwise, we have to calculate things.
    // Then pluck out all the MaterializePlanNodes so that we inspect the
    // tuples
    AbstractPlanNode node = PlanNodeUtil.getRootPlanNodeForStatement(catalog_stmt, true);
    Collection<MaterializePlanNode> matched_nodes = PlanNodeUtil.getPlanNodes(node, MaterializePlanNode.clreplaced);
    if (matched_nodes.isEmpty()) {
        LOG.fatal("Failed to retrieve any MaterializePlanNodes from " + catalog_stmt);
        return 0l;
    } else if (matched_nodes.size() > 1) {
        LOG.fatal("Unexpectadly found more than one MaterializePlanNode in " + catalog_stmt);
        return 0l;
    }
    // MaterializePlanNode mat_node =
    // (MaterializePlanNode)CollectionUtil.getFirst(matched_nodes);
    // This obviously isn't going to be exact because they may be inserting
    // from a SELECT statement or the columns might complex
    // AbstractExpressions
    // That's ok really, because all we really need to do is look at size of
    // the strings
    bytes = 0l;
    boolean numerals_only = true;
    for (Column catalog_col : CatalogUtil.getSortedCatalogItems(catalog_tbl.getColumns(), "index")) {
        VoltType type = VoltType.get((byte) catalog_col.getType());
        switch(type) {
            case TINYINT:
                bytes += 1;
                break;
            case SMALLINT:
                bytes += 2;
                break;
            case INTEGER:
                bytes += 4;
                break;
            case BIGINT:
            case FLOAT:
            case TIMESTAMP:
                bytes += 8;
                break;
            case STRING:
                {
                    numerals_only = false;
                    // if (params[catalog_col.getIndex()] != null) {
                    // bytes += 8 * ((String)
                    // params[catalog_col.getIndex()]).length();
                    // }
                    // XXX
                    bytes += 8 * catalog_col.getSize();
                    /*
                     * AbstractExpression root_exp =
                     * mat_node.getOutputColumnExpressions
                     * ().get(catalog_col.getIndex()); for
                     * (ParameterValueExpression value_exp :
                     * ExpressionUtil.getExpressions(root_exp,
                     * ParameterValueExpression.clreplaced)) { int param_idx =
                     * value_exp.getParameterId(); bytes += 8 *
                     * ((String)params[param_idx]).length(); } // FOR
                     */
                    break;
                }
            default:
                LOG.warn("Unsupported VoltType: " + type);
        }
    // SWITCH
    }
    // FOR
    // If the table only has numerals, then we can store it in our cache
    if (numerals_only)
        TABLE_TUPLE_SIZE.put(catalog_tbl, bytes);
    return (bytes);
}

18 View Complete Implementation : MemoryEstimator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Returns the estimate size of a tuple for the given table in bytes
 * Calculations are based on the Table's Columns specification
 *
 * @param catalog_tbl
 * @return
 */
public static long estimateTupleSize(Table catalog_tbl) {
    long bytes = 0;
    final String table_key = CatalogKey.createKey(catalog_tbl);
    // If the table contains nothing but numeral values, then we don't need
    // to loop
    // through and calculate the estimated tuple size each time around,
    // since it's always
    // going to be the same
    if (CACHE_TABLE_ESTIMATE.containsKey(table_key)) {
        return (CACHE_TABLE_ESTIMATE.get(table_key));
    }
    // This obviously isn't going to be exact because they may be inserting
    // from a SELECT statement or the columns might complex
    // AbstractExpressions
    // That's ok really, because all we really need to do is look at size of
    // the strings
    boolean numerals_only = true;
    for (Column catalog_col : CatalogUtil.getSortedCatalogItems(catalog_tbl.getColumns(), "index")) {
        VoltType type = VoltType.get((byte) catalog_col.getType());
        switch(type) {
            case TINYINT:
                bytes += 1;
                break;
            case SMALLINT:
                bytes += 2;
                break;
            case INTEGER:
                bytes += 4;
                break;
            case BIGINT:
            case FLOAT:
            case TIMESTAMP:
                bytes += 8;
                break;
            case STRING:
                // replacedume always max size
                bytes += catalog_col.getSize();
                break;
            default:
                LOG.fatal("Unsupported VoltType: " + type);
        }
    // SWITCH
    }
    // FOR
    // If the table only has numerals, then we can store it in our cache
    if (numerals_only)
        CACHE_TABLE_ESTIMATE.put(table_key, bytes);
    return (bytes);
}

18 View Complete Implementation : ArgumentsParser.java
Copyright GNU General Public License v3.0
Author : apavlo
@SuppressWarnings("unchecked")
public <T> T getOptParam(int idx, VoltType vt) {
    replacedert (idx >= 0);
    String val = (idx < this.opt_params.size() ? this.opt_params.get(idx) : null);
    if (val != null) {
        try {
            return ((T) VoltTypeUtil.getObjectFromString(vt, val));
        } catch (ParseException ex) {
            throw new RuntimeException("Failed to cast optional parameter " + idx + " [value=" + val + "]", ex);
        }
    }
    return (null);
}

18 View Complete Implementation : JSONUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * For the given JSON string, figure out what kind of object it is and
 * return it
 *
 * @param json_value
 * @param field_clreplaced
 * @param catalog_db
 * @return
 * @throws Exception
 */
private static Object getPrimitiveValue(String json_value, Clreplaced<?> field_clreplaced, Database catalog_db) throws Exception {
    Object value = null;
    // VoltDB Catalog Object
    if (ClreplacedUtil.getSuperClreplacedes(field_clreplaced).contains(CatalogType.clreplaced)) {
        @SuppressWarnings("unchecked")
        Clreplaced<? extends CatalogType> catalog_clreplaced = (Clreplaced<? extends CatalogType>) field_clreplaced;
        try {
            value = CatalogKey.getFromKey(catalog_db, json_value, catalog_clreplaced);
        } catch (Throwable ex) {
            throw new Exception("Failed to get catalog object from \"" + json_value + "\"", ex);
        }
        if (value == null)
            throw new JSONException("Failed to get catalog object from \"" + json_value + "\"");
    } else // Clreplaced
    if (field_clreplaced.equals(Clreplaced.clreplaced)) {
        value = ClreplacedUtil.getClreplaced(json_value);
        if (value == null)
            throw new JSONException("Failed to get clreplaced from '" + json_value + "'");
    } else // Enum
    if (field_clreplaced.isEnum()) {
        if (field_clreplaced.equals(VoltType.clreplaced)) {
            json_value = json_value.replace("VoltType.", "");
        }
        for (Object o : field_clreplaced.getEnumConstants()) {
            Enum<?> e = (Enum<?>) o;
            if (json_value.equals(e.name()))
                return (e);
        }
        // FOR
        throw new JSONException("Invalid enum value '" + json_value + "': " + Arrays.toString(field_clreplaced.getEnumConstants()));
    } else // Boolean
    if (field_clreplaced.equals(Boolean.clreplaced) || field_clreplaced.equals(boolean.clreplaced)) {
        // We have to use field_clreplaced.equals() because the value may be null
        value = Boolean.parseBoolean(json_value);
    } else // Integer
    if (field_clreplaced.equals(Integer.clreplaced) || field_clreplaced.equals(int.clreplaced)) {
        value = Integer.parseInt(json_value);
    } else // Float
    if (field_clreplaced.equals(Float.clreplaced) || field_clreplaced.equals(float.clreplaced)) {
        value = Float.parseFloat(json_value);
    } else // JSONSerializable
    if (ClreplacedUtil.getInterfaces(field_clreplaced).contains(JSONSerializable.clreplaced)) {
        value = ClreplacedUtil.newInstance(field_clreplaced, null, null);
        ((JSONSerializable) value).fromJSON(new JSONObject(json_value), catalog_db);
    } else // Everything else
    {
        // LOG.debug(json_value + " -> " + field_clreplaced);
        VoltType volt_type = VoltType.typeFromClreplaced(field_clreplaced);
        value = VoltTypeUtil.getObjectFromString(volt_type, json_value);
    }
    return (value);
}

17 View Complete Implementation : MappingCalculator.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Process a single transaction trace
 * @param xact_trace
 * @throws Exception
 */
public void processTransaction(TransactionTrace xact_trace) throws Exception {
    if (trace.val)
        LOG.trace("Processing correlations for " + xact_trace);
    Procedure catalog_proc = xact_trace.getCatalogItem(this.catalog_db);
    replacedert (catalog_proc != null);
    ProcedureMappings correlation = this.mappings.get(catalog_proc);
    correlation.start();
    // Cast all the ProcParameters once in the beginning
    Number[][] xact_params = new Number[xact_trace.getParams().length][];
    for (int i = 0; i < xact_params.length; i++) {
        ProcParameter catalog_proc_param = catalog_proc.getParameters().get("index", i);
        replacedert (catalog_proc_param != null);
        VoltType proc_param_type = VoltType.get(catalog_proc_param.getType());
        try {
            // Arrays
            if (catalog_proc_param.getIsarray()) {
                Object[] param_arr = xact_trace.getParam(i);
                xact_params[i] = new Number[param_arr.length];
                for (int ii = 0; ii < param_arr.length; ii++) {
                    xact_params[i][ii] = this.getParamAsNumber(proc_param_type, param_arr[ii]);
                }
            // FOR
            // Scalars (just store in the first element of the array
            } else {
                xact_params[i] = new Number[] { this.getParamAsNumber(proc_param_type, xact_trace.getParam(i)) };
            }
        } catch (Exception ex) {
            LOG.error("Failed to process " + CatalogUtil.getDisplayName(catalog_proc_param));
            throw ex;
        }
    }
    // FOR
    // Now run through all of the queries and calculate the correlation between StmtParameters and ProcParameters
    for (QueryTrace query_trace : xact_trace.getQueries()) {
        Statement catalog_stmt = query_trace.getCatalogItem(this.catalog_db);
        QueryInstance query_instance = correlation.getQueryInstance(catalog_stmt);
        Object[] query_params = query_trace.getParams();
        // For each of the StmtParameter, update the correlation information for each of the ProcParameters
        for (int i = 0; i < query_params.length; i++) {
            StmtParameter catalog_stmt_param = catalog_stmt.getParameters().get(i);
            replacedert (catalog_stmt_param != null);
            VoltType stmt_param_type = VoltType.get(catalog_stmt_param.getJavatype());
            replacedert (stmt_param_type != VoltType.INVALID);
            Number stmt_param_val = this.getParamAsNumber(stmt_param_type, query_params[i]);
            for (int ii = 0; ii < xact_params.length; ii++) {
                ProcParameter catalog_proc_param = catalog_proc.getParameters().get(ii);
                replacedert (catalog_proc_param != null) : "Missing ProcParameter in " + catalog_proc + " at index " + ii;
                VoltType proc_param_type = VoltType.get(catalog_proc_param.getType());
                replacedert (proc_param_type != VoltType.INVALID);
                ProcParameterCorrelation ppc = query_instance.getProcParameterCorrelation(catalog_stmt_param, catalog_proc_param);
                for (int iii = 0; iii < xact_params[ii].length; iii++) {
                    ppc.getAbstractCorrelation(iii).addOccurrence(stmt_param_val, xact_params[ii][iii]);
                }
            // FOR
            }
        // FOR (xact_params)
        }
    // FOR (query_params)
    }
    // FOR (query_trace)
    correlation.finish();
}

17 View Complete Implementation : ParametersUtil.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * Return the corresponding StmtParameter value from the ProcParameter ParameterSet
 * using the information provided in the given ParameterMapping.
 * @param params
 * @param pm
 * @return
 */
public static Object getValue(ParameterSet params, ParameterMapping pm) {
    Object val = null;
    Object orig = params.toArray()[pm.procedure_parameter.getIndex()];
    VoltType vtype = VoltType.get(pm.procedure_parameter.getType());
    if (pm.procedure_parameter.getIsarray()) {
        replacedert (pm.procedure_parameter_index != ParametersUtil.NULL_PROC_PARAMETER_OFFSET);
        switch(vtype) {
            case TINYINT:
                {
                    if (orig instanceof byte[])
                        val = ((byte[]) orig)[pm.procedure_parameter_index];
                    else
                        val = ((Byte[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            case SMALLINT:
                {
                    if (orig instanceof short[])
                        val = ((short[]) orig)[pm.procedure_parameter_index];
                    else
                        val = ((Short[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            case INTEGER:
                {
                    if (orig instanceof int[])
                        val = ((int[]) orig)[pm.procedure_parameter_index];
                    else
                        val = ((Integer[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            case BIGINT:
                {
                    if (orig instanceof long[])
                        val = ((long[]) orig)[pm.procedure_parameter_index];
                    else
                        val = ((Long[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            case FLOAT:
                {
                    if (orig instanceof float[])
                        val = ((float[]) orig)[pm.procedure_parameter_index];
                    else if (orig instanceof double[])
                        val = ((double[]) orig)[pm.procedure_parameter_index];
                    else if (orig instanceof Float[])
                        val = ((Float[]) orig)[pm.procedure_parameter_index];
                    else
                        val = ((Double[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            case TIMESTAMP:
                {
                    val = ((TimestampType[]) orig)[pm.procedure_parameter_index];
                    break;
                }
            default:
                val = ((Object[]) orig)[pm.procedure_parameter_index];
        }
    // SWITCH
    } else {
        val = params.toArray()[pm.procedure_parameter.getIndex()];
    }
    return (val);
}

17 View Complete Implementation : ObjectHistogram.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * A very nice and simple generic Histogram
 *
 * @author svelagap
 * @author pavlo
 */
public clreplaced ObjectHistogram<X> implements Histogram<X> {

    private static final Logger LOG = Logger.getLogger(ObjectHistogram.clreplaced);

    public enum Members {

        VALUE_TYPE, HISTOGRAM, NUM_SAMPLES, KEEP_ZERO_ENTRIES
    }

    protected VoltType value_type = VoltType.INVALID;

    protected final Map<X, Long> histogram = new HashMap<X, Long>();

    protected int num_samples = 0;

    private transient boolean dirty = false;

    private transient Map<Object, String> debug_names;

    private transient boolean debug_percentages = false;

    /**
     * The Min/Max values are the smallest/greatest values we have seen based
     * on some natural ordering
     */
    private transient Comparable<X> min_value;

    protected transient Comparable<X> max_value;

    /**
     * The Min/Max counts are the values that have the smallest/greatest number of
     * occurrences in the histogram
     */
    protected transient long min_count = 0;

    protected transient List<X> min_count_values;

    protected transient long max_count = 0;

    protected transient List<X> max_count_values;

    /**
     * A switchable flag that determines whether non-zero entries are kept or removed
     */
    protected boolean keep_zero_entries = false;

    /**
     * Constructor
     */
    public ObjectHistogram() {
    // Nothing...
    }

    /**
     * Constructor
     * @param keepZeroEntries
     */
    public ObjectHistogram(boolean keepZeroEntries) {
        this.keep_zero_entries = keepZeroEntries;
    }

    /**
     * Copy Constructor.
     * This is the same as calling putHistogram()
     * @param other
     */
    public ObjectHistogram(ObjectHistogram<X> other) {
        replacedert (other != null);
        this.put(other);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ObjectHistogram<?>) {
            ObjectHistogram<?> other = (ObjectHistogram<?>) obj;
            return (this.histogram.equals(other.histogram));
        }
        return (false);
    }

    @Override
    public ObjectHistogram<X> setKeepZeroEntries(boolean flag) {
        // When this option is disabled, we need to remove all of the zeroed entries
        if (!flag && this.keep_zero_entries) {
            synchronized (this) {
                Iterator<X> it = this.histogram.keySet().iterator();
                int ctr = 0;
                while (it.hasNext()) {
                    X key = it.next();
                    if (this.histogram.get(key) == 0) {
                        it.remove();
                        ctr++;
                        this.dirty = true;
                    }
                }
                // WHILE
                if (ctr > 0)
                    LOG.debug("Removed " + ctr + " zero entries from histogram");
            }
        // SYNCHRONIZED
        }
        this.keep_zero_entries = flag;
        return (this);
    }

    @Override
    public boolean isZeroEntriesEnabled() {
        return this.keep_zero_entries;
    }

    /**
     * The main method that updates a value in the histogram with a given sample count
     * This should be called by one of the public interface methods that are synchronized
     * This method is not synchronized on purpose for performance
     * @param value
     * @param count
     * @return Return the new count for the given key
     */
    private long _put(X value, long count) {
        // If we're giving a null value, then the count will always be zero
        if (value == null)
            return (0);
        // HACK: Try to infer the internal type if we don't have it already
        if (this.value_type == VoltType.INVALID) {
            try {
                this.value_type = VoltType.typeFromClreplaced(value.getClreplaced());
            } catch (VoltTypeException ex) {
                this.value_type = VoltType.NULL;
            }
        }
        this.num_samples += count;
        // If we already have this value in our histogram, then add the new
        // count to its existing total
        Long existing = this.histogram.get(value);
        if (existing != null) {
            count += existing.longValue();
        }
        // We can't have a negative value
        if (count < 0) {
            String msg = String.format("Invalid negative count for key '%s' [count=%d]", value, count);
            throw new IllegalArgumentException(msg);
        } else // If the new count is zero, then completely remove it if we're not
        // allowed to have zero entries
        if (count == 0 && this.keep_zero_entries == false) {
            this.histogram.remove(value);
        } else // Otherwise throw it into our map
        {
            this.histogram.put(value, Long.valueOf(count));
        }
        // Mark ourselves as dirty so that we will always recompute
        // internal values (min/max) when they ask for them
        this.dirty = true;
        return (count);
    }

    /**
     * Recalculate the min/max count value sets
     * Since this is expensive, this should only be done whenever that information is needed
     */
    @SuppressWarnings("unchecked")
    private synchronized void calculateInternalValues() {
        // Do this before we check before we're dirty
        if (this.min_count_values == null)
            this.min_count_values = new ArrayList<X>();
        if (this.max_count_values == null)
            this.max_count_values = new ArrayList<X>();
        if (this.dirty == false)
            return;
        // New Min/Max Counts
        // The reason we have to loop through and check every time is that our
        // value may be the current min/max count and thus it may or may not still
        // be after the count is changed
        this.max_count = 0;
        this.min_count = Long.MAX_VALUE;
        this.min_value = null;
        this.max_value = null;
        for (Entry<X, Long> e : this.histogram.entrySet()) {
            X value = e.getKey();
            long cnt = e.getValue().longValue();
            // Is this value the new min/max values?
            if (this.min_value == null || this.min_value.compareTo(value) > 0) {
                this.min_value = (Comparable<X>) value;
            } else if (this.max_value == null || this.max_value.compareTo(value) < 0) {
                this.max_value = (Comparable<X>) value;
            }
            if (cnt <= this.min_count) {
                if (cnt < this.min_count)
                    this.min_count_values.clear();
                this.min_count_values.add(value);
                this.min_count = cnt;
            }
            if (cnt >= this.max_count) {
                if (cnt > this.max_count)
                    this.max_count_values.clear();
                this.max_count_values.add(value);
                this.max_count = cnt;
            }
        }
        // FOR
        this.dirty = false;
    }

    /**
     * Get the number of samples entered into the histogram using the put methods
     * @return
     */
    @Override
    public int getSampleCount() {
        return (this.num_samples);
    }

    /**
     * Get the number of unique values entered into the histogram
     * @return
     */
    @Override
    public int getValueCount() {
        return (this.histogram.values().size());
    }

    /**
     * Return the internal variable for what we "think" the type is for this
     * Histogram Use this at your own risk
     *
     * @return
     */
    public VoltType getEstimatedType() {
        return (this.value_type);
    }

    @Override
    public Collection<X> values() {
        return (Collections.unmodifiableCollection(this.histogram.keySet()));
    }

    @Override
    public Collection<X> getValuesForCount(long count) {
        Set<X> ret = new HashSet<X>();
        for (Entry<X, Long> e : this.histogram.entrySet()) {
            if (e.getValue().longValue() == count)
                ret.add(e.getKey());
        }
        // FOR
        return (ret);
    }

    @Override
    public synchronized void clear() {
        this.histogram.clear();
        this.num_samples = 0;
        this.min_count = 0;
        if (this.min_count_values != null)
            this.min_count_values.clear();
        this.min_value = null;
        this.max_count = 0;
        if (this.max_count_values != null)
            this.max_count_values.clear();
        this.max_value = null;
        replacedert (this.histogram.isEmpty());
        this.dirty = true;
    }

    @Override
    public synchronized void clearValues() {
        if (this.keep_zero_entries) {
            Long zero = Long.valueOf(0);
            for (Entry<X, Long> e : this.histogram.entrySet()) {
                this.histogram.put(e.getKey(), zero);
            }
            // FOR
            this.num_samples = 0;
            this.min_count = 0;
            if (this.min_count_values != null)
                this.min_count_values.clear();
            this.min_value = null;
            this.max_count = 0;
            if (this.max_count_values != null)
                this.max_count_values.clear();
            this.max_value = null;
        } else {
            this.clear();
        }
        this.dirty = true;
    }

    @Override
    public boolean isEmpty() {
        return (this.histogram.isEmpty());
    }

    // ----------------------------------------------------------------------------
    // PUT METHODS
    // ----------------------------------------------------------------------------
    @Override
    public synchronized long put(X value, long delta) {
        return this._put(value, delta);
    }

    @Override
    public synchronized long put(X value) {
        return this._put(value, 1);
    }

    @Override
    public void putAll() {
        this.put(this.histogram.keySet(), 1);
    }

    @Override
    public void put(Collection<X> values) {
        this.put(values, 1);
    }

    @Override
    public synchronized void put(Collection<X> values, long count) {
        for (X v : values) {
            this._put(v, count);
        }
    // FOR
    }

    @Override
    public synchronized void put(Histogram<X> other) {
        if (other == this || other == null)
            return;
        if (other instanceof ObjectHistogram) {
            ObjectHistogram<X> objHistogram = (ObjectHistogram<X>) other;
            for (Entry<X, Long> e : objHistogram.histogram.entrySet()) {
                if (e.getValue().longValue() > 0)
                    this._put(e.getKey(), e.getValue());
            }
        // FOR
        } else {
            for (X value : other.values()) {
                this._put(value, other.get(value));
            }
        // FOR
        }
    }

    // ----------------------------------------------------------------------------
    // DECREMENT METHODS
    // ----------------------------------------------------------------------------
    @Override
    public synchronized long dec(X value, long delta) {
        replacedert (this.histogram.containsKey(value));
        return this._put(value, delta * -1);
    }

    @Override
    public synchronized long dec(X value) {
        return this._put(value, -1);
    }

    @Override
    public synchronized void dec(Collection<X> values) {
        this.dec(values, 1);
    }

    @Override
    public synchronized void dec(Collection<X> values, long delta) {
        for (X v : values) {
            this._put(v, -1 * delta);
        }
    // FOR
    }

    @Override
    public synchronized void dec(Histogram<X> other) {
        if (other instanceof ObjectHistogram) {
            ObjectHistogram<X> objHistogram = (ObjectHistogram<X>) other;
            for (Entry<X, Long> e : objHistogram.histogram.entrySet()) {
                if (e.getValue().longValue() > 0) {
                    this._put(e.getKey(), -1 * e.getValue().longValue());
                }
            }
        // FOR
        } else {
            for (X value : other.values()) {
                this._put(value, -1 * other.get(value));
            }
        // FOR
        }
    }

    // ----------------------------------------------------------------------------
    // MIN/MAX METHODS
    // ----------------------------------------------------------------------------
    @SuppressWarnings("unchecked")
    @Override
    public X getMinValue() {
        this.calculateInternalValues();
        return ((X) this.min_value);
    }

    @SuppressWarnings("unchecked")
    @Override
    public X getMaxValue() {
        this.calculateInternalValues();
        return ((X) this.max_value);
    }

    @Override
    public long getMinCount() {
        this.calculateInternalValues();
        return (this.min_count);
    }

    @Override
    public Collection<X> getMinCountValues() {
        this.calculateInternalValues();
        return (this.min_count_values);
    }

    @Override
    public long getMaxCount() {
        this.calculateInternalValues();
        return (this.max_count);
    }

    @Override
    public Collection<X> getMaxCountValues() {
        this.calculateInternalValues();
        return (this.max_count_values);
    }

    // ----------------------------------------------------------------------------
    // UTILITY METHODS
    // ----------------------------------------------------------------------------
    @Override
    public synchronized long set(X value, long i) {
        Long orig = this.get(value);
        if (orig != null && orig != i) {
            i = (orig > i ? -1 * (orig - i) : i - orig);
        }
        return this._put(value, i);
    }

    @Override
    public synchronized long remove(X value) {
        Long cnt = this.histogram.get(value);
        if (cnt != null && cnt.longValue() > 0) {
            return this._put(value, cnt * -1);
        }
        return 0l;
    }

    // ----------------------------------------------------------------------------
    // GET METHODS
    // ----------------------------------------------------------------------------
    /**
     * Returns the current count for the given value
     * If the value was never entered into the histogram, then the count will be null
     * @param value
     * @return
     */
    public Long get(X value) {
        return (this.histogram.get(value));
    }

    /**
     * Returns the current count for the given value.
     * If that value was nevered entered in the histogram, then the value returned will be value_if_null
     * @param value
     * @param value_if_null
     * @return
     */
    public long get(X value, long value_if_null) {
        Long count = this.histogram.get(value);
        return (count == null ? value_if_null : count.longValue());
    }

    /**
     * Returns true if this histogram contains the specified key.
     * @param value
     * @return
     */
    public boolean contains(X value) {
        return (this.histogram.containsKey(value));
    }

    // ----------------------------------------------------------------------------
    // DEBUG METHODS
    // ----------------------------------------------------------------------------
    @Override
    public Histogram<X> setDebugLabels(Map<?, String> names_map) {
        if (names_map == null) {
            this.debug_names = null;
        } else {
            if (this.debug_names == null) {
                synchronized (this) {
                    if (this.debug_names == null) {
                        this.debug_names = new HashMap<Object, String>();
                    }
                }
            // SYNCH
            }
            this.debug_names.putAll(names_map);
        }
        return (this);
    }

    @Override
    public boolean hasDebugLabels() {
        return (this.debug_names != null && this.debug_names.isEmpty() == false);
    }

    @Override
    public Map<Object, String> getDebugLabels() {
        return (this.debug_names);
    }

    @Override
    public String getDebugLabel(Object key) {
        return (this.debug_names.get(key));
    }

    @Override
    public void enablePercentages() {
        this.debug_percentages = true;
    }

    @Override
    public boolean hasDebugPercentages() {
        return (this.debug_percentages);
    }

    @Override
    public String toString() {
        return HistogramUtil.toString(this);
    }

    @Override
    public String toString(int max_chars) {
        return HistogramUtil.toString(this, max_chars);
    }

    @Override
    public String toString(int max_chars, int max_len) {
        return HistogramUtil.toString(this, max_chars, max_len);
    }

    // ----------------------------------------------------------------------------
    // SERIALIZATION METHODS
    // ----------------------------------------------------------------------------
    public void load(File input_path) throws IOException {
        JSONUtil.load(this, null, input_path);
    }

    @Override
    public void load(File input_path, Database catalog_db) throws IOException {
        JSONUtil.load(this, catalog_db, input_path);
    }

    @Override
    public void save(File output_path) throws IOException {
        JSONUtil.save(this, output_path);
    }

    @Override
    public String toJSONString() {
        return (JSONUtil.toJSONString(this));
    }

    @Override
    public void toJSON(JSONStringer stringer) throws JSONException {
        for (Members element : ObjectHistogram.Members.values()) {
            try {
                Field field = ObjectHistogram.clreplaced.getDeclaredField(element.toString().toLowerCase());
                switch(element) {
                    case HISTOGRAM:
                        {
                            if (this.histogram.isEmpty() == false) {
                                stringer.key(element.name()).object();
                                synchronized (this) {
                                    for (Object value : this.histogram.keySet()) {
                                        stringer.key(value.toString()).value(this.histogram.get(value));
                                    }
                                // FOR
                                }
                                // SYNCH
                                stringer.endObject();
                            }
                            break;
                        }
                    case KEEP_ZERO_ENTRIES:
                        {
                            if (this.keep_zero_entries) {
                                stringer.key(element.name()).value(this.keep_zero_entries);
                            }
                            break;
                        }
                    case VALUE_TYPE:
                        {
                            VoltType vtype = (VoltType) field.get(this);
                            stringer.key(element.name()).value(vtype.name());
                            break;
                        }
                    default:
                        stringer.key(element.name()).value(field.get(this));
                }
            // SWITCH
            } catch (Exception ex) {
                throw new RuntimeException("Failed to serialize '" + element + "'", ex);
            }
        }
    // FOR
    }

    @Override
    public void fromJSON(JSONObject object, Database catalog_db) throws JSONException {
        this.value_type = VoltType.typeFromString(object.get(Members.VALUE_TYPE.name()).toString());
        replacedert (this.value_type != null);
        if (object.has(Members.KEEP_ZERO_ENTRIES.name())) {
            this.setKeepZeroEntries(object.getBoolean(Members.KEEP_ZERO_ENTRIES.name()));
        }
        // This code sucks replaced...
        for (Members element : ObjectHistogram.Members.values()) {
            if (element == Members.VALUE_TYPE || element == Members.KEEP_ZERO_ENTRIES)
                continue;
            try {
                String field_name = element.toString().toLowerCase();
                Field field = ObjectHistogram.clreplaced.getDeclaredField(field_name);
                if (element == Members.HISTOGRAM) {
                    if (object.has(element.name()) == false) {
                        continue;
                    }
                    JSONObject jsonObject = object.getJSONObject(element.name());
                    for (String key_name : CollectionUtil.iterable(jsonObject.keys())) {
                        Object key_value = VoltTypeUtil.getObjectFromString(this.value_type, key_name);
                        Long count = Long.valueOf(jsonObject.getLong(key_name));
                        @SuppressWarnings("unchecked")
                        X x = (X) key_value;
                        this.histogram.put(x, count);
                    }
                // WHILE
                } else if (field_name.endsWith("_count_value")) {
                    @SuppressWarnings("unchecked")
                    Set<Object> set = (Set<Object>) field.get(this);
                    JSONArray arr = object.getJSONArray(element.name());
                    for (int i = 0, cnt = arr.length(); i < cnt; i++) {
                        Object val = VoltTypeUtil.getObjectFromString(this.value_type, arr.getString(i));
                        set.add(val);
                    }
                // FOR
                } else if (field_name.endsWith("_value")) {
                    if (object.isNull(element.name())) {
                        field.set(this, null);
                    } else {
                        Object value = object.get(element.name());
                        field.set(this, VoltTypeUtil.getObjectFromString(this.value_type, value.toString()));
                    }
                } else {
                    field.set(this, object.getInt(element.name()));
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                System.exit(1);
            }
        }
        // FOR
        this.dirty = true;
        this.calculateInternalValues();
    }
}

17 View Complete Implementation : TableStatistics.java
Copyright GNU General Public License v3.0
Author : apavlo
@Override
public void process(Database catalog_db, TransactionTrace xact) throws Exception {
    final Table catalog_tbl = this.getCatalogItem(catalog_db);
    // For each query, check whether they are going to our table
    // If so, then we need to update our statistics
    for (QueryTrace query : xact.getQueries()) {
        Statement catalog_stmt = query.getCatalogItem(catalog_db);
        QueryType query_type = QueryType.get(catalog_stmt.getQuerytype());
        // System.out.println("Examining " + catalog_stmt + " for " +
        // catalog_tbl);
        if (CatalogUtil.getReferencedTables(catalog_stmt).contains(catalog_tbl)) {
            // Query Type Counts
            Long cnt = this.query_type_count.get(query_type);
            if (cnt == null) {
                cnt = 0l;
            }
            this.query_type_count.put(query_type, cnt + 1);
            // Read-only
            if (query_type == QueryType.INSERT || query_type == QueryType.UPDATE || query_type == QueryType.DELETE) {
                this.readonly = false;
            }
            // Now from this point forward we only want to look at INSERTs
            // because
            // that's the only way we can estimate tuple sizes
            if (query_type == QueryType.INSERT) {
                this.tuple_count_total += 1;
                Long bytes = 0l;
                try {
                    bytes = (long) MemoryEstimator.estimateTupleSize(catalog_tbl, catalog_stmt, query.getParams()).intValue();
                } catch (ArrayIndexOutOfBoundsException ex) {
                    // Silently let these preplaced...
                    LOG.debug("Failed to calculate estimated tuple size for " + query, ex);
                }
                this.tuple_size_total += bytes;
                if (bytes != 0 && (this.tuple_size_min == null || this.tuple_size_min > bytes)) {
                    this.tuple_size_min = bytes;
                }
                if (this.tuple_size_max == null || this.tuple_size_max < bytes) {
                    this.tuple_size_max = bytes;
                }
            }
            // Column Stats
            if (query_type == QueryType.INSERT) {
                if (this.target_columns.isEmpty()) {
                    for (Column catalog_col : catalog_tbl.getColumns()) {
                        VoltType col_type = VoltType.get((byte) catalog_col.getType());
                        if (TARGET_COLUMN_TYPES.contains(col_type)) {
                            String col_key = CatalogKey.createKey(catalog_col);
                            this.target_columns.add(this.column_stats.get(col_key));
                        }
                    }
                // FOR
                }
                for (ColumnStatistics col_stats : this.target_columns) {
                    col_stats.process(catalog_db, xact);
                }
            // FOR
            }
        }
    // IF
    }
    // FOR
    return;
}

17 View Complete Implementation : TableDataIterable.java
Copyright GNU General Public License v3.0
Author : apavlo
/**
 * @author pavlo
 */
public clreplaced TableDataIterable implements Iterable<Object[]> {

    private static final Logger LOG = Logger.getLogger(TableDataIterable.clreplaced.getName());

    private final Table catalog_tbl;

    private final File table_file;

    private final CSVReader reader;

    private final VoltType[] types;

    private final boolean[] fkeys;

    private final boolean[] nullable;

    private final boolean auto_generate_first_column;

    private final DateFormat[] timestamp_formats = new DateFormat[] { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") };

    private Set<Column> truncate_warnings = new HashSet<Column>();

    private int line_ctr = 0;

    /**
     * Constructor
     *
     * @param catalog_tbl
     * @param table_file
     * @param has_header
     *            whether we expect the data file to include a header in the
     *            first row
     * @param auto_generate_first_column
     *            TODO
     * @throws Exception
     */
    public TableDataIterable(Table catalog_tbl, File table_file, boolean has_header, boolean auto_generate_first_column) throws Exception {
        this.catalog_tbl = catalog_tbl;
        this.table_file = table_file;
        this.auto_generate_first_column = auto_generate_first_column;
        this.reader = new CSVReader(FileUtil.getReader(this.table_file));
        // Throw away the first row if there is a header
        if (has_header) {
            this.reader.readNext();
            this.line_ctr++;
        }
        // Column Types + Foreign Keys
        // Determine whether the column references a foreign key, and thus will
        // need to be converted to an integer field later
        this.types = new VoltType[catalog_tbl.getColumns().size()];
        this.fkeys = new boolean[this.types.length];
        this.nullable = new boolean[this.types.length];
        for (Column catalog_col : catalog_tbl.getColumns()) {
            int idx = catalog_col.getIndex();
            this.types[idx] = VoltType.get((byte) catalog_col.getType());
            this.fkeys[idx] = (CatalogUtil.getForeignKeyParent(catalog_col) != null);
            this.nullable[idx] = catalog_col.getNullable();
        }
    // FOR
    }

    /**
     * Constructor
     *
     * @param catalog_tbl
     * @param table_file
     * @throws Exception
     */
    public TableDataIterable(Table catalog_tbl, File table_file) throws Exception {
        this(catalog_tbl, table_file, false, false);
    }

    public Iterator<Object[]> iterator() {
        return (new TableIterator());
    }

    public clreplaced TableIterator implements Iterator<Object[]> {

        String[] next = null;

        private void getNext() {
            if (next == null) {
                try {
                    next = reader.readNext();
                } catch (Exception ex) {
                    throw new RuntimeException("Unable to retrieve tuples from '" + table_file + "'", ex);
                }
            }
        }

        @Override
        public boolean hasNext() {
            this.getNext();
            return (next != null);
        }

        @Override
        public Object[] next() {
            this.getNext();
            if (next == null)
                return (next);
            String[] row = null;
            synchronized (this) {
                row = this.next;
                this.next = null;
            }
            // SYNCH
            Object[] tuple = new Object[types.length];
            int row_idx = 0;
            for (int col_idx = 0; col_idx < types.length; col_idx++) {
                Column catalog_col = catalog_tbl.getColumns().get(col_idx);
                replacedert (catalog_col != null) : "The column at position " + col_idx + " for " + catalog_tbl + " is null";
                // Auto-generate first column
                if (col_idx == 0 && auto_generate_first_column) {
                    tuple[col_idx] = new Long(line_ctr);
                } else // Null Values
                if (row_idx >= row.length) {
                    tuple[col_idx] = null;
                } else // Foreign Keys
                if (fkeys[col_idx]) {
                    tuple[col_idx] = row[row_idx++];
                } else // Timestamps
                if (types[col_idx] == VoltType.TIMESTAMP) {
                    for (DateFormat f : timestamp_formats) {
                        try {
                            tuple[col_idx] = f.parse(row[row_idx]);
                        } catch (ParseException ex) {
                        // Ignore...
                        }
                        if (tuple[col_idx] != null)
                            break;
                    }
                    // FOR
                    if (tuple[col_idx] == null) {
                        throw new RuntimeException("Line " + TableDataIterable.this.line_ctr + ": Invalid timestamp format '" + row[row_idx] + "' for " + catalog_col);
                    }
                    row_idx++;
                } else // Store string (truncate if necessary)
                if (types[col_idx] == VoltType.STRING) {
                    // Clip columns that are larger than our limit
                    int limit = catalog_col.getSize();
                    if (row[row_idx].length() > limit) {
                        if (!truncate_warnings.contains(catalog_col)) {
                            LOG.warn("Line " + TableDataIterable.this.line_ctr + ": Truncating data for " + catalog_col.fullName() + " because size " + row[row_idx].length() + " > " + limit);
                            truncate_warnings.add(catalog_col);
                        }
                        row[row_idx] = row[row_idx].substring(0, limit);
                    }
                    tuple[col_idx] = row[row_idx++];
                } else // Default: Cast the string into the proper type
                {
                    if (row[row_idx].isEmpty() && nullable[col_idx]) {
                        tuple[col_idx] = null;
                    } else {
                        try {
                            tuple[col_idx] = VoltTypeUtil.getObjectFromString(types[col_idx], row[row_idx]);
                        } catch (Exception ex) {
                            throw new RuntimeException("Line " + TableDataIterable.this.line_ctr + ": Invalid value for " + catalog_col, ex);
                        }
                    }
                    row_idx++;
                }
            // System.out.println(col_idx + ": " + tuple[col_idx]);
            }
            // FOR
            TableDataIterable.this.line_ctr++;
            return (tuple);
        }

        @Override
        public void remove() {
        // TODO Auto-generated method stub
        }
    }
}