use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class Query method getParameterValues.
public final Value[] getParameterValues() {
ArrayList<Parameter> list = getParameters();
if (list == null) {
list = New.arrayList();
}
int size = list.size();
Value[] params = new Value[size];
for (int i = 0; i < size; i++) {
Value v = list.get(i).getParamValue();
params[i] = v;
}
return params;
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class Comparison method optimize.
@Override
public Expression optimize(Session session) {
left = left.optimize(session);
if (right != null) {
right = right.optimize(session);
if (right instanceof ExpressionColumn) {
if (left.isConstant() || left instanceof Parameter) {
Expression temp = left;
left = right;
right = temp;
compareType = getReversedCompareType(compareType);
}
}
if (left instanceof ExpressionColumn) {
if (right.isConstant()) {
Value r = right.getValue(session);
if (r == ValueNull.INSTANCE) {
if ((compareType & NULL_SAFE) == 0) {
return ValueExpression.getNull();
}
}
} else if (right instanceof Parameter) {
((Parameter) right).setColumn(((ExpressionColumn) left).getColumn());
}
}
}
if (compareType == IS_NULL || compareType == IS_NOT_NULL) {
if (left.isConstant()) {
return ValueExpression.get(getValue(session));
}
} else {
if (SysProperties.CHECK && (left == null || right == null)) {
DbException.throwInternalError();
}
if (left == ValueExpression.getNull() || right == ValueExpression.getNull()) {
// a NULL constants
if ((compareType & NULL_SAFE) == 0) {
return ValueExpression.getNull();
}
}
if (left.isConstant() && right.isConstant()) {
return ValueExpression.get(getValue(session));
}
}
return this;
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class ConditionAndOr method optimize.
@Override
public Expression optimize(Session session) {
// NULL handling: see wikipedia,
// http://www-cs-students.stanford.edu/~wlam/compsci/sqlnulls
left = left.optimize(session);
right = right.optimize(session);
int lc = left.getCost(), rc = right.getCost();
if (rc < lc) {
Expression t = left;
left = right;
right = t;
}
// SELECT * FROM TEST WHERE NOT (B=A AND B=0 AND A=0); // 1, NULL
if (session.getDatabase().getSettings().optimizeTwoEquals && andOrType == AND) {
// try to add conditions (A=B AND B=1: add A=1)
if (left instanceof Comparison && right instanceof Comparison) {
Comparison compLeft = (Comparison) left;
Comparison compRight = (Comparison) right;
Expression added = compLeft.getAdditional(session, compRight, true);
if (added != null) {
added = added.optimize(session);
ConditionAndOr a = new ConditionAndOr(AND, this, added);
return a;
}
}
}
// (A=1 AND (B=2 OR B=3))
if (andOrType == OR && session.getDatabase().getSettings().optimizeOr) {
// try to add conditions (A=B AND B=1: add A=1)
if (left instanceof Comparison && right instanceof Comparison) {
Comparison compLeft = (Comparison) left;
Comparison compRight = (Comparison) right;
Expression added = compLeft.getAdditional(session, compRight, false);
if (added != null) {
return added.optimize(session);
}
} else if (left instanceof ConditionIn && right instanceof Comparison) {
Expression added = ((ConditionIn) left).getAdditional((Comparison) right);
if (added != null) {
return added.optimize(session);
}
} else if (right instanceof ConditionIn && left instanceof Comparison) {
Expression added = ((ConditionIn) right).getAdditional((Comparison) left);
if (added != null) {
return added.optimize(session);
}
} else if (left instanceof ConditionInConstantSet && right instanceof Comparison) {
Expression added = ((ConditionInConstantSet) left).getAdditional(session, (Comparison) right);
if (added != null) {
return added.optimize(session);
}
} else if (right instanceof ConditionInConstantSet && left instanceof Comparison) {
Expression added = ((ConditionInConstantSet) right).getAdditional(session, (Comparison) left);
if (added != null) {
return added.optimize(session);
}
}
}
// TODO optimization: convert .. OR .. to UNION if the cost is lower
Value l = left.isConstant() ? left.getValue(session) : null;
Value r = right.isConstant() ? right.getValue(session) : null;
if (l == null && r == null) {
return this;
}
if (l != null && r != null) {
return ValueExpression.get(getValue(session));
}
switch(andOrType) {
case AND:
if (l != null) {
if (Boolean.FALSE.equals(l.getBoolean())) {
return ValueExpression.get(l);
} else if (Boolean.TRUE.equals(l.getBoolean())) {
return right;
}
} else if (r != null) {
if (Boolean.FALSE.equals(r.getBoolean())) {
return ValueExpression.get(r);
} else if (Boolean.TRUE.equals(r.getBoolean())) {
return left;
}
}
break;
case OR:
if (l != null) {
if (Boolean.TRUE.equals(l.getBoolean())) {
return ValueExpression.get(l);
} else if (Boolean.FALSE.equals(l.getBoolean())) {
return right;
}
} else if (r != null) {
if (Boolean.TRUE.equals(r.getBoolean())) {
return ValueExpression.get(r);
} else if (Boolean.FALSE.equals(r.getBoolean())) {
return left;
}
}
break;
default:
DbException.throwInternalError("type=" + andOrType);
}
return this;
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class ConditionIn method getValue.
@Override
public Value getValue(Session session) {
Value l = left.getValue(session);
if (l == ValueNull.INSTANCE) {
return l;
}
boolean result = false;
boolean hasNull = false;
for (Expression e : valueList) {
Value r = e.getValue(session);
if (r == ValueNull.INSTANCE) {
hasNull = true;
} else {
r = r.convertTo(l.getType());
result = Comparison.compareNotNull(database, l, r, Comparison.EQUAL);
if (result) {
break;
}
}
}
if (!result && hasNull) {
return ValueNull.INSTANCE;
}
return ValueBoolean.get(result);
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class ConditionInSelect method getValueSlow.
private Value getValueSlow(LocalResult rows, Value l) {
// this only returns the correct result if the result has at least one
// row, and if l is not null
boolean hasNull = false;
boolean result = all;
while (rows.next()) {
boolean value;
Value r = rows.currentRow()[0];
if (r == ValueNull.INSTANCE) {
value = false;
hasNull = true;
} else {
value = Comparison.compareNotNull(database, l, r, compareType);
}
if (!value && all) {
result = false;
break;
} else if (value && !all) {
result = true;
break;
}
}
if (!result && hasNull) {
return ValueNull.INSTANCE;
}
return ValueBoolean.get(result);
}
Aggregations