Skip to content

Commit a0657af

Browse files
committed
fix: support nested parametric data types as CAST target
ClickHouse allows parametric (constructor-style) data types as a CAST target, including nested ones such as Nullable(Decimal(10, 2)). Parsing failed because the argument production of ColDataType only accepted atomic tokens (numbers, string literals, bare identifiers/keywords), so the inner opening bracket of a nested parametric type was unexpected. Allow an argument that is itself a parametric data type (a DATA_TYPE or identifier immediately followed by an argument list) to be parsed as a nested ColDataType, recursing through the existing production. The nested type is rendered back via its toString() so the statement round-trips. Fixes #2441 Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent f0ba123 commit a0657af

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10707,6 +10707,7 @@ ColDataType ColDataType():
1070710707
List<Integer> array = new ArrayList<Integer>();
1070810708
List<String> name;
1070910709
ColDataType arrayType;
10710+
ColDataType nestedType = null;
1071010711

1071110712
int precision = -1;
1071210713
int scale = -1;
@@ -10755,6 +10756,9 @@ ColDataType ColDataType():
1075510756
[
1075610757
LOOKAHEAD(2) "(" {tk2 =null;}
1075710758
(
10759+
(
10760+
LOOKAHEAD((<DATA_TYPE> | <S_IDENTIFIER>) "(") nestedType=ColDataType()
10761+
|
1075810762
(
1075910763
(
1076010764
( tk=<S_LONG> | tk=<K_MAX> ) [ LOOKAHEAD(2) (tk2=<K_BYTE> | tk2=<K_CHAR>) ]
@@ -10765,9 +10769,15 @@ ColDataType ColDataType():
1076510769
tk=<S_IDENTIFIER>
1076610770
|
1076710771
tk=<K_CHAR>
10772+
)
1076810773
)
1076910774
{
10770-
argumentsStringList.add(tk.image + (tk2!=null?" " + tk2.image:""));
10775+
if (nestedType != null) {
10776+
argumentsStringList.add(nestedType.toString());
10777+
nestedType = null;
10778+
} else {
10779+
argumentsStringList.add(tk.image + (tk2!=null?" " + tk2.image:""));
10780+
}
1077110781
}
1077210782

1077310783
[ "," ]

src/test/java/net/sf/jsqlparser/statement/select/ClickHouseTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ public void testMultipleSettingsClauseIssue2362() throws JSQLParserException {
140140
Assertions.assertNotNull(select.getSettings());
141141
Assertions.assertEquals(2, select.getSettings().size());
142142
}
143+
144+
@Test
145+
public void testCastToNestedParametricTypeIssue2441() throws JSQLParserException {
146+
// ClickHouse allows parametric (constructor-style) data types as a CAST target,
147+
// including nested ones such as Nullable(Decimal(p, s)).
148+
String sql = "SELECT CAST(x AS Nullable(Decimal(10, 2))) FROM cast_demo";
149+
assertSqlCanBeParsedAndDeparsed(sql, true);
150+
151+
// The inner parametric type may itself be wrapped by another parametric type.
152+
sql = "SELECT CAST(x AS LowCardinality(Decimal(10, 2))) FROM cast_demo";
153+
assertSqlCanBeParsedAndDeparsed(sql, true);
154+
}
143155
}

0 commit comments

Comments
 (0)