|
| 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.alter; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import net.sf.jsqlparser.JSQLParserException; |
| 16 | +import net.sf.jsqlparser.expression.Expression; |
| 17 | +import net.sf.jsqlparser.expression.LongValue; |
| 18 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 19 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 20 | +import net.sf.jsqlparser.statement.Statement; |
| 21 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | + |
| 26 | +class PostgreSqlColumnStatisticsTest { |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = {"SET STATISTICS DEFAULT", "SET STATISTICS -1", "SET STATISTICS 10000", |
| 29 | + "SET (n_distinct = -0.5)", "SET (n_distinct = -1, n_distinct_inherited = 20)", |
| 30 | + "RESET (n_distinct)", "RESET (n_distinct, n_distinct_inherited)"}) |
| 31 | + void tablesAndMaterializedViewsShareColumnActions(String action) throws JSQLParserException { |
| 32 | + for (String object : List.of("TABLE t", "MATERIALIZED VIEW mv")) { |
| 33 | + String sql = "ALTER " + object + " ALTER COLUMN id " + action; |
| 34 | + Statement statement = parse(sql); |
| 35 | + assertRoundTrip(statement); |
| 36 | + assertEquals(2, CCJSqlParserUtil.parseStatements(sql + "; SELECT 1", |
| 37 | + p -> p.withDialect(Dialect.POSTGRESQL)).size()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void defaultTargetsAndAttributeValuesAreMutable() throws JSQLParserException { |
| 43 | + AlterRelation index = |
| 44 | + (AlterRelation) parse("ALTER INDEX ix ALTER COLUMN 1 SET STATISTICS DEFAULT"); |
| 45 | + RelationAlterAction action = index.getActions().get(0); |
| 46 | + assertTrue(action.isStatisticsDefault()); |
| 47 | + assertNull(action.getStatistics()); |
| 48 | + action.setStatistics(200L); |
| 49 | + assertFalse(action.isStatisticsDefault()); |
| 50 | + assertEquals("ALTER INDEX ix ALTER COLUMN 1 SET STATISTICS 200", index.toString()); |
| 51 | + assertRoundTrip(index); |
| 52 | + action.setStatisticsDefault(true); |
| 53 | + assertRoundTrip(index); |
| 54 | + Alter table = (Alter) parse("ALTER TABLE t ALTER COLUMN id SET (n_distinct=10)"); |
| 55 | + action = (RelationAlterAction) table.getAlterExpressions().get(0); |
| 56 | + action.getOptions().get(0).setValue(new LongValue(20)); |
| 57 | + List<Expression> visited = new ArrayList<>(); |
| 58 | + action.visitExpressions(visited::add); |
| 59 | + assertEquals(List.of(new LongValue(20)), visited); |
| 60 | + assertEquals("ALTER TABLE t ALTER COLUMN id SET (n_distinct = 20)", table.toString()); |
| 61 | + assertRoundTrip(table); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void invalidTargetsAndCrossObjectActionsFail() { |
| 66 | + for (String sql : new String[] {"ALTER VIEW v ALTER COLUMN id SET STATISTICS DEFAULT", |
| 67 | + "ALTER INDEX ix ALTER COLUMN 1 SET (n_distinct=10)", |
| 68 | + "ALTER TABLE t ALTER COLUMN id SET (n_distinct)", |
| 69 | + "ALTER TABLE t ALTER COLUMN id SET STATISTICS 10001"}) { |
| 70 | + assertThrows(JSQLParserException.class, () -> parse(sql), sql); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static Statement parse(String sql) throws JSQLParserException { |
| 75 | + return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 76 | + } |
| 77 | + |
| 78 | + private static void assertRoundTrip(Statement statement) throws JSQLParserException { |
| 79 | + StringBuilder sql = new StringBuilder(); |
| 80 | + statement.accept(new StatementDeParser(sql), null); |
| 81 | + assertEquals(statement.toString(), sql.toString()); |
| 82 | + assertEquals(statement.toString(), parse(sql.toString()).toString()); |
| 83 | + } |
| 84 | +} |
0 commit comments