66use geo:: Distance ;
77use geo:: Euclidean ;
88use vortex_array:: ArrayRef ;
9- use vortex_array:: ExecutionCtx ;
109use vortex_array:: arrays:: ScalarFnArray ;
1110use vortex_array:: dtype:: DType ;
12- use vortex_array:: dtype:: Nullability ;
13- use vortex_array:: dtype:: PType ;
14- use vortex_array:: expr:: Expression ;
15- use vortex_array:: expr:: union_child_validities;
16- use vortex_array:: scalar_fn:: Arity ;
17- use vortex_array:: scalar_fn:: ChildName ;
1811use vortex_array:: scalar_fn:: EmptyOptions ;
19- use vortex_array:: scalar_fn:: ExecutionArgs ;
2012use vortex_array:: scalar_fn:: ScalarFnId ;
21- use vortex_array:: scalar_fn:: ScalarFnVTable ;
2213use vortex_array:: scalar_fn:: TypedScalarFnInstance ;
14+ use vortex_array:: scalar_fn:: unstable:: row:: InitializedElement ;
15+ use vortex_array:: scalar_fn:: unstable:: row:: RowFn ;
16+ use vortex_array:: scalar_fn:: unstable:: row:: RowVisitor ;
17+ use vortex_array:: scalar_fn:: unstable:: row:: UninitElementSink ;
2318use vortex_error:: VortexResult ;
24- use vortex_error:: vortex_ensure;
2519use vortex_session:: VortexSession ;
2620use vortex_session:: registry:: CachedId ;
2721
28- use crate :: extension:: is_native_geometry;
29- use crate :: scalar_fn:: execute:: execute_binary_geo_types;
30-
31- /// Validate the two native geometry operands accepted by `ST_Distance`.
32- fn validate_distance_operands ( dtypes : & [ DType ] ) -> VortexResult < ( ) > {
33- vortex_ensure ! (
34- dtypes. len( ) == 2 ,
35- "spatial: distance requires exactly two geometry operands, got {}" ,
36- dtypes. len( )
37- ) ;
38- for dtype in dtypes {
39- vortex_ensure ! (
40- is_native_geometry( dtype) ,
41- "spatial: distance operand {dtype} is not a native geometry type"
42- ) ;
43- }
44- Ok ( ( ) )
45- }
22+ use crate :: scalar_fn:: row:: GeometryRow ;
4623
4724/// Planar (Euclidean) `ST_Distance` (no geodesic correction) between two native geometry
4825/// operands, each a column or a constant literal.
@@ -60,66 +37,41 @@ impl SpatialDistance {
6037 }
6138}
6239
63- impl ScalarFnVTable for SpatialDistance {
40+ impl RowFn for SpatialDistance {
6441 type Options = EmptyOptions ;
6542
43+ const ARG_NAMES : & ' static [ & ' static str ] = & [ "a" , "b" ] ;
44+ const INFALLIBLE : bool = false ;
45+
6646 fn id ( & self ) -> ScalarFnId {
6747 static ID : CachedId = CachedId :: new ( "vortex.st.distance" ) ;
6848 * ID
6949 }
7050
71- fn serialize ( & self , _ : & Self :: Options ) -> VortexResult < Option < Vec < u8 > > > {
51+ fn serialize ( & self , _options : & Self :: Options ) -> VortexResult < Option < Vec < u8 > > > {
7252 Ok ( Some ( vec ! [ ] ) )
7353 }
7454
75- fn deserialize ( & self , _: & [ u8 ] , _: & VortexSession ) -> VortexResult < Self :: Options > {
76- Ok ( EmptyOptions )
77- }
78-
79- fn arity ( & self , _: & Self :: Options ) -> Arity {
80- Arity :: Exact ( 2 )
81- }
82-
83- fn child_name ( & self , _: & Self :: Options , child_idx : usize ) -> ChildName {
84- match child_idx {
85- 0 => ChildName :: from ( "a" ) ,
86- 1 => ChildName :: from ( "b" ) ,
87- _ => unreachable ! ( "distance has exactly two children" ) ,
88- }
89- }
90-
91- fn return_dtype ( & self , _: & Self :: Options , dtypes : & [ DType ] ) -> VortexResult < DType > {
92- validate_distance_operands ( dtypes) ?;
93- let nullability = Nullability :: from ( dtypes. iter ( ) . any ( DType :: is_nullable) ) ;
94- Ok ( DType :: Primitive ( PType :: F64 , nullability) )
95- }
96-
97- fn execute (
55+ fn deserialize (
9856 & self ,
99- _: & Self :: Options ,
100- args : & dyn ExecutionArgs ,
101- ctx : & mut ExecutionCtx ,
102- ) -> VortexResult < ArrayRef > {
103- let a = args. get ( 0 ) ?;
104- let b = args. get ( 1 ) ?;
105- // Distance is a value, not a verdict: no bounding-rect test can decide it.
106- execute_binary_geo_types ( & a, & b, |x, y| Euclidean . distance ( x, y) , None , ctx)
57+ _metadata : & [ u8 ] ,
58+ _session : & VortexSession ,
59+ ) -> VortexResult < Self :: Options > {
60+ Ok ( EmptyOptions )
10761 }
10862
109- fn validity (
63+ fn dispatch < V : RowVisitor < Self :: Options > > (
11064 & self ,
111- _: & Self :: Options ,
112- expression : & Expression ,
113- ) -> VortexResult < Option < Expression > > {
114- union_child_validities ( expression)
115- }
116-
117- fn is_strict ( & self , _: & Self :: Options ) -> bool {
118- true
119- }
120-
121- fn is_infallible ( & self , _: & Self :: Options ) -> bool {
122- true
65+ _options : & Self :: Options ,
66+ _args : & [ DType ] ,
67+ visitor : V ,
68+ ) -> VortexResult < V :: VisitResult > {
69+ visitor. visit_into :: < ( GeometryRow , GeometryRow ) , UninitElementSink < f64 > , _ > (
70+ |( a, b) , output| {
71+ // SAFETY: `output` is the `UninitElementSink` row supplied for this callback.
72+ unsafe { InitializedElement :: write ( output, Euclidean . distance ( a, b) ) }
73+ } ,
74+ )
12375 }
12476}
12577
@@ -196,8 +148,9 @@ mod tests {
196148 Ok ( ( ) )
197149 }
198150
199- /// Distance passes no bounding-rect rejection: a point far outside a constant polygon's
200- /// bounding rect still gets its true distance, alongside an inside point at distance zero.
151+ /// Distance is a value rather than a verdict, so no bounding-rect rejection may fire for it: a
152+ /// point far outside a constant polygon's rect still gets its true distance. Carried over from
153+ /// #9076, which added the rejection to the predicates but deliberately not to this function.
201154 #[ test]
202155 fn distance_to_constant_polygon_is_exact ( ) -> VortexResult < ( ) > {
203156 let session = vortex_array:: array_session ( ) ;
0 commit comments