Search in sources :

Example 1 with Prepared

use of org.h2.command.Prepared in project ignite by apache.

the class GridQueryParsingTest method checkQuery.

/**
     * @param qry Query.
     */
private void checkQuery(String qry) throws Exception {
    Prepared prepared = parse(qry);
    GridSqlStatement gQry = new GridSqlQueryParser(false).parse(prepared);
    String res = gQry.getSQL();
    System.out.println(normalizeSql(res));
    assertSqlEquals(U.firstNotNull(prepared.getPlanSQL(), prepared.getSQL()), res);
}
Also used : Prepared(org.h2.command.Prepared)

Example 2 with Prepared

use of org.h2.command.Prepared in project ignite by apache.

the class GridQueryParsingTest method checkCreateTable.

/**
     * @param qry Query.
     */
private void checkCreateTable(String qry) throws Exception {
    Prepared prepared = parse(qry);
    assertTrue(prepared instanceof CreateTable);
    GridSqlStatement gridStmt = new GridSqlQueryParser(false).parse(prepared);
    String res = createTableToSql((GridSqlCreateTable) gridStmt);
    System.out.println(normalizeSql(res));
    assertSqlEquals(U.firstNotNull(prepared.getPlanSQL(), prepared.getSQL()), res);
}
Also used : Prepared(org.h2.command.Prepared) CreateTable(org.h2.command.ddl.CreateTable)

Example 3 with Prepared

use of org.h2.command.Prepared in project ignite by apache.

the class GridQueryParsingTest method testParseTableFilter.

/**
     * Query AST transformation heavily depends on this behavior.
     *
     * @throws Exception If failed.
     */
public void testParseTableFilter() throws Exception {
    Prepared prepared = parse("select Person.old, p1.old, p1.addrId from Person, Person p1 " + "where exists(select 1 from sch2.Address a where a.id = p1.addrId)");
    GridSqlSelect select = (GridSqlSelect) new GridSqlQueryParser(false).parse(prepared);
    GridSqlJoin join = (GridSqlJoin) select.from();
    GridSqlTable tbl1 = (GridSqlTable) join.leftTable();
    GridSqlAlias tbl2Alias = (GridSqlAlias) join.rightTable();
    GridSqlTable tbl2 = tbl2Alias.child();
    // Must be distinct objects, even if it is the same table.
    assertNotSame(tbl1, tbl2);
    assertNotNull(tbl1.dataTable());
    assertNotNull(tbl2.dataTable());
    assertSame(tbl1.dataTable(), tbl2.dataTable());
    GridSqlColumn col1 = (GridSqlColumn) select.column(0);
    GridSqlColumn col2 = (GridSqlColumn) select.column(1);
    assertSame(tbl1, col1.expressionInFrom());
    // Alias in FROM must be included in column.
    assertSame(tbl2Alias, col2.expressionInFrom());
    // In EXISTS we must correctly reference the column from the outer query.
    GridSqlAst exists = select.where();
    GridSqlSubquery subqry = exists.child();
    GridSqlSelect subSelect = subqry.child();
    GridSqlColumn p1AddrIdCol = (GridSqlColumn) select.column(2);
    assertEquals("ADDRID", p1AddrIdCol.column().getName());
    assertSame(tbl2Alias, p1AddrIdCol.expressionInFrom());
    GridSqlColumn p1AddrIdColExists = subSelect.where().child(1);
    assertEquals("ADDRID", p1AddrIdCol.column().getName());
    assertSame(tbl2Alias, p1AddrIdColExists.expressionInFrom());
}
Also used : Prepared(org.h2.command.Prepared)

Example 4 with Prepared

use of org.h2.command.Prepared in project ignite by apache.

the class GridSqlQuerySplitter method split.

/**
     * @param stmt Prepared statement.
     * @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(JdbcPreparedStatement stmt, 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(stmt), false);
    String originalSql = qry.getSQL();
    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);
    Connection conn = stmt.getConnection();
    // 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 prepared = optimize(h2, conn, mapSqlQry.query(), mapSqlQry.parameters(params), true, enforceJoinOrder);
            allCollocated &= isCollocated((Query) prepared);
            mapSqlQry.query(parse(prepared, 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) Connection(java.sql.Connection) Prepared(org.h2.command.Prepared) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery)

Example 5 with Prepared

use of org.h2.command.Prepared in project ignite by apache.

the class DmlStatementsProcessor method getPlanForStatement.

/**
     * Generate SELECT statements to retrieve data for modifications from and find fast UPDATE or DELETE args,
     * if available.
     *
     * @param schema Schema.
     * @param prepStmt JDBC statement.
     * @return Update plan.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private UpdatePlan getPlanForStatement(String schema, PreparedStatement prepStmt, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    Prepared p = GridSqlQueryParser.prepared(prepStmt);
    H2DmlPlanKey planKey = new H2DmlPlanKey(schema, p.getSQL());
    UpdatePlan res = (errKeysPos == null ? planCache.get(planKey) : null);
    if (res != null)
        return res;
    res = UpdatePlanBuilder.planForStatement(p, errKeysPos);
    // Don't cache re-runs
    if (errKeysPos == null)
        return U.firstNotNull(planCache.putIfAbsent(planKey, res), res);
    else
        return res;
}
Also used : Prepared(org.h2.command.Prepared) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Aggregations

Prepared (org.h2.command.Prepared)12 PreparedStatement (java.sql.PreparedStatement)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 JdbcSqlFieldsQuery (org.apache.ignite.internal.jdbc2.JdbcSqlFieldsQuery)2 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)2 GridCacheTwoStepQuery (org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery)2 GridQueryFieldMetadata (org.apache.ignite.internal.processors.query.GridQueryFieldMetadata)2 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)2 GridH2QueryContext (org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)2 ResultSet (java.sql.ResultSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 UUID (java.util.UUID)1