gluesql icon indicating copy to clipboard operation
gluesql copied to clipboard

Add Expr::Value variant for direct runtime value injection

Open panarch opened this issue 4 months ago • 4 comments

Summary

Add Expr::Value(Value) variant to enable direct runtime value injection into expressions, eliminating unnecessary conversions and simplifying the AST.

Closes #1799 Closes #1842 Closes #1853

Changes

Core: Expr::Value(Value) variant

  • Expressions can now directly hold runtime Value objects
  • Evaluator handles the new variant: Expr::Value(v) => Evaluated::Value(v.clone())
  • Added ToSql implementation for Value to support SQL serialization

Simplified Literal enum

Removed redundant variants that can now be represented as Expr::Value:

  • Literal::NullExpr::Value(Value::Null)
  • Literal::BooleanExpr::Value(Value::Bool)
  • Literal::HexStringExpr::Value(Value::Bytea) (hex decoding moved to translate time)

Remaining variants (Number, QuotedString) are kept for runtime type coercion via Evaluated::Text/Evaluated::Number.

Simplified ParamLiteral

Refactored from a 5-variant enum to a newtype struct ParamLiteral(Value), since all parameter types can now be represented directly as Value.

AST Builder improvements

  • Added value() function and From<Value> impl for ExprNode
  • bytea() now uses Expr::Value(Value::Bytea(...)) directly instead of hex encoding

Removed legacy code

  • Removed TryFrom<Value> for Expr (replaced by direct Expr::Value construction)
  • Removed ValueError::ValueToExprConversionFailure
  • Removed EvaluateError::FailedToDecodeHexString (moved to TranslateError)

Migration

For users of the AST builder API:

  • New value(v) function allows injecting any Value directly into expressions
  • bytea() no longer performs unnecessary hex encoding/decoding

Summary by CodeRabbit

  • Refactor
    • Consolidated internal representation of literal values to improve code consistency and simplify expression handling.
    • Enhanced error detection for invalid hex strings, now caught during parsing rather than execution.

✏️ Tip: You can customize this high-level summary in your review settings.

panarch avatar Dec 07 '25 14:12 panarch

[!WARNING]

Rate limit exceeded

@panarch has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 59 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between aebd742c9f6cadf12daaa4e1866959acf5ec8d79 and 7c3f7e536e924eb3836575c05e3ef7cbaba62def.

📒 Files selected for processing (10)
  • core/src/executor/evaluate.rs (17 hunks)
  • core/src/executor/evaluate/evaluated.rs (16 hunks)
  • core/src/executor/evaluate/evaluated/binary_op.rs (2 hunks)
  • core/src/executor/evaluate/evaluated/cmp.rs (2 hunks)
  • core/src/executor/evaluate/evaluated/concat.rs (4 hunks)
  • core/src/executor/evaluate/evaluated/eq.rs (2 hunks)
  • core/src/executor/evaluate/evaluated/like.rs (4 hunks)
  • core/src/executor/evaluate/evaluated/unary_op.rs (9 hunks)
  • core/src/executor/evaluate/expr.rs (5 hunks)
  • core/src/executor/evaluate/function.rs (39 hunks)

Walkthrough

Adds runtime Value support to expressions by introducing Expr::Value(Value), removing fallible Value→Expr conversions, collapsing several Literal variants, moving Value SQL serialization to a dedicated module, and updating translation, parameter handling, evaluation (using Cow-wrapped evaluated values), planning, executor call sites, and tests to use Value-backed expressions.

Changes

Cohort / File(s) Summary
Expr / Literal defs
core/src/ast/expr.rs, core/src/ast/literal.rs
Add Expr::Value(Value) and SQL rendering; remove Literal::Boolean, Literal::HexString, and Literal::Null.
Value SQL serialization
core/src/data/value.rs, core/src/data/value/to_sql.rs
Replace expr submodule with to_sql; implement ToSql for Value covering booleans, numbers, strings, bytea, inet, date/time, interval, uuid, map/list, point, and null.
Remove Value→Expr conversion
core/src/data/value/expr.rs
Deleted the TryFrom<Value> for Expr conversion module.
AST builder & helpers
core/src/ast_builder.rs, core/src/ast_builder/expr.rs
Export value helper; From<bool> now yields Expr::Value(Value::Bool); add From<Value> for ExprNode and value() constructor; bytea() now produces Value::Bytea.
Translate & params
core/src/translate/literal.rs, core/src/translate/expr.rs, core/src/translate/param.rs, core/src/translate/error.rs
translate_literal() now returns Expr (mapping SQL literals to Expr::Value or Expr::Literal); ParamLiteral now wraps Value; IntoParamLiteral returns ParamLiteral directly; hex-decode failure moved to TranslateError::FailedToDecodeHexString; several param-related error variants removed.
Evaluation core & evaluated types
core/src/executor/evaluate.rs, core/src/executor/evaluate/expr.rs, core/src/executor/evaluate/evaluated.rs, core/src/executor/evaluate/*
Evaluation accepts Expr::Value; literal() simplified; Evaluated::Value now holds Cow<'a, Value> and evaluation helpers updated to produce/consume Cow-wrapped values (binary, unary, concat, cmp, like, function flows, etc.).
Executor call sites
core/src/executor/delete.rs, cli/src/lib.rs
Build predicates and CLI conversions with Expr::Value(value) instead of fallible Expr::try_from(value)?; CLI param handling simplified to direct Value conversion.
Planning & analysis
core/src/plan/expr/deterministic.rs, core/src/plan/expr/nullability.rs, core/src/plan/expr/plan_expr.rs, core/src/plan/index.rs, core/src/plan/planner.rs
Treat Expr::Value as terminal/deterministic; include it in nullability and plan-expression mappings; minor planner terminal-case adjustments.
Glue & param flow
core/src/glue.rs, core/src/translate.rs
Remove parameter-conversion error propagation paths; collect params directly; update docs/err docs to reflect parsing/planning/execution-only errors.
Tests & suite updates
core/src/ast/ddl.rs, core/src/ast/function.rs, core/src/translate.rs, core/src/translate/query.rs, test-suite/src/data_type/bytea.rs, cli/src/lib.rs
Tests updated to expect Expr::Value(Value::...), adjust bytea hex-decode error expectations to TranslateError, remove usages of removed Literal variants, and align with Cow-wrapped Evaluated::Value.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Client
    participant Parser
    participant Translator
    participant Planner
    participant Executor
    participant Storage

    Client->>Parser: submit SQL
    Parser->>Translator: AST (SqlExpr)
    Translator->>Translator: translate_literal -> Expr::Value(Value::...)
    Translator->>Planner: Plan (may contain Expr::Value)
    Planner->>Executor: Execution plan
    Executor->>Executor: evaluate Expr::Value -> Evaluated::Value(Cow)
    Executor->>Storage: read/write rows using evaluated predicates
    Storage-->>Executor: rows/results
    Executor-->>Client: final result

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Pay extra attention to:
    • core/src/data/value/to_sql.rs (escaping, JSON serialization, bytea formatting)
    • Ownership and lifetimes around Evaluated::Value(Cow<'a, Value>) across evaluation helpers
    • Remaining call sites that previously relied on TryFrom<Value> for Expr
    • Translate/param error changes (moved/removed error variants, hex-decode error placement)

Possibly related issues

  • #1799 — Adds runtime Value support to Expr; this PR implements that objective by introducing Expr::Value and migrating call sites.
  • #1852 — Related to migrating literals/params to Value and shifting type-coercion responsibilities; changes are complementary.

Possibly related PRs

  • #1845 — Closely related refactor consolidating literal/evaluated representations into Value / Expr::Value.
  • #1800 — Overlaps on parameterized translation and ParamLiteral / translate-with-params plumbing.
  • #1685 — Related evaluation changes (binary_op, between, InList/InSubquery) touched by this PR.

Suggested labels

enhancement

Suggested reviewers

  • zmrdltl
  • ever0de
  • devgony

Poem

🐰
I nudged Values into Expr with a hop and a twitch,
No more hex tangles or try_from glitch.
Cow-wrapped results snug in their burrow so deep,
Now plans and executor wake from old sleep.
Carrot-powered refactor — hop, test, and keep!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 32.93% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding an Expr::Value variant to carry runtime Value objects directly in expressions.
Linked Issues check ✅ Passed The PR successfully implements all requirements from linked issues #1799, #1842, and #1853: adds Expr::Value(Value) variant, removes Literal variants now representable as Value (Boolean, HexString, Null), moves hex decoding to translate-time, adds Value::to_sql() implementation, refactors ParamLiteral to wrap Value, updates AST builder with value() function and bytea() now uses Value::Bytea directly.
Out of Scope Changes check ✅ Passed All changes are directly related to adding Value support to Expr and simplifying Literal as specified in the linked issues; no unrelated modifications detected.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Dec 07 '25 14:12 coderabbitai[bot]

Codecov Report

:x: Patch coverage is 98.96480% with 5 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 98.14%. Comparing base (c5585d3) to head (7c3f7e5). :warning: Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
core/src/executor/evaluate/evaluated.rs 92.30% 5 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1860      +/-   ##
==========================================
- Coverage   98.16%   98.14%   -0.03%     
==========================================
  Files         319      319              
  Lines       40391    40163     -228     
==========================================
- Hits        39649    39417     -232     
- Misses        742      746       +4     
Flag Coverage Δ
rust 98.14% <98.96%> (-0.03%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Dec 07 '25 15:12 codecov[bot]

GlueSQL Coverage Report

  • Commit: 7c3f7e536e924eb3836575c05e3ef7cbaba62def
  • Timestamp: 2025-12-21T070522Z
  • Report: View report

Pull Request Test Coverage Report for Build 20406141927

Details

  • 478 of 483 (98.96%) changed or added relevant lines in 26 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 98.085%

Changes Missing Coverage Covered Lines Changed/Added Lines %
core/src/executor/evaluate/evaluated.rs 60 65 92.31%
<!-- Total: 478 483
Files with Coverage Reduction New Missed Lines %
core/src/executor/evaluate/function.rs 1 97.76%
<!-- Total: 1
Totals Coverage Status
Change from base Build 20201352857: -0.02%
Covered Lines: 39434
Relevant Lines: 40204

💛 - Coveralls

coveralls avatar Dec 07 '25 15:12 coveralls