Hello to the AGE Community, I would like to open up a discussion on an issue that I have been working on with another PMC, John Gemignani.
Some background info: - AGE relies on implicit casting to smoothly implement PG-type booleans. AG types may be implicitly cast to a boolean type via a typecast created by AGE. - coerce_to_boolean() is a function that we use that is not easily replaceable in its function. In PG, this function checks the coercibility of types into the boolean type and throws an error in the transform phase if said types cannot be coerced to boolean types. *In Postgresql and according to the OpenCypher specification, booleans should be incomparable to any value that is not also a boolean.* But, an issue arises since AGE employs the aforementioned implicit casting to implement the agtype and logic for property constraints and the `WHERE` clause. While this has allowed us to implement `WHERE` conditions and property constraints, it does cause unexpected behavior in some particular edge cases. In the case of the `AND` and `OR` boolean operators, they are optimized to pass or fail with the minimum satisfied requirement. They are also left associative, which means that the values will be evaluated left to right. If the left value fulfills the requirement for a true or false, the executor will optimize out the rest of the evaluation. As an example, in the statement `true OR true`, only the first true would be evaluated, because that satisfies the condition for the OR operator. *Implicit casting causes agtypes to bypass crucial checks in coerce_to_boolean() that ensure correct coercibility in the transform phase, which leaves the error handling to the execution phase.* Right now, this causes non-boolean agtypes to slip through and return a boolean result in certain cases in boolean operations where the left operand is a boolean and the right operand is an agtype: `RETURN true OR <agtype>` will return true when an error is expected. `RETURN false AND <agtype>` will return false when an error is expected. Normally, in the transform phase, coerce_to_boolean() would evaluate that agtypes are not coercible, and the command would error out there. But, since implicit casting causes all agtypes to be deemed "coercible" by the PG function coerce_to_boolean(), they are evaluated as coercible and the error checking can only be done later in the execution phase. Checking the validity of datatypes in the execution phase cannot be feasibly done without performance hits. My questions for discussion are, in face of this issue, how important is it that we restrict the coercibility of booleans in agtype? Are there suggestions on how to address this issue? Any comments, questions and discussion is greatly appreciated. The GitHub Issue has been referenced here: https://github.com/apache/age/issues/1008 Related Documentation can be found here: Expression Evaluation Rules: https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-EXPRESS-EVAL Casting: https://www.postgresql.org/docs/current/sql-createcast.html Thank you for your time, Dehowe