Skip to content

Commit dc985fe

Browse files
committed
Add WooCommerce DuckDB parity coverage
1 parent 5e1619e commit dc985fe

3 files changed

Lines changed: 448 additions & 3 deletions

File tree

‎.github/workflows/duckdb-package-fast-fail-tests.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ WP_DuckDB_Driver_Parity_Tests::test_numeric_identifier_text_value_predicates_mat
55
WP_DuckDB_Driver_Parity_Tests::test_invalid_literal_regexp_patterns_preserve_left_expression_errors
66
WP_DuckDB_Driver_Parity_Tests::test_invalid_literal_regexp_patterns_match_sqlite
77
WP_DuckDB_Driver_Parity_Tests::test_select_list_date_text_numeric_boundaries_match_sqlite
8+
WP_DuckDB_Driver_Parity_Tests::test_sum_aggregate_results_match_sqlite_and_remain_fetchable
89
WP_DuckDB_Driver_Parity_Tests::test_constant_expression_text_value_scalar_comparisons_match_sqlite
910
WP_DuckDB_Driver_Parity_Tests::test_constant_expression_in_list_items_match_sqlite
1011
WP_DuckDB_Driver_Parity_Tests::test_constant_expression_between_bounds_match_sqlite
@@ -33,6 +34,8 @@ WP_DuckDB_Driver_Parity_Tests::test_select_archive_week_group_date_format_order_
3334
WP_DuckDB_Driver_Parity_Tests::test_posts_slug_status_date_order_ties_match_sqlite_index_order
3435
WP_DuckDB_Driver_Parity_Tests::test_select_cast_convert_binary_expressions_match_sqlite
3536
WP_DuckDB_Driver_Parity_Tests::test_select_date_time_literal_functions_match_sqlite
37+
WP_DuckDB_Driver_Parity_Tests::test_woocommerce_lookup_table_schema_metadata_matches_sqlite
38+
WP_DuckDB_Driver_Parity_Tests::test_woocommerce_lookup_queries_match_sqlite
3639
WP_DuckDB_Driver_Tests::test_regexp_predicates_are_emulated
3740
WP_DuckDB_Driver_Parity_Tests::test_alter_table_drop_primary_key_temporary_shadow_matches_sqlite
3841
WP_DuckDB_Driver_Tests::test_wordpress_term_relationships_post_count_fast_path_uses_one_native_query

‎packages/mysql-on-sqlite/src/duckdb/class-wp-duckdb-driver.php‎

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19594,13 +19594,45 @@ private function execute_show_index( array $tokens ): WP_DuckDB_Result_Statement
1959419594
* Build SHOW INDEX-compatible rows for all indexes on a table.
1959519595
*
1959619596
* @param string $table_name Table name.
19597+
* @param bool $temporary Whether to inspect the temporary metadata table.
1959719598
* @return array<int,array<int,mixed>>
1959819599
*/
1959919600
private function index_rows_for_table( string $table_name, bool $temporary = false ): array {
19600-
return array_merge(
19601+
$rows = array_merge(
1960119602
$this->primary_key_index_rows( $table_name, $temporary ),
1960219603
$this->secondary_index_rows( $table_name, $temporary )
1960319604
);
19605+
19606+
return $this->show_index_rows_with_column_metadata( $table_name, $temporary, $rows );
19607+
}
19608+
19609+
/**
19610+
* Fill SHOW INDEX fields that depend on column metadata.
19611+
*
19612+
* @param string $table_name Table name.
19613+
* @param bool $temporary Whether to inspect the temporary metadata table.
19614+
* @param array<int,array<int,mixed>> $rows SHOW INDEX rows.
19615+
* @return array<int,array<int,mixed>>
19616+
*/
19617+
private function show_index_rows_with_column_metadata( string $table_name, bool $temporary, array $rows ): array {
19618+
if ( count( $rows ) === 0 ) {
19619+
return $rows;
19620+
}
19621+
19622+
$nullable_by_column = $this->statistics_nullable_by_column( $table_name, $temporary );
19623+
foreach ( $rows as &$row ) {
19624+
$key_name = (string) $row[2];
19625+
$column_name = null === $row[4] ? null : (string) $row[4];
19626+
19627+
$row[6] = 0;
19628+
$row[9] = '';
19629+
if ( 'PRIMARY' !== $key_name && null !== $column_name ) {
19630+
$row[9] = $nullable_by_column[ strtolower( $column_name ) ] ?? '';
19631+
}
19632+
}
19633+
unset( $row );
19634+
19635+
return $rows;
1960419636
}
1960519637

1960619638
/**
@@ -22257,6 +22289,22 @@ private function translate_tokens_to_duckdb_sql(
2225722289
continue;
2225822290
}
2225922291

22292+
$sum_function = $this->translate_fetchable_sum_function_call(
22293+
$tokens,
22294+
$index,
22295+
$rewrite_information_schema_tables,
22296+
$rewrite_information_schema_columns,
22297+
$rewrite_information_schema_statistics,
22298+
$rewrite_information_schema_table_constraints,
22299+
$rewrite_information_schema_key_column_usage,
22300+
$rewrite_information_schema_referential_constraints,
22301+
$rewrite_information_schema_check_constraints
22302+
);
22303+
if ( null !== $sum_function ) {
22304+
$pieces[] = $sum_function;
22305+
continue;
22306+
}
22307+
2226022308
if (
2226122309
WP_MySQL_Lexer::NOT_SYMBOL === $token->id
2226222310
&& isset( $tokens[ $index + 1 ], $tokens[ $index + 2 ] )
@@ -23247,6 +23295,59 @@ private function translate_sum_length_function_call(
2324723295
. ') AS BIGINT)';
2324823296
}
2324923297

23298+
/**
23299+
* Translate SUM() to a PHP-client-fetchable result type.
23300+
*
23301+
* DuckDB promotes some integer SUM() results to HUGEINT, which the PHP
23302+
* client refuses without bcmath. MySQL/wpdb exposes ordinary plugin report
23303+
* sums as fetchable scalars, so keep the SQL numeric while avoiding HUGEINT.
23304+
*
23305+
* @param WP_Parser_Token[] $tokens MySQL tokens.
23306+
* @param int $index Current token index, advanced on match.
23307+
* @return string|null DuckDB SQL, or null when the token does not start SUM().
23308+
*/
23309+
private function translate_fetchable_sum_function_call(
23310+
array $tokens,
23311+
int &$index,
23312+
bool $rewrite_information_schema_tables,
23313+
bool $rewrite_information_schema_columns,
23314+
bool $rewrite_information_schema_statistics,
23315+
bool $rewrite_information_schema_table_constraints,
23316+
bool $rewrite_information_schema_key_column_usage,
23317+
bool $rewrite_information_schema_referential_constraints,
23318+
bool $rewrite_information_schema_check_constraints
23319+
): ?string {
23320+
if (
23321+
! isset( $tokens[ $index + 1 ] )
23322+
|| $this->is_non_identifier_token( $tokens[ $index ] )
23323+
|| 0 !== strcasecmp( $tokens[ $index ]->get_value(), 'SUM' )
23324+
|| WP_MySQL_Lexer::OPEN_PAR_SYMBOL !== $tokens[ $index + 1 ]->id
23325+
) {
23326+
return null;
23327+
}
23328+
23329+
$end_index = $this->skip_balanced_parentheses( $tokens, $index + 1 );
23330+
$body = array_slice( $tokens, $index + 2, $end_index - $index - 3 );
23331+
$items = $this->split_top_level_comma_items( $body );
23332+
if ( 1 !== count( $items ) ) {
23333+
return null;
23334+
}
23335+
23336+
$index = $end_index - 1;
23337+
return 'CAST(SUM('
23338+
. $this->translate_tokens_to_duckdb_sql(
23339+
$items[0],
23340+
$rewrite_information_schema_tables,
23341+
$rewrite_information_schema_columns,
23342+
$rewrite_information_schema_statistics,
23343+
$rewrite_information_schema_table_constraints,
23344+
$rewrite_information_schema_key_column_usage,
23345+
$rewrite_information_schema_referential_constraints,
23346+
$rewrite_information_schema_check_constraints
23347+
)
23348+
. ') AS DOUBLE)';
23349+
}
23350+
2325023351
/**
2325123352
* Translate MySQL REPLACE ... VALUES to DuckDB INSERT OR REPLACE.
2325223353
*
@@ -34226,8 +34327,8 @@ private function information_schema_statistics_row( array $index_row, array $nul
3422634327
* @param string $table_name Table name.
3422734328
* @return array<string,string> Nullability keyed by lowercase column name.
3422834329
*/
34229-
private function statistics_nullable_by_column( string $table_name ): array {
34230-
$metadata_rows = $this->column_metadata_rows( $table_name );
34330+
private function statistics_nullable_by_column( string $table_name, bool $temporary = false ): array {
34331+
$metadata_rows = $this->column_metadata_rows( $table_name, $temporary );
3423134332
if ( count( $metadata_rows ) === 0 ) {
3423234333
$metadata_rows = $this->pragma_column_metadata_rows( $table_name );
3423334334
}

0 commit comments

Comments
 (0)