Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Keep DuckDB bind values in query order when a parameter repeats

serhiizghamae3e04137470a
diff --git a/src/python/txtai/database/duckdb.py b/src/python/txtai/database/duckdb.py
index 4c77028e..b2a51cee 100644
--- a/src/python/txtai/database/duckdb.py
+++ b/src/python/txtai/database/duckdb.py
@@ -139,20 +139,16 @@ class DuckDB(Embedded):
# Unpack query args
query, parameters = args
- # Iterate over parameters
- # - Replace named parameters with ?'s
- # - Build list of value with position indexes
+ # Match all named parameters in a single pass, so values are listed in the order they appear in the query
+ # - Match on a word boundary, bind parameters can be followed by any non-word character, i.e. `in (:x)`
params = []
- for key, value in parameters.items():
- # Match on a word boundary, bind parameters can be followed by any non-word character, i.e. `in (:x)`
- pattern = rf"\:{key}\b"
- for match in re.finditer(pattern, query):
- params.append((match.start(), value))
-
+ if parameters:
+ pattern = r"\:(" + "|".join(re.escape(key) for key in parameters) + r")\b"
+ params = [parameters[match.group(1)] for match in re.finditer(pattern, query)]
query = re.sub(pattern, "?", query)
# Repack query and parameter list
- args = (query, [value for _, value in sorted(params, key=lambda x: x[0])])
+ args = (query, params)
return args