Skip to content

Read data types as a name and arguments, with one accessor for each part - #2975

Open
minleejae wants to merge 7 commits into
JSQLParser:masterfrom
minleejae:refactor/type-model-20261204
Open

minleejae wants to merge 7 commits into
JSQLParser:masterfrom
minleejae:refactor/type-model-20261204

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

ColDataType read some types as a name with arguments and folded the arguments of others into the name. MEDIUMINT(9) had the name mediumint and the arguments [9], while VARCHAR(10) had the name VARCHAR (10) and no arguments. The difference comes from 5.0, when the DataType() production for implicit casts started to read column types as well; 4.9 read both as a name and arguments. A caller had to strip the name itself, and CastExpression.isOf() returned UNKNOWN for CAST(x AS VARCHAR(10)). Some accessors also gave the same information twice, and some setters changed a value that toString() did not show.

This PR reads the arguments of every type in one production and keeps one accessor for each part of a type. The duplicate API is removed rather than deprecated.

What changes

  • getDataType() returns only the name, and getArgumentsStringList() returns the arguments as written. VARCHAR(10) is VARCHAR with [10], and TIMESTAMP(3) WITH TIME ZONE is TIMESTAMP WITH TIME ZONE with [3]. toString() output is unchanged.
  • getPrecision() and getScale() are read from the arguments. MAX, * and numbers outside the int range return null and stay in the argument list.
  • getSignedness() and isZerofill() are read from getTypeModifiers().
  • Oracle's CHAR(10 BYTE) keeps BYTE, MySQL's CHAR BYTE is read as the binary character set, and MySQL's CAST(x AS UNSIGNED ARRAY) is an array of UNSIGNED instead of a type named UNSIGNED ARRAY.
  • CastExpression constructors take the type as a ColDataType instead of a string.

API changes

Removed Use instead
getBaseTypeName(), getNumericPrecision(), setNumericPrecision(), isMaxPrecision(), fromNumericParameters(), fromTypeParameters(), isUseCharsetKeyword(), setUseCharsetKeyword() (added after 5.4) getDataType(), getArgumentsStringList(), getPrecision(), getCharacterSetSyntax()
setPrecision(), withPrecision(), setScale(), withScale() (5.4) setArgumentsStringList()
setSignedness(), withSignedness(), setZerofill(), withZerofill(), isNational() (5.4) setTypeModifiers(), addTypeModifier(), getNationalCharacterType()
ColDataType(String, int, int) (5.0) new ColDataType(name).addArgumentsStringList(...)
CastExpression constructors with a type string The same constructors with a ColDataType

Code written against 5.0–5.4 that expects getDataType() to return VARCHAR (10) will need to change. usage.rst has a migration table. If these changes should wait for a major release, the first three commits don't change any released API and can be merged on their own.

Validation

  • type-corpus.tsv records what ColDataType returns for MySQL 8.4 and PostgreSQL 18 type spellings, the spellings the servers return for the same columns (COLUMN_TYPE and format_type), and a few spellings from other dialects. In the name and argument commit, only the name and arguments change, except for MAX arguments, CHAR(10 BYTE) and VARCHAR2(10 CHAR). All 343 rows now parse; before this PR, CHAR BYTE was the only failure.
  • Every commit passes gradle check and checkGrammarAmbiguity. The last commit ran 19,130 tests with 0 failures and no grammar choice conflicts. spotlessJavaCheck and spotlessMiscCheck report CCJSqlParserTest.java and CHANGELOG.md, which fail the same way on master.

Prepared with Claude Code.

…8 spellings

ColDataTypeCorpusTest reads type-corpus.tsv and checks the values ColDataType
exposes for each spelling: name, arguments, precision, scale, modifiers, array,
character set, interval qualifier, national type, column specs and toString.

The MySQL and PostgreSQL rows hold the type spellings of MySQL 8.4 and
PostgreSQL 18 in a column definition, and the spelling each server reports for
that column (COLUMN_TYPE, format_type) as a type fragment. A few rows cover
other dialects. The table records the current behaviour, so a change in how a
type is read shows up as changed rows.

Signed-off-by: minleejae <mmj9808@gmail.com>
ColDataType gave the spelling of its character set clause twice:
getCharacterSetSyntax() names every spelling (CHARACTER SET, CHARSET, ASCII,
UNICODE), and isUseCharsetKeyword()/setUseCharsetKeyword() repeated the CHARSET
case as a boolean. The boolean pair was added after 5.4 and is not released, so
it is removed; the parser and the tests use the enum.

Signed-off-by: minleejae <mmj9808@gmail.com>
MySQL takes BYTE where it takes ASCII and UNICODE (opt_charset_with_opt_binary in
sql_yacc.yy): CHAR BYTE is CHAR CHARACTER SET binary, which the server stores as
binary(1). With the MySQL dialect the parser now reads it as the shorthand
CharacterSetSyntax.BYTE, whose character set is binary, as it already does for
ASCII and UNICODE. CHAR BYTE was the only spelling of the MySQL 8.4 corpus that
did not parse.

Signed-off-by: minleejae <mmj9808@gmail.com>
ColDataType gave VARCHAR(10) as the name "VARCHAR (10)" without arguments, but
MEDIUMINT(9), YEAR(4), DEC(5), INTERVAL(3) and other names that are not
DATA_TYPE tokens as the name "mediumint" with the arguments [9]. Since 5.0 the
DataType() production, written for implicit casts, folded the arguments of a
DATA_TYPE name into the name; 4.9 read every type as a name and arguments. A
caller had to know which path a type took, and CastExpression.isOf() gave
UNKNOWN for CAST(x AS VARCHAR(10)) because it compares the name.

One production, TypeArguments(), now reads the arguments of every type, and
getDataType() is the name alone:
- VARCHAR(10) is VARCHAR with [10]. TIMESTAMP(3) WITH TIME ZONE is TIMESTAMP
  WITH TIME ZONE with [3], and toString() writes the precision after the first
  word as before.
- getPrecision() and getScale() read the arguments, so they agree with them and
  with toString(). MAX is an argument, not Integer.MAX_VALUE.
- The length unit of CHAR(10 BYTE) is kept instead of being dropped.
- Arguments are separated by commas, as DataType() required, and * stands only
  in VECTOR or as the precision of NUMBER.
- MySQL's CAST(x AS UNSIGNED ARRAY) keeps ARRAY as the array spelling of the
  type, as PostgreSQL's INT ARRAY does, instead of a word of the name.

Removed, as they read or wrote the folded name or set a precision toString()
did not show: getBaseTypeName(), getNumericPrecision(), setNumericPrecision(),
isMaxPrecision(), fromNumericParameters(), fromTypeParameters() (added after
5.4), setPrecision(), withPrecision(), setScale(), withScale() (5.4) and
ColDataType(String, int, int) (5.0). A type is built with
new ColDataType(name).addArgumentsStringList(...).

In type-corpus.tsv only getDataType() and the arguments change, except for
VARCHAR(MAX) (no precision, MAX as written), CHAR(10 BYTE) (BYTE kept) and
VARCHAR2(10 CHAR) (precision 10).

Signed-off-by: minleejae <mmj9808@gmail.com>
ColDataType kept MySQL's numeric modifiers twice: getTypeModifiers() in the
order written, and signedness and ZEROFILL fields beside it. setSignedness()
and setZerofill() changed the fields but not the list, and toString() writes
the list, so INT(11) UNSIGNED with setSignedness(SIGNED) answered SIGNED and
printed UNSIGNED.

getSignedness() and isZerofill() now read the list: the last of SIGNED and
UNSIGNED, and whether ZEROFILL is in it. setSignedness(), withSignedness(),
setZerofill() and withZerofill() are removed, along with isNational(), which
repeated getNationalCharacterType() != null. All five came with 5.4 and have no
caller in the parser or the tests.

Signed-off-by: minleejae <mmj9808@gmail.com>
Three constructors took the type as a string and stored it whole as the name
of a ColDataType, so new CastExpression(a, "VARCHAR(10)") had the name
"VARCHAR(10)" with no arguments, unlike a parsed CAST.

CastExpression(String keyword, Expression, String) and
CastExpression(Expression, String) now take a ColDataType, and
CastExpression(String dataType, String value) is removed in favour of
CastExpression(ColDataType, String). None of them had a caller in the parser
or the tests.

Signed-off-by: minleejae <mmj9808@gmail.com>
usage.rst gets a "Column data types" section: the accessor for each part of a
ColDataType, an example, and a table from the API this branch removes or
changes to its replacement. The paragraph on negative scales builds the type
with its arguments instead of the removed factory and constructor.

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