maxlath / wikibase-cli

read and edit a Wikibase instance from the command line
MIT License
227 stars 24 forks source link

wd sparql TypeError (though -f csv works) #157

Closed tmtmtmtm closed 2 years ago

tmtmtmtm commented 2 years ago

given the following query.js:

module.exports = function () {
  return `SELECT DISTINCT ?item ?end ?endTime ?endTimePrecision
  WHERE { ?item p:P31 [ ps:P31 wd:Q15238777 ; pq:P642 wd:Q25039919 ] }`
}

wd sparql query.js errors with:

TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    at addAssociatedValue (/usr/local/lib/node_modules/wikibase-cli/node_modules/wikibase-sdk/lib/helpers/simplify_sparql_results.js:107:54)
    at /usr/local/lib/node_modules/wikibase-cli/node_modules/wikibase-sdk/lib/helpers/simplify_sparql_results.js:93:50
    at Array.map (<anonymous>)
    at Object.module.exports [as sparqlResults] (/usr/local/lib/node_modules/wikibase-cli/node_modules/wikibase-sdk/lib/helpers/simplify_sparql_results.js:16:18)
    at /usr/local/lib/node_modules/wikibase-cli/lib/make_sparql_query.js:69:26
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Context: {"results":{"head":{"vars":["item","end","endTime","endTimePrecision"]},"results":{"bindings":[{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q111338353"}},{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q111338362"}},{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q111338363"}},{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q111338361"}},{"item":{"type":"uri","value":"http://www.wikidata.org/entity/Q111338364"}}]}}}

Running this with -f csv works fine.

It seems that all three of ?end, ?endTime, and ?endTimePrecision need to be in the query to make it fail: removing any of the three make it work. I also don't think that it matters that these aren't otherwise referenced in the query: this is a pared-down minimal test-case from a much more elaborate version.

maxlath commented 2 years ago

wb sparql reshapes the results JSON, unless the --raw option is called, and does it by regrouping variables in JSON objects, so that ?item ?itemLabel ?itemAltLabel ?itemType end up in an object as { item: { value, aliases, type } }, based on their variable names sharing the same prefix. The case where a variable (?endTimePrecision) has 2 variables (end and endTime) to which it could be associated was not handled: should have been fixed in https://github.com/maxlath/wikibase-sdk/commit/6137621 and published in wikibase-cli v15.16.7

tmtmtmtm commented 2 years ago

Oh, that's a fun one! I was definitely struggling to figure out why three variables would be problematic. Thanks for the quick fix.