Amazon Athena: no viable alternative at input

Amazon Web-ServicesAmazon Athena

Amazon Web-Services Problem Overview


While creating a table in Athena; it gives me following exception:

> no viable alternative at input

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

hyphens are not allowed in table name.. ( though wizard allows it ) .. Just remove hyphen and it works like a charm

Solution 2 - Amazon Web-Services

Unfortunately, at the moment the syntax validation error messages are not very descriptive in Athena, this error may mean "almost" any possible syntax errors on the create table statement.

Although this is annoying at the moment you will need to check if the syntax follows the Create table documentation

Some examples are:

  • Backticks not in place (as already pointed out)
  • Missing/extra commas (remember that the last column doesn't need the comma after column definition
  • Missing spaces
  • More ..

Solution 3 - Amazon Web-Services

This error generally occurs when the syntax of DDL has some silly errors.There are several answers that explain different errors based on there state.The simple solution to this problem is to patiently look into DDL and verify following points line by line:-

  1. Check for missing commas
  2. Unbalanced `(backtick operator)
  3. Incompatible datatype not supported by HIVE(HIVE DATA TYPES REFERENCE)
  4. Unbalanced comma
  5. Hypen in table name

Solution 4 - Amazon Web-Services

In my case, it was because of a trailing comma after the last column in the table. For example:

CREATE EXTERNAL TABLE IF NOT EXISTS my_table (
  one STRING,
  two STRING,
) LOCATION 's3://my-bucket/some/path';

After I removed the comma at the end of two STRING, it worked fine.

Solution 5 - Amazon Web-Services

My case: it was an external table and the location had a typo (hence didn't exist)

Couple of tips:

Solution 6 - Amazon Web-Services

Slashes. Mine was slashes. I had the DDL from Athena, saved as a python string.

WITH SERDEPROPERTIES ( 
  'escapeChar'='\\', 
  'quoteChar'='\"',
  'separatorChar'=',')  

was changed to

WITH SERDEPROPERTIES ( 
  'escapeChar'='\', 
  'quoteChar'='"',
  'separatorChar'=',')

And everything fell apart.

Had to make it:

WITH SERDEPROPERTIES (
  'escapeChar'='\\\\', 
  'quoteChar'='\\\"',
  'separatorChar'=',')

Solution 7 - Amazon Web-Services

In my case, it was an extra comma in PARTITIONED BY section,

Solution 8 - Amazon Web-Services

In my case, I was missing the singlequotes for the S3 URL

Solution 9 - Amazon Web-Services

In my case, it was that one of the table column names was enclosed in single quotes, as per the AWS documentation :( ('bucket')

Solution 10 - Amazon Web-Services

As other users have noted, the standard syntax validation error message that Athena provides is not particularly helpful. Thoroughly checking the required DDL syntax (see HIVE data types reference) that other users have mentioned can be pretty tedious since it is fairly extensive.

So, an additional troubleshooting trick is to let AWS's own data parsing engine (AWS Glue) give you a hint about where your DDL may be off. The idea here is to let AWS Glue parse the data using its own internal rules and then show you where you may have made your mistake.

Specifically, here are the steps that worked for me to troubleshoot my DDL statement, which was giving me lots of trouble:

  1. create a data crawler in AWS Glue; AWS and lots of other places go through the very detailed steps this requires so I won't repeat it here
  2. point the crawler to the same data that you wanted (but failed) to upload into Athena
  3. set the crawler output to a table (in an Athena database you've already created)
  4. run the crawler and wait for the table with populated data to be created
  5. find the newly-created table in the Athena Query Editor tab, click on the three vertical dots (...), and select "Generate Create Table DLL": enter image description here
  6. this will make Athena create the DLL for this table that is guaranteed to be valid (since the table was already created using that DLL)
  7. take a look at this DLL and see if/where/how it differs from the DLL that you originally wrote. Naturally, this automatically-generated DLL will not have the exact choices for the data types that you may find useful, but at least you will know that it is 100% valid
  8. finally, update your DLL based on this new Glue/Athena-generated-DLL, adjusting the column/field names and data types for your particular use case

Solution 11 - Amazon Web-Services

After searching and following all the good answers here. My issue was that working in Node.js i needed to remove the optional ESCAPED BY '\' used in the Row settings to get my query to work. Hope this helps others.

Solution 12 - Amazon Web-Services

Something that wasn't obvious for me the first time I used the UI is that if you get an error in the create table 'wizard', you can then cancel and there should be the query used that failed written in a new query window, for you to edit and fix.

My database had a hypen, so I added backticks in the query and rerun it.

Solution 13 - Amazon Web-Services

This happened to me due to having comments in the query.

I realized this was a possibility when I tried the "Format Query" button and it turned the entire thing into almost 1 line, mostly commented out. My guess is that the query parser runs this formatter before sending the query to Athena.

Removed the comments, ran the query, and an angel got its wings!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak SinghalView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesDeepak SinghalView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesesalgadoView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesNitin VashisthView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesRalph BoltonView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesE-douView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesJason NicholsView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesSiva BudatiView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesAkshay KalbhorView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesPhilView Answer on Stackoverflow
Solution 10 - Amazon Web-Servicesdrapkin11View Answer on Stackoverflow
Solution 11 - Amazon Web-ServicesMecclesView Answer on Stackoverflow
Solution 12 - Amazon Web-ServicesjavsView Answer on Stackoverflow
Solution 13 - Amazon Web-ServicesGrant GView Answer on Stackoverflow