Skip to content

Commit f62d28a

Browse files
committed
Enable range queries
1 parent 82f0319 commit f62d28a

1 file changed

Lines changed: 176 additions & 5 deletions

File tree

terminusdb_client/woqlquery/woql_query.py

Lines changed: 176 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,177 @@ def triple(self, sub, pred, obj, opt=False):
10051005
self._cursor["object"] = self._clean_object(obj)
10061006
return self
10071007

1008+
def triple_slice(self, sub, pred, obj, low, high):
1009+
"""Creates a triple pattern matching rule for [S, P, O] with a half-open
1010+
value range [low, high) on the object. Returns triples whose typed object
1011+
value falls within the specified range.
1012+
1013+
Parameters
1014+
----------
1015+
sub : str
1016+
Subject, has to be a node (URI) or variable
1017+
pred : str
1018+
Predicate, can be variable (prefix with "v:") or node
1019+
obj : str
1020+
Object, can be variable or node or value
1021+
low : object
1022+
The inclusive lower bound as a typed value
1023+
high : object
1024+
The exclusive upper bound as a typed value
1025+
1026+
Returns
1027+
-------
1028+
WOQLQuery object
1029+
query object that can be chained and/or execute
1030+
"""
1031+
if self._cursor.get("@type"):
1032+
self._wrap_cursor_with_and()
1033+
self._cursor["@type"] = "TripleSlice"
1034+
self._cursor["subject"] = self._clean_subject(sub)
1035+
self._cursor["predicate"] = self._clean_predicate(pred)
1036+
self._cursor["object"] = self._clean_object(obj)
1037+
self._cursor["low"] = self._clean_object(low)
1038+
self._cursor["high"] = self._clean_object(high)
1039+
return self
1040+
1041+
def quad_slice(self, sub, pred, obj, low, high, graph):
1042+
"""Creates a triple pattern matching rule for [S, P, O, G] with a half-open
1043+
value range [low, high) on the object and an explicit graph selector.
1044+
1045+
Parameters
1046+
----------
1047+
sub : str
1048+
Subject, has to be a node (URI) or variable
1049+
pred : str
1050+
Predicate, can be variable (prefix with "v:") or node
1051+
obj : str
1052+
Object, can be variable or node or value
1053+
low : object
1054+
The inclusive lower bound as a typed value
1055+
high : object
1056+
The exclusive upper bound as a typed value
1057+
graph : str
1058+
Graph resource identifier (e.g. 'instance' or 'schema')
1059+
1060+
Returns
1061+
-------
1062+
WOQLQuery object
1063+
query object that can be chained and/or execute
1064+
"""
1065+
self.triple_slice(sub, pred, obj, low, high)
1066+
self._cursor["graph"] = self._clean_graph(graph)
1067+
return self
1068+
1069+
def triple_next(self, sub, pred, obj, next_val):
1070+
"""Finds the next object value after a reference for a given subject-predicate pair.
1071+
When object is bound and next is free, finds the smallest next > object.
1072+
When next is bound and object is free, finds the largest object < next.
1073+
1074+
Parameters
1075+
----------
1076+
sub : str
1077+
Subject, has to be a node (URI) or variable
1078+
pred : str
1079+
Predicate, can be variable (prefix with "v:") or node
1080+
obj : str
1081+
Object value or variable
1082+
next_val : object
1083+
Next object value or variable
1084+
1085+
Returns
1086+
-------
1087+
WOQLQuery object
1088+
query object that can be chained and/or execute
1089+
"""
1090+
if self._cursor.get("@type"):
1091+
self._wrap_cursor_with_and()
1092+
self._cursor["@type"] = "TripleNext"
1093+
self._cursor["subject"] = self._clean_subject(sub)
1094+
self._cursor["predicate"] = self._clean_predicate(pred)
1095+
self._cursor["object"] = self._clean_object(obj)
1096+
self._cursor["next"] = self._clean_object(next_val)
1097+
return self
1098+
1099+
def quad_next(self, sub, pred, obj, next_val, graph):
1100+
"""Finds the next object value with an explicit graph selector.
1101+
1102+
Parameters
1103+
----------
1104+
sub : str
1105+
Subject, has to be a node (URI) or variable
1106+
pred : str
1107+
Predicate, can be variable (prefix with "v:") or node
1108+
obj : str
1109+
Object value or variable
1110+
next_val : object
1111+
Next object value or variable
1112+
graph : str
1113+
Graph resource identifier (e.g. 'instance' or 'schema')
1114+
1115+
Returns
1116+
-------
1117+
WOQLQuery object
1118+
query object that can be chained and/or execute
1119+
"""
1120+
self.triple_next(sub, pred, obj, next_val)
1121+
self._cursor["graph"] = self._clean_graph(graph)
1122+
return self
1123+
1124+
def triple_previous(self, sub, pred, obj, prev_val):
1125+
"""Finds the previous object value before a reference for a given subject-predicate pair.
1126+
When object is bound and previous is free, finds the largest previous < object.
1127+
When previous is bound and object is free, finds the smallest object > previous.
1128+
1129+
Parameters
1130+
----------
1131+
sub : str
1132+
Subject, has to be a node (URI) or variable
1133+
pred : str
1134+
Predicate, can be variable (prefix with "v:") or node
1135+
obj : str
1136+
Object value or variable
1137+
prev_val : object
1138+
Previous object value or variable
1139+
1140+
Returns
1141+
-------
1142+
WOQLQuery object
1143+
query object that can be chained and/or execute
1144+
"""
1145+
if self._cursor.get("@type"):
1146+
self._wrap_cursor_with_and()
1147+
self._cursor["@type"] = "TriplePrevious"
1148+
self._cursor["subject"] = self._clean_subject(sub)
1149+
self._cursor["predicate"] = self._clean_predicate(pred)
1150+
self._cursor["object"] = self._clean_object(obj)
1151+
self._cursor["previous"] = self._clean_object(prev_val)
1152+
return self
1153+
1154+
def quad_previous(self, sub, pred, obj, prev_val, graph):
1155+
"""Finds the previous object value with an explicit graph selector.
1156+
1157+
Parameters
1158+
----------
1159+
sub : str
1160+
Subject, has to be a node (URI) or variable
1161+
pred : str
1162+
Predicate, can be variable (prefix with "v:") or node
1163+
obj : str
1164+
Object value or variable
1165+
prev_val : object
1166+
Previous object value or variable
1167+
graph : str
1168+
Graph resource identifier (e.g. 'instance' or 'schema')
1169+
1170+
Returns
1171+
-------
1172+
WOQLQuery object
1173+
query object that can be chained and/or execute
1174+
"""
1175+
self.triple_previous(sub, pred, obj, prev_val)
1176+
self._cursor["graph"] = self._clean_graph(graph)
1177+
return self
1178+
10081179
def added_triple(self, sub, pred, obj, opt=False):
10091180
"""Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate, Object) added to the current commit.
10101181
@@ -3430,7 +3601,7 @@ def localize(self, param_spec):
34303601
"""Build a localized scope for variables to prevent leaking local variables to outer scope.
34313602
34323603
Returns a tuple (localized_fn, v) where:
3433-
- localized_fn: function that wraps queries with select("") and eq() bindings
3604+
- localized_fn: function that wraps queries with select() (empty variable list) and eq() bindings
34343605
- v: VarsUnique object with unique variable names for use in the inner query
34353606
34363607
Parameters with non-None values are bound from outer scope via eq().
@@ -3460,7 +3631,7 @@ def localize(self, param_spec):
34603631
v = VarsUnique(*param_names)
34613632

34623633
def localized_fn(query=None):
3463-
# Create eq bindings for outer parameters OUTSIDE select("")
3634+
# Create eq bindings for outer parameters OUTSIDE select()
34643635
# This ensures outer parameters are visible in query results
34653636
outer_eq_bindings = []
34663637
for param_name in param_names:
@@ -3474,17 +3645,17 @@ def localized_fn(query=None):
34743645
outer_eq_bindings.append(
34753646
WOQLQuery().eq(outer_value, outer_value)
34763647
)
3477-
# Bind the unique variable to the outer parameter OUTSIDE the select("")
3648+
# Bind the unique variable to the outer parameter OUTSIDE the select()
34783649
outer_eq_bindings.append(
34793650
WOQLQuery().eq(getattr(v, param_name), outer_value)
34803651
)
34813652

34823653
if query is not None:
3483-
# Functional mode: wrap query in select(""), then add outer eq bindings
3654+
# Functional mode: wrap query in select() with empty variable list
34843655
localized_query = WOQLQuery().select(query)
34853656

34863657
if outer_eq_bindings:
3487-
# Wrap: eq(outer) AND select("") { query }
3658+
# Wrap: eq(outer) AND select() { query }
34883659
return WOQLQuery().woql_and(*outer_eq_bindings, localized_query)
34893660
return localized_query
34903661

0 commit comments

Comments
 (0)