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;
}
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();
}
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();
}
}
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;
}
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();
}
Aggregations