Skip to content

Commit

Permalink
代码重构: db模块与表达式相关的代码移到sql模块,独立出optimizer子模块
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Sep 30, 2018
1 parent a2eefd2 commit 7db2300
Show file tree
Hide file tree
Showing 85 changed files with 697 additions and 754 deletions.
26 changes: 0 additions & 26 deletions lealone-common/src/main/java/org/lealone/sql/Expression.java

This file was deleted.

Expand Up @@ -15,16 +15,29 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.lealone.db.expression; package org.lealone.sql;


import org.lealone.db.ServerSession; import java.util.Set;
import org.lealone.db.table.ColumnResolver;
import org.lealone.db.table.TableFilter; import org.lealone.db.Session;
import org.lealone.db.value.Value; import org.lealone.db.value.Value;


public interface Expression extends org.lealone.sql.Expression { public interface IExpression {

interface Evaluator {

IExpression optimizeExpression(Session session, IExpression e);

Value getExpressionValue(Session session, IExpression e, Object data);
}

int getType();

String getSQL();


Value getValue(ServerSession session); IExpression optimize(Session session);

Value getValue(Session session);


String getAlias(); String getAlias();


Expand All @@ -36,9 +49,6 @@ public interface Expression extends org.lealone.sql.Expression {


String getColumnName(); String getColumnName();


@Override
int getType();

long getPrecision(); long getPrecision();


int getNullable(); int getNullable();
Expand All @@ -47,27 +57,13 @@ public interface Expression extends org.lealone.sql.Expression {


int getScale(); int getScale();


@Override
String getSQL();

String getSQL(boolean isDistributed); String getSQL(boolean isDistributed);


Expression getNonAliasExpression(); IExpression getNonAliasExpression();


boolean isConstant(); boolean isConstant();


boolean isEverything(ExpressionVisitor visitor); void getDependencies(Set<?> dependencies);

void setEvaluatable(TableFilter tableFilter, boolean value);

Expression optimize(ServerSession session);

void addFilterConditions(TableFilter filter, boolean outerJoin);

void mapColumns(ColumnResolver resolver, int level);

void createIndexConditions(ServerSession session, TableFilter filter);

Boolean getBooleanValue(ServerSession session);


void getColumns(Set<?> columns);
} }
Expand Up @@ -15,39 +15,38 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.lealone.db.expression; package org.lealone.sql;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;


import org.lealone.db.CommandParameter; import org.lealone.db.CommandParameter;
import org.lealone.db.result.Result; import org.lealone.db.result.Result;
import org.lealone.db.table.Table;


public interface Query { public interface IQuery {


Result query(int maxRows); Result query(int maxRows);


String getPlanSQL(); String getPlanSQL();


boolean isEverything(ExpressionVisitor visitor);

ArrayList<? extends CommandParameter> getParameters(); ArrayList<? extends CommandParameter> getParameters();


void addGlobalCondition(CommandParameter param, int columnId, int comparisonType); void addGlobalCondition(CommandParameter param, int columnId, int indexConditionType);


void disableCache(); void disableCache();


double getCost(); double getCost();


HashSet<Table> getTables(); HashSet<?> getTables();


boolean allowGlobalConditions(); boolean allowGlobalConditions();


int getColumnCount(); int getColumnCount();


ArrayList<? extends Expression> getExpressions(); ArrayList<? extends IExpression> getExpressions();


long getMaxDataModificationId(); long getMaxDataModificationId();


boolean isDeterministic();

} }
Expand Up @@ -15,38 +15,38 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.lealone.db.expression; package org.lealone.sql;


import org.lealone.db.result.LocalResult; import org.lealone.db.result.Result;


public interface SelectUnion { public interface ISelectUnion {


/** /**
* The type of a UNION statement. * The type of a UNION statement.
*/ */
public static final int UNION = 0; public static final int UNION = 0;


/** /**
* The type of a UNION ALL statement. * The type of a UNION ALL statement.
*/ */
public static final int UNION_ALL = 1; public static final int UNION_ALL = 1;


/** /**
* The type of an EXCEPT statement. * The type of an EXCEPT statement.
*/ */
public static final int EXCEPT = 2; public static final int EXCEPT = 2;


/** /**
* The type of an INTERSECT statement. * The type of an INTERSECT statement.
*/ */
public static final int INTERSECT = 3; public static final int INTERSECT = 3;


int getUnionType(); int getUnionType();


Query getLeft(); IQuery getLeft();


Query getRight(); IQuery getRight();


LocalResult getEmptyResult(); Result getEmptyResult();


} }
6 changes: 3 additions & 3 deletions lealone-common/src/main/java/org/lealone/sql/SQLEngine.java
Expand Up @@ -30,10 +30,10 @@ public interface SQLEngine extends PluggableEngine {


CommandParameter createParameter(int index); CommandParameter createParameter(int index);


Expression createValueExpression(Value value); IExpression createValueExpression(Value value);


Expression createSequenceValue(Object sequence); IExpression createSequenceValue(Object sequence);


Expression createConditionAndOr(boolean and, Expression left, Expression right); IExpression createConditionAndOr(boolean and, IExpression left, IExpression right);


} }
Expand Up @@ -21,7 +21,7 @@ public interface SQLParser {


void setRightsChecked(boolean rightsChecked); void setRightsChecked(boolean rightsChecked);


Expression parseExpression(String sql); IExpression parseExpression(String sql);


ParsedStatement parse(String sql); ParsedStatement parse(String sql);


Expand Down
13 changes: 3 additions & 10 deletions lealone-db/src/main/java/org/lealone/db/constraint/Constraint.java
Expand Up @@ -6,12 +6,13 @@
package org.lealone.db.constraint; package org.lealone.db.constraint;


import java.util.HashSet; import java.util.HashSet;
import java.util.Set;


import org.lealone.common.exceptions.DbException; import org.lealone.common.exceptions.DbException;
import org.lealone.common.trace.Trace; import org.lealone.common.trace.Trace;
import org.lealone.db.DbObject;
import org.lealone.db.DbObjectType; import org.lealone.db.DbObjectType;
import org.lealone.db.ServerSession; import org.lealone.db.ServerSession;
import org.lealone.db.expression.ExpressionVisitor;
import org.lealone.db.index.Index; import org.lealone.db.index.Index;
import org.lealone.db.result.Row; import org.lealone.db.result.Row;
import org.lealone.db.schema.Schema; import org.lealone.db.schema.Schema;
Expand Down Expand Up @@ -175,15 +176,7 @@ public boolean isHidden() {
return table.isHidden(); return table.isHidden();
} }


/** public void getDependencies(Set<DbObject> dependencies) {
* Visit all elements in the constraint.
*
* @param visitor the visitor
* @return true if every visited expression returned true, or if there are
* no expressions
*/
public boolean isEverything(ExpressionVisitor visitor) {
return true;
} }


} }
Expand Up @@ -7,29 +7,29 @@


import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set;


import org.lealone.common.exceptions.DbException; import org.lealone.common.exceptions.DbException;
import org.lealone.common.util.New; import org.lealone.common.util.New;
import org.lealone.common.util.StringUtils; import org.lealone.common.util.StringUtils;
import org.lealone.db.DbObject;
import org.lealone.db.ServerSession; import org.lealone.db.ServerSession;
import org.lealone.db.api.ErrorCode; import org.lealone.db.api.ErrorCode;
import org.lealone.db.expression.Expression;
import org.lealone.db.expression.ExpressionVisitor;
import org.lealone.db.index.Index; import org.lealone.db.index.Index;
import org.lealone.db.result.Result; import org.lealone.db.result.Result;
import org.lealone.db.result.Row; import org.lealone.db.result.Row;
import org.lealone.db.schema.Schema; import org.lealone.db.schema.Schema;
import org.lealone.db.table.Column; import org.lealone.db.table.Column;
import org.lealone.db.table.Table; import org.lealone.db.table.Table;
import org.lealone.db.table.TableFilter; import org.lealone.sql.IExpression;


/** /**
* A check constraint. * A check constraint.
*/ */
public class ConstraintCheck extends Constraint { public class ConstraintCheck extends Constraint {


private TableFilter filter; private IExpression.Evaluator exprEvaluator;
private Expression expr; private IExpression expr;


public ConstraintCheck(Schema schema, int id, String name, Table table) { public ConstraintCheck(Schema schema, int id, String name, Table table) {
super(schema, id, name, table); super(schema, id, name, table);
Expand All @@ -40,11 +40,11 @@ public String getConstraintType() {
return Constraint.CHECK; return Constraint.CHECK;
} }


public void setTableFilter(TableFilter filter) { public void setExpressionEvaluator(IExpression.Evaluator exprEvaluator) {
this.filter = filter; this.exprEvaluator = exprEvaluator;
} }


public void setExpression(Expression expr) { public void setExpression(IExpression expr) {
this.expr = expr; this.expr = expr;
} }


Expand Down Expand Up @@ -76,7 +76,7 @@ public String getCreateSQL() {
public void removeChildrenAndResources(ServerSession session) { public void removeChildrenAndResources(ServerSession session) {
table.removeConstraint(this); table.removeConstraint(this);
database.removeMeta(session, getId()); database.removeMeta(session, getId());
filter = null; exprEvaluator = null;
expr = null; expr = null;
table = null; table = null;
invalidate(); invalidate();
Expand All @@ -87,10 +87,9 @@ public void checkRow(ServerSession session, Table t, Row oldRow, Row newRow) {
if (newRow == null) { if (newRow == null) {
return; return;
} }
filter.set(newRow);
Boolean b; Boolean b;
try { try {
b = expr.getValue(session).getBoolean(); b = exprEvaluator.getExpressionValue(session, expr, newRow).getBoolean();
} catch (DbException ex) { } catch (DbException ex) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, ex, getShortDescription()); throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, ex, getShortDescription());
} }
Expand All @@ -113,7 +112,7 @@ public void setIndexOwner(Index index) {
@Override @Override
public HashSet<Column> getReferencedColumns(Table table) { public HashSet<Column> getReferencedColumns(Table table) {
HashSet<Column> columns = New.hashSet(); HashSet<Column> columns = New.hashSet();
expr.isEverything(ExpressionVisitor.getColumnsVisitor(columns)); expr.getColumns(columns);
for (Iterator<Column> it = columns.iterator(); it.hasNext();) { for (Iterator<Column> it = columns.iterator(); it.hasNext();) {
if (it.next().getTable() != table) { if (it.next().getTable() != table) {
it.remove(); it.remove();
Expand All @@ -122,7 +121,7 @@ public HashSet<Column> getReferencedColumns(Table table) {
return columns; return columns;
} }


public Expression getExpression() { public IExpression getExpression() {
return expr; return expr;
} }


Expand All @@ -137,7 +136,7 @@ public void checkExistingData(ServerSession session) {
// don't check at startup // don't check at startup
return; return;
} }
String sql = "SELECT 1 FROM " + filter.getTable().getSQL() + " WHERE NOT(" + expr.getSQL() + ")"; String sql = "SELECT 1 FROM " + table.getSQL() + " WHERE NOT(" + expr.getSQL() + ")";
Result r = session.prepareStatement(sql).query(1); Result r = session.prepareStatement(sql).query(1);
if (r.next()) { if (r.next()) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName()); throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName());
Expand All @@ -155,8 +154,7 @@ public void rebuild() {
} }


@Override @Override
public boolean isEverything(ExpressionVisitor visitor) { public void getDependencies(Set<DbObject> dependencies) {
return expr.isEverything(visitor); expr.getDependencies(dependencies);
} }

} }
Expand Up @@ -15,7 +15,6 @@
import org.lealone.db.CommandParameter; import org.lealone.db.CommandParameter;
import org.lealone.db.ServerSession; import org.lealone.db.ServerSession;
import org.lealone.db.api.ErrorCode; import org.lealone.db.api.ErrorCode;
import org.lealone.db.expression.Expression;
import org.lealone.db.index.Cursor; import org.lealone.db.index.Cursor;
import org.lealone.db.index.Index; import org.lealone.db.index.Index;
import org.lealone.db.result.Result; import org.lealone.db.result.Result;
Expand All @@ -27,6 +26,7 @@
import org.lealone.db.table.Table; import org.lealone.db.table.Table;
import org.lealone.db.value.Value; import org.lealone.db.value.Value;
import org.lealone.db.value.ValueNull; import org.lealone.db.value.ValueNull;
import org.lealone.sql.IExpression;
import org.lealone.sql.PreparedStatement; import org.lealone.sql.PreparedStatement;


/** /**
Expand Down Expand Up @@ -560,7 +560,7 @@ private PreparedStatement prepare(ServerSession session, String sql, int action)
if (action == SET_NULL) { if (action == SET_NULL) {
value = ValueNull.INSTANCE; value = ValueNull.INSTANCE;
} else { } else {
Expression expr = column.getDefaultExpression(); IExpression expr = column.getDefaultExpression();
if (expr == null) { if (expr == null) {
throw DbException.get(ErrorCode.NO_DEFAULT_SET_1, column.getName()); throw DbException.get(ErrorCode.NO_DEFAULT_SET_1, column.getName());
} }
Expand Down

0 comments on commit 7db2300

Please sign in to comment.