You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2018/05/10 15:12:49 UTC
ignite git commit: IGNITE-7535: SQL: Added encoding option to COPY
command. This closes #3487.
Repository: ignite
Updated Branches:
refs/heads/master 89aedb19f -> 492d4118e
IGNITE-7535: SQL: Added encoding option to COPY command. This closes #3487.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/492d4118
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/492d4118
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/492d4118
Branch: refs/heads/master
Commit: 492d4118e715745eaabafdd9d50e4abd26ed34f2
Parents: 89aedb1
Author: tledkov-gridgain <tl...@gridgain.com>
Authored: Thu May 10 18:12:41 2018 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Thu May 10 18:12:41 2018 +0300
----------------------------------------------------------------------
.../ignite/examples/sql/SqlJdbcCopyExample.java | 4 +-
.../thin/JdbcThinBulkLoadAbstractSelfTest.java | 408 +++++++++++++++----
.../src/test/resources/bulkload2_utf.csv | 2 -
.../src/test/resources/bulkload2_utf8.csv | 2 +
.../test/resources/bulkload2_windows1251.csv | 2 +
.../ignite/internal/jdbc2/JdbcQueryTask.java | 2 +-
.../bulkload/BulkLoadCacheWriter.java | 4 +-
.../processors/bulkload/BulkLoadCsvFormat.java | 25 +-
.../processors/bulkload/BulkLoadCsvParser.java | 19 +-
.../processors/bulkload/BulkLoadFormat.java | 4 +-
.../bulkload/pipeline/CharsetDecoderBlock.java | 15 +-
.../bulkload/pipeline/PipelineBlock.java | 5 +-
.../apache/ignite/internal/sql/SqlKeyword.java | 3 +
.../ignite/internal/sql/SqlParseException.java | 13 +
.../apache/ignite/internal/sql/SqlParser.java | 11 +-
.../internal/sql/SqlStrictParseException.java | 32 ++
.../sql/command/SqlBulkLoadCommand.java | 32 +-
.../internal/sql/SqlParserBulkLoadSelfTest.java | 64 ++-
.../processors/query/h2/IgniteH2Indexing.java | 5 +-
.../yardstick/upload/model/QueryFactory.java | 2 +-
20 files changed, 540 insertions(+), 114 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/examples/src/main/java/org/apache/ignite/examples/sql/SqlJdbcCopyExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/sql/SqlJdbcCopyExample.java b/examples/src/main/java/org/apache/ignite/examples/sql/SqlJdbcCopyExample.java
index 1271b39..394c3b0 100644
--- a/examples/src/main/java/org/apache/ignite/examples/sql/SqlJdbcCopyExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/sql/SqlJdbcCopyExample.java
@@ -58,8 +58,8 @@ public class SqlJdbcCopyExample {
print("Created database objects.");
// Load data from CSV file.
- executeCommand(conn, "COPY FROM \"" +
- IgniteUtils.resolveIgnitePath("examples/src/main/resources/sql/city.csv") + "\" " +
+ executeCommand(conn, "COPY FROM '" +
+ IgniteUtils.resolveIgnitePath("examples/src/main/resources/sql/city.csv") + "' " +
"INTO City (ID, Name, CountryCode, District, Population) FORMAT CSV");
// Read data.
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinBulkLoadAbstractSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinBulkLoadAbstractSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinBulkLoadAbstractSelfTest.java
index e9cb832..2a4c799 100644
--- a/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinBulkLoadAbstractSelfTest.java
+++ b/modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinBulkLoadAbstractSelfTest.java
@@ -22,9 +22,16 @@ import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.NearCacheConfiguration;
+import org.apache.ignite.internal.processors.bulkload.BulkLoadCsvFormat;
+import org.apache.ignite.internal.processors.bulkload.BulkLoadCsvParser;
import org.apache.ignite.internal.processors.query.QueryUtils;
+import org.apache.ignite.lang.IgniteClosure;
import org.apache.ignite.testframework.GridTestUtils;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.UnsupportedCharsetException;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -35,7 +42,6 @@ import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.Callable;
-import static org.apache.ignite.IgniteSystemProperties.IGNITE_SQL_PARSER_DISABLE_H2_FALLBACK;
import static org.apache.ignite.cache.CacheMode.PARTITIONED;
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
import static org.apache.ignite.internal.util.IgniteUtils.resolveIgnitePath;
@@ -47,9 +53,13 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
/** Subdirectory with CSV files */
private static final String CSV_FILE_SUBDIR = "/modules/clients/src/test/resources/";
+ /** Default table name. */
+ private static final String TBL_NAME = "Person";
+
/** A CSV file with zero records */
private static final String BULKLOAD_EMPTY_CSV_FILE =
- Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload0.csv")).getAbsolutePath();
+ Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload0.csv"))
+ .getAbsolutePath();
/** A CSV file with one record. */
private static final String BULKLOAD_ONE_LINE_CSV_FILE =
@@ -60,18 +70,19 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload2.csv")).getAbsolutePath();
/** A CSV file in UTF-8. */
- private static final String BULKLOAD_UTF_CSV_FILE =
- Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload2_utf.csv")).getAbsolutePath();
+ private static final String BULKLOAD_UTF8_CSV_FILE =
+ Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload2_utf8.csv")).getAbsolutePath();
- /** Default table name. */
- private static final String TBL_NAME = "Person";
+ /** A CSV file in windows-1251. */
+ private static final String BULKLOAD_CP1251_CSV_FILE =
+ Objects.requireNonNull(resolveIgnitePath(CSV_FILE_SUBDIR + "bulkload2_windows1251.csv")).getAbsolutePath();
/** Basic COPY statement used in majority of the tests. */
public static final String BASIC_SQL_COPY_STMT =
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\"" +
- " into " + TBL_NAME +
- " (_key, age, firstName, lastName)" +
- " format csv";
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "'" +
+ " into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv";
/** JDBC statement. */
private Statement stmt;
@@ -161,8 +172,6 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
@Override protected void beforeTest() throws Exception {
super.beforeTest();
- System.setProperty(IGNITE_SQL_PARSER_DISABLE_H2_FALLBACK, "TRUE");
-
stmt = conn.createStatement();
assertNotNull(stmt);
@@ -176,8 +185,6 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
assertTrue(stmt.isClosed());
- System.clearProperty(IGNITE_SQL_PARSER_DISABLE_H2_FALLBACK);
-
super.afterTest();
}
@@ -203,9 +210,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
*/
public void testEmptyFile() throws SQLException {
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_EMPTY_CSV_FILE + "\" into " + TBL_NAME +
- " (_key, age, firstName, lastName)" +
- " format csv");
+ "copy from '" + BULKLOAD_EMPTY_CSV_FILE + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv");
assertEquals(0, updatesCnt);
@@ -219,9 +226,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
*/
public void testOneLineFile() throws SQLException {
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_ONE_LINE_CSV_FILE + "\" into " + TBL_NAME +
- " (_key, age, firstName, lastName)" +
- " format csv");
+ "copy from '" + BULKLOAD_ONE_LINE_CSV_FILE + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv");
assertEquals(1, updatesCnt);
@@ -229,25 +236,151 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
}
/**
+ * Verifies that error is reported for empty charset name.
+ */
+ public void testEmptyCharset() {
+ GridTestUtils.assertThrows(log, new Callable<Object>() {
+ @Override public Object call() throws Exception {
+ stmt.executeUpdate(
+ "copy from 'any.file' into Person " +
+ "(_key, age, firstName, lastName) " +
+ "format csv charset ''");
+
+ return null;
+ }
+ }, SQLException.class, "Unknown charset name: ''");
+ }
+
+ /**
+ * Verifies that error is reported for unsupported charset name.
+ */
+ public void testNotSupportedCharset() {
+ GridTestUtils.assertThrows(log, new Callable<Object>() {
+ @Override public Object call() throws Exception {
+ stmt.executeUpdate(
+ "copy from 'any.file' into Person " +
+ "(_key, age, firstName, lastName) " +
+ "format csv charset 'nonexistent'");
+
+ return null;
+ }
+ }, SQLException.class, "Charset is not supported: 'nonexistent'");
+ }
+
+ /**
+ * Verifies that error is reported for unknown charset name.
+ */
+ public void testUnknownCharset() {
+ GridTestUtils.assertThrows(log, new Callable<Object>() {
+ @Override public Object call() throws Exception {
+ stmt.executeUpdate(
+ "copy from 'any.file' into Person " +
+ "(_key, age, firstName, lastName) " +
+ "format csv charset '8^)\'");
+
+ return null;
+ }
+ }, SQLException.class, "Unknown charset name: '8^)'");
+ }
+
+ /**
+ * Verifies that ASCII encoding is recognized and imported.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testAsciiCharset() throws SQLException {
+ int updatesCnt = stmt.executeUpdate(
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "'" +
+ " into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv charset 'ascii'");
+
+ assertEquals(2, updatesCnt);
+
+ checkCacheContents(TBL_NAME, true, 2);
+ }
+
+ /**
* Imports two-entry CSV file with UTF-8 characters into a table and checks
* the created entries using SELECT statement.
*
* @throws SQLException If failed.
*/
- public void testUtf() throws SQLException {
+ public void testUtf8Charset() throws SQLException {
+ checkBulkLoadWithCharset(BULKLOAD_UTF8_CSV_FILE, "utf-8");
+ }
+
+ /**
+ * Verifies that ASCII encoding is recognized and imported.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testWin1251Charset() throws SQLException {
+ checkBulkLoadWithCharset(BULKLOAD_CP1251_CSV_FILE, "windows-1251");
+ }
+
+ /**
+ * Bulk-loads specified file specifying charset in the command
+ * and verifies the entries imported.
+ *
+ * @param fileName CSV file to load.
+ * @param charsetName Charset name to specify in the command.
+ * @throws SQLException If failed.
+ */
+ private void checkBulkLoadWithCharset(String fileName, String charsetName) throws SQLException {
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_UTF_CSV_FILE + "\" into " + TBL_NAME +
- " (_key, age, firstName, lastName)" +
- " format csv");
+ "copy from '" + fileName + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv charset '" + charsetName + "'");
assertEquals(2, updatesCnt);
- checkUtfCacheContents(TBL_NAME, true, 2);
+ checkNationalCacheContents(TBL_NAME);
+ }
+
+ /**
+ * Verifies that no error is reported and characters are converted improperly when we import
+ * UTF-8 as windows-1251.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testWrongCharset_Utf8AsWin1251() throws SQLException {
+ checkBulkLoadWithWrongCharset(BULKLOAD_UTF8_CSV_FILE, "UTF-8", "windows-1251");
+ }
+
+ /**
+ * Verifies that no error is reported and characters are converted improperly when we import
+ * windows-1251 as UTF-8.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testWrongCharset_Win1251AsUtf8() throws SQLException {
+ checkBulkLoadWithWrongCharset(BULKLOAD_CP1251_CSV_FILE, "windows-1251", "UTF-8");
+ }
+
+ /**
+ * Verifies that no error is reported and characters are converted improperly when we import
+ * UTF-8 as ASCII.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testWrongCharset_Utf8AsAscii() throws SQLException {
+ checkBulkLoadWithWrongCharset(BULKLOAD_UTF8_CSV_FILE, "UTF-8", "ascii");
+ }
+
+ /**
+ * Verifies that no error is reported and characters are converted improperly when we import
+ * windows-1251 as ASCII.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testWrongCharset_Win1251AsAscii() throws SQLException {
+ checkBulkLoadWithWrongCharset(BULKLOAD_CP1251_CSV_FILE, "windows-1251", "ascii");
}
/**
- * Checks that bulk load works when we use batch size of 1 byte and thus
- * create multiple batches per COPY.
+ * Checks that bulk load works when we use packet size of 1 byte and thus
+ * create multiple packets per COPY.
*
* @throws SQLException If failed.
*/
@@ -260,21 +393,38 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
}
/**
+ * Imports two-entry CSV file with UTF-8 characters into a table and checks
+ * the created entries using SELECT statement.
+ *
+ * @throws SQLException If failed.
+ */
+ public void testDefaultCharset() throws SQLException {
+ int updatesCnt = stmt.executeUpdate(
+ "copy from '" + BULKLOAD_UTF8_CSV_FILE + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv");
+
+ assertEquals(2, updatesCnt);
+
+ checkNationalCacheContents(TBL_NAME);
+ }
+
+ /**
* Imports two-entry CSV file with UTF-8 characters into a table using packet size of one byte
- * (thus splitting each two-byte UTF-8 character into two batches)
+ * (thus splitting each two-byte UTF-8 character into two packets)
* and checks the created entries using SELECT statement.
*
* @throws SQLException If failed.
*/
- public void testUtfPacketSize_1() throws SQLException {
+ public void testDefaultCharsetPacketSize1() throws SQLException {
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_UTF_CSV_FILE + "\" into " + TBL_NAME +
- " (_key, age, firstName, lastName)" +
- " format csv packet_size 1");
+ "copy from '" + BULKLOAD_UTF8_CSV_FILE + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv packet_size 1");
assertEquals(2, updatesCnt);
- checkUtfCacheContents(TBL_NAME, true, 2);
+ checkNationalCacheContents(TBL_NAME);
}
/**
@@ -284,9 +434,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeUpdate(
- "copy from \"nonexistent\" into Person" +
- " (_key, age, firstName, lastName)" +
- " format csv");
+ "copy from 'nonexistent' into Person" +
+ " (_key, age, firstName, lastName)" +
+ " format csv");
return null;
}
@@ -300,9 +450,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeUpdate(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into Peterson" +
- " (_key, age, firstName, lastName)" +
- " format csv");
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "' into Peterson" +
+ " (_key, age, firstName, lastName)" +
+ " format csv");
return null;
}
@@ -316,9 +466,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeUpdate(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into Person" +
- " (_key, age, firstName, lostName)" +
- " format csv");
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "' into Person" +
+ " (_key, age, firstName, lostName)" +
+ " format csv");
return null;
}
@@ -332,9 +482,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeUpdate(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into Person" +
- " (_key, firstName, age, lastName)" +
- " format csv");
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "' into Person" +
+ " (_key, firstName, age, lastName)" +
+ " format csv");
return null;
}
@@ -348,9 +498,10 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
*/
public void testFieldsSubset() throws SQLException {
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into " + TBL_NAME +
- " (_key, age, firstName)" +
- " format csv");
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "'" +
+ " into " + TBL_NAME +
+ " (_key, age, firstName)" +
+ " format csv");
assertEquals(2, updatesCnt);
@@ -372,9 +523,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
" (id int primary key, age int, firstName varchar(30), lastName varchar(30))");
int updatesCnt = stmt.executeUpdate(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into " + tblName +
- "(_key, age, firstName, lastName)" +
- " format csv");
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "' into " + tblName +
+ "(_key, age, firstName, lastName)" +
+ " format csv");
assertEquals(2, updatesCnt);
@@ -401,18 +552,20 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
}
/**
- * Verifies exception thrown if COPY is added into a batch.
+ * Verifies exception thrown if COPY is added into a packet.
+ *
+ * @throws SQLException If failed.
*/
- public void testMultipleStatement() {
+ public void testMultipleStatement() throws SQLException {
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.addBatch(BASIC_SQL_COPY_STMT);
- stmt.addBatch("copy from \"" + BULKLOAD_ONE_LINE_CSV_FILE + "\" into " + TBL_NAME +
+ stmt.addBatch("copy from '" + BULKLOAD_ONE_LINE_CSV_FILE + "' into " + TBL_NAME +
" (_key, age, firstName, lastName)" +
" format csv");
- stmt.addBatch("copy from \"" + BULKLOAD_UTF_CSV_FILE + "\" into " + TBL_NAME +
+ stmt.addBatch("copy from '" + BULKLOAD_UTF8_CSV_FILE + "' into " + TBL_NAME +
" (_key, age, firstName, lastName)" +
" format csv");
@@ -425,8 +578,10 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
/**
* Verifies that COPY command is rejected by Statement.executeQuery().
+ *
+ * @throws SQLException If failed.
*/
- public void testExecuteQuery() {
+ public void testExecuteQuery() throws SQLException {
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
stmt.executeQuery(BASIC_SQL_COPY_STMT);
@@ -466,22 +621,24 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
/**
* Verifies that COPY command reports an error when used with PreparedStatement parameter.
+ *
+ * @throws SQLException If failed.
*/
- public void testPreparedStatementWithParameter() {
+ public void testPreparedStatementWithParameter() throws SQLException {
GridTestUtils.assertThrows(log, new Callable<Object>() {
- @Override public Object call() throws Exception {
- PreparedStatement pstmt = conn.prepareStatement(
- "copy from \"" + BULKLOAD_TWO_LINES_CSV_FILE + "\" into " + TBL_NAME +
+ @Override public Object call() throws Exception {
+ PreparedStatement pstmt = conn.prepareStatement(
+ "copy from '" + BULKLOAD_TWO_LINES_CSV_FILE + "' into " + TBL_NAME +
" (_key, age, firstName, lastName)" +
" format ?");
- pstmt.setString(1, "csv");
+ pstmt.setString(1, "csv");
- pstmt.executeUpdate();
+ pstmt.executeUpdate();
- return null;
- }
- }, SQLException.class, "Unexpected token: \"?\" (expected: \"[identifier]\"");
+ return null;
+ }
+ }, SQLException.class, "Unexpected token: \"?\" (expected: \"[identifier]\"");
}
/**
@@ -515,7 +672,9 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
}
/**
- * Checks cache contents for a typical test using SQL SELECT command.
+ * Checks cache contents after bulk loading data in the above tests: ASCII version.
+ * <p>
+ * Uses SQL SELECT command for querying entries.
*
* @param tblName Table name to query.
* @param checkLastName Check 'lastName' column (not imported in some tests).
@@ -554,18 +713,47 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
}
/**
- * Checks cache contents for a UTF-8 bulk load tests using SQL SELECT command.
+ * Checks cache contents after bulk loading data in the above tests:
+ * national charset version.
+ * <p>
+ * Uses SQL SELECT command for querying entries.
*
* @param tblName Table name to query.
- * @param checkLastName Check 'lastName' column (not imported in some tests).
- * @param recCnt Number of records to expect.
* @throws SQLException When one of checks has failed.
*/
- private void checkUtfCacheContents(String tblName, boolean checkLastName, int recCnt) throws SQLException {
+ private void checkNationalCacheContents(String tblName) throws SQLException {
+ checkRecodedNationalCacheContents(tblName, null, null);
+ }
+
+ /**
+ * Checks cache contents after bulk loading data in the tests:
+ * normal and erroneously recoded national charset version.
+ * <p>
+ * Uses SQL SELECT command for querying entries.
+ *
+ * @param tblName Table name to query.
+ * @param csvCharsetName Either null or the charset used in CSV file
+ * Note that the both {@code csvCharsetName} and {@code stmtCharsetName} should be either null or non-null.
+ * @param stmtCharsetName Either null or the charset specified in COPY statement.
+ * @throws SQLException When one of checks has failed.
+ */
+ private void checkRecodedNationalCacheContents(String tblName,
+ String csvCharsetName, String stmtCharsetName) throws SQLException {
+ assert (csvCharsetName != null) == (stmtCharsetName != null);
+
ResultSet rs = stmt.executeQuery("select _key, age, firstName, lastName from " + tblName);
assert rs != null;
+ IgniteClosure<String, String> recoder =
+ (csvCharsetName != null)
+ ? new WrongCharsetRecoder(csvCharsetName, stmtCharsetName)
+ : new IgniteClosure<String, String>() {
+ @Override public String apply(String input) {
+ return input;
+ }
+ };
+
int cnt = 0;
while (rs.next()) {
@@ -573,15 +761,17 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
if (id == 123) {
assertEquals(12, rs.getInt("age"));
- assertEquals("Имя123 Отчество123", rs.getString("firstName"));
- if (checkLastName)
- assertEquals("Фамилия123", rs.getString("lastName"));
+
+ assertEquals(recoder.apply("Имя123 Отчество123"), rs.getString("firstName"));
+
+ assertEquals(recoder.apply("Фамилия123"), rs.getString("lastName"));
}
else if (id == 456) {
assertEquals(45, rs.getInt("age"));
- assertEquals("Имя456", rs.getString("firstName"));
- if (checkLastName)
- assertEquals("Фамилия456", rs.getString("lastName"));
+
+ assertEquals(recoder.apply("Имя456"), rs.getString("firstName"));
+
+ assertEquals(recoder.apply("Фамилия456"), rs.getString("lastName"));
}
else
fail("Wrong ID: " + id);
@@ -589,6 +779,70 @@ public abstract class JdbcThinBulkLoadAbstractSelfTest extends JdbcThinAbstractD
cnt++;
}
- assertEquals(recCnt, cnt);
+ assertEquals(2, cnt);
+ }
+
+ /**
+ * Checks that no error is reported and characters are converted improperly when we import
+ * file having a different charset than the one specified in the SQL statement.
+ *
+ * @param csvFileName Imported file name.
+ * @param csvCharsetName Imported file charset.
+ * @param stmtCharsetName Charset to specify in the SQL statement.
+ * @throws SQLException If failed.
+ */
+ private void checkBulkLoadWithWrongCharset(String csvFileName, String csvCharsetName, String stmtCharsetName)
+ throws SQLException {
+ int updatesCnt = stmt.executeUpdate(
+ "copy from '" + csvFileName + "' into " + TBL_NAME +
+ " (_key, age, firstName, lastName)" +
+ " format csv charset '" + stmtCharsetName + "'");
+
+ assertEquals(2, updatesCnt);
+
+ checkRecodedNationalCacheContents(TBL_NAME, csvCharsetName, stmtCharsetName);
+ }
+
+ /**
+ * Recodes an input string as if it was encoded in one charset and was read using
+ * another charset using {@link CodingErrorAction#REPLACE} settings for
+ * unmappable and malformed characters.
+ */
+ private static class WrongCharsetRecoder implements IgniteClosure<String, String> {
+ /** Charset in which the string we are reading is actually encoded. */
+ private final Charset actualCharset;
+
+ /** Charset which we use to read the string. */
+ private final Charset appliedCharset;
+
+ /**
+ * Creates the recoder.
+ *
+ * @param actualCharset Charset in which the string we are reading is actually encoded.
+ * @param appliedCharset Charset which we use to read the string.
+ * @throws UnsupportedCharsetException if the charset name is wrong.
+ */
+ WrongCharsetRecoder(String actualCharset, String appliedCharset) {
+ this.actualCharset = Charset.forName(actualCharset);
+ this.appliedCharset = Charset.forName(appliedCharset);
+ }
+
+ /**
+ * Converts string as it was read using a wrong charset.
+ * <p>
+ * First the method converts the string into {@link #actualCharset} and puts bytes into a buffer.
+ * Then it tries to read these bytes from the buffer using {@link #appliedCharset} and
+ * {@link CodingErrorAction#REPLACE} settings for unmappable and malformed characters
+ * (NB: these settings implicitly come from {@link Charset#decode(ByteBuffer)} implementation, while
+ * being explicitly set in {@link BulkLoadCsvParser#BulkLoadCsvParser(BulkLoadCsvFormat)}).
+ *
+ * @param input The input string (in Java encoding).
+ * @return The converted string.
+ */
+ @Override public String apply(String input) {
+ ByteBuffer encodedBuf = actualCharset.encode(input);
+
+ return appliedCharset.decode(encodedBuf).toString();
+ }
}
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/clients/src/test/resources/bulkload2_utf.csv
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/resources/bulkload2_utf.csv b/modules/clients/src/test/resources/bulkload2_utf.csv
deleted file mode 100644
index bdb6489..0000000
--- a/modules/clients/src/test/resources/bulkload2_utf.csv
+++ /dev/null
@@ -1,2 +0,0 @@
-123,12,"Имя123 Отчество123",Фамилия123
-456,45,"Имя456","Фамилия456"
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/clients/src/test/resources/bulkload2_utf8.csv
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/resources/bulkload2_utf8.csv b/modules/clients/src/test/resources/bulkload2_utf8.csv
new file mode 100644
index 0000000..bdb6489
--- /dev/null
+++ b/modules/clients/src/test/resources/bulkload2_utf8.csv
@@ -0,0 +1,2 @@
+123,12,"Имя123 Отчество123",Фамилия123
+456,45,"Имя456","Фамилия456"
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/clients/src/test/resources/bulkload2_windows1251.csv
----------------------------------------------------------------------
diff --git a/modules/clients/src/test/resources/bulkload2_windows1251.csv b/modules/clients/src/test/resources/bulkload2_windows1251.csv
new file mode 100644
index 0000000..b0dcde0
--- /dev/null
+++ b/modules/clients/src/test/resources/bulkload2_windows1251.csv
@@ -0,0 +1,2 @@
+123,12,"���123 ��������123",�������123
+456,45,"���456","�������456"
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcQueryTask.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcQueryTask.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcQueryTask.java
index 07034f4..fce046d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcQueryTask.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc2/JdbcQueryTask.java
@@ -174,7 +174,7 @@ class JdbcQueryTask implements IgniteCallable<JdbcQueryTaskResult> {
if (fldQryCursor instanceof BulkLoadContextCursor) {
fldQryCursor.close();
-
+
throw new SQLException("COPY command is currently supported only in thin JDBC driver.");
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCacheWriter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCacheWriter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCacheWriter.java
index 90714c8..0f7444c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCacheWriter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCacheWriter.java
@@ -20,7 +20,9 @@ package org.apache.ignite.internal.processors.bulkload;
import org.apache.ignite.lang.IgniteBiTuple;
import org.apache.ignite.lang.IgniteInClosure;
-/** A proxy, which stores given key+value pair to a cache. */
+/**
+ * A proxy, which stores given key+value pair to a cache.
+ */
public abstract class BulkLoadCacheWriter implements IgniteInClosure<IgniteBiTuple<?, ?>>, AutoCloseable {
/**
* Returns number of entry updates made by the writer.
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvFormat.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvFormat.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvFormat.java
index ec1dfd1..69cb341 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvFormat.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvFormat.java
@@ -22,7 +22,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
-/** A placeholder for bulk load CSV format parser options. */
+/**
+ * A placeholder for bulk load CSV format parser options.
+ */
public class BulkLoadCsvFormat extends BulkLoadFormat {
/** Line separator pattern. */
@NotNull public static final Pattern DEFAULT_LINE_SEPARATOR = Pattern.compile("[\r\n]+");
@@ -57,6 +59,9 @@ public class BulkLoadCsvFormat extends BulkLoadFormat {
/** Set of escape start characters. */
@Nullable private String escapeChars;
+ /** File charset. */
+ @Nullable private String inputCharsetName;
+
/**
* Returns the name of the format.
*
@@ -155,4 +160,22 @@ public class BulkLoadCsvFormat extends BulkLoadFormat {
public void escapeChars(@Nullable String escapeChars) {
this.escapeChars = escapeChars;
}
+
+ /**
+ * Returns the input file charset name, null if not specified.
+ *
+ * @return The input file charset name, null if not specified.
+ */
+ @Nullable public String inputCharsetName() {
+ return inputCharsetName;
+ }
+
+ /**
+ * Sets the input file charset name. The null here means "not specified".
+ *
+ * @param inputCharsetName The input file charset name.
+ */
+ public void inputCharsetName(@Nullable String inputCharsetName) {
+ this.inputCharsetName = inputCharsetName;
+ }
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvParser.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvParser.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvParser.java
index 0511596..98e994d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvParser.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadCsvParser.java
@@ -24,8 +24,12 @@ import org.apache.ignite.internal.processors.bulkload.pipeline.PipelineBlock;
import org.apache.ignite.internal.processors.bulkload.pipeline.StrListAppenderBlock;
import org.apache.ignite.internal.processors.bulkload.pipeline.LineSplitterBlock;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.LinkedList;
import java.util.List;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
/** CSV parser for COPY command. */
public class BulkLoadCsvParser extends BulkLoadParser {
@@ -41,7 +45,20 @@ public class BulkLoadCsvParser extends BulkLoadParser {
* @param format Format options (parsed from COPY command).
*/
public BulkLoadCsvParser(BulkLoadCsvFormat format) {
- inputBlock = new CharsetDecoderBlock(BulkLoadFormat.DEFAULT_INPUT_CHARSET);
+ try {
+ Charset charset = format.inputCharsetName() == null ? BulkLoadFormat.DEFAULT_INPUT_CHARSET :
+ Charset.forName(format.inputCharsetName());
+
+ inputBlock = new CharsetDecoderBlock(charset);
+ }
+ catch (IllegalCharsetNameException e) {
+ throw new IgniteSQLException("Unknown charset name: '" + format.inputCharsetName() + "': " +
+ e.getMessage());
+ }
+ catch (UnsupportedCharsetException e) {
+ throw new IgniteSQLException("Charset is not supported: '" + format.inputCharsetName() + "': " +
+ e.getMessage());
+ }
collectorBlock = new StrListAppenderBlock();
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadFormat.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadFormat.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadFormat.java
index cff93c5..bb4beb0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadFormat.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/BulkLoadFormat.java
@@ -19,7 +19,9 @@ package org.apache.ignite.internal.processors.bulkload;
import java.nio.charset.Charset;
-/** A superclass and a factory for bulk load format options. */
+/**
+ * A superclass and a factory for bulk load format options.
+ */
public abstract class BulkLoadFormat {
/** The default input charset. */
public static final Charset DEFAULT_INPUT_CHARSET = Charset.forName("UTF-8");
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/CharsetDecoderBlock.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/CharsetDecoderBlock.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/CharsetDecoderBlock.java
index 5b18def..f9031b1 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/CharsetDecoderBlock.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/CharsetDecoderBlock.java
@@ -34,6 +34,9 @@ import java.util.Arrays;
* the erroneous input, appending the coder's replacement value to the output buffer, and resuming the coding operation.
*/
public class CharsetDecoderBlock extends PipelineBlock<byte[], char[]> {
+ /** Empty portion. */
+ public static final char[] EMPTY_PORTION = new char[0];
+
/** Charset decoder */
private final CharsetDecoder charsetDecoder;
@@ -67,7 +70,8 @@ public class CharsetDecoderBlock extends PipelineBlock<byte[], char[]> {
isEndOfInput = isLastAppend;
if (leftover == null && data.length == 0) {
- nextBlock.accept(new char[0], isLastAppend);
+ nextBlock.accept(EMPTY_PORTION, isLastAppend);
+
return;
}
@@ -78,8 +82,7 @@ public class CharsetDecoderBlock extends PipelineBlock<byte[], char[]> {
else {
dataBuf = ByteBuffer.allocate(leftover.length + data.length);
- dataBuf.put(leftover)
- .put(data);
+ dataBuf.put(leftover).put(data);
dataBuf.flip();
@@ -101,8 +104,9 @@ public class CharsetDecoderBlock extends PipelineBlock<byte[], char[]> {
leftover = Arrays.copyOfRange(dataBuf.array(),
dataBuf.arrayOffset() + dataBuf.position(), dataBuf.limit());
+ // See {@link CharsetDecoder} class javadoc for the protocol.
if (isEndOfInput)
- charsetDecoder.flush(outBuf); // See {@link CharsetDecoder} class javadoc for the protocol.
+ charsetDecoder.flush(outBuf);
if (outBuf.position() > 0)
nextBlock.accept(Arrays.copyOfRange(outBuf.array(), outBuf.arrayOffset(), outBuf.position()),
@@ -111,7 +115,8 @@ public class CharsetDecoderBlock extends PipelineBlock<byte[], char[]> {
break;
}
- if (res.isOverflow()) { // Not enough space in the output buffer, flush it and retry.
+ // Not enough space in the output buffer, flush it and retry.
+ if (res.isOverflow()) {
assert outBuf.position() > 0;
nextBlock.accept(Arrays.copyOfRange(outBuf.array(), outBuf.arrayOffset(), outBuf.position()),
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/PipelineBlock.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/PipelineBlock.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/PipelineBlock.java
index 914b4b4..f0a7339 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/PipelineBlock.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/bulkload/pipeline/PipelineBlock.java
@@ -27,7 +27,7 @@ import org.jetbrains.annotations.Nullable;
*/
public abstract class PipelineBlock<I, O> {
/** The next block in pipeline or null if this block is a terminator. */
- @Nullable protected PipelineBlock<O, ?> nextBlock;
+ @Nullable PipelineBlock<O, ?> nextBlock;
/**
* Creates a pipeline block.
@@ -35,7 +35,7 @@ public abstract class PipelineBlock<I, O> {
* <p>(There is no nextBlock argument in the constructor: setting the next block using
* {@link #append(PipelineBlock)} method is more convenient.
*/
- protected PipelineBlock() {
+ PipelineBlock() {
nextBlock = null;
}
@@ -61,6 +61,7 @@ public abstract class PipelineBlock<I, O> {
*
* @param inputPortion Portion of input.
* @param isLastPortion Is this the last portion.
+ * @throws IgniteCheckedException On error.
*/
public abstract void accept(I inputPortion, boolean isLastPortion) throws IgniteCheckedException;
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlKeyword.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlKeyword.java b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlKeyword.java
index e25096d..0fd08f4 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlKeyword.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlKeyword.java
@@ -62,6 +62,9 @@ public class SqlKeyword {
/** Keyword: CHARACTER. */
public static final String CHARACTER = "CHARACTER";
+ /** Keyword: CHARSET. */
+ public static final String CHARSET = "CHARSET";
+
/** Keyword: COPY. */
public static final String COPY = "COPY";
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParseException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParseException.java b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParseException.java
index 96d385d..c31824c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParseException.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParseException.java
@@ -53,6 +53,19 @@ public class SqlParseException extends IgniteException {
}
/**
+ * Copy constructor.
+ *
+ * @param e Copied exception.
+ */
+ protected SqlParseException(SqlParseException e) {
+ super(e.getMessage());
+
+ sql = e.sql;
+ pos = e.pos;
+ code = e. code;
+ }
+
+ /**
* Prepare message.
*
* @param sql Original SQL.
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParser.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParser.java b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParser.java
index cbde936..d46863a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParser.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlParser.java
@@ -113,9 +113,14 @@ public class SqlParser {
break;
case COPY:
- cmd = processCopy();
-
- break;
+ try {
+ cmd = processCopy();
+
+ break;
+ }
+ catch (SqlParseException e) {
+ throw new SqlStrictParseException(e);
+ }
case SET:
cmd = processSet();
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlStrictParseException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlStrictParseException.java b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlStrictParseException.java
new file mode 100644
index 0000000..7c83197
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/sql/SqlStrictParseException.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql;
+
+/**
+ * Parse exception guarantees parse error without. Such error deliver to user
+ * statement isn't passed to H2 parser.
+ */
+public class SqlStrictParseException extends SqlParseException {
+ /**
+ * Constructor.
+ * @param e SQL parse exception.
+ */
+ public SqlStrictParseException(SqlParseException e) {
+ super(e);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/main/java/org/apache/ignite/internal/sql/command/SqlBulkLoadCommand.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/sql/command/SqlBulkLoadCommand.java b/modules/core/src/main/java/org/apache/ignite/internal/sql/command/SqlBulkLoadCommand.java
index 9b0db96..a1e4aec 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/sql/command/SqlBulkLoadCommand.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/sql/command/SqlBulkLoadCommand.java
@@ -33,6 +33,7 @@ import static org.apache.ignite.internal.sql.SqlParserUtils.errorUnexpectedToken
import static org.apache.ignite.internal.sql.SqlParserUtils.parseIdentifier;
import static org.apache.ignite.internal.sql.SqlParserUtils.parseInt;
import static org.apache.ignite.internal.sql.SqlParserUtils.parseQualifiedIdentifier;
+import static org.apache.ignite.internal.sql.SqlParserUtils.parseString;
import static org.apache.ignite.internal.sql.SqlParserUtils.skipCommaOrRightParenthesis;
import static org.apache.ignite.internal.sql.SqlParserUtils.skipIfMatches;
import static org.apache.ignite.internal.sql.SqlParserUtils.skipIfMatchesKeyword;
@@ -84,8 +85,8 @@ public class SqlBulkLoadCommand implements SqlCommand {
* @param lex The lexer.
*/
private void parseFileName(SqlLexer lex) {
- if (lex.lookAhead().tokenType() != SqlLexerTokenType.QUOTED)
- throw errorUnexpectedToken(lex.lookAhead(), "[quoted file name]");
+ if (lex.lookAhead().tokenType() != SqlLexerTokenType.STRING)
+ throw errorUnexpectedToken(lex.lookAhead(), "[file name: string]");
lex.shift();
@@ -150,6 +151,8 @@ public class SqlBulkLoadCommand implements SqlCommand {
fmt.commentChars(BulkLoadCsvFormat.DEFAULT_COMMENT_CHARS);
fmt.escapeChars(BulkLoadCsvFormat.DEFAULT_ESCAPE_CHARS);
+ parseCsvOptions(lex, fmt);
+
inputFormat = fmt;
break;
@@ -161,6 +164,31 @@ public class SqlBulkLoadCommand implements SqlCommand {
}
/**
+ * Parses CSV format options.
+ *
+ * @param lex The lexer.
+ * @param format CSV format object to configure.
+ */
+ private void parseCsvOptions(SqlLexer lex, BulkLoadCsvFormat format) {
+ while (lex.lookAhead().tokenType() == SqlLexerTokenType.DEFAULT) {
+ switch (lex.lookAhead().token()) {
+ case SqlKeyword.CHARSET: {
+ lex.shift();
+
+ String charsetName = parseString(lex);
+
+ format.inputCharsetName(charsetName);
+
+ break;
+ }
+
+ default:
+ return;
+ }
+ }
+ }
+
+ /**
* Parses the optional parameters.
*
* @param lex The lexer.
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/core/src/test/java/org/apache/ignite/internal/sql/SqlParserBulkLoadSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/sql/SqlParserBulkLoadSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/sql/SqlParserBulkLoadSelfTest.java
index 96fe57f..b6716ff 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/sql/SqlParserBulkLoadSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/sql/SqlParserBulkLoadSelfTest.java
@@ -24,63 +24,99 @@ public class SqlParserBulkLoadSelfTest extends SqlParserAbstractSelfTest {
/** Tests for COPY command. */
public void testCopy() {
assertParseError(null,
- "copy grom \"any.file\" into Person (_key, age, firstName, lastName) format csv",
+ "copy grom 'any.file' into Person (_key, age, firstName, lastName) format csv",
"Unexpected token: \"GROM\" (expected: \"FROM\")");
assertParseError(null,
"copy from into Person (_key, age, firstName, lastName) format csv",
- "Unexpected token: \"INTO\" (expected: \"[quoted file name]\"");
+ "Unexpected token: \"INTO\" (expected: \"[file name: string]\"");
assertParseError(null,
"copy from unquoted into Person (_key, age, firstName, lastName) format csv",
- "Unexpected token: \"UNQUOTED\" (expected: \"[quoted file name]\"");
+ "Unexpected token: \"UNQUOTED\" (expected: \"[file name: string]\"");
assertParseError(null,
"copy from unquoted.file into Person (_key, age, firstName, lastName) format csv",
- "Unexpected token: \"UNQUOTED\" (expected: \"[quoted file name]\"");
+ "Unexpected token: \"UNQUOTED\" (expected: \"[file name: string]\"");
new SqlParser(null,
- "copy from \"\" into Person (_key, age, firstName, lastName) format csv")
+ "copy from '' into Person (_key, age, firstName, lastName) format csv")
.nextCommand();
new SqlParser(null,
- "copy from \"d:/copy/from/into/format.csv\" into Person (_key, age, firstName, lastName) format csv")
+ "copy from 'd:/copy/from/into/format.csv' into Person (_key, age, firstName, lastName) format csv")
.nextCommand();
new SqlParser(null,
- "copy from \"/into\" into Person (_key, age, firstName, lastName) format csv")
+ "copy from '/into' into Person (_key, age, firstName, lastName) format csv")
.nextCommand();
new SqlParser(null,
- "copy from \"into\" into Person (_key, age, firstName, lastName) format csv")
+ "copy from 'into' into Person (_key, age, firstName, lastName) format csv")
.nextCommand();
assertParseError(null,
- "copy from \"any.file\" to Person (_key, age, firstName, lastName) format csv",
+ "copy from 'any.file' to Person (_key, age, firstName, lastName) format csv",
"Unexpected token: \"TO\" (expected: \"INTO\")");
// Column list
assertParseError(null,
- "copy from \"any.file\" into Person () format csv",
+ "copy from '" +
+ "any.file' into Person () format csv",
"Unexpected token: \")\" (expected: \"[identifier]\")");
assertParseError(null,
- "copy from \"any.file\" into Person (,) format csv",
+ "copy from 'any.file' into Person (,) format csv",
"Unexpected token: \",\" (expected: \"[identifier]\")");
assertParseError(null,
- "copy from \"any.file\" into Person format csv",
+ "copy from 'any.file' into Person format csv",
"Unexpected token: \"FORMAT\" (expected: \"(\")");
// FORMAT
assertParseError(null,
- "copy from \"any.file\" into Person (_key, age, firstName, lastName)",
+ "copy from 'any.file' into Person (_key, age, firstName, lastName)",
"Unexpected end of command (expected: \"FORMAT\")");
assertParseError(null,
- "copy from \"any.file\" into Person (_key, age, firstName, lastName) format lsd",
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format lsd",
"Unknown format name: LSD");
+
+ // FORMAT CSV CHARSET
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv")
+ .nextCommand();
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset" +
+ " 'utf-8'")
+ .nextCommand();
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset" +
+ " 'UTF-8'")
+ .nextCommand();
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset" +
+ " 'UtF-8'")
+ .nextCommand();
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset" +
+ " 'windows-1251'")
+ .nextCommand();
+
+ new SqlParser(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset" +
+ " 'ISO-2022-JP'")
+ .nextCommand();
+
+ assertParseError(null,
+ "copy from 'any.file' into Person (_key, age, firstName, lastName) format csv charset ",
+ "Unexpected end of command (expected: \"[string]\")");
}
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
index b113a8b..7b1a093 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java
@@ -122,10 +122,10 @@ import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisito
import org.apache.ignite.internal.processors.timeout.GridTimeoutProcessor;
import org.apache.ignite.internal.sql.SqlParseException;
import org.apache.ignite.internal.sql.SqlParser;
+import org.apache.ignite.internal.sql.SqlStrictParseException;
import org.apache.ignite.internal.sql.command.SqlAlterTableCommand;
import org.apache.ignite.internal.sql.command.SqlBulkLoadCommand;
import org.apache.ignite.internal.sql.command.SqlAlterUserCommand;
-import org.apache.ignite.internal.sql.command.SqlBulkLoadCommand;
import org.apache.ignite.internal.sql.command.SqlCommand;
import org.apache.ignite.internal.sql.command.SqlCreateIndexCommand;
import org.apache.ignite.internal.sql.command.SqlCreateUserCommand;
@@ -1525,6 +1525,9 @@ public class IgniteH2Indexing implements GridQueryIndexing {
|| cmd instanceof SqlDropUserCommand))
return null;
}
+ catch (SqlStrictParseException e) {
+ throw new IgniteSQLException(e.getMessage(), IgniteQueryErrorCode.PARSING, e);
+ }
catch (Exception e) {
// Cannot parse, return.
if (log.isDebugEnabled())
http://git-wip-us.apache.org/repos/asf/ignite/blob/492d4118/modules/yardstick/src/main/java/org/apache/ignite/yardstick/upload/model/QueryFactory.java
----------------------------------------------------------------------
diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/upload/model/QueryFactory.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/upload/model/QueryFactory.java
index 8021a3c..87efa83 100644
--- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/upload/model/QueryFactory.java
+++ b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/upload/model/QueryFactory.java
@@ -111,7 +111,7 @@ public class QueryFactory {
if (packetSize != null)
pSizeExpr = " packet_size " + packetSize;
- return "COPY FROM \"" + csvFilePath + "\" " +
+ return "COPY FROM '" + csvFilePath + "' " +
"INTO test_upload " + attributes() + " " +
"FORMAT CSV" + pSizeExpr + ";";
}