|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.expression; |
| 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.assertThrows; |
| 15 | + |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 18 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 19 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 20 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 21 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 22 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | + |
| 27 | +class OrderedSetFilterTest { |
| 28 | + @ParameterizedTest |
| 29 | + @ValueSource(strings = { |
| 30 | + "percentile_cont(0.5) WITHIN GROUP (ORDER BY score) FILTER (WHERE active)", |
| 31 | + "percentile_disc(0.5) WITHIN GROUP (ORDER BY score DESC NULLS LAST) " |
| 32 | + + "FILTER (WHERE active AND score > 0)", |
| 33 | + "mode() WITHIN GROUP (ORDER BY score) FILTER (WHERE active)", |
| 34 | + "rank(5) WITHIN GROUP (ORDER BY score) FILTER (WHERE NOT active)", |
| 35 | + "sum(score) FILTER (WHERE active)", |
| 36 | + "sum(score) FILTER (WHERE active) OVER (PARTITION BY team)", |
| 37 | + "percentile_cont(0.5) WITHIN GROUP (ORDER BY score)" |
| 38 | + }) |
| 39 | + void preservesAggregateClauses(String expression) throws JSQLParserException { |
| 40 | + String sql = "SELECT " + expression + " FROM measurements"; |
| 41 | + PlainSelect select = parse(sql); |
| 42 | + StringBuilder output = new StringBuilder(); |
| 43 | + select.accept(new StatementDeParser(output), null); |
| 44 | + assertEquals(sql, select.toString().replace(" )", ")")); |
| 45 | + assertEquals(sql, output.toString().replace(" )", ")")); |
| 46 | + assertEquals(select.toString(), parse(output.toString()).toString()); |
| 47 | + assertEquals(select.toString(), CCJSqlParserUtil.parse(sql).toString()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void visitsBothOrderingAndFilterExpressions() throws JSQLParserException { |
| 52 | + PlainSelect select = parse("SELECT rank(5) WITHIN GROUP (ORDER BY score + 1) " |
| 53 | + + "FILTER (WHERE score > 2) FROM measurements"); |
| 54 | + AnalyticExpression aggregate = assertInstanceOf(AnalyticExpression.class, |
| 55 | + select.getSelectItem(0).getExpression()); |
| 56 | + assertEquals(AnalyticType.WITHIN_GROUP, aggregate.getType()); |
| 57 | + assertEquals("score > 2", aggregate.getFilterExpression().toString()); |
| 58 | + StringBuilder output = new StringBuilder(); |
| 59 | + ExpressionDeParser expressions = new ExpressionDeParser() { |
| 60 | + @Override |
| 61 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 62 | + return getBuilder().append(value.getValue() + 10); |
| 63 | + } |
| 64 | + }; |
| 65 | + select.accept(new StatementDeParser(expressions, new SelectDeParser(), output), null); |
| 66 | + String expected = "SELECT rank(15) WITHIN GROUP (ORDER BY score + 11) " |
| 67 | + + "FILTER (WHERE score > 12) FROM measurements"; |
| 68 | + assertEquals(expected, output.toString()); |
| 69 | + assertEquals(expected, parse(output.toString()).toString()); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @ValueSource(strings = { |
| 74 | + "percentile_cont(0.5) FILTER (WHERE active) WITHIN GROUP (ORDER BY score)", |
| 75 | + "percentile_cont(0.5) WITHIN GROUP (ORDER BY score) FILTER (active)", |
| 76 | + "percentile_cont(0.5) WITHIN GROUP (ORDER BY score) FILTER (WHERE)", |
| 77 | + "percentile_cont(0.5) WITHIN GROUP (ORDER BY score) " |
| 78 | + + "FILTER (WHERE active) FILTER (WHERE active)", |
| 79 | + "sum(score) OVER (PARTITION BY team) FILTER (WHERE active)" |
| 80 | + }) |
| 81 | + void rejectsInvalidPostgresFilterPlacement(String expression) { |
| 82 | + assertThrows(JSQLParserException.class, |
| 83 | + () -> parse("SELECT " + expression + " FROM measurements")); |
| 84 | + } |
| 85 | + |
| 86 | + private static PlainSelect parse(String sql) throws JSQLParserException { |
| 87 | + return (PlainSelect) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 88 | + } |
| 89 | +} |
0 commit comments