Search in sources :

Example 6 with Column

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

the class Aggregate method getColumnIndex.

private Index getColumnIndex() {
    if (on instanceof ExpressionColumn) {
        ExpressionColumn col = (ExpressionColumn) on;
        Column column = col.getColumn();
        TableFilter filter = col.getTableFilter();
        if (filter != null) {
            Table table = filter.getTable();
            Index index = table.getIndexForColumn(column);
            return index;
        }
    }
    return null;
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) Column(com.wplatform.ddal.dbobject.table.Column) TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Index(com.wplatform.ddal.dbobject.index.Index)

Example 7 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)

Example 8 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 9 with Column

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

the class InsertExecutor method doTranslate.

@Override
protected List<Value> doTranslate(TableNode node, SearchRow row, StatementBuilder buff) {
    String forTable = node.getCompositeObjectName();
    TableMate table = castTableMate(prepared.getTable());
    Column[] columns = table.getColumns();
    return buildInsert(forTable, columns, row, buff);
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 10 with Column

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

the class InsertExecutor method addRow.

@Override
public void addRow(Value[] values) {
    TableMate table = castTableMate(prepared.getTable());
    Row newRow = table.getTemplateRow();
    Column[] columns = prepared.getColumns();
    prepared.setCurrentRowNumber(++rowNumber);
    for (int j = 0, len = columns.length; j < len; j++) {
        Column c = columns[j];
        int index = c.getColumnId();
        try {
            Value v = c.convert(values[j]);
            newRow.setValue(index, v);
        } catch (DbException ex) {
            throw prepared.setRow(ex, rowNumber, Prepared.getSQL(values));
        }
    }
    table.validateConvertUpdateSequence(session, newRow);
    addNewRow(newRow);
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) DbException(com.wplatform.ddal.message.DbException)

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