Skip to content

Commit 0cdb9e5

Browse files
committed
Detect temporal zero defaults from the AST
Check the zero value after the literal default is resolved, so that all numeric forms are covered, including FALSE and 0e0, not only those that a regex matched. Take the fractional seconds precision from the column data type in the AST. This also fixes reading the precision from the AST, which used the whole "(3)" node and cast it to 0, and which could match a CAST(... AS DATETIME(n)) in a default, generated column, or CHECK expression. Besides the zero values, this corrects the DATETIME_PRECISION of new TIME(n), DATETIME(n), and TIMESTAMP(n) columns. String zeros, such as '0', are normalized as well, as older driver versions stored them the same as numeric zeros. The exception is a YEAR column, where MySQL reads the strings '0' and '00' as the year 2000. Expression defaults, such as DEFAULT (0), stay unchanged.
1 parent c1f9a42 commit 0cdb9e5

2 files changed

Lines changed: 178 additions & 59 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ class WP_SQLite_Information_Schema_Builder {
312312
'utf8mb4' => 4,
313313
);
314314

315+
/**
316+
* Zero values of temporal data types, without fractional seconds.
317+
*/
318+
const TEMPORAL_ZERO_VALUE_MAP = array(
319+
'date' => '0000-00-00',
320+
'time' => '00:00:00',
321+
'datetime' => '0000-00-00 00:00:00',
322+
'timestamp' => '0000-00-00 00:00:00',
323+
'year' => '0000',
324+
);
325+
315326
/**
316327
* A prefix for information schema table names.
317328
*
@@ -425,7 +436,7 @@ public function normalize_temporal_zero_defaults(): void {
425436
$columns_table = $this->connection->quote_identifier( $this->get_table_name( false, 'columns' ) );
426437
$columns = $this->connection->query(
427438
sprintf(
428-
"SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_DEFAULT
439+
"SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATETIME_PRECISION, COLUMN_DEFAULT
429440
FROM %s
430441
WHERE DATA_TYPE IN ('date', 'time', 'datetime', 'timestamp', 'year')
431442
AND COLUMN_DEFAULT IS NOT NULL",
@@ -434,10 +445,10 @@ public function normalize_temporal_zero_defaults(): void {
434445
)->fetchAll( PDO::FETCH_ASSOC ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO
435446

436447
foreach ( $columns as $column ) {
437-
$default = $this->get_temporal_zero_default(
438-
(string) $column['COLUMN_DEFAULT'],
448+
$default = $this->get_temporal_zero_value(
439449
$column['DATA_TYPE'],
440-
$column['COLUMN_TYPE']
450+
$column['COLUMN_DEFAULT'],
451+
null === $column['DATETIME_PRECISION'] ? null : (int) $column['DATETIME_PRECISION']
441452
);
442453
if ( null === $default || $default === $column['COLUMN_DEFAULT'] ) {
443454
continue;
@@ -1561,7 +1572,7 @@ private function record_drop_check_constraint(
15611572
private function extract_column_data( string $table_name, string $column_name, WP_Parser_Node $node, int $position ): array {
15621573
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
15631574

1564-
$default = $this->get_column_default( $node, $data_type, $column_name, $column_type );
1575+
$default = $this->get_column_default( $node, $data_type, $column_name );
15651576
$nullable = $this->get_column_nullable( $node );
15661577
$key = $this->get_column_key( $node );
15671578
$extra = $this->get_column_extra( $node );
@@ -2106,10 +2117,9 @@ private function get_table_comment( WP_Parser_Node $node ): string {
21062117
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
21072118
* @param string $data_type The column data type as stored in information schema.
21082119
* @param string $column_name The column name.
2109-
* @param string $column_type The column type as stored in information schema.
21102120
* @return string|null The column default as stored in information schema.
21112121
*/
2112-
private function get_column_default( WP_Parser_Node $node, string $data_type, string $column_name, string $column_type ): ?string {
2122+
private function get_column_default( WP_Parser_Node $node, string $data_type, string $column_name ): ?string {
21132123
$default_attr = null;
21142124
foreach ( $node->get_descendant_nodes( 'columnAttribute' ) as $attr ) {
21152125
if ( $attr->has_child_token( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2141,11 +2151,27 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21412151
$literal = $signed_literal->get_first_child_node( 'literal' );
21422152
if ( null === $literal ) {
21432153
// A signed number, such as "-5", has no "literal" child node.
2144-
$default = $this->get_value( $signed_literal );
2145-
$zero_default = $this->get_temporal_zero_default( $default, $data_type, $column_type );
2146-
return $zero_default ?? $default;
2154+
$default = $this->get_value( $signed_literal );
2155+
} else {
2156+
$default = $this->get_literal_default( $literal, $data_type, $column_name );
2157+
}
2158+
2159+
/*
2160+
* MySQL stores a numeric zero default of a temporal column as a zero
2161+
* value, e.g., "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00".
2162+
*
2163+
* A string zero, such as '0', is normalized the same way, as older
2164+
* driver versions stored it the same as a number. MySQL reads it as
2165+
* a zero value of a TIME column, and rejects it for other temporal
2166+
* columns in strict SQL mode. The exception is a YEAR column, where
2167+
* MySQL reads the strings '0' and '00' as the year 2000.
2168+
*/
2169+
$is_string = null !== $literal && $literal->has_child_node( 'textLiteral' );
2170+
if ( null === $default || ( $is_string && 'year' === $data_type ) ) {
2171+
return $default;
21472172
}
2148-
return $this->get_literal_default( $literal, $data_type, $column_name, $column_type );
2173+
$datetime_precision = $this->get_column_datetime_precision( $node, $data_type );
2174+
return $this->get_temporal_zero_value( $data_type, $default, $datetime_precision ) ?? $default;
21492175
}
21502176

21512177
// DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2163,10 +2189,9 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
21632189
* @param WP_Parser_Node $literal The "literal" AST node.
21642190
* @param string $data_type The column data type as stored in information schema.
21652191
* @param string $column_name The column name.
2166-
* @param string $column_type The column type as stored in information schema.
21672192
* @return string|null The default value as stored in information schema.
21682193
*/
2169-
private function get_literal_default( WP_Parser_Node $literal, string $data_type, string $column_name, string $column_type ): ?string {
2194+
private function get_literal_default( WP_Parser_Node $literal, string $data_type, string $column_name ): ?string {
21702195
// DEFAULT NULL
21712196
if ( $literal->has_child_node( 'nullLiteral' ) ) {
21722197
return null;
@@ -2181,12 +2206,6 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
21812206

21822207
$default = $this->get_value( $literal );
21832208

2184-
// Numeric zero temporal defaults, such as "DEFAULT 0" of a DATETIME column.
2185-
$zero_default = $this->get_temporal_zero_default( $default, $data_type, $column_type );
2186-
if ( null !== $zero_default ) {
2187-
return $zero_default;
2188-
}
2189-
21902209
if ( 'bit' === $data_type ) {
21912210
/*
21922211
* @TODO: Validate and normalize defaults from their AST and the full
@@ -2200,9 +2219,9 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
22002219
}
22012220

22022221
/*
2203-
* @TODO: Other non-BIT literal defaults (apart from numeric zero temporal
2204-
* defaults handled above) are currently stored verbatim. To match MySQL,
2205-
* they should be normalized. For example:
2222+
* @TODO: Non-BIT literal defaults, except for temporal zero values, are
2223+
* currently stored verbatim. To match MySQL, they should be normalized.
2224+
* For example:
22062225
* - BINARY/VARBINARY defaults stored as 0x hex literals.
22072226
* - Hex and bit literals coerced to raw bytes for text columns and to
22082227
* their decimal value for numeric columns.
@@ -2213,42 +2232,29 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
22132232
}
22142233

22152234
/**
2216-
* Normalize a numeric zero default of a temporal column to its MySQL zero value.
2235+
* Get the zero value of a temporal column when its default is a numeric zero.
22172236
*
2218-
* MySQL stores "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00",
2219-
* and it rejects the quoted form "DEFAULT '0'" in strict SQL mode. Other
2220-
* temporal defaults are not handled here.
2237+
* For example, a "0", "-0.0", or "0e0" default of a DATETIME(3) column has
2238+
* the zero value of "0000-00-00 00:00:00.000".
22212239
*
2222-
* @param string $default_value The default value.
2223-
* @param string $data_type The column data type as stored in information schema.
2224-
* @param string $column_type The column type as stored in information schema.
2225-
* @return string|null The zero value, or null when the default is not a numeric zero
2226-
* or the column is not a temporal column.
2240+
* @param string $data_type The column data type as stored in information schema.
2241+
* @param string $default_value The column default value of a number or string literal.
2242+
* @param int|null $datetime_precision The column fractional seconds precision.
2243+
* @return string|null The zero value, or null when the column is not temporal
2244+
* or the default is not a numeric zero.
22272245
*/
2228-
private function get_temporal_zero_default( string $default_value, string $data_type, string $column_type ): ?string {
2229-
if ( ! preg_match( '/\A[+-]?(?:0+(?:\.0*)?|\.0+)\z/', $default_value ) ) {
2230-
return null;
2231-
}
2246+
private function get_temporal_zero_value( string $data_type, string $default_value, ?int $datetime_precision ): ?string {
2247+
$zero_value = self::TEMPORAL_ZERO_VALUE_MAP[ $data_type ] ?? null;
22322248

2233-
// Fractional seconds precision, e.g., "datetime(3)".
2234-
$fraction = '';
2235-
if ( preg_match( '/\((\d+)\)/', $column_type, $matches ) && (int) $matches[1] > 0 ) {
2236-
$fraction = '.' . str_repeat( '0', (int) $matches[1] );
2249+
// A signed number can be a hex literal, such as "-0x0", which is not numeric.
2250+
if ( null === $zero_value || ! is_numeric( $default_value ) || 0.0 !== (float) $default_value ) {
2251+
return null;
22372252
}
22382253

2239-
switch ( $data_type ) {
2240-
case 'date':
2241-
return '0000-00-00';
2242-
case 'year':
2243-
return '0000';
2244-
case 'time':
2245-
return '00:00:00' . $fraction;
2246-
case 'datetime':
2247-
case 'timestamp':
2248-
return '0000-00-00 00:00:00' . $fraction;
2249-
default:
2250-
return null;
2254+
if ( $datetime_precision > 0 ) {
2255+
$zero_value .= '.' . str_repeat( '0', $datetime_precision );
22512256
}
2257+
return $zero_value;
22522258
}
22532259

22542260
/**
@@ -2785,11 +2791,14 @@ private function get_column_numeric_attributes( WP_Parser_Node $node, string $da
27852791
*/
27862792
private function get_column_datetime_precision( WP_Parser_Node $node, string $data_type ): ?int {
27872793
if ( 'time' === $data_type || 'datetime' === $data_type || 'timestamp' === $data_type ) {
2788-
$precision = $node->get_first_descendant_node( 'typeDatetimePrecision' );
2794+
// Expressions in the column definition may have their own precision,
2795+
// e.g., "CAST(... AS DATETIME(6))", so only the data type is checked.
2796+
$type_node = $node->get_first_descendant_node( 'dataType' );
2797+
$precision = $type_node->get_first_descendant_node( 'typeDatetimePrecision' );
27892798
if ( null === $precision ) {
27902799
return 0;
27912800
} else {
2792-
return (int) $this->get_value( $precision );
2801+
return (int) $precision->get_first_child_token( WP_MySQL_Lexer::INT_NUMBER )->get_value();
27932802
}
27942803
}
27952804
return null;

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,6 +5660,77 @@ public function testUnionAll(): void {
56605660
);
56615661
}
56625662

5663+
public function testInformationSchemaColumnsDatetimePrecision(): void {
5664+
$this->assertQuery(
5665+
'CREATE TABLE _tmp_table (
5666+
col_date DATE,
5667+
col_time TIME,
5668+
col_time_fsp TIME(2),
5669+
col_datetime DATETIME,
5670+
col_datetime_fsp DATETIME(3),
5671+
col_timestamp TIMESTAMP NULL,
5672+
col_timestamp_fsp TIMESTAMP(6) NULL,
5673+
col_generated DATETIME AS (CAST(\'2020-01-01\' AS DATETIME(6))),
5674+
col_expression_default DATETIME DEFAULT (CAST(\'2020-01-01\' AS DATETIME(6))),
5675+
col_year YEAR,
5676+
col_int INT
5677+
)'
5678+
);
5679+
5680+
$this->assertQuery(
5681+
"SELECT column_name, datetime_precision FROM information_schema.columns WHERE table_name = '_tmp_table' ORDER BY ordinal_position"
5682+
);
5683+
$this->assertEquals(
5684+
array(
5685+
(object) array(
5686+
'COLUMN_NAME' => 'col_date',
5687+
'DATETIME_PRECISION' => null,
5688+
),
5689+
(object) array(
5690+
'COLUMN_NAME' => 'col_time',
5691+
'DATETIME_PRECISION' => '0',
5692+
),
5693+
(object) array(
5694+
'COLUMN_NAME' => 'col_time_fsp',
5695+
'DATETIME_PRECISION' => '2',
5696+
),
5697+
(object) array(
5698+
'COLUMN_NAME' => 'col_datetime',
5699+
'DATETIME_PRECISION' => '0',
5700+
),
5701+
(object) array(
5702+
'COLUMN_NAME' => 'col_datetime_fsp',
5703+
'DATETIME_PRECISION' => '3',
5704+
),
5705+
(object) array(
5706+
'COLUMN_NAME' => 'col_timestamp',
5707+
'DATETIME_PRECISION' => '0',
5708+
),
5709+
(object) array(
5710+
'COLUMN_NAME' => 'col_timestamp_fsp',
5711+
'DATETIME_PRECISION' => '6',
5712+
),
5713+
(object) array(
5714+
'COLUMN_NAME' => 'col_generated',
5715+
'DATETIME_PRECISION' => '0',
5716+
),
5717+
(object) array(
5718+
'COLUMN_NAME' => 'col_expression_default',
5719+
'DATETIME_PRECISION' => '0',
5720+
),
5721+
(object) array(
5722+
'COLUMN_NAME' => 'col_year',
5723+
'DATETIME_PRECISION' => null,
5724+
),
5725+
(object) array(
5726+
'COLUMN_NAME' => 'col_int',
5727+
'DATETIME_PRECISION' => null,
5728+
),
5729+
),
5730+
$this->last_result
5731+
);
5732+
}
5733+
56635734
public function testShowCreateTableWithTemporalZeroDefaults(): void {
56645735
$this->assertQuery(
56655736
'CREATE TABLE _tmp_table (
@@ -5672,10 +5743,21 @@ public function testShowCreateTableWithTemporalZeroDefaults(): void {
56725743
col_timestamp_fsp TIMESTAMP(6) NOT NULL DEFAULT 0,
56735744
col_time_fsp TIME(2) NOT NULL DEFAULT 0,
56745745
col_decimal_zero DATETIME NOT NULL DEFAULT 0.0,
5746+
col_fraction_zero DATETIME NOT NULL DEFAULT .0,
5747+
col_exponent_zero DATETIME NOT NULL DEFAULT 0e0,
56755748
col_negative_zero DATETIME NOT NULL DEFAULT -0,
56765749
col_positive_zero DATETIME NOT NULL DEFAULT +0,
56775750
col_padded_zero DATETIME NOT NULL DEFAULT 00,
5678-
col_int INT NOT NULL DEFAULT 0
5751+
col_false DATETIME NOT NULL DEFAULT FALSE,
5752+
col_check DATETIME NOT NULL DEFAULT 0 CHECK (col_check < CAST(\'9999-12-31\' AS DATETIME(3))),
5753+
col_string_zero DATETIME NOT NULL DEFAULT \'0\',
5754+
col_time_string_zero TIME NOT NULL DEFAULT \'00\',
5755+
col_year_false YEAR NOT NULL DEFAULT FALSE,
5756+
col_year_string_zero YEAR NOT NULL DEFAULT \'0\',
5757+
col_year_string_double_zero YEAR NOT NULL DEFAULT \'00\',
5758+
col_expression DATETIME NOT NULL DEFAULT (0),
5759+
col_int INT NOT NULL DEFAULT 0,
5760+
col_varchar VARCHAR(10) NOT NULL DEFAULT \'0\'
56795761
)'
56805762
);
56815763

@@ -5694,10 +5776,22 @@ public function testShowCreateTableWithTemporalZeroDefaults(): void {
56945776
" `col_timestamp_fsp` timestamp(6) NOT NULL DEFAULT '0000-00-00 00:00:00.000000',",
56955777
" `col_time_fsp` time(2) NOT NULL DEFAULT '00:00:00.00',",
56965778
" `col_decimal_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5779+
" `col_fraction_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5780+
" `col_exponent_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
56975781
" `col_negative_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
56985782
" `col_positive_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
56995783
" `col_padded_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5700-
" `col_int` int NOT NULL DEFAULT '0'",
5784+
" `col_false` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5785+
" `col_check` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5786+
" `col_string_zero` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',",
5787+
" `col_time_string_zero` time NOT NULL DEFAULT '00:00:00',",
5788+
" `col_year_false` year NOT NULL DEFAULT '0000',",
5789+
" `col_year_string_zero` year NOT NULL DEFAULT '0',",
5790+
" `col_year_string_double_zero` year NOT NULL DEFAULT '00',",
5791+
' `col_expression` datetime NOT NULL DEFAULT (0),',
5792+
" `col_int` int NOT NULL DEFAULT '0',",
5793+
" `col_varchar` varchar(10) NOT NULL DEFAULT '0',",
5794+
" CONSTRAINT `_tmp_table_chk_1` CHECK (col_check < CAST('9999-12-31' AS DATETIME(3)))",
57015795
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
57025796
)
57035797
),
@@ -5731,16 +5825,32 @@ public function testInsertUsesTemporalZeroDefaults(): void {
57315825
'CREATE TABLE _tmp_table (
57325826
id INT NOT NULL,
57335827
created_at TIMESTAMP NOT NULL DEFAULT 0,
5734-
updated_on DATE NOT NULL DEFAULT 0
5828+
created_at_fsp DATETIME(3) NOT NULL DEFAULT 0,
5829+
updated_on DATE NOT NULL DEFAULT FALSE
57355830
)'
57365831
);
5832+
5833+
// Strict SQL mode.
57375834
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (1)' );
5738-
$this->assertQuery( 'SELECT created_at, updated_on FROM _tmp_table' );
5835+
5836+
// Non-strict SQL mode.
5837+
$this->assertQuery( "SET sql_mode = ''" );
5838+
$this->assertQuery( 'INSERT INTO _tmp_table (id) VALUES (2)' );
5839+
5840+
$this->assertQuery( 'SELECT * FROM _tmp_table ORDER BY id' );
57395841
$this->assertEquals(
57405842
array(
57415843
(object) array(
5742-
'created_at' => '0000-00-00 00:00:00',
5743-
'updated_on' => '0000-00-00',
5844+
'id' => '1',
5845+
'created_at' => '0000-00-00 00:00:00',
5846+
'created_at_fsp' => '0000-00-00 00:00:00.000',
5847+
'updated_on' => '0000-00-00',
5848+
),
5849+
(object) array(
5850+
'id' => '2',
5851+
'created_at' => '0000-00-00 00:00:00',
5852+
'created_at_fsp' => '0000-00-00 00:00:00.000',
5853+
'updated_on' => '0000-00-00',
57445854
),
57455855
),
57465856
$this->last_result

0 commit comments

Comments
 (0)