Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import net.sf.jsqlparser.expression.TranscodingFunction;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;
import net.sf.jsqlparser.statement.alter.AlterCollation;
import net.sf.jsqlparser.statement.alter.AlterRoutine;
import net.sf.jsqlparser.statement.alter.AlterTrigger;
Expand Down Expand Up @@ -171,6 +173,16 @@
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.TooManyMethods", "PMD.ExcessiveImports"})
public class StatementFeatureVisitor extends StatementVisitorAdapter<Void> {

/**
* ALTER TABLE actions that delete, swap or replace the rows of a table or partition. Like
* TRUNCATE and DROP they carry MODIFIES_DATA as well as MODIFIES_SCHEMA.
*/
private static final Set<AlterOperation> ALTER_ACTIONS_MODIFYING_DATA =
EnumSet.of(AlterOperation.DROP_PARTITION, AlterOperation.TRUNCATE_PARTITION,
AlterOperation.EXCHANGE_PARTITION, AlterOperation.DISCARD_PARTITION,
AlterOperation.DISCARD_TABLESPACE, AlterOperation.IMPORT_PARTITION,
AlterOperation.IMPORT_TABLESPACE);

private final Analysis analysis;

// ---- construction -----------------------------------------------------------------------
Expand Down Expand Up @@ -672,7 +684,25 @@ public <S> Void visit(PurgeStatement purgeStatement, S context) {

@Override
public <S> Void visit(Alter alter, S context) {
return schemaOnly();
schemaOnly();
if (alter.getAlterExpressions() == null) {
return null;
}
for (AlterExpression action : alter.getAlterExpressions()) {
AlterOperation operation = action.getOperation();
if (ALTER_ACTIONS_MODIFYING_DATA.contains(operation)) {
analysis.certain(StmtFeature.MODIFIES_DATA);
} else if (operation == AlterOperation.REPAIR_PARTITION) {
// like REPAIR TABLE, it may rewrite or discard rows
analysis.possible(StmtFeature.MODIFIES_DATA);
} else if (operation == AlterOperation.UNSPECIFIC) {
// an action the grammar does not model is kept as raw tokens and may write
// rows, e.g. ClickHouse's ALTER TABLE t DELETE WHERE ..
analysis.possible(StmtFeature.MODIFIES_DATA);
analysis.unresolved("unsupported");
}
}
return null;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/site/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ The features
- ``SELECT``, ``DELETE .. RETURNING``, ``SHOW``, ``DESCRIBE``, ``EXPLAIN``
* - ``MODIFIES_DATA``
- rows are written or destroyed
- ``INSERT``, ``UPDATE``, ``DELETE``, ``MERGE``, ``UPSERT``, ``TRUNCATE``, ``DROP``
- ``INSERT``, ``UPDATE``, ``DELETE``, ``MERGE``, ``UPSERT``, ``TRUNCATE``, ``DROP``, ``ALTER TABLE .. TRUNCATE PARTITION``
* - ``MODIFIES_SCHEMA``
- the catalogue changes
- ``CREATE``, ``ALTER``, ``DROP``, ``TRUNCATE``, ``GRANT``, ``COMMENT``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,45 @@ void destructiveDdlAlsoModifiesData() throws JSQLParserException {
}
}

/**
* The ALTER TABLE actions that delete, swap or replace rows are destructive DDL in the same
* sense: {@code TRUNCATE PARTITION ALL} empties a partitioned table just like TRUNCATE.
*/
@ParameterizedTest
@ValueSource(strings = {"ALTER TABLE t TRUNCATE PARTITION ALL",
"ALTER TABLE t TRUNCATE PARTITION p0", "ALTER TABLE t DROP PARTITION p0, p1",
"ALTER TABLE t ADD COLUMN c INT, DROP PARTITION p0",
"ALTER TABLE t EXCHANGE PARTITION p0 WITH TABLE u",
"ALTER TABLE t DISCARD TABLESPACE", "ALTER TABLE t DISCARD PARTITION p0 TABLESPACE",
"ALTER TABLE t IMPORT TABLESPACE", "ALTER TABLE t IMPORT PARTITION p0 TABLESPACE"})
void destructiveAlterActionsAlsoModifyData(String sql) throws JSQLParserException {
StatementFeatures f = analyse(sql);
assertThat(f.modifiesSchema()).isTrue();
assertThat(f.modifiesData()).isTrue();
assertThat(f.returnsResultSet()).isFalse();
}

/**
* An ALTER TABLE action the grammar does not model, such as ClickHouse's DELETE and UPDATE
* mutations, is kept as raw text, so a write cannot be excluded.
*/
@ParameterizedTest
@ValueSource(strings = {"ALTER TABLE t DELETE WHERE id > 0",
"ALTER TABLE t UPDATE c = 1 WHERE id > 0"})
void unmodelledAlterActionMayModifyData(String sql) throws JSQLParserException {
StatementFeatures f = analyse(sql);
assertThat(f.modifiesSchema()).isTrue();
assertThat(f.mayModifyData()).isTrue();
assertThat(f.getUnresolvedReferences()).containsExactly("unsupported");
}

@Test
void repairPartitionMayModifyDataLikeRepairTable() throws JSQLParserException {
StatementFeatures f = analyse("ALTER TABLE t REPAIR PARTITION p0");
assertThat(f.modifiesData()).isFalse();
assertThat(f.mayModifyData()).isTrue();
}

// ---- the cases a flag union gets wrong --------------------------------------------------

@Test
Expand Down
Loading