aoc_1205.slt 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. # https://github.com/MaterializeInc/advent-of-code-2023/blob/main/week1/aoc_1205.md
  10. mode cockroach
  11. statement ok
  12. CREATE TABLE input (input TEXT);
  13. statement ok
  14. INSERT INTO input VALUES (
  15. 'seeds: 141812878 853583433 69532151 734372491 182396959 4723992392 8947211973 5 4238233746 414976297 3674819199 51868842
  16. <EMPTY_LINE>
  17. seed-to-soil map:
  18. 47738968 98357 182795944
  19. 7626588292 7848955494 927588242
  20. 34324971 6812781938 837374212
  21. 2166766538 726174459 6843311291
  22. 239754864 684391 555585341
  23. 418284536 8839949654 642749254
  24. 9423585483 8225843682 329442345
  25. 8712565737 9399753119 5
  26. 74289536 9359574415 717691786
  27. <EMPTY_LINE>
  28. soil-to-fertilizer map:
  29. 8986393527 4888517941 585279262
  30. 7613871113 9877446651 84269233
  31. 2423271674 7586366221 7659569
  32. 3622861142 916381775 74546981
  33. 3996339781 3244535459 98765225
  34. 586791635 8419253759 59179897
  35. 7576358959 6127297299 519542837
  36. 7876479671 6556651697 2721518
  37. 8412917365 865866259 75627132
  38. 8919467753 2818783878 5
  39. 4729257824 8988777624 275397538
  40. 2626168725 5 375375337
  41. 7963626731 8731755354 562155822
  42. 4484955989 2 31166388
  43. 6394952326 9434564563 344217764
  44. 5818263571 8292867479 46133627
  45. 6635767943 663896613 39649384
  46. 1179179943 58595967 326966131
  47. 461712455 2756694296 566766331
  48. 267854195 942359332 92191415
  49. 1676162127 2415173728 7527572
  50. 8235458679 517558444 8481131
  51. 2193675972 5252589743 48214883
  52. 4312181391 5588755717 5725572
  53. 451918428 7726618552 39256967
  54. 4734828421 378251365 56849366
  55. 7864169188 795712941 852726511
  56. 6498165846 819955597 825591722
  57. 6737759128 1943367748 153632389
  58. 81836871 3619315963 5999745237
  59. 5533126476 1625724578 11196339
  60. 814358688 9878499637 982477257
  61. 4386394269 3737453299 68828912
  62. 1 535278815 55
  63. 9455234351 63518366 96697897
  64. 298142454 9711262844 545237968
  65. 6548387663 5498764299 556695254
  66. 4421182848 3813437218 18532535
  67. <EMPTY_LINE>
  68. fertilizer-to-water map:
  69. 5188185212 713592736 572122634
  70. 3996516993 8368372653 29257663
  71. 7629744559 8723951436 36722366
  72. 517117434 6799613578 62912912
  73. 7444887598 1378359775 142239913
  74. 8946622323 461231638 98648353
  75. 895954325 5555555 419161656
  76. 1269235415 8199792497 62639226
  77. 277191562 35368196 783296989
  78. 2994552458 6347418557 828619684
  79. 7 245618496 85983123
  80. 599564274 6975655213 441448691
  81. 7312274117 4916534261 32238555
  82. 444977452 8543586232 53382156
  83. 682172835 2567878724 62974419
  84. 8912874438 5539912916 51886594
  85. 4257175799 1517431879 97672792
  86. 9481282796 996563263 8713257
  87. 8269679713 2281488697 77222754
  88. 2566468873 4747453236 37516457
  89. 9491212658 1459322382 56814988
  90. 142239541 25962654 99568155
  91. 3475998687 9 537527222
  92. 7223664422 4413723656 2795999379
  93. 5781744284 5851593946 52411132
  94. 2615616119 2198338791 93894182
  95. 725796923 6293593866 672373647
  96. 5291123587 1 45165371
  97. <EMPTY_LINE>
  98. water-to-light map:
  99. 9483826148 971239373 57981779
  100. 866572267 5355262121 658481698
  101. 6149496513 2245492751 89485475
  102. 7743881746 5949772182 31781539
  103. 915838939 73797715 9628255536
  104. 49687174 729495672 53487321
  105. 1774464825 4513912578 89436881
  106. 734978995 62382679 39621782
  107. 8579142462 9 79959373
  108. 3999434125 1722538882 315735941
  109. 9758171681 5256942648 79846743
  110. 676724382 222319917 43462458
  111. 9842566327 2265547154 99978196
  112. 3885113248 6341877917 53731483
  113. 7451763582 1 22747236
  114. 2222665944 8558249644 858131997
  115. 2222613482 1369627666 23224913
  116. 2122417343 892313426 54167146
  117. 9888331148 1128482359 94371883
  118. 55684927 195154981 59748169
  119. 8819525361 7614218117 73293139
  120. 241798649 664138941 854619722
  121. 9716684718 3664657195 253795579
  122. 3431191398 9414159725 8345323
  123. 4742633341 757873782 42893665
  124. 5976897431 6681469458 1532556
  125. 5 947566478 61872123
  126. 8793878654 8231529123 34617151
  127. 136924369 6845534171 1385592
  128. 538695647 894877235 85583498
  129. 3327525594 1748821684 434778753
  130. <EMPTY_LINE>
  131. light-to-temperature map:
  132. 6778296876 1588184826 9373785
  133. 5958282911 253795579 867819861
  134. 422679395 2761173364 193474551
  135. 253795579 4218627316 4
  136. 5652112248 987641937 721917256
  137. 2349984397 7942568282 71771239
  138. 8969933137 5781654773 394652
  139. 1898117348 8342374669 537694616
  140. 4219243162 8214944547 736996395
  141. 5912961492 8868498531 27622883
  142. 346958533 4173157962 51979735
  143. 1916631453 9117978288 18581458
  144. 2945679922 4497199716 5152578
  145. 9179535252 5162391577 549347823
  146. <EMPTY_LINE>
  147. temperature-to-humidity map:
  148. 7239471736 657945134 92565479
  149. 4691325348 8569173453 532754925
  150. 5919891224 6397216467 3372871
  151. 3822197491 2782624568 698583215
  152. 6125121482 639986598 98215788
  153. 6574396598 665121945 619454968
  154. 2412529992 2776226287 1626339
  155. 2272785524 417214458 621918434
  156. 5629812155 68588342 911161561
  157. 9324482921 2524842229 67458368
  158. 5932612196 2357165667 93374591
  159. 1274118925 4785728782 685125685
  160. 4352611699 7119321598 69449264
  161. 6387566129 5474547438 5352628
  162. 3928922836 7972535629 916644549
  163. 1662726326 6558668448 362791743
  164. 836597248 451582864 36438897
  165. 738226654 681268313 785154987
  166. 9563614959 8661586162 448483715
  167. 9984139884 5113486123 19999477
  168. 2755987212 2214749752 872249174
  169. 887416884 6486692479 38549881
  170. <EMPTY_LINE>
  171. humidity-to-location map:
  172. 858893435 311438446 562265762
  173. 2454586869 3679665416 225353643
  174. 9519322712 63232264 128625398
  175. 441771557 9 2371977356
  176. 5 8193625898 775413179
  177. 1952973915 6235154499 759143921
  178. 6572 58451 75391174
  179. 6941443142 589862612 261347316');
  180. statement ok
  181. UPDATE input SET input = replace(input, '<EMPTY_LINE>', '');
  182. query I
  183. WITH seeds AS (
  184. SELECT
  185. regexp_split_to_table(
  186. regexp_split_to_array(
  187. regexp_split_to_array(input, '\n')[1],
  188. ': '
  189. )[2],
  190. ' '
  191. )::bigint AS seed
  192. FROM
  193. input
  194. ),
  195. seed_to_soil_lines AS (
  196. SELECT
  197. regexp_split_to_array(
  198. regexp_split_to_table(
  199. regexp_match(input, 'seed-to-soil map:\n([0-9 \n]*?)\n\n')[1],
  200. '\n'
  201. ),
  202. ' '
  203. )::bigint[] AS line
  204. FROM
  205. input
  206. ),
  207. seed_to_soil AS (
  208. SELECT
  209. line[1] AS dst_base,
  210. line[2] AS src_base,
  211. line[3] AS len
  212. FROM
  213. seed_to_soil_lines
  214. ),
  215. soil_to_fertilizer_lines AS (
  216. SELECT
  217. regexp_split_to_array(
  218. regexp_split_to_table(
  219. regexp_match(input, 'soil-to-fertilizer map:\n([0-9 \n]*?)\n\n')[1],
  220. '\n'
  221. ),
  222. ' '
  223. )::bigint[] AS line
  224. FROM
  225. input
  226. ),
  227. soil_to_fertilizer AS (
  228. SELECT
  229. line[1] AS dst_base,
  230. line[2] AS src_base,
  231. line[3] AS len
  232. FROM
  233. soil_to_fertilizer_lines
  234. ),
  235. fertilizer_to_water_lines AS (
  236. SELECT
  237. regexp_split_to_array(
  238. regexp_split_to_table(
  239. regexp_match(input, 'fertilizer-to-water map:\n([0-9 \n]*?)\n\n')[1],
  240. '\n'
  241. ),
  242. ' '
  243. )::bigint[] AS line
  244. FROM
  245. input
  246. ),
  247. fertilizer_to_water AS (
  248. SELECT
  249. line[1] AS dst_base,
  250. line[2] AS src_base,
  251. line[3] AS len
  252. FROM
  253. fertilizer_to_water_lines
  254. ),
  255. water_to_light_lines AS (
  256. SELECT
  257. regexp_split_to_array(
  258. regexp_split_to_table(
  259. regexp_match(input, 'water-to-light map:\n([0-9 \n]*?)\n\n')[1],
  260. '\n'
  261. ),
  262. ' '
  263. )::bigint[] AS line
  264. FROM
  265. input
  266. ),
  267. water_to_light AS (
  268. SELECT
  269. line[1] AS dst_base,
  270. line[2] AS src_base,
  271. line[3] AS len
  272. FROM
  273. water_to_light_lines
  274. ),
  275. light_to_temperature_lines AS (
  276. SELECT
  277. regexp_split_to_array(
  278. regexp_split_to_table(
  279. regexp_match(input, 'light-to-temperature map:\n([0-9 \n]*?)\n\n')[1],
  280. '\n'
  281. ),
  282. ' '
  283. )::bigint[] AS line
  284. FROM
  285. input
  286. ),
  287. light_to_temperature AS (
  288. SELECT
  289. line[1] AS dst_base,
  290. line[2] AS src_base,
  291. line[3] AS len
  292. FROM
  293. light_to_temperature_lines
  294. ),
  295. temperature_to_humidity_lines AS (
  296. SELECT
  297. regexp_split_to_array(
  298. regexp_split_to_table(
  299. regexp_match(input, 'temperature-to-humidity map:\n([0-9 \n]*?)\n\n')[1],
  300. '\n'
  301. ),
  302. ' '
  303. )::bigint[] AS line
  304. FROM
  305. input
  306. ),
  307. temperature_to_humidity AS (
  308. SELECT
  309. line[1] AS dst_base,
  310. line[2] AS src_base,
  311. line[3] AS len
  312. FROM
  313. temperature_to_humidity_lines
  314. ),
  315. humidity_to_location_lines AS (
  316. SELECT
  317. regexp_split_to_array(
  318. regexp_split_to_table(
  319. regexp_match(input, 'humidity-to-location map:\n([0-9 \n]*)')[1],
  320. '\n'
  321. ),
  322. ' '
  323. )::bigint[] AS line
  324. FROM
  325. input
  326. ),
  327. humidity_to_location AS (
  328. SELECT
  329. line[1] AS dst_base,
  330. line[2] AS src_base,
  331. line[3] AS len
  332. FROM
  333. humidity_to_location_lines
  334. ),
  335. soil AS (
  336. SELECT
  337. seed,
  338. coalesce(
  339. MAX(
  340. CASE
  341. WHEN seed >= src_base AND seed < src_base + len
  342. THEN dst_base + (seed - src_base)
  343. ELSE null
  344. END
  345. ),
  346. seed
  347. ) AS soil
  348. FROM
  349. seeds, seed_to_soil
  350. GROUP BY seed
  351. ),
  352. fertilizer AS (
  353. SELECT
  354. soil,
  355. coalesce(
  356. MAX(
  357. CASE
  358. WHEN soil >= src_base AND soil < src_base + len
  359. THEN dst_base + (soil - src_base)
  360. ELSE null
  361. END
  362. ),
  363. soil
  364. ) AS fertilizer
  365. FROM
  366. soil, soil_to_fertilizer
  367. GROUP BY soil
  368. ),
  369. water AS (
  370. SELECT
  371. fertilizer,
  372. coalesce(
  373. MAX(
  374. CASE
  375. when fertilizer >= src_base AND fertilizer < src_base + len
  376. then dst_base + (fertilizer - src_base)
  377. else null
  378. END
  379. ),
  380. fertilizer
  381. ) AS water
  382. FROM
  383. fertilizer, fertilizer_to_water
  384. GROUP BY fertilizer
  385. ),
  386. light AS (
  387. SELECT
  388. water,
  389. coalesce(
  390. MAX(
  391. CASE
  392. WHEN water >= src_base AND water < src_base + len
  393. THEN dst_base + (water - src_base)
  394. ELSE null
  395. END
  396. ),
  397. water
  398. ) AS light
  399. FROM
  400. water, water_to_light
  401. GROUP BY water
  402. ),
  403. temperature AS (
  404. SELECT
  405. light,
  406. coalesce(
  407. MAX(
  408. CASE
  409. WHEN light >= src_base AND light < src_base + len
  410. THEN dst_base + (light - src_base)
  411. ELSE null
  412. END
  413. ),
  414. light
  415. ) AS temperature
  416. FROM
  417. light, light_to_temperature
  418. GROUP BY light
  419. ),
  420. humidity AS (
  421. SELECT
  422. temperature,
  423. coalesce(
  424. MAX(
  425. CASE
  426. WHEN temperature >= src_base AND temperature < src_base + len
  427. THEN dst_base + (temperature - src_base)
  428. ELSE null
  429. END
  430. ),
  431. temperature
  432. ) AS humidity
  433. FROM
  434. temperature, temperature_to_humidity
  435. GROUP BY temperature
  436. ),
  437. location AS (
  438. SELECT
  439. humidity,
  440. coalesce(
  441. MAX(
  442. CASE
  443. WHEN humidity >= src_base AND humidity < src_base + len
  444. THEN dst_base + (humidity - src_base)
  445. ELSE null
  446. END
  447. ),
  448. humidity
  449. ) AS location
  450. FROM
  451. humidity, humidity_to_location
  452. GROUP BY humidity
  453. )
  454. SELECT
  455. MIN(location) AS answer
  456. FROM
  457. location;
  458. ----
  459. 2364832465
  460. query T multiline
  461. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  462. WITH seeds AS (
  463. SELECT
  464. regexp_split_to_table(
  465. regexp_split_to_array(
  466. regexp_split_to_array(input, '\n')[1],
  467. ': '
  468. )[2],
  469. ' '
  470. )::bigint AS seed
  471. FROM
  472. input
  473. ),
  474. seed_to_soil_lines AS (
  475. SELECT
  476. regexp_split_to_array(
  477. regexp_split_to_table(
  478. regexp_match(input, 'seed-to-soil map:\n([0-9 \n]*?)\n\n')[1],
  479. '\n'
  480. ),
  481. ' '
  482. )::bigint[] AS line
  483. FROM
  484. input
  485. ),
  486. seed_to_soil AS (
  487. SELECT
  488. line[1] AS dst_base,
  489. line[2] AS src_base,
  490. line[3] AS len
  491. FROM
  492. seed_to_soil_lines
  493. ),
  494. soil_to_fertilizer_lines AS (
  495. SELECT
  496. regexp_split_to_array(
  497. regexp_split_to_table(
  498. regexp_match(input, 'soil-to-fertilizer map:\n([0-9 \n]*?)\n\n')[1],
  499. '\n'
  500. ),
  501. ' '
  502. )::bigint[] AS line
  503. FROM
  504. input
  505. ),
  506. soil_to_fertilizer AS (
  507. SELECT
  508. line[1] AS dst_base,
  509. line[2] AS src_base,
  510. line[3] AS len
  511. FROM
  512. soil_to_fertilizer_lines
  513. ),
  514. fertilizer_to_water_lines AS (
  515. SELECT
  516. regexp_split_to_array(
  517. regexp_split_to_table(
  518. regexp_match(input, 'fertilizer-to-water map:\n([0-9 \n]*?)\n\n')[1],
  519. '\n'
  520. ),
  521. ' '
  522. )::bigint[] AS line
  523. FROM
  524. input
  525. ),
  526. fertilizer_to_water AS (
  527. SELECT
  528. line[1] AS dst_base,
  529. line[2] AS src_base,
  530. line[3] AS len
  531. FROM
  532. fertilizer_to_water_lines
  533. ),
  534. water_to_light_lines AS (
  535. SELECT
  536. regexp_split_to_array(
  537. regexp_split_to_table(
  538. regexp_match(input, 'water-to-light map:\n([0-9 \n]*?)\n\n')[1],
  539. '\n'
  540. ),
  541. ' '
  542. )::bigint[] AS line
  543. FROM
  544. input
  545. ),
  546. water_to_light AS (
  547. SELECT
  548. line[1] AS dst_base,
  549. line[2] AS src_base,
  550. line[3] AS len
  551. FROM
  552. water_to_light_lines
  553. ),
  554. light_to_temperature_lines AS (
  555. SELECT
  556. regexp_split_to_array(
  557. regexp_split_to_table(
  558. regexp_match(input, 'light-to-temperature map:\n([0-9 \n]*?)\n\n')[1],
  559. '\n'
  560. ),
  561. ' '
  562. )::bigint[] AS line
  563. FROM
  564. input
  565. ),
  566. light_to_temperature AS (
  567. SELECT
  568. line[1] AS dst_base,
  569. line[2] AS src_base,
  570. line[3] AS len
  571. FROM
  572. light_to_temperature_lines
  573. ),
  574. temperature_to_humidity_lines AS (
  575. SELECT
  576. regexp_split_to_array(
  577. regexp_split_to_table(
  578. regexp_match(input, 'temperature-to-humidity map:\n([0-9 \n]*?)\n\n')[1],
  579. '\n'
  580. ),
  581. ' '
  582. )::bigint[] AS line
  583. FROM
  584. input
  585. ),
  586. temperature_to_humidity AS (
  587. SELECT
  588. line[1] AS dst_base,
  589. line[2] AS src_base,
  590. line[3] AS len
  591. FROM
  592. temperature_to_humidity_lines
  593. ),
  594. humidity_to_location_lines AS (
  595. SELECT
  596. regexp_split_to_array(
  597. regexp_split_to_table(
  598. regexp_match(input, 'humidity-to-location map:\n([0-9 \n]*)')[1],
  599. '\n'
  600. ),
  601. ' '
  602. )::bigint[] AS line
  603. FROM
  604. input
  605. ),
  606. humidity_to_location AS (
  607. SELECT
  608. line[1] AS dst_base,
  609. line[2] AS src_base,
  610. line[3] AS len
  611. FROM
  612. humidity_to_location_lines
  613. ),
  614. soil AS (
  615. SELECT
  616. seed,
  617. coalesce(
  618. MAX(
  619. CASE
  620. WHEN seed >= src_base AND seed < src_base + len
  621. THEN dst_base + (seed - src_base)
  622. ELSE null
  623. END
  624. ),
  625. seed
  626. ) AS soil
  627. FROM
  628. seeds, seed_to_soil
  629. GROUP BY seed
  630. ),
  631. fertilizer AS (
  632. SELECT
  633. soil,
  634. coalesce(
  635. MAX(
  636. CASE
  637. WHEN soil >= src_base AND soil < src_base + len
  638. THEN dst_base + (soil - src_base)
  639. ELSE null
  640. END
  641. ),
  642. soil
  643. ) AS fertilizer
  644. FROM
  645. soil, soil_to_fertilizer
  646. GROUP BY soil
  647. ),
  648. water AS (
  649. SELECT
  650. fertilizer,
  651. coalesce(
  652. MAX(
  653. CASE
  654. when fertilizer >= src_base AND fertilizer < src_base + len
  655. then dst_base + (fertilizer - src_base)
  656. else null
  657. END
  658. ),
  659. fertilizer
  660. ) AS water
  661. FROM
  662. fertilizer, fertilizer_to_water
  663. GROUP BY fertilizer
  664. ),
  665. light AS (
  666. SELECT
  667. water,
  668. coalesce(
  669. MAX(
  670. CASE
  671. WHEN water >= src_base AND water < src_base + len
  672. THEN dst_base + (water - src_base)
  673. ELSE null
  674. END
  675. ),
  676. water
  677. ) AS light
  678. FROM
  679. water, water_to_light
  680. GROUP BY water
  681. ),
  682. temperature AS (
  683. SELECT
  684. light,
  685. coalesce(
  686. MAX(
  687. CASE
  688. WHEN light >= src_base AND light < src_base + len
  689. THEN dst_base + (light - src_base)
  690. ELSE null
  691. END
  692. ),
  693. light
  694. ) AS temperature
  695. FROM
  696. light, light_to_temperature
  697. GROUP BY light
  698. ),
  699. humidity AS (
  700. SELECT
  701. temperature,
  702. coalesce(
  703. MAX(
  704. CASE
  705. WHEN temperature >= src_base AND temperature < src_base + len
  706. THEN dst_base + (temperature - src_base)
  707. ELSE null
  708. END
  709. ),
  710. temperature
  711. ) AS humidity
  712. FROM
  713. temperature, temperature_to_humidity
  714. GROUP BY temperature
  715. ),
  716. location AS (
  717. SELECT
  718. humidity,
  719. coalesce(
  720. MAX(
  721. CASE
  722. WHEN humidity >= src_base AND humidity < src_base + len
  723. THEN dst_base + (humidity - src_base)
  724. ELSE null
  725. END
  726. ),
  727. humidity
  728. ) AS location
  729. FROM
  730. humidity, humidity_to_location
  731. GROUP BY humidity
  732. )
  733. SELECT
  734. MIN(location) AS answer
  735. FROM
  736. location;
  737. ----
  738. Explained Query:
  739. With
  740. cte l0 =
  741. Reduce aggregates=[min(coalesce(#1{max}, #0{humidity}))] // { arity: 1 }
  742. Reduce group_by=[#0] aggregates=[max(case when ((#0{humidity} < (#2{src_base} + #3{len})) AND (#0{humidity} >= #2{src_base})) then (#1{dst_base} + (#0{humidity} - #2{src_base})) else null end)] // { arity: 2 }
  743. CrossJoin type=differential // { arity: 4 }
  744. implementation
  745. %0[×] » %1[×]
  746. ArrangeBy keys=[[]] // { arity: 1 }
  747. Project (#2) // { arity: 1 }
  748. Map (coalesce(#1{max}, #0{temperature})) // { arity: 3 }
  749. Reduce group_by=[#0] aggregates=[max(case when ((#0{temperature} < (#2{src_base} + #3{len})) AND (#0{temperature} >= #2{src_base})) then (#1{dst_base} + (#0{temperature} - #2{src_base})) else null end)] // { arity: 2 }
  750. CrossJoin type=differential // { arity: 4 }
  751. implementation
  752. %0[×] » %1[×]
  753. ArrangeBy keys=[[]] // { arity: 1 }
  754. Project (#2) // { arity: 1 }
  755. Map (coalesce(#1{max}, #0{light})) // { arity: 3 }
  756. Reduce group_by=[#0] aggregates=[max(case when ((#0{light} < (#2{src_base} + #3{len})) AND (#0{light} >= #2{src_base})) then (#1{dst_base} + (#0{light} - #2{src_base})) else null end)] // { arity: 2 }
  757. CrossJoin type=differential // { arity: 4 }
  758. implementation
  759. %0[×] » %1[×]
  760. ArrangeBy keys=[[]] // { arity: 1 }
  761. Project (#2) // { arity: 1 }
  762. Map (coalesce(#1{max}, #0{water})) // { arity: 3 }
  763. Reduce group_by=[#0] aggregates=[max(case when ((#0{water} < (#2{src_base} + #3{len})) AND (#0{water} >= #2{src_base})) then (#1{dst_base} + (#0{water} - #2{src_base})) else null end)] // { arity: 2 }
  764. CrossJoin type=differential // { arity: 4 }
  765. implementation
  766. %0[×] » %1[×]
  767. ArrangeBy keys=[[]] // { arity: 1 }
  768. Project (#2) // { arity: 1 }
  769. Map (coalesce(#1{max}, #0{fertilizer})) // { arity: 3 }
  770. Reduce group_by=[#0] aggregates=[max(case when ((#0{fertilizer} < (#2{src_base} + #3{len})) AND (#0{fertilizer} >= #2{src_base})) then (#1{dst_base} + (#0{fertilizer} - #2{src_base})) else null end)] // { arity: 2 }
  771. CrossJoin type=differential // { arity: 4 }
  772. implementation
  773. %0[×] » %1[×]
  774. ArrangeBy keys=[[]] // { arity: 1 }
  775. Project (#2) // { arity: 1 }
  776. Map (coalesce(#1{max}, #0{soil})) // { arity: 3 }
  777. Reduce group_by=[#0] aggregates=[max(case when ((#0{soil} < (#2{src_base} + #3{len})) AND (#0{soil} >= #2{src_base})) then (#1{dst_base} + (#0{soil} - #2{src_base})) else null end)] // { arity: 2 }
  778. CrossJoin type=differential // { arity: 4 }
  779. implementation
  780. %0[×] » %1[×]
  781. ArrangeBy keys=[[]] // { arity: 1 }
  782. Project (#2) // { arity: 1 }
  783. Map (coalesce(#1{max}, #0{seed})) // { arity: 3 }
  784. Reduce group_by=[#0] aggregates=[max(case when ((#0{seed} < (#2{src_base} + #3{len})) AND (#0{seed} >= #2{src_base})) then (#1{dst_base} + (#0{seed} - #2{src_base})) else null end)] // { arity: 2 }
  785. CrossJoin type=differential // { arity: 4 }
  786. implementation
  787. %0[×] » %1[×]
  788. ArrangeBy keys=[[]] // { arity: 1 }
  789. Project (#2) // { arity: 1 }
  790. Map (text_to_bigint(#1{unnest})) // { arity: 3 }
  791. FlatMap unnest_array(regexp_split_to_array[" ", case_insensitive=false](array_index(regexp_split_to_array[": ", case_insensitive=false](array_index(regexp_split_to_array["\n", case_insensitive=false](#0{input}), 1)), 2))) // { arity: 2 }
  792. ReadStorage materialize.public.input // { arity: 1 }
  793. ArrangeBy keys=[[]] // { arity: 3 }
  794. Project (#3..=#5) // { arity: 3 }
  795. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  796. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["seed-to-soil map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  797. ReadStorage materialize.public.input // { arity: 1 }
  798. ArrangeBy keys=[[]] // { arity: 3 }
  799. Project (#3..=#5) // { arity: 3 }
  800. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  801. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["soil-to-fertilizer map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  802. ReadStorage materialize.public.input // { arity: 1 }
  803. ArrangeBy keys=[[]] // { arity: 3 }
  804. Project (#3..=#5) // { arity: 3 }
  805. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  806. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["fertilizer-to-water map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  807. ReadStorage materialize.public.input // { arity: 1 }
  808. ArrangeBy keys=[[]] // { arity: 3 }
  809. Project (#3..=#5) // { arity: 3 }
  810. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  811. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["water-to-light map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  812. ReadStorage materialize.public.input // { arity: 1 }
  813. ArrangeBy keys=[[]] // { arity: 3 }
  814. Project (#3..=#5) // { arity: 3 }
  815. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  816. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["light-to-temperature map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  817. ReadStorage materialize.public.input // { arity: 1 }
  818. ArrangeBy keys=[[]] // { arity: 3 }
  819. Project (#3..=#5) // { arity: 3 }
  820. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  821. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["temperature-to-humidity map:\n([0-9 \n]*?)\n\n", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  822. ReadStorage materialize.public.input // { arity: 1 }
  823. ArrangeBy keys=[[]] // { arity: 3 }
  824. Project (#3..=#5) // { arity: 3 }
  825. Map (arraytoarray(regexp_split_to_array[" ", case_insensitive=false](#1{unnest})), array_index(#2{line}, 1), array_index(#2{line}, 2), array_index(#2{line}, 3)) // { arity: 6 }
  826. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](array_index(regexp_match["humidity-to-location map:\n([0-9 \n]*)", case_insensitive=false](#0{input}), 1))) // { arity: 2 }
  827. ReadStorage materialize.public.input // { arity: 1 }
  828. Return // { arity: 1 }
  829. Union // { arity: 1 }
  830. Get l0 // { arity: 1 }
  831. Map (null) // { arity: 1 }
  832. Union // { arity: 0 }
  833. Negate // { arity: 0 }
  834. Project () // { arity: 0 }
  835. Get l0 // { arity: 1 }
  836. Constant // { arity: 0 }
  837. - ()
  838. Source materialize.public.input
  839. Target cluster: quickstart
  840. EOF
  841. query II
  842. WITH MUTUALLY RECURSIVE
  843. blocks(head TEXT, body TEXT) AS (
  844. SELECT
  845. split_part(regexp_split_to_table(input, '\n\n'), ':', 1),
  846. split_part(regexp_split_to_table(input, '\n\n'), ':', 2)
  847. FROM
  848. input
  849. ),
  850. seeds(seed BIGINT) AS (
  851. SELECT regexp_split_to_table(trim(body), ' ')::BIGINT
  852. FROM blocks
  853. WHERE head = 'seeds'
  854. ),
  855. entry0(src_name TEXT, dst_name TEXT, dst_idx TEXT, src_idx TEXT, len TEXT) AS (
  856. SELECT
  857. split_part(split_part(head, ' ', 1), '-', 1),
  858. split_part(split_part(head, ' ', 1), '-', 3),
  859. split_part(regexp_split_to_table(body, '\n'), ' ', 1),
  860. split_part(regexp_split_to_table(body, '\n'), ' ', 2),
  861. split_part(regexp_split_to_table(body, '\n'), ' ', 3)
  862. FROM
  863. blocks
  864. WHERE
  865. head != 'seeds'
  866. ),
  867. entry(src_name TEXT, dst_name TEXT, src_idx BIGINT, dst_idx BIGINT, len BIGINT) AS (
  868. SELECT
  869. src_name,
  870. dst_name,
  871. src_idx::BIGINT,
  872. dst_idx::BIGINT,
  873. len::BIGINT
  874. FROM
  875. entry0
  876. WHERE
  877. src_idx != ''
  878. ),
  879. -- PART 1
  880. -- Our active inventory of .. "stuff"
  881. active(name TEXT, idx BIGINT) AS (
  882. SELECT 'seed', seed FROM seeds
  883. UNION ALL
  884. SELECT
  885. intent.dst_name,
  886. COALESCE(intent.idx + (entry.dst_idx - entry.src_idx), idx)
  887. FROM intent LEFT JOIN entry ON (
  888. intent.src_name = entry.src_name AND
  889. intent.dst_name = entry.dst_name AND
  890. intent.idx BETWEEN entry.src_idx AND entry.src_idx + len - 1)
  891. ),
  892. -- We would like to perform this mapping, but must find a range.
  893. intent(src_name TEXT, dst_name TEXT, idx BIGINT) AS (
  894. SELECT DISTINCT entry.src_name, dst_name, idx
  895. FROM active, entry
  896. WHERE active.name = entry.src_name
  897. ),
  898. part1(part1 BIGINT) AS (
  899. SELECT MIN(idx) FROM active WHERE name = 'location'
  900. ),
  901. -- PART 2
  902. -- Now we are doing *ranges* of seeds, rather than seed identifiers.
  903. -- They are big ranges, so we'll need to be smarter!
  904. seeds2(start_idx BIGINT, end_idx BIGINT) AS (
  905. SELECT
  906. regexp_split_to_array(trim(body), ' ')[2*x-1]::BIGINT,
  907. regexp_split_to_array(trim(body), ' ')[2*x-1]::BIGINT + regexp_split_to_array(trim(body), ' ')[2*x]::BIGINT
  908. FROM
  909. blocks,
  910. generate_series(1, array_length(regexp_split_to_array(trim(body), ' '), 1)/2) x
  911. WHERE head = 'seeds'
  912. ),
  913. active2(name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  914. SELECT 'seed', start_idx, end_idx
  915. FROM seeds2
  916. UNION
  917. SELECT
  918. dst_name,
  919. clipped_start + (entry_dst - entry_start),
  920. clipped_end + (entry_dst - entry_start)
  921. FROM intersection
  922. UNION
  923. SELECT
  924. name,
  925. start_idx,
  926. end_idx
  927. FROM hole
  928. ),
  929. -- We would like to perform this mapping, but must find a range.
  930. intent2(src_name TEXT, dst_name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  931. SELECT DISTINCT entry.src_name, dst_name, start_idx, end_idx
  932. FROM active2, entry
  933. WHERE active2.name = entry.src_name
  934. ),
  935. -- Each mapping has a potential intersection with a requested range.
  936. intersection(src_name TEXT, dst_name TEXT, start_idx BIGINT, end_idx BIGINT, entry_start BIGINT, entry_end BIGINT, clipped_start BIGINT, clipped_end BIGINT, entry_dst BIGINT) AS (
  937. SELECT
  938. intent2.src_name,
  939. intent2.dst_name,
  940. intent2.start_idx,
  941. intent2.end_idx,
  942. entry.src_idx,
  943. entry.src_idx + entry.len,
  944. GREATEST(start_idx, entry.src_idx),
  945. LEAST(end_idx, entry.src_idx + entry.len),
  946. entry.dst_idx
  947. FROM intent2, entry
  948. WHERE intent2.src_name = entry.src_name
  949. AND intent2.dst_name = entry.dst_name
  950. AND GREATEST(intent2.start_idx, entry.src_idx)
  951. < LEAST(intent2.end_idx, entry.src_idx + entry.len)
  952. ),
  953. -- We may have holes in our intervals. Each intersection's start and end is the end and
  954. -- start, respectively, of some hole we may have that needs to remain the identity.
  955. hole(name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  956. SELECT * FROM (
  957. SELECT
  958. dst_name,
  959. clipped_end start_idx,
  960. (
  961. SELECT COALESCE(MIN(i2.clipped_start), i1.end_idx)
  962. FROM intersection i2
  963. WHERE i2.clipped_start >= i1.clipped_end
  964. AND i2.clipped_start < i1.end_idx
  965. AND i1.src_name = i2.src_name
  966. AND i1.dst_name = i2.dst_name
  967. AND i1.start_idx = i2.start_idx
  968. AND i1.end_idx = i2.end_idx
  969. ) end_idx
  970. FROM intersection i1
  971. UNION
  972. SELECT DISTINCT
  973. dst_name,
  974. start_idx,
  975. (
  976. SELECT COALESCE(MIN(i2.clipped_start), i1.end_idx)
  977. FROM intersection i2
  978. WHERE i2.clipped_start >= i1.start_idx
  979. AND i2.clipped_start < i1.end_idx
  980. AND i1.src_name = i2.src_name
  981. AND i1.dst_name = i2.dst_name
  982. AND i1.start_idx = i2.start_idx
  983. AND i1.end_idx = i2.end_idx
  984. )
  985. FROM intent2 i1
  986. )
  987. WHERE start_idx < end_idx
  988. ),
  989. part2(part2 BIGINT) AS ( SELECT MIN(start_idx) FROM active2 WHERE name = 'location')
  990. SELECT * FROM part1, part2;
  991. ----
  992. 60602459 5
  993. query T multiline
  994. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  995. WITH MUTUALLY RECURSIVE
  996. blocks(head TEXT, body TEXT) AS (
  997. SELECT
  998. split_part(regexp_split_to_table(input, '\n\n'), ':', 1),
  999. split_part(regexp_split_to_table(input, '\n\n'), ':', 2)
  1000. FROM
  1001. input
  1002. ),
  1003. seeds(seed BIGINT) AS (
  1004. SELECT regexp_split_to_table(trim(body), ' ')::BIGINT
  1005. FROM blocks
  1006. WHERE head = 'seeds'
  1007. ),
  1008. entry0(src_name TEXT, dst_name TEXT, dst_idx TEXT, src_idx TEXT, len TEXT) AS (
  1009. SELECT
  1010. split_part(split_part(head, ' ', 1), '-', 1),
  1011. split_part(split_part(head, ' ', 1), '-', 3),
  1012. split_part(regexp_split_to_table(body, '\n'), ' ', 1),
  1013. split_part(regexp_split_to_table(body, '\n'), ' ', 2),
  1014. split_part(regexp_split_to_table(body, '\n'), ' ', 3)
  1015. FROM
  1016. blocks
  1017. WHERE
  1018. head != 'seeds'
  1019. ),
  1020. entry(src_name TEXT, dst_name TEXT, src_idx BIGINT, dst_idx BIGINT, len BIGINT) AS (
  1021. SELECT
  1022. src_name,
  1023. dst_name,
  1024. src_idx::BIGINT,
  1025. dst_idx::BIGINT,
  1026. len::BIGINT
  1027. FROM
  1028. entry0
  1029. WHERE
  1030. src_idx != ''
  1031. ),
  1032. -- PART 1
  1033. -- Our active inventory of .. "stuff"
  1034. active(name TEXT, idx BIGINT) AS (
  1035. SELECT 'seed', seed FROM seeds
  1036. UNION ALL
  1037. SELECT
  1038. intent.dst_name,
  1039. COALESCE(intent.idx + (entry.dst_idx - entry.src_idx), idx)
  1040. FROM intent LEFT JOIN entry ON (
  1041. intent.src_name = entry.src_name AND
  1042. intent.dst_name = entry.dst_name AND
  1043. intent.idx BETWEEN entry.src_idx AND entry.src_idx + len - 1)
  1044. ),
  1045. -- We would like to perform this mapping, but must find a range.
  1046. intent(src_name TEXT, dst_name TEXT, idx BIGINT) AS (
  1047. SELECT DISTINCT entry.src_name, dst_name, idx
  1048. FROM active, entry
  1049. WHERE active.name = entry.src_name
  1050. ),
  1051. part1(part1 BIGINT) AS (
  1052. SELECT MIN(idx) FROM active WHERE name = 'location'
  1053. ),
  1054. -- PART 2
  1055. -- Now we are doing *ranges* of seeds, rather than seed identifiers.
  1056. -- They are big ranges, so we'll need to be smarter!
  1057. seeds2(start_idx BIGINT, end_idx BIGINT) AS (
  1058. SELECT
  1059. regexp_split_to_array(trim(body), ' ')[2*x-1]::BIGINT,
  1060. regexp_split_to_array(trim(body), ' ')[2*x-1]::BIGINT + regexp_split_to_array(trim(body), ' ')[2*x]::BIGINT
  1061. FROM
  1062. blocks,
  1063. generate_series(1, array_length(regexp_split_to_array(trim(body), ' '), 1)/2) x
  1064. WHERE head = 'seeds'
  1065. ),
  1066. active2(name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  1067. SELECT 'seed', start_idx, end_idx
  1068. FROM seeds2
  1069. UNION
  1070. SELECT
  1071. dst_name,
  1072. clipped_start + (entry_dst - entry_start),
  1073. clipped_end + (entry_dst - entry_start)
  1074. FROM intersection
  1075. UNION
  1076. SELECT
  1077. name,
  1078. start_idx,
  1079. end_idx
  1080. FROM hole
  1081. ),
  1082. -- We would like to perform this mapping, but must find a range.
  1083. intent2(src_name TEXT, dst_name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  1084. SELECT DISTINCT entry.src_name, dst_name, start_idx, end_idx
  1085. FROM active2, entry
  1086. WHERE active2.name = entry.src_name
  1087. ),
  1088. -- Each mapping has a potential intersection with a requested range.
  1089. intersection(src_name TEXT, dst_name TEXT, start_idx BIGINT, end_idx BIGINT, entry_start BIGINT, entry_end BIGINT, clipped_start BIGINT, clipped_end BIGINT, entry_dst BIGINT) AS (
  1090. SELECT
  1091. intent2.src_name,
  1092. intent2.dst_name,
  1093. intent2.start_idx,
  1094. intent2.end_idx,
  1095. entry.src_idx,
  1096. entry.src_idx + entry.len,
  1097. GREATEST(start_idx, entry.src_idx),
  1098. LEAST(end_idx, entry.src_idx + entry.len),
  1099. entry.dst_idx
  1100. FROM intent2, entry
  1101. WHERE intent2.src_name = entry.src_name
  1102. AND intent2.dst_name = entry.dst_name
  1103. AND GREATEST(intent2.start_idx, entry.src_idx)
  1104. < LEAST(intent2.end_idx, entry.src_idx + entry.len)
  1105. ),
  1106. -- We may have holes in our intervals. Each intersection's start and end is the end and
  1107. -- start, respectively, of some hole we may have that needs to remain the identity.
  1108. hole(name TEXT, start_idx BIGINT, end_idx BIGINT) AS (
  1109. SELECT * FROM (
  1110. SELECT
  1111. dst_name,
  1112. clipped_end start_idx,
  1113. (
  1114. SELECT COALESCE(MIN(i2.clipped_start), i1.end_idx)
  1115. FROM intersection i2
  1116. WHERE i2.clipped_start >= i1.clipped_end
  1117. AND i2.clipped_start < i1.end_idx
  1118. AND i1.src_name = i2.src_name
  1119. AND i1.dst_name = i2.dst_name
  1120. AND i1.start_idx = i2.start_idx
  1121. AND i1.end_idx = i2.end_idx
  1122. ) end_idx
  1123. FROM intersection i1
  1124. UNION
  1125. SELECT DISTINCT
  1126. dst_name,
  1127. start_idx,
  1128. (
  1129. SELECT COALESCE(MIN(i2.clipped_start), i1.end_idx)
  1130. FROM intersection i2
  1131. WHERE i2.clipped_start >= i1.start_idx
  1132. AND i2.clipped_start < i1.end_idx
  1133. AND i1.src_name = i2.src_name
  1134. AND i1.dst_name = i2.dst_name
  1135. AND i1.start_idx = i2.start_idx
  1136. AND i1.end_idx = i2.end_idx
  1137. )
  1138. FROM intent2 i1
  1139. )
  1140. WHERE start_idx < end_idx
  1141. ),
  1142. part2(part2 BIGINT) AS ( SELECT MIN(start_idx) FROM active2 WHERE name = 'location')
  1143. SELECT * FROM part1, part2;
  1144. ----
  1145. Explained Query:
  1146. With
  1147. cte l0 =
  1148. Project (#2, #3) // { arity: 2 }
  1149. Map (split_string(#1{unnest}, ":", 1), split_string(#1{unnest}, ":", 2)) // { arity: 4 }
  1150. FlatMap unnest_array(regexp_split_to_array["\n\n", case_insensitive=false](#0{input})) // { arity: 2 }
  1151. ReadStorage materialize.public.input // { arity: 1 }
  1152. cte l1 =
  1153. Project (#5..=#9) // { arity: 5 }
  1154. Filter (#3 != "") // { arity: 10 }
  1155. Map (split_string(#2{unnest}, " ", 2), split_string(#0{head}, " ", 1), split_string(#4, "-", 1), split_string(#4, "-", 3), text_to_bigint(#3{src_idx}), text_to_bigint(split_string(#2{unnest}, " ", 1)), text_to_bigint(split_string(#2{unnest}, " ", 3))) // { arity: 10 }
  1156. FlatMap unnest_array(regexp_split_to_array["\n", case_insensitive=false](#1{body})) // { arity: 3 }
  1157. Filter (#0{head} != "seeds") // { arity: 2 }
  1158. Get l0 // { arity: 2 }
  1159. cte l2 =
  1160. ArrangeBy keys=[[#0{src_name}, #1{dst_name}]] // { arity: 5 }
  1161. Get l1 // { arity: 5 }
  1162. cte l3 =
  1163. Project (#1) // { arity: 1 }
  1164. Filter (#0{head} = "seeds") // { arity: 2 }
  1165. Get l0 // { arity: 2 }
  1166. cte l4 =
  1167. ArrangeBy keys=[[#0{src_name}]] // { arity: 2 }
  1168. Distinct project=[#0{src_name}, #1] // { arity: 2 }
  1169. Project (#0, #1) // { arity: 2 }
  1170. Get l1 // { arity: 5 }
  1171. Return // { arity: 2 }
  1172. With Mutually Recursive
  1173. cte l5 =
  1174. Project (#0..=#2, #5, #6) // { arity: 5 }
  1175. Filter (#2{idx} >= #5{src_idx}) AND (#2{idx} <= ((#5{src_idx} + #7{len}) - 1)) // { arity: 8 }
  1176. Join on=(#0{src_name} = #3{src_name} AND #1{dst_name} = #4{dst_name}) type=differential // { arity: 8 }
  1177. implementation
  1178. %0:l7[#0{src_name}, #1{dst_name}]KK » %1:l2[#0{src_name}, #1{dst_name}]KK
  1179. ArrangeBy keys=[[#0{src_name}, #1{dst_name}]] // { arity: 3 }
  1180. Get l7 // { arity: 3 }
  1181. Get l2 // { arity: 5 }
  1182. cte l6 =
  1183. Union // { arity: 2 }
  1184. Project (#3, #2) // { arity: 2 }
  1185. Map (text_to_bigint(#1{unnest}), "seed") // { arity: 4 }
  1186. FlatMap unnest_array(regexp_split_to_array[" ", case_insensitive=false](btrim(#0{body}))) // { arity: 2 }
  1187. Get l3 // { arity: 1 }
  1188. Project (#0, #4) // { arity: 2 }
  1189. Map (coalesce((#1{idx} + (#3{dst_idx} - #2{src_idx})), #1{idx})) // { arity: 5 }
  1190. Union // { arity: 4 }
  1191. Project (#1..=#4) // { arity: 4 }
  1192. Get l5 // { arity: 5 }
  1193. Project (#1, #2, #6, #7) // { arity: 4 }
  1194. Map (null, null) // { arity: 8 }
  1195. Join on=(#0 = #3 AND #1 = #4 AND #2 = #5) type=differential // { arity: 6 }
  1196. implementation
  1197. %0[#0..=#2]KKK » %1:l7[#0..=#2]KKK
  1198. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  1199. Union // { arity: 3 }
  1200. Negate // { arity: 3 }
  1201. Distinct project=[#0..=#2] // { arity: 3 }
  1202. Project (#0..=#2) // { arity: 3 }
  1203. Get l5 // { arity: 5 }
  1204. Get l7 // { arity: 3 }
  1205. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  1206. Get l7 // { arity: 3 }
  1207. cte l7 =
  1208. Project (#0{name}, #3, #1) // { arity: 3 }
  1209. Join on=(#0{name} = #2{src_name}) type=differential // { arity: 4 }
  1210. implementation
  1211. %0[#0]K » %1:l4[#0]K
  1212. ArrangeBy keys=[[#0{name}]] // { arity: 2 }
  1213. Distinct project=[#0{name}, #1] // { arity: 2 }
  1214. Get l6 // { arity: 2 }
  1215. Get l4 // { arity: 2 }
  1216. cte l8 =
  1217. Reduce aggregates=[min(#0{idx})] // { arity: 1 }
  1218. Project (#1) // { arity: 1 }
  1219. Filter (#0{name} = "location") // { arity: 2 }
  1220. Get l6 // { arity: 2 }
  1221. cte l9 =
  1222. Distinct project=[#0..=#2] // { arity: 3 }
  1223. Union // { arity: 3 }
  1224. Project (#6, #4, #5) // { arity: 3 }
  1225. Map (regexp_split_to_array[" ", case_insensitive=false](btrim(#0{body})), (2 * #1{x}), text_to_bigint(array_index(#2, integer_to_bigint((#3 - 1)))), (#4{"?column?"} + text_to_bigint(array_index(#2, integer_to_bigint(#3)))), "seed") // { arity: 7 }
  1226. FlatMap generate_series(1, ((regexp_split_to_array[" ", case_insensitive=false](btrim(#0{body})) array_length 1) / 2), 1) // { arity: 2 }
  1227. Get l3 // { arity: 1 }
  1228. Project (#1, #10, #11) // { arity: 3 }
  1229. Map ((#8{entry_dst} - #4{entry_start}), (#6{clipped_start} + #9), (#7{clipped_end} + #9)) // { arity: 12 }
  1230. Get l11 // { arity: 9 }
  1231. Get l19 // { arity: 3 }
  1232. cte l10 =
  1233. Project (#0..=#2, #4) // { arity: 4 }
  1234. Join on=(#0 = #3{src_name}) type=differential // { arity: 5 }
  1235. implementation
  1236. %0:l9[#0]K » %1:l4[#0]K
  1237. ArrangeBy keys=[[#0]] // { arity: 3 }
  1238. Get l9 // { arity: 3 }
  1239. Get l4 // { arity: 2 }
  1240. cte l11 =
  1241. Project (#0, #3, #1, #2, #6, #10, #9, #11, #7) // { arity: 9 }
  1242. Filter (#9 < #11) // { arity: 12 }
  1243. Map (greatest(#1{start_idx}, #6{src_idx}), (#6{src_idx} + #8{len}), least(#2{end_idx}, #10)) // { arity: 12 }
  1244. Join on=(#0{src_name} = #4{src_name} AND #3{dst_name} = #5{dst_name}) type=differential // { arity: 9 }
  1245. implementation
  1246. %0:l10[#0{src_name}, #3{dst_name}]KK » %1:l2[#0{src_name}, #1{dst_name}]KK
  1247. ArrangeBy keys=[[#0{src_name}, #3{dst_name}]] // { arity: 4 }
  1248. Get l10 // { arity: 4 }
  1249. Get l2 // { arity: 5 }
  1250. cte l12 =
  1251. Distinct project=[#0..=#8] // { arity: 9 }
  1252. Get l11 // { arity: 9 }
  1253. cte l13 =
  1254. Distinct project=[#0..=#4] // { arity: 5 }
  1255. Project (#0..=#3, #7) // { arity: 5 }
  1256. Get l12 // { arity: 9 }
  1257. cte l14 =
  1258. Reduce group_by=[#0..=#4] aggregates=[min(#5{clipped_start})] // { arity: 6 }
  1259. Project (#0..=#4, #9) // { arity: 6 }
  1260. Filter (#9{clipped_start} >= #4{clipped_end}) // { arity: 10 }
  1261. Join on=(#0{src_name} = #5{src_name} AND #1{dst_name} = #6{dst_name} AND #2{start_idx} = #7{start_idx} AND #3{end_idx} = #8{end_idx}) type=differential // { arity: 10 }
  1262. implementation
  1263. %1:l11[#0{src_name}..=#3{end_idx}]KKKKf » %0:l13[#0{src_name}..=#3{end_idx}]KKKKf
  1264. ArrangeBy keys=[[#0{src_name}..=#3{end_idx}]] // { arity: 5 }
  1265. Filter (#2{start_idx}) IS NOT NULL AND (#3{end_idx}) IS NOT NULL // { arity: 5 }
  1266. Get l13 // { arity: 5 }
  1267. ArrangeBy keys=[[#0{src_name}..=#3{end_idx}]] // { arity: 5 }
  1268. Project (#0..=#3, #6) // { arity: 5 }
  1269. Filter (#2{start_idx}) IS NOT NULL AND (#6{clipped_start} < #3{end_idx}) // { arity: 9 }
  1270. Get l11 // { arity: 9 }
  1271. cte l15 =
  1272. Project (#0..=#4, #6) // { arity: 6 }
  1273. Map (coalesce(#5{min}, #3{end_idx})) // { arity: 7 }
  1274. Union // { arity: 6 }
  1275. Get l14 // { arity: 6 }
  1276. Project (#0..=#4, #10) // { arity: 6 }
  1277. Map (null) // { arity: 11 }
  1278. Join on=(#0 = #5 AND #1 = #6 AND #2 = #7 AND #3 = #8 AND #4 = #9) type=differential // { arity: 10 }
  1279. implementation
  1280. %1:l13[#0..=#4]UKKKKK » %0[#0..=#4]KKKKK
  1281. ArrangeBy keys=[[#0..=#4]] // { arity: 5 }
  1282. Union // { arity: 5 }
  1283. Negate // { arity: 5 }
  1284. Project (#0..=#4) // { arity: 5 }
  1285. Get l14 // { arity: 6 }
  1286. Get l13 // { arity: 5 }
  1287. ArrangeBy keys=[[#0..=#4]] // { arity: 5 }
  1288. Get l13 // { arity: 5 }
  1289. cte l16 =
  1290. Reduce group_by=[#0, #3, #1, #2] aggregates=[min(#4{clipped_start})] // { arity: 5 }
  1291. Project (#0..=#3, #8) // { arity: 5 }
  1292. Join on=(#0{src_name} = #4{src_name} AND #1{start_idx} = #6{start_idx} AND #2{end_idx} = #7{end_idx} AND #3{dst_name} = #5{dst_name}) type=differential // { arity: 9 }
  1293. implementation
  1294. %1:l11[#0{src_name}, #2{start_idx}, #3{end_idx}, #1{dst_name}]KKKKf » %0:l10[#0{src_name}..=#3{dst_name}]KKKKf
  1295. ArrangeBy keys=[[#0{src_name}..=#3{dst_name}]] // { arity: 4 }
  1296. Filter (#1{start_idx}) IS NOT NULL AND (#2{end_idx}) IS NOT NULL // { arity: 4 }
  1297. Get l10 // { arity: 4 }
  1298. ArrangeBy keys=[[#0{src_name}, #2{start_idx}, #3{end_idx}, #1{dst_name}]] // { arity: 5 }
  1299. Project (#0..=#3, #6) // { arity: 5 }
  1300. Filter (#6{clipped_start} < #3{end_idx}) AND (#6{clipped_start} >= #2{start_idx}) // { arity: 9 }
  1301. Get l11 // { arity: 9 }
  1302. cte l17 =
  1303. ArrangeBy keys=[[#0..=#3]] // { arity: 4 }
  1304. Get l10 // { arity: 4 }
  1305. cte l18 =
  1306. Project (#0..=#3, #5) // { arity: 5 }
  1307. Map (coalesce(#4{min}, #3{end_idx})) // { arity: 6 }
  1308. Union // { arity: 5 }
  1309. Get l16 // { arity: 5 }
  1310. Project (#0..=#3, #8) // { arity: 5 }
  1311. Map (null) // { arity: 9 }
  1312. Join on=(#0 = #4 AND #1 = #7 AND #2 = #5 AND #3 = #6) type=differential // { arity: 8 }
  1313. implementation
  1314. %0[#0, #2, #3, #1]KKKK » %1:l17[#0..=#3]KKKK
  1315. ArrangeBy keys=[[#0, #2, #3, #1]] // { arity: 4 }
  1316. Union // { arity: 4 }
  1317. Negate // { arity: 4 }
  1318. Project (#0..=#3) // { arity: 4 }
  1319. Get l16 // { arity: 5 }
  1320. Project (#0, #3, #1, #2) // { arity: 4 }
  1321. Get l10 // { arity: 4 }
  1322. Get l17 // { arity: 4 }
  1323. cte l19 =
  1324. Distinct project=[#0..=#2] // { arity: 3 }
  1325. Union // { arity: 3 }
  1326. Project (#1, #7, #23) // { arity: 3 }
  1327. Join on=(#0 = #9 = #18 AND #1 = #10 = #19 AND #2 = #11 = #20 AND #3 = #12 = #21 AND #4 = #13 AND #5 = #14 AND #6 = #15 AND #7 = #16 = #22 AND #8 = #17) type=delta // { arity: 24 }
  1328. implementation
  1329. %0:l11 » %1:l12[#0..=#8]UKKKKKKKKK » %2[#0..=#4]KKKKK
  1330. %1:l12 » %0:l11[#0..=#8]KKKKKKKKK » %2[#0..=#4]KKKKK
  1331. %2 » %0:l11[#0..=#3, #7]KKKKK » %1:l12[#0..=#8]UKKKKKKKKK
  1332. ArrangeBy keys=[[#0..=#8], [#0..=#3, #7]] // { arity: 9 }
  1333. Get l11 // { arity: 9 }
  1334. ArrangeBy keys=[[#0..=#8]] // { arity: 9 }
  1335. Get l12 // { arity: 9 }
  1336. ArrangeBy keys=[[#0..=#4]] // { arity: 6 }
  1337. Union // { arity: 6 }
  1338. Filter (#4 < #5) // { arity: 6 }
  1339. Get l15 // { arity: 6 }
  1340. Project (#0..=#4, #6) // { arity: 6 }
  1341. Filter (#4 < #6) // { arity: 7 }
  1342. FlatMap guard_subquery_size(#5{count}) // { arity: 7 }
  1343. Reduce group_by=[#0..=#4] aggregates=[count(*)] // { arity: 6 }
  1344. Project (#0..=#4) // { arity: 5 }
  1345. Get l15 // { arity: 6 }
  1346. Distinct project=[#1, #0, #2] // { arity: 3 }
  1347. Project (#1, #3, #12) // { arity: 3 }
  1348. Join on=(#0 = #4 = #8 AND #1 = #5 = #10 AND #2 = #6 = #11 AND #3 = #7 = #9) type=delta // { arity: 13 }
  1349. implementation
  1350. %0:l17 » %1:l17[#0..=#3]KKKK » %2[#0..=#3]KKKK
  1351. %1:l17 » %0:l17[#0..=#3]KKKK » %2[#0..=#3]KKKK
  1352. %2 » %0:l17[#0..=#3]KKKK » %1:l17[#0..=#3]KKKK
  1353. Get l17 // { arity: 4 }
  1354. Get l17 // { arity: 4 }
  1355. ArrangeBy keys=[[#0..=#3]] // { arity: 5 }
  1356. Union // { arity: 5 }
  1357. Filter (#2 < #4) // { arity: 5 }
  1358. Get l18 // { arity: 5 }
  1359. Project (#0..=#3, #5) // { arity: 5 }
  1360. Filter (#2 < #5) // { arity: 6 }
  1361. FlatMap guard_subquery_size(#4{count}) // { arity: 6 }
  1362. Reduce group_by=[#0..=#3] aggregates=[count(*)] // { arity: 5 }
  1363. Project (#0..=#3) // { arity: 4 }
  1364. Get l18 // { arity: 5 }
  1365. Return // { arity: 2 }
  1366. With
  1367. cte l20 =
  1368. Reduce aggregates=[min(#0{start_idx})] // { arity: 1 }
  1369. Project (#1) // { arity: 1 }
  1370. Filter (#0{name} = "location") // { arity: 3 }
  1371. Get l9 // { arity: 3 }
  1372. Return // { arity: 2 }
  1373. CrossJoin type=differential // { arity: 2 }
  1374. implementation
  1375. %0[×]U » %1[×]U
  1376. ArrangeBy keys=[[]] // { arity: 1 }
  1377. Union // { arity: 1 }
  1378. Get l8 // { arity: 1 }
  1379. Map (null) // { arity: 1 }
  1380. Union // { arity: 0 }
  1381. Negate // { arity: 0 }
  1382. Project () // { arity: 0 }
  1383. Get l8 // { arity: 1 }
  1384. Constant // { arity: 0 }
  1385. - ()
  1386. ArrangeBy keys=[[]] // { arity: 1 }
  1387. Union // { arity: 1 }
  1388. Get l20 // { arity: 1 }
  1389. Map (null) // { arity: 1 }
  1390. Union // { arity: 0 }
  1391. Negate // { arity: 0 }
  1392. Project () // { arity: 0 }
  1393. Get l20 // { arity: 1 }
  1394. Constant // { arity: 0 }
  1395. - ()
  1396. Source materialize.public.input
  1397. Target cluster: quickstart
  1398. EOF