Skip to content

Add support for PostgreSQL's trigram operators#291

Open
Niols wants to merge 1 commit into
ygrek:masterfrom
Niols:trigram-operators
Open

Add support for PostgreSQL's trigram operators#291
Niols wants to merge 1 commit into
ygrek:masterfrom
Niols:trigram-operators

Conversation

@Niols

@Niols Niols commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Adds parsing and typing for PostgreSQL's pg_trgm operators:

  • Similarity operators <%, %>, <<%, %>> → new TEXT_CMP_OP (comparison, Comp_text_cmp).
  • Distance operators <->, <<->, <->>, <<<->, <->>> → new TEXT_DIST_OP (returns float).
  • The similarity, word_similarity and strict_word_similarity functions (text × text → float).

Note: I deliberately left out the % trigram similarity operator. The difficulty is that % is overloaded across dialects: it's the integer modulo INT -> INT -> INT in both MySQL and PostgreSQL, but in PostgreSQL it also doubles as the trigram similarity operator TEXT -> TEXT -> BOOL. I don't think there is currently a good way to treat such a case, but we would need to have:

  • Some form of union type for operators within the same dialect (eg. PostgreSQL's %s should have type (INT -> INT -> INT) U (TEXT -> TEXT -> BOOL)).
  • Have different types for the same operators in different dialects, or even a way to make operators available only for one dialect.

I left it as a FIXME in the lexer, and I'm willing to work on this some time if you think this is worth pursuing.

Niols added a commit to Niols/sqlgg that referenced this pull request Jul 4, 2026
Comment thread lib/sql.ml
} [@@deriving show]
type logical_op = And | Or | Xor [@@deriving show]
type comparison_op = Comp_equal | Comp_num_cmp | Comp_num_eq | Not_distinct_op | Is_null | Is_not_null [@@deriving eq, show]
type comparison_op = Comp_equal | Comp_num_cmp | Comp_text_cmp | Comp_num_eq | Not_distinct_op | Is_null | Is_not_null [@@deriving eq, show]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was going to say i would expect some typing changes involving new ctor, but then i see there is nothing like this for comp_num_cmp either. Not for this PR but it means this is a missed opportunity to have better inference @jongleb ?

@ygrek

ygrek commented Jul 6, 2026

Copy link
Copy Markdown
Owner

as for % ambiguity i think we already have some ambiguity handling but it is case by case handling, also it will be the first one to depend on dialect i think yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants