-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsupertag-services-query.el
More file actions
1139 lines (1017 loc) · 47.2 KB
/
Copy pathsupertag-services-query.el
File metadata and controls
1139 lines (1017 loc) · 47.2 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; supertag/services/query.el --- Query system for Supertag -*- lexical-binding: t; -*-
;;; Commentary:
;; This module is the read boundary for composed Supertag data. Concrete
;; queries hide Store/index joins from UI consumers. The legacy generic query
;; helpers remain temporarily for compatibility and must not gain new callers.
;;; Code:
(require 'cl-lib)
(require 'supertag-core-scan)
(require 'supertag-core-store)
(require 'supertag-board-ops)
(require 'supertag-ops-relation)
(require 'supertag-ops-node)
(require 'supertag-ops-tag)
(require 'supertag-ops-field)
(require 'supertag-services-formula)
;;; --- Query System ---
(defun supertag-query-node (node-id)
"Return the Document Projection node for NODE-ID, or nil."
(supertag-node-get node-id))
(defun supertag-query-tag-paths ()
"Return sorted Semantic Tag path descriptors.
Each descriptor contains :id, :name, and :display-path."
(let (result)
(maphash
(lambda (tag-id tag)
(let ((tag (supertag--ensure-plist tag)))
(push (list :id tag-id
:name (or (plist-get tag :name) tag-id)
:display-path (supertag-tag-display-path tag-id))
result)))
(supertag-store-get-collection :tags))
(sort result
(lambda (left right)
(let ((left-path (plist-get left :display-path))
(right-path (plist-get right :display-path)))
(if (equal left-path right-path)
(string< (plist-get left :id) (plist-get right :id))
(string< left-path right-path)))))))
(defun supertag-query-tags ()
"Return Semantic Tags as (id . tag-plist) pairs."
(let (result)
(maphash
(lambda (id tag)
(push (cons id (supertag--ensure-plist tag)) result))
(supertag-store-get-collection :tags))
result))
(defun supertag-query-node-ids-by-tag (tag-name &optional include-descendants)
"Return node IDs tagged with TAG-NAME.
When INCLUDE-DESCENDANTS is non-nil, include transitive `:extends' children."
(supertag-index-get-nodes-by-tag tag-name include-descendants))
(defun supertag-query-automations (&optional filter)
"Return automation rules, optionally filtered by FILTER.
FILTER is a function receiving an automation plist and returning non-nil."
(let (result)
(maphash
(lambda (_id rule)
(when (or (null filter) (funcall filter rule))
(push rule result)))
(supertag-store-get-collection :automations))
(sort result
(lambda (a b)
(string< (plist-get a :name) (plist-get b :name))))))
(defun supertag-query-tag-occurrences ()
"Return sorted unique Org Tag Occurrence tokens from projected nodes."
(let (occurrences)
(maphash
(lambda (_node-id node-data)
(dolist (token (plist-get node-data :tag-occurrences))
(when (stringp token)
(push token occurrences))))
(supertag-store-get-collection :nodes))
(sort (delete-dups occurrences) #'string<)))
(defun supertag-query-tag-children (tag-id)
"Return Semantic Tag IDs whose explicit parent is TAG-ID."
(let (result)
(maphash
(lambda (id tag)
(when (string= (plist-get (supertag--ensure-plist tag) :extends) tag-id)
(push id result)))
(supertag-store-get-collection :tags))
result))
(defun supertag-query-field-definition-ids ()
"Return sorted global field definition IDs."
(let (ids)
(maphash
(lambda (fid _def) (push fid ids))
(supertag-store-get-collection :field-definitions))
(sort ids #'string<)))
(defun supertag-query-field-definitions ()
"Return global field definitions as (id . definition) pairs."
(let (result)
(maphash
(lambda (fid def)
(push (cons fid def) result))
(supertag-store-get-collection :field-definitions))
result))
(defun supertag-query-tag-field-associations (tag-id)
"Return TAG-ID's global field association entries."
(gethash tag-id (supertag-store-get-collection :tag-field-associations)))
(defun supertag-query-relations (&optional filter)
"Return all relations, optionally filtered by FILTER predicate."
(let (result)
(maphash
(lambda (_id relation)
(when (or (null filter) (funcall filter relation))
(push relation result)))
(supertag-store-get-collection :relations))
result))
(defun supertag-query-resolved-fields (tag-id)
"Return inherited field definitions resolved for TAG-ID."
(supertag-tag-get-all-fields tag-id))
(defun supertag-query-field-value (node-id tag-id field-name &optional raw-p)
"Return NODE-ID's FIELD-NAME value in TAG-ID's resolved schema.
When RAW-P is non-nil, return the stored value without schema default."
(if raw-p
(supertag-field-get node-id tag-id field-name)
(supertag-field-get-with-default node-id tag-id field-name)))
(defun supertag-query-relations-from (entity-id &optional type kind)
"Return relations from ENTITY-ID, optionally filtered by TYPE and KIND."
(supertag-relation-find-by-from entity-id type kind))
(defun supertag-query-relations-to (entity-id &optional type kind)
"Return relations to ENTITY-ID, optionally filtered by TYPE and KIND."
(supertag-relation-find-by-to entity-id type kind))
(defun supertag-query-relations-among (entity-ids &optional type kind)
"Return relations whose endpoints are both in ENTITY-IDS.
TYPE and KIND optionally filter the induced relation set."
(let ((id-set (make-hash-table :test 'equal))
result)
(dolist (entity-id entity-ids)
(puthash entity-id t id-set))
(maphash
(lambda (entity-id _present)
(dolist (relation (supertag-query-relations-from entity-id type kind))
(when (gethash (plist-get relation :to) id-set)
(push relation result))))
id-set)
(nreverse result)))
(defun supertag-query-node-tags (node-id)
"Return the Semantic Tag IDs attached to NODE-ID."
(let* ((relations (supertag-query-relations-from node-id :node-tag))
(relation-tags (mapcar (lambda (relation) (plist-get relation :to))
relations))
(node (supertag-query-node node-id))
(node-tags (and node (plist-get node :tags))))
(cl-remove-if-not
#'stringp
(cl-delete-duplicates (append relation-tags (or node-tags '()))
:test #'equal))))
(defun supertag-query-node-detail (node-id)
"Return the composed node detail needed by node-oriented views."
(when-let* ((node (supertag-query-node node-id)))
(let ((tag-ids (supertag-query-node-tags node-id))
fields)
(dolist (tag-id tag-ids)
(dolist (field-def (ignore-errors
(supertag-query-resolved-fields tag-id)))
(when-let* ((field-name (plist-get field-def :name)))
(push (list :tag-id tag-id
:field-def field-def
:value (supertag-query-field-value
node-id tag-id field-name))
fields))))
(setq fields (nreverse fields))
(let* ((refs-to (mapcar (lambda (relation) (plist-get relation :to))
(supertag-query-relations-from
node-id :reference)))
(refs-from (mapcar (lambda (relation) (plist-get relation :from))
(supertag-query-relations-to
node-id :reference))))
(list :id node-id
:node node
:tags tag-ids
:fields fields
:refs-to refs-to
:refs-from refs-from
:field-count (length fields)
:ref-count (+ (length refs-to) (length refs-from)))))))
(defun supertag-query-board-detail (board-id)
"Return BOARD-ID with placed node details and their induced relations."
(when-let* ((board (supertag-board-get board-id)))
(let ((placements (plist-get board :node-placements))
nodes)
(dolist (placement placements)
(push (list :id (car placement)
:placement (cdr placement)
:detail (supertag-query-node-detail (car placement)))
nodes))
(list :board board
:nodes (nreverse nodes)
:relations (supertag-query-relations-among
(mapcar #'car placements))))))
(defun supertag-query (collection &optional filter)
"Query data from a COLLECTION in the central store.
COLLECTION is the path to the collection (e.g., :nodes, :tags, :relations).
FILTER is an optional function that receives (id . data) pairs and returns t if the item should be included.
Returns a list of (id . data) pairs for matching items."
(let* ((path (if (listp collection) collection (list collection)))
(key (and (= (length path) 1) (car path))))
(if key
(let ((bucket (supertag-store-get-collection key))
results)
(maphash
(lambda (id value)
(when (or (null filter) (funcall filter id value))
(push (cons id value) results)))
bucket)
(nreverse results))
;; Path is deeper than one segment; use store API for nested access
(let ((data (supertag-store-get-entity (car path) (cadr path))))
(if (not (hash-table-p data))
'()
(let (results)
(maphash
(lambda (id value)
(when (or (null filter) (funcall filter id value))
(push (cons id value) results)))
data)
(nreverse results)))))))
;;; --- S-expression Query Engine (User-facing) ---
;; All simple API queries have been moved to supertag-store.el index functions
(defun supertag-query-nodes (&optional filter)
"Query all nodes in the store with an optional filter.
FILTER is an optional function that receives (id node-data) and returns t if the node should be included.
Returns a list of (id . node-data) pairs."
(supertag-query '(:nodes) filter))
;;; --- S-expression Query Engine ---
;; This is the new high-performance query engine that replaces the old supertag-query.el
;; It uses indexes for O(1) lookups instead of O(n) table scans
(require 'cl-lib)
(defun supertag-query-node-ids (query-sexp)
"Execute QUERY-SEXP and return matching node IDs.
QUERY-SEXP is an S-expression like (and (tag \"foo\") (term \"bar\")).
sort-by modifiers are applied in order, so the returned IDs are sorted
when the query carries sort-by. Aggregate queries must use
`supertag-query-evaluate' instead and signal here.
Returns a list of node IDs matching the query."
(let* ((ast (supertag-query--parse-sexp query-sexp))
(modifiers (supertag-query--ast-modifiers ast))
(node-ids (supertag-query--execute-ast ast)))
(when (cl-some
(lambda (modifier)
(memq (plist-get modifier :type)
'(sum count avg min max first last unique-count concat
group-by)))
modifiers)
(error "Aggregate queries use `supertag-query-evaluate', not `supertag-query-node-ids'"))
(supertag-query--apply-modifiers node-ids modifiers)))
(defun supertag-query-evaluate (query-sexp)
"Execute QUERY-SEXP and return the full result.
Without aggregate modifiers this is the matching node-ID list; with an
aggregate it is a scalar, or an alist of (group-key . value) when the
query also carries group-by."
(let* ((ast (supertag-query--parse-sexp query-sexp))
(node-ids (supertag-query--execute-ast ast)))
(supertag-query--apply-modifiers
node-ids (supertag-query--ast-modifiers ast))))
(defun supertag-query-sexp (query-sexp)
"Compatibility entry point for `supertag-query-node-ids'."
(supertag-query-node-ids query-sexp))
(defun supertag-query-fields (query-sexp)
"Return field keys referenced by QUERY-SEXP, for table headers."
(supertag-query--get-fields-from-ast
(supertag-query--parse-sexp query-sexp)))
(defun supertag-query-validate (query-sexp)
"Validate QUERY-SEXP with the engine's parser, returning it.
Signals the same error the executor would raise on malformed input."
(supertag-query--parse-sexp query-sexp)
query-sexp)
(defun supertag-query-date-valid-p (date-str)
"Return non-nil when DATE-STR is accepted by the engine's date resolver."
(and (supertag-query--resolve-date-string date-str) t))
(defun supertag-query-expand (query-string)
"Expand dynamic variables in QUERY-STRING.
`<%today%>', `<%yesterday%>' and `<%tomorrow%>' become the bare day
symbols today/yesterday/tomorrow, which the date resolver turns into the
local midnight of that day. Run this before reading the string as a sexp."
(replace-regexp-in-string
"<%today%>\\|<%yesterday%>\\|<%tomorrow%>"
(lambda (variable)
(substring variable 2 -2))
query-string t t))
(defun supertag-query--date-arg (arg)
"Normalize a date argument ARG to a string.
Symbols (e.g. from dynamic variables like <%today%>) become their name."
(if (stringp arg) arg (symbol-name arg)))
(defun supertag-query--modifier-ast-p (ast)
"Return non-nil when AST is a result modifier (sort-by/aggregate)."
(memq (plist-get ast :type)
'(sort-by sum count avg min max first last unique-count concat group-by)))
(defun supertag-query--parse-sexp (query-sexp)
"Parse a query S-expression into an AST.
This function is compatible with the old query syntax."
(let ((op (car query-sexp))
(args (cdr query-sexp)))
(cond
((eq op 'and)
(let* ((children (mapcar #'supertag-query--parse-sexp args))
(modifiers (cl-remove-if-not #'supertag-query--modifier-ast-p
children))
(filters (cl-remove-if #'supertag-query--modifier-ast-p
children)))
`(:type and :children ,filters :modifiers ,modifiers)))
((eq op 'or)
(let ((children (mapcar #'supertag-query--parse-sexp args)))
(when (cl-some #'supertag-query--modifier-ast-p children)
(error "Result modifiers like sort-by/aggregates only belong inside 'and'"))
`(:type or :children ,children)))
((eq op 'not)
(unless (>= (length args) 1)
(error "'not' operator expects at least one argument, but got %S" args))
`(:type not :children ,(mapcar #'supertag-query--parse-sexp args)))
((eq op 'tag)
(unless (= (length args) 1)
(error "'tag' operator expects exactly one argument, but got %S" args))
`(:type tag :value ,(if (stringp (car args)) (car args) (symbol-name (car args)))))
((eq op 'sort-by)
(unless (<= 1 (length args) 2)
(error "'sort-by' operator expects a field key and an optional asc/desc order, but got %S" args))
(let* ((key (if (stringp (car args)) (car args) (symbol-name (car args))))
(order-arg (and (cdr args) (cadr args)))
(order (cond
((null order-arg) 'desc)
((eq order-arg 'asc) 'asc)
((eq order-arg 'desc) 'desc)
((and (stringp order-arg) (string= order-arg "asc")) 'asc)
((and (stringp order-arg) (string= order-arg "desc")) 'desc)
(t (error "Invalid sort-by order: %S" order-arg)))))
`(:type sort-by :key ,key :order ,order)))
((memq op '(sum avg min max first last unique-count concat))
(unless (= (length args) 1)
(error "'%s' operator expects exactly one field key, but got %S" op args))
`(:type ,op :key ,(if (stringp (car args)) (car args)
(symbol-name (car args)))))
((eq op 'count)
(unless (null args)
(error "'count' operator takes no arguments, but got %S" args))
'(:type count :key nil))
((eq op 'group-by)
(unless (= (length args) 1)
(error "'group-by' operator expects exactly one field key, but got %S" args))
`(:type group-by :key ,(if (stringp (car args)) (car args)
(symbol-name (car args)))))
((eq op 'task)
;; Zero or more states; zero states match no node (identity of OR).
`(:type task
:values ,(mapcar (lambda (a)
(if (stringp a) a (symbol-name a)))
args)))
((eq op 'priority)
;; Zero or more priorities; zero priorities match no node.
`(:type priority
:values ,(mapcar (lambda (a)
(if (stringp a) a (symbol-name a)))
args)))
((eq op 'field)
(unless (= (length args) 2)
(error "'field' operator expects exactly two arguments, but got %S" args))
`(:type field :key ,(if (stringp (car args)) (car args) (symbol-name (car args)))
:value ,(if (stringp (cadr args)) (cadr args) (symbol-name (cadr args)))))
((eq op 'after)
(unless (= (length args) 1)
(error "'after' operator expects one date string argument, but got %S" args))
`(:type after :date ,(supertag-query--date-arg (car args))))
((eq op 'before)
(unless (= (length args) 1)
(error "'before' operator expects one date string argument, but got %S" args))
`(:type before :date ,(supertag-query--date-arg (car args))))
((eq op 'between)
(unless (= (length args) 2)
(error "'between' operator expects two date string arguments, but got %S" args))
`(:type between :start-date ,(supertag-query--date-arg (car args))
:end-date ,(supertag-query--date-arg (cadr args))))
((eq op 'term)
(unless (= (length args) 1)
(error "'term' operator expects exactly one argument, but got %S" args))
`(:type term :value ,(if (stringp (car args)) (car args) (symbol-name (car args)))))
;; Date sugar below: these desugar to `after'/`between' at parse time,
;; so the executor needs no knowledge of them. All match on :created-at
;; (the same timestamp the other date operators use).
((eq op 'recent-days)
(let ((n (car args)))
(when (and (stringp n) (string-match-p "\\`[0-9]+\\'" n))
(setq n (string-to-number n)))
(unless (and (= (length args) 1) (integerp n) (> n 0))
(error "'recent-days' operator expects one positive integer, but got %S" args))
`(:type after :date ,(format "-%dd" n))))
((eq op 'in-month)
(unless (and (= (length args) 1) (stringp (car args))
(string-match "\\`\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)\\'" (car args)))
(error "'in-month' operator expects one \"YYYY-MM\" string, but got %S" args))
(let* ((year (string-to-number (match-string 1 (car args))))
(month (string-to-number (match-string 2 (car args)))))
(unless (<= 1 month 12)
(error "'in-month': month out of range in %S" (car args)))
`(:type between
:start-date ,(format "%04d-%02d-01" year month)
:end-date ,(if (= month 12)
(format "%04d-01-01" (1+ year))
(format "%04d-%02d-01" year (1+ month))))))
((eq op 'in-year)
(let ((arg (car args)))
(when (and (stringp arg) (string-match-p "\\`[0-9]\\{4\\}\\'" arg))
(setq arg (string-to-number arg)))
(unless (and (= (length args) 1) (integerp arg) (> arg 0))
(error "'in-year' operator expects one \"YYYY\" argument, but got %S" args))
`(:type between
:start-date ,(format "%04d-01-01" arg)
:end-date ,(format "%04d-01-01" (1+ arg)))))
(t (error "Invalid query operator: %S" op)))))
(defun supertag-query--execute-ast (ast)
"Execute a query AST and return a list of matching node IDs.
This uses indexes for O(1) lookups instead of O(n) table scans."
(let ((ast-type (plist-get ast :type)))
(cond
((eq ast-type 'and)
(let ((child-results (mapcar #'supertag-query--execute-ast (plist-get ast :children)))
(all-node-ids (supertag-query--get-all-node-ids)))
(cl-reduce (lambda (result next-list)
(cl-intersection result next-list :test #'equal))
child-results
:initial-value all-node-ids)))
((eq ast-type 'or)
(let ((child-results (mapcar #'supertag-query--execute-ast (plist-get ast :children))))
(cl-reduce #'cl-union child-results :initial-value '())))
((eq ast-type 'not)
;; (not a b ...) excludes the union of the children, i.e.
;; (not (or a b ...)). A single child keeps the old behavior.
(let ((all-node-ids (supertag-query--get-all-node-ids))
(nodes-to-exclude
(cl-reduce #'cl-union
(mapcar #'supertag-query--execute-ast
(plist-get ast :children))
:initial-value '())))
(cl-set-difference (or all-node-ids '()) nodes-to-exclude :test #'equal)))
;; Fast index-based lookups below
((eq ast-type 'tag)
(let ((tag-value (plist-get ast :value)))
(let ((result (supertag-index-get-nodes-by-tag tag-value)))
result)))
((eq ast-type 'task)
;; Case-sensitive match against the projected :todo state; a node
;; without a todo state never matches.
(let ((states (plist-get ast :values))
matches)
(dolist (pair (supertag-query-nodes (lambda (_id data) data)))
(let ((todo (plist-get (cdr pair) :todo)))
(when (and todo (member todo states))
(push (car pair) matches))))
matches))
((eq ast-type 'priority)
;; Case-insensitive match against the projected :priority cookie;
;; the projection stores "#A" (with the leading #) while queries
;; spell it "A", so both sides strip the # before comparing.
(let ((states
(mapcar (lambda (state)
(upcase (replace-regexp-in-string "^#" "" state)))
(plist-get ast :values)))
matches)
(dolist (pair (supertag-query-nodes (lambda (_id data) data)))
(let ((priority (plist-get (cdr pair) :priority)))
(when (and priority
(member (upcase (replace-regexp-in-string
"^#" "" priority))
states))
(push (car pair) matches))))
matches))
((eq ast-type 'field)
(supertag-query--find-nodes-by-field-indexed (plist-get ast :key) (plist-get ast :value)))
((eq ast-type 'after)
(let ((query-time (supertag-query--resolve-date-string (plist-get ast :date))))
(unless query-time (error "Invalid date format for 'after': %s" (plist-get ast :date)))
(supertag-index-get-nodes-by-date-range query-time nil)))
((eq ast-type 'before)
(let ((query-time (supertag-query--resolve-date-string (plist-get ast :date))))
(unless query-time (error "Invalid date format for 'before': %s" (plist-get ast :date)))
(supertag-index-get-nodes-by-date-range nil query-time)))
((eq ast-type 'between)
(let ((start-time (supertag-query--resolve-date-string (plist-get ast :start-date)))
(end-time (supertag-query--resolve-date-string (plist-get ast :end-date))))
(unless start-time (error "Invalid start date for 'between': %s" (plist-get ast :start-date)))
(unless end-time (error "Invalid end date for 'between': %s" (plist-get ast :end-date)))
(supertag-index-get-nodes-by-date-range start-time end-time)))
((eq ast-type 'term)
(supertag-index-get-nodes-by-word (plist-get ast :value)))
;; A bare modifier query (e.g. (sort-by "title" asc)) means
;; "all nodes, modified": modifiers are applied by the public entry
;; point after execution.
((eq ast-type 'sort-by)
(supertag-query--get-all-node-ids))
(t '()))))
(defun supertag-query--get-all-node-ids ()
"Get all node IDs in the system."
(let (all-ids)
(maphash (lambda (id _data) (push id all-ids))
(supertag-store-get-collection :nodes))
all-ids))
(defun supertag-query--ast-modifiers (ast)
"Return the result modifiers carried by AST, in order.
Modifiers live in an 'and' node's :modifiers slot; a bare modifier
query is itself a modifier."
(if (supertag-query--modifier-ast-p ast)
(list ast)
(or (plist-get ast :modifiers) '())))
(defun supertag-query--numeric (value)
"Return VALUE as a number if it is one, or a numeric-looking string. Else nil."
(cond
((numberp value) value)
((and (stringp value)
(string-match-p "\\`[ \t]*-?[0-9]+\\(\\.[0-9]+\\)?[ \t]*\\'" value))
(string-to-number value))
(t nil)))
(defun supertag-query--value< (a b)
"Return non-nil if sort value A sorts before sort value B.
Numeric comparison when both parse as numbers; Emacs-time comparison when
both look like Emacs time values (as used by :created-at/:modified-at);
string comparison otherwise."
(let ((na (supertag-query--numeric a))
(nb (supertag-query--numeric b)))
(cond
((and na nb) (< na nb))
((and (consp a) (consp b) (integerp (car a)) (integerp (car b)))
(time-less-p a b))
(t (string< (format "%s" a) (format "%s" b))))))
(defun supertag-query--sort-value (node-id node key)
"Return the raw sort value for NODE-ID/NODE for normalized sort KEY."
(cond
((string= key "title") (plist-get node :title))
((string= key "created") (plist-get node :created-at))
((string= key "modified") (plist-get node :modified-at))
(t (supertag-query-field-value node-id nil key t))))
(defun supertag-query--sort-node-ids (node-ids key order)
"Sort NODE-IDS by KEY per ORDER.
Nodes missing the sort key are always placed last, regardless of ORDER."
(let (with-key without-key)
(dolist (id node-ids)
(let* ((node (supertag-query-node id))
(v (and node (supertag-query--sort-value id node key))))
(if v (push (cons id v) with-key) (push id without-key))))
(setq with-key (nreverse with-key)
without-key (nreverse without-key))
(setq with-key
(sort with-key
(lambda (a b) (supertag-query--value< (cdr a) (cdr b)))))
(when (eq order 'desc) (setq with-key (nreverse with-key)))
(append (mapcar #'car with-key) without-key)))
(defun supertag-query--aggregate-values (nodes type key)
"Aggregate NODES (list of node plists) with TYPE over KEY.
count ignores KEY; sum/avg return nil for non-numeric value sets."
(if (eq type 'count)
(length nodes)
(let ((values
(cl-remove-if-not
#'identity
(mapcar (lambda (node)
(supertag-query--sort-value
(plist-get node :id) node key))
nodes))))
(if (and (memq type '(sum avg))
(not (cl-every #'numberp values)))
nil
(supertag-rollup-apply type values)))))
(defun supertag-query--group-values (node-ids group-key)
"Group NODE-IDS by GROUP-KEY into a (group-key . node-plists) alist."
(let ((groups (make-hash-table :test 'equal)))
(dolist (id node-ids)
(let* ((node (supertag-query-node id))
(key (and node
(supertag-query--sort-value id node group-key)))
(bucket (or key "__ungrouped__")))
(let ((existing (gethash bucket groups)))
(puthash bucket (cons node existing) groups))))
(let (result)
(maphash (lambda (key value)
(push (cons key (nreverse value)) result))
groups)
(sort result
(lambda (a b)
(string< (format "%s" (car a))
(format "%s" (car b))))))))
(defun supertag-query--apply-modifiers (node-ids modifiers)
"Apply result MODIFIERS to NODE-IDS.
Pipeline: sort-by (if any) -> group-by (if any) -> aggregate (if any).
Returns node IDs without aggregates, a scalar with an aggregate, or an
alist of (group-key . value) with group-by + aggregate."
(let* ((sorts (cl-remove-if-not
(lambda (modifier) (eq (plist-get modifier :type) 'sort-by))
modifiers))
(groups (cl-remove-if-not
(lambda (modifier) (eq (plist-get modifier :type) 'group-by))
modifiers))
(aggregates
(cl-remove-if-not
(lambda (modifier)
(memq (plist-get modifier :type)
'(sum count avg min max first last unique-count concat)))
modifiers))
(sorted node-ids))
(dolist (sort modifiers)
(pcase (plist-get sort :type)
('sort-by
(setq sorted
(supertag-query--sort-node-ids
sorted (plist-get sort :key) (plist-get sort :order))))))
(when (> (length groups) 1)
(error "Only one group-by modifier is allowed"))
(when (> (length aggregates) 1)
(error "Only one aggregate modifier is allowed"))
(when (and groups (null aggregates))
(error "group-by requires an aggregate modifier"))
(if (null aggregates)
sorted
(let* ((group-field (and groups (plist-get (car groups) :key)))
(aggregate (car aggregates))
(agg-type (plist-get aggregate :type))
(agg-key (plist-get aggregate :key)))
(if group-field
(mapcar
(lambda (entry)
(cons (car entry)
(supertag-query--aggregate-values
(cdr entry) agg-type agg-key)))
(supertag-query--group-values sorted group-field))
(supertag-query--aggregate-values
(mapcar #'supertag-query-node sorted) agg-type agg-key))))))
(defun supertag-query-modifiers (query-sexp)
"Return the result modifiers of QUERY-SEXP, in order."
(supertag-query--ast-modifiers
(supertag-query--parse-sexp query-sexp)))
(defun supertag-query--find-nodes-by-field-indexed (field-name value)
"Find nodes by field using indexed lookup.
This is much faster than the old approach that scanned the entire link table."
(let ((matching-nodes nil)
(fid (supertag-field-resolve-id nil field-name))
(values (supertag-store-get-collection :field-values)))
(when (and fid (hash-table-p values))
(maphash
(lambda (node-id table)
(when (and (hash-table-p table)
(equal (gethash fid table) value))
(push node-id matching-nodes)))
values))
(nreverse matching-nodes)))
(defun supertag-query--resolve-date-string (date-str)
"Resolve a date string into an absolute time value.
Handles absolute dates ('YYYY-MM-DD'), 'now', the day symbols
today/yesterday/tomorrow (resolved to local midnight), and relative
dates ('-7d', '+2w', '-4h', '+30min', etc.).
Compatible with the old query engine date format."
(let ((now (current-time)))
(cond
;; Case 1: "now"
((string= date-str "now") now)
;; Case 1b: day symbols, resolved to local midnight.
((member date-str '("today" "yesterday" "tomorrow"))
(let* ((decoded (decode-time now))
(day (nth 3 decoded))
(month (nth 4 decoded))
(year (nth 5 decoded))
(delta (pcase date-str
("today" 0)
("yesterday" -1)
("tomorrow" 1))))
;; encode-time normalizes day overflow (0, 32, ...) itself.
(encode-time 0 0 0 (+ day delta) month year)))
;; Case 2: Relative date like "-7d", "+2w", "-4h", "+30min"
((string-match "^\\([+-]\\)?\\([0-9]+\\)\\(d\\|w\\|m\\|y\\|h\\|min\\)$" date-str)
(let* ((sign (if (match-string 1 date-str) (match-string 1 date-str) "+"))
(num (string-to-number (match-string 2 date-str)))
(unit (match-string 3 date-str))
(seconds-per-day 86400)
(delta-seconds
(* num
(pcase unit
("d" seconds-per-day)
("w" (* 7 seconds-per-day))
;; Approximation: 30 days for a month
("m" (* 30 seconds-per-day))
;; Approximation: 365.25 days for a year
("y" (* 365.25 seconds-per-day))
("h" 3600)
("min" 60)))))
(if (string= sign "-")
(time-subtract now (seconds-to-time delta-seconds))
(time-add now (seconds-to-time delta-seconds)))))
;; Case 3: Absolute date "YYYY-MM-DD"
((string-match "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}$" date-str)
;; `parse-time-string' on a bare date returns a decoded-time list
;; with nil SEC/MIN/HOUR -- not a value `time-less-p' accepts.
;; Fill midnight and encode to a real time value.
(let ((decoded (parse-time-string date-str)))
(encode-time 0 0 0
(nth 3 decoded) (nth 4 decoded) (nth 5 decoded))))
;; Default: Invalid format
(t nil))))
(defun supertag-query--get-fields-from-ast (ast)
"Extract field keys from the query AST.
Used for generating table headers in Org Babel output."
(let ((fields '()))
(cl-labels ((walk (sub-ast)
(let ((type (plist-get sub-ast :type)))
(cond
((member type '(and or))
(dolist (child (plist-get sub-ast :children)) (walk child)))
((eq type 'not)
(dolist (child (plist-get sub-ast :children)) (walk child)))
((eq type 'field)
(push (plist-get sub-ast :key) fields))))))
(walk ast))
(cl-delete-duplicates fields :test #'string=)))
(defun supertag-query--get-node-field-value (node-id field-name)
"Get the value of FIELD-NAME for NODE-ID.
This reads the global field Store."
(when-let* ((fid (supertag-field-resolve-id nil field-name)))
(supertag-node-get-global-field node-id fid)))
;;; --- Extended Query System for Relations and Databases ---
(defun supertag-query-related (entity-id &optional relation-type direction relation-kind)
"Query entities related to ENTITY-ID.
RELATION-TYPE and RELATION-KIND optionally filter relations.
DIRECTION can be :from, :to, or nil (both directions).
Returns list of (related-id . related-data) pairs."
(let ((relations-from (when (or (null direction) (eq direction :from))
(require 'supertag-ops-relation)
(supertag-relation-find-by-from
entity-id relation-type relation-kind)))
(relations-to (when (or (null direction) (eq direction :to))
(require 'supertag-ops-relation)
(supertag-relation-find-by-to
entity-id relation-type relation-kind)))
(results '()))
;; Collect related entities from outgoing relations
(dolist (relation relations-from)
(let* ((related-id (plist-get relation :to))
(related-data (or (supertag-node-get related-id)
(supertag-tag-get related-id))))
(when related-data
(push (cons related-id related-data) results))))
;; Collect related entities from incoming relations
(dolist (relation relations-to)
(let* ((related-id (plist-get relation :from))
(related-data (or (supertag-node-get related-id)
(supertag-tag-get related-id))))
(when related-data
(push (cons related-id related-data) results))))
(cl-remove-duplicates results :test (lambda (a b) (equal (car a) (car b))))))
(defun supertag-query-database-records (database-id &optional view-config)
"Query all records (nodes) belonging to a database.
DATABASE-ID is the database tag identifier.
VIEW-CONFIG is optional view configuration for filtering/sorting.
Returns list of (node-id . node-data) pairs."
(require 'supertag-ops-tag)
(let* ((all-nodes (supertag-query '(:nodes)))
(database-records '()))
;; Find all nodes that belong to this database
(dolist (node-pair all-nodes)
(let* ((node-id (car node-pair))
(node-data (cdr node-pair))
(tags (plist-get node-data :tags)))
(when (member database-id tags)
(push node-pair database-records))))
;; Apply view configuration if provided
(if view-config
(supertag-query--apply-view-config database-records view-config)
database-records)))
(defun supertag-query--apply-view-config (records view-config)
"Apply view configuration to filter and sort RECORDS.
VIEW-CONFIG contains filter, sort, and grouping options."
(let* ((filter-config (plist-get view-config :filter))
(sort-config (plist-get view-config :sort))
(group-config (plist-get view-config :group-by))
(limit (plist-get view-config :limit))
(filtered-records records))
;; Apply filters
(when filter-config
(setq filtered-records
(supertag-query--apply-filters filtered-records filter-config)))
;; Apply sorting
(when sort-config
(setq filtered-records
(supertag-query--apply-sorting filtered-records sort-config)))
;; Apply limit
(when limit
(setq filtered-records (cl-subseq filtered-records 0 (min limit (length filtered-records)))))
;; Apply grouping (returns grouped structure)
(if group-config
(supertag-query--apply-grouping filtered-records group-config)
filtered-records)))
(defun supertag-query--apply-filters (records filter-config)
"Apply filters to RECORDS based on FILTER-CONFIG."
(cl-remove-if-not
(lambda (record)
(let ((node-data (cdr record)))
(supertag-query--evaluate-filter-condition node-data filter-config)))
records))
(defun supertag-query--evaluate-filter-condition (node-data condition)
"Evaluate a filter CONDITION against NODE-DATA."
(pcase (car condition)
('and
(cl-every (lambda (sub-condition)
(supertag-query--evaluate-filter-condition node-data sub-condition))
(cdr condition)))
('or
(cl-some (lambda (sub-condition)
(supertag-query--evaluate-filter-condition node-data sub-condition))
(cdr condition)))
('not
(not (supertag-query--evaluate-filter-condition node-data (cadr condition))))
('org-property
(let* ((prop-name (cadr condition))
(operator (caddr condition))
(expected-value (cadddr condition))
(props (plist-get node-data :properties))
(actual-value (plist-get props (intern prop-name))))
(supertag-query--compare-values actual-value operator expected-value)))
('title
(let* ((operator (cadr condition))
(expected-value (caddr condition))
(actual-value (plist-get node-data :title)))
(supertag-query--compare-values actual-value operator expected-value)))
('tag
(let* ((operator (cadr condition))
(expected-tag (caddr condition))
(tags (plist-get node-data :tags)))
(pcase operator
('has (member expected-tag tags))
('not-has (not (member expected-tag tags)))
(_ nil))))
(_ nil)))
(defun supertag-query--compare-values (actual operator expected)
"Compare ACTUAL value with EXPECTED using OPERATOR."
(pcase operator
('= (equal actual expected))
('!= (not (equal actual expected)))
('> (and (numberp actual) (numberp expected) (> actual expected)))
('< (and (numberp actual) (numberp expected) (< actual expected)))
('>= (and (numberp actual) (numberp expected) (>= actual expected)))
('<= (and (numberp actual) (numberp expected) (<= actual expected)))
('contains (and (stringp actual) (stringp expected) (string-match-p expected actual)))
('starts-with (and (stringp actual) (stringp expected) (string-prefix-p expected actual)))
('ends-with (and (stringp actual) (stringp expected) (string-suffix-p expected actual)))
('empty (or (null actual) (and (stringp actual) (string-empty-p actual))))
('not-empty (not (or (null actual) (and (stringp actual) (string-empty-p actual)))))
(_ nil)))
(defun supertag-query--apply-sorting (records sort-config)
"Apply sorting to RECORDS based on SORT-CONFIG."
(let ((sort-field (plist-get sort-config :field))
(sort-order (or (plist-get sort-config :order) :asc)))
(sort records
(lambda (a b)
(let* ((node-a (cdr a))
(node-b (cdr b))
(value-a (supertag-query--get-sort-value node-a sort-field))
(value-b (supertag-query--get-sort-value node-b sort-field)))
(if (eq sort-order :desc)
(supertag-query--compare-sort-values value-b value-a)
(supertag-query--compare-sort-values value-a value-b)))))))
(defun supertag-query--get-sort-value (node-data field)
"Get sort value from NODE-DATA for FIELD."
(pcase field
('title (plist-get node-data :title))
('created-at (plist-get node-data :created-at))
('modified-at (plist-get node-data :modified-at))
('todo (plist-get node-data :todo))
('priority (plist-get node-data :priority))
(_
;; It's a custom field. We must use supertag-field-get.
;; A field can belong to any tag on the node. Find the first value.
(catch 'found
(dolist (tag-id (plist-get node-data :tags))
(let ((value (supertag-field-get (plist-get node-data :id) tag-id (symbol-name field))))
(when value
(throw 'found value))))))))
(defun supertag-query--compare-sort-values (a b)
"Compare two sort values A and B."
(cond
((and (null a) (null b)) nil)
((null a) t)
((null b) nil)
((and (numberp a) (numberp b)) (< a b))
((and (stringp a) (stringp b)) (string< a b))
((and (listp a) (listp b)) ; timestamps
(time-less-p a b))
(t (string< (format "%s" a) (format "%s" b)))))
(defun supertag-query--apply-grouping (records group-config)
"Apply grouping to RECORDS based on GROUP-CONFIG.
Returns alist of (group-value . records-list)."
(let ((group-field (plist-get group-config :field))
(groups (make-hash-table :test 'equal)))
;; Group records by field value
(dolist (record records)
(let* ((node-data (cdr record))
(group-value (supertag-query--get-sort-value node-data group-field))
(group-key (or group-value "__ungrouped__")))
(let ((existing-group (gethash group-key groups)))
(puthash group-key (cons record existing-group) groups))))
;; Convert to alist and sort groups
(let ((result '()))
(maphash (lambda (key value)
(push (cons key (nreverse value)) result))
groups)