|
13 | 13 | import net.sf.jsqlparser.expression.Alias; |
14 | 14 | import net.sf.jsqlparser.expression.JsonExpression; |
15 | 15 | import net.sf.jsqlparser.expression.StringValue; |
| 16 | +import net.sf.jsqlparser.expression.operators.relational.Intersects; |
16 | 17 | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
17 | 18 | import net.sf.jsqlparser.schema.Column; |
18 | 19 | import net.sf.jsqlparser.schema.Table; |
@@ -138,4 +139,44 @@ void testQuotedIdentifier() throws JSQLParserException { |
138 | 139 | Assertions.assertEquals("`This is a Test Table`", table.getName()); |
139 | 140 |
|
140 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testPostgresHashBinaryOperator() throws JSQLParserException { |
| 145 | + // PostgreSQL 18, Table 9.4 (Mathematical Operators): |
| 146 | + // integral_type # integral_type -> bitwise exclusive OR |
| 147 | + assertSqlCanBeParsedAndDeparsed("SELECT 17 # 5"); |
| 148 | + // PostgreSQL 18, Table 9.36 (Geometric Operators): |
| 149 | + // geometric_type # geometric_type -> point of intersection |
| 150 | + Select select = (Select) assertSqlCanBeParsedAndDeparsed("SELECT lseg1 # lseg2"); |
| 151 | + Intersects intersects = Assertions.assertInstanceOf(Intersects.class, |
| 152 | + select.getPlainSelect().getSelectItem(0).getExpression()); |
| 153 | + Assertions.assertEquals("#", intersects.getStringExpression()); |
| 154 | + Assertions.assertInstanceOf(Column.class, intersects.getLeftExpression()); |
| 155 | + Assertions.assertInstanceOf(Column.class, intersects.getRightExpression()); |
| 156 | + |
| 157 | + assertSqlCanBeParsedAndDeparsed("SELECT a # (b + 1) FROM t"); |
| 158 | + assertSqlCanBeParsedAndDeparsed("SELECT a # b AS x FROM t"); |
| 159 | + // same tier as `&`: binds tighter than the comparison that follows |
| 160 | + assertSqlCanBeParsedAndDeparsed("SELECT * FROM t WHERE a # b = 1"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testPostgresHashOperatorKeepsIdentifiersAndJsonOperators() |
| 165 | + throws JSQLParserException { |
| 166 | + // `#` stays an identifier character: SQL Server `#temp`, `##global` |
| 167 | + // (#1197), Oracle `#$tab1#` and unquoted names containing `#` |
| 168 | + assertSqlCanBeParsedAndDeparsed("SELECT #temp FROM t"); |
| 169 | + assertSqlCanBeParsedAndDeparsed("SELECT ##global FROM t"); |
| 170 | + assertSqlCanBeParsedAndDeparsed("SELECT #$tab1# FROM t"); |
| 171 | + assertSqlCanBeParsedAndDeparsed("SELECT a#b FROM t"); |
| 172 | + // `#>` / `#>>` keep their JSON lexing (#1695) |
| 173 | + assertSqlCanBeParsedAndDeparsed("SELECT data #> '{a,b}' FROM t"); |
| 174 | + assertSqlCanBeParsedAndDeparsed("SELECT data #>> '{0,1}' FROM t"); |
| 175 | + // a lone `#` can no longer be an identifier (alias, column or table |
| 176 | + // name), all such forms fail loudly now that `#` is an operator |
| 177 | + Assertions.assertThrows(JSQLParserException.class, |
| 178 | + () -> CCJSqlParserUtil.parse("SELECT 1 #")); |
| 179 | + Assertions.assertThrows(JSQLParserException.class, |
| 180 | + () -> CCJSqlParserUtil.parse("SELECT # FROM t")); |
| 181 | + } |
141 | 182 | } |
0 commit comments