Skip to content

Commit

Permalink
Serialize VALUES at the top-level if the join is at the top-level.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jan 17, 2022
1 parent 6ddab97 commit fa38ee0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 53 deletions.
6 changes: 6 additions & 0 deletions lib/sparql/algebra/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ def self.arity
# @param [Array<Symbol,Operator>] project (%i(*))
# Terms to project
# @param [Operator] reduced (false)
# @param [Operator] values_clause (nil)
# Top-level Values clause
# @param [Operator] where_clause (true)
# Emit 'WHERE' before GroupGraphPattern
# @param [Hash{Symbol => Object}] options
Expand All @@ -369,6 +371,7 @@ def self.to_sparql(content,
order_ops: [],
project: %i(*),
reduced: false,
values_clause: nil,
where_clause: true,
**options)
str = ""
Expand Down Expand Up @@ -438,6 +441,9 @@ def self.to_sparql(content,
# LimitOffsetClauses
str << "OFFSET #{offset}\n" unless offset.nil?
str << "LIMIT #{limit}\n" unless limit.nil?

# Values Clause
str << values_clause.to_sparql(**options) if values_clause
str
end

Expand Down
8 changes: 7 additions & 1 deletion lib/sparql/algebra/operator/join.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ def optimize!(**options)
# Filter Operations
# @return [String]
def to_sparql(top_level: true, filter_ops: [], extensions: {}, **options)
# If this is top-level, and the last operand is a Table (values), put the values at the outer-level
str = "{\n" + operands.first.to_sparql(top_level: false, extensions: {}, **options)

# Any accrued filters go here.
filter_ops.each do |op|
str << "\nFILTER (#{op.to_sparql(**options)}) ."
end

str << "\n" + operands.last.to_sparql(top_level: false, extensions: {}, **options) + "\n}"
if top_level && operands.last.is_a?(Table)
str << "\n}"
options = options.merge(values_clause: operands.last)
else
str << "\n" + operands.last.to_sparql(top_level: false, extensions: {}, **options) + "\n}"
end

top_level ? Operator.to_sparql(str, extensions: extensions, **options) : str
end
Expand Down
8 changes: 7 additions & 1 deletion lib/sparql/algebra/operator/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Operator
# (bgp (triple ?book dc:title ?title) (triple ?book ns:price ?price))
# (table (vars ?book) (row (?book :book1)))) ))
#
# @example SPARQL Grammar (empty query no values)
# SELECT * { } VALUES () { }
#
# @example SSE (empty query no values)
# (join (bgp) (table empty))
#
# [61] InlineData ::= 'VALUES' DataBlock
#
# @example SPARQL Grammar (InlineData)
Expand Down Expand Up @@ -99,7 +105,7 @@ def to_sparql(**options)
row[1..-1].each do |col|
line << "#{col[1].to_sparql(**options)} "
end
line = line.chop
#line = line.chop
line << ")\n"

str << line
Expand Down
81 changes: 41 additions & 40 deletions spec/algebra/to_sparql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def self.read_examples
# rdfs:label ?child_label .
# FILTER(CONTAINS(STR(?parent), "terms/ensembl/"))
# BIND(STRBEFORE(STRAFTER(STR(?ensg_location), "GRCh38/"), ":") AS ?chromosome)
# VALUES ?chromosome {
# "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
# "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22"
# "X" "Y" "MT"
# }
# }
# VALUES ?chromosome {
# "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
# "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22"
# "X" "Y" "MT"
# }
it_behaves_like "SXP to SPARQL", "#40", %{
(prefix ((obo: <http://purl.obolibrary.org/obo/>)
Expand All @@ -93,41 +93,42 @@ def self.read_examples
(dataset (<http://rdf.integbio.jp/dataset/togosite/ensembl>)
(distinct
(project (?parent ?child ?child_label)
(filter (contains (str ?parent) "terms/ensembl/")
(join
(extend ((?chromosome (strbefore (strafter (str ?ensg_location) "GRCh38/") ":")))
(bgp (triple ?enst obo:SO_transcribed_from ?ensg)
(triple ?ensg a ?parent)
(triple ?ensg obo:RO_0002162 taxon:9606)
(triple ?ensg faldo:location ?ensg_location)
(triple ?ensg dc:identifier ?child)
(triple ?ensg rdfs:label ?child_label)))
(table (vars ?chromosome)
(row (?chromosome "1"))
(row (?chromosome "2"))
(row (?chromosome "3"))
(row (?chromosome "4"))
(row (?chromosome "5"))
(row (?chromosome "6"))
(row (?chromosome "7"))
(row (?chromosome "8"))
(row (?chromosome "9"))
(row (?chromosome "10"))
(row (?chromosome "11"))
(row (?chromosome "12"))
(row (?chromosome "13"))
(row (?chromosome "14"))
(row (?chromosome "15"))
(row (?chromosome "16"))
(row (?chromosome "17"))
(row (?chromosome "18"))
(row (?chromosome "19"))
(row (?chromosome "20"))
(row (?chromosome "21"))
(row (?chromosome "22"))
(row (?chromosome "X"))
(row (?chromosome "Y"))
(row (?chromosome "MT")))))))))
(join
(filter (contains (str ?parent) "terms/ensembl/")
(extend ((?chromosome (strbefore (strafter (str ?ensg_location) "GRCh38/") ":")))
(bgp
(triple ?enst obo:SO_transcribed_from ?ensg)
(triple ?ensg a ?parent)
(triple ?ensg obo:RO_0002162 taxon:9606)
(triple ?ensg faldo:location ?ensg_location)
(triple ?ensg dc:identifier ?child)
(triple ?ensg rdfs:label ?child_label))))
(table (vars ?chromosome)
(row (?chromosome "1"))
(row (?chromosome "2"))
(row (?chromosome "3"))
(row (?chromosome "4"))
(row (?chromosome "5"))
(row (?chromosome "6"))
(row (?chromosome "7"))
(row (?chromosome "8"))
(row (?chromosome "9"))
(row (?chromosome "10"))
(row (?chromosome "11"))
(row (?chromosome "12"))
(row (?chromosome "13"))
(row (?chromosome "14"))
(row (?chromosome "15"))
(row (?chromosome "16"))
(row (?chromosome "17"))
(row (?chromosome "18"))
(row (?chromosome "19"))
(row (?chromosome "20"))
(row (?chromosome "21"))
(row (?chromosome "22"))
(row (?chromosome "X"))
(row (?chromosome "Y"))
(row (?chromosome "MT"))))))))
}
end
end
19 changes: 8 additions & 11 deletions spec/suite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@
when 'sq09.rq', 'sq11.rq', 'sq12.rq', 'sq13.rq', 'sq14.rq',
'syntax-SELECTscope1.rq', 'syntax-SELECTscope3.rq'
pending("TODO SubSelect")
when 'syntax-bindings-02a.rq', 'syntax-bindings-03a.rq', 'syntax-bindings-05a.rq'
pending "TODO top-level values"
end
t.logger = RDF::Spec.logger
t.logger.debug "Source:\n#{t.action.query_string}"
Expand All @@ -166,21 +164,20 @@
when 'ut:UpdateEvaluationTest', 'mf:UpdateEvaluationTest', 'mf:PositiveUpdateSyntaxTest11'
it "Round Trips #{t.entry} - #{t.name}: #{t.comment}" do
case t.entry
when 'syntax-update-38.ru'
pending "empty query"
when 'large-request-01.ru'
skip "large request"
when 'syntax-update-26.ru', 'syntax-update-27.ru', 'syntax-update-28.ru',
'syntax-update-36.ru'
pending("Whitespace in string tokens")
when 'insert-05a.ru', 'insert-data-same-bnode.ru',
'insert-where-same-bnode.ru', 'insert-where-same-bnode2.ru',
'delete-insert-04.ru'
pending("SubSelect")
pending("TODO SubSelect")
when 'delete-insert-04b.ru', 'delete-insert-05b.ru', 'delete-insert-05b.ru'
pending "TODO sub-joins"
when 'syntax-update-38.ru'
pending "empty query"
when 'large-request-01.ru'
skip "large request"
end
pending("Whitespace in string tokens") if %w(
syntax-update-26.ru syntax-update-27.ru syntax-update-28.ru
syntax-update-36.ru
).include?(t.entry)
t.logger = RDF::Spec.logger
t.logger.debug "Source:\n#{t.action.query_string}\n"
sse = SPARQL.parse(t.action.query_string,
Expand Down

0 comments on commit fa38ee0

Please sign in to comment.