Search in sources :

Example 6 with TableFilter

use of com.wplatform.ddal.dbobject.table.TableFilter in project jdbc-shards by wplatform.

the class Optimizer method calculateGenetic.

private void calculateGenetic() {
    TableFilter[] best = new TableFilter[filters.length];
    TableFilter[] list = new TableFilter[filters.length];
    for (int x = 0; x < MAX_GENETIC; x++) {
        if (canStop(x)) {
            break;
        }
        boolean generateRandom = (x & 127) == 0;
        if (!generateRandom) {
            System.arraycopy(best, 0, list, 0, filters.length);
            if (!shuffleTwo(list)) {
                generateRandom = true;
            }
        }
        if (generateRandom) {
            switched = new BitField();
            System.arraycopy(filters, 0, best, 0, filters.length);
            shuffleAll(best);
            System.arraycopy(best, 0, list, 0, filters.length);
        }
        if (testPlan(list)) {
            switched = new BitField();
            System.arraycopy(list, 0, best, 0, filters.length);
        }
    }
}
Also used : BitField(com.wplatform.ddal.util.BitField) TableFilter(com.wplatform.ddal.dbobject.table.TableFilter)

Example 7 with TableFilter

use of com.wplatform.ddal.dbobject.table.TableFilter in project jdbc-shards by wplatform.

the class Optimizer method optimize.

/**
     * Calculate the best query plan to use.
     */
void optimize() {
    calculateBestPlan();
    bestPlan.removeUnusableIndexConditions();
    TableFilter[] f2 = bestPlan.getFilters();
    topFilter = f2[0];
    for (int i = 0; i < f2.length - 1; i++) {
        f2[i].addJoin(f2[i + 1], false, false, null);
    }
    for (TableFilter f : f2) {
        PlanItem item = bestPlan.getItem(f);
        f.setPlanItem(item);
    }
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) PlanItem(com.wplatform.ddal.dbobject.table.PlanItem)

Example 8 with TableFilter

use of com.wplatform.ddal.dbobject.table.TableFilter in project jdbc-shards by wplatform.

the class Optimizer method shuffleTwo.

private boolean shuffleTwo(TableFilter[] f) {
    int a = 0, b = 0, i = 0;
    for (; i < 20; i++) {
        a = random.nextInt(f.length);
        b = random.nextInt(f.length);
        if (a == b) {
            continue;
        }
        if (a < b) {
            int temp = a;
            a = b;
            b = temp;
        }
        int s = a * f.length + b;
        if (switched.get(s)) {
            continue;
        }
        switched.set(s);
        break;
    }
    if (i == 20) {
        return false;
    }
    TableFilter temp = f[a];
    f[a] = f[b];
    f[b] = temp;
    return true;
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter)

Example 9 with TableFilter

use of com.wplatform.ddal.dbobject.table.TableFilter in project jdbc-shards by wplatform.

the class Query method initOrder.

/**
     * Initialize the order by list. This call may extend the expressions list.
     *
     * @param session        the session
     * @param expressions    the select list expressions
     * @param expressionSQL  the select list SQL snippets
     * @param orderList      the order by list
     * @param visible        the number of visible columns in the select list
     * @param mustBeInResult all order by expressions must be in the select list
     * @param filters        the table filters
     */
static void initOrder(Session session, ArrayList<Expression> expressions, ArrayList<String> expressionSQL, ArrayList<SelectOrderBy> orderList, int visible, boolean mustBeInResult, ArrayList<TableFilter> filters) {
    Database db = session.getDatabase();
    for (SelectOrderBy o : orderList) {
        Expression e = o.expression;
        if (e == null) {
            continue;
        }
        // special case: SELECT 1 AS A FROM DUAL ORDER BY A
        // (oracle supports it, but only in order by, not in group by and
        // not in having):
        // SELECT 1 AS A FROM DUAL ORDER BY -A
        boolean isAlias = false;
        int idx = expressions.size();
        if (e instanceof ExpressionColumn) {
            // order by expression
            ExpressionColumn exprCol = (ExpressionColumn) e;
            String tableAlias = exprCol.getOriginalTableAliasName();
            String col = exprCol.getOriginalColumnName();
            for (int j = 0; j < visible; j++) {
                boolean found = false;
                Expression ec = expressions.get(j);
                if (ec instanceof ExpressionColumn) {
                    // select expression
                    ExpressionColumn c = (ExpressionColumn) ec;
                    found = db.equalsIdentifiers(col, c.getColumnName());
                    if (found && tableAlias != null) {
                        String ca = c.getOriginalTableAliasName();
                        if (ca == null) {
                            found = false;
                            if (filters != null) {
                                // select id from test order by test.id
                                for (int i = 0, size = filters.size(); i < size; i++) {
                                    TableFilter f = filters.get(i);
                                    if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) {
                                        found = true;
                                        break;
                                    }
                                }
                            }
                        } else {
                            found = db.equalsIdentifiers(ca, tableAlias);
                        }
                    }
                } else if (!(ec instanceof Alias)) {
                    continue;
                } else if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
                    found = true;
                } else {
                    Expression ec2 = ec.getNonAliasExpression();
                    if (ec2 instanceof ExpressionColumn) {
                        ExpressionColumn c2 = (ExpressionColumn) ec2;
                        String ta = exprCol.getSQL();
                        String tb = c2.getSQL();
                        String s2 = c2.getColumnName();
                        found = db.equalsIdentifiers(col, s2);
                        if (!db.equalsIdentifiers(ta, tb)) {
                            found = false;
                        }
                    }
                }
                if (found) {
                    idx = j;
                    isAlias = true;
                    break;
                }
            }
        } else {
            String s = e.getSQL();
            if (expressionSQL != null) {
                for (int j = 0, size = expressionSQL.size(); j < size; j++) {
                    String s2 = expressionSQL.get(j);
                    if (db.equalsIdentifiers(s2, s)) {
                        idx = j;
                        isAlias = true;
                        break;
                    }
                }
            }
        }
        if (!isAlias) {
            if (mustBeInResult) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL());
            }
            expressions.add(e);
            String sql = e.getSQL();
            expressionSQL.add(sql);
        }
        o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
        Expression expr = expressions.get(idx).getNonAliasExpression();
        o.expression = expr;
    }
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Database(com.wplatform.ddal.engine.Database)

Example 10 with TableFilter

use of com.wplatform.ddal.dbobject.table.TableFilter in project jdbc-shards by wplatform.

the class ExpressionColumn method createIndexConditions.

@Override
public void createIndexConditions(Session session, TableFilter filter) {
    TableFilter tf = getTableFilter();
    if (filter == tf && column.getType() == Value.BOOLEAN) {
        IndexCondition cond = IndexCondition.get(Comparison.EQUAL, this, ValueExpression.get(ValueBoolean.get(true)));
        filter.addIndexCondition(cond);
    }
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) IndexCondition(com.wplatform.ddal.dbobject.index.IndexCondition)

Aggregations

TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)13 Expression (com.wplatform.ddal.command.expression.Expression)4 Column (com.wplatform.ddal.dbobject.table.Column)3 Value (com.wplatform.ddal.value.Value)3 TableMate (com.wplatform.ddal.dbobject.table.TableMate)2 Index (com.wplatform.ddal.dbobject.index.Index)1 IndexCondition (com.wplatform.ddal.dbobject.index.IndexCondition)1 Plan (com.wplatform.ddal.dbobject.table.Plan)1 PlanItem (com.wplatform.ddal.dbobject.table.PlanItem)1 Table (com.wplatform.ddal.dbobject.table.Table)1 Database (com.wplatform.ddal.engine.Database)1 DbException (com.wplatform.ddal.message.DbException)1 Row (com.wplatform.ddal.result.Row)1 SearchRow (com.wplatform.ddal.result.SearchRow)1 BitField (com.wplatform.ddal.util.BitField)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1