graph_query 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQuery
4
+ # Turns an association into an Edge, so a graph edge is declared by naming an association the model already has.
5
+ module Associations
6
+ module_function
7
+
8
+ def edge_for(owner, name, reflection, where: nil)
9
+ if reflection.polymorphic?
10
+ raise ArgumentError,
11
+ "#{owner.name}.#{reflection.name} is polymorphic, so it has no one node type"
12
+ end
13
+
14
+ if reflection.through_reflection?
15
+ through_edge(owner, name, reflection,
16
+ where)
17
+ else
18
+ direct_edge(owner, name, reflection, where)
19
+ end
20
+ end
21
+
22
+ # belongs_to: the owner's row points at the target. has_many / has_one: the target's row points at the owner
23
+ # (and, with as:, names the owner's type).
24
+ def direct_edge(owner, name, reflection, where)
25
+ target = reflection.klass
26
+ if reflection.belongs_to?
27
+ Edge.new(name:, from: owner.polymorphic_name, to: target.polymorphic_name, declared_by: owner.name,
28
+ table_model: owner.name, source_column: owner.primary_key, target_column: reflection.foreign_key,
29
+ conditions: [where])
30
+ else
31
+ conditions = [scope_of(reflection), where]
32
+ conditions << { reflection.type => owner.polymorphic_name } if reflection.type
33
+ Edge.new(name:, from: owner.polymorphic_name, to: target.polymorphic_name, declared_by: owner.name,
34
+ table_model: target.name, source_column: reflection.foreign_key,
35
+ target_column: target.primary_key, conditions:)
36
+ end
37
+ end
38
+
39
+ # has_many :through one join model, which belongs_to the target: each join row is an edge.
40
+ def through_edge(owner, name, reflection, where)
41
+ through = reflection.through_reflection
42
+ source = reflection.source_reflection
43
+ unless !through.through_reflection? && !through.belongs_to? && source.belongs_to? && !source.polymorphic?
44
+ raise ArgumentError, "#{owner.name}.#{reflection.name}: only has_many :through a join model that " \
45
+ 'belongs_to the target can be an edge; name the join model with through: instead'
46
+ end
47
+
48
+ if reflection.scope
49
+ raise ArgumentError, "#{owner.name}.#{reflection.name}: a :through association's own scope filters the " \
50
+ 'target, not the join rows; declare the edge with through: and where: instead'
51
+ end
52
+
53
+ conditions = [scope_of(through), where]
54
+ conditions << { through.type => owner.polymorphic_name } if through.type
55
+ Edge.new(name:, from: owner.polymorphic_name, to: reflection.klass.polymorphic_name, declared_by: owner.name,
56
+ table_model: through.klass.name, source_column: through.foreign_key,
57
+ target_column: source.foreign_key, conditions:)
58
+ end
59
+
60
+ # The association's own scope, when it filters the rows that hold the edge (has_many's target, or the join
61
+ # model). Ordering is dropped; scopes that take the owner can't be walked.
62
+ def scope_of(reflection)
63
+ scope = reflection.scope
64
+ return nil if scope.nil?
65
+ if scope.arity.positive?
66
+ raise ArgumentError,
67
+ "#{reflection.name}: scopes that take the owner record can't be walked"
68
+ end
69
+
70
+ scope
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,224 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQuery
4
+ # Compiles a Query to one WITH RECURSIVE statement. Each node set is a CTE (g0 the start, gN after step N) with
5
+ # columns node_type and node_id, plus what the caller asked to carry: origin_type/origin_id, path, depth.
6
+ #
7
+ # A step of one hop is a join. A longer step is a recursive CTE: bounded steps (depth: 1..3) keep a hop count and
8
+ # stop at the bound; unbounded ones (depth: 0.. or 1..) keep a set of nodes and stop when no new node turns up,
9
+ # so cycles end them. Unbounded steps that also need a hop count (depth: 2.., or with_depth) stop at
10
+ # GraphQuery.max_depth. Paths never revisit a node.
11
+ class Compiler
12
+ attr_reader :types
13
+
14
+ def initialize(query, origin: false, depth: false, path: false)
15
+ @query = query
16
+ @origin = origin
17
+ @depth = depth
18
+ @path = path
19
+ @ctes = []
20
+ @types = [query.start.types]
21
+ compile
22
+ end
23
+
24
+ def sql
25
+ last = "g#{@query.steps.size}"
26
+ "WITH RECURSIVE #{@ctes.join(', ')} SELECT * FROM #{last}"
27
+ end
28
+
29
+ private
30
+
31
+ def conn = ActiveRecord::Base.connection
32
+
33
+ def carry = [*(%w[origin_type origin_id] if @origin), *('path' if @path)]
34
+
35
+ def compile
36
+ start = @query.start
37
+ origin = @origin ? 'node_type AS origin_type, node_id AS origin_id, ' : ''
38
+ path = @path ? "ARRAY[#{key('node_type', 'node_id')}] AS path, " : ''
39
+ @ctes << "g0 AS (SELECT DISTINCT #{origin}#{path}node_type, node_id FROM (#{start.sql}) graph_query_start)"
40
+ @query.steps.each_with_index { |step, index| compile_step(step, index + 1) }
41
+ end
42
+
43
+ def compile_step(step, index)
44
+ types_in = @types.last
45
+ edges, reached = walk_edges(step, types_in)
46
+ @types << arrival_types(step, types_in, reached)
47
+ last = index == @query.steps.size
48
+ want_depth = @depth && last
49
+ edges_sql = lambda do |bound|
50
+ edges.map { |edge, dir| "(#{edge.rows_sql(dir, edge_where: step.edge_where, bound:)})" }.join(' UNION ALL ')
51
+ end
52
+ prev = "g#{index - 1}"
53
+ name = "g#{index}"
54
+
55
+ if edges.empty?
56
+ compile_stay(step, name, prev, want_depth)
57
+ elsif @path
58
+ compile_paths(step, name, prev, edges_sql, want_depth)
59
+ elsif step.max == 1 && !want_depth
60
+ compile_hop(step, name, prev, edges_sql)
61
+ elsif step.max.nil? && step.min <= 1 && !want_depth
62
+ compile_closure(step, name, prev, edges_sql)
63
+ else
64
+ compile_bounded(step, name, prev, edges_sql, want_depth)
65
+ end
66
+ end
67
+
68
+ # No edge to follow: the step keeps the nodes it starts on (depth: 0..).
69
+ def compile_stay(step, name, prev, want_depth)
70
+ list = [*carry, 'node_type', 'node_id'].map { "x.#{it}" }.join(', ')
71
+ @ctes << "#{name} AS (SELECT DISTINCT #{list}#{', 0 AS depth' if want_depth} FROM #{prev} x " \
72
+ "WHERE #{arrive(step, 'x')})"
73
+ end
74
+
75
+ # One hop (or none, for depth: 0..1).
76
+ def compile_hop(step, name, prev, edges_sql)
77
+ parts = []
78
+ parts << stay(prev) if step.min.zero?
79
+ parts << hop(prev, 'p', edges_sql, step)
80
+ @ctes << "#{name}(#{cols_list}node_type, node_id) AS (SELECT DISTINCT * FROM " \
81
+ "(#{parts.join(' UNION ALL ')}) x WHERE #{arrive(step, 'x')})"
82
+ end
83
+
84
+ # Every node reachable in at least min (0 or 1) hops, as a set: ends on cycles.
85
+ def compile_closure(step, name, prev, edges_sql)
86
+ walk = "#{name}_walk"
87
+ seed = step.min.zero? ? stay(prev) : hop(prev, 'p', edges_sql, step)
88
+ @ctes << "#{walk}(#{cols_list}node_type, node_id) AS " \
89
+ "(#{seed} UNION #{hop(walk, 'w', edges_sql, step)})"
90
+ @ctes << "#{name}(#{cols_list}node_type, node_id) AS " \
91
+ "(SELECT DISTINCT * FROM #{walk} x WHERE #{arrive(step, 'x')})"
92
+ end
93
+
94
+ # A hop count kept per row, stopping at max (or GraphQuery.max_depth).
95
+ def compile_bounded(step, name, prev, edges_sql, want_depth)
96
+ walk = "#{name}_walk"
97
+ max = Integer(step.max || GraphQuery.max_depth)
98
+ @ctes << "#{walk}(#{cols_list}node_type, node_id, depth) AS " \
99
+ "(#{stay(prev, depth: true)} UNION #{hop(walk, 'w', edges_sql, step, max:)})"
100
+ group = [*carry, 'node_type', 'node_id'].map { "x.#{it}" }.join(', ')
101
+ depth_col = want_depth ? ', MIN(x.depth) AS depth' : ''
102
+ @ctes << "#{name} AS (SELECT #{group}#{depth_col} FROM #{walk} x " \
103
+ "WHERE x.depth >= #{Integer(step.min)} AND #{arrive(step, 'x')} GROUP BY #{group})"
104
+ end
105
+
106
+ # Every simple path: rows are walks, not nodes, so this is for showing routes, not for big graphs.
107
+ def compile_paths(step, name, prev, edges_sql, want_depth)
108
+ walk = "#{name}_walk"
109
+ max = Integer(step.max || GraphQuery.max_depth)
110
+ @ctes << "#{walk}(#{cols_list}node_type, node_id, depth) AS " \
111
+ "(#{stay(prev, depth: true)} UNION ALL #{hop(walk, 'w', edges_sql, step, max:, path: true)})"
112
+ list = [*carry, 'node_type', 'node_id', *('depth' if want_depth)].map { "x.#{it}" }.join(', ')
113
+ @ctes << "#{name} AS (SELECT #{list} FROM #{walk} x " \
114
+ "WHERE x.depth >= #{Integer(step.min)} AND #{arrive(step, 'x')})"
115
+ end
116
+
117
+ # The rows of set from, unmoved (depth 0).
118
+ def stay(from, depth: false)
119
+ "SELECT #{cols('p')}p.node_type, p.node_id#{', 0' if depth} FROM #{from} p"
120
+ end
121
+
122
+ # One hop from the rows of set from (as alias a) along the step's edges. With max, rows carry a depth and stop
123
+ # there; with path, the path grows and never revisits a node.
124
+ def hop(from, alias_name, edges_sql, step, max: nil, path: false)
125
+ conditions = [through(step)]
126
+ conditions << "#{alias_name}.depth < #{max}" if max
127
+ conditions << "NOT (#{key('e.to_type', 'e.to_id')} = ANY(#{alias_name}.path))" if path
128
+ "SELECT #{cols(alias_name, path_append: path)}e.to_type AS node_type, e.to_id AS node_id" \
129
+ "#{", #{alias_name}.depth + 1" if max} FROM #{from} #{alias_name} " \
130
+ "#{join(edges_sql, alias_name)} WHERE #{conditions.join(' AND ')}"
131
+ end
132
+
133
+ def cols_list = carry.empty? ? '' : "#{carry.join(', ')}, "
134
+
135
+ # The carried columns from alias a; in a path step, the path grows by the node arrived at.
136
+ def cols(alias_name, path_append: false)
137
+ list = carry.map do |col|
138
+ col == 'path' && path_append ? "#{alias_name}.path || #{key('e.to_type', 'e.to_id')}" : "#{alias_name}.#{col}"
139
+ end
140
+ list.map { "#{it}, " }.join
141
+ end
142
+
143
+ # The step's edges leaving each row of alias from, one LATERAL subquery per row so each edge table is read
144
+ # through its index on the column that points back.
145
+ def join(edges_sql, from) = "CROSS JOIN LATERAL (#{edges_sql.call(from)}) e"
146
+
147
+ def key(type, id) = "(#{type} || ':' || #{id}::text)"
148
+
149
+ # Edges this step may follow: those leaving the nodes it starts on and, for steps of more than one hop, those
150
+ # leaving every node they reach.
151
+ def walk_edges(step, types_in)
152
+ seen = types_in.dup
153
+ frontier = types_in
154
+ chosen = []
155
+ loop do
156
+ found = GraphQuery.registry.walkable(frontier, step.names, step.direction)
157
+ .select { |edge, _| edge.filterable_by?(step.edge_where) } - chosen
158
+ chosen.concat(found)
159
+ heads = found.map { |edge, dir| dir == :in ? edge.from : edge.to }.uniq
160
+ frontier = heads - seen
161
+ seen |= heads
162
+ break if frontier.empty? || step.max == 1
163
+ end
164
+ # A step that may stay put (depth: 0..) from nodes with no such edges is just those nodes.
165
+ if chosen.empty? && step.min.positive?
166
+ raise NoEdges,
167
+ "no #{step.names&.join('|') || 'edges'} (#{step.direction}) from #{types_in.join(', ')}"
168
+ end
169
+
170
+ [chosen, chosen.map { |edge, dir| dir == :in ? edge.from : edge.to }.uniq]
171
+ end
172
+
173
+ def arrival_types(step, types_in, reached)
174
+ types = step.min.zero? ? (types_in | reached) : reached
175
+ types &= step.labels if step.labels
176
+ wheres = step.where&.grep_v(Hash)
177
+ types &= wheres.map { GraphQuery.type_name(it) } if wheres.present?
178
+ raise NoEdges, "#{step.names&.join('|') || 'edges'} never reach #{step.labels&.join(', ')}" if types.empty?
179
+
180
+ types
181
+ end
182
+
183
+ # The conditions a node must meet to be in the step's result.
184
+ def arrive(step, from)
185
+ conditions = []
186
+ conditions << "#{from}.node_type IN (#{step.labels.map { conn.quote(it) }.join(', ')})" if step.labels
187
+ if step.where
188
+ options = step.where.map { member(it, from) }
189
+ conditions << "(#{options.join(' OR ')})"
190
+ end
191
+ conditions.empty? ? 'TRUE' : conditions.join(' AND ')
192
+ end
193
+
194
+ # Every node the walk passes through must be in the through relations (nodes of other models pass).
195
+ def through(step)
196
+ return 'TRUE' unless step.through
197
+
198
+ step.through.map do |scope|
199
+ relation = GraphQuery.relation_for(scope)
200
+ type = conn.quote(relation.klass.polymorphic_name)
201
+ "(e.to_type <> #{type} OR e.to_id IN (#{ids(relation)}))"
202
+ end.join(' AND ')
203
+ end
204
+
205
+ def member(scope, from)
206
+ relation = if scope.is_a?(Hash)
207
+ unless @types.last.size == 1
208
+ raise AmbiguousResult,
209
+ "where: Hash needs one model, this step reaches #{@types.last.join(', ')}"
210
+ end
211
+
212
+ @types.last.first.constantize.where(scope)
213
+ else
214
+ GraphQuery.relation_for(scope)
215
+ end
216
+ "(#{from}.node_type = #{conn.quote(relation.klass.polymorphic_name)} AND #{from}.node_id IN (#{ids(relation)}))"
217
+ end
218
+
219
+ def ids(relation)
220
+ relation = relation.unscope(:order) unless relation.limit_value || relation.offset_value
221
+ relation.unscope(:select).select(relation.arel_table[relation.klass.primary_key]).to_sql
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,378 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQuery
4
+ # A read-only Cypher query, run on the CTE walker:
5
+ #
6
+ # GraphQuery.cypher(<<~CYPHER, state: 'Washington')
7
+ # MATCH (s:Locale {name: $state})-[:contains*0..]->()-[:has_venue]->(v:Venue)
8
+ # WHERE v.name STARTS WITH 'The' AND NOT v.closed
9
+ # RETURN v ORDER BY v.name LIMIT 10
10
+ # CYPHER
11
+ #
12
+ # Clauses: MATCH (one or more linear patterns, comma-separated or in several MATCH clauses, joined on the variables
13
+ # they share), WHERE, RETURN [DISTINCT], ORDER BY, SKIP, LIMIT. WHERE takes =, <>, !=, <, <=, >, >=, IS [NOT] NULL,
14
+ # IN [...], STARTS WITH, ENDS WITH, CONTAINS, =~, AND, OR, XOR, NOT, parentheses, toLower() / toUpper(), $params,
15
+ # and a bare property (v.active) as a test for true. Each condition compares properties of one variable: a condition
16
+ # that compares two variables raises UnsupportedCypher.
17
+ #
18
+ # RETURN v gives a relation of v's nodes; RETURN v.name, v.kind gives pluck's rows; RETURN count(v) or count(*)
19
+ # an Integer. One variable per RETURN: walks find nodes, not rows of combinations.
20
+ class Cypher
21
+ include Syntax
22
+
23
+ Prop = Data.define(:var, :name, :func)
24
+ Lit = Data.define(:value)
25
+ Cmp = Data.define(:op, :left, :right)
26
+ Logic = Data.define(:op, :terms)
27
+ Negation = Data.define(:term)
28
+ Item = Data.define(:var, :prop, :count, :alias_name)
29
+ Order = Data.define(:prop, :direction)
30
+
31
+ COMPARISONS = { '=' => :eq, '<>' => :not_eq, '!=' => :not_eq, '<=' => :lteq, '>=' => :gteq, '<' => :lt,
32
+ '>' => :gt }.freeze
33
+ FLIPPED = { eq: :eq, not_eq: :not_eq, lteq: :gteq, gteq: :lteq, lt: :gt, gt: :lt }.freeze
34
+ FUNCTIONS = { 'tolower' => 'LOWER', 'toupper' => 'UPPER' }.freeze
35
+ TEXT_OPERATORS = /STARTS\s+WITH\b|ENDS\s+WITH\b|CONTAINS\b|=~/i
36
+ TEXT_TESTS = { 'STARTS WITH' => :starts_with, 'ENDS WITH' => :ends_with, 'CONTAINS' => :contains,
37
+ '=~' => :regexp }.freeze
38
+
39
+ attr_reader :patterns, :where, :items, :distinct, :orders, :skip, :limit
40
+
41
+ def initialize(source, params = {})
42
+ @source = source
43
+ @params = params.transform_keys(&:to_sym)
44
+ @scanner = StringScanner.new(source)
45
+ @patterns = []
46
+ @conditions = []
47
+ parse
48
+ end
49
+
50
+ # What the query returns: a relation, pluck's rows or a count (see the class comment).
51
+ def result
52
+ var = returned_var
53
+ relation = nodes(var)
54
+ return relation.count if items.any?(&:count)
55
+
56
+ relation = relation.reorder(orders.map { order_node(var, it) }) if orders.any?
57
+ relation = relation.offset(skip) if skip
58
+ relation = relation.limit(limit) if limit
59
+ columns = items.filter_map(&:prop)
60
+ return relation if columns.empty?
61
+
62
+ relation = relation.distinct if distinct
63
+ relation.pluck(*columns.map { column(var, it) })
64
+ end
65
+
66
+ # The nodes a node variable matches across every pattern and the WHERE clause, as a relation.
67
+ def nodes(var)
68
+ bindings = where_bindings
69
+ (patterns.size - 1).times { join_patterns(bindings) }
70
+ pattern = patterns.find { it.variables[var]&.first == :node } || raise(PatternError, "#{var} is not in MATCH")
71
+ match_for(pattern, bindings).nodes(var)
72
+ end
73
+
74
+ private
75
+
76
+ # -- reading
77
+
78
+ def parse
79
+ match_clauses
80
+ fail!('MATCH') if @patterns.empty?
81
+ return_clause
82
+ skip_space.skip(/;/)
83
+ fail!('the end of the query') unless skip_space.eos?
84
+ @where = @conditions.size > 1 ? Logic.new(op: :and, terms: @conditions) : @conditions.first
85
+ end
86
+
87
+ def match_clauses
88
+ loop do
89
+ raise UnsupportedCypher, 'OPTIONAL MATCH is not supported' if keyword?('OPTIONAL')
90
+ break unless keyword('MATCH')
91
+
92
+ @patterns << pattern
93
+ @patterns << pattern while skip_space.skip(/,/)
94
+ @conditions << expression if keyword('WHERE')
95
+ end
96
+ end
97
+
98
+ def return_clause
99
+ keyword('RETURN') || fail!('RETURN')
100
+ @distinct = !keyword('DISTINCT').nil?
101
+ @items = [item]
102
+ @items << item while skip_space.skip(/,/)
103
+ @orders = skip_space.skip(/ORDER\s+BY\b/i) ? orders_list : []
104
+ @skip = count_value if keyword('SKIP')
105
+ @limit = count_value if keyword('LIMIT')
106
+ end
107
+
108
+ def pattern
109
+ skip_space
110
+ Pattern.new(@source, params: @params, scanner: @scanner)
111
+ end
112
+
113
+ def expression = logic(:or) { logic(:xor) { logic(:and) { negation } } }
114
+
115
+ def logic(word, &term)
116
+ terms = [term.call]
117
+ terms << term.call while keyword(word.to_s.upcase)
118
+ terms.size == 1 ? terms.first : Logic.new(op: word, terms:)
119
+ end
120
+
121
+ def negation = keyword('NOT') ? Negation.new(term: negation) : predicate
122
+
123
+ def predicate
124
+ return grouped if skip_space.skip(/\(/)
125
+
126
+ left = operand
127
+ null_test(left) || in_test(left) || text_test(left) || comparison_test(left) || truth_test(left)
128
+ end
129
+
130
+ def grouped
131
+ inner = expression
132
+ expect(')')
133
+ inner
134
+ end
135
+
136
+ # x IS NULL, x IS NOT NULL
137
+ def null_test(left)
138
+ return unless keyword('IS')
139
+
140
+ negated = keyword('NOT')
141
+ keyword('NULL') || fail!('NULL')
142
+ test = Cmp.new(op: :eq, left:, right: Lit.new(value: nil))
143
+ negated ? Negation.new(term: test) : test
144
+ end
145
+
146
+ # x IN [...], x NOT IN [...]
147
+ def in_test(left)
148
+ negated = skip_space.skip(/NOT\s+IN\b/i)
149
+ return unless negated || keyword('IN')
150
+
151
+ test = Cmp.new(op: :in, left:, right: Lit.new(value: Array(value)))
152
+ negated ? Negation.new(term: test) : test
153
+ end
154
+
155
+ def text_test(left)
156
+ word = skip_space.scan(TEXT_OPERATORS) or return
157
+ Cmp.new(op: TEXT_TESTS.fetch(word.upcase.squeeze(' ').gsub(/\s+/, ' ')), left:, right: operand)
158
+ end
159
+
160
+ def comparison_test(left)
161
+ operator = skip_space.scan(/<>|!=|<=|>=|=|<|>/) or return
162
+ Cmp.new(op: COMPARISONS.fetch(operator), left:, right: operand)
163
+ end
164
+
165
+ # A bare property is a test for true.
166
+ def truth_test(left)
167
+ raise UnsupportedCypher, "#{left.inspect} isn't a condition" unless left.is_a?(Prop)
168
+
169
+ Cmp.new(op: :eq, left:, right: Lit.new(value: true))
170
+ end
171
+
172
+ # v.prop, toLower(v.prop), or a value.
173
+ def operand
174
+ skip_space
175
+ if (name = @scanner.check(/[A-Za-z_]\w*(?=\s*\()/)) && FUNCTIONS.key?(name.downcase)
176
+ @scanner.skip(/[A-Za-z_]\w*\s*\(/)
177
+ inner = operand
178
+ expect(')')
179
+ raise UnsupportedCypher, "#{name}() takes a property" unless inner.is_a?(Prop)
180
+
181
+ return inner.with(func: FUNCTIONS.fetch(name.downcase))
182
+ end
183
+ if (var = @scanner.check(/[A-Za-z_]\w*(?=\.)/)) && !@scanner.check(/\d/)
184
+ @scanner.skip(/[A-Za-z_]\w*\./)
185
+ return Prop.new(var: var.to_sym, name: ident || fail!('a property name'), func: nil)
186
+ end
187
+
188
+ Lit.new(value:)
189
+ end
190
+
191
+ def item
192
+ if keyword('count')
193
+ expect('(')
194
+ keyword('DISTINCT')
195
+ var = skip_space.skip(/\*/) ? nil : (ident || fail!('a variable or *')).to_sym
196
+ expect(')')
197
+ return Item.new(var:, prop: nil, count: true, alias_name: alias_name)
198
+ end
199
+
200
+ var = (ident || fail!('a variable')).to_sym
201
+ prop = skip_space.skip(/\./) ? (ident || fail!('a property name')) : nil
202
+ Item.new(var:, prop:, count: false, alias_name: alias_name)
203
+ end
204
+
205
+ def alias_name = keyword('AS') ? ident : nil
206
+
207
+ def orders_list
208
+ list = [order]
209
+ list << order while skip_space.skip(/,/)
210
+ list
211
+ end
212
+
213
+ def order
214
+ prop = operand
215
+ raise UnsupportedCypher, 'ORDER BY takes properties (v.name)' unless prop.is_a?(Prop)
216
+
217
+ direction = keyword('DESCENDING') || keyword('DESC') ? :desc : (keyword('ASCENDING') || keyword('ASC')) && :asc
218
+ Order.new(prop:, direction: direction || :asc)
219
+ end
220
+
221
+ def count_value
222
+ count = value
223
+ raise PatternError, "SKIP and LIMIT take a whole number, not #{count.inspect}" unless count.is_a?(Integer)
224
+
225
+ count
226
+ end
227
+
228
+ # -- running
229
+
230
+ def returned_var
231
+ vars = items.filter_map(&:var).uniq
232
+ vars = [last_var] if vars.empty?
233
+ raise UnsupportedCypher, "RETURN one variable, not #{vars.join(', ')}" unless vars.size == 1
234
+ raise UnsupportedCypher, 'RETURN a node variable' unless node_vars.include?(vars.first)
235
+ if (other = orders.map { it.prop.var }.find { it != vars.first })
236
+ raise UnsupportedCypher, "ORDER BY #{other} when RETURN gives #{vars.first}"
237
+ end
238
+
239
+ vars.first
240
+ end
241
+
242
+ def last_var = patterns.last.nodes.last.var || raise(UnsupportedCypher, 'count(*) needs the last node named')
243
+
244
+ def node_vars = patterns.flat_map { |pattern| pattern.nodes.filter_map(&:var) }.uniq
245
+
246
+ # One relation per variable the WHERE clause tests, from its conditions.
247
+ def where_bindings
248
+ conjuncts(where).group_by { vars_of(it) }.each_with_object({}) do |(vars, terms), bindings|
249
+ raise UnsupportedCypher, 'a condition must test a property' if vars.empty?
250
+ raise UnsupportedCypher, "a condition compares #{vars.join(' and ')}; compare one variable" if vars.size > 1
251
+
252
+ var = vars.first
253
+ model = model_for(var)
254
+ bindings[var] = model.where(terms.map { arel(model, it) }.reduce { |all, one| all.and(one) })
255
+ end
256
+ end
257
+
258
+ def conjuncts(term)
259
+ return [] if term.nil?
260
+ return term.terms.flat_map { conjuncts(it) } if term.is_a?(Logic) && term.op == :and
261
+
262
+ [term]
263
+ end
264
+
265
+ def vars_of(term)
266
+ case term
267
+ when Prop then [term.var]
268
+ when Cmp then (vars_of(term.left) + vars_of(term.right)).uniq
269
+ when Logic then term.terms.flat_map { vars_of(it) }.uniq
270
+ when Negation then vars_of(term.term)
271
+ else []
272
+ end
273
+ end
274
+
275
+ # The model a node variable's rows come from: its one label, or the one model the walk reaches there.
276
+ def model_for(var)
277
+ patterns.each do |pattern|
278
+ kind, index = pattern.variables[var]
279
+ next unless kind == :node
280
+
281
+ labels = pattern.nodes[index].labels
282
+ return labels.first.constantize if labels&.size == 1
283
+
284
+ types = Match.new(pattern, {}).query(index).types
285
+ return types.first.constantize if types.size == 1
286
+
287
+ raise UnsupportedCypher, "(#{var}) can be #{types.join(' or ')}: give it a label"
288
+ end
289
+ raise PatternError, "#{var} is not a node in MATCH"
290
+ end
291
+
292
+ # Narrow the variables patterns share: each pattern keeps a shared variable's nodes to those it matches.
293
+ def join_patterns(bindings)
294
+ counts = patterns.flat_map { |pattern| pattern.variables.select { _2.first == :node }.keys }.tally
295
+ patterns.each do |pattern|
296
+ shared = pattern.variables.select { |var, (kind, _)| kind == :node && counts[var] > 1 }.keys
297
+ next if shared.empty? || patterns.size == 1
298
+
299
+ match = match_for(pattern, bindings)
300
+ shared.each do |var|
301
+ found = match.nodes(var)
302
+ model = found.klass
303
+ bindings[var] = (bindings[var] || model.all).where(model.primary_key => found.select(model.primary_key))
304
+ end
305
+ end
306
+ end
307
+
308
+ def match_for(pattern, bindings) = Match.new(pattern, bindings.slice(*pattern.variables.keys))
309
+
310
+ def arel(model, term)
311
+ case term
312
+ when Logic
313
+ nodes = term.terms.map { arel(model, it) }
314
+ case term.op
315
+ when :and then Arel::Nodes::Grouping.new(Arel::Nodes::And.new(nodes))
316
+ when :or then nodes.reduce { |any, one| any.or(one) }
317
+ else Arel::Nodes::Grouping.new(nodes.reduce { |left, right| xor(left, right) })
318
+ end
319
+ when Negation then Arel::Nodes::Not.new(arel(model, term.term))
320
+ when Cmp then comparison(model, term)
321
+ else raise UnsupportedCypher, "#{term.inspect} isn't a condition"
322
+ end
323
+ end
324
+
325
+ def xor(left, right)
326
+ Arel::Nodes::InfixOperation.new('<>', Arel::Nodes::Grouping.new(left), Arel::Nodes::Grouping.new(right))
327
+ end
328
+
329
+ def comparison(model, cmp)
330
+ left = cmp.left
331
+ right = cmp.right
332
+ op = cmp.op
333
+ left, right, op = right, left, FLIPPED.fetch(op) if left.is_a?(Lit) && FLIPPED.key?(op)
334
+ raise UnsupportedCypher, 'a condition must test a property' unless left.is_a?(Prop)
335
+
336
+ attribute = attribute(model, left)
337
+ other = right.is_a?(Prop) ? attribute(model, right) : right.value
338
+ case op
339
+ when :eq, :not_eq then equality(attribute, op, other)
340
+ when :in then attribute.in(other)
341
+ when :starts_with then like(attribute, other, '%s%%')
342
+ when :ends_with then like(attribute, other, '%%%s')
343
+ when :contains then like(attribute, other, '%%%s%%')
344
+ when :regexp then Arel::Nodes::Regexp.new(attribute, Arel::Nodes.build_quoted("^(?:#{other})$"))
345
+ else attribute.public_send(op, other)
346
+ end
347
+ end
348
+
349
+ def equality(attribute, operator, other)
350
+ return attribute.public_send(operator, other) unless other.nil?
351
+
352
+ operator == :eq ? attribute.eq(nil) : attribute.not_eq(nil)
353
+ end
354
+
355
+ def like(attribute, text, shape)
356
+ raise UnsupportedCypher, 'STARTS WITH, ENDS WITH and CONTAINS take a string' unless text.is_a?(String)
357
+
358
+ attribute.matches(format(shape, ActiveRecord::Base.sanitize_sql_like(text)), '\\', true)
359
+ end
360
+
361
+ def attribute(model, prop)
362
+ model.columns_hash.key?(prop.name) || raise(UnsupportedCypher, "#{model.name} has no property #{prop.name}")
363
+ attribute = model.arel_table[prop.name]
364
+ prop.func ? Arel::Nodes::NamedFunction.new(prop.func, [attribute]) : attribute
365
+ end
366
+
367
+ def column(var, name)
368
+ model = model_for(var)
369
+ model.columns_hash.key?(name) || raise(UnsupportedCypher, "#{model.name} has no property #{name}")
370
+ model.arel_table[name]
371
+ end
372
+
373
+ def order_node(var, order)
374
+ node = attribute(model_for(var), order.prop)
375
+ order.direction == :desc ? node.desc : node.asc
376
+ end
377
+ end
378
+ end