💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Parse exponent notation numbers as literals in SQL queries (#1382)
Muneeb Ullah6af1557039f7
diff --git a/src/python/txtai/database/sql/base.py b/src/python/txtai/database/sql/base.pyindex ed9557ba..2dce9152 100644--- a/src/python/txtai/database/sql/base.py+++ b/src/python/txtai/database/sql/base.py@@ -7,6 +7,7 @@ from shlex import shlexfrom .error import SQLErrorfrom .expression import Expression+from .token import Tokenclass SQL:@@ -146,6 +147,13 @@ class SQL:tokens.commenters = ""tokens = list(tokens)+ # Rejoin signed exponents split on punctuation (1e-3 is tokenized as 1e, -, 3)+ x = 0+ while x < len(tokens) - 2:+ if tokens[x][-1] in "eE" and tokens[x + 1] in ("+", "-") and Token.NUMERIC.fullmatch("".join(tokens[x : x + 3])):+ tokens[x : x + 3] = ["".join(tokens[x : x + 3])]+ x += 1+# Identify sql clause token positionspositions = {}diff --git a/src/python/txtai/database/sql/token.py b/src/python/txtai/database/sql/token.pyindex 5213dcc5..0d817c56 100644--- a/src/python/txtai/database/sql/token.py+++ b/src/python/txtai/database/sql/token.py@@ -2,6 +2,8 @@Token module"""+import re+class Token:"""@@ -29,6 +31,9 @@ class Token:# Default list of boolean literalsBOOLEANS = ["true", "false"]+ # Numeric literals, including exponent notation+ NUMERIC = re.compile(r"(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?")+@staticmethoddef get(tokens, x):"""@@ -201,7 +206,7 @@ class Token:"""# Literals are wrapped in quotes, parens, wildcards, numeric or boolean.- return token and (token.startswith(("'", '"', ",", "(", ")", "*")) or token.replace(".", "", 1).isdigit() or token.lower() in Token.BOOLEANS)+ return token and (token.startswith(("'", '"', ",", "(", ")", "*")) or Token.NUMERIC.fullmatch(token) or token.lower() in Token.BOOLEANS)@staticmethoddef islogicseparator(token):diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.pyindex 3bb19f5f..0ed7a9a4 100644--- a/test/python/testdatabase/testsql.py+++ b/test/python/testdatabase/testsql.py@@ -451,6 +451,10 @@ class TestSQL(unittest.TestCase):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 a > 1e3", "json_extract(data, '$.a') > 1e3")+ self.assertSql("where", prefix + "where a > 1.5E+3 and b < 2e-3", "json_extract(data, '$.a') > 1.5E+3 and json_extract(data, '$.b') < 2e-3")+ self.assertSql("where", prefix + "where a > .5e-3 and x1e-3 > 1", "json_extract(data, '$.a') > .5e-3 and json_extract(data, '$.x1e') - 3 > 1")+ self.assertSql("select", "select 1e-3 as x from txtai", "1e-3 as x")def testWhereCompound(self):"""