Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@
@Evolving
public interface ReducibleFunction<I, O> {

/**
* Generic reducer for parameterized functions (bucket, truncate, etc.).
*
* If this function is 'reducible' on another function, return the {@link Reducer}.
* <p>
* This method supports functions with any number of parameters of any type.
* <p>
* Examples:
* <ul>
* <li>bucket(4, x) and bucket(2, x):
* <br>thisParams = [4], otherParams = [2]
* <br>Extract with: thisParams.getInt(0), otherParams.getInt(0)
* </li>
* <li>truncate(x, 3) and truncate(x, 5):
* <br>thisParams = [3], otherParams = [5]
* <br>Extract with: thisParams.getInt(0), otherParams.getInt(0)
* </li>
* <li>hypothetical range_bucket(x, 0L, 100L, 4):
* <br>thisParams = [0L, 100L, 4]
* <br>Extract with: thisParams.getLong(0), thisParams.getLong(1), thisParams.getInt(2)
* </li>
* </ul>
*
* @param thisParams parameters for this function
* @param otherFunction the other parameterized function
* @param otherParams parameters for the other function
* @return a reduction function if reducible, null otherwise
* @since 5.0.0
*/
default Reducer<I, O> reducer(
ReducibleParameters thisParams,
ReducibleFunction<?, ?> otherFunction,
ReducibleParameters otherParams) {
throw new UnsupportedOperationException();
}

/**
* This method is for the bucket function.
*
Expand All @@ -78,7 +114,12 @@ public interface ReducibleFunction<I, O> {
* @param otherBucketFunction the other parameterized function
* @param otherNumBuckets parameter for the other function
* @return a reduction function if it is reducible, null if not
* @deprecated as of 5.0.0. Please override
* {@link #reducer(ReducibleParameters, ReducibleFunction, ReducibleParameters)} instead.
* The new overload supports transforms with any number of parameters of any type
* (e.g. truncate width, multi-arg range buckets), not just a single int.
*/
@Deprecated(since = "5.0.0")
default Reducer<I, O> reducer(
int thisNumBuckets,
ReducibleFunction<?, ?> otherBucketFunction,
Expand All @@ -101,6 +142,6 @@ default Reducer<I, O> reducer(
* @return a reduction function if it is reducible, null if not.
*/
default Reducer<I, O> reducer(ReducibleFunction<?, ?> otherFunction) {
throw new UnsupportedOperationException();
return reducer(ReducibleParameters.EMPTY, otherFunction, ReducibleParameters.EMPTY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.
*/
package org.apache.spark.sql.connector.catalog.functions;

import org.apache.spark.annotation.Evolving;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Container for reducible function literal parameters.
* Provides type-safe access to parameters of various types.
*
* Examples:
* <ul>
* <li>bucket(4, col) → ReducibleParameters([4])</li>
* <li>truncate(col, 3) → ReducibleParameters([3])</li>
* <li>range_bucket(col, 0L, 100L, 10) → ReducibleParameters([0L, 100L, 10])</li>
* <li>custom_transform(col, "param") → ReducibleParameters(["param"])</li>
* </ul>
*
* @since 5.0.0
*/
@Evolving
public class ReducibleParameters {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structural alternative — reuse V2Literal instead of introducing ReducibleParameters. Surfacing this even though it's late in the cycle, because it's the kind of design choice worth weighing before locking in a new public class.

The proposal: drop ReducibleParameters entirely and have the generalized reducer take org.apache.spark.sql.connector.expressions.Literal (the V2 literal type already used everywhere else in the connector API):

default Reducer<I, O> reducer(
    org.apache.spark.sql.connector.expressions.Literal<?>[] thisParams,
    ReducibleFunction<?, ?> otherFunction,
    org.apache.spark.sql.connector.expressions.Literal<?>[] otherParams) {
  throw new UnsupportedOperationException();
}

Connector use:

@Override
public Reducer<UTF8String, UTF8String> reducer(
    Literal<?>[] thisParams,
    ReducibleFunction<?, ?> otherFunc,
    Literal<?>[] otherParams) {
  if (otherFunc != TruncateFunction) return null;
  int thisWidth = (Integer) thisParams[0].value();
  int otherWidth = (Integer) otherParams[0].value();
  ...
}

What this fixes simultaneously:

  1. Inline Removed reference to incubation in README.md. #1's gap evaporates. No typed getters to maintain — connectors call .value() and dispatch on .dataType(). No more "we forgot getBigDecimal" / "we'll need getCalendarInterval" / etc.
  2. The extractParameters partial-conversion concern (General-section bullet Removed reference to incubation in Spark user docs. #2) goes away. V2Literal carries dataType() alongside the value, so the connector interprets it correctly regardless of whether it's String, BigDecimal, byte[], CalendarInterval, etc. No Catalyst-internal types leaking, no type-by-type special-casing — extractParameters shrinks to a Catalyst-Literal → V2-Literal conversion (essentially the inverse of what V2ExpressionUtils.toCatalyst already does for V2 literals).
  3. One fewer public class to learn / maintain / stabilize. V2Literal is already public, already @Evolving → stable, already used by every connector that authors V2 transforms (Expressions.literal(N), Expressions.bucket(N, col)). Receiving them back through the reducer API is symmetric round-trip with how transforms are constructed — V2 connectors never have to cross the Catalyst boundary in this public surface.

Trade-offs being honest about:

  • Slightly more verbose at the connector use site ((Integer) params[0].value() vs params.getInt(0)). Real but small.
  • Java generics + arrays awkwardness; Literal<?>[] produces unchecked-warning ceremony. List<Literal<?>> or Literal<?>... varargs are cleaner alternatives.
  • The deprecated int-API → new-API dispatch shim still needed (single int → Literal<Integer>).

cc @sunchao @szehon-ho — would value your read on this trade-off, given your existing reviews of ReducibleParameters. Should the API rebase on V2Literal, or stay with the new class as drafted?

public static final ReducibleParameters EMPTY = new ReducibleParameters();

private final List<Object> values;

private ReducibleParameters() {
this.values = new ArrayList<>();
}

public ReducibleParameters(List<Object> values) {
this.values = values;
}

public ReducibleParameters(Object... values) {
this.values = Arrays.asList(values);
}

/**
* Get the number of parameters.
*/
public int count() {
return values.size();
}

/**
* Check if this container has parameters.
*/
public boolean isEmpty() {
return values.isEmpty();
}

/**
* Get parameter at index as Integer.
* @throws ClassCastException if parameter is not an Integer
* @throws IndexOutOfBoundsException if index is invalid
*/
public int getInt(int index) {
return (Integer) values.get(index);
}

/**
* Get parameter at index as Long.
* @throws ClassCastException if parameter is not a Long
* @throws IndexOutOfBoundsException if index is invalid
*/
public long getLong(int index) {
return (Long) values.get(index);
}

/**
* Get parameter at index as String.
* @throws ClassCastException if parameter is not a String
* @throws IndexOutOfBoundsException if index is invalid
*/
public String getString(int index) {
return (String) values.get(index);
}

/**
* Get parameter at index as Double.
* @throws ClassCastException if parameter is not a Double
* @throws IndexOutOfBoundsException if index is invalid
*/
public double getDouble(int index) {
return (Double) values.get(index);
}

/**
* Get parameter at index as Float.
* @throws ClassCastException if parameter is not a Float
* @throws IndexOutOfBoundsException if index is invalid
*/
public float getFloat(int index) {
return (Float) values.get(index);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractParameters in TransformExpression.scala:171 converts Decimal → java.math.BigDecimal on the way in, but this class exposes no getBigDecimal accessor. A connector with a decimal parameter therefore has to fall back to the untyped Object get(int index) and cast by hand — exactly the type-safety the wrapper was introduced to provide. Either add a getBigDecimal getter alongside the others, or remove the Decimal special-case in extractParameters and document the supported types explicitly. Concretely:

/**
 * Get parameter at index as BigDecimal.
 * @throws ClassCastException if parameter is not a BigDecimal
 * @throws IndexOutOfBoundsException if index is invalid
 */
public java.math.BigDecimal getBigDecimal(int index) {
    return (java.math.BigDecimal) values.get(index);
}

Same gap exists for any other type the Spark side might convert (binary, interval, etc.) — see the ## General note about driving extractParameters off DataType. Inline #6 proposes a structural alternative that would make this whole class of issue moot.


/**
* Get raw parameter value at index.
*/
public Object get(int index) {
return values.get(index);
}

/**
* Get all parameter values as a list.
*/
public List<Object> getAll() {
return values;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ReducibleParameters that = (ReducibleParameters) o;
return values.equals(that.values);
}

@Override
public int hashCode() {
return values.hashCode();
}

@Override
public String toString() {
return "ReducibleParameters(" + values + ")";
}
}
Loading