@@ -380,11 +380,19 @@ func (c *clientConn) handleQuery(body []byte) error {
380380
381381 result , err := c .db .Exec (query )
382382 if err != nil {
383- c .sendError ("ERROR" , "42000" , err .Error ())
384- c .setTxError ()
385- writeReadyForQuery (c .writer , c .txStatus )
386- c .writer .Flush ()
387- return nil
383+ // Retry ALTER TABLE as ALTER VIEW if target is a view
384+ if isAlterTableNotTableError (err ) {
385+ if alteredQuery , ok := transpiler .ConvertAlterTableToAlterView (query ); ok {
386+ result , err = c .db .Exec (alteredQuery )
387+ }
388+ }
389+ if err != nil {
390+ c .sendError ("ERROR" , "42000" , err .Error ())
391+ c .setTxError ()
392+ writeReadyForQuery (c .writer , c .txStatus )
393+ c .writer .Flush ()
394+ return nil
395+ }
388396 }
389397
390398 c .updateTxStatus (cmdType )
@@ -1561,10 +1569,18 @@ func (c *clientConn) handleExecute(body []byte) {
15611569 // Non-result-returning query: use Exec with converted query
15621570 result , err := c .db .Exec (p .stmt .convertedQuery , args ... )
15631571 if err != nil {
1564- log .Printf ("[%s] Execute error: %v" , c .username , err )
1565- c .sendError ("ERROR" , "42000" , err .Error ())
1566- c .setTxError ()
1567- return
1572+ // Retry ALTER TABLE as ALTER VIEW if target is a view
1573+ if isAlterTableNotTableError (err ) {
1574+ if alteredQuery , ok := transpiler .ConvertAlterTableToAlterView (p .stmt .convertedQuery ); ok {
1575+ result , err = c .db .Exec (alteredQuery , args ... )
1576+ }
1577+ }
1578+ if err != nil {
1579+ log .Printf ("[%s] Execute error: %v" , c .username , err )
1580+ c .sendError ("ERROR" , "42000" , err .Error ())
1581+ c .setTxError ()
1582+ return
1583+ }
15681584 }
15691585 c .updateTxStatus (cmdType )
15701586 tag := c .buildCommandTag (cmdType , result )
@@ -1689,3 +1705,12 @@ func readCString(r *bytes.Reader) (string, error) {
16891705 }
16901706 return buf .String (), nil
16911707}
1708+
1709+ // isAlterTableNotTableError checks if the error indicates that an ALTER TABLE
1710+ // was attempted on a view. DuckDB returns this error when trying to use
1711+ // ALTER TABLE ... RENAME TO on a view instead of ALTER VIEW.
1712+ func isAlterTableNotTableError (err error ) bool {
1713+ msg := strings .ToLower (err .Error ())
1714+ return strings .Contains (msg , "cannot use alter table" ) &&
1715+ strings .Contains (msg , "not a table" )
1716+ }
0 commit comments