Skip to content

Commit cf6be95

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. ALTER COLUMN ... SET DEFAULT is normalized the same way, including a parenthesized literal, such as SET DEFAULT (0), which MySQL stores as a literal. As the statement has no data type, the precision comes from the information schema.
1 parent d2262de commit cf6be95

2 files changed

Lines changed: 228 additions & 80 deletions

File tree

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

Lines changed: 79 additions & 73 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;
@@ -1068,7 +1079,7 @@ private function record_alter_column_default(
10681079
): void {
10691080
$columns_table = $this->get_table_name( $table_is_temporary, 'columns' );
10701081
$column = $this->connection->query(
1071-
'SELECT DATA_TYPE, COLUMN_TYPE, IS_NULLABLE, EXTRA FROM ' . $this->connection->quote_identifier( $columns_table ) . '
1082+
'SELECT DATA_TYPE, DATETIME_PRECISION, IS_NULLABLE, EXTRA FROM ' . $this->connection->quote_identifier( $columns_table ) . '
10721083
WHERE table_schema = ? AND table_name = ? AND column_name = ?',
10731084
array( self::SAVED_DATABASE_NAME, $table_name, $column_name )
10741085
)->fetch( PDO::FETCH_ASSOC );
@@ -1090,11 +1101,12 @@ private function record_alter_column_default(
10901101
$default = null;
10911102
$extra = trim( str_replace( 'DEFAULT_GENERATED', '', $column['EXTRA'] ) );
10921103
if ( $node->has_child_token( WP_MySQL_Lexer::SET_SYMBOL ) ) {
1093-
$expression = $node->get_first_child_node( 'exprWithParentheses' );
1094-
$literal = $expression ? $this->get_parenthesized_default_literal( $expression ) : null;
1095-
$default = $literal
1096-
? $this->get_literal_default( $literal, $column['DATA_TYPE'], $column_name, $column['COLUMN_TYPE'] )
1097-
: $this->get_default_value( $node, $column['DATA_TYPE'], $column_name, $column['COLUMN_TYPE'] );
1104+
$expression = $node->get_first_child_node( 'exprWithParentheses' );
1105+
$literal = $expression ? $this->get_parenthesized_default_literal( $expression ) : null;
1106+
$datetime_precision = null === $column['DATETIME_PRECISION'] ? null : (int) $column['DATETIME_PRECISION'];
1107+
$default = $literal
1108+
? $this->get_literal_default( $literal, $column['DATA_TYPE'], $column_name, $datetime_precision )
1109+
: $this->get_default_value( $node, $column['DATA_TYPE'], $column_name, $datetime_precision );
10981110
if ( null === $default && 'NO' === $column['IS_NULLABLE'] ) {
10991111
throw WP_SQLite_Information_Schema_Exception::invalid_default_value( $column_name );
11001112
}
@@ -1668,7 +1680,7 @@ private function record_drop_check_constraint(
16681680
private function extract_column_data( string $table_name, string $column_name, WP_Parser_Node $node, int $position ): array {
16691681
list ( $data_type, $column_type ) = $this->get_column_data_types( $node );
16701682

1671-
$default = $this->get_column_default( $node, $data_type, $column_name, $column_type );
1683+
$default = $this->get_column_default( $node, $data_type, $column_name );
16721684
$nullable = $this->get_column_nullable( $node );
16731685
$key = $this->get_column_key( $node );
16741686
$extra = $this->get_column_extra( $node );
@@ -2213,10 +2225,9 @@ private function get_table_comment( WP_Parser_Node $node ): string {
22132225
* @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node.
22142226
* @param string $data_type The column data type as stored in information schema.
22152227
* @param string $column_name The column name.
2216-
* @param string $column_type The column type as stored in information schema.
22172228
* @return string|null The column default as stored in information schema.
22182229
*/
2219-
private function get_column_default( WP_Parser_Node $node, string $data_type, string $column_name, string $column_type ): ?string {
2230+
private function get_column_default( WP_Parser_Node $node, string $data_type, string $column_name ): ?string {
22202231
$default_attr = null;
22212232
foreach ( $node->get_descendant_nodes( 'columnAttribute' ) as $attr ) {
22222233
if ( $attr->has_child_token( WP_MySQL_Lexer::DEFAULT_SYMBOL ) ) {
@@ -2227,19 +2238,20 @@ private function get_column_default( WP_Parser_Node $node, string $data_type, st
22272238
if ( null === $default_attr ) {
22282239
return null;
22292240
}
2230-
return $this->get_default_value( $default_attr, $data_type, $column_name, $column_type );
2241+
$datetime_precision = $this->get_column_datetime_precision( $node, $data_type );
2242+
return $this->get_default_value( $default_attr, $data_type, $column_name, $datetime_precision );
22312243
}
22322244

22332245
/**
22342246
* Extract a default value from a column attribute or ALTER COLUMN action.
22352247
*
2236-
* @param WP_Parser_Node $node The "columnAttribute" or "alterListItem" AST node.
2237-
* @param string $data_type The column data type as stored in information schema.
2238-
* @param string $column_name The column name.
2239-
* @param string $column_type The column type as stored in information schema.
2240-
* @return string|null The default value as stored in information schema.
2248+
* @param WP_Parser_Node $node The "columnAttribute" or "alterListItem" AST node.
2249+
* @param string $data_type The column data type as stored in information schema.
2250+
* @param string $column_name The column name.
2251+
* @param int|null $datetime_precision The column fractional seconds precision.
2252+
* @return string|null The default value as stored in information schema.
22412253
*/
2242-
private function get_default_value( WP_Parser_Node $node, string $data_type, string $column_name, string $column_type ): ?string {
2254+
private function get_default_value( WP_Parser_Node $node, string $data_type, string $column_name, ?int $datetime_precision ): ?string {
22432255
/*
22442256
* [GRAMMAR]
22452257
* columnAttribute:
@@ -2268,10 +2280,11 @@ private function get_default_value( WP_Parser_Node $node, string $data_type, str
22682280
if ( null === $literal ) {
22692281
// A signed number, such as "-5", has no "literal" child node.
22702282
// Like MySQL, drop the unary plus, e.g., "+5" becomes "5".
2271-
$number = ltrim( $this->get_value( $signed_literal ), '+' );
2272-
return $this->normalize_literal_default( $number, $data_type, $column_name, $column_type );
2283+
$number = ltrim( $this->get_value( $signed_literal ), '+' );
2284+
$default = $this->normalize_literal_default( $number, $data_type, $column_name );
2285+
return $this->get_temporal_zero_value( $data_type, $default, $datetime_precision ) ?? $default;
22732286
}
2274-
return $this->get_literal_default( $literal, $data_type, $column_name, $column_type );
2287+
return $this->get_literal_default( $literal, $data_type, $column_name, $datetime_precision );
22752288
}
22762289

22772290
// DEFAULT (expression) - MySQL 8.0.13+ supports exprWithParentheses
@@ -2286,13 +2299,13 @@ private function get_default_value( WP_Parser_Node $node, string $data_type, str
22862299
/**
22872300
* Extract and normalize a literal default value.
22882301
*
2289-
* @param WP_Parser_Node $literal The "literal" AST node.
2290-
* @param string $data_type The column data type as stored in information schema.
2291-
* @param string $column_name The column name.
2292-
* @param string $column_type The column type as stored in information schema.
2293-
* @return string|null The default value as stored in information schema.
2302+
* @param WP_Parser_Node $literal The "literal" AST node.
2303+
* @param string $data_type The column data type as stored in information schema.
2304+
* @param string $column_name The column name.
2305+
* @param int|null $datetime_precision The column fractional seconds precision.
2306+
* @return string|null The default value as stored in information schema.
22942307
*/
2295-
private function get_literal_default( WP_Parser_Node $literal, string $data_type, string $column_name, string $column_type ): ?string {
2308+
private function get_literal_default( WP_Parser_Node $literal, string $data_type, string $column_name, ?int $datetime_precision ): ?string {
22962309
// DEFAULT NULL
22972310
if ( $literal->has_child_node( 'nullLiteral' ) ) {
22982311
return null;
@@ -2302,10 +2315,20 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
23022315
if ( $literal->has_child_node( 'boolLiteral' ) ) {
23032316
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
23042317
$bool_value = $bool_literal->has_child_token( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1' : '0';
2305-
return 'bit' === $data_type ? "b'{$bool_value}'" : $bool_value;
2318+
$default = 'bit' === $data_type ? "b'{$bool_value}'" : $bool_value;
2319+
} else {
2320+
$default = $this->normalize_literal_default( $this->get_value( $literal ), $data_type, $column_name );
23062321
}
23072322

2308-
return $this->normalize_literal_default( $this->get_value( $literal ), $data_type, $column_name, $column_type );
2323+
/*
2324+
* Normalize numeric zero defaults to MySQL's temporal zero values.
2325+
* Also normalize string zeros, even where strict MySQL rejects them.
2326+
* Preserve YEAR strings '0' and '00', which MySQL interprets as 2000.
2327+
*/
2328+
if ( 'year' === $data_type && $literal->has_child_node( 'textLiteral' ) ) {
2329+
return $default;
2330+
}
2331+
return $this->get_temporal_zero_value( $data_type, $default, $datetime_precision ) ?? $default;
23092332
}
23102333

23112334
/**
@@ -2314,16 +2337,9 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
23142337
* @param string $default_value The literal default value.
23152338
* @param string $data_type The column data type as stored in information schema.
23162339
* @param string $column_name The column name.
2317-
* @param string $column_type The column type as stored in information schema.
23182340
* @return string The default value as stored in information schema.
23192341
*/
2320-
private function normalize_literal_default( string $default_value, string $data_type, string $column_name, string $column_type ): string {
2321-
// Numeric zero temporal defaults, such as "DEFAULT 0" of a DATETIME column.
2322-
$zero_default = $this->get_temporal_zero_default( $default_value, $data_type, $column_type );
2323-
if ( null !== $zero_default ) {
2324-
return $zero_default;
2325-
}
2326-
2342+
private function normalize_literal_default( string $default_value, string $data_type, string $column_name ): string {
23272343
if ( 'bit' === $data_type ) {
23282344
/*
23292345
* @TODO: Validate and normalize defaults from their AST and the full
@@ -2337,9 +2353,9 @@ private function normalize_literal_default( string $default_value, string $data_
23372353
}
23382354

23392355
/*
2340-
* @TODO: Other non-BIT literal defaults (apart from numeric zero temporal
2341-
* defaults handled above) are currently stored verbatim. To match MySQL,
2342-
* they should be normalized. For example:
2356+
* @TODO: Non-BIT literal defaults, except for temporal zero values, are
2357+
* currently stored verbatim. To match MySQL, they should be normalized.
2358+
* For example:
23432359
* - BINARY/VARBINARY defaults stored as 0x hex literals.
23442360
* - Hex and bit literals coerced to raw bytes for text columns and to
23452361
* their decimal value for numeric columns.
@@ -2350,42 +2366,29 @@ private function normalize_literal_default( string $default_value, string $data_
23502366
}
23512367

23522368
/**
2353-
* Normalize a numeric zero default of a temporal column to its MySQL zero value.
2369+
* Get the zero value of a temporal column when its default is zero.
23542370
*
2355-
* MySQL stores "DEFAULT 0" of a DATETIME column as "0000-00-00 00:00:00",
2356-
* and it rejects the quoted form "DEFAULT '0'" in strict SQL mode. Other
2357-
* temporal defaults are not handled here.
2371+
* For example, a 0, '0', -0.0, or 0e0 default of a DATETIME(3) column has
2372+
* the zero value of "0000-00-00 00:00:00.000".
23582373
*
2359-
* @param string $default_value The default value.
2360-
* @param string $data_type The column data type as stored in information schema.
2361-
* @param string $column_type The column type as stored in information schema.
2362-
* @return string|null The zero value, or null when the default is not a numeric zero
2363-
* or the column is not a temporal column.
2374+
* @param string $data_type The column data type as stored in information schema.
2375+
* @param string $default_value The column default value of a number or string literal.
2376+
* @param int|null $datetime_precision The column fractional seconds precision.
2377+
* @return string|null The zero value, or null when the column is not temporal
2378+
* or the default is not zero.
23642379
*/
2365-
private function get_temporal_zero_default( string $default_value, string $data_type, string $column_type ): ?string {
2366-
if ( ! preg_match( '/\A[+-]?(?:0+(?:\.0*)?|\.0+)\z/', $default_value ) ) {
2367-
return null;
2368-
}
2380+
private function get_temporal_zero_value( string $data_type, string $default_value, ?int $datetime_precision ): ?string {
2381+
$zero_value = self::TEMPORAL_ZERO_VALUE_MAP[ $data_type ] ?? null;
23692382

2370-
// Fractional seconds precision, e.g., "datetime(3)".
2371-
$fraction = '';
2372-
if ( preg_match( '/\((\d+)\)/', $column_type, $matches ) && (int) $matches[1] > 0 ) {
2373-
$fraction = '.' . str_repeat( '0', (int) $matches[1] );
2383+
// A signed number can be a hex literal, such as "-0x0", which is not numeric.
2384+
if ( null === $zero_value || ! is_numeric( $default_value ) || 0.0 !== (float) $default_value ) {
2385+
return null;
23742386
}
23752387

2376-
switch ( $data_type ) {
2377-
case 'date':
2378-
return '0000-00-00';
2379-
case 'year':
2380-
return '0000';
2381-
case 'time':
2382-
return '00:00:00' . $fraction;
2383-
case 'datetime':
2384-
case 'timestamp':
2385-
return '0000-00-00 00:00:00' . $fraction;
2386-
default:
2387-
return null;
2388+
if ( $datetime_precision > 0 ) {
2389+
$zero_value .= '.' . str_repeat( '0', $datetime_precision );
23882390
}
2391+
return $zero_value;
23892392
}
23902393

23912394
/**
@@ -2922,11 +2925,14 @@ private function get_column_numeric_attributes( WP_Parser_Node $node, string $da
29222925
*/
29232926
private function get_column_datetime_precision( WP_Parser_Node $node, string $data_type ): ?int {
29242927
if ( 'time' === $data_type || 'datetime' === $data_type || 'timestamp' === $data_type ) {
2925-
$precision = $node->get_first_descendant_node( 'typeDatetimePrecision' );
2928+
// Expressions in the column definition may have their own precision,
2929+
// e.g., "CAST(... AS DATETIME(6))", so only the data type is checked.
2930+
$type_node = $node->get_first_descendant_node( 'dataType' );
2931+
$precision = $type_node->get_first_descendant_node( 'typeDatetimePrecision' );
29262932
if ( null === $precision ) {
29272933
return 0;
29282934
} else {
2929-
return (int) $this->get_value( $precision );
2935+
return (int) $precision->get_first_child_token( WP_MySQL_Lexer::INT_NUMBER )->get_value();
29302936
}
29312937
}
29322938
return null;

0 commit comments

Comments
 (0)