cozodb / cozo

A transactional, relational-graph-vector database that uses Datalog for query. The hippocampus for AI!
https://cozodb.org
Mozilla Public License 2.0
3.44k stars 108 forks source link

Cannot include escaped doublequote character in string #223

Open skyrod-vactai opened 11 months ago

skyrod-vactai commented 11 months ago

According to the docs:

Strings can be typed in the same way as they do in JSON using double quotes "", with the same escape rules.

And from the tutorial:

The input literal representations are similar to those in JavaScript.
In particular, strings in double quotes are guaranteed to be interpreted in the same way as in JSON. 

JSON rules do allow \" and serde_json produces JSON with that sequence. But cozo doesn't accept it:

=> ?[foo] <- [["blah\"\""]] 
parser::pest

  × The query parser has encountered unexpected input / end of input at 19..19

I ran into this while trying to insert data. What I did was format the row data as JSON, and spliced that into a generic query that does a :put. If there's a better way to insert data let me know. I know you can import an entire JSON file but I want something I can call from my program (I'm using the cozo library in a rust program).

Volland commented 11 months ago
Screenshot 2023-12-31 at 19 46 41

yep \" is not working

aramallo commented 9 months ago

There is another issue with strings. I've tried avoiding the problem reported here by using raw string notation e.g. ___"I'm a raw string"___. But this will fail when a hash char (#) is included in the string.

The following example will FAIL with reason The query parser has encountered unexpected input / end of input at 58..58

?[repository_uri, oid, data, size] <- [['test', '000', ___\"John said  'I like color #298594'\"___, 30]]
:put blob {repository_uri, oid => data, size}

Just removing the # char makes it work.

I am guessing this might be because Rust uses # instead of _ for raw strings?

The combination of the issue reported by @skyrod-vactai and this makes it impossible to store arbitrary strings in CozoDB. For example storing JSON, HTML, CSS, XML, SVG and any other source code that typically contains multiple instances of double, single quotes and the hash char.

The following simpler example will also fail

?[repository_uri, oid, data, size] <- [['test', '000', ___\"#298594\"___, 8]]
:put blob {repository_uri, oid => data, size}
aramallo commented 9 months ago

I just reported this as a separate issue in #234

Volland commented 9 months ago

@aramallo @skyrod-vactai i figure out workaround - if i pass strings as params to a query it is works . so you could use variables binded to a query params - it is working well

aramallo commented 9 months ago

Do you mind posting an example here? Thanks a lot @Volland

aramallo commented 9 months ago

BTW the solution I posted works on the parser online playground (which I guess uses the latest version) but not when used inside coso, I need to check upgrading the parser lib version in cozo and doublecheck

Volland commented 8 months ago

@aramallo sure So query like ?[id , label] := nodes[id, label], label in [ '\" boom \"'] will fail I move it to a param ?[id , label] := nodes[id, label], is_in(label , $list) where {list : [ '\" boom \"']}

aramallo commented 8 months ago

Ah nice thanks @Volland !!