|
| 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.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import net.sf.jsqlparser.JSQLParserException; |
| 21 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 22 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 23 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 24 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 25 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.CsvFileSource; |
| 29 | +import org.junit.jupiter.params.provider.EnumSource; |
| 30 | +import org.junit.jupiter.params.provider.ValueSource; |
| 31 | + |
| 32 | +class LambdaRoundTripTest { |
| 33 | + |
| 34 | + @ParameterizedTest |
| 35 | + @ValueSource(strings = {"SELECT arrayMap((x) -> x * 2, [1, 2])", |
| 36 | + "SELECT list_transform([1, 2], (x) -> x + 1)", |
| 37 | + "SELECT f(1, [1, 2], (x) -> CASE WHEN x > 1 THEN x ELSE 0 END)", |
| 38 | + "SELECT arrayMap((x, y) -> x + y, [1], [2])", |
| 39 | + "SELECT list_transform([1], (x) -> list_transform([2], (y) -> x + y))"}) |
| 40 | + void preservesParameterGroupingAndNestedAst(String sql) throws JSQLParserException { |
| 41 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sql); |
| 42 | + List<LambdaExpression> before = lambdas(select); |
| 43 | + assertFalse(before.isEmpty()); |
| 44 | + assertTrue(before.stream().allMatch(LambdaExpression::isParenthesized)); |
| 45 | + StringBuilder visitorSql = new StringBuilder(); |
| 46 | + select.accept(new StatementDeParser(visitorSql)); |
| 47 | + for (String rendered : List.of(select.toString(), visitorSql.toString())) { |
| 48 | + PlainSelect reparsed = (PlainSelect) CCJSqlParserUtil.parse(rendered); |
| 49 | + List<LambdaExpression> after = lambdas(reparsed); |
| 50 | + assertEquals(before.size(), after.size(), rendered); |
| 51 | + for (int i = 0; i < before.size(); i++) { |
| 52 | + assertEquals(before.get(i).getIdentifiers(), after.get(i).getIdentifiers()); |
| 53 | + assertEquals(before.get(i).getExpression().getClass(), |
| 54 | + after.get(i).getExpression().getClass()); |
| 55 | + assertTrue(after.get(i).isParenthesized()); |
| 56 | + } |
| 57 | + assertEquals(select.toString(), reparsed.toString()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void keepsUnparenthesizedSingleParameterRendering() throws JSQLParserException { |
| 63 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse( |
| 64 | + "SELECT list_transform([1], x -> x + 1)"); |
| 65 | + assertFalse(lambdas(select).get(0).isParenthesized()); |
| 66 | + assertEquals("SELECT list_transform([1], x -> x + 1)", select.toString()); |
| 67 | + LambdaExpression constructed = new LambdaExpression("x", new LongValue(1)); |
| 68 | + assertEquals("x -> 1", constructed.toString()); |
| 69 | + constructed.setParenthesized(true); |
| 70 | + assertEquals("( x ) -> 1", constructed.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void sharedRendererStillVisitsAndRewritesLambdaBody() throws JSQLParserException { |
| 75 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse( |
| 76 | + "SELECT arrayMap((x) -> x + 1, [2])"); |
| 77 | + StringBuilder sql = new StringBuilder(); |
| 78 | + ExpressionDeParser expressions = new ExpressionDeParser() { |
| 79 | + @Override |
| 80 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 81 | + return getBuilder().append(value.getValue() + 10); |
| 82 | + } |
| 83 | + }; |
| 84 | + select.accept(new StatementDeParser(expressions, |
| 85 | + new net.sf.jsqlparser.util.deparser.SelectDeParser(), sql)); |
| 86 | + assertEquals("SELECT arrayMap(( x ) -> x + 11, [12])", sql.toString()); |
| 87 | + assertEquals("SELECT arrayMap(( x ) -> x + 1, [2])", select.toString()); |
| 88 | + assertEquals(1, lambdas((PlainSelect) CCJSqlParserUtil.parse(sql.toString())).size()); |
| 89 | + } |
| 90 | + |
| 91 | + // All fixture statements execute on PostgreSQL 18.6 or MySQL 8.4.11, respectively. |
| 92 | + @ParameterizedTest |
| 93 | + @CsvFileSource(resources = "/net/sf/jsqlparser/expression/arrow-dialect-cases.tsv", |
| 94 | + delimiter = '\t') |
| 95 | + void jsonArrowRemainsJsonInEveryArgumentPosition(Dialect dialect, String sql) |
| 96 | + throws JSQLParserException { |
| 97 | + for (boolean complex : List.of(false, true)) { |
| 98 | + PlainSelect select = parseJson(sql, dialect, complex); |
| 99 | + StringBuilder visitorSql = new StringBuilder(); |
| 100 | + select.accept(new StatementDeParser(visitorSql)); |
| 101 | + for (String rendered : List.of(sql, select.toString(), visitorSql.toString())) { |
| 102 | + PlainSelect reparsed = parseJson(rendered, dialect, complex); |
| 103 | + assertTrue(lambdas(reparsed).isEmpty(), rendered); |
| 104 | + Function function = assertInstanceOf(Function.class, |
| 105 | + reparsed.getSelectItem(0).getExpression()); |
| 106 | + assertTrue(function.getParameters().stream() |
| 107 | + .anyMatch(JsonExpression.class::isInstance), rendered); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @ParameterizedTest |
| 113 | + @EnumSource(value = Dialect.class, names = {"MYSQL", "POSTGRESQL"}) |
| 114 | + void rejectsMissingJsonOperand(Dialect dialect) { |
| 115 | + assertThrows(JSQLParserException.class, |
| 116 | + () -> parseJson("SELECT COALESCE(NULL, payload ->)", dialect, true)); |
| 117 | + } |
| 118 | + |
| 119 | + private static PlainSelect parseJson(String sql, Dialect dialect, boolean complex) |
| 120 | + throws JSQLParserException { |
| 121 | + return (PlainSelect) CCJSqlParserUtil.parse(sql, |
| 122 | + parser -> parser.withDialect(dialect).withAllowComplexParsing(complex)); |
| 123 | + } |
| 124 | + |
| 125 | + private static List<LambdaExpression> lambdas(PlainSelect select) { |
| 126 | + List<LambdaExpression> result = new ArrayList<>(); |
| 127 | + select.getSelectItem(0).getExpression().accept(new ExpressionVisitorAdapter<Void>() { |
| 128 | + @Override |
| 129 | + public <S> Void visit(LambdaExpression expression, S context) { |
| 130 | + result.add(expression); |
| 131 | + return super.visit(expression, context); |
| 132 | + } |
| 133 | + }); |
| 134 | + return result; |
| 135 | + } |
| 136 | +} |
0 commit comments