Search in sources :

Example 11 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class IgniteH2Indexing method queryLocalSqlFields.

/**
 * Queries individual fields (generally used by JDBC drivers).
 *
 * @param schemaName Schema name.
 * @param qry Query.
 * @param params Query parameters.
 * @param filter Cache name and key filter.
 * @param enforceJoinOrder Enforce join order of tables in the query.
 * @param timeout Query timeout in milliseconds.
 * @param cancel Query cancel.
 * @return Query result.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
GridQueryFieldsResult queryLocalSqlFields(final String schemaName, final String qry, @Nullable final Collection<Object> params, final IndexingQueryFilter filter, boolean enforceJoinOrder, final int timeout, final GridQueryCancel cancel) throws IgniteCheckedException {
    final Connection conn = connectionForSchema(schemaName);
    H2Utils.setupConnection(conn, false, enforceJoinOrder);
    final PreparedStatement stmt = preparedStatementWithParams(conn, qry, params, true);
    if (GridSqlQueryParser.checkMultipleStatements(stmt))
        throw new IgniteSQLException("Multiple statements queries are not supported for local queries");
    Prepared p = GridSqlQueryParser.prepared(stmt);
    if (DmlStatementsProcessor.isDmlStatement(p)) {
        SqlFieldsQuery fldsQry = new SqlFieldsQuery(qry);
        if (params != null)
            fldsQry.setArgs(params.toArray());
        fldsQry.setEnforceJoinOrder(enforceJoinOrder);
        fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS);
        return dmlProc.updateSqlFieldsLocal(schemaName, conn, p, fldsQry, filter, cancel);
    } else if (DdlStatementsProcessor.isDdlStatement(p))
        throw new IgniteSQLException("DDL statements are supported for the whole cluster only", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    List<GridQueryFieldMetadata> meta;
    try {
        meta = H2Utils.meta(stmt.getMetaData());
    } catch (SQLException e) {
        throw new IgniteCheckedException("Cannot prepare query metadata", e);
    }
    final GridH2QueryContext ctx = new GridH2QueryContext(nodeId, nodeId, 0, LOCAL).filter(filter).distributedJoinMode(OFF);
    return new GridQueryFieldsResultAdapter(meta, null) {

        @Override
        public GridCloseableIterator<List<?>> iterator() throws IgniteCheckedException {
            assert GridH2QueryContext.get() == null;
            GridH2QueryContext.set(ctx);
            GridRunningQueryInfo run = new GridRunningQueryInfo(qryIdGen.incrementAndGet(), qry, SQL_FIELDS, schemaName, U.currentTimeMillis(), cancel, true);
            runs.putIfAbsent(run.id(), run);
            try {
                ResultSet rs = executeSqlQueryWithTimer(stmt, conn, qry, params, timeout, cancel);
                return new H2FieldsIterator(rs);
            } finally {
                GridH2QueryContext.clearThreadLocal();
                runs.remove(run.id());
            }
        }
    };
}
Also used : GridQueryFieldsResultAdapter(org.apache.ignite.internal.processors.query.GridQueryFieldsResultAdapter) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Connection(java.sql.Connection) Prepared(org.h2.command.Prepared) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) PreparedStatement(java.sql.PreparedStatement) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)

Example 12 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class IgniteH2Indexing method streamBatchedUpdateQuery.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
@Override
public List<Long> streamBatchedUpdateQuery(String schemaName, String qry, List<Object[]> params, SqlClientContext cliCtx) throws IgniteCheckedException {
    if (cliCtx == null || !cliCtx.isStream()) {
        U.warn(log, "Connection is not in streaming mode.");
        return zeroBatchedStreamedUpdateResult(params.size());
    }
    final Connection conn = connectionForSchema(schemaName);
    final PreparedStatement stmt = prepareStatementAndCaches(conn, qry);
    if (GridSqlQueryParser.checkMultipleStatements(stmt))
        throw new IgniteSQLException("Multiple statements queries are not supported for streaming mode.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    checkStatementStreamable(stmt);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    UpdatePlan plan = dmlProc.getPlanForStatement(schemaName, conn, p, null, true, null);
    IgniteDataStreamer<?, ?> streamer = cliCtx.streamerForCache(plan.cacheContext().name());
    assert streamer != null;
    List<Long> res = new ArrayList<>(params.size());
    for (int i = 0; i < params.size(); i++) res.add(dmlProc.streamUpdateQuery(schemaName, streamer, stmt, params.get(i)));
    return res;
}
Also used : Connection(java.sql.Connection) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Prepared(org.h2.command.Prepared) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 13 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class UpdatePlan method processRow.

/**
 * Convert a row into key-value pair.
 *
 * @param row Row to process.
 * @throws IgniteCheckedException if failed.
 */
public IgniteBiTuple<?, ?> processRow(List<?> row) throws IgniteCheckedException {
    if (mode != BULK_LOAD && row.size() != colNames.length)
        throw new IgniteSQLException("Not enough values in a row: " + row.size() + " instead of " + colNames.length, IgniteQueryErrorCode.ENTRY_PROCESSING);
    GridH2RowDescriptor rowDesc = tbl.rowDescriptor();
    GridQueryTypeDescriptor desc = rowDesc.type();
    GridCacheContext cctx = rowDesc.context();
    Object key = keySupplier.apply(row);
    if (QueryUtils.isSqlType(desc.keyClass())) {
        assert keyColIdx != -1;
        key = DmlUtils.convert(key, rowDesc, desc.keyClass(), colTypes[keyColIdx]);
    }
    Object val = valSupplier.apply(row);
    if (QueryUtils.isSqlType(desc.valueClass())) {
        assert valColIdx != -1;
        val = DmlUtils.convert(val, rowDesc, desc.valueClass(), colTypes[valColIdx]);
    }
    if (key == null) {
        if (F.isEmpty(desc.keyFieldName()))
            throw new IgniteSQLException("Key for INSERT, COPY, or MERGE must not be null", IgniteQueryErrorCode.NULL_KEY);
        else
            throw new IgniteSQLException("Null value is not allowed for column '" + desc.keyFieldName() + "'", IgniteQueryErrorCode.NULL_KEY);
    }
    if (val == null) {
        if (F.isEmpty(desc.valueFieldName()))
            throw new IgniteSQLException("Value for INSERT, COPY, MERGE, or UPDATE must not be null", IgniteQueryErrorCode.NULL_VALUE);
        else
            throw new IgniteSQLException("Null value is not allowed for column '" + desc.valueFieldName() + "'", IgniteQueryErrorCode.NULL_VALUE);
    }
    int actualColCnt = Math.min(colNames.length, row.size());
    Map<String, Object> newColVals = new HashMap<>();
    for (int i = 0; i < actualColCnt; i++) {
        if (i == keyColIdx || i == valColIdx)
            continue;
        String colName = colNames[i];
        GridQueryProperty prop = desc.property(colName);
        assert prop != null;
        Class<?> expCls = prop.type();
        newColVals.put(colName, DmlUtils.convert(row.get(i), rowDesc, expCls, colTypes[i]));
    }
    desc.setDefaults(key, val);
    // We update columns in the order specified by the table for a reason - table's
    // column order preserves their precedence for correct update of nested properties.
    Column[] tblCols = tbl.getColumns();
    // First 3 columns are _key, _val and _ver. Skip 'em.
    for (int i = DEFAULT_COLUMNS_COUNT; i < tblCols.length; i++) {
        if (tbl.rowDescriptor().isKeyValueOrVersionColumn(i))
            continue;
        String colName = tblCols[i].getName();
        if (!newColVals.containsKey(colName))
            continue;
        Object colVal = newColVals.get(colName);
        desc.setValue(colName, key, val, colVal);
    }
    if (cctx.binaryMarshaller()) {
        if (key instanceof BinaryObjectBuilder)
            key = ((BinaryObjectBuilder) key).build();
        if (val instanceof BinaryObjectBuilder)
            val = ((BinaryObjectBuilder) val).build();
    }
    desc.validateKeyAndValue(key, val);
    return new IgniteBiTuple<>(key, val);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 14 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project ignite by apache.

the class GridH2CollocationModel method buildCollocationModel.

/**
 * @param qctx Query context.
 * @param info Sub-query info.
 * @param filters Filters.
 * @param filter Filter.
 * @param validate Query validation flag.
 * @return Collocation.
 */
public static GridH2CollocationModel buildCollocationModel(GridH2QueryContext qctx, SubQueryInfo info, TableFilter[] filters, int filter, boolean validate) {
    GridH2CollocationModel cm;
    if (info != null) {
        // Go up until we reach the root query.
        cm = buildCollocationModel(qctx, info.getUpper(), info.getFilters(), info.getFilter(), validate);
    } else {
        // We are at the root query.
        cm = qctx.queryCollocationModel();
        if (cm == null) {
            cm = createChildModel(null, -1, null, true, validate);
            qctx.queryCollocationModel(cm);
        }
    }
    assert cm.view;
    Select select = filters[0].getSelect();
    // For sub-queries we will drop collocation models, so that they will be recalculated anyways.
    if (cm.select != null && cm.select != select) {
        List<GridH2CollocationModel> unions = cm.getOrCreateUnions();
        // Start with 1 because at 0 it always will be c.
        for (int i = 1; i < unions.size(); i++) {
            GridH2CollocationModel u = unions.get(i);
            if (u.select == select) {
                cm = u;
                break;
            }
        }
        // Nothing was found, need to create new child in union.
        if (cm.select != select)
            cm = createChildModel(cm.upper, cm.filter, unions, true, validate);
    }
    cm.childFilters(filters);
    return cm.child(filter, true);
}
Also used : Select(org.h2.command.dml.Select)

Example 15 with In

use of org.h2.dev.util.BinaryArithmeticStream.In in project nifi by apache.

the class DBCPServiceTest method testDropInvalidConnectionsH2_Better.

/**
 * Test Drop invalid connections and create new ones.
 * Better behavior, invalid connections are dropped and valid created.
 */
@Test
public void testDropInvalidConnectionsH2_Better() throws Exception {
    // start the H2 TCP Server
    String[] args = new String[0];
    Server server = Server.createTcpServer(args).start();
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    final DBCPConnectionPool service = new DBCPConnectionPool();
    runner.addControllerService("test-dropcreate", service);
    runner.setProperty(service, DBCPConnectionPool.DATABASE_URL, "jdbc:h2:tcp://localhost:" + server.getPort() + "/~/test");
    runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME, "org.h2.Driver");
    runner.setProperty(service, DBCPConnectionPool.VALIDATION_QUERY, "SELECT 5");
    runner.enableControllerService(service);
    runner.assertValid(service);
    final DBCPService dbcpService = (DBCPService) runner.getProcessContext().getControllerServiceLookup().getControllerService("test-dropcreate");
    Assert.assertNotNull(dbcpService);
    // get and verify connections
    for (int i = 0; i < 10; i++) {
        final Connection connection = dbcpService.getConnection();
        System.out.println(connection);
        Assert.assertNotNull(connection);
        assertValidConnectionH2(connection, i);
        connection.close();
    }
    // restart server, connections in pool should became invalid
    server.stop();
    server.shutdown();
    server.start();
    // Pool should remove invalid connections and create new valid connections.
    for (int i = 0; i < 10; i++) {
        final Connection connection = dbcpService.getConnection();
        System.out.println(connection);
        Assert.assertNotNull(connection);
        assertValidConnectionH2(connection, i);
        connection.close();
    }
    server.shutdown();
}
Also used : Server(org.h2.tools.Server) TestRunner(org.apache.nifi.util.TestRunner) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)63 Connection (java.sql.Connection)59 DbException (org.h2.message.DbException)56 PreparedStatement (java.sql.PreparedStatement)54 ResultSet (java.sql.ResultSet)47 Statement (java.sql.Statement)44 Value (org.h2.value.Value)40 IOException (java.io.IOException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)30 InputStream (java.io.InputStream)29 Column (org.h2.table.Column)24 ArrayList (java.util.ArrayList)23 SimpleResultSet (org.h2.tools.SimpleResultSet)23 Random (java.util.Random)19 Expression (org.h2.expression.Expression)18 JdbcConnection (org.h2.jdbc.JdbcConnection)18 Index (org.h2.index.Index)16 ValueString (org.h2.value.ValueString)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)15