Search in sources :

Example 76 with AND

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.

the class GridSubqueryJoinOptimizer method isSimpleSelect.

/**
 * Whether Select query is simple or not.
 * <p>
 * We call query simple if it is select query (not union) and it has neither having nor grouping,
 * has no distinct clause, has no aggregations, has no limits, no sorting, no offset clause.
 * Also it is not SELECT FOR UPDATE.
 *
 * @param subQry Sub query.
 * @return {@code true} if it is simple query.
 */
private static boolean isSimpleSelect(GridSqlQuery subQry) {
    if (subQry instanceof GridSqlUnion)
        return false;
    GridSqlSelect select = (GridSqlSelect) subQry;
    boolean simple = F.isEmpty(select.sort()) && select.offset() == null && select.limit() == null && !select.isForUpdate() && !select.distinct() && select.havingColumn() < 0 && F.isEmpty(select.groupColumns());
    if (!simple)
        return false;
    for (GridSqlAst col : select.columns(true)) {
        if (!(col instanceof GridSqlElement))
            continue;
        // we have to traverse the tree because there may be such expressions
        // like ((MAX(col) - MIN(col)) / COUNT(col)
        ASTNodeFinder aggFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlAggregateFunction);
        if (aggFinder.findNext() != null)
            return false;
        // In case of query like "SELECT * FROM (SELECT i||j FROM t) u;", where subquery contains pure operation
        // without an alias, we cannot determine which generated alias in the parent query the original expression
        // belongs to. So the best we can do is skip the case.
        ASTNodeFinder operationFinder = new ASTNodeFinder(col, (p, c) -> p instanceof GridSqlOperation, ast -> false);
        if (operationFinder.findNext() != null)
            return false;
    }
    return true;
}
Also used : GridSqlUnion(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlAggregateFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAggregateFunction) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Example 77 with AND

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.

the class SchemaManager method createInitialUserIndex.

/**
 * Add initial user index.
 *
 * @param schemaName Schema name.
 * @param desc Table descriptor.
 * @param h2Idx User index.
 * @throws IgniteCheckedException If failed.
 */
private void createInitialUserIndex(String schemaName, H2TableDescriptor desc, GridH2IndexBase h2Idx) throws IgniteCheckedException {
    GridH2Table h2Tbl = desc.table();
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, false);
        connMgr.executeStatement(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 78 with AND

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.

the class DmlAstUtils method findKeyValueEqualityCondition.

/**
 * @param where Element to test.
 * @return Whether given element corresponds to {@code WHERE _key = ?}, and key is a literal expressed
 * in query or a query param.
 */
@SuppressWarnings("RedundantCast")
private static IgnitePair<GridSqlElement> findKeyValueEqualityCondition(GridSqlElement where) {
    if (!(where instanceof GridSqlOperation))
        return null;
    GridSqlOperation whereOp = (GridSqlOperation) where;
    // Does this WHERE limit only by _key?
    if (isKeyEqualityCondition(whereOp))
        return new IgnitePair<>((GridSqlElement) whereOp.child(1), null);
    // Or maybe it limits both by _key and _val?
    if (whereOp.operationType() != GridSqlOperationType.AND)
        return null;
    GridSqlElement left = whereOp.child(0);
    GridSqlElement right = whereOp.child(1);
    if (!(left instanceof GridSqlOperation && right instanceof GridSqlOperation))
        return null;
    GridSqlOperation leftOp = (GridSqlOperation) left;
    GridSqlOperation rightOp = (GridSqlOperation) right;
    if (isKeyEqualityCondition(leftOp)) {
        // _key = ? and _val = ?
        if (!isValueEqualityCondition(rightOp))
            return null;
        return new IgnitePair<>((GridSqlElement) leftOp.child(1), (GridSqlElement) rightOp.child(1));
    } else if (isKeyEqualityCondition(rightOp)) {
        // _val = ? and _key = ?
        if (!isValueEqualityCondition(leftOp))
            return null;
        return new IgnitePair<>((GridSqlElement) rightOp.child(1), (GridSqlElement) leftOp.child(1));
    } else
        // Neither
        return null;
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)

Example 79 with AND

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.

the class DmlAstUtils method findParams.

/**
 * @param qry Select.
 * @param params Parameters.
 * @param target Extracted parameters.
 * @param paramIdxs Parameter indexes.
 * @return Extracted parameters list.
 */
private static List<Object> findParams(GridSqlSelect qry, Object[] params, ArrayList<Object> target, IntArray paramIdxs) {
    if (params.length == 0)
        return target;
    for (GridSqlAst el : qry.columns(false)) findParams((GridSqlElement) el, params, target, paramIdxs);
    findParams((GridSqlElement) qry.from(), params, target, paramIdxs);
    findParams((GridSqlElement) qry.where(), params, target, paramIdxs);
    // Don't search in GROUP BY and HAVING since they expected to be in select list.
    findParams((GridSqlElement) qry.limit(), params, target, paramIdxs);
    findParams((GridSqlElement) qry.offset(), params, target, paramIdxs);
    return target;
}
Also used : GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)

Example 80 with AND

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.

the class OpenCensusSqlNativeTracingTest method checkDmlQuerySpans.

/**
 * Executes DML query and checks corresponding span tree.
 *
 * @param qry SQL query to execute.
 * @param fetchRequired Whether query need to fetch data before cache update.
 */
private void checkDmlQuerySpans(String qry, boolean fetchRequired, int expCacheUpdates) throws Exception {
    SpanId rootSpan = executeAndCheckRootSpan(qry, TEST_SCHEMA, false, false, false);
    checkChildSpan(SQL_QRY_PARSE, rootSpan);
    SpanId dmlExecSpan = checkChildSpan(SQL_DML_QRY_EXECUTE, rootSpan);
    if (fetchRequired) {
        checkChildSpan(SQL_ITER_OPEN, dmlExecSpan);
        int fetchedRows = findChildSpans(SQL_PAGE_FETCH, null).stream().mapToInt(span -> parseInt(getAttribute(span, SQL_PAGE_ROWS))).sum();
        assertEquals(expCacheUpdates, fetchedRows);
    }
    int cacheUpdates = findChildSpans(SQL_CACHE_UPDATE, dmlExecSpan).stream().mapToInt(span -> parseInt(getAttribute(span, SQL_CACHE_UPDATES))).sum();
    assertEquals(expCacheUpdates, cacheUpdates);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SQL_DML_QRY_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_RESP) SQL_NEXT_PAGE_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_NEXT_PAGE_REQ) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) Arrays(java.util.Arrays) SQL_IDX_RANGE_ROWS(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_IDX_RANGE_ROWS) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) SQL_PAGE_ROWS(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_PAGE_ROWS) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SQL_QRY_ID(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_QRY_ID) SQL_CMD_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CMD_QRY_EXECUTE) IgniteEx(org.apache.ignite.internal.IgniteEx) SQL_FAIL_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_FAIL_RESP) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) NoopSpan(org.apache.ignite.internal.processors.tracing.NoopSpan) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQL_PAGE_WAIT(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_WAIT) SpanId(io.opencensus.trace.SpanId) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) NODE(org.apache.ignite.internal.processors.tracing.SpanTags.NODE) Tracing(io.opencensus.trace.Tracing) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) ImmutableMap(com.google.common.collect.ImmutableMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Set(java.util.Set) SQL_IDX_RANGE_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_IDX_RANGE_RESP) SQL_ITER_CLOSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_ITER_CLOSE) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) DFLT_SCHEMA(org.apache.ignite.internal.processors.query.QueryUtils.DFLT_SCHEMA) SQL_CURSOR_CANCEL(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_CANCEL) SAMPLING_RATE_ALWAYS(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS) MTC(org.apache.ignite.internal.processors.tracing.MTC) TracingSpi(org.apache.ignite.spi.tracing.TracingSpi) SQL_IDX_RANGE_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_IDX_RANGE_REQ) SQL_CURSOR_OPEN(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_OPEN) SQL(org.apache.ignite.spi.tracing.Scope.SQL) Pattern(java.util.regex.Pattern) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridQueryNextPageRequest(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageRequest) SQL_QRY_EXEC_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_EXEC_REQ) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) SQL_CACHE_UPDATE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CACHE_UPDATE) SQL_SCHEMA(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_SCHEMA) U(org.apache.ignite.internal.util.typedef.internal.U) SQL_PARTITIONS_RESERVE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PARTITIONS_RESERVE) SQL_QRY_CANCEL_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_CANCEL_REQ) SQL_DML_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_EXECUTE) OpenCensusTracingSpi(org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi) SQL_PAGE_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_RESP) SQL_QRY_TEXT(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_QRY_TEXT) SQL_CACHE_UPDATES(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_CACHE_UPDATES) SpanTags.tag(org.apache.ignite.internal.processors.tracing.SpanTags.tag) SQL_ITER_OPEN(org.apache.ignite.internal.processors.tracing.SpanType.SQL_ITER_OPEN) Iterator(java.util.Iterator) Pattern.compile(java.util.regex.Pattern.compile) SQL_CURSOR_CLOSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_CLOSE) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) Test(org.junit.Test) SQL_PAGE_FETCH(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_FETCH) Ignite(org.apache.ignite.Ignite) SQL_PARSER_CACHE_HIT(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_PARSER_CACHE_HIT) SQL_DML_QRY_EXEC_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_EXEC_REQ) NAME(org.apache.ignite.internal.processors.tracing.SpanTags.NAME) Integer.parseInt(java.lang.Integer.parseInt) SQL_QRY(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY) SQL_PAGE_PREPARE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_PREPARE) CONSISTENT_ID(org.apache.ignite.internal.processors.tracing.SpanTags.CONSISTENT_ID) NODE_ID(org.apache.ignite.internal.processors.tracing.SpanTags.NODE_ID) Ignition(org.apache.ignite.Ignition) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) SQL_QRY_PARSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_PARSE) SQL_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_EXECUTE) CacheMode(org.apache.ignite.cache.CacheMode) SpanId(io.opencensus.trace.SpanId)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)37 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)33 ArrayList (java.util.ArrayList)26 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)25 List (java.util.List)22 IgniteException (org.apache.ignite.IgniteException)21 SQLException (java.sql.SQLException)15 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)13 HashMap (java.util.HashMap)12 Column (org.h2.table.Column)12 LinkedHashMap (java.util.LinkedHashMap)11 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)11 Index (org.h2.index.Index)11 PreparedStatement (java.sql.PreparedStatement)9 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)9 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)9 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)9 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)9 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)9