💡 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
Raise SQLError on unterminated bracket, function and similar clauses
Sanjay Santhanam796c5bb0a8af
diff --git a/src/python/txtai/database/sql/expression.py b/src/python/txtai/database/sql/expression.pyindex eb4c98c4..40c61f08 100644--- a/src/python/txtai/database/sql/expression.py+++ b/src/python/txtai/database/sql/expression.py@@ -202,6 +202,31 @@ 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.@@ -224,7 +249,7 @@ class Expression:# Read until token is a end bracketwhile token and (token != "]" or brackets > 0):- x, token = next(iterator, (None, None))+ x, token = self.advance(iterator)# Increase/decrease bracket counterif token == "[":@@ -263,7 +288,7 @@ class Expression:# Read until token is a closing parenwhile token and token != ")":- x, token = next(iterator, (None, None))+ x, token = self.advance(iterator)if token and token not in ["(", ",", ")"]:# Strip quotes and accumulate tokensparams.append(token.replace("'", "").replace('"', ""))@@ -291,7 +316,7 @@ class Expression:# Consume function parameterswhile token and token != ")":- x, token = next(iterator, (None, None))+ x, token = self.advance(iterator)# Check if token is a square bracketif Token.isbracket(token):diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.pyindex 2cf8191b..cfe2ef95 100644--- a/test/python/testdatabase/testsql.py+++ b/test/python/testdatabase/testsql.py@@ -70,6 +70,32 @@ 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