|
| 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.statement.select; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Set; |
| 20 | +import net.sf.jsqlparser.JSQLParserException; |
| 21 | +import net.sf.jsqlparser.expression.Function; |
| 22 | +import net.sf.jsqlparser.expression.LongValue; |
| 23 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 24 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 25 | +import net.sf.jsqlparser.statement.create.table.ColDataType; |
| 26 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 27 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 28 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 29 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 30 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.CsvFileSource; |
| 34 | + |
| 35 | +class RowsFromColumnDefinitionTest { |
| 36 | + |
| 37 | + // Accepted/rejected cases are data records, not executable SQL batch scripts. |
| 38 | + // Both outcomes were checked against PostgreSQL 18.6. |
| 39 | + @ParameterizedTest |
| 40 | + @CsvFileSource(resources = "rows-from-cases.tsv", delimiter = '\t') |
| 41 | + void preservesValidDefinitionsAndRejectsMalformedLists(boolean valid, String sql) |
| 42 | + throws JSQLParserException { |
| 43 | + if (!valid) { |
| 44 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 45 | + return; |
| 46 | + } |
| 47 | + PlainSelect select = parse(sql); |
| 48 | + StringBuilder visitor = new StringBuilder(); |
| 49 | + select.accept(new StatementDeParser(visitor)); |
| 50 | + for (String rendered : List.of(select.toString(), visitor.toString())) { |
| 51 | + PlainSelect reparsed = parse(rendered); |
| 52 | + assertEquals(select.toString(), reparsed.toString()); |
| 53 | + assertEquals(select.getFromItem().getClass(), reparsed.getFromItem().getClass()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void eachFunctionOwnsItsDefinitionsIndependentlyOfTheOuterAlias() |
| 59 | + throws JSQLParserException { |
| 60 | + PlainSelect select = parse("SELECT * FROM ROWS FROM (" |
| 61 | + + "json_to_record('{}') AS (amount numeric(10, 2), label text COLLATE \"C\"), " |
| 62 | + + "generate_series(1, 2), json_to_record('{}') AS (items integer[])) " |
| 63 | + + "WITH ORDINALITY AS r(total, name, n, data, ord)"); |
| 64 | + TableFunction table = (TableFunction) select.getFromItem(); |
| 65 | + assertTrue(table.isRowsFrom()); |
| 66 | + assertEquals(3, table.getRowsFromFunctions().size()); |
| 67 | + assertEquals("ORDINALITY", table.getWithClause()); |
| 68 | + assertEquals(5, table.getAlias().getAliasColumns().size()); |
| 69 | + List<ColumnDefinition> first = table.getFunctions().get(0).getResultColumnDefinitions(); |
| 70 | + assertEquals("amount", first.get(0).getColumnName()); |
| 71 | + assertEquals(10, first.get(0).getColDataType().getPrecision()); |
| 72 | + assertEquals(2, first.get(0).getColDataType().getScale()); |
| 73 | + assertEquals("\"C\"", first.get(1).getColumnOptions().get(0).getCollation()); |
| 74 | + assertNull(table.getFunctions().get(1).getResultColumnDefinitions()); |
| 75 | + assertEquals(1, table.getFunctions().get(2).getResultColumnDefinitions().get(0) |
| 76 | + .getColDataType().getArrayData().size()); |
| 77 | + |
| 78 | + first.get(0).setColumnName("changed"); |
| 79 | + first.get(0).setColDataType(new ColDataType("bigint")); |
| 80 | + TableFunction reparsed = (TableFunction) parse(select.toString()).getFromItem(); |
| 81 | + assertEquals("changed", reparsed.getFunctions().get(0).getResultColumnDefinitions() |
| 82 | + .get(0).getColumnName()); |
| 83 | + assertEquals("bigint", reparsed.getFunctions().get(0).getResultColumnDefinitions() |
| 84 | + .get(0).getColDataType().getDataType()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void legacyFunctionListEditsKeepDefinitionsAttachedToTheirFunction() |
| 89 | + throws JSQLParserException { |
| 90 | + TableFunction table = (TableFunction) parse("SELECT * FROM ROWS FROM (" |
| 91 | + + "json_to_record('{}') AS (a integer), generate_series(1, 2))").getFromItem(); |
| 92 | + Function record = table.getRowsFromFunctions().remove(0); |
| 93 | + table.getRowsFromFunctions().add(record); |
| 94 | + TableFunction reparsed = (TableFunction) parse("SELECT * FROM " + table).getFromItem(); |
| 95 | + assertNull(reparsed.getFunctions().get(0).getResultColumnDefinitions()); |
| 96 | + assertNotNull(reparsed.getFunctions().get(1).getResultColumnDefinitions()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void tableFunctionRendererVisitsFunctionArguments() throws JSQLParserException { |
| 101 | + PlainSelect select = parse("SELECT * FROM ROWS FROM (json_to_record('{}') AS (a integer), " |
| 102 | + + "generate_series(1, 2)) WITH ORDINALITY AS r(a, n, ord)"); |
| 103 | + String original = select.toString(); |
| 104 | + StringBuilder sql = new StringBuilder(); |
| 105 | + ExpressionDeParser expressions = new ExpressionDeParser() { |
| 106 | + @Override |
| 107 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 108 | + return getBuilder().append(value.getValue() + 10); |
| 109 | + } |
| 110 | + }; |
| 111 | + select.accept(new StatementDeParser(expressions, new SelectDeParser(), sql)); |
| 112 | + assertTrue(sql.toString().contains("generate_series(11, 12)")); |
| 113 | + assertTrue(sql.toString().contains("AS (a integer)")); |
| 114 | + assertEquals(original, select.toString()); |
| 115 | + assertEquals(sql.toString(), parse(sql.toString()).toString()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void tableDiscoveryStillVisitsSubqueriesInRecordFunctionArguments() |
| 120 | + throws JSQLParserException { |
| 121 | + String sql = "SELECT * FROM ROWS FROM (" |
| 122 | + + "json_to_record((SELECT payload FROM events)) AS (a integer))"; |
| 123 | + assertEquals(Set.of("events"), TablesNamesFinder.findTables(sql)); |
| 124 | + } |
| 125 | + |
| 126 | + private static PlainSelect parse(String sql) throws JSQLParserException { |
| 127 | + return (PlainSelect) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 128 | + } |
| 129 | +} |
0 commit comments