Search in sources :

Example 46 with Prepared

use of org.h2.test.db.Db.Prepared in project ignite by apache.

the class GridSqlQuerySplitter method split.

/**
 * @param conn Connection.
 * @param prepared Prepared.
 * @param params Parameters.
 * @param collocatedGrpBy Whether the query has collocated GROUP BY keys.
 * @param distributedJoins If distributed joins enabled.
 * @param enforceJoinOrder Enforce join order.
 * @param h2 Indexing.
 * @return Two step query.
 * @throws SQLException If failed.
 * @throws IgniteCheckedException If failed.
 */
public static GridCacheTwoStepQuery split(Connection conn, Prepared prepared, Object[] params, boolean collocatedGrpBy, boolean distributedJoins, boolean enforceJoinOrder, IgniteH2Indexing h2) throws SQLException, IgniteCheckedException {
    if (params == null)
        params = GridCacheSqlQuery.EMPTY_PARAMS;
    // Here we will just do initial query parsing. Do not use optimized
    // subqueries because we do not have unique FROM aliases yet.
    GridSqlQuery qry = parse(prepared, false);
    String originalSql = qry.getSQL();
    // debug("ORIGINAL", originalSql);
    final boolean explain = qry.explain();
    qry.explain(false);
    GridSqlQuerySplitter splitter = new GridSqlQuerySplitter(params, collocatedGrpBy, h2.kernalContext());
    // Normalization will generate unique aliases for all the table filters in FROM.
    // Also it will collect all tables and schemas from the query.
    splitter.normalizeQuery(qry);
    // debug("NORMALIZED", qry.getSQL());
    // Here we will have correct normalized AST with optimized join order.
    // The distributedJoins parameter is ignored because it is not relevant for
    // the REDUCE query optimization.
    qry = parse(optimize(h2, conn, qry.getSQL(), params, false, enforceJoinOrder), true);
    // Do the actual query split. We will update the original query AST, need to be careful.
    splitter.splitQuery(qry);
    // We must have at least one map query.
    assert !F.isEmpty(splitter.mapSqlQrys) : "map";
    // We must have a reduce query.
    assert splitter.rdcSqlQry != null : "rdc";
    // distributed joins at all.
    if (distributedJoins) {
        boolean allCollocated = true;
        for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) {
            Prepared prepared0 = optimize(h2, conn, mapSqlQry.query(), mapSqlQry.parameters(params), true, enforceJoinOrder);
            allCollocated &= isCollocated((Query) prepared0);
            mapSqlQry.query(parse(prepared0, true).getSQL());
        }
        // We do not need distributed joins if all MAP queries are collocated.
        if (allCollocated)
            distributedJoins = false;
    }
    // Setup resulting two step query and return it.
    GridCacheTwoStepQuery twoStepQry = new GridCacheTwoStepQuery(originalSql, splitter.tbls);
    twoStepQry.reduceQuery(splitter.rdcSqlQry);
    for (GridCacheSqlQuery mapSqlQry : splitter.mapSqlQrys) twoStepQry.addMapQuery(mapSqlQry);
    twoStepQry.skipMergeTable(splitter.rdcQrySimple);
    twoStepQry.explain(explain);
    twoStepQry.distributedJoins(distributedJoins);
    // all map queries must have non-empty derivedPartitions to use this feature.
    twoStepQry.derivedPartitions(mergePartitionsFromMultipleQueries(twoStepQry.mapQueries()));
    return twoStepQry;
}
Also used : GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) Query(org.h2.command.dml.Query) Prepared(org.h2.command.Prepared) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)

Example 47 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class ExecuteProcedure method setParameters.

private void setParameters() {
    Prepared prepared = procedure.getPrepared();
    ArrayList<Parameter> params = prepared.getParameters();
    for (int i = 0; params != null && i < params.size() && i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        Parameter p = params.get(i);
        p.setValue(expr.getValue(session));
    }
}
Also used : Expression(org.h2.expression.Expression) Prepared(org.h2.command.Prepared) Parameter(org.h2.expression.Parameter)

Example 48 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class SelectUnion method prepare.

@Override
public void prepare() {
    if (isPrepared) {
        // sometimes a subquery is prepared twice (CREATE TABLE AS SELECT)
        return;
    }
    if (SysProperties.CHECK && !checkInit) {
        DbException.throwInternalError("not initialized");
    }
    isPrepared = true;
    left.prepare();
    right.prepare();
    int len = left.getColumnCount();
    // set the correct expressions now
    expressions = New.arrayList();
    ArrayList<Expression> le = left.getExpressions();
    ArrayList<Expression> re = right.getExpressions();
    ColumnNamer columnNamer = new ColumnNamer(session);
    for (int i = 0; i < len; i++) {
        Expression l = le.get(i);
        Expression r = re.get(i);
        int type = Value.getHigherOrder(l.getType(), r.getType());
        long prec = Math.max(l.getPrecision(), r.getPrecision());
        int scale = Math.max(l.getScale(), r.getScale());
        int displaySize = Math.max(l.getDisplaySize(), r.getDisplaySize());
        String columnName = columnNamer.getColumnName(l, i, l.getAlias());
        Column col = new Column(columnName, type, prec, scale, displaySize);
        Expression e = new ExpressionColumn(session.getDatabase(), col);
        expressions.add(e);
    }
    if (orderList != null) {
        initOrder(session, expressions, null, orderList, getColumnCount(), true, null);
        sort = prepareOrder(orderList, expressions.size());
        orderList = null;
    }
    expressionArray = expressions.toArray(new Expression[0]);
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 49 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class Session method prepareLocal.

/**
 * Parse and prepare the given SQL statement.
 * This method also checks if the connection has been closed.
 *
 * @param sql the SQL statement
 * @return the prepared statement
 */
public Command prepareLocal(String sql) {
    if (closed) {
        throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "session closed");
    }
    Command command;
    if (queryCacheSize > 0) {
        if (queryCache == null) {
            queryCache = SmallLRUCache.newInstance(queryCacheSize);
            modificationMetaID = database.getModificationMetaId();
        } else {
            long newModificationMetaID = database.getModificationMetaId();
            if (newModificationMetaID != modificationMetaID) {
                queryCache.clear();
                modificationMetaID = newModificationMetaID;
            }
            command = queryCache.get(sql);
            if (command != null && command.canReuse()) {
                command.reuse();
                return command;
            }
        }
    }
    Parser parser = new Parser(this);
    try {
        command = parser.prepareCommand(sql);
    } finally {
        // we can't reuse sub-query indexes, so just drop the whole cache
        subQueryIndexCache = null;
    }
    command.prepareJoinBatch();
    if (queryCache != null) {
        if (command.isCacheable()) {
            queryCache.put(sql, command);
        }
    }
    return command;
}
Also used : Command(org.h2.command.Command) Parser(org.h2.command.Parser)

Example 50 with Prepared

use of org.h2.test.db.Db.Prepared in project h2database by h2database.

the class Session method prepare.

/**
 * Parse and prepare the given SQL statement.
 *
 * @param sql the SQL statement
 * @param rightsChecked true if the rights have already been checked
 * @param literalsChecked true if the sql string has already been checked
 *            for literals (only used if ALLOW_LITERALS NONE is set).
 * @return the prepared statement
 */
public Prepared prepare(String sql, boolean rightsChecked, boolean literalsChecked) {
    Parser parser = new Parser(this);
    parser.setRightsChecked(rightsChecked);
    parser.setLiteralsChecked(literalsChecked);
    return parser.prepare(sql);
}
Also used : Parser(org.h2.command.Parser)

Aggregations

Prepared (org.h2.command.Prepared)32 ValueString (org.h2.value.ValueString)16 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)11 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)11 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)10 Parameter (org.h2.expression.Parameter)10 DbException (org.h2.message.DbException)10 PreparedStatement (java.sql.PreparedStatement)9 Value (org.h2.value.Value)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 Query (org.h2.command.dml.Query)7 Expression (org.h2.expression.Expression)7 Column (org.h2.table.Column)7 Connection (java.sql.Connection)6 IndexColumn (org.h2.table.IndexColumn)6 ResultSet (java.sql.ResultSet)5 SQLClientInfoException (java.sql.SQLClientInfoException)5 Savepoint (java.sql.Savepoint)5