Search in sources :

Example 1 with Column

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

the class RoutingHandlerImpl method exportRangeArg.

/**
 * @param table
 * @param first
 * @param last
 * @param routingArgs
 */
private void exportRangeArg(TableMate table, SearchRow first, SearchRow last, Map<String, List<Value>> routingArgs) {
    TableRouter tr = table.getTableRouter();
    List<RuleColumn> ruleCols = tr.getRuleColumns();
    if (first != null && last != null) {
        for (int i = 0; first != null && i < first.getColumnCount(); i++) {
            Value firstV = first.getValue(i);
            Value listV = last.getValue(i);
            if (firstV == null || listV == null || firstV == ValueNull.INSTANCE || listV == ValueNull.INSTANCE) {
                continue;
            }
            Column col = table.getColumn(i);
            String colName = col.getName();
            RuleColumn matched = null;
            for (RuleColumn ruleColumn : ruleCols) {
                if (colName.equalsIgnoreCase(ruleColumn.getName())) {
                    matched = ruleColumn;
                }
            }
            if (matched == null) {
                continue;
            }
            List<Value> values = routingArgs.get(matched.getName());
            if (values == null) {
                values = New.arrayList();
                routingArgs.put(matched.getName(), values);
            }
            int compare = database.compare(firstV, listV);
            if (compare == 0) {
                values.add(firstV);
            } else if (compare < 0) {
                List<Value> enumValue = enumRange(firstV, listV);
                if (enumValue != null) {
                    values.addAll(enumValue);
                }
            } else {
                throw new TableRoutingException(table.getName() + " routing error. The conidition " + matched.getName() + " is alwarys false.");
            }
        }
    }
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) List(java.util.List)

Example 2 with Column

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

the class RoutingHandlerImpl method getRuleColumnArgs.

private Map<String, List<Value>> getRuleColumnArgs(TableMate table, SearchRow row) {
    Map<String, List<Value>> args = New.hashMap();
    TableRouter tableRouter = table.getTableRouter();
    for (RuleColumn ruleCol : tableRouter.getRuleColumns()) {
        Column[] columns = table.getColumns();
        Column matched = null;
        for (Column column : columns) {
            String colName = column.getName();
            if (colName.equalsIgnoreCase(ruleCol.getName())) {
                matched = column;
                break;
            }
        }
        if (matched == null) {
            throw DbException.getInvalidValueException("RuleColumn", ruleCol);
        }
        Value value = row.getValue(matched.getColumnId());
        if (value != null && value != ValueNull.INSTANCE) {
            List<Value> values = New.arrayList(1);
            values.add(value);
            args.put(ruleCol.getName(), values);
        }
    }
    return args;
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) List(java.util.List)

Example 3 with Column

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

the class IndexMate method getCostRangeIndex.

/**
 * Calculate the cost for the given mask as if this index was a typical
 * b-tree range index. This is the estimated cost required to search one
 * row, and then iterate over the given number of rows.
 *
 * @param masks     the search mask
 * @param rowCount  the number of rows in the index
 * @param filter    the table filter
 * @param sortOrder the sort order
 * @return the estimated cost
 */
protected long getCostRangeIndex(int[] masks, long rowCount, TableFilter filter, SortOrder sortOrder) {
    rowCount += Constants.COST_ROW_OFFSET;
    long cost = rowCount;
    long rows = rowCount;
    int totalSelectivity = 0;
    if (masks == null) {
        return cost;
    }
    for (int i = 0, len = columns.length; i < len; i++) {
        Column column = columns[i];
        int index = column.getColumnId();
        int mask = masks[index];
        if ((mask & IndexCondition.EQUALITY) == IndexCondition.EQUALITY) {
            if (i == columns.length - 1 && getIndexType().isUnique()) {
                cost = 3;
                break;
            }
            totalSelectivity = 100 - ((100 - totalSelectivity) * (100 - column.getSelectivity()) / 100);
            long distinctRows = rowCount * totalSelectivity / 100;
            if (distinctRows <= 0) {
                distinctRows = 1;
            }
            rows = Math.max(rowCount / distinctRows, 1);
            cost = 2 + rows;
        } else if ((mask & IndexCondition.RANGE) == IndexCondition.RANGE) {
            cost = 2 + rows / 4;
            break;
        } else if ((mask & IndexCondition.START) == IndexCondition.START) {
            cost = 2 + rows / 3;
            break;
        } else if ((mask & IndexCondition.END) == IndexCondition.END) {
            cost = rows / 3;
            break;
        } else {
            break;
        }
    }
    // it will be cheaper than another index, so adjust the cost accordingly
    if (sortOrder != null) {
        boolean sortOrderMatches = true;
        int coveringCount = 0;
        int[] sortTypes = sortOrder.getSortTypes();
        for (int i = 0, len = sortTypes.length; i < len; i++) {
            if (i >= indexColumns.length) {
                // more of the order by columns
                break;
            }
            Column col = sortOrder.getColumn(i, filter);
            if (col == null) {
                sortOrderMatches = false;
                break;
            }
            IndexColumn indexCol = indexColumns[i];
            if (col != indexCol.column) {
                sortOrderMatches = false;
                break;
            }
            int sortType = sortTypes[i];
            if (sortType != indexCol.sortType) {
                sortOrderMatches = false;
                break;
            }
            coveringCount++;
        }
        if (sortOrderMatches) {
            // "coveringCount" makes sure that when we have two
            // or more covering indexes, we choose the one
            // that covers more
            cost -= coveringCount;
        }
    }
    return cost;
}
Also used : IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn) Column(com.wplatform.ddal.dbobject.table.Column) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn)

Example 4 with Column

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

the class TableFunction method getTable.

private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
    int len = columnList.length;
    Expression[] header = new Expression[len];
    Database db = session.getDatabase();
    for (int i = 0; i < len; i++) {
        Column c = columnList[i];
        ExpressionColumn col = new ExpressionColumn(db, c);
        header[i] = col;
    }
    LocalResult result = new LocalResult(session, header, len);
    if (distinctRows) {
        result.setDistinct();
    }
    if (!onlyColumnList) {
        Value[][] list = new Value[len][];
        int rows = 0;
        for (int i = 0; i < len; i++) {
            Value v = argList[i].getValue(session);
            if (v == ValueNull.INSTANCE) {
                list[i] = new Value[0];
            } else {
                ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
                Value[] l = array.getList();
                list[i] = l;
                rows = Math.max(rows, l.length);
            }
        }
        for (int row = 0; row < rows; row++) {
            Value[] r = new Value[len];
            for (int j = 0; j < len; j++) {
                Value[] l = list[j];
                Value v;
                if (l.length <= row) {
                    v = ValueNull.INSTANCE;
                } else {
                    Column c = columnList[j];
                    v = l[row];
                    v = c.convert(v);
                    v = v.convertPrecision(c.getPrecision(), false);
                    v = v.convertScale(true, c.getScale());
                }
                r[j] = v;
            }
            result.addRow(r);
        }
    }
    result.done();
    ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
    return vr;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Column(com.wplatform.ddal.dbobject.table.Column) Database(com.wplatform.ddal.engine.Database)

Example 5 with Column

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

the class Insert method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(")\n");
    if (insertFromSelect) {
        buff.append("DIRECT ");
    }
    if (sortedInsertMode) {
        buff.append("SORTED ");
    }
    if (list.size() > 0) {
        buff.append("VALUES ");
        int row = 0;
        if (list.size() > 1) {
            buff.append('\n');
        }
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(",\n");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Aggregations

Column (com.wplatform.ddal.dbobject.table.Column)38 Expression (com.wplatform.ddal.command.expression.Expression)15 Value (com.wplatform.ddal.value.Value)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)12 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)8 DbException (com.wplatform.ddal.message.DbException)7 Row (com.wplatform.ddal.result.Row)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Query (com.wplatform.ddal.command.dml.Query)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Index (com.wplatform.ddal.dbobject.index.Index)3 Table (com.wplatform.ddal.dbobject.table.Table)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Database (com.wplatform.ddal.engine.Database)3 List (java.util.List)3 Prepared (com.wplatform.ddal.command.Prepared)2