[docs]classDatabricksVectorSearchTranslator(Visitor):"""Translate `Databricks vector search` internal query language elements to valid filters.""""""Subset of allowed logical operators."""allowed_operators=[Operator.AND,Operator.NOT,Operator.OR]"""Subset of allowed logical comparators."""allowed_comparators=[Comparator.EQ,Comparator.GT,Comparator.GTE,Comparator.LT,Comparator.LTE,Comparator.IN,Comparator.LIKE,]def_visit_and_operation(self,operation:Operation)->Dict:returndict(ChainMap(*[arg.accept(self)forarginoperation.arguments]))def_visit_or_operation(self,operation:Operation)->Dict:filter_args=[arg.accept(self)forarginoperation.arguments]flattened_args=list(chain.from_iterable(filter_arg.items()forfilter_arginfilter_args))return{" OR ".join(keyforkey,_inflattened_args):[valuefor_,valueinflattened_args]}def_visit_not_operation(self,operation:Operation)->Dict:iflen(operation.arguments)>1:raiseValueError(f'"{operation.operator.value}" can have only one argument 'f"in Databricks vector search")filter_arg=operation.arguments[0].accept(self)return{f"{colum_with_bool_expression} NOT":valueforcolum_with_bool_expression,valueinfilter_arg.items()}
[docs]defvisit_operation(self,operation:Operation)->Dict:self._validate_func(operation.operator)ifoperation.operator==Operator.AND:returnself._visit_and_operation(operation)elifoperation.operator==Operator.OR:returnself._visit_or_operation(operation)elifoperation.operator==Operator.NOT:returnself._visit_not_operation(operation)else:raiseNotImplementedError(f'Operator "{operation.operator}" is not supported')