lvaudor / glitter

an R package which writes SPARQL queries
https://lvaudor.github.io/glitter
43 stars 4 forks source link

multiple .required=FALSE statements #216

Open lvaudor opened 2 months ago

lvaudor commented 2 months ago

A weird thing is happening here:

spq_init() %>%
    spq_add("?flood wdt:P31/wdt:P279* wd:Q8068") %>% 
    spq_add("?flood wdt:P625 ?coords",.required=FALSE) %>%
    spq_add("?flood wdt:P17 ?country",.required=FALSE) %>%
    spq_add("?flood wdt:P1120 ?deathtoll",.required=FALSE) %>%
    spq_add("?flood wdt:P276 ?loc",.required=FALSE) %>% 
    spq_add("?loc wdt:P17 ?country_loc",.required=FALSE) %>% 
    spq_head(100)

When adding the last triplet (?loc wdt:P17 ?country_loc") the formerly partially empty loc column is filled with (wrong) values...

lvaudor commented 2 months ago

OK the problem here seems to be that there should be nested OPTIONAL statements (just like what Maëlle did for spq_label on an optional variable)

This would work:


PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?coords ?coordsloc ?country ?deathtoll ?flood ?loc
WHERE {

?flood wdt:P31/wdt:P279* wd:Q8068.
OPTIONAL {?flood wdt:P625 ?coords.}
OPTIONAL {?flood wdt:P17 ?country.}
OPTIONAL {?flood wdt:P1120 ?deathtoll.}
OPTIONAL {?flood wdt:P276 ?loc.
         OPTIONAL{?loc wdt:P17 ?country_loc.}
         }
}