Skip to content

Add basic Ltree support for Postgresql #5875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

yesitskev
Copy link

Added basic support for ltree postgresql extension.

CREATE TABLE myTable(
    path LTREE NOT NULL
);

SELECT path FROM myTable
WHERE path @> 'a.b.c';

SELECT path FROM myTable
WHERE path <@ 'a.b.c';

SELECT path FROM myTable
WHERE path @ 'a.*.c';

SELECT path FROM myTable
WHERE path ? 'a.*.c';

There are a few operators that are not support due to conflicting mixin expressions that I was unsure of how to navigate. These include ~ and || operators.

Copy link
Collaborator

@griffio griffio left a comment

Choose a reason for hiding this comment

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

When adding new data types - it must be mapped in the type resolver
see

open class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeResolver by parentResolver {

This can depend on where the data type is used - functions, arguments etc

jsona_binary_operator ::= '->' | '->>' | '#>' | '#>>'
jsonb_binary_operator ::= '#-'
jsonb_boolean_operator ::= '@?' | '??|' | '??&' | '??'
ltree_binary_operator ::= '?@>' | '?<@' | '?@' | '?~'
ltree_boolean_operator ::= '<@' | '@>' | '@' | '~' | '^<@' | '^@>' | '^@' | '^~' | '?'
Copy link
Collaborator

@griffio griffio Aug 9, 2025

Choose a reason for hiding this comment

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

The ? is a reserved token for bind arguments - it is escaped as ?? - see jsonb_boolean_operator
as it would be written as

SELECT *
FROM Graph WHERE path ?? ?; -- ? operator is escaped with extra ? in jdbc

jsona_binary_operator ::= '->' | '->>' | '#>' | '#>>'
jsonb_binary_operator ::= '#-'
jsonb_boolean_operator ::= '@?' | '??|' | '??&' | '??'
ltree_binary_operator ::= '?@>' | '?<@' | '?@' | '?~'
ltree_boolean_operator ::= '<@' | '@>' | '@' | '~' | '^<@' | '^@>' | '^@' | '^~' | '?'
contains_operator ::= '@>' | '<@'
Copy link
Collaborator

Choose a reason for hiding this comment

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

The issue here is that @> and <@ are already defined as the contains operator - this is used across various data types.

The problem is factoring out the shared operators with JSON etc and making it a general operator

Copy link
Author

Choose a reason for hiding this comment

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

@griffio I noticed this while trying to implement LTREE. I adjusted the the one ContainsOperatorExpressionMixin to include LTREE. I'm not sure how this may affect usages outside of this.

Could you please advise?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have created a basic implementation for the contains operator only as this doesn't require any extra rules
see #5880 - so you can see what is involved in creating a new data type

Adding more operators can be tackled as it will require more trial and error

Note: The LTREE type is a Postgresql extension - but is a trusted extension and still requires CREATE EXTENSION Ltree to use it. (see integration test)

@@ -0,0 +1,15 @@
CREATE TABLE myTable(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fixture tests can appear to pass but they don't generate any types and can pass even if it doesn't work

Need Integration tests for actual executing queries against Postgres - the more the better
see https://github.com/sqldelight/sqldelight/blob/master/sqldelight-gradle-plugin/src/test/integration-postgresql/src/test/kotlin/app/cash/sqldelight/postgresql/integration/PostgreSqlTest.kt

Copy link
Collaborator

@griffio griffio left a comment

Choose a reason for hiding this comment

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

I will have more time next week to look at it as sorting out the shared operators is the most complex part

@griffio griffio mentioned this pull request Aug 10, 2025
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants