Skip to content

Commit c3fb547

Browse files
committed
Explicitly add "compress" query parameter according to given option, and avoid "Content-Encoding" header when compression option is "none"
1 parent bbfca81 commit c3fb547

File tree

3 files changed

+70
-40
lines changed

3 files changed

+70
-40
lines changed

clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/ClickHouseStatementImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Map;
1919
import java.util.TimeZone;
2020
import java.util.UUID;
21-
2221
import org.apache.http.Header;
2322
import org.apache.http.HttpEntity;
2423
import org.apache.http.HttpResponse;
@@ -35,7 +34,7 @@
3534
import org.apache.http.util.EntityUtils;
3635
import org.slf4j.Logger;
3736
import org.slf4j.LoggerFactory;
38-
37+
import ru.yandex.clickhouse.domain.ClickHouseCompression;
3938
import ru.yandex.clickhouse.domain.ClickHouseFormat;
4039
import ru.yandex.clickhouse.except.ClickHouseException;
4140
import ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier;
@@ -1014,7 +1013,7 @@ void sendStream(Writer writer, HttpEntity content) throws ClickHouseException {
10141013

10151014
HttpPost httpPost = new HttpPost(uri);
10161015

1017-
if (writer.getCompression() != null) {
1016+
if (writer.getCompression() != ClickHouseCompression.none) {
10181017
httpPost.addHeader("Content-Encoding", writer.getCompression().name());
10191018
}
10201019
httpPost.setEntity(content);

clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/Writer.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package ru.yandex.clickhouse;
22

3-
import org.apache.http.HttpEntity;
4-
import org.apache.http.entity.InputStreamEntity;
5-
import ru.yandex.clickhouse.domain.ClickHouseCompression;
6-
import ru.yandex.clickhouse.domain.ClickHouseFormat;
7-
import ru.yandex.clickhouse.util.ClickHouseStreamCallback;
8-
import ru.yandex.clickhouse.util.ClickHouseStreamHttpEntity;
3+
import static ru.yandex.clickhouse.domain.ClickHouseFormat.Native;
4+
import static ru.yandex.clickhouse.domain.ClickHouseFormat.RowBinary;
5+
import static ru.yandex.clickhouse.domain.ClickHouseFormat.TabSeparated;
96

107
import java.io.File;
118
import java.io.FileInputStream;
129
import java.io.IOException;
1310
import java.io.InputStream;
1411
import java.sql.SQLException;
12+
import java.util.Objects;
1513

16-
import static ru.yandex.clickhouse.domain.ClickHouseFormat.Native;
17-
import static ru.yandex.clickhouse.domain.ClickHouseFormat.RowBinary;
18-
import static ru.yandex.clickhouse.domain.ClickHouseFormat.TabSeparated;
14+
import org.apache.http.HttpEntity;
15+
import org.apache.http.entity.InputStreamEntity;
16+
import ru.yandex.clickhouse.domain.ClickHouseCompression;
17+
import ru.yandex.clickhouse.domain.ClickHouseFormat;
18+
import ru.yandex.clickhouse.settings.ClickHouseQueryParam;
19+
import ru.yandex.clickhouse.util.ClickHouseStreamCallback;
20+
import ru.yandex.clickhouse.util.ClickHouseStreamHttpEntity;
1921

2022
public class Writer extends ConfigurableApi<Writer> {
2123

@@ -27,10 +29,12 @@ public class Writer extends ConfigurableApi<Writer> {
2729

2830
Writer(ClickHouseStatementImpl statement) {
2931
super(statement);
32+
33+
dataCompression(ClickHouseCompression.none);
3034
}
3135

3236
/**
33-
* Specifies format for further insert of data via send()
37+
* Specifies format for further insert of data via send().
3438
*
3539
* @param format
3640
* the format of the data to upload
@@ -45,7 +49,7 @@ public Writer format(ClickHouseFormat format) {
4549
}
4650

4751
/**
48-
* Set table name for data insertion
52+
* Set table name for data insertion.
4953
*
5054
* @param table
5155
* name of the table to upload the data to
@@ -58,7 +62,7 @@ public Writer table(String table) {
5862
}
5963

6064
/**
61-
* Set SQL for data insertion
65+
* Set SQL for data insertion.
6266
*
6367
* @param sql
6468
* in a form "INSERT INTO table_name [(X,Y,Z)] VALUES "
@@ -71,7 +75,7 @@ public Writer sql(String sql) {
7175
}
7276

7377
/**
74-
* Specifies data input stream
78+
* Specifies data input stream.
7579
*
7680
* @param stream
7781
* a stream providing the data to upload
@@ -83,7 +87,7 @@ public Writer data(InputStream stream) {
8387
}
8488

8589
/**
86-
* Specifies data input stream, and the format to use
90+
* Specifies data input stream, and the format to use.
8791
*
8892
* @param stream
8993
* a stream providing the data to upload
@@ -96,7 +100,7 @@ public Writer data(InputStream stream, ClickHouseFormat format) {
96100
}
97101

98102
/**
99-
* Shortcut method for specifying a file as an input
103+
* Shortcut method for specifying a file as an input.
100104
*
101105
* @param input
102106
* the file to upload
@@ -116,10 +120,9 @@ public Writer data(File input, ClickHouseFormat format, ClickHouseCompression co
116120
}
117121

118122
public Writer dataCompression(ClickHouseCompression compression) {
119-
if (null == compression) {
120-
throw new NullPointerException("Compression can not be null");
121-
}
122-
this.compression = compression;
123+
this.compression = Objects.requireNonNull(compression, "Compression can not be null");
124+
this.addDbParam(ClickHouseQueryParam.COMPRESS, String.valueOf(compression != ClickHouseCompression.none));
125+
123126
return this;
124127
}
125128

@@ -128,7 +131,7 @@ public Writer data(File input, ClickHouseFormat format) {
128131
}
129132

130133
/**
131-
* Method to call, when Writer is fully configured
134+
* Method to call, when Writer is fully configured.
132135
*/
133136
public void send() throws SQLException {
134137
HttpEntity entity;
@@ -149,7 +152,7 @@ private void send(HttpEntity entity) throws SQLException {
149152
}
150153

151154
/**
152-
* Allows to send stream of data to ClickHouse
155+
* Allows to send stream of data to ClickHouse.
153156
*
154157
* @param sql
155158
* in a form of "INSERT INTO table_name (X,Y,Z) VALUES "
@@ -165,7 +168,7 @@ public void send(String sql, InputStream data, ClickHouseFormat format) throws S
165168
}
166169

167170
/**
168-
* Convenient method for importing the data into table
171+
* Convenient method for importing the data into table.
169172
*
170173
* @param table
171174
* table name
@@ -182,7 +185,7 @@ public void sendToTable(String table, InputStream data, ClickHouseFormat format)
182185

183186
/**
184187
* Sends the data in {@link ClickHouseFormat#RowBinary RowBinary} or in
185-
* {@link ClickHouseFormat#Native Native} format
188+
* {@link ClickHouseFormat#Native Native} format.
186189
*
187190
* @param sql
188191
* the SQL statement to execute

clickhouse-jdbc/src/test/java/ru/yandex/clickhouse/integration/StreamSQLTest.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package ru.yandex.clickhouse.integration;
22

3-
import org.testng.Assert;
4-
import org.testng.annotations.BeforeTest;
5-
import org.testng.annotations.Test;
6-
import ru.yandex.clickhouse.ClickHouseConnection;
7-
import ru.yandex.clickhouse.ClickHouseContainerForTest;
8-
import ru.yandex.clickhouse.ClickHouseDataSource;
9-
import ru.yandex.clickhouse.domain.ClickHouseCompression;
10-
import ru.yandex.clickhouse.domain.ClickHouseFormat;
113

12-
import java.io.*;
4+
import java.io.BufferedInputStream;
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
139
import java.math.BigDecimal;
1410
import java.nio.charset.Charset;
1511
import java.sql.ResultSet;
@@ -19,6 +15,14 @@
1915
import java.time.ZoneId;
2016
import java.time.format.DateTimeFormatter;
2117
import java.util.zip.GZIPOutputStream;
18+
import org.testng.Assert;
19+
import org.testng.annotations.BeforeTest;
20+
import org.testng.annotations.Test;
21+
import ru.yandex.clickhouse.ClickHouseConnection;
22+
import ru.yandex.clickhouse.ClickHouseContainerForTest;
23+
import ru.yandex.clickhouse.ClickHouseDataSource;
24+
import ru.yandex.clickhouse.domain.ClickHouseCompression;
25+
import ru.yandex.clickhouse.domain.ClickHouseFormat;
2226

2327
public class StreamSQLTest {
2428
private static final DateTimeFormatter DATE_TIME_FORMATTER_TZ =
@@ -49,8 +53,8 @@ public void simpleCSVInsert() throws SQLException {
4953
String string = "5,6\n1,6";
5054
InputStream inputStream = new ByteArrayInputStream(string.getBytes(Charset.forName("UTF-8")));
5155

52-
connection.createStatement().
53-
write()
56+
connection.createStatement()
57+
.write()
5458
.sql("insert into test.csv_stream_sql format CSV")
5559
.data(inputStream)
5660
.send();
@@ -114,7 +118,7 @@ public void multiRowTSVInsert() throws SQLException {
114118
private InputStream gzStream( InputStream is ) throws IOException
115119
{
116120
final int bufferSize = 16384;
117-
byte data[] = new byte[bufferSize];
121+
byte[] data = new byte[bufferSize];
118122
ByteArrayOutputStream os = new ByteArrayOutputStream();
119123
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(os);
120124
BufferedInputStream es = new BufferedInputStream(is, bufferSize);
@@ -137,8 +141,8 @@ public void multiRowTSVInsertCompressed() throws SQLException, IOException {
137141
final int rowsCount = 100000;
138142

139143
InputStream gz = gzStream(getTSVStream(rowsCount));
140-
connection.createStatement().
141-
write()
144+
connection.createStatement()
145+
.write()
142146
.sql("insert into test.tsv_compressed_stream_sql format TSV")
143147
.data(gz, ClickHouseFormat.TSV, ClickHouseCompression.gzip)
144148
.send();
@@ -151,6 +155,30 @@ public void multiRowTSVInsertCompressed() throws SQLException, IOException {
151155
Assert.assertEquals(rs.getInt("uniq"), rowsCount);
152156
}
153157

158+
@Test
159+
public void multiRowTSVInsertNotCompressed() throws SQLException, IOException {
160+
connection.createStatement().execute("DROP TABLE IF EXISTS test.tsv_not_compressed_stream_sql");
161+
connection.createStatement().execute(
162+
"CREATE TABLE test.tsv_not_compressed_stream_sql (value Int32, string_value String) ENGINE = Log()"
163+
);
164+
165+
final int rowsCount = 100000;
166+
167+
InputStream in = getTSVStream(rowsCount);
168+
connection.createStatement()
169+
.write()
170+
.sql("insert into test.tsv_not_compressed_stream_sql format TSV")
171+
.data(in, ClickHouseFormat.TSV, ClickHouseCompression.none)
172+
.send();
173+
174+
ResultSet rs = connection.createStatement().executeQuery(
175+
"SELECT count() AS cnt, sum(value) AS sum, uniqExact(string_value) uniq FROM test.tsv_not_compressed_stream_sql");
176+
Assert.assertTrue(rs.next());
177+
Assert.assertEquals(rs.getInt("cnt"), rowsCount);
178+
Assert.assertEquals(rs.getInt("sum"), rowsCount);
179+
Assert.assertEquals(rs.getInt("uniq"), rowsCount);
180+
}
181+
154182

155183
@Test
156184
public void JSONEachRowInsert() throws SQLException {

0 commit comments

Comments
 (0)