Search in sources :

Example 21 with VoltType

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

the class RegressionSuite method assertApproximateContentOfRow.

private static void assertApproximateContentOfRow(int row, Object[] expectedRow, VoltTable actualRow, double epsilon) {
    assertEquals("Actual row has wrong number of columns", expectedRow.length, actualRow.getColumnCount());
    for (int i = 0; i < expectedRow.length; ++i) {
        String msg = "Row " + row + ", col " + i + ": ";
        Object expectedObj = expectedRow[i];
        if (expectedObj == null) {
            VoltType vt = actualRow.getColumnType(i);
            actualRow.get(i, vt);
            assertTrue(msg, actualRow.wasNull());
        } else if (expectedObj instanceof GeographyPointValue) {
            assertApproximatelyEquals(msg, (GeographyPointValue) expectedObj, actualRow.getGeographyPointValue(i), epsilon);
        } else if (expectedObj instanceof GeographyValue) {
            assertApproximatelyEquals(msg, (GeographyValue) expectedObj, actualRow.getGeographyValue(i), epsilon);
        } else if (expectedObj instanceof Long) {
            long val = ((Long) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Integer) {
            long val = ((Integer) expectedObj).longValue();
            assertEquals(msg, val, actualRow.getLong(i));
        } else if (expectedObj instanceof Double) {
            double expectedValue = (Double) expectedObj;
            double actualValue = actualRow.getDouble(i);
            // for null values, convert value into double min
            if (actualRow.wasNull()) {
                actualValue = Double.MIN_VALUE;
            }
            if (epsilon <= 0) {
                String fullMsg = msg + String.format("Expected value %f != actual value %f", expectedValue, actualValue);
                assertEquals(fullMsg, expectedValue, actualValue);
            } else {
                String fullMsg = msg + String.format("abs(Expected Value - Actual Value) = %e >= %e", Math.abs(expectedValue - actualValue), epsilon);
                assertTrue(fullMsg, Math.abs(expectedValue - actualValue) < epsilon);
            }
        } else if (expectedObj instanceof BigDecimal) {
            BigDecimal exp = (BigDecimal) expectedObj;
            BigDecimal got = actualRow.getDecimalAsBigDecimal(i);
            // Either both are null or neither are null.
            assertEquals(exp == null, got == null);
            assertEquals(msg, exp.doubleValue(), got.doubleValue(), epsilon);
        } else if (expectedObj instanceof String) {
            String val = (String) expectedObj;
            assertEquals(msg, val, actualRow.getString(i));
        } else if (expectedObj instanceof TimestampType) {
            TimestampType val = (TimestampType) expectedObj;
            assertEquals(msg, val, actualRow.getTimestampAsTimestamp(i));
        } else {
            fail("Unexpected type in expected row: " + expectedObj.getClass().getSimpleName());
        }
    }
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Example 22 with VoltType

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

the class TestHashinatorLite method tandemTestNulls.

private void tandemTestNulls(HashinatorLite h1, TheHashinator h2, int partitionCount) throws Exception {
    VoltType[] hashableTypes = new VoltType[] { VoltType.TINYINT, VoltType.SMALLINT, VoltType.INTEGER, VoltType.BIGINT, VoltType.STRING, VoltType.VARBINARY };
    for (VoltType type : hashableTypes) {
        Object nullToHash = type.getNullValue();
        int hash1 = h1.getHashedPartitionForParameter(type.getValue(), nullToHash);
        int hash2 = h2.getHashedPartitionForParameter(type.getValue(), nullToHash);
        if (hash1 != 0 || hash2 != 0) {
            System.out.println("Lite " + hash1 + " Std " + hash2);
        }
        assertEquals(0, hash1);
        assertEquals(0, hash2);
    }
}
Also used : VoltType(org.voltdb.VoltType)

Example 23 with VoltType

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

the class EchoServer method echo.

static void echo(byte[] t, byte[] buffer, int length) {
    VoltType type = null;
    ByteBuffer buf;
    if (t[0] == ARRAY_BEGIN) {
        isArray = true;
        return;
    } else if (t[0] == ARRAY_END) {
        isArray = false;
        return;
    } else {
        type = VoltType.get(t[0]);
    }
    FastDeserializer fds = new FastDeserializer(buffer);
    int count = 1;
    try {
        fs.writeInt(length);
        fs.writeByte(type.getValue());
        if (isArray) {
            if (type == VoltType.TINYINT) {
                count = fds.readInt();
                fs.writeInt(count);
            } else {
                count = fds.readShort();
                fs.writeShort(count);
            }
        }
        for (int ii = 0; ii < count; ii++) {
            switch(type) {
                case TINYINT:
                    byte b = fds.readByte();
                    fs.write(b);
                    break;
                case SMALLINT:
                    short s = fds.readShort();
                    fs.writeShort(s);
                    break;
                case INTEGER:
                    int i = fds.readInt();
                    fs.writeInt(i);
                    break;
                case BIGINT:
                    long l = fds.readLong();
                    fs.writeLong(l);
                    break;
                case FLOAT:
                    double f = fds.readDouble();
                    fs.writeDouble(f);
                    break;
                case STRING:
                    String str = fds.readString();
                    fs.writeString(str);
                    break;
                case VARBINARY:
                    byte[] bin = fds.readVarbinary();
                    fs.writeVarbinary(bin);
                    break;
                case TIMESTAMP:
                    long micros = fds.readLong();
                    fs.writeLong(micros);
                    break;
                case DECIMAL:
                    BigDecimal bd = VoltDecimalHelper.deserializeBigDecimal(fds.buffer());
                    buf = ByteBuffer.allocate(16);
                    VoltDecimalHelper.serializeBigDecimal(bd, buf);
                    buf.flip();
                    fs.write(buf);
                    break;
                case VOLTTABLE:
                    VoltTable table = PrivateVoltTableFactory.createVoltTableFromSharedBuffer(fds.buffer());
                    buf = ByteBuffer.allocate(table.getSerializedSize());
                    table.flattenToBuffer(buf);
                    buf.flip();
                    fs.write(buf);
                    break;
                default:
                    throw new RuntimeException("FIXME: Unsupported type " + type);
            }
        }
        client.getOutputStream().write(fs.getBytes());
        fs.clear();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) VoltTable(org.voltdb.VoltTable) BigDecimal(java.math.BigDecimal) VoltType(org.voltdb.VoltType)

Example 24 with VoltType

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

the class DDLCompiler method addIndexToCatalog.

private static void addIndexToCatalog(Database db, Table table, VoltXMLElement node, Map<String, String> indexReplacementMap, HashMap<String, Index> indexMap, HashMap<String, Column> columnMap, VoltCompiler compiler) throws VoltCompilerException {
    assert node.name.equals("index");
    String name = node.attributes.get("name");
    boolean unique = Boolean.parseBoolean(node.attributes.get("unique"));
    boolean assumeUnique = Boolean.parseBoolean(node.attributes.get("assumeunique"));
    AbstractParsedStmt dummy = new ParsedSelectStmt(null, db);
    dummy.setDDLIndexedTable(table);
    StringBuffer msg = new StringBuffer(String.format("Index \"%s\" ", name));
    // "parse" the expression trees for an expression-based index (vs. a simple column value index)
    List<AbstractExpression> exprs = null;
    // "parse" the WHERE expression for partial index if any
    AbstractExpression predicate = null;
    // Some expressions have special validation in indices.  Not all the expression
    // can be indexed. We scan for result type at first here and block those which
    // can't be indexed like boolean, geo ... We gather rest of expression into
    // checkExpressions list.  We will check on them all at once.
    List<AbstractExpression> checkExpressions = new ArrayList<>();
    for (VoltXMLElement subNode : node.children) {
        if (subNode.name.equals("exprs")) {
            exprs = new ArrayList<>();
            for (VoltXMLElement exprNode : subNode.children) {
                AbstractExpression expr = dummy.parseExpressionTree(exprNode);
                expr.resolveForTable(table);
                expr.finalizeValueTypes();
                // string will be populated with an expression's details when
                // its value type is not indexable
                StringBuffer exprMsg = new StringBuffer();
                if (!expr.isValueTypeIndexable(exprMsg)) {
                    // indexing on expression with boolean result is not supported.
                    throw compiler.new VoltCompilerException("Cannot create index \"" + name + "\" because it contains " + exprMsg + ", which is not supported.");
                }
                if ((unique || assumeUnique) && !expr.isValueTypeUniqueIndexable(exprMsg)) {
                    // indexing on expression with boolean result is not supported.
                    throw compiler.new VoltCompilerException("Cannot create unique index \"" + name + "\" because it contains " + exprMsg + ", which is not supported.");
                }
                // rest of the validity guards will be evaluated after collecting all the expressions.
                checkExpressions.add(expr);
                exprs.add(expr);
            }
        } else if (subNode.name.equals("predicate")) {
            assert (subNode.children.size() == 1);
            VoltXMLElement predicateXML = subNode.children.get(0);
            assert (predicateXML != null);
            predicate = buildPartialIndexPredicate(dummy, name, predicateXML, table, compiler);
        }
    }
    // Check all the subexpressions we gathered up.
    if (!AbstractExpression.validateExprsForIndexesAndMVs(checkExpressions, msg)) {
        // The error message will be in the StringBuffer msg.
        throw compiler.new VoltCompilerException(msg.toString());
    }
    String colList = node.attributes.get("columns");
    String[] colNames = colList.split(",");
    Column[] columns = new Column[colNames.length];
    boolean has_nonint_col = false;
    boolean has_geo_col = false;
    String nonint_col_name = null;
    for (int i = 0; i < colNames.length; i++) {
        columns[i] = columnMap.get(colNames[i]);
        if (columns[i] == null) {
            return;
        }
    }
    UnsafeOperatorsForDDL unsafeOps = new UnsafeOperatorsForDDL();
    if (exprs == null) {
        for (int i = 0; i < colNames.length; i++) {
            VoltType colType = VoltType.get((byte) columns[i].getType());
            if (!colType.isIndexable()) {
                String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " values are not currently supported as index keys: \"" + colNames[i] + "\"";
                throw compiler.new VoltCompilerException(emsg);
            }
            if ((unique || assumeUnique) && !colType.isUniqueIndexable()) {
                String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " values are not currently supported as unique index keys: \"" + colNames[i] + "\"";
                throw compiler.new VoltCompilerException(emsg);
            }
            if (!colType.isBackendIntegerType()) {
                has_nonint_col = true;
                nonint_col_name = colNames[i];
                has_geo_col = colType.equals(VoltType.GEOGRAPHY);
                if (has_geo_col && colNames.length > 1) {
                    String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " values must be the only component of an index key: \"" + nonint_col_name + "\"";
                    throw compiler.new VoltCompilerException(emsg);
                }
            }
        }
    } else {
        for (AbstractExpression expression : exprs) {
            VoltType colType = expression.getValueType();
            if (!colType.isIndexable()) {
                String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " valued expressions are not currently supported as index keys.";
                throw compiler.new VoltCompilerException(emsg);
            }
            if ((unique || assumeUnique) && !colType.isUniqueIndexable()) {
                String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " valued expressions are not currently supported as unique index keys.";
                throw compiler.new VoltCompilerException(emsg);
            }
            if (!colType.isBackendIntegerType()) {
                has_nonint_col = true;
                nonint_col_name = "<expression>";
                has_geo_col = colType.equals(VoltType.GEOGRAPHY);
                if (has_geo_col) {
                    if (exprs.size() > 1) {
                        String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " values must be the only component of an index key.";
                        throw compiler.new VoltCompilerException(emsg);
                    } else if (!(expression instanceof TupleValueExpression)) {
                        String emsg = "Cannot create index \"" + name + "\" because " + colType.getName() + " expressions must be simple column expressions.";
                        throw compiler.new VoltCompilerException(emsg);
                    }
                }
            }
            expression.findUnsafeOperatorsForDDL(unsafeOps);
        }
    }
    Index index = table.getIndexes().add(name);
    index.setCountable(false);
    index.setIssafewithnonemptysources(!unsafeOps.isUnsafe());
    // Set the index type.  It will be one of:
    // - Covering cell index (geo index for CONTAINS predicates)
    // - HASH index (set in HSQL because "hash" is in the name of the
    //   constraint or the index
    // - TREE index, which is the default
    boolean isHashIndex = node.attributes.get("ishashindex").equals("true");
    if (has_geo_col) {
        index.setType(IndexType.COVERING_CELL_INDEX.getValue());
    } else if (isHashIndex) {
        // warn user that hash index will be deprecated
        compiler.addWarn("Hash indexes are deprecated. In a future release, VoltDB will only support tree indexes, even if the index name contains the string \"hash\"");
        // make the index a hash.
        if (has_nonint_col) {
            String emsg = "Index " + name + " in table " + table.getTypeName() + " uses a non-hashable column " + nonint_col_name;
            throw compiler.new VoltCompilerException(emsg);
        }
        index.setType(IndexType.HASH_TABLE.getValue());
    } else {
        index.setType(IndexType.BALANCED_TREE.getValue());
        index.setCountable(true);
    }
    // but they still represent the columns that will trigger an index update when their values change.
    for (int i = 0; i < columns.length; i++) {
        ColumnRef cref = index.getColumns().add(columns[i].getTypeName());
        cref.setColumn(columns[i]);
        cref.setIndex(i);
    }
    if (exprs != null) {
        try {
            index.setExpressionsjson(convertToJSONArray(exprs));
        } catch (JSONException e) {
            throw compiler.new VoltCompilerException("Unexpected error serializing non-column expressions for index '" + name + "' on type '" + table.getTypeName() + "': " + e.toString());
        }
    }
    index.setUnique(unique);
    if (assumeUnique) {
        index.setUnique(true);
    }
    index.setAssumeunique(assumeUnique);
    if (predicate != null) {
        try {
            index.setPredicatejson(convertToJSONObject(predicate));
        } catch (JSONException e) {
            throw compiler.new VoltCompilerException("Unexpected error serializing predicate for partial index '" + name + "' on type '" + table.getTypeName() + "': " + e.toString());
        }
    }
    // will make two indexes different
    for (Index existingIndex : table.getIndexes()) {
        // skip thineself
        if (existingIndex == index) {
            continue;
        }
        if (indexesAreDups(existingIndex, index)) {
            // replace any constraints using one index with the other
            //for () TODO
            // get ready for replacements from constraints created later
            indexReplacementMap.put(index.getTypeName(), existingIndex.getTypeName());
            // if the index is a user-named index...
            if (index.getTypeName().startsWith(HSQLInterface.AUTO_GEN_PREFIX) == false) {
                // on dup-detection, add a warning but don't fail
                String emsg = String.format("Dropping index %s on table %s because it duplicates index %s.", index.getTypeName(), table.getTypeName(), existingIndex.getTypeName());
                compiler.addWarn(emsg);
            }
            // drop the index and GTFO
            table.getIndexes().delete(index.getTypeName());
            return;
        }
    }
    String smsg = "Created index: " + name + " on table: " + table.getTypeName() + " of type: " + IndexType.get(index.getType()).name();
    compiler.addInfo(smsg);
    indexMap.put(name, index);
}
Also used : TupleValueExpression(org.voltdb.expressions.TupleValueExpression) ArrayList(java.util.ArrayList) JSONException(org.json_voltpatches.JSONException) Index(org.voltdb.catalog.Index) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement) AbstractParsedStmt(org.voltdb.planner.AbstractParsedStmt) Constraint(org.voltdb.catalog.Constraint) UnsafeOperatorsForDDL(org.voltdb.expressions.AbstractExpression.UnsafeOperatorsForDDL) AbstractExpression(org.voltdb.expressions.AbstractExpression) Column(org.voltdb.catalog.Column) VoltType(org.voltdb.VoltType) ParsedSelectStmt(org.voltdb.planner.ParsedSelectStmt) ColumnRef(org.voltdb.catalog.ColumnRef) VoltCompilerException(org.voltdb.compiler.VoltCompiler.VoltCompilerException)

Example 25 with VoltType

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

the class PerPartitionTable method reinsertFailed.

private void reinsertFailed(List<VoltBulkLoaderRow> rows) throws Exception {
    VoltTable tmpTable = new VoltTable(m_columnInfo);
    for (final VoltBulkLoaderRow row : rows) {
        // previously successful.
        try {
            Object[] row_args = new Object[row.m_rowData.length];
            for (int i = 0; i < row_args.length; i++) {
                final VoltType type = m_columnTypes[i];
                row_args[i] = ParameterConverter.tryToMakeCompatible(type.classFromType(), row.m_rowData[i]);
            }
            tmpTable.addRow(row_args);
        } catch (VoltTypeException ex) {
            // should have caught this
            continue;
        }
        ProcedureCallback callback = new ProcedureCallback() {

            @Override
            public void clientCallback(ClientResponse response) throws Exception {
                row.m_loader.m_loaderCompletedCnt.incrementAndGet();
                row.m_loader.m_outstandingRowCount.decrementAndGet();
                //one insert at a time callback
                if (response.getStatus() != ClientResponse.SUCCESS) {
                    row.m_loader.m_notificationCallBack.failureCallback(row.m_rowHandle, row.m_rowData, response);
                }
            }
        };
        loadTable(callback, tmpTable);
    }
}
Also used : ProcedureCallback(org.voltdb.client.ProcedureCallback) ClientResponse(org.voltdb.client.ClientResponse) VoltType(org.voltdb.VoltType) VoltTypeException(org.voltdb.VoltTypeException) VoltTable(org.voltdb.VoltTable)

Aggregations

VoltType (org.voltdb.VoltType)54 AbstractExpression (org.voltdb.expressions.AbstractExpression)10 Constraint (org.voltdb.catalog.Constraint)9 BigDecimal (java.math.BigDecimal)8 ArrayList (java.util.ArrayList)8 Column (org.voltdb.catalog.Column)8 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)7 VoltTypeException (org.voltdb.VoltTypeException)7 VoltCompilerException (org.voltdb.compiler.VoltCompiler.VoltCompilerException)6 JSONException (org.json_voltpatches.JSONException)5 ExpressionType (org.voltdb.types.ExpressionType)5 VoltTable (org.voltdb.VoltTable)4 Index (org.voltdb.catalog.Index)4 SQLException (java.sql.SQLException)3 ColumnRef (org.voltdb.catalog.ColumnRef)3 Table (org.voltdb.catalog.Table)3 ConstantValueExpression (org.voltdb.expressions.ConstantValueExpression)3 ParameterValueExpression (org.voltdb.expressions.ParameterValueExpression)3 TupleValueExpression (org.voltdb.expressions.TupleValueExpression)3 GeographyPointValue (org.voltdb.types.GeographyPointValue)3