Skip to content

Commit dd87821

Browse files
committed
Add DuckDB view DDL parity
1 parent dc985fe commit dd87821

3 files changed

Lines changed: 394 additions & 62 deletions

File tree

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

Lines changed: 244 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ public function query( string $query ): WP_DuckDB_Result_Statement {
556556
return $this->execute_truncate_table( $tokens );
557557
case WP_MySQL_Lexer::ALTER_SYMBOL:
558558
$this->found_rows = 0;
559+
if ( $this->is_alter_view_statement( $tokens ) ) {
560+
return $this->execute_alter_view( $tokens );
561+
}
559562
return $this->execute_alter_table( $tokens );
560563
case WP_MySQL_Lexer::ANALYZE_SYMBOL:
561564
case WP_MySQL_Lexer::CHECK_SYMBOL:
@@ -10689,7 +10692,7 @@ private function concatenate_token_bytes( array $tokens ): string {
1068910692
*/
1069010693
private function execute_create( array $tokens ): WP_DuckDB_Result_Statement {
1069110694
if ( $this->is_create_view_statement( $tokens ) ) {
10692-
throw new WP_DuckDB_Driver_Exception( 'Unsupported CREATE VIEW statement in DuckDB driver. MySQL view lifecycle metadata is not supported (createView).' );
10695+
return $this->execute_create_view( $tokens );
1069310696
}
1069410697

1069510698
if ( isset( $tokens[1] ) && WP_MySQL_Lexer::TABLE_SYMBOL === $tokens[1]->id ) {
@@ -10708,7 +10711,7 @@ private function execute_create( array $tokens ): WP_DuckDB_Result_Statement {
1070810711
return $this->execute_create_index( $tokens );
1070910712
}
1071010713

10711-
throw new WP_DuckDB_Driver_Exception( 'Unsupported CREATE statement in DuckDB driver. Only CREATE TABLE and CREATE INDEX are supported.' );
10714+
throw new WP_DuckDB_Driver_Exception( 'Unsupported CREATE statement in DuckDB driver. Only CREATE TABLE, CREATE INDEX, and CREATE VIEW are supported.' );
1071210715
}
1071310716

1071410717
/**
@@ -10726,6 +10729,216 @@ private function is_create_view_statement( array $tokens ): bool {
1072610729
return null !== $this->find_top_level_token_index( $tokens, $view_index + 1, WP_MySQL_Lexer::AS_SYMBOL );
1072710730
}
1072810731

10732+
/**
10733+
* Check whether an ALTER statement targets a view definition.
10734+
*
10735+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10736+
* @return bool Whether this is an ALTER VIEW statement.
10737+
*/
10738+
private function is_alter_view_statement( array $tokens ): bool {
10739+
$view_index = $this->find_top_level_token_index( $tokens, 1, WP_MySQL_Lexer::VIEW_SYMBOL );
10740+
if ( null === $view_index ) {
10741+
return false;
10742+
}
10743+
10744+
return null !== $this->find_top_level_token_index( $tokens, $view_index + 1, WP_MySQL_Lexer::AS_SYMBOL );
10745+
}
10746+
10747+
/**
10748+
* Execute a supported CREATE VIEW statement.
10749+
*
10750+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10751+
* @return WP_DuckDB_Result_Statement
10752+
*/
10753+
private function execute_create_view( array $tokens ): WP_DuckDB_Result_Statement {
10754+
return $this->execute_create_or_alter_view( $tokens, 'CREATE VIEW' );
10755+
}
10756+
10757+
/**
10758+
* Execute a supported ALTER VIEW statement.
10759+
*
10760+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10761+
* @return WP_DuckDB_Result_Statement
10762+
*/
10763+
private function execute_alter_view( array $tokens ): WP_DuckDB_Result_Statement {
10764+
return $this->execute_create_or_alter_view( $tokens, 'ALTER VIEW' );
10765+
}
10766+
10767+
/**
10768+
* Execute a supported CREATE/ALTER VIEW definition.
10769+
*
10770+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10771+
* @param string $statement Statement label for errors.
10772+
* @return WP_DuckDB_Result_Statement
10773+
*/
10774+
private function execute_create_or_alter_view( array $tokens, string $statement ): WP_DuckDB_Result_Statement {
10775+
$index = 0;
10776+
$or_replace = 'ALTER VIEW' === $statement;
10777+
10778+
if ( 'CREATE VIEW' === $statement ) {
10779+
$this->expect_token( $tokens, $index, WP_MySQL_Lexer::CREATE_SYMBOL, 'Expected CREATE.' );
10780+
++$index;
10781+
if (
10782+
isset( $tokens[ $index + 1 ] )
10783+
&& WP_MySQL_Lexer::OR_SYMBOL === $tokens[ $index ]->id
10784+
&& WP_MySQL_Lexer::REPLACE_SYMBOL === $tokens[ $index + 1 ]->id
10785+
) {
10786+
$or_replace = true;
10787+
$index += 2;
10788+
}
10789+
} else {
10790+
$this->expect_token( $tokens, $index, WP_MySQL_Lexer::ALTER_SYMBOL, 'Expected ALTER.' );
10791+
++$index;
10792+
}
10793+
10794+
if ( $this->contains_unsupported_view_prefix_clause( $tokens, $index ) ) {
10795+
$this->throw_unsupported_view_statement( $statement );
10796+
}
10797+
10798+
if ( ! isset( $tokens[ $index ] ) || WP_MySQL_Lexer::VIEW_SYMBOL !== $tokens[ $index ]->id ) {
10799+
$this->throw_unsupported_view_statement( $statement );
10800+
}
10801+
++$index;
10802+
10803+
$reference = $this->parse_schema_lifecycle_table_reference( $tokens, $index, $statement );
10804+
$index = $reference['next_index'];
10805+
10806+
$columns_sql = $this->parse_view_column_list_sql( $tokens, $index, $statement );
10807+
if ( ! isset( $tokens[ $index ] ) || WP_MySQL_Lexer::AS_SYMBOL !== $tokens[ $index ]->id ) {
10808+
$this->throw_unsupported_view_statement( $statement );
10809+
}
10810+
++$index;
10811+
10812+
if (
10813+
! isset( $tokens[ $index ] )
10814+
|| WP_MySQL_Lexer::SELECT_SYMBOL !== $tokens[ $index ]->id
10815+
|| $this->contains_unsupported_view_select_suffix_clause( $tokens, $index )
10816+
) {
10817+
$this->throw_unsupported_view_statement( $statement );
10818+
}
10819+
10820+
$select_tokens = array_slice( $tokens, $index );
10821+
$select_sql = $this->translate_tokens_to_duckdb_sql( $select_tokens );
10822+
10823+
return $this->execute_duckdb_query(
10824+
'CREATE '
10825+
. ( $or_replace ? 'OR REPLACE ' : '' )
10826+
. 'VIEW '
10827+
. $this->connection->quote_identifier( $reference['requested_table_name'] )
10828+
. $columns_sql
10829+
. ' AS '
10830+
. $select_sql,
10831+
'Failed to create DuckDB view'
10832+
);
10833+
}
10834+
10835+
/**
10836+
* Parse an optional MySQL view column list.
10837+
*
10838+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10839+
* @param int $index Current token index.
10840+
* @param string $statement Statement label for errors.
10841+
* @return string DuckDB column-list SQL.
10842+
*/
10843+
private function parse_view_column_list_sql( array $tokens, int &$index, string $statement ): string {
10844+
if ( ! isset( $tokens[ $index ] ) || WP_MySQL_Lexer::OPEN_PAR_SYMBOL !== $tokens[ $index ]->id ) {
10845+
return '';
10846+
}
10847+
10848+
++$index;
10849+
try {
10850+
list( $items, $index ) = $this->collect_parenthesized_items( $tokens, $index );
10851+
} catch ( WP_DuckDB_Driver_Exception $e ) {
10852+
$this->throw_unsupported_view_statement( $statement );
10853+
}
10854+
10855+
$columns = array();
10856+
foreach ( $items as $item ) {
10857+
if ( 1 !== count( $item ) || $this->is_non_identifier_token( $item[0] ) ) {
10858+
$this->throw_unsupported_view_statement( $statement );
10859+
}
10860+
$columns[] = $this->connection->quote_identifier( $this->identifier_value( $item[0] ) );
10861+
}
10862+
10863+
if ( count( $columns ) === 0 ) {
10864+
$this->throw_unsupported_view_statement( $statement );
10865+
}
10866+
10867+
return ' (' . implode( ', ', $columns ) . ')';
10868+
}
10869+
10870+
/**
10871+
* Check for unsupported MySQL-only view definition prefixes.
10872+
*
10873+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10874+
* @param int $index Current token index.
10875+
* @return bool Whether unsupported syntax was found before VIEW.
10876+
*/
10877+
private function contains_unsupported_view_prefix_clause( array $tokens, int $index ): bool {
10878+
for ( ; $index < count( $tokens ); ++$index ) {
10879+
if ( WP_MySQL_Lexer::VIEW_SYMBOL === $tokens[ $index ]->id ) {
10880+
return false;
10881+
}
10882+
10883+
if (
10884+
in_array( $tokens[ $index ]->id, array( WP_MySQL_Lexer::ALGORITHM_SYMBOL, WP_MySQL_Lexer::DEFINER_SYMBOL, WP_MySQL_Lexer::SECURITY_SYMBOL ), true )
10885+
|| (
10886+
WP_MySQL_Lexer::SQL_SYMBOL === $tokens[ $index ]->id
10887+
&& isset( $tokens[ $index + 1 ] )
10888+
&& WP_MySQL_Lexer::SECURITY_SYMBOL === $tokens[ $index + 1 ]->id
10889+
)
10890+
) {
10891+
return true;
10892+
}
10893+
}
10894+
10895+
return false;
10896+
}
10897+
10898+
/**
10899+
* Check for unsupported MySQL-only view select suffix clauses.
10900+
*
10901+
* @param WP_Parser_Token[] $tokens MySQL tokens.
10902+
* @param int $index First token of the SELECT statement.
10903+
* @return bool Whether unsupported suffix syntax was found.
10904+
*/
10905+
private function contains_unsupported_view_select_suffix_clause( array $tokens, int $index ): bool {
10906+
$depth = 0;
10907+
for ( ; $index < count( $tokens ); ++$index ) {
10908+
if ( WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $tokens[ $index ]->id ) {
10909+
++$depth;
10910+
continue;
10911+
}
10912+
if ( WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $tokens[ $index ]->id ) {
10913+
--$depth;
10914+
if ( $depth < 0 ) {
10915+
return true;
10916+
}
10917+
continue;
10918+
}
10919+
if (
10920+
0 === $depth
10921+
&& WP_MySQL_Lexer::WITH_SYMBOL === $tokens[ $index ]->id
10922+
&& isset( $tokens[ $index + 1 ] )
10923+
&& in_array( $tokens[ $index + 1 ]->id, array( WP_MySQL_Lexer::CASCADED_SYMBOL, WP_MySQL_Lexer::CHECK_SYMBOL, WP_MySQL_Lexer::LOCAL_SYMBOL ), true )
10924+
) {
10925+
return true;
10926+
}
10927+
}
10928+
10929+
return 0 !== $depth;
10930+
}
10931+
10932+
/**
10933+
* Throw a stable unsupported view statement exception.
10934+
*
10935+
* @param string $statement Statement label.
10936+
* @return void
10937+
*/
10938+
private function throw_unsupported_view_statement( string $statement ): void {
10939+
throw new WP_DuckDB_Driver_Exception( 'Unsupported ' . $statement . ' statement in DuckDB driver.' );
10940+
}
10941+
1072910942
/**
1073010943
* Execute a supported CREATE TABLE statement.
1073110944
*
@@ -12554,24 +12767,40 @@ private function execute_drop_view( array $tokens ): WP_DuckDB_Result_Statement
1255412767
}
1255512768

1255612769
$view_tokens = array_slice( $tokens, $index );
12770+
if ( count( $view_tokens ) > 0 ) {
12771+
$last_token = $view_tokens[ count( $view_tokens ) - 1 ];
12772+
if ( WP_MySQL_Lexer::RESTRICT_SYMBOL === $last_token->id || WP_MySQL_Lexer::CASCADE_SYMBOL === $last_token->id ) {
12773+
array_pop( $view_tokens );
12774+
}
12775+
}
1255712776
if ( count( $view_tokens ) === 0 ) {
12558-
throw new WP_DuckDB_Driver_Exception( 'DROP VIEW requires a view name.' );
12777+
throw new WP_DuckDB_Driver_Exception( 'DROP VIEW requires at least one view name.' );
1255912778
}
12560-
if ( count( $this->split_top_level_comma_items( $view_tokens ) ) > 1 ) {
12561-
throw new WP_DuckDB_Driver_Exception( 'Unsupported DROP VIEW statement in DuckDB driver. Only a single view target is supported.' );
12779+
12780+
$targets = array();
12781+
foreach ( $this->split_top_level_comma_items( $view_tokens ) as $view_item ) {
12782+
$reference = $this->parse_schema_lifecycle_table_reference( $view_item, 0, 'DROP VIEW' );
12783+
if ( count( $view_item ) !== $reference['next_index'] ) {
12784+
throw new WP_DuckDB_Driver_Exception( 'Unsupported DROP VIEW statement in DuckDB driver. View aliases and extra options are not supported.' );
12785+
}
12786+
$targets[] = $reference['requested_table_name'];
1256212787
}
1256312788

12564-
$reference = $this->parse_schema_lifecycle_table_reference( $tokens, $index, 'DROP VIEW' );
12565-
if ( count( $tokens ) !== $reference['next_index'] ) {
12566-
throw new WP_DuckDB_Driver_Exception( 'Unsupported DROP VIEW statement in DuckDB driver. View aliases and extra options are not supported.' );
12789+
$result = null;
12790+
foreach ( $targets as $view_name ) {
12791+
$result = $this->execute_duckdb_query(
12792+
'DROP VIEW '
12793+
. ( $if_exists ? 'IF EXISTS ' : '' )
12794+
. $this->connection->quote_identifier( $view_name ),
12795+
'Failed to drop DuckDB view'
12796+
);
1256712797
}
1256812798

12569-
return $this->execute_duckdb_query(
12570-
'DROP VIEW '
12571-
. ( $if_exists ? 'IF EXISTS ' : '' )
12572-
. $this->connection->quote_identifier( $reference['requested_table_name'] ),
12573-
'Failed to drop DuckDB view'
12574-
);
12799+
if ( null === $result ) {
12800+
throw new WP_DuckDB_Driver_Exception( 'DROP VIEW requires at least one view name.' );
12801+
}
12802+
12803+
return $result;
1257512804
}
1257612805

1257712806
/**
@@ -18734,7 +18963,7 @@ private function execute_show( array $tokens ): WP_DuckDB_Result_Statement {
1873418963
&& WP_MySQL_Lexer::CREATE_SYMBOL === $tokens[1]->id
1873518964
&& WP_MySQL_Lexer::VIEW_SYMBOL === $tokens[2]->id
1873618965
) {
18737-
throw new WP_DuckDB_Driver_Exception( 'Unsupported SHOW CREATE VIEW statement in DuckDB driver. MySQL view lifecycle metadata is not supported (showStatement > CREATE).' );
18966+
throw new WP_DuckDB_Driver_Exception( 'Unsupported SHOW CREATE VIEW statement in DuckDB driver. MySQL view definition metadata is not supported.' );
1873818967
}
1873918968

1874018969
if (

‎packages/mysql-on-sqlite/tests/duckdb/WP_DuckDB_Driver_Parity_Tests.php‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,21 +1566,13 @@ public function test_show_full_tables_sql_matches_sqlite(): void {
15661566
$this->assertParityRows( "SHOW FULL TABLES LIKE 'temp_only_show'" );
15671567
}
15681568

1569-
public function test_view_lifecycle_limitations_match_sqlite(): void {
1569+
public function test_view_metadata_limitations_match_sqlite(): void {
15701570
$this->runParitySetup(
15711571
array(
15721572
'CREATE TABLE view_source (id INT, name VARCHAR(20))',
15731573
)
15741574
);
15751575

1576-
$this->assertParityErrorContains(
1577-
'CREATE VIEW visible_view AS SELECT id, name FROM view_source',
1578-
'createView'
1579-
);
1580-
$this->assertParityErrorContains(
1581-
'CREATE OR REPLACE VIEW visible_view AS SELECT id FROM view_source',
1582-
'createView'
1583-
);
15841576
$this->assertParityErrorContains( 'SHOW CREATE VIEW visible_view', 'CREATE' );
15851577
$this->assertParityErrorContains( 'DROP VIEW missing_view', 'missing_view' );
15861578
$this->runParitySetup( array( 'DROP VIEW IF EXISTS missing_view' ) );

0 commit comments

Comments
 (0)