Skip to content

Commit 10bd173

Browse files
committed
fix: correct grammar alternative ordering for RLS statements
Fixed parser failures when parsing PostgreSQL Row Level Security (RLS) statements by reordering grammar alternatives to check more specific patterns before less specific ones. Problem: - ALTER TABLE ... ENABLE/DISABLE ROW LEVEL SECURITY failed to parse - Parser was incorrectly choosing ENABLE/DISABLE KEYS path first - Grammar warning about WITH keyword conflict in CREATE POLICY Solution: 1. Reordered ENABLE alternatives: ENABLE ROW LEVEL SECURITY now checked before ENABLE KEYS (lines 9674-9684) 2. Reordered DISABLE alternatives: DISABLE ROW LEVEL SECURITY now checked before DISABLE KEYS (lines 9661-9671) 3. Added LOOKAHEAD(2) to WITH CHECK clause in CREATE POLICY to resolve conflict with CTEs (line 10470) Impact: - All 19 existing RLS tests pass (8 AlterRowLevelSecurityTest, 11 CreatePolicyTest) - WITH keyword conflict warning eliminated - Parser can now handle real-world SQL migration files with RLS statements - No regressions in existing functionality Technical Note: In JavaCC, when multiple alternatives share a common prefix (like ENABLE), the more specific pattern (longer token sequence) must appear FIRST in the grammar to be matched correctly. LOOKAHEAD values help disambiguate, but ordering is critical for correct parsing.
1 parent 7e62119 commit 10bd173

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9657,29 +9657,29 @@ AlterExpression AlterExpression():
96579657
)
96589658
)
96599659
|
9660-
9661-
LOOKAHEAD(2) (
9662-
<K_DISABLE> <K_KEYS> {
9663-
alterExp.setOperation(AlterOperation.DISABLE_KEYS);
9660+
9661+
LOOKAHEAD(4) (
9662+
<K_DISABLE> <K_ROW> <K_LEVEL> <K_SECURITY> {
9663+
alterExp.setOperation(AlterOperation.DISABLE_ROW_LEVEL_SECURITY);
96649664
}
96659665
)
9666-
96679666
|
96689667
LOOKAHEAD(2) (
9669-
<K_ENABLE> <K_KEYS> {
9670-
alterExp.setOperation(AlterOperation.ENABLE_KEYS);
9668+
<K_DISABLE> <K_KEYS> {
9669+
alterExp.setOperation(AlterOperation.DISABLE_KEYS);
96719670
}
96729671
)
9672+
96739673
|
96749674
LOOKAHEAD(4) (
96759675
<K_ENABLE> <K_ROW> <K_LEVEL> <K_SECURITY> {
96769676
alterExp.setOperation(AlterOperation.ENABLE_ROW_LEVEL_SECURITY);
96779677
}
96789678
)
96799679
|
9680-
LOOKAHEAD(4) (
9681-
<K_DISABLE> <K_ROW> <K_LEVEL> <K_SECURITY> {
9682-
alterExp.setOperation(AlterOperation.DISABLE_ROW_LEVEL_SECURITY);
9680+
LOOKAHEAD(2) (
9681+
<K_ENABLE> <K_KEYS> {
9682+
alterExp.setOperation(AlterOperation.ENABLE_KEYS);
96839683
}
96849684
)
96859685
|
@@ -10467,7 +10467,7 @@ CreatePolicy CreatePolicy() #CreatePolicy:
1046710467

1046810468
[ <K_USING> "(" usingExpr=Expression() ")" { createPolicy.setUsingExpression(usingExpr); } ]
1046910469

10470-
[ <K_WITH> <K_CHECK> "(" checkExpr=Expression() ")" { createPolicy.setWithCheckExpression(checkExpr); } ]
10470+
[ LOOKAHEAD(2) <K_WITH> <K_CHECK> "(" checkExpr=Expression() ")" { createPolicy.setWithCheckExpression(checkExpr); } ]
1047110471

1047210472
{
1047310473

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2025 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.expression;
211

312
import net.sf.jsqlparser.JSQLParserException;

0 commit comments

Comments
 (0)