|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from datafusion import LogicalPlan, SessionContext |
| 21 | +from datafusion_ffi_example import MyLogicalExtensionCodec |
| 22 | + |
| 23 | + |
| 24 | +def _setup_session_with_codec() -> tuple[SessionContext, MyLogicalExtensionCodec]: |
| 25 | + """Build a session with the user-supplied logical extension codec |
| 26 | + installed. Tests use a FROM-less query so plan serialization does |
| 27 | + not pull in `try_encode_table_provider`, which the default codec |
| 28 | + leaves unimplemented.""" |
| 29 | + base = SessionContext() |
| 30 | + codec = MyLogicalExtensionCodec() |
| 31 | + ctx = base.with_logical_extension_codec(codec) |
| 32 | + return ctx, codec |
| 33 | + |
| 34 | + |
| 35 | +def test_ffi_logical_codec_install_and_export(): |
| 36 | + """Installing a user FFI codec replaces the session's logical |
| 37 | + codec; the capsule getter on the session re-exports it.""" |
| 38 | + ctx, _codec = _setup_session_with_codec() |
| 39 | + capsule = ctx.__datafusion_logical_extension_codec__() |
| 40 | + assert capsule is not None |
| 41 | + |
| 42 | + |
| 43 | +def test_ffi_logical_codec_consulted_on_udf_encode(): |
| 44 | + """Serializing a plan that references an FFI-imported scalar UDF |
| 45 | + routes through the installed codec's `try_encode_udf` path — |
| 46 | + PythonLogicalCodec delegates non-Python UDFs to the inner codec.""" |
| 47 | + ctx, codec = _setup_session_with_codec() |
| 48 | + df = ctx.sql("SELECT abs(-1) AS x") |
| 49 | + plan = df.logical_plan() |
| 50 | + |
| 51 | + before = codec.encode_udf_calls() |
| 52 | + _ = plan.to_bytes(ctx) |
| 53 | + after = codec.encode_udf_calls() |
| 54 | + |
| 55 | + assert after > before, ( |
| 56 | + f"Expected user FFI codec encode_udf to fire, before={before} after={after}" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_ffi_logical_codec_roundtrip(): |
| 61 | + """A plan referencing an FFI-imported UDF round-trips through the |
| 62 | + user-supplied logical codec (encode via codec, decode resolves from |
| 63 | + registry — `try_decode_udf` is only consulted when the UDF is not |
| 64 | + in the registry, which is the codec-inlined case).""" |
| 65 | + ctx, _codec = _setup_session_with_codec() |
| 66 | + df = ctx.sql("SELECT abs(-1) AS x") |
| 67 | + blob = df.logical_plan().to_bytes(ctx) |
| 68 | + |
| 69 | + restored = LogicalPlan.from_bytes(ctx, blob) |
| 70 | + assert restored is not None |
0 commit comments