Compiled Cypher on Postgres: apply =~'s two pattern rewrites in drivine4j's order

Open Beginner friendly
#250 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, python
Domain
database

Research direction

Start at drivine/cypher/sql/postgres_cypher_compiler.py:589, in matches, and compare the two regexp_replace calls with the order shown in the issue. Swap their order so anchoring happens first. Check the compiled SQL for the example pattern and confirm the spec statements produce the same text as the other Drivine libraries.

Written by the indexing model from the issue text.

Description

A tidy-up. The results are the same; only the SQL text differs from drivine4j's.

I compiled the 998 statements of the Cypher spec at v0.6.18 with drivine4j (main, 3c5c4e5a), Drivine for TypeScript (master, e5b5d2b) and this library (main, a684245). On Postgres, =~ is the one place where this library alone writes different text from the other two.

What happens: matches (drivine/cypher/sql/postgres_cypher_compiler.py:589) first adds the w flag, when the flags hold both s and m, and then anchors the pattern:

regexp_replace(regexp_replace(<pattern>, '^\(\?([a-z]*(s[a-z]*m|m[a-z]*s)[a-z]*)\)', '(?\1w)'), '^(\(\?([a-z]+)\))?(.*)$', '(?p\2)\\A(\3)\\Z')

drivine4j (PostgresCypherCompiler.kt:416) and Drivine for TypeScript (PostgresCypherCompiler.ts:503) anchor first and then add the w:

regexp_replace(regexp_replace(<pattern>, '^(\(\?([a-z]+)\))?(.*)$', '(?p\2)\\A(\3)\\Z'), '^\(\?([a-z]*(s[a-z]*m|m[a-z]*s)[a-z]*)\)', '(?\1w)')

Both orders turn (?sm)a.b into (?psmw)\A(a.b)\Z. A pattern without both flags gets no w either way. So the results are the same, and the spec passes here. This shows up in 11 spec statements, for example RETURN 'a\nb' =~ '(?ims)A$.^B'.

Why bother: with the same text, a compile of the spec compares byte for byte across the libraries, so a real difference stands out. These 11 are about a quarter of the 41 Postgres statements where the two ports' SQL differs.

A fix: swap the two regexp_replace calls in matches, so the pattern is anchored first, as drivine4j does.

A tidy-up, so it goes in Next release + 2.

Dominant language
Python
Stars
1
Forks
0
Avg merge
1h 32m
Merged PRs (30d)
149

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from liberation-data/drivine-python

All issues in liberation-data/drivine-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.