Bug #121997 Prepared temporal parameters bypass TO_SECONDS and TO_DAYS validation

Submitted: 30 Nov 11:32 Modified: 30 Nov 14:07
Reporter: QiFan Liu Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: ALLOW_INVALID_DATES, prepare, temporal types, to_days, TO_SECONDS

[30 Nov 11:32] QiFan Liu
Description:
TO_SECONDS() and TO_DAYS() validate invalid typed temporal values when they appear directly in a statement, but fail to apply the same validation when the equivalent typed value is supplied to a prepared-statement parameter.

With ALLOW_INVALID_DATES, TO_SECONDS(CAST('2021-02-31 12:34:56' AS DATETIME)) returns NULL and produces one warning. Passing the same DATETIME through a prepared parameter returns a non-NULL result without a warning.

Observed output for the main case was:

static_is_null
1
@@session.warning_count
1
param_is_null
0
@@session.warning_count
0

The same discrepancy occurs for TO_DAYS() with a DATETIME containing a zero day. A DATE-to-DATETIME variant using '0000-00-00' also makes prepared TO_SECONDS(?) return non-NULL, while the static expression and a prepared TO_DAYS(?) control return NULL.

How to repeat:
Run with the mysql client as root.

SET @old_sql_mode = @@SESSION.sql_mode;
SET SESSION sql_mode = 'ALLOW_INVALID_DATES';

SET @invalid_datetime = CAST('2021-02-31 12:34:56' AS DATETIME);

DO 1;
SELECT TO_SECONDS(CAST('2021-02-31 12:34:56' AS DATETIME)) IS NULL
  AS static_is_null;
SHOW COUNT(*) WARNINGS;

PREPARE stmt FROM
  'SELECT TO_SECONDS(?) IS NULL AS param_is_null';
DO 1;
EXECUTE stmt USING @invalid_datetime;
SHOW COUNT(*) WARNINGS;
DEALLOCATE PREPARE stmt;

-- Returns 1, 1, 0, 0. Both IS NULL checks and warning counts
-- are expected to be 1.

-- Variant: TO_DAYS() with a zero component in a typed DATETIME.
SET @zero_component_datetime = CAST('2020-02-00 12:34:56' AS DATETIME);

DO 1;
SELECT TO_DAYS(CAST('2020-02-00 12:34:56' AS DATETIME)) IS NULL
  AS static_is_null;
SHOW COUNT(*) WARNINGS;

PREPARE stmt FROM
  'SELECT TO_DAYS(?) IS NULL AS param_is_null';
DO 1;
EXECUTE stmt USING @zero_component_datetime;
SHOW COUNT(*) WARNINGS;
DEALLOCATE PREPARE stmt;

-- Also returns 1, 1, 0, 0 instead of 1, 1, 1, 1.

-- Variant: DATE-to-DATETIME conversion for TO_SECONDS().
SET SESSION sql_mode = '';
SET @zero_date = CAST('0000-00-00' AS DATE);

SELECT TO_SECONDS(CAST('0000-00-00' AS DATE)) IS NULL
  AS static_is_null;

PREPARE stmt FROM
  'SELECT TO_SECONDS(?) IS NULL AS param_seconds_is_null,
          TO_DAYS(?) IS NULL AS param_days_is_null';
EXECUTE stmt USING @zero_date, @zero_date;
DEALLOCATE PREPARE stmt;

-- Returns static_is_null=1, param_seconds_is_null=0,
-- param_days_is_null=1. All three are expected to be 1.

SET SESSION sql_mode = @old_sql_mode;
SET @invalid_datetime = NULL;
SET @zero_component_datetime = NULL;
SET @zero_date = NULL;
SET @old_sql_mode = NULL;

Suggested fix:
Apply the consumer-supplied temporal validation flags when prepared parameters holding DATE or DATETIME values are converted for TO_SECONDS() and TO_DAYS(), including DATE-to-DATETIME and DATETIME-to-DATE conversions.
[30 Nov 14:07] Roy Lyseng
Thank you for the bug report.
Verified as described.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.049 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.