0

I need to drop from a hive table dynamically and this is the way I am trying to do:

   set hivevar:range=select date_sub(date '2019-10-21',19);
   hive> ${hivevar:range};
   OK
   2019-10-02
   Time taken: 0.417 seconds, Fetched: 1 row(s)

But when I try the following

   hive> alter table table_name drop if exists partition (data_date<'${hivevar:range}');

I get this error:

   NoViableAltException(340@[319:1: constant : ( ( intervalLiteral )=> intervalLiteral | Number | dateLiteral | timestampLiteral | StringLiteral | stringLiteralSequence | IntegralLiteral | NumberLiteral | charSetStringLiteral | booleanValue | KW_NULL -> TOK_NULL );])
at org.antlr.runtime.DFA.noViableAlt(DFA.java:158)
at org.antlr.runtime.DFA.predict(DFA.java:116)
at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.constant(HiveParser_IdentifiersParser.java:5267)
at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.dropPartitionVal(HiveParser_IdentifiersParser.java:10752)
at org.apache.hadoop.hive.ql.parse.HiveParser_IdentifiersParser.dropPartitionSpec(HiveParser_IdentifiersParser.java:10621)
at org.apache.hadoop.hive.ql.parse.HiveParser.dropPartitionSpec(HiveParser.java:41976)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatementSuffixDropPartitions(HiveParser.java:11915)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterTableStatementSuffix(HiveParser.java:8535)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatement(HiveParser.java:8013)
at org.apache.hadoop.hive.ql.parse.HiveParser.ddlStatement(HiveParser.java:3850)
at org.apache.hadoop.hive.ql.parse.HiveParser.execStatement(HiveParser.java:2382)
at org.apache.hadoop.hive.ql.parse.HiveParser.statement(HiveParser.java:1333)
at org.apache.hadoop.hive.ql.parse.ParseDriver.parse(ParseDriver.java:208)
at org.apache.hadoop.hive.ql.parse.ParseUtils.parse(ParseUtils.java:77)
at org.apache.hadoop.hive.ql.parse.ParseUtils.parse(ParseUtils.java:70)
at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:468)
at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1317)
at org.apache.hadoop.hive.ql.Driver.runInternal(Driver.java:1457)
at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1237)
at org.apache.hadoop.hive.ql.Driver.run(Driver.java:1227)
at org.apache.hadoop.hive.cli.CliDriver.processLocalCmd(CliDriver.java:233)
at org.apache.hadoop.hive.cli.CliDriver.processCmd(CliDriver.java:184)
at org.apache.hadoop.hive.cli.CliDriver.processLine(CliDriver.java:403)
at org.apache.hadoop.hive.cli.CliDriver.executeDriver(CliDriver.java:821)
at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:759)
at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:686)
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:244)
at org.apache.hadoop.util.RunJar.main(RunJar.java:158)
FAILED: ParseException line 1:95 cannot recognize input near ''select date_sub(date '' '2019' '-' in constant

Any idea how to resolve this? while I try it like this it is working fine:

 alter table table_name drop if exists partition (data_date<'2019-10-02');

But I want to use sub_date as shown above. Does hive support storing query result into a variables?

Alan
  • 417
  • 1
  • 7
  • 22
  • https://stackoverflow.com/a/42958406/6336479 – David דודו Markovitz Jan 10 '20 at 19:29
  • As I mentioned in the question I used the same method in that link that you shared. But when i want to use it in another query i get that error. Is there a way in hive to pass the value instead that is calculated in a previous query? – Alan Jan 10 '20 at 20:21
  • 4
    The link demonstrates that what is being assigned to a variable is a text, not a query/expression result. You `alter` command actually looks like `alter table rerank_session_features drop if exists partition (data_date<'select date_sub(date '2019-10-21',19)');`. Clearly this is going to fail. You will have to run 2 separate queries (calling the hive shell twice) - one to calculate the date and the other to perform the partitions dropping based on the date you will pass to that query/ – David דודו Markovitz Jan 10 '20 at 21:55

0 Answers0