

# Providing Ad Hoc Query Capabilities

Many organizations migrating from on-premises Hadoop rely heavily on interactive, ad hoc query capabilities for data exploration, business intelligence, and operational analytics. This chapter covers how to replicate and improve those capabilities on AWS, including considerations for Amazon Athena (serverless SQL), Trino and Presto on Amazon EMR (distributed query engines), HBase workloads (see [HBase Workloads on Amazon EMR](hbase-workloads.md)), and migrating from Apache Impala (see [Migrating Apache Impala](migrating-apache-impala.md)). The goal is to provide your analysts and data scientists with faster, more cost-effective interactive query experiences after migration.

## Considerations for Amazon Athena, Trino and Presto
<a name="athena-trino-presto-considerations"></a>

Amazon Athena is a serverless interactive query engine that runs SQL queries on data in Amazon S3. Customers use it for interactive querying, exploring data, powering dashboards on operational metrics in Amazon S3, and powering visualization tools such as AWS Quick or Tableau. We strongly recommend considering Amazon Athena for these workloads. It is easy to integrate, offers cost management, workgroup-level controls, and fine-grained security, and requires little capacity planning, which lowers operational burden and cost.

The Amazon EMR documentation (Amazon EMR version 6.4.0 and later) recommends Trino, the open-source successor to PrestoSQL, over PrestoDB for new deployments. Presto (PrestoDB) is still available with Amazon EMR, but AWS recommends Trino going forward, because Trino receives feature and performance updates on Amazon EMR.

However, some use cases suit PrestoDB or Trino on Amazon EMR better. Consider the following priorities:
+ Cost optimization. If cost is the primary goal, run a quick proof-of-concept that estimates cost on both approaches with representative workloads. Some load and query patterns are cheaper on Presto or Trino on Amazon EMR, and others benefit from the serverless pricing of Amazon Athena.
+ Performance requirements. If your workload is highly performance-sensitive, you can fine-tune a Trino or Presto cluster with dedicated hardware, custom memory allocations, and query-specific tuning that is not available in a fully managed serverless service. Amazon Athena Provisioned Capacity offers dedicated processing capacity with consistent performance and might address some of these needs without managing Amazon EMR clusters. Evaluate it first.
+ Critical features. Consider Trino or Presto on Amazon EMR for features that Amazon Athena does not currently provide, such as custom serializers and deserializers for custom data types, connectors to unsupported data stores, or specific Trino or Presto SQL extensions.
+ Data locality or hybrid requirements. Consider Trino or Presto on Amazon EMR for co-located compute and storage, non-Amazon S3 data sources, or strict latency SLAs that benefit from cluster tuning.

For performance tips, see the AWS Big Data Blog posts [Best practices for Trino on Amazon EMR](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-trino-advanced.html), [Run Trino queries 2.7 times faster with Amazon EMR](https://aws.amazon.com/blogs/big-data/run-trino-queries-2-7-times-faster-with-amazon-emr-6-15-0/), and [Optimize Athena performance](https://docs.aws.amazon.com/athena/latest/ug/performance-tuning.html).


**Comparison at a glance**  

| Criteria | Amazon Athena | Trino or PrestoDB on Amazon EMR | 
| --- | --- | --- | 
| Infrastructure | Serverless, fully managed | Self-managed cluster on Amazon EMR | 
| Cost model | Pay per query (per TB scanned) | Pay per Amazon EC2 instance-hour; can use Spot | 
| Scalability | Auto-scaling, no cluster management | Manual or auto-scaling, Instance Fleets | 
| Performance tuning | Limited; managed by AWS | Full configuration control | 
| Custom connectors | Amazon Athena federated query support | Full Trino or Presto connector ecosystem | 
| Best for | Exploratory analytics, dashboards, low overhead | High-performance, complex workloads, custom tuning | 

### Trino migration from PrestoDB and tuning
<a name="trino-migration-tuning"></a>

#### Migration
<a name="trino-migration"></a>

On Amazon EMR, PrestoDB and Trino both use the same command line executable (`presto-cli`) and share the same default web interface port (`8889`), which simplifies client-side migration. However, you cannot run PrestoDB and Trino at the same time on the same cluster. Create a new Amazon EMR cluster with Trino installed.

Plan for the following key changes:
+ Update Amazon EMR configuration classifications from `presto-config` and `presto-connector-hive` to `trino-config` and `trino-connector-hive`.
+ Update client JDBC connection URLs from `jdbc:presto:` to `jdbc:trino:`. During the transition, add `protocol.v1.alternate-header-name=Presto` to your Trino configuration to allow older Presto clients to connect while you migrate them. This property is not available on Amazon EMR 7.x, so plan to update all clients before you update.
+ Update JMX-based monitoring references from `presto` to `trino` namespaces.
+ Review custom connector configurations, because the Trino connector ecosystem has diverged from PrestoDB.
+ Validate your SQL queries and functions, because some functions and syntax have changed since the fork.

If you use the AWS Glue Data Catalog as your Hive metastore, Trino supports it the same way PrestoDB does, so no metastore migration is needed. Run your representative query workload on both engines in parallel before you cut over.

#### Tuning
<a name="trino-tuning"></a>

On-premises Presto clusters are typically right-sized through tribal knowledge over time, and Amazon EMR defaults might not match your workload. Focus tuning on three areas:
+ Memory. Set `query.max-memory` and `query.max-memory-per-node` based on your heaviest queries, and ensure that the JVM heap size exceeds the sum of `query.max-memory-per-node` and `memory.heap-headroom-per-node`. If memory-intensive queries that succeeded on-premises now fail, enable spill-to-disk with `spill-enabled=true` as a safety net. Right-sizing memory is preferred, because spilled queries run slower.
+ Data layout. Queries against Amazon S3 benefit from the columnar formats Parquet and ORC with appropriate partitioning. Re-evaluate partition keys if your on-premises data used HDFS with different partitioning.
+ Instance selection and scaling. Use memory-optimized Graviton instances such as r7g or r8g for Trino coordinator and worker nodes, because Trino is memory-intensive. Use Amazon EMR managed scaling to handle variable query concurrency instead of over-provisioning a fixed cluster.

For the complete tuning parameters, including dynamic filtering and table statistics, see [Best practices for Trino on Amazon EMR](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-trino-advanced.html) and [Run Trino queries 2.7x faster with Amazon EMR](https://aws.amazon.com/blogs/big-data/run-trino-queries-2-7-times-faster-with-amazon-emr-6-15-0/).

## Metadata management
<a name="metadata-management"></a>

### AWS Glue Data Catalog as the Hive metastore
<a name="adhoc-glue-data-catalog-metastore"></a>

Starting with Amazon EMR release 5.10.0, Amazon EMR can use the AWS Glue Data Catalog as the default Hive metastore for Presto and Trino. When you use it, the authorization mechanism (such as Hive SQL authorization) is replaced with AWS IAM-based policies and AWS Glue resource policies, which centralizes access governance across services and query engines that share the same catalog. You are also required to separately secure the underlying data in Amazon S3, using an Amazon S3 bucket policy or an IAM policy. IAM policies might be more efficient, because they centralize access control for both Amazon S3 and the AWS Glue Data Catalog from one policy framework.

Key benefits include the following:
+ Centralized schema registry. All Amazon EMR applications, including Spark, Hive, Presto, and Trino, share one consistent catalog view.
+ Automatic schema discovery. AWS Glue crawlers detect and register new datasets in Amazon S3.
+ Cross-account access. Resource-based policies enable catalog sharing across your organization.
+ Reduced operational burden. Using the AWS Glue Data Catalog eliminates provisioning and maintaining a separate Hive metastore database such as Amazon RDS MySQL or Amazon Aurora.

For more information, see the following documentation:
+ [Using Presto with the AWS Glue Data Catalog](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-presto-glue.html)
+ [Easily manage table metadata for Presto running on Amazon EMR using the AWS Glue Data Catalog](https://aws.amazon.com/blogs/big-data/easily-manage-table-metadata-for-presto-running-on-amazon-emr-using-the-aws-glue-data-catalog/)
+ [Configuring Trino on Amazon EMR](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-trino-config.html)
+ [Launch an Amazon EMR cluster with Trino](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-trino-getting-started-launch.html)
+ [Connect to the primary node for the Amazon EMR cluster and run queries](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-trino-getting-started-connect.html)
+ [Using the AWS Glue Data Catalog as the metastore for Hive](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-hive-metastore-glue.html)

### EMRFS, EMR S3A, and PrestoS3FileSystem configuration
<a name="emrfs-s3a-prestos3filesystem"></a>

By default, Presto running on Amazon EMR release versions 5.12.0 through 7.9 uses EMRFS to access data on Amazon S3. Starting with Amazon EMR 7.10, all engines, including Presto and Trino, use EMR S3A as the default Amazon S3 connector. Presto on earlier versions uses PrestoS3FileSystem, a component of the Hive connector. Accessing data through EMRFS lets you configure Amazon S3 encryption requirements through an Amazon EMR security configuration and use separate IAM roles for fine-grained access by user, group, or Amazon S3 location.

Amazon EMR is transitioning from EMRFS to EMR S3A as the default file system connector starting with Amazon EMR 7.10. EMR S3A achieves performance parity with EMRFS and delivers up to 65% lower latency than open-source S3A on read-heavy and mixed workloads. The transition is seamless, because Amazon EMR automatically maps EMRFS configurations to S3A equivalents when S3A configurations are not explicitly set (see [Migration of Existing EMRFS Configurations to S3A Configurations](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-s3a-migrate.html)). For Amazon EMR 5.12.0 or later, you can still switch from EMRFS to PrestoS3FileSystem if needed, which might benefit organizations that continue to rely on Hive SQL-based authorization with an RDBMS-backed Hive metastore.

For Amazon S3 encryption configuration (SSE-S3, SSE-KMS, and CSE-KMS or CSE-C) when you use EMRFS or S3A with Presto or Trino, see the Encryption section in [Securing your Resources on Amazon EMR](securing-resources.md).

## Additional migration considerations
<a name="additional-migration-considerations"></a>

### Iceberg table support across query engines
<a name="iceberg-query-engine-support"></a>

If you are adopting Apache Iceberg (see the Choosing a table format for your migration section in [Incremental Data Processing](incremental-data-processing.md)), ensure that your ad hoc query engine supports it.


**Iceberg support by query engine**  

| Engine | Iceberg support | Migration note | 
| --- | --- | --- | 
| Amazon Athena | Native. Reads Iceberg tables registered in the AWS Glue Data Catalog. | No cluster to manage; ideal for ad hoc exploration of migrated Iceberg tables. | 
| Trino on Amazon EMR | Native. Available through the Iceberg connector and the AWS Glue Data Catalog. | Full DML support (INSERT, UPDATE, DELETE, MERGE); use for heavy interactive workloads. | 
| PrestoDB on Amazon EMR | Limited. Read-only Iceberg support. | If you are migrating from Presto, we recommend switching to Trino for full Iceberg write support. | 

All three engines support time travel queries, schema evolution, and hidden partition pruning on Iceberg tables, which are capabilities that are not available when querying plain Hive tables on-premises.

### Access control: AWS Lake Formation replaces Ranger for query engines
<a name="lake-formation-access-control"></a>

On-premises, Apache Ranger (or Apache Sentry) provided column and row-level access control for Presto, Hive, and Impala. On AWS, AWS Lake Formation provides equivalent fine-grained access control for both Amazon Athena and Trino on Amazon EMR. It is centrally managed, cross-engine, and integrated with the AWS Glue Data Catalog.

**Note**  
Trino hosted on Amazon EMR does not support Trusted Identity Propagation (TIP) directly. If you need TIP-based user-level access control for interactive SQL queries, use Amazon Athena, which uses Trino under the hood and supports TIP natively, instead of running Trino on an Amazon EMR cluster.

When you migrate interactive query workloads, map your existing Ranger policies to AWS Lake Formation permissions instead of deploying Ranger on Amazon EMR. For more information, see [Securing your Resources on Amazon EMR](securing-resources.md).

### Amazon EMR Serverless for ad hoc Trino workloads
<a name="emr-serverless-trino"></a>

For ad hoc query workloads with unpredictable usage, consider Trino on Amazon EMR Serverless as an alternative to a persistent Trino cluster:
+ Use Amazon EMR Serverless when query volume is sporadic, you want zero idle cost, and sub-minute cold-start latency is acceptable.
+ Use a persistent Amazon EMR Trino cluster when you need consistent sub-second response times, BI tool connection pooling, or high-concurrency interactive sessions.

For deployment guidance, see [Using Trino on Amazon EMR Serverless](https://docs.aws.amazon.com/emr/latest/EMR-Serverless-UserGuide/emr-serverless.html).

### Querying on-premises data during transition
<a name="querying-on-premises-data"></a>

During migration, you might need to query data that is not yet moved to Amazon S3. Amazon Athena Federated Query provides JDBC connectors that can query on-premises databases, Hadoop clusters (through the Hive connector), and other data sources directly from Amazon Athena without migrating the data first. This enables a gradual migration, where analysts use a single query engine (Amazon Athena) while data moves from on-premises to Amazon S3 in phases.

For federated query architecture patterns and recommended migration phasing, see the Zero-ETL and federated query section in [Data Migration](data-migration.md).