// Copyright (c) 2023-2025 ParadeDB, Inc. // // This file is part of ParadeDB - Postgres for Search and Analytics // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . use crate::api::operator::{ anyelement_text_opoid, anyelement_text_procoid, estimate_selectivity, make_search_query_input_opexpr_node, ReturnedNodePointer, }; use crate::postgres::utils::locate_bm25_index; use crate::query::SearchQueryInput; use crate::{nodecast, UNKNOWN_SELECTIVITY}; use pgrx::{pg_extern, pg_sys, AnyElement, FromDatum, Internal, PgList}; use super::fieldname_from_node; /// This is the function behind the `@@@(anyelement, text)` operator. Since we transform those to /// use `@@@(anyelement, searchqueryinput`), this function won't be called in normal circumstances, but it /// could be called if the rhs of the @@@ is some kind of volatile value. /// /// And in that case we just have to give up. #[pg_extern(immutable, parallel_safe, cost = 1000000000)] pub fn search_with_text( _element: AnyElement, query: &str, _fcinfo: pg_sys::FunctionCallInfo, ) -> bool { panic!("query is incompatible with pg_search's `@@@(key_field, TEXT)` operator: `{query}`") } #[pg_extern(immutable, parallel_safe)] pub unsafe fn text_support(arg: Internal) -> ReturnedNodePointer { text_support_request_simplify(arg).unwrap_or(ReturnedNodePointer(None)) } fn text_support_request_simplify(arg: Internal) -> Option { unsafe { let srs = nodecast!( SupportRequestSimplify, T_SupportRequestSimplify, arg.unwrap()?.cast_mut_ptr::() )?; if (*srs).root.is_null() { return None; } let mut input_args = PgList::::from_pg((*(*srs).fcall).args); let lhs = input_args.get_ptr(0)?; let rhs = input_args.get_ptr(1)?; let (query, param) = if let Some(const_) = nodecast!(Const, T_Const, rhs) { // the field name comes from the lhs of the @@@ operator let (_, query) = make_query_from_node_and_const((*srs).root, lhs, const_); (Some(query), None) } else { let (_, attname) = fieldname_from_node((*srs).root, lhs).expect("lhs is not a Var/FuncExpr"); ( None, Some(( rhs, Some(attname.expect("should be able to determine Node name")), )), ) }; Some(make_search_query_input_opexpr_node( srs, &mut input_args, lhs, query, param, anyelement_text_opoid(), anyelement_text_procoid(), )) } } #[pg_extern(immutable, parallel_safe)] pub fn text_restrict( planner_info: Internal, // , operator_oid: pg_sys::Oid, args: Internal, // , _var_relid: i32, ) -> f64 { fn inner_text( planner_info: Internal, // , args: Internal, // , ) -> Option { unsafe { let info = planner_info.unwrap()?.cast_mut_ptr::(); let args = PgList::::from_pg(args.unwrap()?.cast_mut_ptr::()); let lhs = args.get_ptr(0)?; let const_ = nodecast!(Const, T_Const, args.get_ptr(1)?)?; let (heaprelid, search_query_input) = make_query_from_node_and_const(info, lhs, const_); let indexrel = locate_bm25_index(heaprelid)?; estimate_selectivity(&indexrel, &search_query_input) } } assert!(operator_oid == anyelement_text_opoid()); let mut selectivity = inner_text(planner_info, args).unwrap_or(UNKNOWN_SELECTIVITY); if selectivity > 1.0 { selectivity = UNKNOWN_SELECTIVITY; } selectivity } unsafe fn make_query_from_node_and_const( root: *mut pg_sys::PlannerInfo, node: *mut pg_sys::Node, const_: *mut pg_sys::Const, ) -> (pg_sys::Oid, SearchQueryInput) { let (heaprelid, attname) = fieldname_from_node(root, node).expect("node is not a Var/Expr"); // the query comes from the rhs of the @@@ operator. we've already proved it's a `pg_sys::Const` node let query_string = String::from_datum((*const_).constvalue, (*const_).constisnull) .expect("query must not be NULL"); let query = match attname { // the Var represents a field name. we use that name with the Const value to // form a query for that field Some(field) => SearchQueryInput::ParseWithField { field, query_string, lenient: None, conjunction_mode: None, }, // the Var represents a table reference, and that means the Const value is to be used // as-is as a query None => SearchQueryInput::Parse { query_string, lenient: None, conjunction_mode: None, }, }; (heaprelid, query) }