pjwerneck / pyrql Goto Github PK
View Code? Open in Web Editor NEWPython RQL Parser
License: MIT License
Python RQL Parser
License: MIT License
I would expect a parse action to produce output that could also be unparsed (see examples 1 and 4 specifically). Examples 5 and 6 expectedly fail but are included just for completeness. If 1 and 4 are indeed invalid queries, then the exception should be triggered at the parser().
from pyrql.parser import Parser
from pyrql.unparser import Unparser
rql_string_test_1 = r"eq(accountId,(123456789))&eq(yyyy,(2020))&eq(mm,(10))"
rql_string_test_2 = r"eq(accountId,123456789)&eq(yyyy,2020)&eq(mm,10)"
rql_string_test_3 = r"and(eq(accountId,123456789), eq(yyyy,2020), eq(mm,10))"
rql_string_test_4 = r"and(eq(accountId,(123456789)), eq(yyyy,(2020)), eq(mm,(10)))"
rql_string_test_5 = r"and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))"
rql_string_test_6 = r"eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))"
parser = Parser()
unparser = Unparser()
# Returns ERROR: can only concatenate str (not "int") to str
print(f"RQL test {rql_string_test_1}")
print("Parsing test 1")
try:
parsed_1 = parser.parse(rql_string_test_1)
print(parsed_1)
print("Unparsing test 1")
try:
unparsed_1 = unparser.unparse(parsed_1)
print(unparsed_1)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test eq(accountId,(123456789))&eq(yyyy,(2020))&eq(mm,(10))
# Parsing test 1
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', (123456789,)]}, {'name': 'eq', 'args': ['yyyy', (2020,)]}, {'name': 'eq', 'args': ['mm', (10,)]}]}
# Unparsing test 1
# ERROR: can only concatenate str (not "int") to str
# Returns good result
print(f"RQL test {rql_string_test_2}")
print("Parsing test 2")
try:
parsed_2 = parser.parse(rql_string_test_2)
print(parsed_2)
print("Unparsing test 2")
try:
unparsed_2 = unparser.unparse(parsed_2)
print(unparsed_2)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test eq(accountId,123456789)&eq(yyyy,2020)&eq(mm,10)
# Parsing test 2
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', 123456789]}, {'name': 'eq', 'args': ['yyyy', 2020]}, {'name': 'eq', 'args': ['mm', 10]}]}
# Unparsing test 2
# and(eq(accountId,123456789),eq(yyyy,2020),eq(mm,10))
# Returns good result
print(f"RQL test {rql_string_test_3}")
print("Parsing test 3")
try:
parsed_3 = parser.parse(rql_string_test_3)
print(parsed_3)
print("Unparsing test 3")
try:
unparsed_3 = unparser.unparse(parsed_3)
print(unparsed_3)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test and(eq(accountId,123456789), eq(yyyy,2020), eq(mm,10))
# Parsing test 3
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', 123456789]}, {'name': 'eq', 'args': ['yyyy', 2020]}, {'name': 'eq', 'args': ['mm', 10]}]}
# Unparsing test 3
# and(eq(accountId,123456789),eq(yyyy,2020),eq(mm,10))
# Returns ERROR: can only concatenate str (not "int") to str
print(f"RQL test {rql_string_test_4}")
print("Parsing test 4")
try:
parsed_4 = parser.parse(rql_string_test_4)
print(parsed_4)
print("Unparsing test 4")
try:
unparsed_4 = unparser.unparse(parsed_4)
print(unparsed_4)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test and(eq(accountId,(123456789)), eq(yyyy,(2020)), eq(mm,(10)))
# Parsing test 4
# {'name': 'and', 'args': [{'name': 'eq', 'args': ['accountId', (123456789,)]}, {'name': 'eq', 'args': ['yyyy', (2020,)]}, {'name': 'eq', 'args': ['mm', (10,)]}]}
# Unparsing test 4
# ERROR: can only concatenate str (not "int") to str
# Returns ERROR: ('and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))', 6, 'Expected ")"')
print(f"RQL test {rql_string_test_5}")
print("Parsing test 5")
try:
parsed_5 = parser.parse(rql_string_test_5)
print(parsed_5)
print("Unparsing test 5")
try:
unparsed_5 = unparser.unparse(parsed_5)
print(unparsed_5)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))
# Parsing test 5
# ERROR: ('and(eq(accountId,(123456789,)), eq(yyyy,(2020,)), eq(mm,(10,)))', 6, 'Expected ")"')
# Returns ERROR: ('eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))', 12, 'Expected ")"')
print(f"RQL test {rql_string_test_6}")
print("Parsing test 6")
try:
parsed_6 = parser.parse(rql_string_test_6)
print(parsed_6)
print("Unparsing test 6")
try:
unparsed_6 = unparser.unparse(parsed_6)
print(unparsed_6)
except Exception as e:
print(f"ERROR: {e}")
except Exception as e:
print(f"ERROR: {e}")
print()
print()
# OUTPUT
# RQL test eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))
# Parsing test 6
# ERROR: ('eq(accountId,(123456789,))&eq(yyyy,(2020,))&eq(mm,(10,))', 12, 'Expected ")"')
This query, structured properly works:
>>> parsed = pyrql.parse("in(foo,(foo,bar))&sort(+foo)&eq(userid,user)")
>>> parsed
{'name': 'and', 'args': [{'name': 'in', 'args': ['foo', ('foo', 'bar')]}, {'name': 'sort', 'args': [('+', 'foo')]}, {'name': 'eq', 'args': ['userid', 'user']}]}
However, if there is an extra paranthesis in the query such as:
"in(foo,(foo,bar))&sort(+foo))&eq(userid,user)"
Instead of throwing an error, it just ignores the rest of the query making it unsafe to use when appending query elements to sanitize user provided queries.
>>> parsed = pyrql.parse("in(foo,(foo,bar))&sort(+foo))&eq(userid,user)")
>>> parsed
{'name': 'and', 'args': [{'name': 'in', 'args': ['foo', ('foo', 'bar')]}, {'name': 'sort', 'args': [('+', 'foo')]}]}
Is there any quick/easy fix for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.