Skip to content

feat: Add pmod (positive modulo) scalar function#27543

Open
natashasehgal wants to merge 1 commit intoprestodb:masterfrom
natashasehgal:export-D99449450
Open

feat: Add pmod (positive modulo) scalar function#27543
natashasehgal wants to merge 1 commit intoprestodb:masterfrom
natashasehgal:export-D99449450

Conversation

@natashasehgal
Copy link
Copy Markdown
Contributor

@natashasehgal natashasehgal commented Apr 8, 2026

Summary: Add pmod (positive modulo) as a native Presto scalar function, registered across all numeric types: TINYINT, SMALLINT, INTEGER, BIGINT, DOUBLE, and REAL.

Motivation
pmod is already implemented in the Velox native engine but has no corresponding function registration in the Java coordinator. Without these stubs, the coordinator cannot resolve pmod in query plans, so queries using
it fail during analysis.

Design

  • Stubs only: All 6 overloads throw UnsupportedOperationException — actual execution is handled by Velox's PModIntFunction / PModFloatFunction
  • Signatures: Strictly (T, T) → T for each type (no cross-type overloads)
  • Semantics: Returns the positive remainder of a % n. Returns NULL when n = 0.

Test Plan

  • Velox-side tests already exist in ArithmeticTest.cpp covering edge cases (division by zero, MIN_VALUE, negative inputs)
  • Java stubs are pass-through — no coordinator-side logic to test

== RELEASE NOTES ==

General Changes

  • Add pmod (positive modulo) scalar function for all numeric types (TINYINT, SMALLINT, INTEGER, BIGINT, DOUBLE, REAL). Execution is handled by the native engine; the Java coordinator registers stub declarations for
    query planning.

Differential Revision: D99449450

Summary by Sourcery

New Features:

  • Introduce the pmod scalar function that returns the positive remainder for numeric types including tinyint, smallint, integer, bigint, double, and real.

Summary: Add `pmod` function to Presto's MathFunctions, matching Velox's implementation (D99229614). `pmod(a, n)` returns the positive remainder of `a` divided by `n`, ensuring non-negative results for positive divisors. Overloads for TINYINT, SMALLINT, INTEGER, BIGINT, DOUBLE, and REAL.

Differential Revision: D99449450
@natashasehgal natashasehgal requested a review from a team as a code owner April 8, 2026 22:01
@prestodb-ci prestodb-ci added the from:Meta PR from Meta label Apr 8, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 8, 2026

Reviewer's Guide

Adds a new scalar SQL function pmod to Presto’s MathFunctions with multiple type overloads, declared in Java but delegated entirely to the native engine implementation.

Sequence diagram for query execution using pmod delegated to native engine

sequenceDiagram
    actor User
    participant PrestoClient
    participant PrestoCoordinator
    participant FunctionRegistry
    participant NativeEngine

    User->>PrestoClient: Submit SQL query
    PrestoClient->>PrestoCoordinator: Send query text
    PrestoCoordinator->>FunctionRegistry: Resolve function pmod
    FunctionRegistry-->>PrestoCoordinator: Return MathFunctions.pmod* overload metadata
    PrestoCoordinator->>NativeEngine: Plan and execute pmod with resolved types
    NativeEngine-->>PrestoCoordinator: Return pmod results
    PrestoCoordinator-->>PrestoClient: Return query results
    PrestoClient-->>User: Display results
Loading

Class diagram for new pmod scalar overloads in MathFunctions

classDiagram
    class MathFunctions {
        <<utility>>
        +long pmodTinyint(long num1, long num2)
        +long pmodSmallint(long num1, long num2)
        +long pmodInteger(long num1, long num2)
        +long pmodBigint(long num1, long num2)
        +double pmodDouble(double num1, double num2)
        +long pmodFloat(long num1, long num2)
    }

    class ScalarFunctionAnnotation {
    }

    class DescriptionAnnotation {
    }

    MathFunctions .. ScalarFunctionAnnotation : uses
    MathFunctions .. DescriptionAnnotation : uses
Loading

File-Level Changes

Change Details Files
Declare pmod scalar function overloads for integral and floating-point types, delegating execution to the native engine.
  • Introduce @ScalarFunction("pmod") methods for TINYINT, SMALLINT, INTEGER, BIGINT, DOUBLE, and REAL in MathFunctions
  • Annotate each overload with @Description("positive remainder of given quotient") and appropriate @SqlType signatures
  • Have each method throw UnsupportedOperationException with a message indicating pmod is implemented in the native engine
presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Since these Java stubs are registered as scalar functions but always throw, consider either providing a simple Java implementation or gating registration so non-native engine paths don’t unexpectedly hit UnsupportedOperationException at runtime.
  • If this pattern of delegating to the native engine is intentional, align the error message and annotations with other native-only scalar functions in MathFunctions (or a shared helper) for consistency and easier debugging.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since these Java stubs are registered as scalar functions but always throw, consider either providing a simple Java implementation or gating registration so non-native engine paths don’t unexpectedly hit `UnsupportedOperationException` at runtime.
- If this pattern of delegating to the native engine is intentional, align the error message and annotations with other native-only scalar functions in `MathFunctions` (or a shared helper) for consistency and easier debugging.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@natashasehgal natashasehgal changed the title [presto] Add pmod (positive modulo) scalar function feat: Add pmod (positive modulo) scalar function Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants