Skip to content

Chg #960: Pass non-null and non-expression values only to dbTypecast() - #1197

Open
KalimeroMK wants to merge 3 commits into
yiisoft:masterfrom
KalimeroMK:pass-non-null-to-dbtypecast
Open

Chg #960: Pass non-null and non-expression values only to dbTypecast()#1197
KalimeroMK wants to merge 3 commits into
yiisoft:masterfrom
KalimeroMK:pass-non-null-to-dbtypecast

Conversation

@KalimeroMK

@KalimeroMK KalimeroMK commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Fix #960

Changes

  • Adds the Yiisoft\Db\dbTypecast() function (proposed in the issue) that returns null values as is, and delegates everything else to ColumnInterface::dbTypecast().

  • Internal call sites (AbstractDMLQueryBuilder, AbstractColumnDefinitionBuilder, DateTimeValueBuilder) now use the function.

  • Column implementations are intentionally left untouched. Two findings from the driver test matrices:

    • Expression is Stringable, so removing the ExpressionInterface arms silently stringifies expressions instead of passing them through (breaks DDL builders in db-pgsql/db-mssql that pass Expression default values directly).
    • Some driver implementations process expressions: Yiisoft\Db\Mssql\Column\BinaryColumn::dbTypecast() unwraps Param values into CONVERT(VARBINARY(MAX), ...) expressions, so a wrapper must not filter ExpressionInterface either.

    Net effect: only null is safe to pre-filter. Simplifying the implementations needs a coordinated change across the driver packages and can be done as a follow-up for 3.0.

Benchmark

2M calls per round, median of 7 rounds, PHP 8.5 with OPcache + JIT (tracing), workload: 80% scalars, 10% null, 10% expressions, IntegerColumn + StringColumn:

approach time per round
current master (direct call) ~34 ms
this PR (dbTypecast() function) ~42 ms
check inlined at the call site ~34.5 ms

The function adds ~4 ns per call on trivial casts. In the real DML path, buildValue() dominates the per-value cost, so the end-to-end impact is within noise; the same applies to AR inserts/updates where network I/O prevails. The numbers are here so the trade-off is explicit.

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.62%. Comparing base (f0b0d2b) to head (fb89316).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1197   +/-   ##
=========================================
  Coverage     98.62%   98.62%           
  Complexity     1645     1645           
=========================================
  Files           120      121    +1     
  Lines          4288     4290    +2     
=========================================
+ Hits           4229     4231    +2     
  Misses           59       59           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Drivers and custom code call ColumnInterface::dbTypecast() directly (e.g. with Expression
default values in DDL builders, or Param values). Since Expression is Stringable, removing
the ExpressionInterface arms silently stringified expressions instead of passing them through,
breaking all driver test matrices. The wrapper function and internal call sites stay; the
simplification of implementations needs a coordinated change across driver packages.
Expressions must reach ColumnInterface::dbTypecast(): some implementations process them
(for example, MSSQL's binary column unwraps Param values into CONVERT expressions).
Filtering them in the wrapper bypassed that processing and broke the MSSQL test matrix.
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.

Pass to ColumnInterface::dbTypecast() non-null and non-expression values only

1 participant