|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.statement.select; |
11 | 11 |
|
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 15 | + |
12 | 16 | import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.expression.AnalyticExpression; |
| 18 | +import net.sf.jsqlparser.expression.Expression; |
| 19 | +import net.sf.jsqlparser.expression.WindowElement; |
| 20 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
13 | 21 | import net.sf.jsqlparser.test.TestUtils; |
14 | 22 | import org.junit.jupiter.api.Test; |
15 | 23 |
|
@@ -38,4 +46,71 @@ public void RedshiftRespectIgnoreNulls() throws JSQLParserException { |
38 | 46 |
|
39 | 47 | TestUtils.assertSqlCanBeParsedAndDeparsed(sqlString, true); |
40 | 48 | } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testWindowFrameGroupsIssue2431() throws JSQLParserException { |
| 52 | + String sqlString = |
| 53 | + "SELECT SUM(value) OVER (ORDER BY ts " |
| 54 | + + "GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW) FROM events"; |
| 55 | + |
| 56 | + WindowElement windowElement = parseWindowElement(sqlString, 0); |
| 57 | + assertEquals(WindowElement.Type.GROUPS, windowElement.getType()); |
| 58 | + TestUtils.assertSqlCanBeParsedAndDeparsed(sqlString, true); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testWindowFrameGroupsExcludeTiesIssue2431() throws JSQLParserException { |
| 63 | + String sqlString = |
| 64 | + "SELECT id, ts, value, SUM(value) OVER (ORDER BY ts " |
| 65 | + + "GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE TIES) AS sum_excl_ties " |
| 66 | + + "FROM events ORDER BY ts, id"; |
| 67 | + |
| 68 | + WindowElement windowElement = parseWindowElement(sqlString, 3); |
| 69 | + assertEquals(WindowElement.Exclusion.TIES, windowElement.getExclusion()); |
| 70 | + TestUtils.assertSqlCanBeParsedAndDeparsed(sqlString, true); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testWindowFrameExclusionsIssue2431() throws JSQLParserException { |
| 75 | + String[] sqlStrings = { |
| 76 | + "SELECT SUM(value) OVER (ORDER BY ts ROWS UNBOUNDED PRECEDING EXCLUDE CURRENT ROW) FROM events", |
| 77 | + "SELECT SUM(value) OVER (ORDER BY ts RANGE CURRENT ROW EXCLUDE GROUP) FROM events", |
| 78 | + "SELECT SUM(value) OVER (ORDER BY ts GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE TIES) FROM events", |
| 79 | + "SELECT SUM(value) OVER (ORDER BY ts GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS) FROM events" |
| 80 | + }; |
| 81 | + |
| 82 | + for (String sqlString : sqlStrings) { |
| 83 | + TestUtils.assertSqlCanBeParsedAndDeparsed(sqlString, true); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testFrameExclusionIdentifierCompatibilityIssue2431() throws JSQLParserException { |
| 89 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT ties FROM ties", true); |
| 90 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT others FROM others", true); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testWindowFrameGroupsVariantsIssue2431() throws JSQLParserException { |
| 95 | + String singleSidedSqlString = |
| 96 | + "SELECT SUM(value) OVER (ORDER BY ts GROUPS UNBOUNDED PRECEDING) FROM events"; |
| 97 | + String namedWindowSqlString = |
| 98 | + "SELECT SUM(value) OVER w FROM events " |
| 99 | + + "WINDOW w AS (ORDER BY ts GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW)"; |
| 100 | + |
| 101 | + TestUtils.assertSqlCanBeParsedAndDeparsed(singleSidedSqlString, true); |
| 102 | + TestUtils.assertSqlCanBeParsedAndDeparsed(namedWindowSqlString, true); |
| 103 | + } |
| 104 | + |
| 105 | + private WindowElement parseWindowElement(String sqlString, int selectItemIndex) |
| 106 | + throws JSQLParserException { |
| 107 | + PlainSelect plainSelect = (PlainSelect) CCJSqlParserUtil.parse(sqlString); |
| 108 | + Expression expression = plainSelect.getSelectItem(selectItemIndex).getExpression(); |
| 109 | + assertInstanceOf(AnalyticExpression.class, expression); |
| 110 | + AnalyticExpression analyticExpression = (AnalyticExpression) expression; |
| 111 | + WindowElement windowElement = analyticExpression.getWindowElement(); |
| 112 | + |
| 113 | + assertNotNull(windowElement); |
| 114 | + return windowElement; |
| 115 | + } |
41 | 116 | } |
0 commit comments