Skip to content

feat: coalesce helper#35

Merged
cungminh2710 merged 1 commit into
mainfrom
minhc/coalesce-helper
Mar 30, 2026
Merged

feat: coalesce helper#35
cungminh2710 merged 1 commit into
mainfrom
minhc/coalesce-helper

Conversation

@cungminh2710
Copy link
Copy Markdown
Contributor

What this PR for?

What did you do for validating the changes?

Anything else you want to add? (Optional)

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 30, 2026

Greptile Summary

This PR introduces a typed COALESCE helper to the ORM, replacing the need to write raw SQL strings like Raw("COALESCE(...)") with a first-class schema.Coalesce(expr1, expr2, ...) expression that is validated and compiled by the query engine.

  • Adds CoalesceExpr struct implementing the Expression interface with an .As() alias method, consistent with AggregateExpr and other expression types.
  • Adds schema.Coalesce(...Expression) constructor that panics eagerly on fewer than two arguments or any nil argument.
  • Adds a case schema.CoalesceExpr: branch in the compiler's writeExpressionInContext switch that re-validates at compile time (defensive against direct struct construction) and emits COALESCE(expr1, expr2, ...) with correct dialect-aware placeholders.
  • Updates the ORM showdown benchmark to use the new typed helper.
  • Comprehensive test coverage: happy-path unit tests, panic/arity tests, and a full SQL-output integration test for Postgres.

Confidence Score: 5/5

  • Safe to merge — the implementation is clean, consistent with existing patterns, and well-tested with no apparent logic or correctness issues.
  • All changes are additive, validation is applied both at construction time (panic) and compile time (error), tests cover happy-path, aliasing, arity panics, nil panics, and SQL output. No regressions to existing paths.
  • No files require special attention.

Important Files Changed

Filename Overview
pkg/schema/schema.go Adds CoalesceExpr struct with isExpression()/As() methods and a validated Coalesce() constructor that panics on <2 args or nil expressions; consistent with existing aggregate helpers.
pkg/rain/query_compile.go Adds CoalesceExpr case to the expression compiler switch; re-validates arity and nil elements at compile time to handle direct struct construction, then emits well-formed COALESCE(…) SQL.
pkg/rain/query_select_test.go Adds an integration test for the new Coalesce helper in a Postgres SELECT, verifying correct SQL and bound args output.
pkg/schema/schema_internal_test.go Adds unit tests for Coalesce() happy-path payload, isExpression() marker, and both panic conditions (arity and nil).
pkg/schema/schema_test.go Adds public API tests for Coalesce() expression count and aliasing via .As().
pkg/rain/query_compile_internal_test.go Adds a compile-layer test for the arity error returned when CoalesceExpr is constructed with fewer than two expressions directly.
benchmarks/ormshowdown/harness_test.go Migrates one benchmark usage from Raw("COALESCE(…)") to the new typed Coalesce() helper.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["schema.Coalesce(exprs...)"] -->|"len < 2 or nil"| B["panic (eager validation)"]
    A -->|"valid"| C["CoalesceExpr{Exprs: exprs}"]
    C -->|".As(alias)"| D["AliasExpr{Expr: CoalesceExpr, Alias: alias}"]
    C --> E["query compiler\nwriteExpressionInContext"]
    D --> E
    E -->|"len < 2 or nil element"| F["return error (compile-time guard)"]
    E -->|"valid"| G["emit COALESCE(expr1, expr2, ...)"]
    G --> H["dialect placeholder substitution\n(? for MySQL, $N for Postgres)"]
Loading

Reviews (1): Last reviewed commit: "." | Re-trigger Greptile

@cungminh2710 cungminh2710 merged commit 258e27d into main Mar 30, 2026
5 checks passed
@cungminh2710 cungminh2710 deleted the minhc/coalesce-helper branch March 30, 2026 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant