@@ -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