For more than six years, Apache Impala was how people at work queried our Hadoop cluster interactively: dashboards, internal reporting tools, ad hoc analysis. This year we built a new cluster, and Impala didn’t come with us. We replaced it with Trino. This post covers why, and the differences between the two SQL dialects that people ran into while moving their queries.
Why Impala had to go
Impala came with our Hadoop distribution, Cloudera’s CDH, so it cost us nothing to start using. That stopped being true in stages. After Cloudera and Hortonworks merged, their free distributions were replaced by CDP, which needs a commercial licence. Then in February Cloudera put the downloads for older CDH versions behind a paywall too, including the versions we were running. Even with a licence, CDP didn’t include everything we use, such as Airflow. So the new cluster runs on Apache Bigtop, and every component that came bundled with CDH had to justify itself again.
Impala had a hard time doing that:
- No Debian support. All our servers run Debian, which Impala doesn’t officially support. It can be built for it, but we’d be on our own.
- Complex types work differently. Impala queries maps and arrays with a join-like syntax that Hive and Spark SQL don’t use, so those queries don’t port, and our BI tool couldn’t explore map columns through Impala at all.
- In-memory, all-or-nothing execution. Most of the work happens in memory, which limits how much data a single query can handle, and a failure anywhere means re-running the whole query.
- A second set of metadata. Impala caches table metadata on top of the Hive metastore, so every table changed by Hive or Spark needs a refresh. That mostly worked, and occasionally didn’t.
- UDFs in two languages. Our UDFs were written in Java for Hive and Spark, but Impala UDFs that take complex types have to be written in C++.
- Fixed-size daemons. Each Impala daemon starts with a fixed amount of memory, which handles bursty workloads poorly.
Whatever replaced it had to query data where it already was in HDFS, use the tables and statistics in the Hive metastore, speak a dialect close to HiveQL and Spark SQL, and be fast enough for dashboards. The candidates on our list were Hive on LLAP or Tez, Spark SQL’s Thrift server and Presto. We went with Trino, the fork of Presto led by its original authors, and a colleague had it running on the new cluster by September.
One thing made the migration much easier than it could have been: Trino on the new cluster could also read the old cluster’s tables through a second catalog. People could run a query in Impala and the same query in Trino against the same data, and compare the results before switching anything.
Dialect differences
Trino sticks closely to standard SQL. Impala doesn’t, so most of the work was in places where Impala had been lenient. This is the list we collected while teams migrated their queries.
Quoting and types
- String literals take single quotes only.
col = "value"becomescol = 'value'. - Identifiers are quoted with double quotes, not backticks:
`timestamp`becomes"timestamp". - There is no
stringtype.CAST(x AS string)becomesCAST(x AS varchar), andCAST(x AS float)becomesCAST(x AS double). - No implicit conversions. Dates stored as strings need an explicit
CAST(x AS timestamp)before comparing them with timestamps, and booleans have to be cast to integers before you canSUMthem. - Join hints like
[SHUFFLE]aren’t supported. Remove them.
GROUP BY and HAVING
Trino doesn’t accept column aliases from the SELECT list in GROUP BY or HAVING. Ordinals still work in GROUP BY and ORDER BY, but not in HAVING, where you repeat the expression:
-- Impala
SELECT lower(country) AS country, count(*) AS cnt
FROM visits
GROUP BY country
HAVING country IN ('in', 'us');
-- Trino
SELECT lower(country) AS country, count(*) AS cnt
FROM visits
GROUP BY 1
HAVING lower(country) IN ('in', 'us');
For distinct counts over several columns, COUNT(DISTINCT a, b) becomes COUNT(DISTINCT ROW(a, b)). Check how your data handles NULLs here: a row with a NULL field is still a distinct value in Trino.
Anti and semi joins
There’s no LEFT ANTI JOIN or LEFT SEMI JOIN syntax. Rewrite anti joins as NOT EXISTS, or as an outer join that keeps the unmatched rows:
-- Impala
SELECT e.event_name
FROM events e
LEFT ANTI JOIN spammers s ON s.user_id = e.user_id;
-- Trino
SELECT e.event_name
FROM events e
LEFT JOIN spammers s ON s.user_id = e.user_id
WHERE s.user_id IS NULL;
Semi joins become EXISTS or IN (SELECT ...).
Maps and arrays
This was the most welcome change. Getting a value out of a map column in Impala means listing the column in the FROM clause as if it were a table and matching the key in WHERE. In Trino it’s a function call:
SELECT element_at(properties, 'post_type') AS post_type
FROM events;
element_at works for arrays and maps and returns NULL for a missing key. The subscript operator, properties['post_type'], is shorter but fails the query if the key is missing, so use it only when you know the key is always there.
Functions
trunc(ts, 'MM')becomesdate_trunc('month', ts).datediff(end, start)becomesdate_diff('day', start, end). Note the argument order, and that Impala’s version only looks at the date part while Trino’s works at whatever unit you ask for.- There’s no
date_sub.date_sub(d, 1)becomesdate_add('day', -1, d). last_day(d)becomeslast_day_of_month(d), andto_date(ts)becomesdate(ts).dayofweek(d)becomesday_of_week(d), and the numbering changes: Impala counts 1 to 7 from Sunday, Trino from Monday.nvl(x, y),isnull(x, y)andzeroifnull(x)all becomecoalesce.- Timestamps come back with milliseconds, which can break code that compares them as strings.
Metadata
SHOW COLUMN STATS tbecomesSHOW COLUMNS FROM tfor listing columns, orSHOW STATS FOR tfor statistics.SHOW PARTITIONS tbecomes a query against the hidden partitions table:SELECT * FROM "t$partitions".- There’s no
REFRESHorINVALIDATE METADATA. Trino reads the Hive metastore directly, so data written by Spark is visible as soon as it’s registered there.
That last point removes a step that occasionally failed with Impala. And the side-by-side catalog meant every migrated query could be checked against the old results before anyone depended on it. Trino’s own guide to migrating from Hive covers more of the same ground.
Leave a Reply