💡 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
Cleanup code from #1125
davidmezzettiaa0a8cfc328c
diff --git a/src/python/txtai/database/sql/__init__.py b/src/python/txtai/database/sql/__init__.pyindex 91c6b654..ba0ad0a5 100644--- a/src/python/txtai/database/sql/__init__.py+++ b/src/python/txtai/database/sql/__init__.py@@ -3,6 +3,7 @@ SQL imports"""from .aggregate import Aggregate-from .base import SQL, SQLError+from .base import SQL+from .error import SQLErrorfrom .expression import Expressionfrom .token import Tokendiff --git a/src/python/txtai/database/sql/base.py b/src/python/txtai/database/sql/base.pyindex 58d4156f..5c36f08c 100644--- a/src/python/txtai/database/sql/base.py+++ b/src/python/txtai/database/sql/base.py@@ -181,9 +181,3 @@ class SQL:clause = self.expression(clause, alias, aliases, similar)return clause---class SQLError(Exception):- """- Raised for errors generated by user SQL queries- """diff --git a/src/python/txtai/database/sql/error.py b/src/python/txtai/database/sql/error.pynew file mode 100644index 00000000..5745bea2--- /dev/null+++ b/src/python/txtai/database/sql/error.py@@ -0,0 +1,9 @@+"""+Error module+"""+++class SQLError(Exception):+ """+ Raised for errors generated by user SQL queries+ """diff --git a/src/python/txtai/database/sql/expression.py b/src/python/txtai/database/sql/expression.pyindex 40c61f08..85b3304b 100644--- a/src/python/txtai/database/sql/expression.py+++ b/src/python/txtai/database/sql/expression.py@@ -2,6 +2,7 @@Expression module"""+from .error import SQLErrorfrom .token import Token@@ -202,31 +203,6 @@ class Expression:# Build alias text expressionreturn ", ".join(expression)- def advance(self, iterator):- """- Advances the token iterator by one position. This is used when consuming the body of a bracket,- function or similar clause, all of which require a closing token. Raises a SQLError if the iterator- is exhausted before that closing token is found (i.e. an unterminated clause).-- Args:- iterator: tokens iterator-- Returns:- (position, token) for the next token-- Raises:- SQLError: if the iterator has no more tokens (unterminated bracket, function or similar clause)- """-- x, token = next(iterator, (None, None))- if x is None:- # Local import to avoid a circular import with the base module- from .base import SQLError # pylint: disable=import-outside-toplevel-- raise SQLError("Unterminated clause in SQL expression")-- return x, token-def bracket(self, iterator, tokens, x):"""Consumes a [bracket] expression.@@ -249,7 +225,7 @@ class Expression:# Read until token is a end bracketwhile token and (token != "]" or brackets > 0):- x, token = self.advance(iterator)+ x, token = self.readtoken(iterator)# Increase/decrease bracket counterif token == "[":@@ -288,7 +264,7 @@ class Expression:# Read until token is a closing parenwhile token and token != ")":- x, token = self.advance(iterator)+ x, token = self.readtoken(iterator)if token and token not in ["(", ",", ")"]:# Strip quotes and accumulate tokensparams.append(token.replace("'", "").replace('"', ""))@@ -316,7 +292,7 @@ class Expression:# Consume function parameterswhile token and token != ")":- x, token = self.advance(iterator)+ x, token = self.readtoken(iterator)# Check if token is a square bracketif Token.isbracket(token):@@ -427,3 +403,20 @@ class Expression:return tokenreturn self.resolver(token)++ def readtoken(self, iterator):+ """+ Reads the next token from the token iterator. Raises a SQLError if the iterator has no more tokens.++ Args:+ iterator: token iterator++ Returns:+ (x, token) for the next token+ """++ x, token = next(iterator, (None, None))+ if x is None:+ raise SQLError("Unterminated clause in SQL expression")++ return x, tokendiff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.pyindex cfe2ef95..9739ce1c 100644--- a/test/python/testdatabase/testsql.py+++ b/test/python/testdatabase/testsql.py@@ -70,32 +70,6 @@ class TestSQL(unittest.TestCase):with self.assertRaises(SQLError):self.db.search("select a b c from txtai where id match id")- def testUnterminated(self):- """- Test unterminated bracket, function and similar clauses raise SQLError instead of an internal error.- """-- # Unterminated bracket expressions- with self.assertRaises(SQLError):- self.db.search("select [a from txtai")-- with self.assertRaises(SQLError):- self.db.search("select avg([a) from txtai")-- with self.assertRaises(SQLError):- self.db.search("select [a[0] from txtai")-- # Unterminated function expressions- with self.assertRaises(SQLError):- self.db.search("select func(a from txtai")-- with self.assertRaises(SQLError):- self.db.search("select * from txtai where coalesce(a")-- # Unterminated similar clause- with self.assertRaises(SQLError):- self.db.search("select * from txtai where similar('abc'")-def testBracket(self):"""Test bracket expressions@@ -265,6 +239,32 @@ class TestSQL(unittest.TestCase):self.assertSql("where", prefix + "where coalesce(similar('abc'), similar('abc'))", "coalesce(__SIMILAR__0, __SIMILAR__1)")self.assertSql("similar", prefix + "where coalesce(similar('abc'), similar('abc'))", [["abc"], ["abc"]])+ def testUnterminated(self):+ """+ Test unterminated clauses+ """++ # Unterminated bracket expressions+ with self.assertRaises(SQLError):+ self.db.search("select [a from txtai")++ with self.assertRaises(SQLError):+ self.db.search("select avg([a) from txtai")++ with self.assertRaises(SQLError):+ self.db.search("select [a[0] from txtai")++ # Unterminated function expressions+ with self.assertRaises(SQLError):+ self.db.search("select func(a from txtai")++ with self.assertRaises(SQLError):+ self.db.search("select * from txtai where coalesce(a")++ # Unterminated similar clause+ with self.assertRaises(SQLError):+ self.db.search("select * from txtai where similar('abc'")+def testUpper(self):"""Test SQL statements are case insensitive.