Search in sources :

Example 36 with VoltType

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

the class SQLCommandOutputFormatterCSV method printTable.

@Override
public void printTable(PrintStream stream, VoltTable t, boolean includeColumnNames) throws IOException {
    final int columnCount = t.getColumnCount();
    List<VoltType> columnTypes = new ArrayList<VoltType>(columnCount);
    for (int i = 0; i < columnCount; i++) {
        columnTypes.add(t.getColumnType(i));
    }
    CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(stream));
    if (includeColumnNames) {
        String[] columnNames = new String[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnNames[i] = t.getColumnName(i);
        }
        csvWriter.writeNext(columnNames);
    }
    VoltTableUtil.toCSVWriter(csvWriter, t, columnTypes);
}
Also used : VoltType(org.voltdb.VoltType) ArrayList(java.util.ArrayList) CSVWriter(au.com.bytecode.opencsv_voltpatches.CSVWriter)

Example 37 with VoltType

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

the class ParameterizationInfo method parameterizeRecursively.

static void parameterizeRecursively(VoltXMLElement node, VoltXMLElement paramsNode, Map<String, Integer> idToParamIndexMap, List<String> paramValues) {
    if (node.name.equals("value")) {
        String idStr = node.attributes.get("id");
        assert (idStr != null);
        // A value id is currently a "string-formatted long", but there's no need to commit
        // to that format in this early processing -- here, the id just needs to be a unique
        // string for each parsed value. It allows hsql to replicate a parameter reference
        // within its parse trees without causing code like this to lose track of its identity.
        Integer paramIndex = idToParamIndexMap.get(idStr);
        if (paramIndex == null) {
            // Use the next param index for each new value with an unfamiliar id,
            // starting at 0.
            paramIndex = paramValues.size();
            // Later references to this value's id will re-use this same param index.
            idToParamIndexMap.put(idStr, paramIndex);
            VoltXMLElement paramIndexNode = new VoltXMLElement("parameter");
            paramIndexNode.attributes.put("index", String.valueOf(paramIndex));
            paramIndexNode.attributes.put("id", idStr);
            paramsNode.children.add(paramIndexNode);
            // handle parameter value type
            String typeStr = node.attributes.get("valuetype");
            VoltType vt = VoltType.typeFromString(typeStr);
            String value = null;
            if (vt != VoltType.NULL) {
                value = node.attributes.get("value");
            }
            paramValues.add(value);
            // Thanks for Hsqldb 1.9, FLOAT literal values have been handled well with E sign.
            if (vt == VoltType.NUMERIC) {
                vt = VoltTypeUtil.getNumericLiteralType(VoltType.BIGINT, value);
            }
            node.attributes.put("valuetype", vt.getName());
            paramIndexNode.attributes.put("valuetype", vt.getName());
        }
        // Assume that all values, whether or not their ids have been seen before, can
        // be considered planner-generated parameters (proxies for user-provided constants).
        // This is one simplification that leverages the fact that statements that came with
        // user-provided parameters were barred from being (further) parameterized.
        node.attributes.put("isparam", "true");
        node.attributes.put("isplannergenerated", "true");
        // Remove the "value" attribute -- this is the critical step to folding
        // different raw VoltXML trees into the same parameterized VoltXML tree.
        // The value differences are extracted into paramValues for future reference by:
        //     the execution engine which needs to substitute actual values for all parameters
        //         to run the query
        //     the index scan planner which may need to "bind" the parameters to their original
        //     values to apply indexes on expressions like "(colA + 2 * colB)" when used in a query
        //     like "... WHERE t2.colA + 2 * t2.colB > 3*t1.colC".
        node.attributes.remove("value");
    }
    for (VoltXMLElement child : node.children) {
        parameterizeRecursively(child, paramsNode, idToParamIndexMap, paramValues);
    }
}
Also used : VoltType(org.voltdb.VoltType) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement)

Example 38 with VoltType

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

the class TestTypeConversionSuite method testTypeConversion.

public void testTypeConversion() throws IOException, ProcCallException {
    Client client = getClient();
    client.callProcedure("T.Insert", 1, 1, 1, 1, 1, new BigDecimal(1), "2012-12-01", "hi", "10", null, null);
    // test different cases of type conversion that are allowed
    client.callProcedure("ProcToTestTypeConversion", ProcToTestTypeConversion.TestAllAllowedTypeConv, 0, 0);
    // Use the conversion matrix to test type conversion cases that are allowed and blocked.
    // This uses non-array argument list as supplied arguments for params (except varbinary
    // that is byte[])
    VoltType typeToTest = VoltType.INVALID;
    String errorMsg = null;
    for (int fromType = 0; fromType < m_tableColTypeVal.length; ++fromType) {
        boolean[] supportedFromType = m_typeConversionMatrix[fromType];
        typeToTest = m_tableColTypeVal[fromType];
        for (int toType = 0; toType < m_tableColTypeVal.length; ++toType) {
            if (supportedFromType[toType]) {
                // type conversion feasible
                client.callProcedure("ProcToTestTypeConversion", ProcToTestTypeConversion.TestTypeConvWithInsertProc, toType, typeToTest.getValue());
            } else {
                String typeTriedByInsert = m_javaTypeNamePatternForInsertTest[fromType];
                // type conversion not allowed
                if (typeToTest == VoltType.TIMESTAMP && m_tableColTypeVal[toType] == VoltType.DECIMAL) {
                    // Conversion from Timestamp -> decimal is allowed in voltqueue
                    // sql as the Timestamp -> decimal is supported in comparison
                    // expression for select and insert statement with select statement with
                    // where predicate. So for update queries this works and hence is allowed
                    // in voltqueue sql. But in case of insert this conversion is not allowed
                    // as the conversion is flagged in EE with different error message So
                    // test for that
                    errorMsg = "Type " + typeToTest.getName() + " can't be cast as " + m_tableColTypeVal[toType].getName();
                } else {
                    errorMsg = "Incompatible parameter type: can not convert type '" + typeTriedByInsert + "' to '" + m_tableColTypeVal[toType].getName() + "' for arg " + toType + " for SQL stmt";
                }
                verifyProcFails(client, errorMsg, "ProcToTestTypeConversion", ProcToTestTypeConversion.TestTypeConvWithInsertProc, toType, typeToTest.getValue());
            }
            if (typeToTest == VoltType.TINYINT && m_tableColTypeVal[fromType] == VoltType.TINYINT) {
                continue;
            }
            // Test that array arguments are not typically compatible
            // with parameters passed into column comparisons.
            String typeTriedByCompare = m_javaTypeNamePatternForInListTest[fromType];
            errorMsg = "Incompatible parameter type: can not convert type '" + typeTriedByCompare + "' to '" + m_tableColTypeVal[toType].getName() + "' for arg 0 for SQL stmt";
            verifyProcFails(client, errorMsg, "ProcToTestTypeConversion", ProcToTestTypeConversion.TestFailingArrayTypeCompare, toType, typeToTest.getValue());
        }
    }
    // unless/until we find a way around that interpretation.
    for (int fromType = 1; fromType < m_tableColTypeVal.length; ++fromType) {
        boolean[] supportedForFromType = m_typeConversionMatrixInList[fromType];
        String typeTriedByInListQuery = m_javaTypeNamePatternForInListTest[fromType];
        typeToTest = m_tableColTypeVal[fromType];
        for (int toType = 0; toType < m_tableColTypeVal.length; ++toType) {
            VoltType inListType = m_tableColInListTypeVal[toType];
            if (supportedForFromType[toType]) {
                // type conversion feasible
                client.callProcedure("ProcToTestTypeConversion", ProcToTestTypeConversion.TestTypesInList, toType, typeToTest.getValue());
                if (inListType == null) {
                    // compiler as tested in a statement compiler test, not here.
                    continue;
                }
            } else {
                if (inListType == null) {
                    // compiler as tested in a statement compiler test, not here.
                    continue;
                }
                errorMsg = "Incompatible parameter type: can not convert type '" + typeTriedByInListQuery + "' to '" + inListType.getName() + "' for arg 0 for SQL stmt";
                verifyProcFails(client, errorMsg, "ProcToTestTypeConversion", ProcToTestTypeConversion.TestTypesInList, toType, typeToTest.getValue());
            }
            // Test that non-array arguments are not allowed for IN LIST params.
            String typeExpectedToFailInListQuery = m_javaTypeNamePatternForInListFailureTest[fromType];
            if (typeExpectedToFailInListQuery.endsWith("]") && inListType == VoltType.INLIST_OF_BIGINT) {
                errorMsg = "rhs of IN expression is of a non-list type varbinary";
            } else {
                errorMsg = "Incompatible parameter type: can not convert type '" + typeExpectedToFailInListQuery + "' to '" + inListType.getName() + "' for arg 0 for SQL stmt";
            }
            verifyProcFails(client, errorMsg, "ProcToTestTypeConversion", ProcToTestTypeConversion.TestFailingTypesInList, toType, typeToTest.getValue());
        }
    }
}
Also used : VoltType(org.voltdb.VoltType) Client(org.voltdb.client.Client) BigDecimal(java.math.BigDecimal)

Example 39 with VoltType

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

the class AbstractParsedStmt method parseOperationExpression.

/**
     *
     * @param paramsById
     * @param exprNode
     * @return
     */
private AbstractExpression parseOperationExpression(VoltXMLElement exprNode) {
    String optype = exprNode.attributes.get("optype");
    ExpressionType exprType = ExpressionType.get(optype);
    AbstractExpression expr = null;
    if (exprType == ExpressionType.INVALID) {
        throw new PlanningErrorException("Unsupported operation type '" + optype + "'");
    }
    try {
        expr = exprType.getExpressionClass().newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
    expr.setExpressionType(exprType);
    if (exprType == ExpressionType.OPERATOR_CASE_WHEN || exprType == ExpressionType.OPERATOR_ALTERNATIVE) {
        String valueType = exprNode.attributes.get("valuetype");
        expr.setValueType(VoltType.typeFromString(valueType));
    }
    if (expr instanceof ComparisonExpression) {
        String opsubtype = exprNode.attributes.get("opsubtype");
        if (opsubtype != null) {
            QuantifierType quantifier = QuantifierType.get(opsubtype);
            if (quantifier != QuantifierType.NONE) {
                ((ComparisonExpression) expr).setQuantifier(quantifier);
            }
        }
    }
    // get the first (left) node that is an element
    VoltXMLElement leftExprNode = exprNode.children.get(0);
    assert (leftExprNode != null);
    // recursively parse the left subtree (could be another operator or
    // a constant/tuple/param value operand).
    AbstractExpression leftExpr = parseExpressionNode(leftExprNode);
    assert ((leftExpr != null) || (exprType == ExpressionType.AGGREGATE_COUNT));
    expr.setLeft(leftExpr);
    // get the second (right) node that is an element (might be null)
    VoltXMLElement rightExprNode = null;
    if (exprNode.children.size() > 1) {
        rightExprNode = exprNode.children.get(1);
    }
    if (expr.needsRightExpression()) {
        assert (rightExprNode != null);
        // recursively parse the right subtree
        AbstractExpression rightExpr = parseExpressionNode(rightExprNode);
        assert (rightExpr != null);
        expr.setRight(rightExpr);
    } else {
        assert (rightExprNode == null);
        if (exprType == ExpressionType.OPERATOR_CAST) {
            String valuetype = exprNode.attributes.get("valuetype");
            assert (valuetype != null);
            VoltType voltType = VoltType.typeFromString(valuetype);
            expr.setValueType(voltType);
            // We don't support parameterized casting, such as specifically to "VARCHAR(3)" vs. VARCHAR,
            // so assume max length for variable-length types (VARCHAR and VARBINARY).
            int size = voltType.getMaxLengthInBytes();
            expr.setValueSize(size);
        }
    }
    if (exprType == ExpressionType.COMPARE_EQUAL && QuantifierType.ANY == ((ComparisonExpression) expr).getQuantifier()) {
        // Break up UNION/INTERSECT (ALL) set ops into individual selects connected by
        // AND/OR operator
        // col IN ( queryA UNION queryB ) - > col IN (queryA) OR col IN (queryB)
        // col IN ( queryA INTERSECTS queryB ) - > col IN (queryA) AND col IN (queryB)
        expr = ParsedUnionStmt.breakUpSetOpSubquery(expr);
    } else if (exprType == ExpressionType.OPERATOR_EXISTS) {
        expr = optimizeExistsExpression(expr);
    }
    return expr;
}
Also used : ComparisonExpression(org.voltdb.expressions.ComparisonExpression) AbstractExpression(org.voltdb.expressions.AbstractExpression) VoltType(org.voltdb.VoltType) QuantifierType(org.voltdb.types.QuantifierType) VoltXMLElement(org.hsqldb_voltpatches.VoltXMLElement) ExpressionType(org.voltdb.types.ExpressionType) JSONException(org.json_voltpatches.JSONException) Constraint(org.voltdb.catalog.Constraint)

Example 40 with VoltType

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

the class VoltTableUtil method toCSVWriter.

public static void toCSVWriter(CSVWriter csv, VoltTable vt, List<VoltType> columnTypes) throws IOException {
    final SimpleDateFormat sdf = m_sdf.get();
    String[] fields = new String[vt.getColumnCount()];
    while (vt.advanceRow()) {
        for (int ii = 0; ii < vt.getColumnCount(); ii++) {
            final VoltType type = columnTypes.get(ii);
            if (type == VoltType.BIGINT || type == VoltType.INTEGER || type == VoltType.SMALLINT || type == VoltType.TINYINT) {
                final long value = vt.getLong(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Long.toString(value);
                }
            } else if (type == VoltType.FLOAT) {
                final double value = vt.getDouble(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Double.toString(value);
                }
            } else if (type == VoltType.DECIMAL) {
                final BigDecimal bd = vt.getDecimalAsBigDecimal(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = bd.toString();
                }
            } else if (type == VoltType.STRING) {
                final String str = vt.getString(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = str;
                }
            } else if (type == VoltType.TIMESTAMP) {
                final TimestampType timestamp = vt.getTimestampAsTimestamp(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = sdf.format(timestamp.asApproximateJavaDate());
                    fields[ii] += String.format("%03d", timestamp.getUSec());
                }
            } else if (type == VoltType.VARBINARY) {
                byte[] bytes = vt.getVarbinary(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Encoder.hexEncode(bytes);
                }
            } else if (type == VoltType.GEOGRAPHY_POINT) {
                final GeographyPointValue pt = vt.getGeographyPointValue(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = pt.toString();
                }
            } else if (type == VoltType.GEOGRAPHY) {
                final GeographyValue gv = vt.getGeographyValue(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = gv.toString();
                }
            }
        }
        csv.writeNext(fields);
    }
    csv.flush();
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) SimpleDateFormat(java.text.SimpleDateFormat) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

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