How to change a column name in hive - hive

I have a hive table where the name of the columns are orderbook.time, orderbook.price, etc. I want to remove the prefix orderbook from the column names without changing anything else in the table. I'm using the following command
alter table orderbook change orderbook.time time;
but it gives me the following error message
NoViableAltException(17#[])
at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.identifier(HiveParser_IdentifiersParser.java:11568)
at org.apache.hadoop.hive.ql.parse.HiveParser.identifier(HiveParser.java:45214)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatementSuffixRenameCol(HiveParser.java:10258)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterTblPartitionStatementSuffix(HiveParser.java:8533)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterTableStatementSuffix(HiveParser.java:8148)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatement(HiveParser.java:7192)
at org.apache.hadoop.hive.ql.parse.HiveParser.ddlStatement(HiveParser.java:2604)
at org.apache.hadoop.hive.ql.parse.HiveParser.execStatement(HiveParser.java:1591)
at org.apache.hadoop.hive.ql.parse.HiveParser.statement(HiveParser.java:1067)
at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:205)
at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:170)
at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:524)
at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1358)
at org.apache.hadoop.hive.ql.Driver.runInternal(Driver.java:1475)
at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1287)
at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1277)
at org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:226)
at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:175)
at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:389)
at org.apache.hadoop.hive.cli.CliDriver.executeDriver(CliDriver.java:781)
at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:699)
at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:634)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.RunJar.run(RunJar.java:226)
at org.apache.hadoop.util.RunJar.main(RunJar.java:141)
FAILED: ParseException line 1:38 cannot recognize input near '.' 'time' 'time' in rename column name
I tried to put the old column names (e.g. orderbook.time) into quatation, but I'm getting the same error message. How can I change the column names?

You are missing datatype of time column and Then try escaping the column name with period with `(backtick)
Try with below query:
alter table orderbook change `orderbook.time` time <data_type>;
In general Syntax to change column name in Hive:
alter table <db_name>.<table_name> change `<col_name>` `<new_col_name>` <data_type>;

Related

How do I change column data type in Redshift?

I have tried changing the column data type in Redshift via SQL. Keep getting an error:
[Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42601] ERROR: syntax error at or near "TABLE" LINE 17: ALTER TABLE bmd_disruption_fv ^
Unable to connect to the Amazon Redshift server 'eceim.master.datamart.eceim.sin.auto.prod.c0.sq.com.sg'. Check that the server is running and that you have access privileges to the requested database
The first sql query works. I have tried writing the Alter Table script before the Select lines but it did not work too.
`
*Extract selected columns and renaming them for easier reference
*/
select ID, Completion_Time AS Date_Reported, Name2 AS Name, Contact_Info_for_updates AS Contact_Info,
Your_operation_line AS Operation_Line, Aircraft_Registration_SMU_SMT_etc AS Aircraft_Reg,
Designation_trade_B1_B2_ACT_AST_AAT AS Trade, Choose_your_Issue AS Issue, Manpower, Material, Equipment_GES,
Information, Tools, State_details_here_SVO_number_too AS Issue_Details, Time_wasted_on_due_to_issue AS Time_Wasted,
State_additional_comments_suggestions AS Additional_Comments, Stakeholders, Status
from bmdm.bmd_disruption_fv
/*Change colum data type
*/
ALTER TABLE bmd_disruption_fv
{
ALTER COLUMN ID TYPE INT
}
`
Several things are causing issues here. First the curly brackets '{}' should not be in the alter table statement. Like this:
alter table event alter column eventname type varchar(300);
Second, and likely more importantly, you can only change the length of varchar columns. So changing a column type to INT is not possible. You will need to perform a multistep process to make this change to the table.

ParseException when having a field name which contains '#' in SparkSQL

I'm doing a simple operation of inserting some fields of table into another table, both tables are hive tables in databricks, so I'm able to do it with a simple query like:
INSERT INTO <BBDD_NAME>.<TABLE1_NAME> (<FIELD_1>, <FIELD_2>)
SELECT <FIELD_1>, <FIELD_2># FROM <BBDD_NAME>.<TABLE2_NAME>
The problem I have is because one of the fields has a '#' inside its name and consequently I get a ParseException Error:
Error in SQL statement: ParseException:
mismatched input '#' expecting {<EOF>, ';'}
TABLE2 is F0911 from JDE (JDE table doc) and is being inserted directly into databricks via spark inferring the schema from its origin. So, table was created with no problem containing the '#' containing field.
Is there any way of avoiding this ParseException Error?
Thanks in advance.
You can use backticks to escape illegal identifiers in column names.
This should work:
INSERT INTO <BBDD_NAME>.<TABLE1_NAME> (<FIELD_1>, <FIELD_2>)
SELECT <FIELD_1>, `<FIELD_2>#` FROM <BBDD_NAME>.<TABLE2_NAME>

Moving a table from one schema to another in Exasol

I am trying to move a table which resides in a certain schema to a different schema with the same table name. I have tried the following but they do not work:
rename <OLD_SCHEMA_NAME>.<TABLE_NAME> TO <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: invalid identifier chain for new name [line 1, column 100] (Session: 1722923178259251200)
and
ALTER TABLE <OLD_SCHEMA_NAME>.<TABLE_NAME> RENAME <NEW_SCHEMA_NAME>.<TABLE_NAME>;
The error that appears is:
SQL Error [42000]: syntax error, unexpected IDENTIFIER_PART_, expecting COLUMN_ or CONSTRAINT_ [line 1, column 62] (Session: 1722923178259251200)
Many Thanks!
According to Exasol documentation there is no way to move table between schemas using RENAME statement:
Schema objects cannot be shifted to another schema with the RENAME
statement. For example, 'RENAME TABLE s1.t1 TO s2.t2' is not allowed.
I would move the table this way:
create table <NEW_SCHEMA_NAME>.<TABLE_NAME>
like <OLD_SCHEMA_NAME>.<TABLE_NAME>
including defaults
including identity
including comments;
insert into <NEW_SCHEMA_NAME>.<TABLE_NAME>
select *
from <OLD_SCHEMA_NAME>.<TABLE_NAME>;
drop table <OLD_SCHEMA_NAME>.<TABLE_NAME>;

Changing column datatype in parquet file

I have an external table pointing to an s3 location (parquet file) which has all the datatypes as string. I want to correct the datatypes of all the columns instead of just reading everything as a string. when i drop the external table and recreate with new datatypes, the select query always throws error which looks something like below:
java.lang.UnsupportedOperationException: org.apache.parquet.column.values.dictionary.PlainValuesDictionary$PlainBinaryDictionary
at org.apache.parquet.column.Dictionary.decodeToInt(Dictionary.java:48)
at org.apache.spark.sql.execution.vectorized.OnHeapColumnVector.getInt(OnHeapColumnVector.java:233)
at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source)
at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)
at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:395)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:234)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:228)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:827)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:827)
Specify type as BigInt which is Equivalent to long type,hive don't have long datatype.
hive> alter table table change col col bigint;
Duplicate content, from Hortonworks forum

Rename column with reserved word as column name; Works on one, fails on another

I'm renaming two columns in two tables, both of which are named LEVEL, which is a reserved word.
What is confusing me greatly is that I am running an identical command on both these tables, but only one of them is failing. Here are the commands:
--this works
ALTER TABLE notificationsubscriptions RENAME COLUMN "LEVEL" TO nlevel;
--this doesn't
ALTER TABLE notifications RENAME COLUMN "LEVEL" TO nlevel;
The second statement results in the following error:
Error starting at line : 9 in command -
ALTER TABLE notifications RENAME COLUMN "LEVEL" TO nlevel
Error report -
SQL Error: ORA-00900: invalid SQL statement
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
Both these columns are of the same datatype (number). I am baffled - Why would this command work for one of these tables but not the other?
The database is running Oracle 10.2.
Try to remove any links that exist on this column from indexes and/or foreign key constraints
Drop indexes
Rename column
Create indexes with the new name of column