regex.slt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. # Run all tests on a multi-process cluster, because there were some serialization issues in the past that happened
  11. # only on multi-process clusters.
  12. statement ok
  13. CREATE CLUSTER multiprocess REPLICAS (a (SIZE '2-2'));
  14. statement ok
  15. SET cluster = 'multiprocess';
  16. statement ok
  17. CREATE TABLE data (input text)
  18. statement ok
  19. INSERT INTO data VALUES ('asdfjkl'), ('foo'), ('asdf'), (NULL), ('jkl')
  20. query TTT
  21. SELECT data.*, reg.* FROM data, regexp_extract('(asdf)|(?P<foo>jkl)', data.input) reg ORDER BY data.input
  22. ----
  23. asdf asdf NULL
  24. asdfjkl asdf NULL
  25. jkl NULL jkl
  26. # TODO - Test that the columns have the correct nullability, once they actually do (database-issues#612)
  27. # Standard regex matching.
  28. query TTT
  29. SELECT 'foo' ~ 'foo?', 'fo' ~ 'foo?', 'f' ~ 'foo?'
  30. ----
  31. true true false
  32. # Standard regex not-matching.
  33. query TTT
  34. SELECT 'foo' !~ 'foo?', 'fo' !~ 'foo?', 'f' !~ 'foo?'
  35. ----
  36. false false true
  37. # Case-insensitive matching.
  38. query TT
  39. SELECT 'Foo' ~ 'foo', 'Foo' ~* 'foo'
  40. ----
  41. false true
  42. # Case-insensitive not-matching.
  43. query TT
  44. SELECT 'Foo' !~ 'foo', 'Foo' !~* 'foo'
  45. ----
  46. true false
  47. mode standard
  48. # Check that literal regexes are pre-compiled...
  49. query T multiline
  50. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT input ~ 'foo?' FROM data
  51. ----
  52. Explained Query:
  53. Project (#1) // { arity: 1 }
  54. Map (is_regexp_match["foo?", case_insensitive=false](#0{input})) // { arity: 2 }
  55. ReadStorage materialize.public.data // { arity: 1 }
  56. Source materialize.public.data
  57. Target cluster: multiprocess
  58. EOF
  59. # ...but not variable regexes.
  60. query T multiline
  61. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR SELECT input ~ input FROM data
  62. ----
  63. Explained Query:
  64. Project (#1) // { arity: 1 }
  65. Map ((#0{input} ~ #0{input})) // { arity: 2 }
  66. ReadStorage materialize.public.data // { arity: 1 }
  67. Source materialize.public.data
  68. Target cluster: multiprocess
  69. EOF
  70. query T multiline
  71. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  72. SELECT regexp_match('ABC', 'a.*');
  73. ----
  74. Explained Query (fast path):
  75. Constant
  76. - (null)
  77. Target cluster: multiprocess
  78. EOF
  79. query T
  80. SELECT regexp_match('ABC', 'a.*');
  81. ----
  82. NULL
  83. query T multiline
  84. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  85. SELECT regexp_match('ABC', 'a.*', 'i');
  86. ----
  87. Explained Query (fast path):
  88. Constant
  89. - ({"ABC"})
  90. Target cluster: multiprocess
  91. EOF
  92. query T
  93. SELECT regexp_match('ABC', 'a.*', 'i');
  94. ----
  95. {ABC}
  96. # We have to accept it when both flags are present (taking the last one), because Postgres also does the same.
  97. query T
  98. SELECT regexp_match('ABC', 'a.*', 'ic');
  99. ----
  100. NULL
  101. query T
  102. SELECT regexp_match('ABC', 'a.*', 'ci');
  103. ----
  104. {ABC}
  105. query error invalid regular expression: regex parse error:
  106. SELECT 'abs' ~ '\';
  107. # Case-insensitive vs. case-sensitive regexes when there is no full const folding, but MirScalarExpr::reduce changes
  108. # from the binary to unary versions.
  109. statement ok
  110. CREATE TABLE t(s string, like_pat string, regex_pat string);
  111. statement ok
  112. INSERT INTO t VALUES ('abc', 'a%', 'a.*'), ('ABC', 'a%', 'a.*'), ('ccc', 'a%', 'a.*');
  113. # In Postgres' regexes, `.` matches newlines by default, so let's test this as well.
  114. # See https://www.postgresql.org/docs/current/functions-matching.html#POSIX-MATCHING-RULES
  115. # "newline-sensitive matching"
  116. statement ok
  117. INSERT INTO t VALUES (
  118. E'test line 1\ntest line 2',
  119. '%1%2%',
  120. '1.*2'
  121. );
  122. # Note that sqllogictest can't handle newlines in string output, so replace those when displaying `s`
  123. query T multiline
  124. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  125. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s~'a.*';
  126. ----
  127. Explained Query:
  128. Project (#3)
  129. Filter is_regexp_match["a.*", case_insensitive=false](#0{s})
  130. Map (replace(#0{s}, "\n", "<newline>"))
  131. ReadStorage materialize.public.t
  132. Source materialize.public.t
  133. filter=(is_regexp_match["a.*", case_insensitive=false](#0{s}))
  134. Target cluster: multiprocess
  135. EOF
  136. query T
  137. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s~'a.*';
  138. ----
  139. abc
  140. query T multiline
  141. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  142. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s~*'a.*';
  143. ----
  144. Explained Query:
  145. Project (#3)
  146. Filter is_regexp_match["a.*", case_insensitive=true](#0{s})
  147. Map (replace(#0{s}, "\n", "<newline>"))
  148. ReadStorage materialize.public.t
  149. Source materialize.public.t
  150. filter=(is_regexp_match["a.*", case_insensitive=true](#0{s}))
  151. Target cluster: multiprocess
  152. EOF
  153. query T
  154. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s~*'a.*';
  155. ----
  156. ABC
  157. abc
  158. query T multiline
  159. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  160. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*') FROM t;
  161. ----
  162. Explained Query:
  163. Project (#3, #4)
  164. Map (replace(#0{s}, "\n", "<newline>"), regexp_match["a.*", case_insensitive=false](#0{s}))
  165. ReadStorage materialize.public.t
  166. Source materialize.public.t
  167. Target cluster: multiprocess
  168. EOF
  169. query TT valuesort
  170. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*') FROM t;
  171. ----
  172. ABC
  173. NULL
  174. ccc
  175. NULL
  176. test line 1<newline>test line 2
  177. NULL
  178. abc
  179. {abc}
  180. query T multiline
  181. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  182. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*', 'i') FROM t;
  183. ----
  184. Explained Query:
  185. Project (#3, #4)
  186. Map (replace(#0{s}, "\n", "<newline>"), regexp_match["a.*", case_insensitive=true](#0{s}))
  187. ReadStorage materialize.public.t
  188. Source materialize.public.t
  189. Target cluster: multiprocess
  190. EOF
  191. query TT valuesort
  192. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*', 'i') FROM t;
  193. ----
  194. ccc
  195. NULL
  196. test line 1<newline>test line 2
  197. NULL
  198. ABC
  199. {ABC}
  200. abc
  201. {abc}
  202. query TT valuesort
  203. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*', 'ic') FROM t;
  204. ----
  205. ABC
  206. NULL
  207. ccc
  208. NULL
  209. test line 1<newline>test line 2
  210. NULL
  211. abc
  212. {abc}
  213. query TT valuesort
  214. SELECT replace(s, E'\n', '<newline>'), regexp_match(s, 'a.*', 'ci') FROM t;
  215. ----
  216. ccc
  217. NULL
  218. test line 1<newline>test line 2
  219. NULL
  220. ABC
  221. {ABC}
  222. abc
  223. {abc}
  224. query error db error: ERROR: Evaluation error: invalid regular expression: regex parse error:
  225. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~ '\';
  226. # Dynamic regexes (binary (or variadic) versions)
  227. query T multiline
  228. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  229. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~ regex_pat;
  230. ----
  231. Explained Query:
  232. Project (#3)
  233. Filter (#0{s} ~ #2{regex_pat})
  234. Map (replace(#0{s}, "\n", "<newline>"))
  235. ReadStorage materialize.public.t
  236. Source materialize.public.t
  237. filter=((#0{s} ~ #2{regex_pat}))
  238. Target cluster: multiprocess
  239. EOF
  240. query T
  241. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~ regex_pat;
  242. ----
  243. abc
  244. test line 1<newline>test line 2
  245. query T multiline
  246. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  247. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~* regex_pat;
  248. ----
  249. Explained Query:
  250. Project (#3)
  251. Filter (#0{s} ~* #2{regex_pat})
  252. Map (replace(#0{s}, "\n", "<newline>"))
  253. ReadStorage materialize.public.t
  254. Source materialize.public.t
  255. filter=((#0{s} ~* #2{regex_pat}))
  256. Target cluster: multiprocess
  257. EOF
  258. query T valuesort
  259. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~* regex_pat;
  260. ----
  261. ABC
  262. abc
  263. test line 1<newline>test line 2
  264. query T multiline
  265. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  266. SELECT replace(s, E'\n', '<newline>'), regex_pat, regexp_match(s, regex_pat) FROM t;
  267. ----
  268. Explained Query:
  269. Project (#3, #2{regex_pat}, #4)
  270. Map (replace(#0{s}, "\n", "<newline>"), regexp_match(#0{s}, #2{regex_pat}))
  271. ReadStorage materialize.public.t
  272. Source materialize.public.t
  273. Target cluster: multiprocess
  274. EOF
  275. # We need to also replace newlines in the output here to not confuse sqllogictest.
  276. query TTT valuesort
  277. SELECT replace(s, E'\n', '<newline>'), regex_pat, replace(regexp_match(s, regex_pat)::text, E'\n', '<newline>') FROM t;
  278. ----
  279. ABC
  280. a.*
  281. NULL
  282. ccc
  283. a.*
  284. NULL
  285. abc
  286. a.*
  287. {abc}
  288. test line 1<newline>test line 2
  289. 1.*2
  290. {"1<newline>test line 2"}
  291. query T multiline
  292. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  293. SELECT replace(s, E'\n', '<newline>'), regex_pat, regexp_match(s, regex_pat, 'i') FROM t;
  294. ----
  295. Explained Query:
  296. Project (#3, #2{regex_pat}, #4)
  297. Map (replace(#0{s}, "\n", "<newline>"), regexp_match(#0{s}, #2{regex_pat}, "i"))
  298. ReadStorage materialize.public.t
  299. Source materialize.public.t
  300. Target cluster: multiprocess
  301. EOF
  302. query TTT valuesort
  303. SELECT replace(s, E'\n', '<newline>'), regex_pat, replace(regexp_match(s, regex_pat, 'i')::text, E'\n', '<newline>') FROM t;
  304. ----
  305. ccc
  306. a.*
  307. NULL
  308. ABC
  309. a.*
  310. {ABC}
  311. abc
  312. a.*
  313. {abc}
  314. test line 1<newline>test line 2
  315. 1.*2
  316. {"1<newline>test line 2"}
  317. query TTT valuesort
  318. SELECT replace(s, E'\n', '<newline>'), regex_pat, replace(regexp_match(s, regex_pat, 'ic')::text, E'\n', '<newline>') FROM t;
  319. ----
  320. ABC
  321. a.*
  322. NULL
  323. ccc
  324. a.*
  325. NULL
  326. abc
  327. a.*
  328. {abc}
  329. test line 1<newline>test line 2
  330. 1.*2
  331. {"1<newline>test line 2"}
  332. query TTT valuesort
  333. SELECT replace(s, E'\n', '<newline>'), regex_pat, replace(regexp_match(s, regex_pat, 'ci')::text, E'\n', '<newline>') FROM t;
  334. ----
  335. ccc
  336. a.*
  337. NULL
  338. ABC
  339. a.*
  340. {ABC}
  341. abc
  342. a.*
  343. {abc}
  344. test line 1<newline>test line 2
  345. 1.*2
  346. {"1<newline>test line 2"}
  347. statement ok
  348. INSERT INTO T VALUES ('this is gonna be an invalid regex', '', '\');
  349. # Note: The actual error msg shows the regex itself (as it should), but it seems sqllogictest can't handle multiline
  350. # error msgs.
  351. query error db error: ERROR: Evaluation error: invalid regular expression: regex parse error:
  352. SELECT replace(s, E'\n', '<newline>') FROM t WHERE s ~* regex_pat;
  353. query T multiline
  354. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  355. SELECT *, s~'\' FROM t;
  356. ----
  357. Explained Query:
  358. Map (error("invalid regular expression: regex parse error:\n \\n ^\nerror: incomplete escape sequence, reached end of pattern prematurely"))
  359. ReadStorage materialize.public.t
  360. Source materialize.public.t
  361. Target cluster: multiprocess
  362. EOF
  363. mode cockroach
  364. query T
  365. SELECT regexp_split_to_array(txt, re) FROM (VALUES
  366. ('hello world'),
  367. ('12 34'),
  368. (' 12 34 '),
  369. (''),
  370. (' ')
  371. ) TXT(txt),
  372. (VALUES
  373. (''),
  374. (' '),
  375. ('\s+'),
  376. ('\s*'),
  377. ('\s')
  378. ) RE(re)
  379. ORDER BY 1
  380. ----
  381. {""}
  382. {""}
  383. {""}
  384. {""}
  385. {""}
  386. {"",""}
  387. {"",""}
  388. {"",""}
  389. {"",""}
  390. {"",1,2,3,4,""}
  391. {"",12,"",34,""}
  392. {"",12,"",34,""}
  393. {"",12,34,""}
  394. {" "}
  395. {" ",1,2," "," ",3,4," "}
  396. {1,2," "," ",3,4}
  397. {1,2,3,4}
  398. {12,"",34}
  399. {12,"",34}
  400. {12,34}
  401. {h,e,l,l,o," ",w,o,r,l,d}
  402. {h,e,l,l,o,w,o,r,l,d}
  403. {hello,world}
  404. {hello,world}
  405. {hello,world}
  406. # TODO: Correct ordering when database-issues#4764 is fixed.
  407. query T
  408. SELECT foo FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '\s+') AS foo ORDER BY 1
  409. ----
  410. brown
  411. dog
  412. fox
  413. jumps
  414. lazy
  415. over
  416. quick
  417. the
  418. the
  419. # TODO: Correct ordering when database-issues#4764 is fixed.
  420. query T
  421. SELECT foo FROM regexp_split_to_table('the quick brown fox', '\s*') AS foo ORDER BY 1
  422. ----
  423. b
  424. c
  425. e
  426. f
  427. h
  428. i
  429. k
  430. n
  431. o
  432. o
  433. q
  434. r
  435. t
  436. u
  437. w
  438. x
  439. query T
  440. SELECT regexp_split_to_array('no match', 'nope')
  441. ----
  442. {"no match"}
  443. query T
  444. SELECT regexp_split_to_table('no match', 'nope')
  445. ----
  446. no match
  447. # Test regexp_replace success paths
  448. query T multiline
  449. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  450. SELECT regexp_replace('FOOBARBAZ', 'b..', 'X');
  451. ----
  452. Explained Query (fast path):
  453. Constant
  454. - ("FOOBARBAZ")
  455. Target cluster: multiprocess
  456. EOF
  457. query T
  458. SELECT regexp_replace('FOOBARBAZ', 'b..', 'X');
  459. ----
  460. FOOBARBAZ
  461. query T multiline
  462. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  463. SELECT regexp_replace('FOOBARBAZ', 'b..', 'X', 'i');
  464. ----
  465. Explained Query (fast path):
  466. Constant
  467. - ("FOOXBAZ")
  468. Target cluster: multiprocess
  469. EOF
  470. query T
  471. SELECT regexp_replace('FOOBARBAZ', 'b..', 'X', 'i');
  472. ----
  473. FOOXBAZ
  474. query T
  475. SELECT regexp_replace('FOOBARBAZ', 'b..', 'X', 'ig');
  476. ----
  477. FOOXX
  478. query T
  479. SELECT regexp_replace('foobarbaz', 'b(..)', 'X${1}Y', 'g')
  480. ----
  481. fooXarYXazY
  482. query T
  483. SELECT regexp_replace('foobarbaz', 'no match', 'X')
  484. ----
  485. foobarbaz
  486. statement ok
  487. CREATE TABLE r(s string, regex string, replacement string);
  488. statement ok
  489. INSERT INTO r VALUES ('foobarbaz', 'b..', 'X'), ('foobarbaz', 'b(..)', '>${1}<'), ('FOOBARBAZ', 'b..', 'X'), ('FOOZARZAZ', 'b..', 'X');
  490. query T multiline
  491. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  492. SELECT regexp_replace(s, 'b..', 'X') FROM r;
  493. ----
  494. Explained Query:
  495. Project (#3)
  496. Map (regexp_replace["b..", case_insensitive=false, limit=1](#0{s}, "X"))
  497. ReadStorage materialize.public.r
  498. Source materialize.public.r
  499. Target cluster: multiprocess
  500. EOF
  501. query TT rowsort
  502. SELECT s, regexp_replace(s, 'b..', 'X') FROM r;
  503. ----
  504. foobarbaz fooXbaz
  505. foobarbaz fooXbaz
  506. FOOBARBAZ FOOBARBAZ
  507. FOOZARZAZ FOOZARZAZ
  508. query T multiline
  509. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  510. SELECT regexp_replace(s, 'b..', 'X', 'i') FROM r;
  511. ----
  512. Explained Query:
  513. Project (#3)
  514. Map (regexp_replace["b..", case_insensitive=true, limit=1](#0{s}, "X"))
  515. ReadStorage materialize.public.r
  516. Source materialize.public.r
  517. Target cluster: multiprocess
  518. EOF
  519. query TT rowsort
  520. SELECT s, regexp_replace(s, 'b..', 'X', 'i') FROM r;
  521. ----
  522. FOOBARBAZ FOOXBAZ
  523. foobarbaz fooXbaz
  524. foobarbaz fooXbaz
  525. FOOZARZAZ FOOZARZAZ
  526. query T multiline
  527. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  528. SELECT regexp_replace(s, 'b..', 'X', 'ig') FROM r;
  529. ----
  530. Explained Query:
  531. Project (#3)
  532. Map (regexp_replace["b..", case_insensitive=true, limit=0](#0{s}, "X"))
  533. ReadStorage materialize.public.r
  534. Source materialize.public.r
  535. Target cluster: multiprocess
  536. EOF
  537. query TT rowsort
  538. SELECT s, regexp_replace(s, 'b..', 'X', 'ig') FROM r;
  539. ----
  540. FOOBARBAZ FOOXX
  541. foobarbaz fooXX
  542. foobarbaz fooXX
  543. FOOZARZAZ FOOZARZAZ
  544. query T multiline
  545. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  546. SELECT regexp_replace(s, regex, replacement, 'ig') FROM r;
  547. ----
  548. Explained Query:
  549. Project (#3)
  550. Map (regexp_replace(#0{s}, #1{regex}, #2{replacement}, "ig"))
  551. ReadStorage materialize.public.r
  552. Source materialize.public.r
  553. Target cluster: multiprocess
  554. EOF
  555. query TT rowsort
  556. SELECT s, regexp_replace(s, regex, replacement, 'ig') FROM r;
  557. ----
  558. FOOBARBAZ FOOXX
  559. foobarbaz fooXX
  560. FOOZARZAZ FOOZARZAZ
  561. foobarbaz foo>ar<>az<
  562. # Test regexp_replace error paths
  563. query T multiline
  564. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  565. SELECT regexp_replace('foobarbaz', 'b(..', 'X', 'ig');
  566. ----
  567. Explained Query (fast path):
  568. Error "invalid regular expression: regex parse error:\n b(..\n ^\nerror: unclosed group"
  569. Target cluster: multiprocess
  570. EOF
  571. query T
  572. SELECT regexp_replace(null, 'b(..', 'X', 'ig');
  573. ----
  574. NULL
  575. query error invalid regular expression: regex parse error:
  576. SELECT regexp_replace('foobarbaz', 'b(..', 'X', 'ig');
  577. query error invalid regular expression flag: x
  578. SELECT regexp_replace('foobarbaz', 'b(..', 'X', 'igx');
  579. statement ok
  580. CREATE TABLE e(s string, regex string, replacement string);
  581. statement ok
  582. INSERT INTO e VALUES ('foobarbaz', 'b(..', 'X');
  583. query T multiline
  584. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
  585. SELECT s, regexp_replace(s, 'b(..', replacement, 'ig') FROM e;
  586. ----
  587. Explained Query:
  588. Project (#0{s}, #3)
  589. Map (case when (#0{s}) IS NULL then null else error("invalid regular expression: regex parse error:\n b(..\n ^\nerror: unclosed group") end)
  590. ReadStorage materialize.public.e
  591. Source materialize.public.e
  592. Target cluster: multiprocess
  593. EOF
  594. query error invalid regular expression: regex parse error:
  595. SELECT s, regexp_replace(s, 'b(..', replacement, 'ig') FROM e;
  596. query error invalid regular expression: regex parse error:
  597. SELECT s, regexp_replace(s, regex, replacement, 'ig') FROM e;
  598. query error invalid regular expression flag: x
  599. SELECT regexp_replace(s, regex, replacement, 'igx') FROM e;
  600. # regexp_matches
  601. query T
  602. SELECT regexp_matches('foobarbaz', '(b..)')
  603. ----
  604. {bar}
  605. query T
  606. SELECT regexp_matches('foobarbaz', '(b..)', 'g')
  607. ----
  608. {bar}
  609. {baz}
  610. query T
  611. SELECT regexp_matches('foobarbequebazilbarfbonkbilbobagginsbarbellbeachball', '(b[^b]+)(b[^b]+)(b[^b]+)');
  612. ----
  613. {bar,beque,bazil}
  614. query T rowsort
  615. SELECT regexp_matches('foobarbequebazilbarfbonkbilbobagginsbarbellbeachball', '(b[^b]+)(b[^b]+)(b[^b]+)', 'g');
  616. ----
  617. {barf,bonk,bil}
  618. {bo,baggins,bar}
  619. {bar,beque,bazil}
  620. {bell,beach,ball}
  621. query error invalid regular expression flag: x
  622. SELECT regexp_matches('foobarbaz', '(b..)', 'igx');
  623. query T
  624. SELECT regexp_matches('orangeoculus', '(b[^b]+)(b[^b]+)(b[^b]+)', 'g');
  625. ----
  626. query T rowsort
  627. SELECT regexp_matches('fooBarBequebazilbarfBonkbilbobagginsbarbellbeachball', '(b[^b]+)(b[^b]+)(b[^b]+)', 'ig');
  628. ----
  629. {barf,Bonk,bil}
  630. {bo,baggins,bar}
  631. {Bar,Beque,bazil}
  632. {bell,beach,ball}
  633. statement OK
  634. CREATE TABLE foo(bar text, pattern text);
  635. statement OK
  636. insert into foo values ('fooBarBequebazilbarfBonkbilbobagginsbarbellbeachball', '(b[^b]+)(b[^b]+)(b[^b]+)'), ('bellbeachball', '(b[^b]+)(b[^b]+)(b[^b]+)');
  637. query T rowsort
  638. SELECT regexp_matches(bar, pattern, 'ig') FROM foo;
  639. ----
  640. {barf,Bonk,bil}
  641. {bo,baggins,bar}
  642. {Bar,Beque,bazil}
  643. {bell,beach,ball}
  644. {bell,beach,ball}
  645. query T
  646. SELECT regexp_matches('abc', '', 'ig');
  647. ----
  648. {}
  649. {}
  650. {}
  651. {}
  652. statement ok
  653. CREATE TABLE foo2(bar int2);
  654. statement ok
  655. insert into foo2 values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
  656. simple
  657. SELECT regexp_matches(bar, '(b[^b]+)(b[^b]+)(b[^b]+)', 'ig') FROM foo2;
  658. ----
  659. db error: ERROR: function regexp_matches(smallint, unknown, unknown) does not exist
  660. HINT: No function matches the given name and argument types. You might need to add explicit type casts.
  661. statement ok
  662. DROP CLUSTER multiprocess;