Skip to main content

SemiJoinPushdownThroughInnerJoin

Struct SemiJoinPushdownThroughInnerJoin 

Source
pub struct SemiJoinPushdownThroughInnerJoin { /* private fields */ }
Expand description

Push an existing semi-join down through an inner join, so the selective side filters one join input instead of the join’s output.

§The query that motivated this

TPC-H q18’s o_orderkey IN (SELECT l_orderkey … HAVING sum(l_quantity) > 300) decorrelates to a semi-join, and DataFusion leaves it at the very top:

HashJoin [RightSemi] on (l_orderkey, o_orderkey)      300.92 s
  Filter: sum(l_quantity) > 300                        <- keeps ~570 of 150M orders
    Aggregate: groupBy=[l_orderkey]
  HashJoin [Inner] on (o_orderkey, l_orderkey)         764.03 s  <- all 600M rows
    HashJoin [Inner] on (c_custkey, o_custkey)          68.07 s

Measured at SF100 the joins are 82.9% of the query and the aggregate only 16.9%, so this is a join-ordering problem, not an aggregation one. The most selective predicate in the whole query — 570 surviving orders out of 150M — executes last, after the 764 s join has already materialised the full customer/orders/lineitem cross-section.

§The rewrite

For an inner join whose output feeds a semi- or anti-join keyed on columns from only one side:

  SemiJoin(Inner(A, B), S)  on A.k     ==>  Inner(SemiJoin(A, S) on A.k, B)
  AntiJoin(Inner(A, B), S)  on A.k     ==>  Inner(AntiJoin(A, S) on A.k, B)

§Anti joins and residual filters

Both were originally refused — anti joins as needing “their own reasoning”, and any join carrying a residual filter because it “may reference both sides”. Between them those two guards made the rule inert on TPC-H q21, whose EXISTS/NOT EXISTS produce exactly a semi and an anti join, each carrying l_suppkey <> l_suppkey. q21 was the slowest query in the SF100 sweep at 4309 s against Spark’s 391 s — the largest single loss of the 22 — with the most selective predicate in the query running above the whole four-way join.

The reasoning does carry over. For both kinds the existence test is a function of the filtered row and the probe alone, so a row of Inner(A, B) passes exactly when its A row passes. The residual is carried down and remapped at each level (see remap_residual) rather than refused, and re-attached only where every column it names resolves into the child being landed on or the probe.

§Why it is safe

  • The join below must be Inner. An outer join null-pads its non-preserved side, so a key that is null after the join was not null before it, and filtering earlier would keep different rows.
  • Every semi-join key must resolve into one side. If the keys straddle A and B, the existence test genuinely depends on the joined row and cannot be evaluated before the join. The same test is applied to the residual’s columns.
  • Row multiplicity is preserved. A semi-join emits each surviving row at most once and adds no columns, so Inner(SemiJoin(A,S), B) produces exactly the rows of Inner(A,B) whose A.k had a match — which is the definition of the original. Counts and sums downstream are unchanged.
  • The output schema is identical. Semi-joins project only their filtered side, so A ⧺ B in both forms, in the same order.

The outer semi-join is replaced rather than duplicated, so there is no fixed-point concern: after one application the top node is an inner join.

Implementations§

Source§

impl SemiJoinPushdownThroughInnerJoin

Source

pub fn forced() -> Self

The rule with its env gate bypassed, for tests and explicit opt-in.

Trait Implementations§

Source§

impl Debug for SemiJoinPushdownThroughInnerJoin

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SemiJoinPushdownThroughInnerJoin

Source§

fn default() -> SemiJoinPushdownThroughInnerJoin

Returns the “default value” for a type. Read more
Source§

impl OptimizerRule for SemiJoinPushdownThroughInnerJoin

Source§

fn name(&self) -> &str

A human readable name for this optimizer rule
Source§

fn apply_order(&self) -> Option<ApplyOrder>

How should the rule be applied by the optimizer? See comments on ApplyOrder for details. Read more
Source§

fn rewrite( &self, plan: LogicalPlan, _config: &dyn OptimizerConfig, ) -> Result<Transformed<LogicalPlan>>

Try to rewrite plan to an optimized form, returning Transformed::yes if the plan was rewritten and Transformed::no if it was not. Read more
Source§

fn supports_rewrite(&self) -> bool

👎Deprecated since 47.0.0:

This method is no longer used

Does this rule support rewriting owned plans (rather than by reference)?

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more