Skip to content

Commit 5b5fe6c

Browse files
authored
Fix nested PostgreSQL composite field access after cast (#2404)
1 parent 126c1a1 commit 5b5fe6c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7009,8 +7009,8 @@ Expression PrimaryExpression() #PrimaryExpression:
70097009
}
70107010

70117011

7012-
// RowGet Expression
7013-
[ LOOKAHEAD(2) "." tmp=RelObjectNameExt() { retval = new RowGetExpression(retval, tmp); }]
7012+
// RowGet Expressions
7013+
( LOOKAHEAD(2) "." tmp=RelObjectNameExt() { retval = new RowGetExpression(retval, tmp); } )*
70147014
)
70157015
)
70167016

src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
*/
1010
package net.sf.jsqlparser.expression;
1111

12+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
13+
1214
import net.sf.jsqlparser.JSQLParserException;
15+
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
1316
import net.sf.jsqlparser.statement.select.PlainSelect;
1417
import net.sf.jsqlparser.test.TestUtils;
1518
import org.junit.jupiter.api.Assertions;
1619
import org.junit.jupiter.api.Test;
1720

18-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
19-
2021
/**
2122
*
2223
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
@@ -114,4 +115,29 @@ void testDateTimeCast() throws JSQLParserException {
114115
+ "as time_tstz;";
115116
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
116117
}
118+
119+
@Test
120+
void testNestedCompositeTypeCastIssue2341() throws JSQLParserException {
121+
String sqlStr = "SELECT\n"
122+
+ " (product_data::product_info_similarity).info.category AS category,\n"
123+
+ " COUNT(*) AS num_products\n"
124+
+ "FROM products\n"
125+
+ "GROUP BY (product_data::product_info_similarity).info.category;";
126+
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
127+
128+
RowGetExpression categoryAccess =
129+
Assertions.assertInstanceOf(RowGetExpression.class,
130+
select.getSelectItem(0).getExpression());
131+
Assertions.assertEquals("category", categoryAccess.getColumnName());
132+
133+
RowGetExpression infoAccess = Assertions.assertInstanceOf(RowGetExpression.class,
134+
categoryAccess.getExpression());
135+
Assertions.assertEquals("info", infoAccess.getColumnName());
136+
137+
ParenthesedExpressionList<?> parenthesedCast =
138+
Assertions.assertInstanceOf(ParenthesedExpressionList.class,
139+
infoAccess.getExpression());
140+
Assertions.assertEquals(1, parenthesedCast.size());
141+
Assertions.assertInstanceOf(CastExpression.class, parenthesedCast.get(0));
142+
}
117143
}

0 commit comments

Comments
 (0)