Skip to content

and, or, and zip literal simplification changes the inferred result nullability #10009

Description

@mhk197

What happened?

Literal simplification can turn a well-typed nullable expression into a non-nullable expression.

For example:

| Expression | Root dtype | Optimized expression | Original dtype | Optimized dtype |
| --- | --- | --- | --- | --- |
| `and(root(), lit(false))` | `bool?` | `false` | `bool?` | `bool` |
| `or(root(), lit(true))` | `bool?` | `true` | `bool?` | `bool` |
| `and(root(), lit(Some(true)))` | `bool` | `root()` | `bool?` | `bool` |
| `or(root(), lit(Some(false)))` | `bool` | `root()` | `bool?` | `bool` |
| `zip_expr(lit(true), lit(1i32), root())` | `i32?` | `1i32` | `i32?` | `i32` |
| `zip_expr(lit(false), root(), lit(1i32))` | `i32?` | `1i32` | `i32?` | `i32` |

This is because:

  1. The AND/OR literal rules return a fresh non-nullable bool literal or an existing child without retaining the original result nullability.
  2. The ZIP literal-mask rule returns the selected child without preserving nullability contributed by the other branch.

Steps to reproduce

use rstest::rstest;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::expr::Expression;
use vortex_array::expr::and;
use vortex_array::expr::lit;
use vortex_array::expr::or;
use vortex_array::expr::root;
use vortex_array::expr::zip_expr;
use vortex_array::scalar::Scalar;
use vortex_error::VortexResult;

#[rstest]
#[case::and_annihilator(and(root(), lit(false)), DType::Bool(Nullability::Nullable))]
#[case::or_annihilator(or(root(), lit(true)), DType::Bool(Nullability::Nullable))]
#[case::and_nullable_identity(
    and(root(), lit(Scalar::from(Some(true)))),
    DType::Bool(Nullability::NonNullable)
)]
#[case::or_nullable_identity(
    or(root(), lit(Scalar::from(Some(false)))),
    DType::Bool(Nullability::NonNullable)
)]
#[case::zip_true(
    zip_expr(lit(true), lit(1i32), root()),
    DType::Primitive(PType::I32, Nullability::Nullable)
)]
#[case::zip_false(
    zip_expr(lit(false), root(), lit(1i32)),
    DType::Primitive(PType::I32, Nullability::Nullable)
)]
fn literal_simplification_preserves_result_dtype(
    #[case] expr: Expression,
    #[case] scope: DType,
) -> VortexResult<()> {
    let original = expr.bind(&scope)?;
    let optimized = expr.optimize_recursive(&scope)?;
    let rebound = optimized.bind(&scope)?;
    eprintln!(
        "{expr} -> {optimized}: {} -> {}",
        original.dtype(),
        rebound.dtype()
    );
    assert_eq!(rebound.dtype(), original.dtype());
    Ok(())
}

Environment

  • Vortex: develop at 9e8abcafca
  • OS: macOS 26.6.1, Apple Silicon
  • Rust: 1.98.0

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA bug issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions