Search in sources :

Example 1 with UnsafeOperatorsForDDL

use of org.voltdb.expressions.AbstractExpression.UnsafeOperatorsForDDL 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 2 with UnsafeOperatorsForDDL

use of org.voltdb.expressions.AbstractExpression.UnsafeOperatorsForDDL in project voltdb by VoltDB.

the class MaterializedViewProcessor method checkViewMeetsSpec.

/**
     * Verify the materialized view meets our arcane rules about what can and can't
     * go in a materialized view. Throw hopefully helpful error messages when these
     * rules are inevitably borked.
     *
     * @param viewName The name of the view being checked.
     * @param stmt The output from the parser describing the select statement that creates the view.
     * @throws VoltCompilerException
     */
private void checkViewMeetsSpec(String viewName, ParsedSelectStmt stmt) throws VoltCompilerException {
    int groupColCount = stmt.groupByColumns().size();
    int displayColCount = stmt.m_displayColumns.size();
    StringBuffer msg = new StringBuffer();
    msg.append("Materialized view \"" + viewName + "\" ");
    if (stmt.getParameters().length > 0) {
        msg.append("contains placeholders (?), which are not allowed in the SELECT query for a view.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    List<AbstractExpression> checkExpressions = new ArrayList<>();
    int i;
    // the beginning of the display list.
    for (i = 0; i < groupColCount; i++) {
        ParsedColInfo gbcol = stmt.groupByColumns().get(i);
        ParsedColInfo outcol = stmt.m_displayColumns.get(i);
        // The columns must be equal.
        if (!outcol.expression.equals(gbcol.expression)) {
            msg.append("must exactly match the GROUP BY clause at index " + String.valueOf(i) + " of SELECT list.");
            throw m_compiler.new VoltCompilerException(msg.toString());
        }
        // check if the expression return type is not unique indexable
        StringBuffer exprMsg = new StringBuffer();
        if (!outcol.expression.isValueTypeUniqueIndexable(exprMsg)) {
            msg.append("with " + exprMsg + " in GROUP BY clause not supported.");
            throw m_compiler.new VoltCompilerException(msg.toString());
        }
        // collect all the expressions and we will check
        // for other guards on all of them together
        checkExpressions.add(outcol.expression);
    }
    // check for count star in the display list
    boolean countStarFound = false;
    if (i < displayColCount) {
        AbstractExpression coli = stmt.m_displayColumns.get(i).expression;
        if (coli.getExpressionType() == ExpressionType.AGGREGATE_COUNT_STAR) {
            countStarFound = true;
        }
    }
    if (countStarFound == false) {
        msg.append("must have count(*) after the GROUP BY columns (if any) but before the aggregate functions (if any).");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    UnsafeOperatorsForDDL unsafeOps = new UnsafeOperatorsForDDL();
    // must be count(), min(), max() or sum().
    for (i++; i < displayColCount; i++) {
        ParsedColInfo outcol = stmt.m_displayColumns.get(i);
        // second one would fail.
        if ((outcol.expression.getExpressionType() != ExpressionType.AGGREGATE_COUNT) && (outcol.expression.getExpressionType() != ExpressionType.AGGREGATE_SUM) && (outcol.expression.getExpressionType() != ExpressionType.AGGREGATE_MIN) && (outcol.expression.getExpressionType() != ExpressionType.AGGREGATE_MAX)) {
            msg.append("must have non-group by columns aggregated by sum, count, min or max.");
            throw m_compiler.new VoltCompilerException(msg.toString());
        }
        // want to fail on legal aggregate expressions.
        if (outcol.expression.getLeft() != null) {
            checkExpressions.add(outcol.expression.getLeft());
        }
        // Check if the aggregation is safe for non-empty view source table.
        outcol.expression.findUnsafeOperatorsForDDL(unsafeOps);
        assert (outcol.expression.getRight() == null);
        assert (outcol.expression.getArgs() == null || outcol.expression.getArgs().size() == 0);
    }
    AbstractExpression where = stmt.getSingleTableFilterExpression();
    if (where != null) {
        checkExpressions.add(where);
    }
    /*
         * Gather up all the join expressions.  The ParsedSelectStatement
         * has not been analyzed yet, so it's not clear where these are.  But
         * the stmt knows.
         */
    stmt.gatherJoinExpressions(checkExpressions);
    if (stmt.getHavingPredicate() != null) {
        checkExpressions.add(stmt.getHavingPredicate());
    }
    // Check all the subexpressions we gathered up.
    if (!AbstractExpression.validateExprsForIndexesAndMVs(checkExpressions, msg)) {
        // The error message will be in the StringBuffer msg.
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    // views on nonempty tables.
    for (AbstractExpression expr : checkExpressions) {
        expr.findUnsafeOperatorsForDDL(unsafeOps);
    }
    if (unsafeOps.isUnsafe()) {
        stmt.setUnsafeDDLMessage(unsafeOps.toString());
    }
    if (stmt.hasSubquery()) {
        msg.append("with subquery sources is not supported.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    if (!stmt.m_joinTree.allInnerJoins()) {
        throw m_compiler.new VoltCompilerException("Materialized view only supports INNER JOIN.");
    }
    if (stmt.orderByColumns().size() != 0) {
        msg.append("with ORDER BY clause is not supported.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    if (stmt.hasLimitOrOffset()) {
        msg.append("with LIMIT or OFFSET clause is not supported.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    if (stmt.getHavingPredicate() != null) {
        msg.append("with HAVING clause is not supported.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    if (displayColCount <= groupColCount) {
        msg.append("has too few columns.");
        throw m_compiler.new VoltCompilerException(msg.toString());
    }
    checkViewSources(stmt.m_tableList);
}
Also used : UnsafeOperatorsForDDL(org.voltdb.expressions.AbstractExpression.UnsafeOperatorsForDDL) ParsedColInfo(org.voltdb.planner.ParsedColInfo) AbstractExpression(org.voltdb.expressions.AbstractExpression) ArrayList(java.util.ArrayList) VoltCompilerException(org.voltdb.compiler.VoltCompiler.VoltCompilerException) Constraint(org.voltdb.catalog.Constraint)

Aggregations

ArrayList (java.util.ArrayList)2 Constraint (org.voltdb.catalog.Constraint)2 VoltCompilerException (org.voltdb.compiler.VoltCompiler.VoltCompilerException)2 AbstractExpression (org.voltdb.expressions.AbstractExpression)2 UnsafeOperatorsForDDL (org.voltdb.expressions.AbstractExpression.UnsafeOperatorsForDDL)2 VoltXMLElement (org.hsqldb_voltpatches.VoltXMLElement)1 JSONException (org.json_voltpatches.JSONException)1 VoltType (org.voltdb.VoltType)1 Column (org.voltdb.catalog.Column)1 ColumnRef (org.voltdb.catalog.ColumnRef)1 Index (org.voltdb.catalog.Index)1 TupleValueExpression (org.voltdb.expressions.TupleValueExpression)1 AbstractParsedStmt (org.voltdb.planner.AbstractParsedStmt)1 ParsedColInfo (org.voltdb.planner.ParsedColInfo)1 ParsedSelectStmt (org.voltdb.planner.ParsedSelectStmt)1