Search in sources :

Example 11 with VoltType

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

the class CatalogSchemaTools method toSchema.

/**
     * Convert a Table catalog object into the proper SQL DDL, including all indexes,
     * constraints, and foreign key references.
     * Also returns just the CREATE TABLE statement, since, like all good methods,
     * it should have two purposes....
     * It would be nice to have a separate method to just generate the CREATE TABLE,
     * but we use that pass to also figure out what separate constraint and index
     * SQL DDL needs to be generated, so instead, we opt to build the CREATE TABLE DDL
     * separately as we go here, and then fill it in to the StringBuilder being used
     * to construct the full canonical DDL at the appropriate time.
     * @param sb - the schema being built
     * @param catalog_tbl - object to be analyzed
     * @param viewQuery - the Query if this Table is a View
     * @param isExportOnly Is this a export table.
     * @param streamPartitionColumn stream partition column
     * @param streamTarget - true if this Table is an Export Table
     * @return SQL Schema text representing the CREATE TABLE statement to generate the table
     */
public static String toSchema(StringBuilder sb, Table catalog_tbl, String viewQuery, boolean isExportOnly, String streamPartitionColumn, String streamTarget) {
    assert (!catalog_tbl.getColumns().isEmpty());
    boolean tableIsView = (viewQuery != null);
    // We need the intermediate results of building the table schema string so that
    // we can return the full CREATE TABLE statement, so accumulate it separately
    final StringBuilder table_sb = new StringBuilder();
    final Set<Index> skip_indexes = new HashSet<>();
    final Set<Constraint> skip_constraints = new HashSet<>();
    if (tableIsView) {
        table_sb.append("CREATE VIEW ").append(catalog_tbl.getTypeName()).append(" (");
    } else {
        if (isExportOnly) {
            table_sb.append("CREATE STREAM ").append(catalog_tbl.getTypeName());
            if (streamPartitionColumn != null && viewQuery == null) {
                table_sb.append(" PARTITION ON COLUMN ").append(streamPartitionColumn);
            }
            //Default target means no target.
            if (streamTarget != null && !streamTarget.equalsIgnoreCase(Constants.DEFAULT_EXPORT_CONNECTOR_NAME)) {
                table_sb.append(" EXPORT TO TARGET ").append(streamTarget);
            }
        } else {
            table_sb.append("CREATE TABLE ").append(catalog_tbl.getTypeName());
        }
        table_sb.append(" (");
    }
    // Columns
    String add = "\n";
    for (Column catalog_col : CatalogUtil.getSortedCatalogItems(catalog_tbl.getColumns(), "index")) {
        VoltType col_type = VoltType.get((byte) catalog_col.getType());
        if (tableIsView) {
            table_sb.append(add).append(spacer).append(catalog_col.getTypeName());
            add = ",\n";
            continue;
        }
        table_sb.append(add).append(spacer).append(catalog_col.getTypeName()).append(" ").append(col_type.toSQLString()).append(col_type.isVariableLength() && catalog_col.getSize() > 0 ? "(" + catalog_col.getSize() + (catalog_col.getInbytes() ? " BYTES" : "") + ")" : "");
        // Default value
        String defaultvalue = catalog_col.getDefaultvalue();
        //VoltType defaulttype = VoltType.get((byte)catalog_col.getDefaulttype());
        boolean nullable = catalog_col.getNullable();
        // TODO: Shouldn't have to check whether the string contains "null"
        if (defaultvalue == null) {
        } else if (defaultvalue.toLowerCase().equals("null") && nullable) {
            defaultvalue = null;
        } else {
            if (col_type == VoltType.TIMESTAMP) {
                if (defaultvalue.startsWith("CURRENT_TIMESTAMP")) {
                    defaultvalue = "CURRENT_TIMESTAMP";
                } else {
                    assert (defaultvalue.matches("[0-9]+"));
                    long epoch = Long.parseLong(defaultvalue);
                    Date d = new Date(epoch / 1000);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    defaultvalue = "\'" + sdf.format(d) + "." + StringUtils.leftPad(String.valueOf(epoch % 1000000), 6, "0") + "\'";
                }
            } else {
                // XXX: if (defaulttype != VoltType.VOLTFUNCTION) {
                // TODO: Escape strings properly
                defaultvalue = defaultvalue.replace("\'", "\'\'");
                defaultvalue = "'" + defaultvalue + "'";
            }
        }
        if (defaultvalue == null) {
            table_sb.append((!nullable ? " NOT NULL" : ""));
        } else {
            table_sb.append(" DEFAULT ").append(defaultvalue != null ? defaultvalue : "NULL").append(!nullable ? " NOT NULL" : "");
        }
        // Single-column constraints
        for (ConstraintRef catalog_const_ref : catalog_col.getConstraints()) {
            Constraint catalog_const = catalog_const_ref.getConstraint();
            ConstraintType const_type = ConstraintType.get(catalog_const.getType());
            // Check if there is another column in our table with the same constraint
            // If there is, then we need to add it to the end of the table definition
            boolean found = false;
            for (Column catalog_other_col : catalog_tbl.getColumns()) {
                if (catalog_other_col.equals(catalog_col))
                    continue;
                if (catalog_other_col.getConstraints().getIgnoreCase(catalog_const.getTypeName()) != null) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                switch(const_type) {
                    case FOREIGN_KEY:
                        {
                            Table catalog_fkey_tbl = catalog_const.getForeignkeytable();
                            Column catalog_fkey_col = null;
                            for (ColumnRef ref : catalog_const.getForeignkeycols()) {
                                catalog_fkey_col = ref.getColumn();
                                // Nasty hack to get first item
                                break;
                            }
                            assert (catalog_fkey_col != null);
                            table_sb.append(" REFERENCES ").append(catalog_fkey_tbl.getTypeName()).append(" (").append(catalog_fkey_col.getTypeName()).append(")");
                            skip_constraints.add(catalog_const);
                            break;
                        }
                    default:
                }
            }
        }
        add = ",\n";
    }
    // Constraints
    for (Constraint catalog_const : catalog_tbl.getConstraints()) {
        if (skip_constraints.contains(catalog_const))
            continue;
        ConstraintType const_type = ConstraintType.get(catalog_const.getType());
        // Primary Keys / Unique Constraints
        if (const_type == ConstraintType.PRIMARY_KEY || const_type == ConstraintType.UNIQUE) {
            Index catalog_idx = catalog_const.getIndex();
            if (!tableIsView) {
                // Get the ConstraintType.
                table_sb.append(add).append(spacer);
                if (!catalog_const.getTypeName().startsWith(HSQLInterface.AUTO_GEN_PREFIX)) {
                    table_sb.append("CONSTRAINT ").append(catalog_const.getTypeName()).append(" ");
                }
                if (const_type == ConstraintType.PRIMARY_KEY || const_type == ConstraintType.UNIQUE) {
                    if (const_type == ConstraintType.PRIMARY_KEY) {
                        table_sb.append("PRIMARY KEY (");
                    } else {
                        if (catalog_idx.getAssumeunique()) {
                            table_sb.append("ASSUMEUNIQUE (");
                        } else {
                            table_sb.append("UNIQUE (");
                        }
                    }
                    String col_add = "";
                    if (catalog_idx.getExpressionsjson() != null && !catalog_idx.getExpressionsjson().equals("")) {
                        String exprStrings = new String();
                        StmtTargetTableScan tableScan = new StmtTargetTableScan(catalog_tbl);
                        try {
                            List<AbstractExpression> expressions = AbstractExpression.fromJSONArrayString(catalog_idx.getExpressionsjson(), tableScan);
                            String sep = "";
                            for (AbstractExpression expr : expressions) {
                                exprStrings += sep + expr.explain(catalog_tbl.getTypeName());
                                sep = ",";
                            }
                        } catch (JSONException e) {
                        }
                        table_sb.append(col_add).append(exprStrings);
                    } else {
                        for (ColumnRef catalog_colref : CatalogUtil.getSortedCatalogItems(catalog_idx.getColumns(), "index")) {
                            table_sb.append(col_add).append(catalog_colref.getColumn().getTypeName());
                            col_add = ", ";
                        }
                    // FOR
                    }
                    table_sb.append(")");
                }
            }
            if (catalog_idx.getTypeName().startsWith(HSQLInterface.AUTO_GEN_PREFIX) || catalog_idx.getTypeName().startsWith(HSQLInterface.AUTO_GEN_MATVIEW)) {
                skip_indexes.add(catalog_idx);
            }
        // Foreign Key
        } else if (const_type == ConstraintType.FOREIGN_KEY) {
            Table catalog_fkey_tbl = catalog_const.getForeignkeytable();
            String col_add = "";
            String our_columns = "";
            String fkey_columns = "";
            for (ColumnRef catalog_colref : catalog_const.getForeignkeycols()) {
                // The name of the ColumnRef is the column in our base table
                Column our_column = catalog_tbl.getColumns().getIgnoreCase(catalog_colref.getTypeName());
                assert (our_column != null);
                our_columns += col_add + our_column.getTypeName();
                Column fkey_column = catalog_colref.getColumn();
                assert (fkey_column != null);
                fkey_columns += col_add + fkey_column.getTypeName();
                col_add = ", ";
            }
            table_sb.append(add).append(spacer + "CONSTRAINT ").append(catalog_const.getTypeName()).append(" FOREIGN KEY (").append(our_columns).append(") REFERENCES ").append(catalog_fkey_tbl.getTypeName()).append(" (").append(fkey_columns).append(")");
        }
        skip_constraints.add(catalog_const);
    }
    if (catalog_tbl.getTuplelimit() != Integer.MAX_VALUE) {
        table_sb.append(add).append(spacer + "LIMIT PARTITION ROWS ").append(String.valueOf(catalog_tbl.getTuplelimit()));
        String deleteStmt = CatalogUtil.getLimitPartitionRowsDeleteStmt(catalog_tbl);
        if (deleteStmt != null) {
            if (deleteStmt.endsWith(";")) {
                // StatementCompiler appends the semicolon, we don't want it here.
                deleteStmt = deleteStmt.substring(0, deleteStmt.length() - 1);
            }
            table_sb.append("\n" + spacer + spacer + "EXECUTE (").append(deleteStmt).append(")");
        }
    }
    if (viewQuery != null) {
        table_sb.append("\n) AS \n");
        table_sb.append(spacer).append(viewQuery).append(";\n");
    } else {
        table_sb.append("\n);\n");
    }
    // We've built the full CREATE TABLE statement for this table,
    // Append the generated table schema to the canonical DDL StringBuilder
    sb.append(table_sb.toString());
    // Partition Table for regular tables (non-streams)
    if (catalog_tbl.getPartitioncolumn() != null && viewQuery == null && !isExportOnly) {
        sb.append("PARTITION TABLE ").append(catalog_tbl.getTypeName()).append(" ON COLUMN ").append(catalog_tbl.getPartitioncolumn().getTypeName()).append(";\n");
    }
    // All other Indexes
    for (Index catalog_idx : catalog_tbl.getIndexes()) {
        if (skip_indexes.contains(catalog_idx))
            continue;
        if (catalog_idx.getUnique()) {
            if (catalog_idx.getAssumeunique()) {
                sb.append("CREATE ASSUMEUNIQUE INDEX ");
            } else {
                sb.append("CREATE UNIQUE INDEX ");
            }
        } else {
            sb.append("CREATE INDEX ");
        }
        sb.append(catalog_idx.getTypeName()).append(" ON ").append(catalog_tbl.getTypeName()).append(" (");
        add = "";
        String jsonstring = catalog_idx.getExpressionsjson();
        if (jsonstring.isEmpty()) {
            for (ColumnRef catalog_colref : CatalogUtil.getSortedCatalogItems(catalog_idx.getColumns(), "index")) {
                sb.append(add).append(catalog_colref.getColumn().getTypeName());
                add = ", ";
            }
        } else {
            List<AbstractExpression> indexedExprs = null;
            try {
                indexedExprs = AbstractExpression.fromJSONArrayString(jsonstring, new StmtTargetTableScan(catalog_tbl));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (indexedExprs != null) {
                for (AbstractExpression expr : indexedExprs) {
                    sb.append(add).append(expr.explain(catalog_tbl.getTypeName()));
                    add = ", ";
                }
            }
        }
        sb.append(")");
        String jsonPredicate = catalog_idx.getPredicatejson();
        if (!jsonPredicate.isEmpty()) {
            try {
                AbstractExpression predicate = AbstractExpression.fromJSONString(jsonPredicate, new StmtTargetTableScan(catalog_tbl));
                sb.append(" WHERE ").append(predicate.explain(catalog_tbl.getTypeName()));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        sb.append(";\n");
    }
    if (catalog_tbl.getIsdred()) {
        sb.append("DR TABLE ").append(catalog_tbl.getTypeName()).append(";\n");
    }
    sb.append("\n");
    // statement to whoever might be interested (DDLCompiler, I'm looking in your direction)
    return table_sb.toString();
}
Also used : Table(org.voltdb.catalog.Table) Constraint(org.voltdb.catalog.Constraint) JSONException(org.json_voltpatches.JSONException) Index(org.voltdb.catalog.Index) Date(java.util.Date) AbstractExpression(org.voltdb.expressions.AbstractExpression) Column(org.voltdb.catalog.Column) VoltType(org.voltdb.VoltType) StmtTargetTableScan(org.voltdb.planner.parseinfo.StmtTargetTableScan) ConstraintType(org.voltdb.types.ConstraintType) ColumnRef(org.voltdb.catalog.ColumnRef) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet) ConstraintRef(org.voltdb.catalog.ConstraintRef)

Example 12 with VoltType

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

the class CatalogUtil method getSignatureForTable.

/**
     * Get a string signature for the table represented by the args
     * @param name The name of the table
     * @param schema A sorted map of the columns in the table, keyed by column index
     * @return The table signature string.
     */
public static String getSignatureForTable(String name, SortedMap<Integer, VoltType> schema) {
    StringBuilder sb = new StringBuilder();
    sb.append(name).append(SIGNATURE_TABLE_NAME_SEPARATOR);
    for (VoltType t : schema.values()) {
        sb.append(t.getSignatureChar());
    }
    return sb.toString();
}
Also used : VoltType(org.voltdb.VoltType)

Example 13 with VoltType

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

the class ProcToTestTypeConversion method getInListParameter.

private Object getInListParameter(byte valueTypeToPopulateWith, boolean strTs) {
    VoltType toType = VoltType.get(valueTypeToPopulateWith);
    switch(toType) {
        case TINYINT:
            byte[] tinyValue = { m_byteVal, m_byteVal };
            return tinyValue;
        case SMALLINT:
            short[] shortValue = { m_shortVal, m_shortVal };
            return shortValue;
        case INTEGER:
            int[] intValue = { m_intVal, m_intVal };
            return intValue;
        case BIGINT:
            long[] bigintValue = { m_bigIntVal, m_bigIntVal };
            return bigintValue;
        case FLOAT:
            double[] floatValue = { m_floatVal, m_floatVal };
            return floatValue;
        case DECIMAL:
            BigDecimal[] bigdecValue = { m_bigDecVal, m_bigDecVal };
            return bigdecValue;
        case TIMESTAMP:
            TimestampType[] tsValue = { m_tsVal, m_tsVal };
            return tsValue;
        case STRING:
            String str = strTs ? m_strTs : m_strNum;
            String[] strValue = { str, str };
            return strValue;
        case VARBINARY:
            byte[][] binValue = { { m_byteVal, m_byteVal }, { m_byteVal, m_byteVal } };
            return binValue;
        case GEOGRAPHY_POINT:
            GeographyPointValue[] ptValue = { m_pt, m_pt };
            return ptValue;
        case GEOGRAPHY:
            GeographyValue[] polyValue = { m_poly, m_poly };
            return polyValue;
        default:
            assert (false);
            break;
    }
    return null;
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) BigDecimal(java.math.BigDecimal) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 14 with VoltType

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

the class TruncatePeople method validateSame.

private void validateSame(VoltTable expectedRows, VoltTable actualRows) {
    throwVoltAbortExceptionIf("column count mismatch.  Expected: " + expectedRows.getColumnCount() + " actual: " + actualRows.getColumnCount(), expectedRows.getColumnCount() != actualRows.getColumnCount(), expectedRows, actualRows);
    throwVoltAbortExceptionIf("row count mismatch.  Expected: " + expectedRows.getRowCount() + " actual: " + actualRows.getRowCount(), expectedRows.getRowCount() != actualRows.getRowCount(), expectedRows, actualRows);
    int ii = 0;
    while (expectedRows.advanceToRow(ii)) {
        throwVoltAbortExceptionIf("too few actual rows; expected more than " + (ii + 1), !actualRows.advanceToRow(ii), expectedRows, actualRows);
        for (int j = 0; j < actualRows.getColumnCount(); j++) {
            String columnName = actualRows.getColumnName(j);
            String colPrefix = "row " + ii + ": column: " + columnName + ": ";
            VoltType actualType = actualRows.getColumnType(j);
            VoltType expectedType = expectedRows.getColumnType(j);
            throwVoltAbortExceptionIf(colPrefix + "type mismatch", expectedType != actualType, expectedRows, actualRows);
            Object expectedObj = expectedRows.get(j, expectedType);
            Object actualObj = actualRows.get(j, actualType);
            boolean expectedNull = expectedRows.wasNull();
            boolean actualNull = actualRows.wasNull();
            throwVoltAbortExceptionIf(colPrefix + "null/not null mismatch", expectedNull != actualNull, expectedRows, actualRows);
            if (expectedNull) {
                continue;
            }
            if (expectedType == VoltType.FLOAT) {
                // than assuming that FLOAT is a don't care case.
                continue;
            }
            String message = colPrefix + "values not equal: expected: " + expectedObj + ", actual: " + actualObj;
            throwVoltAbortExceptionIf(message, !expectedObj.toString().equals(actualObj.toString()), expectedRows, actualRows);
        }
        ii++;
    }
}
Also used : VoltType(org.voltdb.VoltType)

Example 15 with VoltType

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

the class TruncateTables method compareTables.

private String compareTables(String prefix, VoltTable expectedRows, VoltTable actualRows, Double epsilon) {
    if (expectedRows.getColumnCount() != actualRows.getColumnCount()) {
        return prefix + "column count mismatch." + " Expected: " + expectedRows.getColumnCount() + " actual: " + actualRows.getColumnCount();
    }
    if (expectedRows.getRowCount() != actualRows.getRowCount()) {
        long expRowCount = expectedRows.getRowCount();
        long actRowCount = actualRows.getRowCount();
        if (expRowCount + actRowCount < TOO_MUCH_INFO) {
            System.out.println("Expected: " + expectedRows);
            System.out.println("Actual:   " + actualRows);
        } else {
            System.out.println("Expected: " + expRowCount + " rows");
            System.out.println("Actual:   " + actRowCount + " rows");
        }
        return prefix + "row count mismatch.  Expected: " + expectedRows.getRowCount() + " actual: " + actualRows.getRowCount();
    }
    int ii = 1;
    while (expectedRows.advanceRow()) {
        if (!actualRows.advanceRow()) {
            return prefix + "too few actual rows; expected more than " + ii;
        }
        for (int j = 0; j < actualRows.getColumnCount(); j++) {
            String columnName = actualRows.getColumnName(j);
            String colPrefix = prefix + "row " + ii + ": column: " + columnName + ": ";
            VoltType actualType = actualRows.getColumnType(j);
            VoltType expectedType = expectedRows.getColumnType(j);
            if (expectedType != actualType) {
                return colPrefix + "type mismatch: expected:" + expectedType + " got: " + actualType;
            }
            Object expectedObj = expectedRows.get(j, expectedType);
            Object actualObj = actualRows.get(j, actualType);
            if (expectedRows.wasNull()) {
                if (actualRows.wasNull()) {
                    continue;
                }
                return colPrefix + "expected null, got non null value: " + actualObj;
            } else {
                if (actualRows.wasNull()) {
                    return colPrefix + "expected the value " + expectedObj + ", got a null value.";
                }
                String message = colPrefix + "values not equal: expected: " + expectedObj + ", actual: " + actualObj;
                if (expectedType == VoltType.FLOAT) {
                    if (epsilon != null) {
                        if (((Double) expectedObj).doubleValue() + epsilon < ((Double) actualObj).doubleValue() || (((Double) actualObj).doubleValue() + epsilon < ((Double) expectedObj).doubleValue())) {
                            return message;
                        }
                        continue;
                    }
                    // With no epsilon provided, fall through to take
                    // a chance on an exact value match, but helpfully
                    // annotate any false positive that results.
                    message += ". NOTE: You may want to pass a" + " non-null epsilon value >= " + Math.abs((Double) expectedObj - (Double) actualObj) + " to the table comparison test " + " if nearly equal FLOAT values are " + " causing a false positive mismatch.";
                }
                if (!expectedObj.equals(actualObj)) {
                    return message;
                }
            }
        }
        ++ii;
    }
    return "";
}
Also used : VoltType(org.voltdb.VoltType)

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