Search in sources :

Example 21 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project dble by actiontech.

the class DefaultDruidParser method buildRouteCalculateUnits.

private List<RouteCalculateUnit> buildRouteCalculateUnits(Map<String, String> tableAliasMap, List<List<Condition>> conditionList) {
    List<RouteCalculateUnit> retList = new ArrayList<>();
    // find partition column in condition
    for (List<Condition> aConditionList : conditionList) {
        RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
        for (Condition condition : aConditionList) {
            List<Object> values = condition.getValues();
            if (values.size() == 0) {
                continue;
            }
            if (checkConditionValues(values)) {
                String columnName = StringUtil.removeBackQuote(condition.getColumn().getName().toUpperCase());
                String tableName = StringUtil.removeBackQuote(condition.getColumn().getTable());
                if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
                    tableName = tableName.toLowerCase();
                }
                if (tableAliasMap != null && tableAliasMap.get(tableName) == null) {
                    // ignore subQuery's alias
                    continue;
                }
                if (tableAliasMap != null && tableAliasMap.get(tableName) != null && !tableAliasMap.get(tableName).equals(tableName)) {
                    tableName = tableAliasMap.get(tableName);
                }
                String operator = condition.getOperator();
                // execute only between ,in and =
                if (operator.equals("between")) {
                    RangeValue rv = new RangeValue(values.get(0), values.get(1), RangeValue.EE);
                    routeCalculateUnit.addShardingExpr(tableName, columnName, rv);
                } else if (operator.equals("=") || operator.toLowerCase().equals("in")) {
                    routeCalculateUnit.addShardingExpr(tableName, columnName, values.toArray());
                }
            }
        }
        retList.add(routeCalculateUnit);
    }
    return retList;
}
Also used : RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) RangeValue(com.actiontech.dble.sqlengine.mpp.RangeValue)

Example 22 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.

the class SchemaStatVisitor method handleCondition.

protected void handleCondition(SQLExpr expr, String operator, SQLExpr... valueExprs) {
    if (expr instanceof SQLCastExpr) {
        expr = ((SQLCastExpr) expr).getExpr();
    } else if (expr instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr func = (SQLMethodInvokeExpr) expr;
        List<SQLExpr> arguments = func.getArguments();
        if (func.methodNameHashCode64() == FnvHash.Constants.COALESCE && arguments.size() > 0) {
            boolean allLiteral = true;
            for (int i = 1; i < arguments.size(); ++i) {
                SQLExpr arg = arguments.get(i);
                if (!(arg instanceof SQLLiteralExpr)) {
                    allLiteral = false;
                }
            }
            if (allLiteral) {
                expr = arguments.get(0);
            }
        }
    }
    Column column = getColumn(expr);
    if (column == null && expr instanceof SQLBinaryOpExpr && valueExprs.length == 1 && valueExprs[0] instanceof SQLLiteralExpr) {
        SQLBinaryOpExpr left = (SQLBinaryOpExpr) expr;
        SQLLiteralExpr right = (SQLLiteralExpr) valueExprs[0];
        if (left.getRight() instanceof SQLIntegerExpr && right instanceof SQLIntegerExpr) {
            long v0 = ((SQLIntegerExpr) left.getRight()).getNumber().longValue();
            long v1 = ((SQLIntegerExpr) right).getNumber().longValue();
            SQLBinaryOperator op = left.getOperator();
            long v;
            switch(op) {
                case Add:
                    v = v1 - v0;
                    break;
                case Subtract:
                    v = v1 + v0;
                    break;
                default:
                    return;
            }
            handleCondition(left.getLeft(), operator, new SQLIntegerExpr(v));
            return;
        }
    }
    if (column == null) {
        return;
    }
    Condition condition = null;
    for (Condition item : this.getConditions()) {
        if (item.getColumn().equals(column) && item.getOperator().equals(operator)) {
            condition = item;
            break;
        }
    }
    if (condition == null) {
        condition = new Condition(column, operator);
        this.conditions.add(condition);
    }
    for (SQLExpr item : valueExprs) {
        Column valueColumn = getColumn(item);
        if (valueColumn != null) {
            continue;
        }
        Object value;
        if (item instanceof SQLCastExpr) {
            item = ((SQLCastExpr) item).getExpr();
        }
        if (item instanceof SQLMethodInvokeExpr || item instanceof SQLCurrentTimeExpr) {
            value = item.toString();
        } else {
            value = SQLEvalVisitorUtils.eval(dbType, item, parameters, false);
            if (value == SQLEvalVisitor.EVAL_VALUE_NULL) {
                value = null;
            }
        }
        condition.addValue(value);
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) Column(com.alibaba.druid.stat.TableStat.Column) SchemaObject(com.alibaba.druid.sql.repository.SchemaObject)

Example 23 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project druid by alibaba.

the class ExportConditions method evaluate.

public String evaluate(String sql, String dbTypeName, Boolean compactValues) {
    DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>();
        List<Condition> conditions = visitor.getConditions();
        for (int i = 0; i < conditions.size(); ++i) {
            TableStat.Condition condition = conditions.get(i);
            Column column = condition.getColumn();
            String operator = condition.getOperator();
            List<Object> values = condition.getValues();
            List<Object> row = new ArrayList<Object>();
            row.add(column.getTable());
            row.add(column.getName());
            row.add(operator);
            if (values.size() == 0) {
                row.add(null);
            } else if (values.size() == 1) {
                if (compactValues != null && compactValues.booleanValue()) {
                    row.add(values);
                } else {
                    row.add(values.get(0));
                }
            } else {
                row.add(values);
            }
            rows.add(row);
        }
        return JSONUtils.toJSONString(rows);
    } catch (Exception ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) Column(com.alibaba.druid.stat.TableStat.Column) Condition(com.alibaba.druid.stat.TableStat.Condition) List(java.util.List) ArrayList(java.util.ArrayList) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 24 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method visit.

@Override
public boolean visit(SQLBetweenExpr x) {
    String begin = null;
    if (x.beginExpr instanceof SQLCharExpr) {
        begin = (String) ((SQLCharExpr) x.beginExpr).getValue();
    } else {
        begin = x.beginExpr.toString();
    }
    String end = null;
    if (x.endExpr instanceof SQLCharExpr) {
        end = (String) ((SQLCharExpr) x.endExpr).getValue();
    } else {
        end = x.endExpr.toString();
    }
    Column column = getColumn(x);
    if (column == null) {
        return true;
    }
    Condition condition = null;
    for (Condition item : this.getConditions()) {
        if (item.getColumn().equals(column) && item.getOperator().equals("between")) {
            condition = item;
            break;
        }
    }
    if (condition == null) {
        condition = new Condition();
        condition.setColumn(column);
        condition.setOperator("between");
        this.conditions.add(condition);
    }
    condition.getValues().add(begin);
    condition.getValues().add(end);
    return true;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Column(com.alibaba.druid.stat.TableStat.Column)

Example 25 with Condition

use of com.alibaba.druid.stat.TableStat.Condition in project Mycat-Server by MyCATApache.

the class MycatSchemaStatVisitor method getConditionsFromWhereUnit.

private void getConditionsFromWhereUnit(WhereUnit whereUnit) {
    List<List<Condition>> retList = new ArrayList<List<Condition>>();
    // or语句外层的条件:如where condition1 and (condition2 or condition3),condition1就会在外层条件中,因为之前提取
    List<Condition> outSideCondition = new ArrayList<Condition>();
    // stashOutSideConditions();
    outSideCondition.addAll(conditions);
    this.conditions.clear();
    for (SQLExpr sqlExpr : whereUnit.getSplitedExprList()) {
        sqlExpr.accept(this);
        // List<Condition> conditions = new ArrayList<Condition>();
        // conditions.addAll(getConditions()); conditions.addAll(outSideCondition);
        /**
         * 合并两个条件列表的元素为一个条件列表,减少不必要多的条件项</br>
         *
         * @author SvenAugustus
         */
        List<Condition> conditions = mergeSqlConditionList(getConditions(), outSideCondition);
        retList.add(conditions);
        this.conditions.clear();
    }
    whereUnit.setConditionList(retList);
    for (WhereUnit subWhere : whereUnit.getSubWhereUnit()) {
        getConditionsFromWhereUnit(subWhere);
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

Condition (com.alibaba.druid.stat.TableStat.Condition)28 ArrayList (java.util.ArrayList)22 List (java.util.List)16 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 Column (com.alibaba.druid.stat.TableStat.Column)7 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 Method (java.lang.reflect.Method)3 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2