Parse true and false as boolean literals in SQL queries (#1316)

This commit is contained in:
Yash Raj Pandey
2026-10-02 14:28:51 -04:00
committed by GitHub
parent 55d52fda92
commit 00622f2cf1
2 changed files with 8 additions and 2 deletions
+5 -2
View File
@@ -26,6 +26,9 @@ class Token:
# Default list of sort order operators # Default list of sort order operators
SORT_ORDER = ["asc", "desc"] SORT_ORDER = ["asc", "desc"]
# Default list of boolean literals
BOOLEANS = ["true", "false"]
@staticmethod @staticmethod
def get(tokens, x): def get(tokens, x):
""" """
@@ -197,8 +200,8 @@ class Token:
True if this token is a literal, False otherwise True if this token is a literal, False otherwise
""" """
# Literals are wrapped in quotes, parens, wildcards or numeric. # Literals are wrapped in quotes, parens, wildcards, numeric or boolean.
return token and (token.startswith(("'", '"', ",", "(", ")", "*")) or token.replace(".", "", 1).isdigit()) return token and (token.startswith(("'", '"', ",", "(", ")", "*")) or token.replace(".", "", 1).isdigit() or token.lower() in Token.BOOLEANS)
@staticmethod @staticmethod
def islogicseparator(token): def islogicseparator(token):
+3
View File
@@ -128,6 +128,7 @@ class TestSQL(unittest.TestCase):
self.assertSql("where", "select * from txtai where [a b] < 1 or a > 1", "json_extract(data, '$.a b') < 1 or json_extract(data, '$.a') > 1") self.assertSql("where", "select * from txtai where [a b] < 1 or a > 1", "json_extract(data, '$.a b') < 1 or json_extract(data, '$.a') > 1")
self.assertSql("where", "select [a[0].c[0]] a from txtai where a < 1", "a < 1") self.assertSql("where", "select [a[0].c[0]] a from txtai where a < 1", "a < 1")
self.assertSql("where", "select * from txtai where [true] = 1", "json_extract(data, '$.true') = 1")
self.assertSql("groupby", "select * from txtai group by [a]", "json_extract(data, '$.a')") self.assertSql("groupby", "select * from txtai group by [a]", "json_extract(data, '$.a')")
self.assertSql("orderby", "select * from txtai where order by [a]", "json_extract(data, '$.a')") self.assertSql("orderby", "select * from txtai where order by [a]", "json_extract(data, '$.a')")
@@ -338,6 +339,8 @@ class TestSQL(unittest.TestCase):
self.assertSql("where", prefix + "WHERE a NOT LIKE 'abc'", "json_extract(data, '$.a') NOT LIKE 'abc'") self.assertSql("where", prefix + "WHERE a NOT LIKE 'abc'", "json_extract(data, '$.a') NOT LIKE 'abc'")
self.assertSql("where", prefix + "WHERE a IN (1, 2, 3, b)", "json_extract(data, '$.a') IN (1, 2, 3, json_extract(data, '$.b'))") self.assertSql("where", prefix + "WHERE a IN (1, 2, 3, b)", "json_extract(data, '$.a') IN (1, 2, 3, json_extract(data, '$.b'))")
self.assertSql("where", prefix + "WHERE a is not null", "json_extract(data, '$.a') is not null") self.assertSql("where", prefix + "WHERE a is not null", "json_extract(data, '$.a') is not null")
self.assertSql("where", prefix + "where a = true", "json_extract(data, '$.a') = true")
self.assertSql("where", prefix + "WHERE a is not FALSE", "json_extract(data, '$.a') is not FALSE")
self.assertSql("where", prefix + "WHERE score >= 0.15", "score >= 0.15") self.assertSql("where", prefix + "WHERE score >= 0.15", "score >= 0.15")
def testWhereCompound(self): def testWhereCompound(self):