/*
* Copyright 2014-2015 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wplatform.ddal.command.expression;
import java.util.List;
import com.wplatform.ddal.dbobject.table.ColumnResolver;
import com.wplatform.ddal.dbobject.table.TableFilter;
import com.wplatform.ddal.engine.Session;
import com.wplatform.ddal.value.Value;
import com.wplatform.ddal.value.ValueNull;
/**
* A NOT condition.
*/
public class ConditionNot extends Condition {
private Expression condition;
public ConditionNot(Expression condition) {
this.condition = condition;
}
@Override
public Expression getNotIfPossible(Session session) {
return condition;
}
@Override
public Value getValue(Session session) {
Value v = condition.getValue(session);
if (v == ValueNull.INSTANCE) {
return v;
}
return v.convertTo(Value.BOOLEAN).negate();
}
@Override
public void mapColumns(ColumnResolver resolver, int level) {
condition.mapColumns(resolver, level);
}
@Override
public Expression optimize(Session session) {
Expression e2 = condition.getNotIfPossible(session);
if (e2 != null) {
return e2.optimize(session);
}
Expression expr = condition.optimize(session);
if (expr.isConstant()) {
Value v = expr.getValue(session);
if (v == ValueNull.INSTANCE) {
return ValueExpression.getNull();
}
return ValueExpression.get(v.convertTo(Value.BOOLEAN).negate());
}
condition = expr;
return this;
}
@Override
public void setEvaluatable(TableFilter tableFilter, boolean b) {
condition.setEvaluatable(tableFilter, b);
}
@Override
public String getSQL() {
return "(NOT " + condition.getSQL() + ")";
}
@Override
public void updateAggregate(Session session) {
condition.updateAggregate(session);
}
@Override
public void addFilterConditions(TableFilter filter, boolean outerJoin) {
if (outerJoin) {
// can not optimize:
// select * from test t1 left join test t2 on t1.id = t2.id where
// not t2.id is not null
// to
// select * from test t1 left join test t2 on t1.id = t2.id and
// t2.id is not null
return;
}
super.addFilterConditions(filter, outerJoin);
}
@Override
public boolean isEverything(ExpressionVisitor visitor) {
return condition.isEverything(visitor);
}
@Override
public int getCost() {
return condition.getCost();
}
@Override
public String exportParameters(TableFilter filter, List<Value> container) {
return "(NOT " + condition.exportParameters(filter, container) + ")";
}
}