Fix: (ancestors, parent) Normalize sub-queries

I'm not sure why calling the REC labeled function didn't correctly
normalize these sub-queries; it seemed to only normalize for one
round, leaving some queries incorrectly normalized.  But this fixes
it.

Fixes #365.

Reported-by: Gabriele Mongiano <https://github.com/kofm>
This commit is contained in:
Adam Porter 2024-02-21 05:41:49 -06:00
parent b6efba9aeb
commit c90bae0eab
4 changed files with 58 additions and 41 deletions

View file

@ -557,6 +557,7 @@ Simple links may also be written manually in either sexp or non-sexp form, like:
*Fixes* *Fixes*
+ Predicate ~heading~ incorrectly matched strings as regular expressions, sometimes returning incorrect results. (See [[https://github.com/alphapapa/org-ql/discussions/410][discussion]]. Thanks to [[https://github.com/al3xandru][Alex Popescu]] for reporting.) + Predicate ~heading~ incorrectly matched strings as regular expressions, sometimes returning incorrect results. (See [[https://github.com/alphapapa/org-ql/discussions/410][discussion]]. Thanks to [[https://github.com/al3xandru][Alex Popescu]] for reporting.)
+ Predicates ~ancestor~ and ~parent~ did not normalize their sub-queries, sometimes returning incorrect results. ([[https://github.com/alphapapa/org-ql/issues/365][#365]]. Thanks to [[https://github.com/kofm][Gabriele Mongiano]] for reporting.)
** 0.8.4 ** 0.8.4

View file

@ -2077,7 +2077,7 @@ With KEYWORDS, return non-nil if its keyword is one of KEYWORDS."
:normalizers ((`(,predicate-names :normalizers ((`(,predicate-names
;; Avoid infinitely compiling already-compiled functions. ;; Avoid infinitely compiling already-compiled functions.
,(and query (guard (not (byte-code-function-p query))))) ,(and query (guard (not (byte-code-function-p query)))))
`(ancestors ,(org-ql--query-predicate (rec query)))) `(ancestors ,(org-ql--query-predicate (org-ql--normalize-query query))))
(`(,predicate-names) '(ancestors (lambda () t)))) (`(,predicate-names) '(ancestors (lambda () t))))
:body :body
(org-with-wide-buffer (org-with-wide-buffer
@ -2089,7 +2089,7 @@ With KEYWORDS, return non-nil if its keyword is one of KEYWORDS."
:normalizers ((`(,predicate-names :normalizers ((`(,predicate-names
;; Avoid infinitely compiling already-compiled functions. ;; Avoid infinitely compiling already-compiled functions.
,(and query (guard (not (byte-code-function-p query))))) ,(and query (guard (not (byte-code-function-p query)))))
`(parent ,(org-ql--query-predicate (rec query)))) `(parent ,(org-ql--query-predicate (org-ql--normalize-query query))))
(`(,predicate-names) '(parent (lambda () t)))) (`(,predicate-names) '(parent (lambda () t))))
:body :body
(org-with-wide-buffer (org-with-wide-buffer

View file

@ -1082,6 +1082,10 @@ File: README.info, Node: 085-pre, Next: 084, Up: Changelog
discussion (https://github.com/alphapapa/org-ql/discussions/410). discussion (https://github.com/alphapapa/org-ql/discussions/410).
Thanks to Alex Popescu (https://github.com/al3xandru) for Thanks to Alex Popescu (https://github.com/al3xandru) for
reporting.) reporting.)
• Predicates ancestor and parent did not normalize their
sub-queries, sometimes returning incorrect results. (#365
(https://github.com/alphapapa/org-ql/issues/365). Thanks to
Gabriele Mongiano (https://github.com/kofm) for reporting.)
 
File: README.info, Node: 084, Next: 083, Prev: 085-pre, Up: Changelog File: README.info, Node: 084, Next: 083, Prev: 085-pre, Up: Changelog
@ -1984,45 +1988,45 @@ Node: Links38883
Node: Tips39570 Node: Tips39570
Node: Changelog39894 Node: Changelog39894
Node: 085-pre40788 Node: 085-pre40788
Node: 08441175 Node: 08441444
Node: 08341631 Node: 08341900
Node: 08241972 Node: 08242241
Node: 08142365 Node: 08142634
Node: 0842786 Node: 0843055
Node: 07445510 Node: 07445779
Node: 07345733 Node: 07346002
Node: 07246465 Node: 07246734
Node: 07147384 Node: 07147653
Node: 0748195 Node: 0748464
Node: 06351061 Node: 06351330
Node: 06251594 Node: 06251863
Node: 06151901 Node: 06152170
Node: 0652471 Node: 0652740
Node: 05255527 Node: 05255796
Node: 05155829 Node: 05156098
Node: 0556254 Node: 0556523
Node: 04957785 Node: 04958054
Node: 04858067 Node: 04858336
Node: 04758416 Node: 04758685
Node: 04658825 Node: 04659094
Node: 04559233 Node: 04559502
Node: 04459594 Node: 04459863
Node: 04359953 Node: 04360222
Node: 04260156 Node: 04260425
Node: 04160317 Node: 04160586
Node: 0460564 Node: 0460833
Node: 03264665 Node: 03264934
Node: 03165068 Node: 03165337
Node: 0365265 Node: 0365534
Node: 02368565 Node: 02368834
Node: 02268799 Node: 02269068
Node: 02169079 Node: 02169348
Node: 0269284 Node: 0269553
Node: 0173362 Node: 0173631
Node: Notes73463 Node: Notes73732
Node: Comparison with Org Agenda searches73625 Node: Comparison with Org Agenda searches73894
Node: org-sidebar74514 Node: org-sidebar74783
Node: License74793 Node: License75062
 
End Tag Table End Tag Table

View file

@ -254,6 +254,18 @@ with keyword arg NOW in PLIST."
(expect (org-ql--normalize-query "\"quoted phrase\"") (expect (org-ql--normalize-query "\"quoted phrase\"")
:to-equal '(rifle :regexps '("\"quoted phrase\"")))) :to-equal '(rifle :regexps '("\"quoted phrase\""))))
(describe "Ancestor/Parent predicates"
;; NOTE: Because the ancestor and parent predicates byte-compile their
;; subquery predicates, we have to test the byte-compiled forms here.
(expect (org-ql--normalize-query '(ancestors "scheduled"))
;; No colon after keyword, so not a predicate query.
:to-equal ;; '(ancestors (rifle :regexps '("scheduled")))
`(ancestors #[nil "\300\301\302\"\207" [rifle :regexps ("scheduled")] 3]))
(expect (org-ql--normalize-query '(parent "scheduled"))
;; No colon after keyword, so not a predicate query.
:to-equal ;; '(parent (rifle :regexps '("scheduled")))
`(parent #[nil "\300\301\302\"\207" [rifle :regexps ("scheduled")] 3])))
(describe "Plain strings" (describe "Plain strings"
(it "normalizes plain strings to the default predicate (using AND)" (it "normalizes plain strings to the default predicate (using AND)"
(expect (org-ql--normalize-query '(and "string1" "string2")) (expect (org-ql--normalize-query '(and "string1" "string2"))