Commit 2b2442c
fix(optimizer): Handle CAST wrapping an RPC function call
Summary:
The AIFunctionRewriteOptimizer rewrites meta.ai.embedding to CAST(fb_text_embedding(...) AS ARRAY<DOUBLE>) because fb_text_embedding returns ARRAY<REAL> but meta.ai.embedding is defined to return ARRAY<DOUBLE>.
When the RpcFunctionOptimizer processes this plan, it creates an RPC node for fb_text_embedding, but the CAST of __rpc_result remains in the Project node. The VerifyProjectionLocality sanity check then fails because it expects any CallExpression in a REMOTE projection (like CAST) to be an external function (Python UDF), but the CAST from the AI rewrite is just a plain type conversion, not an external function.
This causes queries with meta.ai.embedding and meta_ai_function_use_rpc_backend=true to fail with:
Expect expression CAST(__rpc_result) to be an external function
Fix: In RpcFunctionOptimizer.createRpcRewriter().rewriteCall(), handle the case where a CAST wraps an RPC function call. When the node is a CAST and its argument is an RPC function (e.g., fb_text_embedding), extract the inner RPC call and use the CAST target type (e.g., ARRAY<DOUBLE>) for the result variable instead of the inner call's type (e.g., ARRAY<REAL>). This eliminates the CAST from the plan, so the RPC node directly outputs the correct type, and VerifyProjectionLocality no longer sees a problematic CAST.
The fix follows the same pattern as the existing TRY handling in the same method - it intercepts the CAST before the general RPC function check, processes the inner call, and returns the result variable directly.
Fixes the issue where meta.ai.embedding was not usable with the RPC backend, forcing users to either use fb_text_embedding directly or disable the RPC backend and use the slower Python UDF path. Now meta.ai.embedding works with the RPC backend, enabling 20-50x speedup for embedding workloads.
== RELEASE NOTES ==
General Changes
* Fix a query failure that occurred when the result of a remote function (RPC) call was wrapped in a ``CAST`` expression. :pr:`28179`
Reviewed By: sebastianopeluso
Differential Revision: D1126210721 parent a6c71c5 commit 2b2442c
1 file changed
Lines changed: 28 additions & 0 deletions
File tree
- presto-main-base/src/main/java/com/facebook/presto/sql/planner/optimizations
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
282 | 310 | | |
283 | 311 | | |
284 | 312 | | |
| |||
0 commit comments