diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-mariadb/llama_index/vector_stores/mariadb/base.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-mariadb/llama_index/vector_stores/mariadb/base.py index 86dfb71bc27ba..34b5f644a7823 100644 --- a/llama-index-integrations/vector_stores/llama-index-vector-stores-mariadb/llama_index/vector_stores/mariadb/base.py +++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-mariadb/llama_index/vector_stores/mariadb/base.py @@ -371,33 +371,17 @@ def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResul text, embedding, metadata, - VEC_DISTANCE_COSINE(embedding, vec_fromtext('{query.query_embedding}')) AS distance - FROM `{self.table_name}` - ORDER BY distance - LIMIT {query.similarity_top_k} - """ + VEC_DISTANCE_COSINE(embedding, VEC_FromText('{query.query_embedding}')) AS distance + FROM `{self.table_name}`""" if query.filters: - where = self._filters_to_where_clause(query.filters) + stmt += f""" + WHERE {self._filters_to_where_clause(query.filters)}""" - # We cannot use the query above when there is a WHERE clause, - # because of a bug in MariaDB: https://jira.mariadb.org/browse/MDEV-34774. - # The following query works around it. - stmt = f""" - SELECT * FROM ( - SELECT - node_id, - text, - embedding, - metadata, - VEC_DISTANCE_COSINE(embedding, vec_fromtext('{query.query_embedding}')) AS distance - FROM `{self.table_name}` - WHERE {where} - LIMIT 1000000 - ) AS unordered - ORDER BY distance - LIMIT {query.similarity_top_k} - """ + stmt += f""" + ORDER BY distance + LIMIT {query.similarity_top_k} + """ with self._engine.connect() as connection: result = connection.execute(sqlalchemy.text(stmt))