Skip to content

Commit 55e4406

Browse files
committed
Treat #> as operator
1 parent c894b54 commit 55e4406

8 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/Tokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ private function makeTokenizeRegexes(): array
842842

843843
return [
844844
Token::TOKEN_TYPE_WHITESPACE => '\s+',
845-
Token::TOKEN_TYPE_COMMENT => '(?:--|#)[^\n]*+',
845+
Token::TOKEN_TYPE_COMMENT => '(?:--|#(?!>))[^\n]*+', // #>, #>> and <#> are PostgreSQL operators
846846
Token::TOKEN_TYPE_BLOCK_COMMENT => '/\*(?:[^*]+|\*(?!/))*+(?:\*|$)(?:/|$)',
847847
// 1. backtick quoted string using `` to escape
848848
// 2. square bracket quoted string (SQL Server) using ]] to escape

tests/TokenizerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,20 @@ public static function tokenizeData(): Generator
16581658
],
16591659
'/* foo...',
16601660
];
1661+
1662+
yield 'PostgreSQL operator' => [
1663+
[
1664+
new Token(Token::TOKEN_TYPE_RESERVED_TOPLEVEL, 'select'),
1665+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1666+
new Token(Token::TOKEN_TYPE_WORD, 'json'),
1667+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1668+
new Token(Token::TOKEN_TYPE_BOUNDARY, '#'),
1669+
new Token(Token::TOKEN_TYPE_BOUNDARY, '>'),
1670+
new Token(Token::TOKEN_TYPE_WHITESPACE, ' '),
1671+
new Token(Token::TOKEN_TYPE_RESERVED, 'null'),
1672+
],
1673+
'select json #> null',
1674+
];
16611675
}
16621676

16631677
public function testTokenizeLongConcat(): void

tests/clihighlight.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,9 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
12051205
CREATE TABLE t (
12061206
c VARCHAR(20)
12071207
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
1208+
---
1209+
SELECT
1210+
'{}' :: json #> '{}'
1211+
---
1212+
SELECT
1213+
vector1 <#> vector2

tests/compress.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ SELECT a FROM test STRAIGHT_JOIN test2 ON test.id = test2.id
111111
SELECT t.id, t.start, t.end, t.end AS e2, t.limit, t.begin, t.case, t.when, t.then, t.else FROM t WHERE t.start = t.end
112112
---
113113
CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
114+
---
115+
SELECT '{}'::json #> '{}'
116+
---
117+
SELECT vector1 <#> vector2

tests/format-highlight.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,9 @@
12051205
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (
12061206
<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)
12071207
) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
1208+
---
1209+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
1210+
<span style="color: blue;">'{}'</span> <span >::</span> <span style="color: #333;">json</span> <span >#</span><span >&gt;</span> <span style="color: blue;">'{}'</span></pre>
1211+
---
1212+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
1213+
<span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>

tests/format.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,3 +1203,9 @@ WHERE
12031203
CREATE TABLE t (
12041204
c VARCHAR(20)
12051205
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
1206+
---
1207+
SELECT
1208+
'{}' :: json #> '{}'
1209+
---
1210+
SELECT
1211+
vector1 <#> vector2

tests/highlight.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,7 @@
425425
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">start</span> <span >=</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">end</span></pre>
426426
---
427427
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
428+
---
429+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">'{}'</span><span >::</span><span style="color: #333;">json</span> <span >#</span><span >&gt;</span> <span style="color: blue;">'{}'</span></pre>
430+
---
431+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>

tests/sql.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,7 @@ FROM t
425425
WHERE t.start = t.end
426426
---
427427
CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
428+
---
429+
SELECT '{}'::json #> '{}'
430+
---
431+
SELECT vector1 <#> vector2

0 commit comments

Comments
 (0)