Search in sources :

Example 1 with H2Utils.session

use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.

the class GridReduceQueryExecutor method createMergeTable.

/**
 * @param conn Connection.
 * @param qry Query.
 * @param explain Explain.
 * @return Table.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
private ReduceTable createMergeTable(H2PooledConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
    try {
        Session ses = H2Utils.session(conn);
        CreateTableData data = new CreateTableData();
        data.tableName = "T___";
        data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
        data.create = true;
        if (!explain) {
            LinkedHashMap<String, ?> colsMap = qry.columns();
            assert colsMap != null;
            ArrayList<Column> cols = new ArrayList<>(colsMap.size());
            for (Map.Entry<String, ?> e : colsMap.entrySet()) {
                String alias = e.getKey();
                GridSqlType type = (GridSqlType) e.getValue();
                assert !F.isEmpty(alias);
                Column col0;
                if (type == GridSqlType.UNKNOWN) {
                    // Special case for parameter being set at the top of the query (e.g. SELECT ? FROM ...).
                    // Re-map it to STRING in the same way it is done in H2, because any argument can be cast
                    // to string.
                    col0 = new Column(alias, Value.STRING);
                } else {
                    col0 = new Column(alias, type.type(), type.precision(), type.scale(), type.displaySize());
                }
                cols.add(col0);
            }
            data.columns = cols;
        } else
            data.columns = planColumns();
        boolean sortedIndex = !F.isEmpty(qry.sortColumns());
        ReduceTable tbl = new ReduceTable(data);
        ArrayList<Index> idxs = new ArrayList<>(2);
        if (explain) {
            idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, sortedIndex ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
        } else if (sortedIndex) {
            List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
            SortedReduceIndexAdapter sortedMergeIdx = new SortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
            idxs.add(ReduceTable.createScanIndex(sortedMergeIdx));
            idxs.add(sortedMergeIdx);
        } else
            idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_UNSORTED));
        tbl.indexes(idxs);
        return tbl;
    } catch (Exception e) {
        throw new IgniteCheckedException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Index(org.h2.index.Index) CreateTableData(org.h2.command.ddl.CreateTableData) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) IgniteTxAlreadyCompletedCheckedException(org.apache.ignite.internal.transactions.IgniteTxAlreadyCompletedCheckedException) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) SQLException(java.sql.SQLException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridSqlSortColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSortColumn) Column(org.h2.table.Column) GridSqlSortColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSortColumn) GridSqlType(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonMap(java.util.Collections.singletonMap) Session(org.h2.engine.Session)

Example 2 with H2Utils.session

use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project gridgain by gridgain.

the class GridQueryParsingTest method parse.

/**
 * @param sql Sql.
 */
@SuppressWarnings("unchecked")
private <T extends Prepared> T parse(String sql) throws Exception {
    try (H2PooledConnection conn = connection()) {
        Session ses = H2Utils.session(conn);
        ses.setQueryContext(QueryContext.parseContext(null, true));
        return (T) ses.prepare(sql);
    }
}
Also used : H2PooledConnection(org.apache.ignite.internal.processors.query.h2.H2PooledConnection) Session(org.gridgain.internal.h2.engine.Session)

Example 3 with H2Utils.session

use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.

the class GridQueryParsingTest method parse.

/**
 * @param sql Sql.
 */
@SuppressWarnings("unchecked")
private <T extends Prepared> T parse(String sql) throws Exception {
    try (H2PooledConnection conn = connection()) {
        Session ses = H2Utils.session(conn);
        H2Utils.setupConnection(conn, QueryContext.parseContext(null, true), false, false, false);
        return (T) ses.prepare(sql);
    }
}
Also used : H2PooledConnection(org.apache.ignite.internal.processors.query.h2.H2PooledConnection) Session(org.h2.engine.Session)

Example 4 with H2Utils.session

use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.

the class IgniteH2Indexing method executeSqlQuery.

/**
 * Executes sql query statement.
 *
 * @param conn Connection,.
 * @param stmt Statement.
 * @param timeoutMillis Query timeout.
 * @param cancel Query cancel.
 * @return Result.
 * @throws IgniteCheckedException If failed.
 */
private ResultSet executeSqlQuery(final Connection conn, final PreparedStatement stmt, int timeoutMillis, @Nullable GridQueryCancel cancel) throws IgniteCheckedException {
    final MapQueryLazyWorker lazyWorker = MapQueryLazyWorker.currentWorker();
    if (cancel != null) {
        cancel.set(new Runnable() {

            @Override
            public void run() {
                if (lazyWorker != null) {
                    lazyWorker.submit(new Runnable() {

                        @Override
                        public void run() {
                            cancelStatement(stmt);
                        }
                    });
                } else
                    cancelStatement(stmt);
            }
        });
    }
    Session ses = H2Utils.session(conn);
    if (timeoutMillis > 0)
        ses.setQueryTimeout(timeoutMillis);
    if (lazyWorker != null)
        ses.setLazyQueryExecution(true);
    try {
        return stmt.executeQuery();
    } catch (SQLException e) {
        // Throw special exception.
        if (e.getErrorCode() == ErrorCode.STATEMENT_WAS_CANCELED)
            throw new QueryCancelledException();
        throw new IgniteCheckedException("Failed to execute SQL query. " + e.getMessage(), e);
    } finally {
        if (timeoutMillis > 0)
            ses.setQueryTimeout(0);
        if (lazyWorker != null)
            ses.setLazyQueryExecution(false);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) MapQueryLazyWorker(org.apache.ignite.internal.processors.query.h2.twostep.MapQueryLazyWorker) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) Session(org.h2.engine.Session)

Example 5 with H2Utils.session

use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project gridgain by gridgain.

the class GridReduceQueryExecutor method createMergeTable.

/**
 * @param conn Connection.
 * @param qry Query.
 * @param explain Explain.
 * @return Table.
 * @throws IgniteCheckedException If failed.
 */
private ReduceTable createMergeTable(H2PooledConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
    try {
        Session ses = H2Utils.session(conn);
        CreateTableData data = new CreateTableData();
        data.tableName = "T___";
        data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
        data.create = true;
        if (!explain) {
            LinkedHashMap<String, ?> colsMap = qry.columns();
            assert colsMap != null;
            ArrayList<Column> cols = new ArrayList<>(colsMap.size());
            for (Map.Entry<String, ?> e : colsMap.entrySet()) {
                String alias = e.getKey();
                GridSqlType type = (GridSqlType) e.getValue();
                assert !F.isEmpty(alias);
                Column col0;
                if (type == GridSqlType.UNKNOWN) {
                    // Special case for parameter being set at the top of the query (e.g. SELECT ? FROM ...).
                    // Re-map it to STRING in the same way it is done in H2, because any argument can be cast
                    // to string.
                    col0 = new Column(alias, Value.STRING);
                } else
                    col0 = new Column(alias, type.type());
                cols.add(col0);
            }
            data.columns = cols;
        } else
            data.columns = planColumns();
        boolean sortedIdx = !F.isEmpty(qry.sortColumns());
        ReduceTable tbl = new ReduceTable(data);
        ArrayList<Index> idxs = new ArrayList<>(2);
        if (explain) {
            idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, sortedIdx ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
        } else if (sortedIdx) {
            List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
            SortedReduceIndexAdapter sortedMergeIdx = new SortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
            idxs.add(ReduceTable.createScanIndex(sortedMergeIdx, tbl));
            idxs.add(sortedMergeIdx);
        } else
            idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_UNSORTED));
        tbl.indexes(idxs);
        return tbl;
    } catch (Exception e) {
        throw new IgniteCheckedException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Index(org.gridgain.internal.h2.index.Index) CreateTableData(org.gridgain.internal.h2.command.ddl.CreateTableData) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) SqlMemoryQuotaExceededException(org.apache.ignite.cache.query.exceptions.SqlMemoryQuotaExceededException) IgniteSQLMapStepException(org.apache.ignite.internal.processors.query.IgniteSQLMapStepException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) IgniteTxAlreadyCompletedCheckedException(org.apache.ignite.internal.transactions.IgniteTxAlreadyCompletedCheckedException) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) SQLException(java.sql.SQLException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Column(org.gridgain.internal.h2.table.Column) GridSqlSortColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSortColumn) GridSqlSortColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSortColumn) GridSqlType(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonMap(java.util.Collections.singletonMap) Session(org.gridgain.internal.h2.engine.Session)

Aggregations

SQLException (java.sql.SQLException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)3 Session (org.h2.engine.Session)3 ArrayList (java.util.ArrayList)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CacheException (javax.cache.CacheException)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 IgniteException (org.apache.ignite.IgniteException)2 QueryRetryException (org.apache.ignite.cache.query.QueryRetryException)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 H2PooledConnection (org.apache.ignite.internal.processors.query.h2.H2PooledConnection)2