Question

I am trying to build some hive (0.9) queries from an application which allows users to specify column name aliases.

I found that whenever the column alias is two characters long and is a digit followed by 'b' or 'B' then it gives the error: "cannot recognize input near 'AS' '2b' ',' in selection target"

Queries that don't work:

select t1.emp_id as 1b from someTable t1;
select t1.emp_id as 2b from someTable t1;
select t1.emp_id as 1B from someTable t1;

Queries that work:

select t1.emp_id as 1b1 from someTable t1;
select t1.emp_id as 1ba from someTable t1;
select t1.emp_id as 1c from someTable t1;

Is this a known issue in Hive? I could not find any JIRA issues that relate to such errors.

Was it helpful?

Solution

OK, I'm pretty sure I found it. The hive parser is recognizing this as a ByteLengthLiteral which is defined as the following:

ByteLengthLiteral
    :
    (Digit)+ ('b' | 'B' | 'k' | 'K' | 'm' | 'M' | 'g' | 'G')
    ;

I tried the same statement replacing b with k, m, and g and got the same error.

And for the sake of information, an identifier (as [identifier]) is defined as...

Identifier
    :
    (Letter | Digit) (Letter | Digit | '_')*
    | '`' RegexComponent+ '`'
    ;

Which seems like it should work, except that the ByteLengthLiteral has already been processed and so this fails to recognize the identifier.

OTHER TIPS

climbage gave a great answer on why this is happening; here's an answer on how to work around it - just use backticks, like so:

select t1.emp_id as `1b` from someTable t1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top