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
8 changes: 7 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -9946,7 +9946,13 @@ CastExpression CastExpression():
")"
)
|
type=ColDataType() { retval.setColDataType(type); }
(
type=ColDataType() { retval.setColDataType(type); }
// MySQL casts to an array of the given type when a multi-valued index key part is
// defined, e.g. CAST(data->'$.zips' AS UNSIGNED ARRAY). ARRAY reads as a further
// word of the type, the way INT UNSIGNED already does.
[ <K_ARRAY_LITERAL> { type.setDataType(type.getDataType() + " ARRAY"); } ]
)
)

// BigQuery FORMAT clause
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/CastExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -155,4 +156,18 @@ void testNestedCompositeTypeCastIssue2341() throws JSQLParserException {
Assertions.assertEquals(1, parenthesedCast.size());
Assertions.assertInstanceOf(CastExpression.class, parenthesedCast.get(0));
}

@Test
public void testCastToArrayIssue2490() throws JSQLParserException {
// MySQL casts to an array of the given type when a multi-valued index key part is
// defined, and accepts the same cast anywhere else an expression is allowed.
assertSqlCanBeParsedAndDeparsed("SELECT CAST(data -> '$.zips' AS UNSIGNED ARRAY) FROM t");
assertSqlCanBeParsedAndDeparsed("SELECT CAST(x AS CHAR (10) ARRAY) FROM t");

PlainSelect select = (PlainSelect) CCJSqlParserUtil
.parse("SELECT CAST(data -> '$.zips' AS UNSIGNED ARRAY) FROM t");
CastExpression cast = Assertions.assertInstanceOf(CastExpression.class,
select.getSelectItem(0).getExpression());
Assertions.assertEquals("UNSIGNED ARRAY", cast.getColDataType().getDataType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2414,4 +2414,10 @@ public void testAlterTableAddIndexKeyPartWithPrefixLengthAndDirectionIssue2490()
assertSqlCanBeParsedAndDeparsed("ALTER TABLE t ADD INDEX i33 (c1 (20) ASC)");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE t ADD UNIQUE INDEX i34 (c1 (10) DESC)");
}

@Test
public void testAlterTableAddMultiValuedIndexIssue2490() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"ALTER TABLE t ADD INDEX i31 ((CAST(data -> '$.zips' AS UNSIGNED ARRAY)))");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,10 @@ public void testCreateFullTextAndSpatialIndexIssue2490() throws JSQLParserExcept
assertSqlCanBeParsedAndDeparsed("CREATE FULLTEXT INDEX i18 ON t (body) WITH PARSER ngram");
assertSqlCanBeParsedAndDeparsed("CREATE SPATIAL INDEX i19 ON t (g)");
}

@Test
public void testCreateMultiValuedIndexIssue2490() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"CREATE INDEX i20 ON t ((CAST(data -> '$.zips' AS UNSIGNED ARRAY)))");
}
}
Loading