Coverage report: /development/source/library/org/datagraph/spocq-shard/src/core/query-deconstruction.lisp

KindCoveredAll%
expression058 0.0
branch06 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; -*- package: spocq.i; -*-
2
 
3
 (in-package :org.datagraph.spocq.implementation)
4
 
5
 (defgeneric run-deconstructed-query (query repository &key agent)
6
   (:documentation "Execute a query by walking the algebra tree depth-first
7
    to execute each node in turn and save the individual results for later analysis.
8
    Return a tree of the individual counts and execution times.")
9
   (:method ((query query) repository &key (agent (task-agent query)))
10
     (run-deconstructed-query (query-sse-expression query) repository :agent agent))
11
   
12
   (:method ((query string) repository &rest args)
13
     (apply #'run-deconstructed-query (parse-sparql query) repository args))
14
 
15
   #+allegrograph
16
   (:method ((query cons) repository &key agent)
17
     (declare (ignore agent))
18
     (let ((results ()))
19
       (labels ((run-clause (form)
20
                  (print form)
21
                  (let ((start-time (get-internal-real-time))
22
                        (result (ignore-errors (run-sparql
23
                                                `(sparql.parser:sparql :select
24
                                                  :where ,form
25
                                                  :vars (?count ?distinctCount)
26
                                                  :aggregates
27
                                                  ((?@aggregate-1 (:count :* :distinct t))
28
                                                   (?@aggregate-2 (:count :*))
29
                                                   (?distinctCount (:_assign ?@aggregate-1))
30
                                                   (?count (:_assign ?@aggregate-2))))
31
                                                :results-format :lists)))
32
                        (end-time (get-internal-real-time)))
33
                    (when result
34
                      (destructuring-bind ((count distinct-count)) result
35
                        (push `(:time ,(- end-time start-time)
36
                                      :count ,(upi-model-value count)
37
                                      :distinct-count ,(upi-model-value distinct-count)
38
                                      :form ,form)
39
                              results))
40
                      (loop for form in (rest form)
41
                        when (traceable-form-p form)
42
                        do (run-clause form))))))
43
         (cond ((eq (first query) 'sparql.parser:sparql)
44
                (run-clause (getf (cddr query) :where)))
45
               ((traceable-form-p query)
46
                (run-clause query)))
47
         results)))
48
 
49
   (:method ((query cons) repository &key agent)
50
     (labels ((run-clause (form)
51
                (let ((start-time (get-internal-real-time)))
52
                  (multiple-value-bind (result condition)
53
                                       (ignore-errors (run-sparql
54
                                                       `(spocq.a:|select|
55
                                                                 ,form
56
                                                                 ((?::|count| (spocq.a:|count| SPOCQ.S:*))
57
                                                                  (?::|distinctCount| (spocq.a:|count| SPOCQ.S:*
58
                                                                                               :DISTINCT spocq.a:|distinct|))))
59
                                                       :repository-id repository
60
                                                       :agent agent))
61
                    (cond (result
62
                           (destructuring-bind ((count distinct-count)) result
63
                             (let ((children (unless (member (first form) '(spocq.a:|bgp| spocq.a:|service|))
64
                                               ;; suppress forms for which no sub-expression should stand independent
65
                                               ;; eg. pushed filters in bgp forms and all service sub-expressions
66
                                               (loop for form in (rest form)
67
                                                 when (traceable-form-p form)
68
                                                 collect (run-clause form)))))
69
                               `(:operator ,(string (first form))
70
                                 :time ,(- (get-internal-real-time) start-time)
71
                                 :count ,count
72
                                 :distinct-count ,distinct-count
73
                                 :form ,form
74
                                 :children ,children))))
75
                        (t
76
                         `(:form ,form :error ,(format nil "~a" condition))))))))
77
       (run-clause query))))
78
 
79
 (defparameter *traceable-operators*
80
   '(spocq.a:|and|
81
     spocq.a:|ask|
82
     spocq.a:|bgp|
83
     spocq.a:|construct|
84
     spocq.a:|describe|
85
     spocq.a:|extend|
86
     spocq.a:|filter|
87
     spocq.a:|graph|
88
     spocq.a:|join|
89
     spocq.a:|leftjoin|
90
     spocq.a:|minus|
91
     spocq.a:|not|
92
     spocq.a:|or|
93
     spocq.a:|project|
94
     spocq.a:|select|
95
     spocq.a:|service|
96
     spocq.a:|slice|
97
     spocq.a:|sum|
98
     spocq.a:|table|
99
     spocq.a:|union|)
100
   "A list of the operators for which to perform deconstructed tracing")
101
 
102
 (defun traceable-form-p (form)
103
   (and (consp form)
104
        (member (first form) *traceable-operators*)))
105
 
106
 
107
 
108
 #|
109
 (progn
110
   (sparql:parse-sparql "
111
     select (count (distinct *) as ?distinctCount)  (count (*) as ?count)
112
     where {?s ?p ?o}")
113
 
114
 
115
   
116
 
117
 (let ((*triple-store-name* "aegl-formerprod-nosensors-009-20140527-02-20140725"))
118
   (wmdb () 
119
   (format-deconstruction
120
    (run-deconstructed-query
121
     "select (count (*) as ?count)
122
      where { { ?s a <http://www.w3.org/2002/07/owl#Class> }
123
               optional
124
              { ?s <http://eu.sekkat.gl/pred#internalRealTime> ?time }
125
            }
126
     ")
127
    :stream *trace-output*)))
128
 
129
 (let ((*triple-store-name* "aegl-formerprod-nosensors-009-20140527-02-20140725"))
130
   (wmdb () 
131
         (pprint 
132
         (sparql:run-sparql "select distinct ?p where {?s ?p ?o} limit 10"
133
                     :results-type :lists))))
134
 (let ((*triple-store-name* "aegl-formerprod-nosensors-009-20140527-02-20140725"))
135
   (wmdb () 
136
         (pprint 
137
         (sparql:run-sparql "select distinct ?o where {?s a ?o} limit 10"
138
                     :results-type :lists))))
139
 
140
 (run-deconstructed-query "select * where { {?s ?p ?o} union { graph ?g {?s ?p ?o} } }"
141
                          "james/test")
142
 |#