|
@@ -9,11 +9,11 @@ import org.elasticsearch.client.Request;
|
|
|
import org.elasticsearch.common.CheckedBiFunction;
|
|
|
import org.elasticsearch.common.CheckedConsumer;
|
|
|
import org.elasticsearch.common.CheckedFunction;
|
|
|
+import org.elasticsearch.common.CheckedSupplier;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
-import org.elasticsearch.xpack.sql.jdbc.EsDataSource;
|
|
|
import org.elasticsearch.xpack.sql.jdbc.EsType;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -22,7 +22,6 @@ import java.io.Reader;
|
|
|
import java.sql.Blob;
|
|
|
import java.sql.Clob;
|
|
|
import java.sql.Connection;
|
|
|
-import java.sql.DriverManager;
|
|
|
import java.sql.NClob;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
@@ -80,6 +79,34 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
dateTimeTestingFields.put(new Tuple<String, Object>("test_keyword", "true"), EsType.KEYWORD);
|
|
|
}
|
|
|
|
|
|
+ public void testMultiValueFieldWithMultiValueLeniencyEnabled() throws Exception {
|
|
|
+ createTestDataForMultiValueTests();
|
|
|
+
|
|
|
+ doWithQuery(() -> esWithLeniency(true), "SELECT int, keyword FROM test", (results) -> {
|
|
|
+ results.next();
|
|
|
+ Object number = results.getObject(1);
|
|
|
+ Object string = results.getObject(2);
|
|
|
+ assertEquals(-10, number);
|
|
|
+ assertEquals("-10", string);
|
|
|
+ assertFalse(results.next());
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testMultiValueFieldWithMultiValueLeniencyDisabled() throws Exception {
|
|
|
+ createTestDataForMultiValueTests();
|
|
|
+
|
|
|
+ SQLException expected = expectThrows(SQLException.class,
|
|
|
+ () -> doWithQuery(() -> esWithLeniency(false), "SELECT int, keyword FROM test", (results) -> {
|
|
|
+ }));
|
|
|
+ assertTrue(expected.getMessage().contains("Arrays (returned by [int]) are not supported"));
|
|
|
+
|
|
|
+ // default has multi value disabled
|
|
|
+ expected = expectThrows(SQLException.class,
|
|
|
+ () -> doWithQuery(() -> esJdbc(), "SELECT int, keyword FROM test", (results) -> {
|
|
|
+ }));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
// Byte values testing
|
|
|
public void testGettingValidByteWithoutCasting() throws Exception {
|
|
|
byte random1 = randomByte();
|
|
@@ -1132,7 +1159,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
/*
|
|
|
* Checks StackOverflowError fix for https://github.com/elastic/elasticsearch/pull/31735
|
|
|
*/
|
|
|
- public void testNoInfiniteRecursiveGetObjectCalls() throws SQLException, IOException {
|
|
|
+ public void testNoInfiniteRecursiveGetObjectCalls() throws Exception {
|
|
|
index("library", "1", builder -> {
|
|
|
builder.field("name", "Don Quixote");
|
|
|
builder.field("page_count", 1072);
|
|
@@ -1303,17 +1330,16 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
}
|
|
|
|
|
|
private void doWithQuery(String query, CheckedConsumer<ResultSet, SQLException> consumer) throws SQLException {
|
|
|
- try (Connection connection = esJdbc()) {
|
|
|
- try (PreparedStatement statement = connection.prepareStatement(query)) {
|
|
|
- try (ResultSet results = statement.executeQuery()) {
|
|
|
- consumer.accept(results);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ doWithQuery(() -> esJdbc(), query, consumer);
|
|
|
}
|
|
|
|
|
|
private void doWithQueryAndTimezone(String query, String tz, CheckedConsumer<ResultSet, SQLException> consumer) throws SQLException {
|
|
|
- try (Connection connection = esJdbc(tz)) {
|
|
|
+ doWithQuery(() -> esJdbc(tz), query, consumer);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void doWithQuery(CheckedSupplier<Connection, SQLException> con, String query, CheckedConsumer<ResultSet, SQLException> consumer)
|
|
|
+ throws SQLException {
|
|
|
+ try (Connection connection = con.get()) {
|
|
|
try (PreparedStatement statement = connection.prepareStatement(query)) {
|
|
|
try (ResultSet results = statement.executeQuery()) {
|
|
|
consumer.accept(results);
|
|
@@ -1355,7 +1381,29 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
client().performRequest(request);
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForByteValueTests(byte random1, byte random2, byte random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForMultiValueTests() throws Exception {
|
|
|
+ createIndex("test");
|
|
|
+ updateMapping("test", builder -> {
|
|
|
+ builder.startObject("int").field("type", "integer").endObject();
|
|
|
+ builder.startObject("keyword").field("type", "keyword").endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ Integer[] values = randomArray(3, 15, s -> new Integer[s], () -> Integer.valueOf(randomInt(50)));
|
|
|
+ // add the minimal value in the middle yet the test will pick it up since the results are sorted
|
|
|
+ values[2] = Integer.valueOf(-10);
|
|
|
+
|
|
|
+ String[] stringValues = new String[values.length];
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
+ stringValues[i] = String.valueOf(values[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ index("test", "1", builder -> {
|
|
|
+ builder.array("int", (Object[]) values);
|
|
|
+ builder.array("keyword", stringValues);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createTestDataForByteValueTests(byte random1, byte random2, byte random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_byte").field("type", "byte").endObject();
|
|
@@ -1373,7 +1421,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForShortValueTests(short random1, short random2, short random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForShortValueTests(short random1, short random2, short random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_short").field("type", "short").endObject();
|
|
@@ -1391,7 +1439,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForIntegerValueTests(int random1, int random2, int random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForIntegerValueTests(int random1, int random2, int random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_integer").field("type", "integer").endObject();
|
|
@@ -1409,7 +1457,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForLongValueTests(long random1, long random2, long random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForLongValueTests(long random1, long random2, long random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_long").field("type", "long").endObject();
|
|
@@ -1427,7 +1475,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForDoubleValueTests(double random1, double random2, double random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForDoubleValueTests(double random1, double random2, double random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_double").field("type", "double").endObject();
|
|
@@ -1445,7 +1493,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void createTestDataForFloatValueTests(float random1, float random2, float random3) throws Exception, IOException {
|
|
|
+ private void createTestDataForFloatValueTests(float random1, float random2, float random3) throws Exception {
|
|
|
createIndex("test");
|
|
|
updateMapping("test", builder -> {
|
|
|
builder.startObject("test_float").field("type", "float").endObject();
|
|
@@ -1481,7 +1529,7 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
* Creates test data for all numeric get* methods. All values random and different from the other numeric fields already generated.
|
|
|
* It returns a map containing the field name and its randomly generated value to be later used in checking the returned values.
|
|
|
*/
|
|
|
- private Map<String,Number> createTestDataForNumericValueTypes(Supplier<Number> randomGenerator) throws Exception, IOException {
|
|
|
+ private Map<String, Number> createTestDataForNumericValueTypes(Supplier<Number> randomGenerator) throws Exception {
|
|
|
Map<String,Number> map = new HashMap<>();
|
|
|
createIndex("test");
|
|
|
updateMappingForNumericValuesTests("test");
|
|
@@ -1575,31 +1623,19 @@ public class ResultSetTestCase extends JdbcIntegrationTestCase {
|
|
|
}
|
|
|
|
|
|
private Connection esJdbc(String timeZoneId) throws SQLException {
|
|
|
- return randomBoolean() ? useDriverManager(timeZoneId) : useDataSource(timeZoneId);
|
|
|
- }
|
|
|
-
|
|
|
- private Connection useDriverManager(String timeZoneId) throws SQLException {
|
|
|
- String elasticsearchAddress = getProtocol() + "://" + elasticsearchAddress();
|
|
|
- String address = "jdbc:es://" + elasticsearchAddress;
|
|
|
Properties connectionProperties = connectionProperties();
|
|
|
connectionProperties.put(JDBC_TIMEZONE, timeZoneId);
|
|
|
- Connection connection = DriverManager.getConnection(address, connectionProperties);
|
|
|
-
|
|
|
+ Connection connection = esJdbc(connectionProperties);
|
|
|
assertNotNull("The timezone should be specified", connectionProperties.getProperty(JDBC_TIMEZONE));
|
|
|
return connection;
|
|
|
}
|
|
|
|
|
|
- private Connection useDataSource(String timeZoneId) throws SQLException {
|
|
|
- String elasticsearchAddress = getProtocol() + "://" + elasticsearchAddress();
|
|
|
- EsDataSource dataSource = new EsDataSource();
|
|
|
- String address = "jdbc:es://" + elasticsearchAddress;
|
|
|
- dataSource.setUrl(address);
|
|
|
+ private Connection esWithLeniency(boolean multiValueLeniency) throws SQLException {
|
|
|
+ String property = "field.multi.value.leniency";
|
|
|
Properties connectionProperties = connectionProperties();
|
|
|
- connectionProperties.put(JDBC_TIMEZONE, timeZoneId);
|
|
|
- dataSource.setProperties(connectionProperties);
|
|
|
- Connection connection = dataSource.getConnection();
|
|
|
-
|
|
|
- assertNotNull("The timezone should be specified", connectionProperties.getProperty(JDBC_TIMEZONE));
|
|
|
+ connectionProperties.setProperty(property, Boolean.toString(multiValueLeniency));
|
|
|
+ Connection connection = esJdbc(connectionProperties);
|
|
|
+ assertNotNull("The leniency should be specified", connectionProperties.getProperty(property));
|
|
|
return connection;
|
|
|
}
|
|
|
}
|