Search in sources :

Example 21 with Plan

use of org.h2.table.Plan in project ignite by apache.

the class DmlStatementsProcessor method streamUpdateQuery.

/**
 * Perform given statement against given data streamer. Only rows based INSERT is supported.
 *
 * @param schemaName Schema name.
 * @param streamer Streamer to feed data to.
 * @param stmt Statement.
 * @param args Statement arguments.
 * @return Number of rows in given INSERT statement.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(String schemaName, IgniteDataStreamer streamer, PreparedStatement stmt, final Object[] args) throws IgniteCheckedException {
    idx.checkStatementStreamable(stmt);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    assert p != null;
    final UpdatePlan plan = getPlanForStatement(schemaName, null, p, null, true, null);
    assert plan.isLocalSubquery();
    final GridCacheContext cctx = plan.cacheContext();
    QueryCursorImpl<List<?>> cur;
    final ArrayList<List<?>> data = new ArrayList<>(plan.rowCount());
    QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            try {
                Iterator<List<?>> it;
                if (!F.isEmpty(plan.selectQuery())) {
                    GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQuery(), F.asList(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)), null, false, 0, null);
                    it = res.iterator();
                } else
                    it = plan.createRows(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)).iterator();
                return new GridQueryCacheObjectsIterator(it, idx.objectContext(), cctx.keepBinary());
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }
    }, null);
    data.addAll(stepCur.getAll());
    cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            return data.iterator();
        }
    }, null);
    if (plan.rowCount() == 1) {
        IgniteBiTuple t = plan.processRow(cur.iterator().next());
        streamer.addData(t.getKey(), t.getValue());
        return 1;
    }
    Map<Object, Object> rows = new LinkedHashMap<>(plan.rowCount());
    for (List<?> row : cur) {
        final IgniteBiTuple t = plan.processRow(row);
        rows.put(t.getKey(), t.getValue());
    }
    streamer.addData(rows);
    return rows.size();
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 22 with Plan

use of org.h2.table.Plan in project h2database by h2database.

the class MergeUsing method getPlanSQL.

// Use the regular merge syntax as our plan SQL
@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(targetTable.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (keys != null) {
        buff.append(" KEY(");
        buff.resetCount();
        for (Column c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    }
    buff.append('\n');
    if (!valuesExpressionList.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : valuesExpressionList) {
            if (row++ > 0) {
                buff.append(", ");
            }
            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(org.h2.table.Column) Expression(org.h2.expression.Expression) StatementBuilder(org.h2.util.StatementBuilder)

Example 23 with Plan

use of org.h2.table.Plan in project h2database by h2database.

the class Optimizer method calculateBruteForceSome.

private void calculateBruteForceSome() {
    int bruteForce = getMaxBruteForceFilters(filters.length);
    TableFilter[] list = new TableFilter[filters.length];
    Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce);
    for (int x = 0; !canStop(x) && p.next(); x++) {
        // find out what filters are not used yet
        for (TableFilter f : filters) {
            f.setUsed(false);
        }
        for (int i = 0; i < bruteForce; i++) {
            list[i].setUsed(true);
        }
        // fill the remaining elements with the unused elements (greedy)
        for (int i = bruteForce; i < filters.length; i++) {
            double costPart = -1.0;
            int bestPart = -1;
            for (int j = 0; j < filters.length; j++) {
                if (!filters[j].isUsed()) {
                    if (i == filters.length - 1) {
                        bestPart = j;
                        break;
                    }
                    list[i] = filters[j];
                    Plan part = new Plan(list, i + 1, condition);
                    double costNow = part.calculateCost(session);
                    if (costPart < 0 || costNow < costPart) {
                        costPart = costNow;
                        bestPart = j;
                    }
                }
            }
            filters[bestPart].setUsed(true);
            list[i] = filters[bestPart];
        }
        testPlan(list);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) Plan(org.h2.table.Plan)

Example 24 with Plan

use of org.h2.table.Plan in project h2database by h2database.

the class Optimizer method optimize.

/**
 * Calculate the best query plan to use.
 *
 * @param parse If we do not need to really get the best plan because it is
 *            a view parsing stage.
 */
void optimize(boolean parse) {
    if (parse) {
        calculateFakePlan();
    } else {
        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, null);
    }
    if (parse) {
        return;
    }
    for (TableFilter f : f2) {
        PlanItem item = bestPlan.getItem(f);
        f.setPlanItem(item);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Example 25 with Plan

use of org.h2.table.Plan in project h2database by h2database.

the class Optimizer method calculateFakePlan.

private void calculateFakePlan() {
    cost = -1;
    bestPlan = new Plan(filters, filters.length, condition);
}
Also used : Plan(org.h2.table.Plan)

Aggregations

PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)14 Connection (java.sql.Connection)13 Statement (java.sql.Statement)13 SimpleResultSet (org.h2.tools.SimpleResultSet)9 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 Column (org.h2.table.Column)7 StatementBuilder (org.h2.util.StatementBuilder)6 ValueString (org.h2.value.ValueString)6 ArrayList (java.util.ArrayList)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteException (org.apache.ignite.IgniteException)4 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 Prepared (org.h2.command.Prepared)4 Expression (org.h2.expression.Expression)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 GridQueryCacheObjectsIterator (org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator)3 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)3 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)3