Skip to content

Commit 8169abd

Browse files
committed
In Query#optimize! partition required from optional patterns so that optional patterns always come last.
1 parent 2075320 commit 8169abd

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

lib/rdf/query.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,22 @@ def optimize(**options)
247247
# Optimizes this query by reordering its constituent triple patterns
248248
# according to their cost estimates.
249249
#
250+
# Optional patterns have greater cost than non-optional patterns so they will always come after non-optional patterns
251+
#
250252
# @param [Hash{Symbol => Object}] options
251253
# any additional options for optimization
252254
# @return [self]
253255
# @see RDF::Query::Pattern#cost
254256
# @since 0.3.0
255257
def optimize!(**options)
256-
@patterns.sort! do |a, b|
258+
optional, required = @patterns.partition(&:optional?)
259+
required.sort! do |a, b|
260+
(a.cost || 0) <=> (b.cost || 0)
261+
end
262+
optional.sort! do |a, b|
257263
(a.cost || 0) <=> (b.cost || 0)
258264
end
265+
@patterns = required + optional
259266
self
260267
end
261268

lib/rdf/query/variable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Variable
6565
# the variable name
6666
# @param [RDF::Term] value
6767
# an optional variable value
68-
# @param [Boolean] distinguished (true) Also interpreted by leading '??' or '$$' in name.
68+
# @param [Boolean] distinguished (true) Also interpreted by leading '?' or '$' in name. If non-distinguished, '??' or '$$'.
6969
# @param [Boolean] existential (true) Also interpreted by leading '$' in name
7070
def initialize(name = nil, value = nil, distinguished: nil, existential: nil)
7171
name = (name || "g#{__id__.to_i.abs}").to_s

0 commit comments

Comments
 (0)