Skip to content

Comments

Field Enhancements and Select Bind Fix#1428

Open
StrangeYear wants to merge 4 commits intogo-gorm:masterfrom
StrangeYear:master
Open

Field Enhancements and Select Bind Fix#1428
StrangeYear wants to merge 4 commits intogo-gorm:masterfrom
StrangeYear:master

Conversation

@StrangeYear
Copy link

@StrangeYear StrangeYear commented Feb 19, 2026

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This pull request enhances gorm/gen field expression capabilities and fixes an issue related to bind variable index generation in parameterized SELECT expressions. It includes the following updates:

  • Add Coalesce function to field expressions.
  • Add ILike / NotILike helpers for PostgreSQL.
  • Add Filter function to support PostgreSQL FILTER (WHERE ...) syntax in aggregate expressions.
  • Fix incorrect bind index generation when using parameterized expressions inside Select(...).

New Features

  1. Coalesce

Adds support for SQL COALESCE in field.Expr:

u.Name.Coalesce("unknown")

Generates:

COALESCE("name", ?)
  1. ILike / NotILike

Adds PostgreSQL case-insensitive pattern matching:

u.Name.ILike("%john%")
u.Name.NotILike("%test%")

Generates:

"name" ILIKE ?
"name" NOT ILIKE ?
  1. Filter (PostgreSQL aggregate filter)

Adds support for conditional aggregation:

u.ID.Count().Filter(u.Age.Gt(18))

Generates:

COUNT("id") FILTER (WHERE "age" > ?)

This allows clean construction of conditional aggregate expressions without falling back to raw SQL.

Bug Fix

Previously, when multiple field.Expr values containing bind variables were passed into Select(...), each expression was built using a new gorm.Statement. This caused bind indexes to restart from $1 for every expression (PostgreSQL), producing invalid SQL such as:

SELECT $1, $1

instead of:

SELECT $1, $2

This PR fixes the issue by preserving bind index offsets across select expressions while still preventing SQL accumulation between statements.

The fix:

  • Ensures continuous and correct bind index generation
  • Does not change any public API
  • Does not affect existing users who do not use parameterized select expressions
  • Corrects previously invalid SQL behavior

This change is considered a bug fix rather than a breaking change.

User Case Description

This PR enables advanced SQL construction in gorm/gen:

  1. Conditional aggregation (PostgreSQL):
Select(
    u.ID.Count().Filter(u.Status.Eq("active")),
)
  1. Case-insensitive search:
Where(u.Name.ILike("%john%"))
  1. Null fallback handling:
Select(u.Nickname.Coalesce(u.Name))
  1. Parameterized expressions inside Select:
Select(
    field.Expr("?", 1),
    field.Expr("?", 2),
)

Now correctly generates:

SELECT $1, $2

Compatibility

  • No public API changes
  • No breaking behavior
  • Existing non-parameterized Select usage unaffected
  • Only fixes incorrect bind index behavior when parameters are used in select expressions

propel-code-bot[bot]
propel-code-bot bot previously approved these changes Feb 19, 2026
Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢

Details
Why was this auto-approved?
APPROVED: All 1 generated comments were filtered by review level settings.

Filtered comments by type:
1 Critical Error

💡 These suggestions were filtered to reduce noise, but you can still see them by adjusting your review level settings.

@propel-code-bot propel-code-bot bot dismissed their stale review February 19, 2026 08:41

Dismissing previous approval: 1 new comment(s) require attention based on Default policy

@propel-code-bot
Copy link

propel-code-bot bot commented Feb 19, 2026

These helpers gracefully downgrade to standard LIKE/NOT LIKE semantics or the base aggregate form on non-PostgreSQL backends, and the select-expression plumbing now preserves the running bind offset while preventing SQL accumulation between statements.

Affected Areas

field/clause.go
field/generics.go
field/expr.go
do.go

This summary was automatically generated by @propel-code-bot

propel-code-bot[bot]
propel-code-bot bot previously approved these changes Feb 19, 2026
Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢

Details
Why was this auto-approved?
APPROVED: No issues detected.

@propel-code-bot propel-code-bot bot dismissed their stale review February 19, 2026 12:32

Dismissing previous approval: 1 new comment(s) require attention based on Default policy

Copy link

@propel-code-bot propel-code-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it! 🚢

Details
Why was this auto-approved?
APPROVED: No issues detected.

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