Skip to content

Commit 534eacc

Browse files
committed
Updated tests for IN processing
1 parent e1bc500 commit 534eacc

File tree

2 files changed

+117
-38
lines changed

2 files changed

+117
-38
lines changed

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,14 @@ private void assertResultSetCount(ResultSet rs, int count) throws SQLException {
665665
@ValueSource(strings = {"true", "false"})
666666
public void inListTest(boolean convertInToList) throws SQLException {
667667
String option = String.valueOf(convertInToList);
668+
String arg1Name = convertInToList ? "$jp1[0]" : "$jp1";
668669
String arg2Name = convertInToList ? "$jp1[1]" : "$jp2";
669670
try (Connection conn = jdbc.createCustomConnection("replaceJdbcInByYqlList", option)) {
670671
String upsert = TEST_TABLE.upsertOne(SqlQueries.JdbcQuery.STANDARD, "c_Text", "Text");
671-
String selectByIds = TEST_TABLE.withTableName("select count(*) from #tableName where key in (?, ?)");
672-
String selectByValue = TEST_TABLE.withTableName("select count(*) from #tableName where c_Text in (?, ?)");
672+
String selectPrefix = TEST_TABLE.withTableName("select count(*) from #tableName ");
673+
String selectByIds = selectPrefix + "where key in (?, ?)";
674+
String selectByValue = selectPrefix + "where c_Text in (?, ?)";
675+
String selectByTuple = selectPrefix + "where (key, c_Text) in ((?, ?), (?, ?))";
673676

674677
try (PreparedStatement ps = conn.prepareStatement(upsert)) {
675678
ps.setInt(1, 1);
@@ -717,6 +720,8 @@ public void inListTest(boolean convertInToList) throws SQLException {
717720
}
718721

719722
try (PreparedStatement ps = conn.prepareStatement(selectByValue)) {
723+
ExceptionAssert.sqlException("Missing value for parameter: " + arg1Name, ps::executeQuery);
724+
720725
ps.setString(1, null);
721726
ExceptionAssert.sqlException("Missing value for parameter: " + arg2Name, ps::executeQuery);
722727

@@ -744,12 +749,35 @@ public void inListTest(boolean convertInToList) throws SQLException {
744749
ps.setString(2, "3");
745750
assertResultSetCount(ps.executeQuery(), 2);
746751
}
752+
753+
try (PreparedStatement ps = conn.prepareStatement(selectByTuple)) {
754+
ExceptionAssert.sqlException("Missing value for parameter: " + arg1Name, ps::executeQuery);
755+
756+
ps.setInt(1, 1);
757+
ExceptionAssert.sqlException("Missing value for parameter: " + arg2Name, ps::executeQuery);
758+
759+
ps.setInt(1, 1);
760+
ps.setInt(3, 2);
761+
ps.setString(4, "3");
762+
ExceptionAssert.sqlException("Missing value for parameter: " + arg2Name, ps::executeQuery);
763+
764+
ps.setInt(1, 1);
765+
ps.setString(2, null);
766+
ps.setInt(3, 2);
767+
ps.setString(4, "3");
768+
assertResultSetCount(ps.executeQuery(), 0);
769+
770+
ps.setInt(1, 1);
771+
ps.setString(2, "1");
772+
ps.setInt(3, 3);
773+
ps.setString(4, "3");
774+
assertResultSetCount(ps.executeQuery(), 2);
775+
}
747776
}
748777
}
749778

750779
@Test
751780
public void jdbcTableListTest() throws SQLException {
752-
String arg2Name = "$jp1[1]";
753781
String upsert = TEST_TABLE.upsertOne(SqlQueries.JdbcQuery.STANDARD, "c_Text", "Text");
754782
String selectByIds = TEST_TABLE.withTableName(
755783
"select count(*) from jdbc_table(?,?) as j join #tableName t on t.key=j.x"
@@ -779,8 +807,10 @@ public void jdbcTableListTest() throws SQLException {
779807
}
780808

781809
try (PreparedStatement ps = jdbc.connection().prepareStatement(selectByIds)) {
810+
ExceptionAssert.sqlException("Missing value for parameter: $jp1[0]", ps::executeQuery);
811+
782812
ps.setInt(1, 1);
783-
ExceptionAssert.sqlException("Missing value for parameter: " + arg2Name, ps::executeQuery);
813+
ExceptionAssert.sqlException("Missing value for parameter: $jp1[1]", ps::executeQuery);
784814

785815
ps.setInt(1, 1);
786816
ps.setInt(2, 2);
@@ -798,7 +828,7 @@ public void jdbcTableListTest() throws SQLException {
798828

799829
try (PreparedStatement ps = jdbc.connection().prepareStatement(selectByValue)) {
800830
ps.setString(1, null);
801-
ExceptionAssert.sqlException("Missing value for parameter: " + arg2Name, ps::executeQuery);
831+
ExceptionAssert.sqlException("Missing value for parameter: $jp1[1]", ps::executeQuery);
802832

803833
ps.setString(1, null);
804834
ps.setString(2, null);

jdbc/src/test/java/tech/ydb/jdbc/query/YdbQueryParserTest.java

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public void noOffsetParameterTest(String query) throws SQLException {
287287
+ "@'select * from test_table where id in $jp1'",
288288
"'select * from test_table where id In(?--comment\n,?,?/**other /** inner */ comment*/)'"
289289
+ "@'select * from test_table where id In $jp1'",
290+
"'select * from test_table where (id, value) in ((?, ?), (?, ?))'"
291+
+ "@'select * from test_table where (id, value) in $jp1'",
292+
"'select * from test_table where tuple in ((?,\n?),(?, ?))'"
293+
+ "@'select * from test_table where tuple in $jp1'",
290294
}, delimiter = '@')
291295
public void inListParameterTest(String query, String parsed) throws SQLException {
292296
YdbQueryParser parser = new YdbQueryParser(types, query, props);
@@ -307,44 +311,18 @@ public void inListParameterTest(String query, String parsed) throws SQLException
307311
}
308312
}
309313

310-
@ParameterizedTest(name = "[{index}] {0} has as_table list parameter")
311-
@CsvSource(value = {
312-
"'select * from jdbc_table(?) as t join test_table on id=t.x'"
313-
+ "@'select * from AS_TABLE($jp1) as t join test_table on id=t.x'",
314-
"'select * from jdbc_table(?,\n?, ?, \t?)'"
315-
+ "@'select * from AS_TABLE($jp1)'",
316-
"'select * from JDbc_Table (?--comment\n,?,?/**other /** inner */ comment*/)'"
317-
+ "@'select * from AS_TABLE($jp1)'",
318-
}, delimiter = '@')
319-
public void jdbcTableinListParameterTest(String query, String parsed) throws SQLException {
320-
YdbQueryParser parser = new YdbQueryParser(types, query, props);
321-
Assertions.assertEquals(parsed, parser.parseSQL());
322-
323-
Assertions.assertEquals(1, parser.getStatements().size());
324-
325-
QueryStatement statement = parser.getStatements().get(0);
326-
Assertions.assertEquals(QueryType.DATA_QUERY, statement.getType());
327-
328-
Assertions.assertTrue(statement.hasJdbcParameters());
329-
int idx = 0;
330-
for (JdbcPrm.Factory factory : statement.getJdbcPrmFactories()) {
331-
for (JdbcPrm prm: factory.create()) {
332-
Assertions.assertEquals("$jp1[" + idx + "]", prm.getName());
333-
idx++;
334-
}
335-
}
336-
}
337-
338314
@ParameterizedTest(name = "[{index}] {0} has not in list parameter")
339315
@CsvSource(value = {
340316
"'select * from test_table where id in (?)'"
341317
+ "@'select * from test_table where id in ($jp1)'",
318+
"'select * from test_table where (id, value) in ((?, ?), (?, ?))'"
319+
+ "@'select * from test_table where (id, value) in (($jp1, $jp2), ($jp3, $jp4))'",
342320
"'select * from test_table where id in (?,\n?, ?, \t?)'"
343321
+ "@'select * from test_table where id in ($jp1,\n$jp2, $jp3, \t$jp4)'",
344322
"'select * from test_table where id In(?--comment\n,?,?/**other /** inner */ comment*/)'"
345323
+ "@'select * from test_table where id In($jp1--comment\n,$jp2,$jp3/**other /** inner */ comment*/)'",
346324
}, delimiter = '@')
347-
public void disabledInListParameterTest(String query, String parsed) throws SQLException {
325+
public void inListParameterDisabledTest(String query, String parsed) throws SQLException {
348326
Properties config = new Properties();
349327
config.put("replaceJdbcInByYqlList", "false");
350328
YdbQueryParser parser = new YdbQueryParser(types, query, new YdbQueryProperties(config));
@@ -365,7 +343,7 @@ public void disabledInListParameterTest(String query, String parsed) throws SQLE
365343
}
366344
}
367345

368-
@ParameterizedTest(name = "[{index}] {0} has in list parameter")
346+
@ParameterizedTest(name = "[{index}] {0} doesn't have in list parameter")
369347
@CsvSource(value = {
370348
"'select * from test_table where id in (?, 1, ?)'"
371349
+ "@'select * from test_table where id in ($jp1, 1, $jp2)'",
@@ -379,12 +357,81 @@ public void disabledInListParameterTest(String query, String parsed) throws SQLE
379357
+ "@'select * from test_table where id in($jp1, $jp2, $jp3'",
380358
"'select * from test_table where id in ?, ?, ?'"
381359
+ "@'select * from test_table where id in $jp1, $jp2, $jp3'",
382-
"'select * from test_table where id in ((?))'"
383-
+ "@'select * from test_table where id in (($jp1))'",
360+
"'select * from test_table where id in (((?)))'"
361+
+ "@'select * from test_table where id in ((($jp1)))'",
384362
"'select * from test_table where id in ,?)'"
385363
+ "@'select * from test_table where id in ,$jp1)'",
364+
"'select * from test_table where id in (())'"
365+
+ "@'select * from test_table where id in (())'",
366+
"'select * from test_table where id in ((?, ?), ?)'"
367+
+ "@'select * from test_table where id in (($jp1, $jp2), $jp3)'",
368+
"'select * from test_table where id in ((?,?),(?,?,?))'"
369+
+ "@'select * from test_table where id in (($jp1,$jp2),($jp3,$jp4,$jp5))'",
370+
}, delimiter = '@')
371+
public void inListParametersUnparsableTest(String query, String parsed) throws SQLException {
372+
YdbQueryParser parser = new YdbQueryParser(types, query, props);
373+
Assertions.assertEquals(parsed, parser.parseSQL());
374+
375+
Assertions.assertEquals(1, parser.getStatements().size());
376+
377+
QueryStatement statement = parser.getStatements().get(0);
378+
Assertions.assertEquals(QueryType.DATA_QUERY, statement.getType());
379+
380+
int idx = 0;
381+
for (JdbcPrm.Factory factory : statement.getJdbcPrmFactories()) {
382+
for (JdbcPrm prm: factory.create()) {
383+
idx++;
384+
Assertions.assertEquals("$jp" + idx, prm.getName());
385+
}
386+
}
387+
}
388+
389+
@ParameterizedTest(name = "[{index}] {0} has as_table list parameter")
390+
@CsvSource(value = {
391+
"'select * from jdbc_table(?) as t join test_table on id=t.x'"
392+
+ "@'select * from AS_TABLE($jp1) as t join test_table on id=t.x'",
393+
"'select * from jdbc_table(?,\n?, ?, \t?)'"
394+
+ "@'select * from AS_TABLE($jp1)'",
395+
"'select * from JDbc_Table (?--comment\n,?,?/**other /** inner */ comment*/)'"
396+
+ "@'select * from AS_TABLE($jp1)'",
397+
}, delimiter = '@')
398+
public void jdbcTableParameterTest(String query, String parsed) throws SQLException {
399+
YdbQueryParser parser = new YdbQueryParser(types, query, props);
400+
Assertions.assertEquals(parsed, parser.parseSQL());
401+
402+
Assertions.assertEquals(1, parser.getStatements().size());
403+
404+
QueryStatement statement = parser.getStatements().get(0);
405+
Assertions.assertEquals(QueryType.DATA_QUERY, statement.getType());
406+
407+
Assertions.assertTrue(statement.hasJdbcParameters());
408+
int idx = 0;
409+
for (JdbcPrm.Factory factory : statement.getJdbcPrmFactories()) {
410+
for (JdbcPrm prm: factory.create()) {
411+
Assertions.assertEquals("$jp1[" + idx + "]", prm.getName());
412+
idx++;
413+
}
414+
}
415+
}
416+
417+
@ParameterizedTest(name = "[{index}] {0} doesn't have as_table list parameter")
418+
@CsvSource(value = {
419+
"'select * from jdbc_table((?)) as t join test_table on id=t.x'"
420+
+ "@'select * from jdbc_table(($jp1)) as t join test_table on id=t.x'",
421+
"'select * from jdbc_table(?,) as t join test_table on id=t.x'"
422+
+ "@'select * from jdbc_table($jp1,) as t join test_table on id=t.x'",
423+
"'select * from jdbc_table where id = ?'"
424+
+ "@'select * from jdbc_table where id = $jp1'",
425+
"'select * from jdbc_table,other_table where id = ?'"
426+
+ "@'select * from jdbc_table,other_table where id = $jp1'",
427+
"'select * from jdbc_table(?,,?) as t join test_table on id=t.x'"
428+
+ "@'select * from jdbc_table($jp1,,$jp2) as t join test_table on id=t.x'",
429+
"'select * from jdbc_table(?,'"
430+
+ "@'select * from jdbc_table($jp1,'",
431+
"'select * from jdbc_table(??) where id=?'"
432+
+ "@'select * from jdbc_table(?) where id=$jp1'",
386433
}, delimiter = '@')
387-
public void wrongInListParameterTest(String query, String parsed) throws SQLException {
434+
public void jdbcTableParameterUnparsableTest(String query, String parsed) throws SQLException {
388435
YdbQueryParser parser = new YdbQueryParser(types, query, props);
389436
Assertions.assertEquals(parsed, parser.parseSQL());
390437

@@ -393,6 +440,7 @@ public void wrongInListParameterTest(String query, String parsed) throws SQLExce
393440
QueryStatement statement = parser.getStatements().get(0);
394441
Assertions.assertEquals(QueryType.DATA_QUERY, statement.getType());
395442

443+
Assertions.assertTrue(statement.hasJdbcParameters());
396444
int idx = 0;
397445
for (JdbcPrm.Factory factory : statement.getJdbcPrmFactories()) {
398446
for (JdbcPrm prm: factory.create()) {
@@ -402,6 +450,7 @@ public void wrongInListParameterTest(String query, String parsed) throws SQLExce
402450
}
403451
}
404452

453+
405454
@ParameterizedTest(name = "[{index}] {0} is batched insert query")
406455
@ValueSource(strings = {
407456
"Insert into table_name(c1, c2, c3) values (?, ? , ?)",

0 commit comments

Comments
 (0)