fix(sparql): bind boolean expression results as xsd:boolean - #1113
Open
Anai-Guo wants to merge 1 commit into
Open
fix(sparql): bind boolean expression results as xsd:boolean#1113Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
bool is a subclass of int, so in _eval_extend the
`isinstance(val, (int, float))` branch catches Python bools first and the
`isinstance(val, bool)` branch below it is unreachable. BIND on any
expression that yields a bool - every relational operator, IN, EXISTS,
NOT EXISTS, REGEX, isIRI, BOUND, LANGMATCHES - therefore binds a plain
untyped literal "True"/"False" instead of an xsd:boolean "true"/"false".
That is not just a lexical-form problem. _effective_boolean() in
expressions.py only reads a literal as a boolean when its datatype is
xsd:boolean; an untyped literal falls through to `len(v) > 0`, and
"False" is five characters long. So
BIND(?a > ?b AS ?ok) . FILTER(?ok)
keeps the solution even when the comparison was false.
Move the bool branch ahead of (int, float). The branch bodies are
unchanged, and int/float/str/Term results bind exactly as before.
Contributor License AgreementThank you for your contribution! Before we can accept it, the following contributor(s) must sign our CLA: Please read the appropriate agreement:
Once you have read the appropriate agreement, post the following as a comment on this PR (copy and paste exactly): The bot will record your signature and update this PR automatically. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
boolis a subclass ofintin Python, so in_eval_extend(trustgraph-flow/trustgraph/query/sparql/algebra.py) the(int, float)branch catches Python bools first and theboolbranch written just below it is dead code:evaluate_expression()returns a Pythonboolfor a large part of the expression language — its own docstring says so ("The result value (Term, bool, number, string, or None)") — including every relational operator,IN,EXISTS/NOT EXISTS,REGEX,isIRI/isLITERAL/isBLANK,BOUNDandLANGMATCHES.So
BIND(?a > ?b AS ?ok)bindsTerm(LITERAL, value="True")with no datatype, instead ofTerm(LITERAL, value="true", datatype=xsd:boolean).Why it produces wrong results, not just a wrong lexical form
_effective_boolean()inexpressions.pyonly reads a literal as a boolean when its datatype isxsd:boolean. An untyped literal falls through to the string rule:"False"is five characters long, so its EBV isTrue. A query likekeeps every solution, including the ones where the comparison was false.
Fix
Move the
boolbranch ahead of(int, float). The branch bodies are untouched.Verification
_eval_extendwas extracted fromalgebra.pyand_effective_booleanfromexpressions.pywithast.get_source_segment(no hand-copying), the graph-walking neighbours stubbed, and theBIND(...) . FILTER(?ok)chain run before and after the patch:Truevalue='True' datatype=Nonevalue='true' datatype=xsd:booleanFalsevalue='False' datatype=Nonevalue='false' datatype=xsd:boolean1value='1' datatype=Nonevalue='1' datatype=None0value='0' datatype=Nonevalue='0' datatype=None42value='42' datatype=Nonevalue='42' datatype=None2.5value='2.5' datatype=Nonevalue='2.5' datatype=None'text'value='text' datatype=Nonevalue='text' datatype=NoneOnly
boolresults change;int,float,strandTermresults bind exactly as before.🤖 Generated with Claude Code