1313import java .util .Map .Entry ;
1414
1515import com .clickhouse .client .ClickHouseChecker ;
16+ import com .clickhouse .client .ClickHouseClient ;
1617import com .clickhouse .client .ClickHouseConfig ;
1718import com .clickhouse .client .ClickHouseFormat ;
1819import com .clickhouse .client .ClickHouseRequest ;
3031import com .clickhouse .jdbc .SqlExceptionUtils ;
3132import com .clickhouse .jdbc .JdbcWrapper ;
3233import com .clickhouse .jdbc .parser .ClickHouseSqlStatement ;
34+ import com .clickhouse .jdbc .parser .StatementType ;
3335
3436public class ClickHouseStatementImpl extends JdbcWrapper implements ClickHouseStatement {
3537 private static final Logger log = LoggerFactory .getLogger (ClickHouseStatementImpl .class );
@@ -50,7 +52,7 @@ public class ClickHouseStatementImpl extends JdbcWrapper implements ClickHouseSt
5052 private int maxFieldSize ;
5153 private int maxRows ;
5254 private boolean poolable ;
53- private String queryId ;
55+ private volatile String queryId ;
5456 private int queryTimeout ;
5557
5658 private ClickHouseResultSet currentResult ;
@@ -149,6 +151,7 @@ protected int executeInsert(String sql, InputStream input) throws SQLException {
149151 try (ClickHouseResponse resp = request .write ().query (sql , queryId = connection .newQueryId ())
150152 .format (ClickHouseFormat .RowBinary ).data (input ).execute ()
151153 .get ()) {
154+ updateResult (new ClickHouseSqlStatement (sql , StatementType .INSERT ), resp );
152155 summary = resp .getSummary ();
153156 } catch (InterruptedException e ) {
154157 log .error ("can not close stream: %s" , e .getMessage ());
@@ -197,6 +200,9 @@ protected ResultSet updateResult(ClickHouseSqlStatement stmt, ClickHouseResponse
197200 rs = currentResult ;
198201 } else {
199202 currentUpdateCount = response .getSummary ().getUpdateCount ();
203+ if (currentUpdateCount <= 0 ) {
204+ currentUpdateCount = 1 ;
205+ }
200206 response .close ();
201207 }
202208
@@ -362,11 +368,19 @@ public void setQueryTimeout(int seconds) throws SQLException {
362368
363369 @ Override
364370 public void cancel () throws SQLException {
365- if (this .queryId == null || isClosed ()) {
371+ final String qid ;
372+ if ((qid = this .queryId ) == null || isClosed ()) {
366373 return ;
367374 }
368375
369- executeQuery (String .format ("KILL QUERY WHERE query_id='%s'" , queryId ));
376+ ClickHouseClient .send (request .getServer (), String .format ("KILL QUERY WHERE query_id='%s'" , qid ))
377+ .whenComplete ((summary , exception ) -> {
378+ if (exception != null ) {
379+ log .warn ("Failed to kill query [%s] due to: %s" , qid , exception .getMessage ());
380+ } else if (summary != null ) {
381+ log .debug ("Killed query [%s]" , qid );
382+ }
383+ });
370384 }
371385
372386 @ Override
@@ -500,8 +514,10 @@ public int[] executeBatch() throws SQLException {
500514 int len = batchStmts .size ();
501515 int [] results = new int [len ];
502516 for (int i = 0 ; i < len ; i ++) {
503- try (ClickHouseResponse r = executeStatement (batchStmts .get (i ), null , null , null )) {
504- results [i ] = (int ) r .getSummary ().getWrittenRows ();
517+ ClickHouseSqlStatement s = batchStmts .get (i );
518+ try (ClickHouseResponse r = executeStatement (s , null , null , null )) {
519+ updateResult (s , r );
520+ results [i ] = currentUpdateCount <= 0 ? 0 : currentUpdateCount ;
505521 } catch (Exception e ) {
506522 results [i ] = EXECUTE_FAILED ;
507523 log .error ("Faled to execute task %d of %d" , i + 1 , len , e );
0 commit comments