Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Column to Spark Table via SQL ALTER TABLE command

Can I add a new column to an existing spark table using the ALTER TABLE command ?

var query = "ALTER TABLE " + "global_temp." + tableName(0) + " ADD COLUMN " + newColumnName + " " + newColumnDatatype
var drt = spark.sql(query)

The above code raises the following error.

 no viable alternative at input 'ALTER TABLE global_temp.people_ty ADD COLUMN' new_age integer

EDIT

The correct syntax is as follows

ALTER TABLE tablename ADD COLUMNS (newColumn newDataType)

But, it also throws the following error.

ALTER ADD COLUMNS does not support views.
You must drop and re-create the views for adding the new columns. Views: `global_temp`.`people_ty`
like image 254
Soumyadip Ghosh Avatar asked Aug 08 '26 23:08

Soumyadip Ghosh


1 Answers

In Spark SQL the syntax is as mentioned by Soumyadip Ghosh in the comments

ALTER TABLE table_identifier ADD COLUMNS ( col_spec [ , ... ] )

Works for me.

like image 158
Reed Richards Avatar answered Aug 10 '26 14:08

Reed Richards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!