Skip to content

Commit a2f8642

Browse files
committed
Add INFIX_NOT_OPERATOR feature flag.
Supports ! as a unary expression for NOT. Has a higher precedence but is otherwise equivalent. This matches the MySQL ! behavior. A slight tweak of a proposal from @nelgau to be slightly more consistent with other feature flag behavior. Fixes #105
1 parent f7f7ef4 commit a2f8642

15 files changed

Lines changed: 167 additions & 3 deletions

‎src/main/java/com/foundationdb/sql/parser/SQLParserFeature.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ public enum SQLParserFeature
3434
INFIX_MOD,
3535
INFIX_BIT_OPERATORS,
3636
INFIX_LOGICAL_OPERATORS,
37-
DOUBLE_QUOTED_STRING
37+
DOUBLE_QUOTED_STRING,
38+
/** Allows {@code !} as {@code NOT}, but with a higher precedence. */
39+
INFIX_NOT_OPERATOR,
3840
}

‎src/main/javacc/SQLGrammar.jj‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,17 @@ class SQLGrammar
17561756
}
17571757
}
17581758

1759+
boolean unaryLogicalFollows() {
1760+
if (!hasFeature(SQLParserFeature.INFIX_NOT_OPERATOR))
1761+
return false;
1762+
switch(getToken(1).kind) {
1763+
case EXCLAMATION_MARK:
1764+
return true;
1765+
default:
1766+
return false;
1767+
}
1768+
}
1769+
17591770
// This LOOKAHEAD is required because a + or - sign can come
17601771
// before any expression, and also can be part of a literal. If it
17611772
// comes before a number, we want it to be considered part of the
@@ -2668,6 +2679,7 @@ TOKEN :
26682679
| <DOUBLE_GREATER: ">>">
26692680
| <FIELD_REFERENCE: "->">
26702681
| <ASTERISK_ASTERISK: "**">
2682+
| <EXCLAMATION_MARK: "!">
26712683
}
26722684

26732685
TOKEN :
@@ -5873,6 +5885,9 @@ unaryExpression(ValueNode farLeftOperand,
58735885
|
58745886
LOOKAHEAD( { unaryBitFollows() })
58755887
sign = unaryBitOperator()
5888+
|
5889+
LOOKAHEAD( { unaryLogicalFollows() } )
5890+
sign = unaryLogicalOperator()
58765891
)
58775892
]
58785893
value = primaryExpression()
@@ -5892,6 +5907,11 @@ unaryExpression(ValueNode farLeftOperand,
58925907
value,
58935908
parserContext);
58945909
}
5910+
else if ("!".equals(sign)) {
5911+
value = (ValueNode)nodeFactory.getNode(NodeTypes.NOT_NODE,
5912+
value,
5913+
parserContext);
5914+
}
58955915
else {
58965916
assert (sign == null) : "Unknown unary operator '" + sign + "'";
58975917
}
@@ -5928,6 +5948,18 @@ unaryBitOperator() :
59285948
}
59295949
}
59305950

5951+
String
5952+
unaryLogicalOperator() :
5953+
{
5954+
Token s;
5955+
}
5956+
{
5957+
s = <EXCLAMATION_MARK>
5958+
{
5959+
return s.image;
5960+
}
5961+
}
5962+
59315963
ValueNode
59325964
primaryExpressionXX() throws StandardException :
59335965
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
com.foundationdb.sql.parser.SQLParserException: Lexical error at line 1, column 3. Encountered: <EOF> after : "!!"
1+
com.foundationdb.sql.parser.SQLParserException: Lexical error at line 1, column 3. Encountered: <EOF> after : ""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
!!
1+
$$
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
com.foundationdb.sql.parser.CursorNode@677bb8fe
2+
name: null
3+
updateMode: UNSPECIFIED
4+
statementType: SELECT
5+
resultSet:
6+
com.foundationdb.sql.parser.SelectNode@6bae60c5
7+
isDistinct: false
8+
resultColumns:
9+
com.foundationdb.sql.parser.ResultColumnList@5535cbe
10+
11+
[0]:
12+
com.foundationdb.sql.parser.ResultColumn@23edc0ad
13+
exposedName: null
14+
name: null
15+
tableName: null
16+
isDefaultColumn: false
17+
type: null
18+
expression:
19+
com.foundationdb.sql.parser.NotNode@4e04b048
20+
operator: not
21+
methodName: not
22+
type: null
23+
operand:
24+
com.foundationdb.sql.parser.BooleanConstantNode@15d16efc
25+
value: false
26+
type: BOOLEAN NOT NULL
27+
fromList:
28+
com.foundationdb.sql.parser.FromList@68cd6d94
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+INFIX_NOT_OPERATOR
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT !FALSE
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
com.foundationdb.sql.parser.CursorNode@1fe30f8
2+
name: null
3+
updateMode: UNSPECIFIED
4+
statementType: SELECT
5+
resultSet:
6+
com.foundationdb.sql.parser.SelectNode@5740eacd
7+
isDistinct: false
8+
resultColumns:
9+
com.foundationdb.sql.parser.ResultColumnList@66f7657b
10+
11+
[0]:
12+
com.foundationdb.sql.parser.ResultColumn@7c27a30c
13+
exposedName: null
14+
name: null
15+
tableName: null
16+
isDefaultColumn: false
17+
type: null
18+
expression:
19+
com.foundationdb.sql.parser.BetweenOperatorNode@e41a882
20+
operator: BETWEEN
21+
methodName: null
22+
type: null
23+
leftOperand:
24+
com.foundationdb.sql.parser.NotNode@789df61d
25+
operator: not
26+
methodName: not
27+
type: null
28+
operand:
29+
com.foundationdb.sql.parser.ColumnReference@4a8927c8
30+
columnName: a
31+
tableName: null
32+
type: null
33+
rightOperandList:
34+
com.foundationdb.sql.parser.ValueNodeList@7292e1b2
35+
36+
[0]:
37+
com.foundationdb.sql.parser.ColumnReference@91fa7e2
38+
columnName: b
39+
tableName: null
40+
type: null
41+
[1]:
42+
com.foundationdb.sql.parser.ColumnReference@7d46eb1b
43+
columnName: c
44+
tableName: null
45+
type: null
46+
fromList:
47+
com.foundationdb.sql.parser.FromList@3af579de
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+INFIX_NOT_OPERATOR
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT ! A BETWEEN B AND C

0 commit comments

Comments
 (0)