pwrliang commented on code in PR #722: URL: https://github.com/apache/sedona-db/pull/722#discussion_r3083812424
########## rust/sedona-spatial-join-gpu/src/physical_planner.rs: ########## @@ -0,0 +1,365 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::sync::Arc; + +use crate::join_provider::GpuSpatialJoinProvider; +use crate::options::GpuOptions; +use arrow_schema::Schema; +use datafusion::physical_plan::ExecutionPlan; +use datafusion_common::{DataFusionError, JoinSide, Result}; +use datafusion_physical_expr::PhysicalExpr; +use sedona_common::SpatialJoinOptions; +use sedona_query_planner::probe_shuffle_exec::ProbeShuffleExec; +use sedona_query_planner::spatial_join_physical_planner::{ + PlanSpatialJoinArgs, SpatialJoinPhysicalPlanner, +}; +use sedona_query_planner::spatial_predicate::{ + RelationPredicate, SpatialPredicate, SpatialRelationType, +}; +use sedona_schema::datatypes::SedonaType; +use sedona_schema::matchers::ArgMatcher; +use sedona_spatial_join::SpatialJoinExec; + +/// [SpatialJoinFactory] implementation for the default spatial join +/// +/// This struct is the entrypoint to ensuring the SedonaQueryPlanner is able +/// to instantiate the [ExecutionPlan] implemented in this crate. +#[derive(Debug)] +pub struct GpuSpatialJoinPhysicalPlanner; + +impl GpuSpatialJoinPhysicalPlanner { + /// Create a new default join factory + pub fn new() -> Self { + Self {} + } +} + +impl Default for GpuSpatialJoinPhysicalPlanner { + fn default() -> Self { + Self::new() + } +} + +impl SpatialJoinPhysicalPlanner for GpuSpatialJoinPhysicalPlanner { + fn plan_spatial_join( + &self, + args: &PlanSpatialJoinArgs<'_>, + ) -> Result<Option<Arc<dyn ExecutionPlan>>> { + let supported = is_spatial_predicate_supported_on_gpu(args.spatial_predicate); + let gpu_options = args + .options + .extensions + .get::<GpuOptions>() + .cloned() + .unwrap_or_default(); + + if !gpu_options.enable { + return Ok(None); + } + + if !supported { + if gpu_options.fallback_to_cpu { + log::warn!("Falling back to CPU spatial join as the spatial predicate is not supported on GPU"); + return Ok(None); + } else { + return Err(DataFusionError::Plan("GPU spatial join is enabled, but the spatial predicate is not supported on GPU".into())); + } + } + + let should_swap = !matches!( + args.spatial_predicate, + SpatialPredicate::KNearestNeighbors(_) + ) && args.join_type.supports_swap() + && should_swap_join_order( + args.join_options, + args.physical_left.as_ref(), + args.physical_right.as_ref(), + )?; + + // Repartition the probe side when enabled. This breaks spatial locality in sorted/skewed + // datasets, leading to more balanced workloads during out-of-core spatial join. + // We determine which pre-swap input will be the probe AFTER any potential swap, and + // repartition it here. swap_inputs() will then carry the RepartitionExec to the correct + // child position. + let (physical_left, physical_right) = if args.join_options.repartition_probe_side { + repartition_probe_side( + args.physical_left.clone(), + args.physical_right.clone(), + args.spatial_predicate, + should_swap, + )? + } else { + (args.physical_left.clone(), args.physical_right.clone()) + }; Review Comment: I have removed the this exec node -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
