-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile_query.py
More file actions
630 lines (504 loc) · 22 KB
/
tile_query.py
File metadata and controls
630 lines (504 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/usr/bin/env python3
"""
Tile Query Language — Expressive Search for Knowledge Tiles
===========================================================
Provides a flexible query DSL for searching and aggregating knowledge tiles.
Supports boolean operators, proximity search, and aggregate queries.
Architecture:
QueryParser — parses query expression strings into AST nodes
QueryNode — abstract base for query AST nodes
TagQuery — match tiles by tag
SourceQuery — match tiles by source
ConfidenceQuery — match tiles by confidence range
TimeRangeQuery — match tiles by creation time range
DomainQuery — match tiles by domain
IdQuery — match tiles by ID pattern
BoolQuery — AND / OR / NOT combinators
ProximityQuery — find tiles related to a seed tile
AggregateQuery — count, avg, top-sources over results
TileQueryEngine — executes queries against a TileStore + TileIndex
"""
from __future__ import annotations
import re
import time
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Set,
Tuple,
)
from dataclasses import dataclass, field
from enum import Enum
from knowledge_tiles import KnowledgeTile, TileStore, TileIndex
# ═══════════════════════════════════════════════════════════════
# Query AST Nodes
# ═══════════════════════════════════════════════════════════════
class QueryNode:
"""Abstract base class for query AST nodes."""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
"""Execute this query node against the store/index."""
raise NotImplementedError
def __repr__(self) -> str:
return f"{self.__class__.__name__}()"
@dataclass
class TagQuery(QueryNode):
"""Match tiles that have a specific tag."""
tag: str = ""
match_all: bool = False
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
if self.match_all:
return index.search_by_tags([self.tag], match_all=True)
return index.search_by_tag(self.tag)
def __repr__(self) -> str:
return f"TagQuery(tag={self.tag!r})"
@dataclass
class SourceQuery(QueryNode):
"""Match tiles from a specific source."""
source: str = ""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_source(self.source)
def __repr__(self) -> str:
return f"SourceQuery(source={self.source!r})"
@dataclass
class ConfidenceQuery(QueryNode):
"""Match tiles within a confidence range."""
min_conf: float = 0.0
max_conf: float = 1.0
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_confidence(self.min_conf, self.max_conf)
def __repr__(self) -> str:
return f"ConfidenceQuery(min={self.min_conf}, max={self.max_conf})"
@dataclass
class TimeRangeQuery(QueryNode):
"""Match tiles created within a time range (Unix timestamps)."""
after: float = 0.0
before: float = float("inf")
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_time_range(self.after, self.before)
def __repr__(self) -> str:
return f"TimeRangeQuery(after={self.after}, before={self.before})"
@dataclass
class DomainQuery(QueryNode):
"""Match tiles in a specific domain."""
domain: str = ""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_domain(self.domain)
def __repr__(self) -> str:
return f"DomainQuery(domain={self.domain!r})"
@dataclass
class IdQuery(QueryNode):
"""Match tiles whose ID contains a pattern (substring match)."""
pattern: str = ""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_id_pattern(self.pattern)
def __repr__(self) -> str:
return f"IdQuery(pattern={self.pattern!r})"
@dataclass
class BoolQuery(QueryNode):
"""Boolean combinator: AND, OR, NOT over child queries."""
operator: str = "AND" # AND, OR, NOT
children: List[QueryNode] = field(default_factory=list)
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
if not self.children:
return []
if self.operator == "AND":
result_ids: Optional[Set[str]] = None
for child in self.children:
child_tiles = child.execute(store, index)
child_ids = {t.id for t in child_tiles}
if result_ids is None:
result_ids = child_ids
else:
result_ids = result_ids & child_ids
if result_ids is None:
return []
return [t for t in child_tiles if t.id in result_ids]
elif self.operator == "OR":
all_tiles: Dict[str, KnowledgeTile] = {}
for child in self.children:
for tile in child.execute(store, index):
all_tiles[tile.id] = tile
return list(all_tiles.values())
elif self.operator == "NOT":
if len(self.children) != 1:
return []
exclude_ids = {t.id for t in self.children[0].execute(store, index)}
return [t for t in store.get_all() if t.id not in exclude_ids]
return []
def __repr__(self) -> str:
return f"BoolQuery({self.operator}, {len(self.children)} children)"
@dataclass
class PrerequisiteQuery(QueryNode):
"""Match tiles that require a specific prerequisite."""
prereq_id: str = ""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return index.search_by_prerequisite(self.prereq_id)
def __repr__(self) -> str:
return f"PrerequisiteQuery(prereq={self.prereq_id!r})"
@dataclass
class WildcardQuery(QueryNode):
"""Match all tiles (wildcard)."""
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
return store.get_all()
def __repr__(self) -> str:
return "WildcardQuery(*)"
@dataclass
class ProximityQuery(QueryNode):
"""Find tiles related to a seed tile.
Relatedness is computed via shared tags, same domain, and
prerequisite relationships (both directions).
Attributes:
seed_id: The seed tile's ID.
max_results: Maximum number of related tiles to return.
tag_weight: Weight for shared-tag similarity.
domain_weight: Weight for same-domain bonus.
prereq_weight: Weight for prerequisite linkage.
"""
seed_id: str = ""
max_results: int = 10
tag_weight: float = 0.4
domain_weight: float = 0.3
prereq_weight: float = 0.3
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
seed = store.get(self.seed_id)
if seed is None:
return []
all_tiles = store.get_all()
scored: List[Tuple[float, KnowledgeTile]] = []
for tile in all_tiles:
if tile.id == self.seed_id:
continue
score = 0.0
# Shared tags
seed_tags = set(seed.tags)
tile_tags = set(tile.tags)
if seed_tags and tile_tags:
shared = seed_tags & tile_tags
total = seed_tags | tile_tags
score += (len(shared) / len(total)) * self.tag_weight
# Same domain
if tile.domain == seed.domain:
score += self.domain_weight
# Prerequisite linkage (bidirectional)
if self.seed_id in tile.prerequisites:
score += self.prereq_weight
if tile.id in seed.prerequisites:
score += self.prereq_weight
if score > 0:
scored.append((score, tile))
scored.sort(key=lambda x: x[0], reverse=True)
return [tile for _, tile in scored[:self.max_results]]
def __repr__(self) -> str:
return f"ProximityQuery(seed={self.seed_id!r})"
# ═══════════════════════════════════════════════════════════════
# Aggregate Query Results
# ═══════════════════════════════════════════════════════════════
class AggregateFunction(str, Enum):
"""Supported aggregate functions."""
COUNT = "count"
AVG_CONFIDENCE = "avg_confidence"
TOP_SOURCES = "top_sources"
TAG_BREAKDOWN = "tag_breakdown"
DOMAIN_BREAKDOWN = "domain_breakdown"
@dataclass
class AggregateResult:
"""Result of an aggregate query.
Attributes:
function: The aggregate function used.
value: The computed value (type varies by function).
raw_tiles: The tiles that were aggregated.
"""
function: str
value: Any = None
raw_tiles: List[KnowledgeTile] = field(default_factory=list)
def to_dict(self) -> dict:
return {
"function": self.function,
"value": self.value,
"tile_count": len(self.raw_tiles),
}
# ═══════════════════════════════════════════════════════════════
# Query Parser
# ═══════════════════════════════════════════════════════════════
class QueryParser:
"""Parses query expression strings into QueryNode AST.
Supported syntax:
tag:python — tiles with tag "python"
source:wikipedia — tiles from source "wikipedia"
domain:code — tiles in domain "code"
confidence:>0.8 — tiles with confidence > 0.8
confidence:<0.3 — tiles with confidence < 0.3
confidence:0.5-0.9 — tiles with confidence in [0.5, 0.9]
after:1700000000 — tiles created after timestamp
before:1700000000 — tiles created before timestamp
id:auth — tiles with "auth" in their ID
prereq:basic_auth — tiles requiring "basic_auth"
near:tile_id — tiles related to seed tile
AND(tag:python, source:wiki) — boolean AND
OR(tag:python, tag:rust) — boolean OR
NOT(tag:deprecated) — boolean NOT
AGG(count, tag:python) — aggregate query
AGG(avg_confidence, domain:code)
AGG(top_sources, tag:python)
AGG(tag_breakdown, *)
"""
def parse(self, expression: str) -> QueryNode:
"""Parse a query expression into a QueryNode AST.
Args:
expression: The query expression string.
Returns:
A QueryNode ready for execution.
Raises:
ValueError: If the expression cannot be parsed.
"""
expression = expression.strip()
# Check for aggregate queries
agg_match = re.match(r"^AGG\((\w+),\s*(.+)\)$", expression, re.IGNORECASE)
if agg_match:
func_name = agg_match.group(1)
inner_expr = agg_match.group(2).strip()
# Wrap in a special aggregate marker
return AggregateQueryNode(
function=func_name,
inner=self.parse(inner_expr),
)
# Check for boolean operators
and_match = re.match(r"^AND\((.+)\)$", expression)
if and_match:
children = self._split_args(and_match.group(1))
return BoolQuery(operator="AND",
children=[self.parse(c) for c in children])
or_match = re.match(r"^OR\((.+)\)$", expression)
if or_match:
children = self._split_args(or_match.group(1))
return BoolQuery(operator="OR",
children=[self.parse(c) for c in children])
not_match = re.match(r"^NOT\((.+)\)$", expression)
if not_match:
child = self.parse(not_match.group(1).strip())
return BoolQuery(operator="NOT", children=[child])
# Field-specific queries
tag_match = re.match(r"^tag:(.+)$", expression)
if tag_match:
return TagQuery(tag=tag_match.group(1).strip())
source_match = re.match(r"^source:(.+)$", expression)
if source_match:
return SourceQuery(source=source_match.group(1).strip())
domain_match = re.match(r"^domain:(.+)$", expression)
if domain_match:
return DomainQuery(domain=domain_match.group(1).strip())
conf_match = re.match(r"^confidence:(.+)$", expression)
if conf_match:
return self._parse_confidence(conf_match.group(1).strip())
after_match = re.match(r"^after:(.+)$", expression)
if after_match:
ts = float(after_match.group(1).strip())
return TimeRangeQuery(after=ts, before=float("inf"))
before_match = re.match(r"^before:(.+)$", expression)
if before_match:
ts = float(before_match.group(1).strip())
return TimeRangeQuery(after=0.0, before=ts)
id_match = re.match(r"^id:(.+)$", expression)
if id_match:
return IdQuery(pattern=id_match.group(1).strip())
prereq_match = re.match(r"^prereq:(.+)$", expression)
if prereq_match:
return PrerequisiteQuery(prereq_id=prereq_match.group(1).strip())
near_match = re.match(r"^near:(.+)$", expression)
if near_match:
return ProximityQuery(seed_id=near_match.group(1).strip())
# Fallback: treat as tag search (wildcard * = all tiles)
if expression.strip() == "*":
return WildcardQuery()
return TagQuery(tag=expression)
def _parse_confidence(self, spec: str) -> ConfidenceQuery:
"""Parse a confidence specification like '>0.8', '<0.3', or '0.5-0.9'."""
gt_match = re.match(r"^>(\d*\.?\d+)$", spec)
if gt_match:
return ConfidenceQuery(min_conf=float(gt_match.group(1)),
max_conf=1.0)
lt_match = re.match(r"^<(\d*\.?\d+)$", spec)
if lt_match:
return ConfidenceQuery(min_conf=0.0,
max_conf=float(lt_match.group(1)))
range_match = re.match(r"^(\d*\.?\d+)\s*-\s*(\d*\.?\d+)$", spec)
if range_match:
return ConfidenceQuery(
min_conf=float(range_match.group(1)),
max_conf=float(range_match.group(2)),
)
# Exact match
try:
val = float(spec)
return ConfidenceQuery(min_conf=val, max_conf=val)
except ValueError:
return ConfidenceQuery()
def _split_args(self, args_str: str) -> List[str]:
"""Split comma-separated arguments, respecting nested parentheses."""
args: List[str] = []
depth = 0
current = ""
for ch in args_str:
if ch == "(" :
depth += 1
current += ch
elif ch == ")":
depth -= 1
current += ch
elif ch == "," and depth == 0:
args.append(current.strip())
current = ""
else:
current += ch
if current.strip():
args.append(current.strip())
return args
@dataclass
class AggregateQueryNode(QueryNode):
"""Wraps an inner query with an aggregate function."""
function: str = "count"
inner: QueryNode = field(default_factory=TagQuery)
def execute(self, store: TileStore, index: TileIndex) -> List[KnowledgeTile]:
# Aggregate queries return the raw tiles; aggregation is done
# by the TileQueryEngine
return self.inner.execute(store, index)
def __repr__(self) -> str:
return f"AggregateQueryNode(function={self.function!r})"
# ═══════════════════════════════════════════════════════════════
# TileQueryEngine — Execute Queries
# ═══════════════════════════════════════════════════════════════
@dataclass
class QueryResult:
"""Result of executing a query.
Attributes:
tiles: The matching tiles.
aggregate: Optional aggregate result.
query_expression: The original query string.
execution_time_ms: How long the query took.
"""
tiles: List[KnowledgeTile] = field(default_factory=list)
aggregate: Optional[AggregateResult] = None
query_expression: str = ""
execution_time_ms: float = 0.0
def to_dict(self) -> dict:
result: dict = {
"query": self.query_expression,
"tile_count": len(self.tiles),
"tiles": [t.to_dict() for t in self.tiles],
"execution_time_ms": round(self.execution_time_ms, 2),
}
if self.aggregate:
result["aggregate"] = self.aggregate.to_dict()
return result
class TileQueryEngine:
"""Executes tile queries against a TileStore and TileIndex.
Supports parsed AST queries, string expressions, and aggregate queries.
Attributes:
store: The tile store to query against.
index: The tile index for fast lookup.
parser: The query expression parser.
"""
def __init__(self, store: TileStore, index: TileIndex) -> None:
self.store = store
self.index = index
self.parser = QueryParser()
def execute(self, query: str) -> QueryResult:
"""Execute a query expression string.
Args:
query: The query expression.
Returns:
A QueryResult with matching tiles and optional aggregates.
"""
start = time.time()
ast = self.parser.parse(query)
# Check for aggregate queries
if isinstance(ast, AggregateQueryNode):
tiles = ast.inner.execute(self.store, self.index)
agg = self._compute_aggregate(ast.function, tiles)
elapsed = (time.time() - start) * 1000
return QueryResult(
tiles=tiles,
aggregate=agg,
query_expression=query,
execution_time_ms=elapsed,
)
tiles = ast.execute(self.store, self.index)
elapsed = (time.time() - start) * 1000
return QueryResult(
tiles=tiles,
query_expression=query,
execution_time_ms=elapsed,
)
def execute_ast(self, node: QueryNode) -> QueryResult:
"""Execute a pre-parsed QueryNode AST."""
start = time.time()
tiles = node.execute(self.store, self.index)
elapsed = (time.time() - start) * 1000
return QueryResult(tiles=tiles, execution_time_ms=elapsed)
def find_related(self, tile_id: str,
max_results: int = 10) -> List[KnowledgeTile]:
"""Find tiles related to a seed tile using proximity search."""
query = ProximityQuery(seed_id=tile_id, max_results=max_results)
return query.execute(self.store, self.index)
def _compute_aggregate(self, function: str,
tiles: List[KnowledgeTile]) -> AggregateResult:
"""Compute an aggregate function over query results."""
func_lower = function.lower()
if func_lower == "count":
return AggregateResult(
function=function,
value=len(tiles),
raw_tiles=tiles,
)
elif func_lower == "avg_confidence":
if not tiles:
return AggregateResult(function=function, value=0.0,
raw_tiles=tiles)
avg = sum(t.confidence for t in tiles) / len(tiles)
return AggregateResult(
function=function,
value=round(avg, 4),
raw_tiles=tiles,
)
elif func_lower == "top_sources":
source_counts: Dict[str, int] = {}
for t in tiles:
source_counts[t.source] = source_counts.get(t.source, 0) + 1
sorted_sources = sorted(source_counts.items(),
key=lambda x: x[1], reverse=True)
return AggregateResult(
function=function,
value=[{"source": s, "count": c}
for s, c in sorted_sources],
raw_tiles=tiles,
)
elif func_lower == "tag_breakdown":
tag_counts: Dict[str, int] = {}
for t in tiles:
for tag in t.tags:
tag_counts[tag] = tag_counts.get(tag, 0) + 1
sorted_tags = sorted(tag_counts.items(),
key=lambda x: x[1], reverse=True)
return AggregateResult(
function=function,
value=[{"tag": t, "count": c}
for t, c in sorted_tags],
raw_tiles=tiles,
)
elif func_lower == "domain_breakdown":
domain_counts: Dict[str, int] = {}
for t in tiles:
d = t.domain.value
domain_counts[d] = domain_counts.get(d, 0) + 1
sorted_domains = sorted(domain_counts.items(),
key=lambda x: x[1], reverse=True)
return AggregateResult(
function=function,
value=[{"domain": d, "count": c}
for d, c in sorted_domains],
raw_tiles=tiles,
)
return AggregateResult(function=function, value=None, raw_tiles=tiles)