← Back to Pull requests

feat: Use Velox NULLIF special form (#27615) #27615

Open
peterenescu opened this pull request with 28 lines diff across 5 files
View on GitHub
peterenescu peterenescu commented on Apr 20, 2026 20:49 UTC
Author

Summary:

Updates Prestissimo to use NULLIF special form rather than expression rewrite, following added support in D101170683. Previously, Presto rewrote NULLIF(a, b) to IF(equal(a, b), null, a) before execution in Velox, duplicating the first argument and producing wrong results for non-deterministic expressions (see: https://velox-lib.io/blog/nullif-special-form/). We want to update this usage with a NULLIF special form to avoid duplicating non-deterministic expressions, resulting in correctness issues.

NO RELEASE NOTE

Differential Revision: D101267194

Diff: 28 lines (+24 / -4) across 5 files Reviewer: aditi-pandit Review duration: 327069m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.36) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 28 lines diff across 5 files
0.36
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.88 · 5 substantive comment(s)
0.88
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
88%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.201
Real PR Conversation Timeline
10 items
sourcery-ai[bot] sourcery-ai[bot] commented on Apr 20, 2026 20:49 UTC
Bot

<!-- Generated by sourcery-ai[bot]: start review_guide -->

Reviewer's Guide

Implements full support for the Velox NULL_IF special form in both Java (planning/translation) and C++ (expression conversion and round‑trip), replacing the previous rewrite-based handling and enabling correctness for non-deterministic expressions, with associated tests updated/enabled.

Sequence diagram for NULLIF special form planning and native execution

sequenceDiagram
  actor User
  participant PrestoPlanner as PrestoPlanner
  participant SqlToRowExpressionTranslator as SqlToRowExpressionTranslator
  participant RowExpression as RowExpression_NULL_IF
  participant PrestoToVelox as VeloxExprConverter
  participant VeloxEngine as VeloxExecution
  participant VeloxToPresto as VeloxToPrestoExprConverter

  User->>PrestoPlanner: Submit query with NULLIF(a, b)
  PrestoPlanner->>SqlToRowExpressionTranslator: visitNullIfExpression(NullIfExpression)
  SqlToRowExpressionTranslator->>SqlToRowExpressionTranslator: functionAndTypeResolver.getCommonSuperType(first.type, second.type)
  alt Types differ and have common super type
    SqlToRowExpressionTranslator->>SqlToRowExpressionTranslator: CAST second to commonType
  else Types not comparable
    SqlToRowExpressionTranslator-->>User: SemanticException TYPE_MISMATCH
  end
  SqlToRowExpressionTranslator->>RowExpression: Build SpecialForm NULL_IF(first, second)

  PrestoPlanner->>PrestoToVelox: toVeloxExpr(SpecialForm NULL_IF)
  PrestoToVelox->>PrestoToVelox: VELOX_USER_CHECK_EQ(args.size, 2)
  PrestoToVelox->>PrestoToVelox: commonType = args[1].type
  PrestoToVelox->>VeloxEngine: Create NullIfTypedExpr(args[0], args[1], commonType)

  VeloxEngine->>VeloxEngine: Evaluate NullIfTypedExpr without duplicating first arg

  VeloxEngine->>VeloxToPresto: getRowExpression(NullIfTypedExpr)
  VeloxToPresto->>VeloxToPresto: Build protocol.SpecialFormExpression NULL_IF
  VeloxToPresto-->>PrestoPlanner: RowExpression NULL_IF(first, second)

Class diagram for updated NULLIF handling across Presto and Velox

classDiagram
  class SqlToRowExpressionTranslator {
    +RowExpression visitNullIfExpression(NullIfExpression node, Context context)
    -FunctionAndTypeResolver functionAndTypeResolver
  }

  class FunctionAndTypeManager {
    +boolean nullIfSpecialFormEnabled()
  }

  class FunctionAndTypeResolver {
    +Optional~Type~ getCommonSuperType(Type first, Type second)
    +FunctionHandle lookupCast(String castName, Type fromType, Type toType)
  }

  class RowExpression {
  }

  class SpecialFormExpression {
    +Form form
    +Type returnType
    +List~RowExpression~ arguments
  }

  class Form {
    <<enumeration>>
    NULL_IF
    IF
  }

  class VeloxExprConverter {
    +TypedExprPtr toVeloxExpr(RowExpression rowExpr)
  }

  class NullIfTypedExpr {
    +NullIfTypedExpr(TypedExprPtr first, TypedExprPtr second, TypePtr commonType)
    +const std::vector~TypedExprPtr~& inputs()
    +TypePtr type()
  }

  class VeloxToPrestoExprConverter {
    +RowExpressionPtr getRowExpression(velox::core::TypedExprPtr expr)
  }

  class Type {
  }

  class Optional~Type~ {
  }

  class FunctionHandle {
  }

  class TypePtr {
  }

  class TypedExprPtr {
  }

  SqlToRowExpressionTranslator --> FunctionAndTypeResolver : uses
  SqlToRowExpressionTranslator --> RowExpression : builds
  RowExpression <|-- SpecialFormExpression
  SpecialFormExpression --> Form : has
  VeloxExprConverter --> NullIfTypedExpr : creates
  NullIfTypedExpr --> TypePtr : uses
  VeloxToPrestoExprConverter --> SpecialFormExpression : creates
  FunctionAndTypeResolver --> Type : uses
  FunctionAndTypeManager --> SqlToRowExpressionTranslator : configures behavior via nullIfSpecialFormEnabled

File-Level Changes

| Change | Details | Files |
| ------ | ------- | ----- |
| Translate SQL NULLIF into a dedicated special form, including casting the second argument to a common supertype when needed. | <ul><li>In SqlToRowExpressionTranslator.visitNullIfExpression, keep the legacy IF-based rewrite only for non-orderable types without equality operator.</li><li>For comparable types, compute a common supertype for the two arguments via FunctionAndTypeResolver.getCommonSuperType.</li><li>If the second argument type does not match the common supertype, insert an explicit CAST on the second argument to that common type before forming the NULL_IF special form.</li><li>Return a NULL_IF special form RowExpression (instead of an IF expression) using the original first argument and the possibly-cast second argument.</li></ul> | presto-main-base/src/main/java/com/facebook/presto/sql/relational/SqlToRowExpressionTranslator.java |
| Enable and support the NULL_IF special form in native/Velox execution, including Presto↔Velox conversion and tests. | <ul><li>In FunctionAndTypeManager.nullIfSpecialFormEnabled, always return true so the planner can emit NULL_IF special forms even for native execution.</li><li>In PrestoToVeloxExpr, replace the previous VELOX_UNREACHABLE for NULL_IF with construction of a velox::core::NullIfTypedExpr using the two arguments and the second argument's type as the common type.</li><li>In VeloxToPrestoExprConverter.getRowExpression, add handling for velox::core::ExprKind::kNullIf by constructing a protocol::SpecialFormExpression with form NULL_IF, appropriate return type, and recursively converted inputs.</li><li>Update/enable Java native expression interpreter tests so that testNullIf now runs against the native interpreter instead of being disabled.</li><li>Add C++ RowExpression tests that parse NULL_IF special forms from JSON, convert them to Velox expressions, and assert correct NullIfTypedExpr structure and type/commonType behavior, including cases where the second argument is already of the common type or is a CAST expression to the common type.</li><li>Include velox/common/base/tests/GTestUtils.h in the C++ test file to use VELOX_ASSERT_EQ_TYPES.</li></ul> | presto-main-base/src/main/java/com/facebook/presto/metadata/FunctionAndTypeManager.java<br/>presto-native-execution/presto_cpp/main/types/PrestoToVeloxExpr.cpp<br/>presto-native-execution/presto_cpp/main/types/VeloxToPrestoExpr.cpp<br/>presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionInterpreter.java<br/>presto-native-execution/presto_cpp/main/types/tests/RowExpressionTest.cpp |

Possibly linked issues

  • #0: PR changes Prestissimo to use Velox NULLIF special form, resolving the semantic and implementation concerns raised in the issue.
  • ---

    <details>
    <summary>Tips and commands</summary>

    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

  • [Contact our support team](mailto:support@sourcery.ai) for questions or feedback.

  • Visit our documentation for detailed guides and information.

  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
  • </details>

    <!-- Generated by sourcery-ai[bot]: end review_guide -->

    sourcery-ai[bot] sourcery-ai[bot] commented on Apr 20, 2026 20:52 UTC
    Reviewer

    Hey - I've left some high level feedback:

  • In PrestoToVeloxExpr.cpp the NULL_IF construction blindly sets commonType to args[1]->type(); if a malformed or future RowExpression violates the assumption that the second argument is already coerced to the common supertype, this will silently propagate the wrong type, so it would be safer to either derive the common type on the C++ side or validate that both inputs are compatible with commonType.

  • In VeloxToPrestoExprConverter::getRowExpression for the kNullIf case you create a SpecialFormExpression without setting any source location; if other expression kinds preserve location metadata, consider filling it here as well to keep error reporting consistent.
  • <details>
    <summary>Prompt for AI Agents</summary>

    ~~~markdown
    Please address the comments from this code review:

    Overall Comments


  • In PrestoToVeloxExpr.cpp the NULL_IF construction blindly sets commonType to args[1]->type(); if a malformed or future RowExpression violates the assumption that the second argument is already coerced to the common supertype, this will silently propagate the wrong type, so it would be safer to either derive the common type on the C++ side or validate that both inputs are compatible with commonType.

  • In VeloxToPrestoExprConverter::getRowExpression for the kNullIf case you create a SpecialFormExpression without setting any source location; if other expression kinds preserve location metadata, consider filling it here as well to keep error reporting consistent.

  • ~~~

    </details>

    ***

    <details>
    <summary>Sourcery is free for open source - if you like our reviews please consider sharing them ✨</summary>

  • X

  • Mastodon

  • https://sourcery.ai" target="_blank" rel="noopener noreferrer" class="gh-md-link">LinkedIn

  • https://sourcery.ai" target="_blank" rel="noopener noreferrer" class="gh-md-link">Facebook
  • </details>

    <sub>
    Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
    </sub>

    aditi-pandit aditi-pandit commented on code on Apr 21, 2026 20:16 UTC
    Code Review
    presto-native-execution/presto_cpp/main/types/PrestoToVeloxExpr.cpp:1
    @@ -858,7 +858,14 @@ TypedExprPtr VeloxExprConverter::toVeloxExpr(
       }
     
       if (pexpr->form == protocol::Form::NULL_IF) {
    +#if __has_include("velox/expression/NullIfExpr.h")

    @peterenescu : Thanks for these code changes.

    We've never included Velox code like this before. Would be better to Advance Velox first as we are assured that it is well tested then. Why have you followed this style ?

    peterenescu peterenescu commented on code on Apr 21, 2026 22:14 UTC
    Code Review
    presto-native-execution/presto_cpp/main/types/PrestoToVeloxExpr.cpp:1
    @@ -858,7 +858,14 @@ TypedExprPtr VeloxExprConverter::toVeloxExpr(
       }
     
       if (pexpr->form == protocol::Form::NULL_IF) {
    +#if __has_include("velox/expression/NullIfExpr.h")

    Yep didn't mean to export the has_include, was just for testing 😅 Exporting without now to validate Velox has advanced

    pramodsatya pramodsatya commented on code on Apr 30, 2026 02:25 UTC
    Code Review
    presto-native-sidecar-plugin/src/test/java/com/facebook/presto/sidecar/expressions/TestNativeExpressionInterpreter.java:1
    @@ -296,10 +296,12 @@ public void testLikeOptimization() {}
         @Test(enabled = false)
         public void testInvalidLike() {}
     
    -    /// TODO: NULL_IF special form is unsupported in Presto C++.
         @Override
    -    @Test(enabled = false)
    -    public void testNullIf() {}
    +    @Test
    +    public void testNullIf()
    +    {
    +        super.testNullIf();

    this can be reverted and the @Override should be removed.

    pramodsatya pramodsatya commented on code on Apr 30, 2026 02:26 UTC
    Code Review
    presto-native-execution/presto_cpp/main/types/VeloxToPrestoExpr.cpp:1
    @@ -531,6 +532,18 @@ RowExpressionPtr VeloxToPrestoExprConverter::getRowExpression(
               expr->asUnchecked<velox::core::LambdaTypedExpr>();
           return getLambdaExpression(lambdaExpr);
         }
    +    case velox::core::ExprKind::kNullIf: {
    +      const auto* nullIfExpr =
    +          expr->asUnchecked<velox::core::NullIfTypedExpr>();
    +      protocol::SpecialFormExpression result;
    +      result._type = kSpecial;

    getSpecialFormExpression can be reused.

    pramodsatya pramodsatya commented on code on Apr 30, 2026 02:27 UTC
    Code Review
    presto-native-execution/presto_cpp/main/types/tests/RowExpressionTest.cpp:1
    @@ -1358,3 +1359,141 @@ TEST_F(RowExpressionTest, dereference) {
     
       ASSERT_EQ(fieldAccess->name(), "partkey");
     }
    +
    +TEST_F(RowExpressionTest, nullIf) {

    These tests dont validate expression round-trip and can be reverted.

    pramodsatya pramodsatya commented on Apr 30, 2026 02:27 UTC
    Reviewer

    Thanks @peterenescu.

    peterenescu peterenescu commented on May 06, 2026 20:50 UTC
    Author

    @pramodsatya updated according to comments, please have another look

    Reviewers
    Assignees
    peterenescu peterenescu
    Labels
    risk: low confidence: 88.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36% (MEDIUM) · Higher = larger blast radius
    36%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.1% · Lower = rushed or shallow review
    88.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.3% · Safe to merge
    4.3%