Skip to content

Commit

Permalink
#8 allow for csv queries, firt step
Browse files Browse the repository at this point in the history
  • Loading branch information
doom369 committed Aug 23, 2019
1 parent b3a8220 commit 5729d6a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
31 changes: 25 additions & 6 deletions src/main/java/ru/yandex/clickhouse/ClickHouseStatementImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.yandex.clickhouse;

import static ru.yandex.clickhouse.util.ClickHouseFormat.*;

import com.google.common.base.Strings;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -20,10 +18,19 @@
import org.slf4j.LoggerFactory;
import ru.yandex.clickhouse.except.ClickHouseException;
import ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier;
import ru.yandex.clickhouse.response.*;
import ru.yandex.clickhouse.response.ClickHouseLZ4Stream;
import ru.yandex.clickhouse.response.ClickHouseResponse;
import ru.yandex.clickhouse.response.ClickHouseResultSet;
import ru.yandex.clickhouse.response.ClickHouseScrollableResultSet;
import ru.yandex.clickhouse.response.FastByteArrayOutputStream;
import ru.yandex.clickhouse.settings.ClickHouseProperties;
import ru.yandex.clickhouse.settings.ClickHouseQueryParam;
import ru.yandex.clickhouse.util.*;
import ru.yandex.clickhouse.util.ClickHouseFormat;
import ru.yandex.clickhouse.util.ClickHouseRowBinaryInputStream;
import ru.yandex.clickhouse.util.ClickHouseStreamCallback;
import ru.yandex.clickhouse.util.ClickHouseStreamHttpEntity;
import ru.yandex.clickhouse.util.Patterns;
import ru.yandex.clickhouse.util.Utils;
import ru.yandex.clickhouse.util.guava.StreamUtils;

import java.io.ByteArrayInputStream;
Expand All @@ -35,7 +42,18 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.*;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;

import static ru.yandex.clickhouse.util.ClickHouseFormat.CSVWithNames;
import static ru.yandex.clickhouse.util.ClickHouseFormat.JSONCompact;
import static ru.yandex.clickhouse.util.ClickHouseFormat.RowBinary;
import static ru.yandex.clickhouse.util.ClickHouseFormat.TabSeparated;
import static ru.yandex.clickhouse.util.ClickHouseFormat.TabSeparatedWithNamesAndTypes;


public class ClickHouseStatementImpl implements ClickHouseStatement {
Expand Down Expand Up @@ -459,7 +477,8 @@ private static String addFormatIfAbsent(String sql, ClickHouseFormat format) {
&& !woSemicolon.endsWith(" " + TabSeparatedWithNamesAndTypes)
&& !woSemicolon.endsWith(" " + TabSeparated)
&& !woSemicolon.endsWith(" " + JSONCompact)
&& !woSemicolon.endsWith(" " + RowBinary)) {
&& !woSemicolon.endsWith(" " + RowBinary)
&& !woSemicolon.endsWith(" " + CSVWithNames)) {
if (sql.endsWith(";")) {
sql = sql.substring(0, sql.length() - 1);
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/ru/yandex/clickhouse/util/ClickHouseFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
*/
public enum ClickHouseFormat {

TabSeparated,
TabSeparatedWithNamesAndTypes,
JSONCompact,
RowBinary,
Native
TabSeparated("TabSeparated"),
TabSeparatedWithNamesAndTypes("TabSeparatedWithNamesAndTypes"),
JSONCompact("JSONCompact"),
RowBinary("RowBinary"),
Native("Native"),
CSVWithNames("CSVWithNames");

public final String name;

ClickHouseFormat(String name) {
this.name = name;
}

}
29 changes: 15 additions & 14 deletions src/test/java/ru/yandex/clickhouse/ClickHouseStatementTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package ru.yandex.clickhouse;


import java.net.URI;
import java.net.URISyntaxException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.google.common.collect.ImmutableMap;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;

import ru.yandex.clickhouse.settings.ClickHouseProperties;

import java.net.URI;
import java.sql.ResultSet;
import java.util.Properties;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

public class ClickHouseStatementTest {

@Test
public void testClickhousify() throws Exception {
public void testClickhousify() {
String sql = "SELECT ololo FROM ololoed;";
assertEquals("SELECT ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql));

Expand All @@ -39,13 +37,16 @@ public void testClickhousify() throws Exception {

String sql6 = " show ololo FROM ololoed;";
assertEquals("show ololo FROM ololoed FORMAT TabSeparatedWithNamesAndTypes;", ClickHouseStatementImpl.clickhousifySql(sql6));

String sql7 = " show ololo FROM ololoed FORMAT CSVWithNames;";
assertEquals("show ololo FROM ololoed FORMAT CSVWithNames;", ClickHouseStatementImpl.clickhousifySql(sql7));
}

@Test
public void testCredentials() throws SQLException, URISyntaxException {
public void testCredentials() {
ClickHouseProperties properties = new ClickHouseProperties(new Properties());
ClickHouseProperties withCredentials = properties.withCredentials("test_user", "test_password");
assertTrue(withCredentials != properties);
assertNotSame(withCredentials, properties);
assertNull(properties.getUser());
assertNull(properties.getPassword());
assertEquals(withCredentials.getUser(), "test_user");
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testMaxExecutionTime() throws Exception {
}

@Test
public void testMaxMemoryUsage() throws Exception {
public void testMaxMemoryUsage() {
ClickHouseProperties properties = new ClickHouseProperties();
properties.setMaxMemoryUsage(41L);
ClickHouseStatementImpl statement = new ClickHouseStatementImpl(HttpClientBuilder.create().build(), null,
Expand All @@ -90,7 +91,7 @@ public void testMaxMemoryUsage() throws Exception {
}

@Test
public void testAdditionalRequestParams() throws Exception {
public void testAdditionalRequestParams() {
ClickHouseProperties properties = new ClickHouseProperties();
ClickHouseStatementImpl statement = new ClickHouseStatementImpl(
HttpClientBuilder.create().build(),
Expand Down

0 comments on commit 5729d6a

Please sign in to comment.