Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 11 additions & 1 deletion java/src/main/java/ai/rapids/cudf/Cudf.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -11,6 +11,16 @@ public class Cudf {
NativeDepsLoader.loadNativeDeps();
}

/**
* Initialize the libcudf JIT runtime and program cache.
* This method may be called repeatedly. It validates runtime dependencies but does not actively
* enable JIT evaluation; {@code CompiledExpression.computeColumn} continues to use the
* process-level libcudf configuration.
*
* @throws CudfException if the JIT runtime cannot be initialized
*/
public static native void initializeJitRuntime();
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved

/**
* cuDF copies that are smaller than the threshold will use a kernel to copy, instead
* of cudaMemcpyAsync.
Expand Down
8 changes: 5 additions & 3 deletions java/src/main/java/ai/rapids/cudf/ast/AstExpression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2021, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -12,14 +12,16 @@
public abstract class AstExpression {
/**
* Enumeration for the types of AST nodes that can appear in a serialized AST.
* NOTE: This must be kept in sync with the `jni_serialized_node_type` in CompiledExpression.cpp!
* NOTE: This must be kept in sync with `jni_serialized_expression_type` in
* CompiledExpression.cpp!
*/
protected enum ExpressionType {
VALID_LITERAL(0),
NULL_LITERAL(1),
COLUMN_REFERENCE(2),
UNARY_EXPRESSION(3),
BINARY_EXPRESSION(4);
BINARY_EXPRESSION(4),
JIT_EXPRESSION(5);

private final byte nativeId;

Expand Down
16 changes: 15 additions & 1 deletion java/src/main/java/ai/rapids/cudf/ast/CompiledExpression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2021, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -77,6 +77,19 @@ public ColumnVector computeColumn(Table table) {
return new ColumnVector(computeColumn(cleaner.nativeHandle, table.getNativeView()));
}

/**
* Compute a new column by applying this expression with the libcudf JIT executor, independent
* of the process-level backend selected for {@link #computeColumn}.
*
* @param table input table for this expression
* @return new column computed from this expression applied to the input table
* @throws ai.rapids.cudf.CudfException if the expression refers to
* {@link TableReference#RIGHT}, or if JIT compilation or evaluation fails
*/
public ColumnVector computeColumnJit(Table table) {
return new ColumnVector(computeColumnJit(cleaner.nativeHandle, table.getNativeView()));
}

@Override
public synchronized void close() {
cleaner.delRef();
Expand All @@ -95,5 +108,6 @@ public long getNativeHandle() {

private static native long compile(byte[] serializedExpression);
private static native long computeColumn(long astHandle, long tableHandle);
private static native long computeColumnJit(long astHandle, long tableHandle);
private static native void destroy(long handle);
}
35 changes: 35 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ast/JitErrorPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

package ai.rapids.cudf.ast;

import java.nio.ByteBuffer;

/**
* Error handling policy for fallible JIT AST operations.
*
* NOTE: This must be kept in sync with `jni_to_jit_error_policy` in CompiledExpression.cpp!
*/
public enum JitErrorPolicy {
/** Propagate an evaluation error to the caller. */
PROPAGATE(0),
/** Produce null for a row where evaluation fails. */
NULLIFY(1);

private final byte nativeId;

JitErrorPolicy(int nativeId) {
this.nativeId = (byte) nativeId;
assert this.nativeId == nativeId;
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved
}

int getSerializedSize() {
return Byte.BYTES;
}

void serialize(ByteBuffer bb) {
bb.put(nativeId);
}
}
137 changes: 137 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ast/JitOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

package ai.rapids.cudf.ast;

import java.nio.ByteBuffer;
import java.util.Objects;

/**
* A libcudf row-IR operation. Expressions containing a JIT operation must be evaluated with
Comment thread
lamarrr marked this conversation as resolved.
Resolved
* {@link CompiledExpression#computeColumnJit}.
*/
public final class JitOperation extends AstExpression {
private final JitOperator op;
private final JitErrorPolicy errorPolicy;
private final AstExpression[] inputs;
private final Integer targetScale;

/**
* Construct an operation that propagates evaluation errors.
*
* @param op operator to apply
* @param inputs operator inputs
* @throws NullPointerException if {@code op}, {@code inputs}, or an input is null
* @throws IllegalArgumentException if the operator arity or target-scale usage is invalid
*/
public JitOperation(JitOperator op, AstExpression... inputs) {
this(op, JitErrorPolicy.PROPAGATE, null, inputs);
}

/**
* Construct an operation with an explicit error policy.
*
* @param op operator to apply
* @param errorPolicy error handling policy
* @param inputs operator inputs
* @throws NullPointerException if any argument or input is null
* @throws IllegalArgumentException if the arity, policy, or target-scale usage is invalid
*/
public JitOperation(JitOperator op, JitErrorPolicy errorPolicy, AstExpression... inputs) {
this(op, errorPolicy, null, inputs);
}

/**
* Construct a target-scale operation that propagates evaluation errors.
* The target scale is valid only for {@link JitOperator#RESCALE}.
*
* @param op operator to apply
* @param targetScale target fixed-point scale
* @param inputs operator inputs
* @throws NullPointerException if {@code op}, {@code inputs}, or an input is null
* @throws IllegalArgumentException if the operator is not {@link JitOperator#RESCALE} or its
* arity is invalid
*/
public JitOperation(JitOperator op, int targetScale, AstExpression... inputs) {
this(op, JitErrorPolicy.PROPAGATE, Integer.valueOf(targetScale), inputs);
}

private JitOperation(
JitOperator op,
JitErrorPolicy errorPolicy,
Integer targetScale,
AstExpression... inputs) {
this.op = Objects.requireNonNull(op, "op is null");
this.errorPolicy = Objects.requireNonNull(errorPolicy, "errorPolicy is null");
this.inputs = Objects.requireNonNull(inputs, "inputs is null").clone();
this.targetScale = targetScale;
if (this.inputs.length != op.getArity()) {
throw new IllegalArgumentException(
op + " requires " + op.getArity() + " inputs, found " + this.inputs.length);
}
if (!op.isFallible() && errorPolicy == JitErrorPolicy.NULLIFY) {
throw new IllegalArgumentException(op + " cannot nullify errors");
}
if (op.requiresTargetScale() != (targetScale != null)) {
throw new IllegalArgumentException(op + " target scale usage is invalid");
}
for (AstExpression input : this.inputs) {
Objects.requireNonNull(input, "input is null");
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved
}
}

@Override
int getSerializedSize() {
int size = ExpressionType.JIT_EXPRESSION.getSerializedSize() +
op.getSerializedSize() +
errorPolicy.getSerializedSize() +
Byte.BYTES +
Byte.BYTES;
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved
if (targetScale != null) {
size += Integer.BYTES;
}
for (AstExpression input : inputs) {
size += input.getSerializedSize();
}
return size;
}

@Override
void serialize(ByteBuffer bb) {
ExpressionType.JIT_EXPRESSION.serialize(bb);
op.serialize(bb);
errorPolicy.serialize(bb);
bb.put((byte) inputs.length);
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved
bb.put((byte) (targetScale == null ? 0 : 1));
Comment thread
igorpeshansky marked this conversation as resolved.
Resolved
if (targetScale != null) {
bb.putInt(targetScale);
}
for (AstExpression input : inputs) {
input.serialize(bb);
}
}

@Override
public String toString() {
StringBuilder ret = new StringBuilder(op.toString());
if (errorPolicy != JitErrorPolicy.PROPAGATE) {
ret.append("[").append(errorPolicy).append("]");
}
ret.append("(");
for (int i = 0; i < inputs.length; i++) {
if (i > 0) {
ret.append(", ");
}
ret.append(inputs[i]);
}
if (targetScale != null) {
if (inputs.length > 0) {
ret.append(", ");
}
ret.append("scale=").append(targetScale);
}
return ret.append(")").toString();
}
}
96 changes: 96 additions & 0 deletions java/src/main/java/ai/rapids/cudf/ast/JitOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

package ai.rapids.cudf.ast;

import java.nio.ByteBuffer;

/**
* The subset of libcudf row-IR operators exposed through the Java AST API. Standard comparison,
* logical, and mathematical operations remain available through {@link BinaryOperation} and
* {@link UnaryOperation}.
* Operators ending in {@code _OVERFLOW} and {@link #CHECK_PRECISION} are fallible and support
* {@link JitErrorPolicy#NULLIFY}. Other operators require the default
* {@link JitErrorPolicy#PROPAGATE} policy.
*
* NOTE: This must be kept in sync with `jni_to_jit_operator` in CompiledExpression.cpp!
*/
public enum JitOperator {
/** Return the first non-null input. */
COALESCE(0, 2, false, false),
/** Convert a nullable boolean input into an always-valid predicate. */
PREDICATE(1, 1, false, false),
ADD(2, 2, false, false),
SUB(3, 2, false, false),
MUL(4, 2, false, false),
/** Divide without reporting arithmetic errors. */
DIV(5, 2, false, false),
NEG(6, 1, false, false),
ABS(7, 1, false, false),
MOD(8, 2, false, false),
ADD_OVERFLOW(9, 2, true, false),
SUB_OVERFLOW(10, 2, true, false),
MUL_OVERFLOW(11, 2, true, false),
/** Divide and report division-by-zero and signed-overflow errors. */
DIV_OVERFLOW(12, 2, true, false),
NEG_OVERFLOW(13, 1, true, false),
ABS_OVERFLOW(14, 1, true, false),
MOD_OVERFLOW(15, 2, true, false),
/** Verify that decimal input 0 fits the INT32 precision supplied by input 1. */
CHECK_PRECISION(16, 2, true, false),
BITWISE_SHIFT_LEFT(17, 2, false, false),
BITWISE_SHIFT_RIGHT(18, 2, false, false),
CAST_TO_BOOL8(19, 1, false, false),
CAST_TO_INT8(20, 1, false, false),
CAST_TO_INT16(21, 1, false, false),
CAST_TO_INT32(22, 1, false, false),
CAST_TO_INT64(23, 1, false, false),
CAST_TO_UINT8(24, 1, false, false),
CAST_TO_UINT16(25, 1, false, false),
CAST_TO_UINT32(26, 1, false, false),
CAST_TO_UINT64(27, 1, false, false),
CAST_TO_FLOAT32(28, 1, false, false),
CAST_TO_FLOAT64(29, 1, false, false),
CAST_TO_DECIMAL32(30, 1, false, false),
CAST_TO_DECIMAL64(31, 1, false, false),
CAST_TO_DECIMAL128(32, 1, false, false),
/** Rescale a decimal input to the target scale supplied to {@link JitOperation}. */
RESCALE(33, 1, false, true),
/** Select input 0 when input 2 is true, otherwise input 1. */
IF_ELSE(34, 3, false, false);

private final byte nativeId;
private final int arity;
private final boolean fallible;
private final boolean requiresTargetScale;

JitOperator(int nativeId, int arity, boolean fallible, boolean requiresTargetScale) {
this.nativeId = (byte) nativeId;
this.arity = arity;
this.fallible = fallible;
this.requiresTargetScale = requiresTargetScale;
assert this.nativeId == nativeId;
}

int getArity() {
return arity;
}

boolean isFallible() {
return fallible;
}

boolean requiresTargetScale() {
return requiresTargetScale;
}

int getSerializedSize() {
return Byte.BYTES;
}

void serialize(ByteBuffer bb) {
bb.put(nativeId);
}
}
Comment thread
lamarrr marked this conversation as resolved.
Resolved
Loading
Loading