Search in sources :

Example 36 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class Plan method calculateCost.

/**
 * Calculate the cost of this query plan.
 *
 * @param session the session
 * @return the cost
 */
public double calculateCost(Session session) {
    double cost = 1;
    boolean invalidPlan = false;
    int level = 1;
    for (TableFilter tableFilter : allFilters) {
        PlanItem item = tableFilter.getBestPlanItem(session, level++);
        planItems.put(tableFilter, item);
        cost += cost * item.cost;
        setEvaluatable(tableFilter, true);
        Expression on = tableFilter.getJoinCondition();
        if (on != null) {
            if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
                invalidPlan = true;
                break;
            }
        }
    }
    if (invalidPlan) {
        cost = Double.POSITIVE_INFINITY;
    }
    for (TableFilter f : allFilters) {
        setEvaluatable(f, false);
    }
    return cost;
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression)

Example 37 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class SortOrder method getColumn.

/**
 * Get the column for the given table filter, if the sort column is for this
 * filter.
 *
 * @param index  the column index (0, 1,..)
 * @param filter the table filter
 * @return the column, or null
 */
public Column getColumn(int index, TableFilter filter) {
    if (orderList == null) {
        return null;
    }
    SelectOrderBy order = orderList.get(index);
    Expression expr = order.expression;
    if (expr == null) {
        return null;
    }
    expr = expr.getNonAliasExpression();
    if (expr.isConstant()) {
        return null;
    }
    if (!(expr instanceof ExpressionColumn)) {
        return null;
    }
    ExpressionColumn exprCol = (ExpressionColumn) expr;
    if (exprCol.getTableFilter() != filter) {
        return null;
    }
    return exprCol.getColumn();
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy) Expression(com.wplatform.ddal.command.expression.Expression) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn)

Example 38 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class Update method updateRows.

protected int updateRows() {
    tableFilter.startQuery(session);
    tableFilter.reset();
    RowList rows = new RowList(session);
    try {
        Table table = tableFilter.getTable();
        session.getUser().checkRight(table, Right.UPDATE);
        // table.lock(session, true, false);
        int columnCount = table.getColumns().length;
        // get the old rows, compute the new rows
        setCurrentRowNumber(0);
        int count = 0;
        Column[] columns = table.getColumns();
        int limitRows = -1;
        if (limitExpr != null) {
            Value v = limitExpr.getValue(session);
            if (v != ValueNull.INSTANCE) {
                limitRows = v.getInt();
            }
        }
        while (tableFilter.next()) {
            setCurrentRowNumber(count + 1);
            if (limitRows >= 0 && count >= limitRows) {
                break;
            }
            if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
                Row oldRow = tableFilter.get();
                Row newRow = table.getTemplateRow();
                for (int i = 0; i < columnCount; i++) {
                    Expression newExpr = expressionMap.get(columns[i]);
                    Value newValue;
                    if (newExpr == null) {
                        newValue = oldRow.getValue(i);
                    } else if (newExpr == ValueExpression.getDefault()) {
                        Column column = table.getColumn(i);
                        newValue = table.getDefaultValue(session, column);
                    } else {
                        Column column = table.getColumn(i);
                        newValue = column.convert(newExpr.getValue(session));
                    }
                    newRow.setValue(i, newValue);
                }
                table.validateConvertUpdateSequence(session, newRow);
                rows.add(oldRow);
                rows.add(newRow);
                count++;
            }
        }
        // table.updateRows(this, session, rows);
        return count;
    } finally {
        rows.close();
    }
}
Also used : RowList(com.wplatform.ddal.result.RowList) Table(com.wplatform.ddal.dbobject.table.Table) Column(com.wplatform.ddal.dbobject.table.Column) ValueExpression(com.wplatform.ddal.command.expression.ValueExpression) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) Row(com.wplatform.ddal.result.Row)

Example 39 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class IndexCondition method getCurrentValueList.

/**
 * Get the current value list of the expression. The value list is of the
 * same type as the column, distinct, and sorted.
 *
 * @param session the session
 * @return the value list
 */
public Value[] getCurrentValueList(Session session) {
    HashSet<Value> valueSet = new HashSet<Value>();
    for (Expression e : expressionList) {
        Value v = e.getValue(session);
        v = column.convert(v);
        valueSet.add(v);
    }
    Value[] array = new Value[valueSet.size()];
    valueSet.toArray(array);
    final CompareMode mode = session.getDatabase().getCompareMode();
    Arrays.sort(array, new Comparator<Value>() {

        @Override
        public int compare(Value o1, Value o2) {
            return o1.compareTo(o2, mode);
        }
    });
    return array;
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) CompareMode(com.wplatform.ddal.value.CompareMode)

Example 40 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class IndexCondition method getSQL.

/**
 * Get the SQL snippet of this comparison.
 *
 * @return the SQL snippet
 */
public String getSQL() {
    if (compareType == Comparison.FALSE) {
        return "FALSE";
    }
    StatementBuilder buff = new StatementBuilder();
    buff.append(column.getSQL());
    switch(compareType) {
        case Comparison.EQUAL:
            buff.append(" = ");
            break;
        case Comparison.EQUAL_NULL_SAFE:
            buff.append(" IS ");
            break;
        case Comparison.BIGGER_EQUAL:
            buff.append(" >= ");
            break;
        case Comparison.BIGGER:
            buff.append(" > ");
            break;
        case Comparison.SMALLER_EQUAL:
            buff.append(" <= ");
            break;
        case Comparison.SMALLER:
            buff.append(" < ");
            break;
        case Comparison.IN_LIST:
            buff.append(" IN(");
            for (Expression e : expressionList) {
                buff.appendExceptFirst(", ");
                buff.append(e.getSQL());
            }
            buff.append(')');
            break;
        case Comparison.IN_QUERY:
            buff.append(" IN(");
            buff.append(expressionQuery.getPlanSQL());
            buff.append(')');
            break;
        default:
            DbException.throwInternalError("type=" + compareType);
    }
    if (expression != null) {
        buff.append(expression.getSQL());
    }
    return buff.toString();
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Aggregations

Expression (com.wplatform.ddal.command.expression.Expression)40 Column (com.wplatform.ddal.dbobject.table.Column)15 Value (com.wplatform.ddal.value.Value)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)9 Row (com.wplatform.ddal.result.Row)5 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)4 TableMate (com.wplatform.ddal.dbobject.table.TableMate)4 DbException (com.wplatform.ddal.message.DbException)4 SearchRow (com.wplatform.ddal.result.SearchRow)4 Query (com.wplatform.ddal.command.dml.Query)3 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Database (com.wplatform.ddal.engine.Database)3 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 Parameter (com.wplatform.ddal.command.expression.Parameter)2 Index (com.wplatform.ddal.dbobject.index.Index)2 LocalResult (com.wplatform.ddal.result.LocalResult)2 Prepared (com.wplatform.ddal.command.Prepared)1 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 SelectOrderBy (com.wplatform.ddal.command.dml.SelectOrderBy)1