💡 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
Fix issue with similar and bracket SQL expressions embedded in functions, closes #382
davidmezzetti096927a1337c
diff --git a/src/python/txtai/database/sql/expression.py b/src/python/txtai/database/sql/expression.pyindex 88bd797c..647050d0 100644--- a/src/python/txtai/database/sql/expression.py+++ b/src/python/txtai/database/sql/expression.py@@ -84,7 +84,7 @@ class Expression:# Check if token is a functionelif Token.isfunction(tokens, x):# Resolve function expression- self.function(iterator, tokens, token, aliases)+ self.function(iterator, tokens, token, aliases, similar)# Check for alias expressionelif Token.isalias(tokens, x, alias):@@ -99,7 +99,7 @@ class Expression:# Check for compound expressionelif Token.iscompound(tokens, x):# Resolve compound expression- self.compound(iterator, tokens, x, aliases)+ self.compound(iterator, tokens, x, aliases, similar)# Remove replaced tokensreturn [token for token in tokens if token]@@ -277,7 +277,7 @@ class Expression:# Save parameterssimilar.append(params)- def function(self, iterator, tokens, token, aliases):+ def function(self, iterator, tokens, token, aliases, similar):"""Resolves column names within the function's parameters.@@ -286,20 +286,37 @@ class Expression:tokens: input tokenstoken: current tokenaliases: dict of generated aliases, if present these tokens should NOT be resolved+ similar: list where similar function call parameters are stored"""# Consume function parameterswhile token and token != ")":x, token = next(iterator, (None, None))- if Token.isfunction(tokens, x):++ # Check if token is a square bracket+ if Token.isbracket(token):+ # Resolve bracket expression+ self.bracket(iterator, tokens, x)++ # Check if token is a similar function+ elif Token.issimilar(tokens, x, similar):+ # Resolve similar expression+ self.similar(iterator, tokens, x, similar)++ # Check if token is a function+ elif Token.isfunction(tokens, x):# Resolve function parameters that are functions- self.function(iterator, tokens, token, aliases)+ self.function(iterator, tokens, token, aliases, similar)++ # Check for attribute expressionelif Token.isattribute(tokens, x):# Resolve attributesself.attribute(tokens, x, aliases)++ # Check for compound expressionelif Token.iscompound(tokens, x):# Resolve compound expressions- self.compound(iterator, tokens, x, aliases)+ self.compound(iterator, tokens, x, aliases, similar)def alias(self, iterator, tokens, x, aliases, index):"""@@ -339,7 +356,7 @@ class Expression:# Resolve attribute expressiontokens[x] = self.resolve(tokens[x], aliases)- def compound(self, iterator, tokens, x, aliases):+ def compound(self, iterator, tokens, x, aliases, similar):"""Resolves column names in a compound expression (left side <operator(s)> right side).@@ -348,6 +365,7 @@ class Expression:tokens: input tokensx: current token positionaliases: dict of generated aliases, if present these tokens should NOT be resolved+ similar: list where similar function call parameters are stored"""# Resolve left side (left side already had function processing applied through standard loop)@@ -363,7 +381,7 @@ class Expression:if token and Token.iscolumn(token):# Need to process functions since it hasn't went through the standard loop yetif Token.isfunction(tokens, x):- self.function(iterator, tokens, token, aliases)+ self.function(iterator, tokens, token, aliases, similar)else:tokens[x] = self.resolve(token, aliases)diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.pyindex 042b61d8..3249614a 100644--- a/test/python/testdatabase/testsql.py+++ b/test/python/testdatabase/testsql.py@@ -83,6 +83,7 @@ class TestSQL(unittest.TestCase):self.assertSql("select", "select [a[0]] from txtai", 'json_extract(data, "$.a[0]") as "a[0]"')self.assertSql("select", "select [a[0].ab] from txtai", 'json_extract(data, "$.a[0].ab") as "a[0].ab"')self.assertSql("select", "select [a[0].c[0]] from txtai", 'json_extract(data, "$.a[0].c[0]") as "a[0].c[0]"')+ self.assertSql("select", "select avg([a]) from txtai", 'avg(json_extract(data, "$.a")) as "avg([a])"')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")@@ -232,6 +233,9 @@ class TestSQL(unittest.TestCase):self.assertSql("where", prefix + "where similar('abc', 1000) and similar('def', 10)", "__SIMILAR__0 and __SIMILAR__1")self.assertSql("similar", prefix + "where similar('abc', 1000) and similar('def', 10)", [["abc", "1000"], ["def", "10"]])+ 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 testWhereBasic(self):"""Test basic where clauses