|
10 | 10 | package net.sf.jsqlparser.statement.select; |
11 | 11 |
|
12 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
13 | 15 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
14 | 18 |
|
| 19 | +import java.util.concurrent.CancellationException; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.IntStream; |
15 | 23 | import net.sf.jsqlparser.JSQLParserException; |
| 24 | +import net.sf.jsqlparser.expression.Function; |
| 25 | +import net.sf.jsqlparser.parser.CCJSqlParser; |
| 26 | +import net.sf.jsqlparser.parser.CCJSqlParserConstants; |
| 27 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 28 | +import net.sf.jsqlparser.parser.ParseException; |
| 29 | +import net.sf.jsqlparser.parser.Token; |
16 | 30 | import net.sf.jsqlparser.test.TestUtils; |
17 | 31 | import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.Timeout; |
18 | 33 | import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.CsvSource; |
19 | 35 | import org.junit.jupiter.params.provider.ValueSource; |
| 36 | +import org.mockito.Mockito; |
20 | 37 |
|
21 | 38 | class WithItemTest { |
22 | 39 |
|
| 40 | + @ParameterizedTest |
| 41 | + @CsvSource({"2047, 1", "2047, -1", "2048, 1", "4096, 1"}) |
| 42 | + void testLargeExpressionAlias(int arguments, String firstArgument) throws JSQLParserException { |
| 43 | + String expression = "coalesce(" + firstArgument + ", 1".repeat(arguments - 1) + ")"; |
| 44 | + Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 45 | + "WITH " + expression + " AS v, t AS (SELECT v) SELECT v FROM t", true); |
| 46 | + WithItem<?> item = select.getWithItemsList().get(0); |
| 47 | + Function function = assertInstanceOf(Function.class, item.getExpression()); |
| 48 | + assertEquals("v", item.getAliasName()); |
| 49 | + assertNull(item.getParenthesedStatement()); |
| 50 | + assertEquals("coalesce", function.getName()); |
| 51 | + assertEquals(arguments, function.getParameters().size()); |
| 52 | + assertEquals(firstArgument, function.getParameters().get(0).toString()); |
| 53 | + assertEquals("1", function.getParameters().get(arguments - 1).toString()); |
| 54 | + assertNotNull(select.getWithItemsList().get(1).getParenthesedStatement()); |
| 55 | + } |
| 56 | + |
| 57 | + @ParameterizedTest |
| 58 | + @ValueSource(ints = {2047, 2048}) |
| 59 | + void testLargeCteColumnList(int columns) throws JSQLParserException { |
| 60 | + String names = IntStream.range(0, columns).mapToObj(i -> "c" + i) |
| 61 | + .collect(Collectors.joining(", ")); |
| 62 | + Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 63 | + "WITH t(" + names + ") AS (SELECT 1" + ", 1".repeat(columns - 1) |
| 64 | + + ") SELECT * FROM t", |
| 65 | + true); |
| 66 | + WithItem<?> item = select.getWithItemsList().get(0); |
| 67 | + assertEquals("t", item.getAliasName()); |
| 68 | + assertNull(item.getExpression()); |
| 69 | + assertNotNull(item.getParenthesedStatement()); |
| 70 | + assertEquals(columns, item.getWithItemList().size()); |
| 71 | + assertEquals("c" + (columns - 1), item.getWithItemList().get(columns - 1).toString()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testLargeNestedExpressionAlias() throws JSQLParserException { |
| 76 | + String expression = "concat(concat('(', ')')" + ", 'x'".repeat(2047) + ")"; |
| 77 | + Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 78 | + "WITH " + expression + " AS v SELECT v", true); |
| 79 | + Function function = assertInstanceOf(Function.class, |
| 80 | + select.getWithItemsList().get(0).getExpression()); |
| 81 | + assertEquals(2048, function.getParameters().size()); |
| 82 | + assertEquals("concat('(', ')')", function.getParameters().get(0).toString()); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @ValueSource(ints = {1, 64, 65}) |
| 87 | + void testQualifiedExpressionAlias(int qualifiers) throws JSQLParserException { |
| 88 | + String name = "s.".repeat(qualifiers) + "coalesce"; |
| 89 | + Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 90 | + "WITH " + name + "(1, 2) AS v SELECT v", true); |
| 91 | + Function function = assertInstanceOf(Function.class, |
| 92 | + select.getWithItemsList().get(0).getExpression()); |
| 93 | + assertEquals(name, function.getName()); |
| 94 | + assertEquals(2, function.getParameters().size()); |
| 95 | + } |
| 96 | + |
| 97 | + @ParameterizedTest |
| 98 | + @ValueSource(ints = {2, 2048}) |
| 99 | + @Timeout(5) |
| 100 | + void testUnclosedExpressionAlias(int arguments) { |
| 101 | + String sql = "WITH coalesce(1" + ", 1".repeat(arguments - 1); |
| 102 | + assertThrows(ParseException.class, () -> CCJSqlParserUtil.newParser(sql).Statement()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testInterruptedWithItemLookaheadStopsReading() { |
| 107 | + CCJSqlParser parser = CCJSqlParserUtil.newParser( |
| 108 | + "WITH coalesce(1" + ", 1".repeat(512) + ") AS v SELECT v"); |
| 109 | + AtomicBoolean reachedAlias = new AtomicBoolean(); |
| 110 | + parser.token_source = Mockito.spy(parser.token_source); |
| 111 | + Mockito.doAnswer(invocation -> { |
| 112 | + Token token = (Token) invocation.callRealMethod(); |
| 113 | + if (token.kind == CCJSqlParserConstants.S_LONG) { |
| 114 | + parser.interrupted = true; |
| 115 | + } else if (token.kind == CCJSqlParserConstants.K_AS) { |
| 116 | + reachedAlias.set(true); |
| 117 | + } |
| 118 | + return token; |
| 119 | + }).when(parser.token_source).getNextToken(); |
| 120 | + assertThrows(CancellationException.class, parser::Statement); |
| 121 | + assertFalse(reachedAlias.get()); |
| 122 | + } |
| 123 | + |
23 | 124 | @Test |
24 | 125 | void testNotMaterializedIssue2251() throws JSQLParserException { |
25 | 126 | String sqlStr = "WITH devices AS NOT MATERIALIZED (\n" |
|
0 commit comments