Skip to content

Separate index and constraint ASTs (breaking API change) - #2747

Open
minleejae wants to merge 4 commits into
JSQLParser:masterfrom
minleejae:refactor/constraint-inspection-20261002
Open

minleejae wants to merge 4 commits into
JSQLParser:masterfrom
minleejae:refactor/constraint-inspection-20261002

Conversation

@minleejae

@minleejae minleejae commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Make indexes and logical constraints distinct AST concepts so callers can inspect and edit CREATE TABLE and ALTER TABLE definitions without treating every constraint as an index. Preserve declared SQL and share only the components the definitions actually have in common.

Current design (As-is)

Before this change, table constraints inherit Index. CHECK and DEFAULT therefore expose irrelevant key/index options, and getIndexes() mixes physical index declarations with PRIMARY KEY, UNIQUE, FOREIGN KEY and other constraints. Inline constraints and ALTER payloads require different inspection paths, while some editing helpers retain duplicate state.

Target design (To-be)

This PR implements the following refactoring:

  • Separate Index from an independent abstract NamedConstraint base with ConstraintKind. PRIMARY KEY/UNIQUE use KeyConstraint; ForeignKeyIndex becomes ForeignKeyConstraint. Index.Kind contains only index classifications.
  • Share KeyElement, IndexOption and IndexOptions through composition where appropriate. CHECK, DEFAULT and NOT NULL have no irrelevant key/index fields. ConstraintUsingIndex references an existing index without manufacturing a backing index or key list.
  • Give CREATE TABLE separate mutable getIndexes() and getTableConstraints() views. getConstraints() combines inline/table declarations in source order and retains editable source nodes and owning columns.
  • Give ALTER separate constraint/index accessors and canonical key definitions for PK/UK editing. Constraint renames expose source/target names. Named DROP CONSTRAINT does not guess its target's kind. Ordinary column DEFAULT remains an option; explicit DefaultConstraint nodes remain constraints.
  • Adapt visitors, validators and both SQL renderers. Preserve active ALTER payload precedence, builder declarations and permissive index suffix order. MySQL table UNIQUE KEY/INDEX remain constraints; standalone CREATE UNIQUE INDEX remains an index statement.

This is a breaking API change. usage.rst documents replacement types/accessors, extracted shared components and CREATE/ALTER migration examples. The refactoring does not infer catalogue objects or implicit supporting indexes.

Validation

  • At 00a8400c, all checks pass: Codacy (zero new issues), grammar conflicts, Gradle Check and Maven Verify on Linux, macOS and Windows.
  • Fresh local Java 17 Gradle check: 9,476 tests, 9,451 passed, 25 skipped, no failures. Checkstyle, PMD, SpotBugs, Spotless and JaCoCo coverage verification pass with cached dependencies and the Spotless baseline set to upstream/master.
  • The latest build reparses 882 DDL fixtures previously executed on MySQL 8.4.11/PostgreSQL 18.6. All 875 supported cases preserve AST inspection, both SQL renderers and reparsing; seven baseline parse gaps are unchanged. No fresh live database replay is claimed.

Signed-off-by: minleejae <mmj9808@gmail.com>
Signed-off-by: minleejae <mmj9808@gmail.com>
Signed-off-by: minleejae <mmj9808@gmail.com>
@minleejae minleejae changed the title Expose unified constraint inspection for CREATE and ALTER Separate index and constraint ASTs (breaking API change) Oct 2, 2026
Signed-off-by: minleejae <mmj9808@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant