Skip to content

Commit

Permalink
Enable GIN index scans on metadata = queries 🔍
Browse files Browse the repository at this point in the history
  • Loading branch information
Kezzsim committed Oct 24, 2023
1 parent ea9d24a commit ed9686a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions tiled/catalog/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shutil
import sys
import uuid
from functools import partial
from functools import partial, reduce
from pathlib import Path
from urllib.parse import quote_plus, urlparse

Expand Down Expand Up @@ -938,9 +938,18 @@ def _prepare_structure(structure_family, structure):

def binary_op(query, tree, operation):
dialect_name = tree.engine.url.get_dialect().name
attr = orm.Node.metadata_[query.key.split(".")]
keys = query.key.split(".")
attr = orm.Node.metadata_[keys]
if dialect_name == "sqlite":
condition = operation(_get_value(attr, type(query.value)), query.value)
# specific case where GIN optomized index can be used to speed up POSTGRES equals queries
elif dialect_name == "postgresql" and operation == operator.eq:
condition = orm.Node.metadata_.op("@>")(
type_coerce(
{keys[0]: reduce(lambda x, y: {y: x}, keys[1:][::-1], query.value)},
orm.Node.metadata_.type,
)
)
else:
condition = operation(attr, type_coerce(query.value, orm.Node.metadata_.type))
return tree.new_variation(conditions=tree.conditions + [condition])
Expand Down
2 changes: 1 addition & 1 deletion tiled/catalog/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def initialize_database(engine):

async with engine.connect() as connection:
# Install extensions
if (engine.dialect.name == "postgresql"):
if engine.dialect.name == "postgresql":
await connection.execute(text("create extension btree_gin;"))
# Create all tables.
await connection.run_sync(Base.metadata.create_all)
Expand Down

0 comments on commit ed9686a

Please sign in to comment.