feat: Use Velox NULLIF special form (#27615) #27615
<!-- 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
---
<details>
<summary>Tips and commands</summary>
Interacting with Sourcery
@sourcery-ai review on the pull request.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.@sourcery-ai anywhere in the pullrequest 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.@sourcery-ai summary anywhere inthe 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.
@sourcery-ai guide on the pullrequest to (re-)generate the reviewer's guide at any time.
@sourcery-ai resolve on thepull request to resolve all Sourcery comments. Useful if you've already
addressed all the comments and don't want to see them anymore.
@sourcery-ai dismiss on the pullrequest 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:
summary, the reviewer's guide, and others.
Getting Help
</details>
<!-- Generated by sourcery-ai[bot]: end review_guide -->
Hey - I've left some high level feedback:
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.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
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.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>
</details>
<sub>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
</sub>
@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 ?
Yep didn't mean to export the has_include, was just for testing 😅 Exporting without now to validate Velox has advanced
this can be reverted and the @Override should be removed.
getSpecialFormExpression can be reused.
These tests dont validate expression round-trip and can be reverted.
Thanks @peterenescu.
@pramodsatya updated according to comments, please have another look
aditi-pandit
peterenescu
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.
Differential Revision: D101267194