|
1 | 1 | package ru.yandex.clickhouse.integration; |
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream; |
| 4 | +import java.io.InputStream; |
4 | 5 | import java.io.UnsupportedEncodingException; |
5 | 6 | import java.math.BigInteger; |
| 7 | +import java.nio.charset.Charset; |
6 | 8 | import java.sql.Array; |
7 | 9 | import java.sql.Connection; |
8 | 10 | import java.sql.ResultSet; |
@@ -146,6 +148,59 @@ public void testExternalData() throws SQLException, UnsupportedEncodingException |
146 | 148 | Assert.assertEquals(groupName, "Group"); |
147 | 149 | } |
148 | 150 |
|
| 151 | + |
| 152 | + private InputStream getTSVStream(final int rowsCount) { |
| 153 | + return new InputStream() { |
| 154 | + private int si = 0; |
| 155 | + private String s = ""; |
| 156 | + private int i = 0; |
| 157 | + |
| 158 | + private boolean genNextString() { |
| 159 | + if (i >= rowsCount) return false; |
| 160 | + si = 0; |
| 161 | + s = String.format("%d\t%d\n", i, i); |
| 162 | + i++; |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + public int read() { |
| 167 | + if (si >= s.length()) { |
| 168 | + if ( ! genNextString() ) { |
| 169 | + return -1; |
| 170 | + } |
| 171 | + } |
| 172 | + return s.charAt( si++ ); |
| 173 | + } |
| 174 | + }; |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testExternalDataStream() throws SQLException, UnsupportedEncodingException { |
| 180 | + final ClickHouseStatement statement = connection.createStatement(); |
| 181 | + connection.createStatement().execute("DROP TABLE IF EXISTS test.testExternalData"); |
| 182 | + connection.createStatement().execute("CREATE TABLE test.testExternalData (id UInt64, s String) ENGINE = Memory"); |
| 183 | + connection.createStatement().execute("insert into test.testExternalData select number, toString(number) from numbers(500,100000)"); |
| 184 | + |
| 185 | + InputStream inputStream = getTSVStream(100000); |
| 186 | + |
| 187 | + ClickHouseExternalData extData = new ClickHouseExternalData("ext_data", inputStream); |
| 188 | + extData.setStructure("id UInt64, s String"); |
| 189 | + |
| 190 | + ResultSet rs = connection.createStatement().executeQuery( |
| 191 | + "select count() cnt from test.testExternalData where (id,s) in ext_data", |
| 192 | + null, |
| 193 | + Collections.singletonList(extData) |
| 194 | + ); |
| 195 | + |
| 196 | + rs.next(); |
| 197 | + |
| 198 | + int cnt = rs.getInt("cnt"); |
| 199 | + |
| 200 | + Assert.assertEquals(cnt, 99500); |
| 201 | + } |
| 202 | + |
| 203 | + |
149 | 204 | @Test |
150 | 205 | public void testResultSetWithExtremes() throws SQLException { |
151 | 206 | ClickHouseProperties properties = new ClickHouseProperties(); |
|
0 commit comments