Skip to content

Commit

Permalink
Add Postgis and Postgres routing to set up
Browse files Browse the repository at this point in the history
  • Loading branch information
NolanTrem committed Feb 4, 2025
1 parent 738b4a1 commit 1de1fd4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
36 changes: 5 additions & 31 deletions py/core/providers/database/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2632,9 +2632,7 @@ def _build_filters(
)

def parse_condition(key: str, value: Any) -> str:
# ----------------------------------------------------------------------
# 1) If it's the normal base_id_column (like "parent_id" or "collection_id")
# ----------------------------------------------------------------------
if key == base_id_column:
if isinstance(value, dict):
op, clause = next(iter(value.items()))
Expand All @@ -2653,12 +2651,10 @@ def parse_condition(key: str, value: Any) -> str:
parameters.append(str(value))
return f"{base_id_column} = ${len(parameters)}::uuid"

# ----------------------------------------------------------------------
# 2) SPECIAL: if user specifically sets "collection_ids" in filters
# We interpret that to mean "Look for rows whose parent_id (or collection_id)
# is in the array of values" – i.e. we do the same logic but we forcibly
# direct it to the same column: parent_id or collection_id.
# ----------------------------------------------------------------------
elif key == "collection_ids":
# If we are searching communities, the relevant field is `collection_id`.
# If searching entities/relationships, the relevant field is `parent_id`.
Expand Down Expand Up @@ -2687,9 +2683,7 @@ def parse_condition(key: str, value: Any) -> str:
parameters.append(str(value))
return f"{col_to_use} = ${len(parameters)}::uuid"

# ----------------------------------------------------------------------
# 3) If key starts with "metadata.", handle metadata-based filters
# ----------------------------------------------------------------------
elif key.startswith("metadata."):
field = key.split("metadata.")[1]
if isinstance(value, dict):
Expand All @@ -2708,14 +2702,10 @@ def parse_condition(key: str, value: Any) -> str:
parameters.append(value)
return f"(metadata->>'{field}') = ${len(parameters)}"

# ----------------------------------------------------------------------
# 4) Not recognized => return empty so we skip it
# ----------------------------------------------------------------------
return ""

# --------------------------------------------------------------------------
# 5) parse_filter() is the recursive walker that sees $and/$or or normal fields
# --------------------------------------------------------------------------
def parse_filter(fd: dict) -> str:
filter_conditions = []
for k, v in fd.items():
Expand Down Expand Up @@ -2848,7 +2838,10 @@ async def dijkstra_shortest_path(
('x' || substring(replace(id::text, '-', ''), 1, 16))::bit(64)::bigint AS id,
('x' || substring(replace(subject_id::text, '-', ''), 1, 16))::bit(64)::bigint AS source,
('x' || substring(replace(object_id::text, '-', ''), 1, 16))::bit(64)::bigint AS target,
CASE WHEN weight <= 0 THEN 1 ELSE weight END AS cost,
CASE
WHEN weight <= 0 THEN 10 -- Handle zero/negative weights
ELSE (11 - weight) -- Invert the 0-10 scale to 10-1
END AS cost,
id as original_id,
subject_id,
object_id
Expand Down Expand Up @@ -2942,17 +2935,7 @@ async def dijkstra_shortest_path(
status_code=404,
)

path = {
"path": [
{
"type": "entity",
"id": str(source_id),
"name": results[0]["current_entity_name"],
}
],
"total_cost": 0,
"num_hops": 0,
}
path = {"path": [], "total_cost": 0, "num_hops": 0}

for row in results:
if row["relationship_uuid"]:
Expand All @@ -2969,15 +2952,6 @@ async def dijkstra_shortest_path(
}
)

if row["next_entity_id"]:
path["path"].append(
{
"type": "entity",
"id": str(row["next_entity_id"]),
"name": row["next_entity_name"],
}
)

if results:
path["total_cost"] = results[-1]["total_cost"]
path["num_hops"] = len(
Expand Down
9 changes: 7 additions & 2 deletions py/core/providers/database/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ async def initialize(self):
await conn.execute("CREATE EXTENSION IF NOT EXISTS vector;")
await conn.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;")
await conn.execute("CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;")
await conn.execute("CREATE EXTENSION IF NOT EXISTS postgis;")
await conn.execute("CREATE EXTENSION IF NOT EXISTS pgRouting;")
try:
await conn.execute("CREATE EXTENSION IF NOT EXISTS postgis;")
await conn.execute("CREATE EXTENSION IF NOT EXISTS pgRouting;")
except Exception as e:
logger.warning(
"Error creating postgis and pgrouting extensions. Some beta features, such as graph traversal, may not work."
)

# Create schema if it doesn't exist
await conn.execute(
Expand Down
12 changes: 10 additions & 2 deletions py/r2r/compose.full.swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ services:
timeout: 5s
retries: 5
command: >
postgres
-c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024}
bash -c '
apt-get update &&
apt-get install -y postgresql-16-postgis-3 postgresql-16-postgis-3-scripts postgresql-16-pgrouting &&
dpkg --configure -a &&
docker-entrypoint.sh postgres -c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024} &
until pg_isready; do sleep 1; done &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS postgis;" &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS pgrouting;" &&
wait
'
deploy:
replicas: 1
restart_policy:
Expand Down
12 changes: 10 additions & 2 deletions py/r2r/compose.full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ services:
retries: 5
restart: on-failure
command: >
postgres
-c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024}
bash -c '
apt-get update &&
apt-get install -y postgresql-16-postgis-3 postgresql-16-postgis-3-scripts postgresql-16-pgrouting &&
dpkg --configure -a &&
docker-entrypoint.sh postgres -c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024} &
until pg_isready; do sleep 1; done &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS postgis;" &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS pgrouting;" &&
wait
'
hatchet-postgres:
image: postgres:latest
Expand Down
12 changes: 10 additions & 2 deletions py/r2r/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ services:
retries: 5
restart: on-failure
command: >
postgres
-c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024}
bash -c '
apt-get update &&
apt-get install -y postgresql-16-postgis-3 postgresql-16-postgis-3-scripts postgresql-16-pgrouting &&
dpkg --configure -a &&
docker-entrypoint.sh postgres -c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024} &
until pg_isready; do sleep 1; done &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS postgis;" &&
psql -U postgres -c "CREATE EXTENSION IF NOT EXISTS pgrouting;" &&
wait
'
r2r:
image: ${R2R_IMAGE:-ragtoriches/prod:latest}
Expand Down

0 comments on commit 1de1fd4

Please sign in to comment.