Impala-3 3 PDF
Impala-3 3 PDF
| Contents | ii
Contents
Installing Impala..................................................................................................... 24
What is Included in an Impala Installation........................................................................................................25
Managing Impala....................................................................................................25
Post-Installation Configuration for Impala.........................................................................................................25
Upgrading Impala...................................................................................................27
Upgrading Impala............................................................................................................................................... 27
Impala Upgrade Considerations......................................................................................................................... 28
Grant REFRESH Privilege to Impala Roles with SELECT or INSERT Privilege when Upgrading
to Impala 3.0..................................................................................................................................... 28
List of Reserved Words Updated in Impala 3.0.................................................................................... 28
Decimal V2 Used by Default in Impala 3.0.......................................................................................... 28
Behavior of Column Aliases Changed in Impala 3.0............................................................................ 28
Default PARQUET_ARRAY_RESOLUTION Changed in Impala 3.0................................................ 28
| Contents | iii
Starting Impala....................................................................................................... 30
Starting Impala from the Command Line.......................................................................................................... 30
Modifying Impala Startup Options.................................................................................................................... 31
Configuring Impala Startup Options through the Command Line.........................................................31
Checking the Values of Impala Configuration Options.........................................................................33
Startup Options for catalogd Daemon....................................................................................................34
Impala Tutorials..................................................................................................... 34
Tutorials for Getting Started.............................................................................................................................. 34
Explore a New Impala Instance............................................................................................................. 34
Load CSV Data from Local Files.......................................................................................................... 39
Point an Impala Table at Existing Data Files........................................................................................ 41
Describe the Impala Table......................................................................................................................43
Query the Impala Table..........................................................................................................................44
Data Loading and Querying Examples.................................................................................................. 44
Advanced Tutorials.............................................................................................................................................46
Attaching an External Partitioned Table to an HDFS Directory Structure............................................ 46
Switching Back and Forth Between Impala and Hive...........................................................................49
Cross Joins and Cartesian Products with the CROSS JOIN Operator...................................................49
Dealing with Parquet Files with Unknown Schema.......................................................................................... 52
Download the Data Files into HDFS..................................................................................................... 52
Create Database and Tables................................................................................................................... 52
Examine Physical and Logical Schema................................................................................................. 53
Analyze Data...........................................................................................................................................55
Impala Administration........................................................................................... 64
Setting Timeout Periods for Daemons, Queries, and Sessions..........................................................................65
Increasing the Statestore Timeout.......................................................................................................... 65
Setting the Idle Query and Idle Session Timeouts for impalad............................................................. 65
Setting Timeout and Retries for Thrift Connections to the Backend Client.......................................... 66
Cancelling a Query................................................................................................................................. 66
Using Impala through a Proxy for High Availability........................................................................................ 67
Overview of Proxy Usage and Load Balancing for Impala...................................................................67
Choosing the Load-Balancing Algorithm.............................................................................................. 68
Special Proxy Considerations for Clusters Using Kerberos.................................................................. 68
Special Proxy Considerations for TLS/SSL Enabled Clusters...............................................................69
Example of Configuring HAProxy Load Balancer for Impala.............................................................. 70
Managing Disk Space for Impala Data..............................................................................................................72
Impala Security....................................................................................................... 73
Security Guidelines for Impala.......................................................................................................................... 74
Securing Impala Data and Log Files................................................................................................................. 75
Installation Considerations for Impala Security.................................................................................................75
| Contents | iv
How Admission Control works with Impala Clients (JDBC, ODBC, HiveServer2)........................... 656
SQL and Schema Considerations for Admission Control....................................................................656
Guidelines for Using Admission Control.............................................................................................657
Configuring Admission Control....................................................................................................................... 657
Configuring Admission Control in Command Line Interface............................................................. 658
Configuring Cluster-wide Admission Control..................................................................................... 660
Using Impala with the Azure Data Lake Store (ADLS)................................... 740
Prerequisites...................................................................................................................................................... 740
How Impala SQL Statements Work with ADLS.............................................................................................740
Specifying Impala Credentials to Access Data in ADLS................................................................................ 741
Loading Data into ADLS for Impala Queries................................................................................................. 742
Using Impala DML Statements for ADLS Data..................................................................................742
Manually Loading Data into Impala Tables on ADLS........................................................................742
Creating Impala Databases, Tables, and Partitions for Data Stored on ADLS................................................742
Internal and External Tables Located on ADLS..............................................................................................744
Running and Tuning Impala Queries for Data Stored on ADLS.................................................................... 746
Understanding and Tuning Impala Query Performance for ADLS Data.............................................747
Restrictions on Impala Support for ADLS...................................................................................................... 747
Best Practices for Using Impala with ADLS...................................................................................................748
Impala Benefits
Impala provides:
• Familiar SQL interface that data scientists and analysts already know.
• Ability to query high volumes of data (“big data”) in Apache Hadoop.
• Distributed queries in a cluster environment, for convenient scaling and to make use of cost-effective commodity
hardware.
• Ability to share data files between different components with no copy or export/import step; for example, to write
with Pig, transform with Hive and query with Impala. Impala can read from and write to Hive tables, enabling
simple data interchange using Impala for analytics on Hive-produced data.
• Single system for big data processing and analytics, so customers can avoid costly modeling and ETL just for
analytics.
In Impala 2.9 and higher, you can control which hosts act as query coordinators and which act as query executors, to
improve scalability for highly concurrent workloads on large clusters. See How to Configure Impala with Dedicated
Coordinators on page 642 for details.
Related information: Modifying Impala Startup Options on page 31, Starting Impala on page 30, Setting
the Idle Query and Idle Session Timeouts for impalad on page 65, Ports Used by Impala on page 781, Using
Impala through a Proxy for High Availability on page 67
• If set to false, the metadata of a table is loaded when it is referenced for the first time. This means that
the first run of a particular query can be slower than subsequent runs. Starting in Impala 2.2, the default for
‑‑load_catalog_in_background is false.
• If set to true, the catalog service attempts to load metadata for a table even if no query needed that metadata. So
metadata will possibly be already loaded when the first query that would need it is run. However, for the following
reasons, we recommend not to set the option to true.
• Background load can interfere with query-specific metadata loading. This can happen on startup or after
invalidating metadata, with a duration depending on the amount of metadata, and can lead to a seemingly
random long running queries that are difficult to diagnose.
• Impala may load metadata for tables that are possibly never used, potentially increasing catalog size and
consequently memory usage for both catalog service and Impala Daemon.
Most considerations for load balancing and high availability apply to the impalad daemon. The statestored
and catalogd daemons do not have special requirements for high availability, because problems with those
daemons do not result in data loss. If those daemons become unavailable due to an outage on a particular host, you
can stop the Impala service, delete the Impala StateStore and Impala Catalog Server roles, add the roles on a
different host, and restart the Impala service.
Note:
In Impala 1.2.4 and higher, you can specify a table name with INVALIDATE METADATA after the table is created
in Hive, allowing you to make individual tables visible to Impala without doing a full reload of the catalog metadata.
Impala 1.2.4 also includes other changes to make the metadata broadcast mechanism faster and more responsive,
especially during Impala startup. See New Features in Impala 1.2.4 on page 820 for details.
Related information: Modifying Impala Startup Options on page 31, Starting Impala on page 30, Ports
Used by Impala on page 781
For users coming to Impala from traditional database or data warehousing backgrounds, the following aspects of the
SQL dialect might require some learning and practice for you to become proficient in the Hadoop environment:
• Impala SQL is focused on queries and includes relatively little DML. There is no UPDATE or DELETE statement.
Stale data is typically discarded (by DROP TABLE or ALTER TABLE ... DROP PARTITION statements) or
replaced (by INSERT OVERWRITE statements).
• All data creation is done by INSERT statements, which typically insert data in bulk by querying from other tables.
There are two variations, INSERT INTO which appends to the existing data, and INSERT OVERWRITE which
replaces the entire contents of a table or partition (similar to TRUNCATE TABLE followed by a new INSERT).
Although there is an INSERT ... VALUES syntax to create a small number of values in a single statement, it is
far more efficient to use the INSERT ... SELECT to copy and transform large amounts of data from one table
to another in a single operation.
• You often construct Impala table definitions and data files in some other environment, and then attach Impala so
that it can run real-time queries. The same data files and table metadata are shared with other components of the
Hadoop ecosystem. In particular, Impala can access tables created by Hive or data inserted by Hive, and Hive can
access tables and data produced by Impala. Many other Hadoop components can write files in formats such as
Parquet and Avro, that can then be queried by Impala.
• Because Hadoop and Impala are focused on data warehouse-style operations on large data sets, Impala SQL
includes some idioms that you might find in the import utilities for traditional database systems. For example, you
can create a table that reads comma-separated or tab-separated text files, specifying the separator in the CREATE
TABLE statement. You can create external tables that read existing data files but do not move or transform them.
• Because Impala reads large quantities of data that might not be perfectly tidy and predictable, it does not require
length constraints on string data types. For example, you can define a database column as STRING with unlimited
length, rather than CHAR(1) or VARCHAR(64). (Although in Impala 2.0 and later, you can also use length-
constrained CHAR and VARCHAR types.)
Related information: Impala SQL Language Reference on page 94, especially Impala SQL Statements on page
205 and Impala Built-In Functions on page 412
In particular, Impala keeps its table definitions in a traditional MySQL or PostgreSQL database known as the
metastore, the same database where Hive keeps this type of data. Thus, Impala can access tables defined or loaded by
Hive, as long as all columns use Impala-supported data types, file formats, and compression codecs.
The initial focus on query features and performance means that Impala can read more types of data with the SELECT
statement than it can write with the INSERT statement. To query data using the Avro, RCFile, or SequenceFile file
formats, you load the data using Hive.
The Impala query optimizer can also make use of table statistics and column statistics. Originally, you gathered this
information with the ANALYZE TABLE statement in Hive; in Impala 1.2.2 and higher, use the Impala COMPUTE
STATS statement instead. COMPUTE STATS requires less setup, is more reliable, and does not require switching
back and forth between impala-shell and the Hive shell.
Impala Requirements
To perform as expected, Impala depends on the availability of the software, hardware, and configurations described in
the following sections.
Java Dependencies
Although Impala is primarily written in C++, it does use Java to communicate with various Hadoop components:
• The officially supported JVM for Impala is the Oracle JVM. Other JVMs might cause issues, typically resulting in
a failure at impalad startup. In particular, the JamVM used by default on certain levels of Ubuntu systems can
cause impalad to fail to start.
• Internally, the impalad daemon relies on the JAVA_HOME environment variable to locate the system Java
libraries. Make sure the impalad service is not run from an environment with an incorrect setting for this
variable.
• All Java dependencies are packaged in the impala-dependencies.jar file, which is located at /usr/
lib/impala/lib/. These map to everything that is built under fe/target/dependency.
the Impala daemon's hostname flag resolves to the IP address of the DataNode. For single-homed machines, this is
usually automatic, but for multi-homed machines, ensure that the Impala daemon's hostname resolves to the correct
interface. Impala tries to detect the correct hostname at start-up, and prints the derived hostname at the start of the log
in a message of the form:
In the majority of cases, this automatic detection works correctly. If you need to explicitly set the hostname, do so by
setting the --hostname flag.
Hardware Requirements
The memory allocation should be consistent across Impala executor nodes. A single Impala executor with a lower
memory limit than the rest can easily become a bottleneck and lead to suboptimal performance.
This guideline does not apply to coordinator-only nodes.
groups for that user. Impala Security on page 73 for details. These security features do not change the underlying
file permission requirements; the impala user still needs to be able to access the data files.
For example, consider a Parquet table where each data file is 1 HDFS block, with a maximum block size of 1 GB.
(In Impala 2.0 and later, the default Parquet block size is reduced to 256 MB. For this exercise, let's assume you have
bumped the size back up to 1 GB by setting the query option PARQUET_FILE_SIZE=1g.) if you have a 10-node
cluster, you need 10 data files (up to 10 GB) to give each node some work to do for a query. But each core on each
machine can process a separate data block in parallel. With 16-core machines on a 10-node cluster, a query could
process up to 160 GB fully in parallel. If there are only a few data files per partition, not only are most cluster nodes
sitting idle during queries, so are most cores on those machines.
You can reduce the Parquet block size to as low as 128 MB or 64 MB to increase the number of files per partition and
improve parallelism. But also consider reducing the level of partitioning so that analytic queries have enough data to
work with.
Installing Impala
Impala is an open-source analytic database for Apache Hadoop that returns rapid responses to queries.
Follow these steps to set up Impala on a cluster by building from source:
• Download the latest release. See the Impala downloads page for the link to the latest release.
• Check the README.md file for a pointer to the build instructions.
• Please check the MD5 and SHA1 and GPG signature, the latter by using the code signing keys of the release
managers.
• Developers interested in working on Impala can clone the Impala source repository:
Managing Impala
This section explains how to configure Impala to accept connections from applications that use popular programming
APIs:
• Post-Installation Configuration for Impala on page 25
• Configuring Impala to Work with ODBC on page 765
• Configuring Impala to Work with JDBC on page 765
This type of configuration is especially useful when using Impala in combination with Business Intelligence tools,
which use these standard interfaces to query different kinds of database and Big Data systems.
You can also configure these other aspects of Impala:
• Impala Security on page 73
• Modifying Impala Startup Options on page 31
<property>
<name>dfs.client.read.shortcircuit</name>
<value>true</value>
</property>
<property>
<name>dfs.domain.socket.path</name>
<value>/var/run/hdfs-sockets/dn</value>
</property>
<property>
<name>dfs.client.file-block-storage-locations.timeout.millis</name>
<value>10000</value>
</property>
3. If /var/run/hadoop-hdfs/ is group-writable, make sure its group is root.
Note: If you are also going to enable block location tracking, you can skip copying configuration files and
restarting DataNodes and go straight to Optional: Block Location Tracking. Configuring short-circuit reads and
block location tracking require the same process of copying files and restarting services, so you can complete that
process once when you have completed all configuration changes. Whether you copy files and restart services now
or during configuring block location tracking, short-circuit reads are not enabled until you complete those final
steps.
4. After applying these changes, restart all DataNodes.
<property>
<name>dfs.datanode.hdfs-blocks-metadata.enabled</name>
<value>true</value>
</property>
2. Copy the client core-site.xml and hdfs-site.xml configuration files from the Hadoop configuration
directory to the Impala configuration directory. The default Impala configuration location is /etc/impala/
conf.
3. After applying these changes, restart all DataNodes.
| Upgrading Impala | 27
Upgrading Impala
Upgrading Impala involves building or acquiring new Impala-related binaries, and then restarting Impala services.
Upgrading Impala
• Shut down all Impala-related daemons on all relevant hosts in the cluster:
1. Stop impalad on each Impala node in your cluster:
Restart the state store service before the Impala server service to avoid “Not connected” errors when you run
impala-shell.
2. Restart the Impala catalog service on whichever host it runs on in your cluster. Expect to see a process named
catalogd if the service started successfully.
3. Restart the Impala daemon service on each node in your cluster. Expect to see a process named impalad if
the service started successfully.
Note:
If the services did not start successfully (even though the sudo service command might display [OK]), check for
errors in the Impala log file, typically in /var/log/impala.
Grant REFRESH Privilege to Impala Roles with SELECT or INSERT Privilege when
Upgrading to Impala 3.0
To use the fine grained privileges feature in Impala 3.0, if a role has the SELECT or INSERT privilege on an object
in Impala before upgrading to Impala 3.0, grant that role the REFRESH privilege after the upgrade.
‑‑reserved_words_version=2.11.0
SET DECIMAL_V2=FALSE;
Return Type Changed for EXTRACT and DATE_PART Functions in Impala 3.0
The following changes were made to the EXTRACT and DATE_PART functions:
• The output type of the EXTRACT and DATE_PART functions was changed to BIGINT.
• Extracting the millisecond part from a TIMESTAMP returns the seconds component and the milliseconds
component. For example, EXTRACT (CAST('2006-05-12 18:27:28.123456789' AS
TIMESTAMP), 'MILLISECOND') will return 28123.
In Impala 3.3 and higher, a new startup flag, --accepted_client_cnxn_timeout, was added to control how
the server should treat new connection requests if we have run out of the configured number of server threads.
If --accepted_client_cnxn_timeout > 0, new connection requests are rejected after the specified
timeout.
If --accepted_client_cnxn_timeout=0, clients waits indefinitely to connect to Impala. You can use this
setting to restore the pre-Impala 3.3 behavior.
The default timeout is 5 minutes.
Starting Impala
To activate Impala if it is installed but not yet started:
1. Set any necessary configuration options for the Impala services. See Modifying Impala Startup Options on page
31 for details.
2. Start one instance of the Impala statestore. The statestore helps Impala to distribute work efficiently, and
to continue running in the event of availability problems for other Impala nodes. If the statestore becomes
unavailable, Impala continues to function.
3. Start one instance of the Impala catalog service.
4. Start the main Impala daemon services.
Once Impala is running, you can conduct interactive experiments using the instructions in Impala Tutorials on page
34 and try Using the Impala Shell (impala-shell Command) on page 754.
Start the Impala daemon services using a command similar to the following:
Note:
In Impala 2.5 and higher, Impala UDFs and UDAs written in C++ are persisted in the metastore database. Java UDFs
are also persisted, if they were created with the new CREATE FUNCTION syntax for Java UDFs, where the Java
function argument and return types are omitted. Java-based UDFs created with the old CREATE FUNCTION syntax
do not persist across restarts because they are held in the memory of the catalogd daemon. Until you re-create
such Java UDFs using the new CREATE FUNCTION syntax, you must reload those Java-based UDFs by running the
original CREATE FUNCTION statements again each time you restart the catalogd daemon. Prior to Impala 2.5 the
requirement to reload functions after a restart applied to both C++ and Java functions.
If any of the services fail to start, review:
• Reviewing Impala Logs on page 750
• Troubleshooting Impala on page 769
IMPALA_STATE_STORE_HOST=127.0.0.1
IMPALA_STATE_STORE_PORT=24000
IMPALA_BACKEND_PORT=22000
IMPALA_LOG_DIR=/var/log/impala
IMPALA_CATALOG_SERVICE_HOST=...
IMPALA_STATE_STORE_HOST=...
export IMPALA_STATE_STORE_ARGS=${IMPALA_STATE_STORE_ARGS:- \
-log_dir=${IMPALA_LOG_DIR} -state_store_port=${IMPALA_STATE_STORE_PORT}}
IMPALA_SERVER_ARGS=" \
-log_dir=${IMPALA_LOG_DIR} \
-catalog_service_host=${IMPALA_CATALOG_SERVICE_HOST} \
-state_store_port=${IMPALA_STATE_STORE_PORT} \
-state_store_host=${IMPALA_STATE_STORE_HOST} \
-be_port=${IMPALA_BACKEND_PORT}"
export ENABLE_CORE_DUMPS=${ENABLE_COREDUMPS:-false}
| Starting Impala | 32
To use alternate values, edit the defaults file, then restart all the Impala-related services so that the changes take
effect. Restart the Impala server using the following commands:
IMPALA_STATE_STORE_HOST=127.0.0.1
to:
IMPALA_STATE_STORE_HOST=192.168.0.27
• Catalog server address (including both the hostname and the port number). Update the value of the
IMPALA_CATALOG_SERVICE_HOST variable. Where practical, run the catalog server on the same host as
the statestore. In that recommended configuration, the impalad daemon cannot refer to the catalog server
using the loopback address. If the catalog service is hosted on a machine with an IP address of 192.168.0.27, add
the following line:
IMPALA_CATALOG_SERVICE_HOST=192.168.0.27:26000
export IMPALA_SERVER_ARGS=${IMPALA_SERVER_ARGS:- \
-log_dir=${IMPALA_LOG_DIR} \
-state_store_port=${IMPALA_STATE_STORE_PORT} \
-state_store_host=${IMPALA_STATE_STORE_HOST} \
-be_port=${IMPALA_BACKEND_PORT}}
to:
export IMPALA_SERVER_ARGS=${IMPALA_SERVER_ARGS:- \
-log_dir=${IMPALA_LOG_DIR} -state_store_port=
${IMPALA_STATE_STORE_PORT} \
| Starting Impala | 33
-state_store_host=${IMPALA_STATE_STORE_HOST} \
-be_port=${IMPALA_BACKEND_PORT} -mem_limit=70%}
You can specify the memory limit using absolute notation such as 500m or 2G, or as a percentage of physical
memory such as 60%.
Note: Queries that exceed the specified memory limit are aborted. Percentage limits are based on the physical
memory of the machine and do not consider cgroups.
• Core dump enablement. To enable core dumps, change:
export ENABLE_CORE_DUMPS=${ENABLE_COREDUMPS:-false}
to:
export ENABLE_CORE_DUMPS=${ENABLE_COREDUMPS:-true}
Note:
• The location of core dump files may vary according to your operating system configuration.
• Other security settings may prevent Impala from writing core dumps even when this option is enabled.
• Authorization. Specify the ‑‑server_name option as part of the IMPALA_SERVER_ARGS and
IMPALA_CATALOG_ARGS settings to enable the core Impala support for authorization. See Starting Impala with
Sentry Authorization Enabled on page 81 for details.
• Auditing for successful or blocked Impala queries, another aspect of security.
Specify the ‑‑audit_event_log_dir=directory_path option and optionally the
‑‑max_audit_event_log_file_size=number_of_queries and ‑‑abort_on_failed_audit_event options as part
of the IMPALA_SERVER_ARGS settings, for each Impala node, to enable and customize auditing. See Auditing
Impala Operations on page 91 for details.
• Password protection for the Impala web UI, which listens on port 25000 by default. This feature involves
adding some or all of the ‑‑webserver_password_file, ‑‑webserver_authentication_domain, and
‑‑webserver_certificate_file options to the IMPALA_SERVER_ARGS and IMPALA_STATE_STORE_ARGS
settings. See Security Guidelines for Impala on page 74 for details.
• Another setting you might add to IMPALA_SERVER_ARGS is a comma-separated list of query options and
values:
‑‑default_query_options='option=value,option=value,...'
These options control the behavior of queries performed by this impalad instance. The option values you
specify here override the default values for Impala query options, as shown by the SET statement in impala-
shell.
• During troubleshooting, the appropriate support channel might direct you to change other values, particularly for
IMPALA_SERVER_ARGS, to work around issues or gather debugging information.
Note:
These startup options for the impalad daemon are different from the command-line options for the impala-
shell command. For the impala-shell options, see impala-shell Configuration Options on page 755.
Impala Tutorials
This section includes tutorial scenarios that demonstrate how to begin using Impala once the software is installed. It
focuses on techniques for loading data, because once you have some data in tables and can query that data, you can
quickly progress to more advanced Impala features.
Note:
Where practical, the tutorials take you from “ground zero” to having the desired Impala tables and data. In some
cases, you might need to download additional files from outside sources, set up additional software components,
modify commands or scripts to fit your own configuration, or substitute your own sample data.
Before trying these tutorial lessons, install Impala using one of these procedures:
• If you already have some Apache Hadoop environment set up and just need to add Impala to it, follow the
installation process described in Installing Impala on page 24. Make sure to also install the Hive metastore
service if you do not already have Hive configured.
The following example shows how to see the available databases, and the tables in each. If the list of databases or
tables is long, you can use wildcard notation to locate specific databases or tables based on their names.
| time_dim |
| tpc_tables |
+------------------------+
[localhost:21000] > show tables in tpc like 'customer*';
+-----------------------+
| name |
+-----------------------+
| customer |
| customer_address |
| customer_demographics |
+-----------------------+
Once you know what tables and databases are available, you descend into a database with the USE statement. To
understand the structure of each table, you use the DESCRIBE command. Once inside a database, you can issue
statements such as INSERT and SELECT that operate on particular tables.
The following example explores a database named TPC whose name we learned in the previous example. It shows
how to filter the table names within a database based on a search string, examine the columns of a table, and run
queries to examine the characteristics of the table data. For example, for an unfamiliar table you might want to know
the number of rows, the number of different values for a column, and other properties such as whether the column
contains any NULL values. When sampling the actual data values from a table, use a LIMIT clause to avoid excessive
output if the table contains more rows or distinct values than you expect.
| c_birth_year | int | |
| c_birth_country | string | |
| c_login | string | |
| c_email_address | string | |
| c_last_review_date | string | |
+------------------------+--------+---------+
[localhost:21000] > select count(*) from customer;
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
[localhost:21000] > select count(distinct c_birth_month) from customer;
+-------------------------------+
| count(distinct c_birth_month) |
+-------------------------------+
| 12 |
+-------------------------------+
[localhost:21000] > select count(*) from customer where c_email_address is
null;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
[localhost:21000] > select distinct c_salutation from customer limit 10;
+--------------+
| c_salutation |
+--------------+
| Mr. |
| Ms. |
| Dr. |
| |
| Miss |
| Sir |
| Mrs. |
+--------------+
When you graduate from read-only exploration, you use statements such as CREATE DATABASE and CREATE
TABLE to set up your own database objects.
The following example demonstrates creating a new database holding a new table. Although the last example ended
inside the TPC database, the new EXPERIMENTS database is not nested inside TPC; all databases are arranged in a
single top-level list.
+---------------+
| name |
+---------------+
| experiments |
| explain_plans |
+---------------+
The following example creates a new table, T1. To illustrate a common mistake, it creates this table inside the wrong
database, the TPC database where the previous example ended. The ALTER TABLE statement lets you move the
table to the intended database, EXPERIMENTS, as part of a rename operation. The USE statement is always needed
to switch to a new database, and the current_database() function confirms which database the session is in, to
avoid these kinds of mistakes.
For your initial experiments with tables, you can use ones with just a few columns and a few rows, and text-format
data files.
Note: As you graduate to more realistic scenarios, you will use more elaborate tables with many columns, features
such as partitioning, and file formats such as Parquet. When dealing with realistic data volumes, you will bring in data
using LOAD DATA or INSERT ... SELECT statements to operate on millions or billions of rows at once.
| Impala Tutorials | 39
The following example sets up a couple of simple tables with a few rows, and performs queries involving sorting,
aggregate functions and joins.
$ whoami
username
$ hdfs dfs -ls /user
Found 3 items
drwxr-xr-x - username username 0 2013-04-22 18:54 /user/
username
drwxrwx--- - mapred mapred 0 2013-03-15 20:11 /user/history
drwxr-xr-x - hue supergroup 0 2013-03-15 20:10 /user/hive
| Impala Tutorials | 40
Here is some sample data, for two tables named TAB1 and TAB2.
Copy the following content to .csv files in your local filesystem:
tab1.csv:
1,true,123.123,2012-10-24 08:55:00
2,false,1243.5,2012-10-25 13:40:00
3,false,24453.325,2008-08-22 09:33:21.123
4,false,243423.325,2007-05-12 22:32:21.33454
5,true,243.325,1953-04-22 09:11:33
tab2.csv:
1,true,12789.123
2,false,1243.5
3,false,24453.325
4,false,2423.3254
5,true,243.325
60,false,243565423.325
70,true,243.325
80,false,243423.325
90,true,243.325
Put each .csv file into a separate HDFS directory using commands like the following, which use paths available in
the Impala Demo VM:
The name of each data file is not significant. In fact, when Impala examines the contents of the data directory for the
first time, it considers all files in the directory to make up the data of the table, regardless of how many files there are
or what the files are named.
To understand what paths are available within your own HDFS filesystem and what the permissions are for the
various directories and files, issue hdfs dfs -ls / and work your way down the tree doing -ls operations for
the various directories.
Use the impala-shell command to create tables, either interactively or through a SQL script.
The following example shows creating three tables. For each table, the example shows creating columns with various
attributes such as Boolean or integer types. The example also includes commands that provide information about how
the data is formatted, such as rows terminating with commas, which makes sense in the case of importing data from a
.csv file. Where we already have .csv files containing data in the HDFS directory tree, we specify the location of
the directory containing the appropriate .csv file. Impala considers all the data from all the files in that directory to
represent the data for the table.
-- The EXTERNAL clause means the data is located outside the central
location
-- for Impala data files and is preserved when the associated Impala table
is dropped.
-- We expect the data to already exist in the directory specified by the
LOCATION clause.
CREATE EXTERNAL TABLE tab1
(
id INT,
col_1 BOOLEAN,
col_2 DOUBLE,
col_3 TIMESTAMP
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/user/username/sample_data/tab1';
Note: Getting through these CREATE TABLE statements successfully is an important validation step to confirm
everything is configured correctly with the Hive metastore and HDFS permissions. If you receive any errors during
the CREATE TABLE statements:
• Make sure you followed the installation instructions closely, in Installing Impala on page 24.
• Make sure the hive.metastore.warehouse.dir property points to a directory that Impala can write to.
The ownership should be hive:hive, and the impala user should also be a member of the hive group.
the directory containing one or more data files, and Impala queries the combined content of all the files inside that
directory. Here is how we examine the directories and files within the HDFS filesystem:
$ cd ~/username/datasets
$ ./tpcds-setup.sh
... Downloads and unzips the kit, builds the data and loads it into HDFS ...
$ hdfs dfs -ls /user/hive/tpcds/customer
Found 1 items
-rw-r--r-- 1 username supergroup 13209372 2013-03-22 18:09 /user/hive/
tpcds/customer/customer.dat
$ hdfs dfs -cat /user/hive/tpcds/customer/customer.dat | more
1|AAAAAAAABAAAAAAA|980124|7135|32946|2452238|2452208|Mr.|Javier|Lewis|Y|9|
12|1936|CHILE||Javie
[email protected]|2452508|
2|AAAAAAAACAAAAAAA|819667|1461|31655|2452318|2452288|Dr.|Amy|Moses|Y|9|4|
1966|TOGO||Amy.Moses@
Ovk9KjHH.com|2452318|
3|AAAAAAAADAAAAAAA|1473522|6247|48572|2449130|2449100|Miss|Latisha|Hamilton|
N|18|9|1979|NIUE||
[email protected]|2452313|
4|AAAAAAAAEAAAAAAA|1703214|3986|39558|2450030|2450000|Dr.|Michael|White|N|7|
6|1983|MEXICO||Mic
[email protected]|2452361|
5|AAAAAAAAFAAAAAAA|953372|4470|36368|2449438|2449408|Sir|Robert|Moran|N|8|5|
1956|FIJI||Robert.
[email protected]|2452469|
...
Here is a SQL script to set up Impala tables pointing to some of these data files in HDFS. (The script in the VM
sets up tables like this through Hive; ignore those tables for purposes of this demonstration.) Save the following as
customer_setup.sql:
--
-- store_sales fact table and surrounding dimension tables only
--
create database tpcds;
use tpcds;
$ impala-shell -i impala-host
Connected to localhost:21000
[impala-host:21000] > select count(*) from customer_address;
50000
Returned 1 row(s) in 0.37s
• Passing a set of commands contained in a file:
Loading Data
Loading data involves:
• Establishing a data set. The example below uses .csv files.
• Creating tables to which to load data.
• Loading the data into the tables you created.
Sample Queries
To run these sample queries, create a SQL query file query.sql, copy and paste each query into the query file,
and then run the query file using the shell. For example, to run query.sql on impala-host, you might use the
command:
The examples and results below assume you have loaded the sample data into the tables as described above.
interactive query starts displaying an unexpected volume of data, press Ctrl-C in impala-
shell to cancel the query.)
Results:
+----+-------+------------+-------------------------------+
| id | col_1 | col_2 | col_3 |
+----+-------+------------+-------------------------------+
| 1 | true | 123.123 | 2012-10-24 08:55:00 |
| 2 | false | 1243.5 | 2012-10-25 13:40:00 |
| 3 | false | 24453.325 | 2008-08-22 09:33:21.123000000 |
| 4 | false | 243423.325 | 2007-05-12 22:32:21.334540000 |
| 5 | true | 243.325 | 1953-04-22 09:11:33 |
+----+-------+------------+-------------------------------+
+----+-------+---------------+
| id | col_1 | col_2 |
+----+-------+---------------+
| 1 | true | 12789.123 |
| 2 | false | 1243.5 |
| 3 | false | 24453.325 |
| 4 | false | 2423.3254 |
| 5 | true | 243.325 |
| 60 | false | 243565423.325 |
| 70 | true | 243.325 |
| 80 | false | 243423.325 |
| 90 | true | 243.325 |
+----+-------+---------------+
+----+-------+-----------+
| id | col_1 | col_2 |
+----+-------+-----------+
| 1 | true | 12789.123 |
| 2 | false | 1243.5 |
| 3 | false | 24453.325 |
| 4 | false | 2423.3254 |
| 5 | true | 243.325 |
+----+-------+-----------+
Results:
+-------+-----------------+-----------------+
| col_1 | max(tab2.col_2) | min(tab2.col_2) |
+-------+-----------------+-----------------+
| false | 24453.325 | 1243.5 |
| true | 12789.123 | 243.325 |
+-------+-----------------+-----------------+
| Impala Tutorials | 46
SELECT tab2.*
FROM tab2,
(SELECT tab1.col_1, MAX(tab2.col_2) AS max_col2
FROM tab2, tab1
WHERE tab1.id = tab2.id
GROUP BY col_1) subquery1
WHERE subquery1.max_col2 = tab2.col_2;
Results:
+----+-------+-----------+
| id | col_1 | col_2 |
+----+-------+-----------+
| 1 | true | 12789.123 |
| 3 | false | 24453.325 |
+----+-------+-----------+
Results:
+----+-------+---------+-------+-----+
| id | col_1 | col_2 | month | day |
+----+-------+---------+-------+-----+
| 1 | true | 123.123 | 10 | 24 |
| 2 | false | 1243.5 | 10 | 25 |
+----+-------+---------+-------+-----+
Advanced Tutorials
These tutorials walk you through advanced scenarios or specialized features.
subdirectories within the table structure, and are not part of the CSV files. We use STRING for each of these columns
so that we can produce consistent subdirectory names, with leading zeros for a consistent length.
Back in the Linux shell, we examine the HDFS directory structure. (Your Impala data directory might be in a different
location; for historical reasons, it is sometimes under the HDFS path /user/hive/warehouse.) We use the
hdfs dfs -ls command to examine the nested subdirectories corresponding to each partitioning column, with
separate subdirectories at each level (with = in their names) representing the different values for each partitioning
column. When we get to the lowest level of subdirectory, we use the hdfs dfs -cat command to examine the
data file and see CSV-formatted data produced by the INSERT statement in Impala.
Still in the Linux shell, we use hdfs dfs -mkdir to create several data directories outside the HDFS directory
tree that Impala controls (/user/impala/warehouse in this example, maybe different in your case). Depending
on your configuration, you might need to log in as a user with permission to write into this HDFS directory tree; for
example, the commands shown here were run while logged in as the hdfs user.
We make a tiny CSV file, with values different than in the INSERT statements used earlier, and put a copy within
each subdirectory that we will use as an Impala partition.
$ cat >dummy_log_data
bar,baz,bletch
$ hdfs dfs -mkdir -p /user/impala/data/external_partitions/year=2013/
month=08/day=01/host=host1
$ hdfs dfs -mkdir -p /user/impala/data/external_partitions/year=2013/
month=07/day=28/host=host1
$ hdfs dfs -mkdir -p /user/impala/data/external_partitions/year=2013/
month=07/day=28/host=host2
$ hdfs dfs -mkdir -p /user/impala/data/external_partitions/year=2013/
month=07/day=29/host=host1
$ hdfs dfs -put dummy_log_data /user/impala/data/logs/year=2013/month=07/
day=28/host=host1
$ hdfs dfs -put dummy_log_data /user/impala/data/logs/year=2013/month=07/
day=28/host=host2
$ hdfs dfs -put dummy_log_data /user/impala/data/logs/year=2013/month=07/
day=29/host=host1
$ hdfs dfs -put dummy_log_data /user/impala/data/logs/year=2013/month=08/
day=01/host=host1
Back in the impala-shell interpreter, we move the original Impala-managed table aside, and create a new
external table with a LOCATION clause pointing to the directory under which we have set up all the partition
subdirectories and data files.
use external_partitions;
alter table logs rename to logs_original;
create external table logs (field1 string, field2 string, field3 string)
partitioned by (year string, month string, day string, host string)
row format delimited fields terminated by ','
location '/user/impala/data/logs';
Because partition subdirectories and data files come and go during the data lifecycle, you must identify each of the
partitions through an ALTER TABLE statement before Impala recognizes the data files they contain.
We issue a REFRESH statement for the table, always a safe practice when data files have been manually added,
removed, or changed. Then the data is ready to be queried. The SELECT * statement illustrates that the data from
our trivial CSV file was recognized in each of the partitions where we copied it. Although in this case there are only a
few rows, we include a LIMIT clause on this test query just in case there is more data than we expect.
refresh log_type;
select * from log_type limit 100;
+--------+--------+--------+------+-------+-----+-------+
| field1 | field2 | field3 | year | month | day | host |
+--------+--------+--------+------+-------+-----+-------+
| bar | baz | bletch | 2013 | 07 | 28 | host1 |
| bar | baz | bletch | 2013 | 08 | 01 | host1 |
| bar | baz | bletch | 2013 | 07 | 29 | host1 |
| bar | baz | bletch | 2013 | 07 | 28 | host2 |
+--------+--------+--------+------+-------+-----+-------+
Cross Joins and Cartesian Products with the CROSS JOIN Operator
Originally, Impala restricted join queries so that they had to include at least one equality comparison between the
columns of the tables on each side of the join operator. With the huge tables typically processed by Impala, any
miscoded query that produced a full Cartesian product as a result set could consume a huge amount of cluster
resources.
| Impala Tutorials | 50
In Impala 1.2.2 and higher, this restriction is lifted when you use the CROSS JOIN operator in the query. You still
cannot remove all WHERE clauses from a query like SELECT * FROM t1 JOIN t2 to produce all combinations
of rows from both tables. But you can use the CROSS JOIN operator to explicitly request such a Cartesian product.
Typically, this operation is applicable for smaller tables, where the result set still fits within the memory of a single
Impala node.
The following example sets up data for use in a series of comic books where characters battle each other. At first, we
use an equijoin query, which only allows characters from the same time period and the same planet to meet.
[localhost:21000] > create table heroes (name string, era string, planet
string);
[localhost:21000] > create table villains (name string, era string, planet
string);
[localhost:21000] > insert into heroes values
> ('Tesla','20th century','Earth'),
> ('Pythagoras','Antiquity','Earth'),
> ('Zopzar','Far Future','Mars');
Inserted 3 rows in 2.28s
[localhost:21000] > insert into villains values
> ('Caligula','Antiquity','Earth'),
> ('John Dillinger','20th century','Earth'),
> ('Xibulor','Far Future','Venus');
Inserted 3 rows in 1.93s
[localhost:21000] > select concat(heroes.name,' vs. ',villains.name) as
battle
> from heroes join villains
> where heroes.era = villains.era and heroes.planet =
villains.planet;
+--------------------------+
| battle |
+--------------------------+
| Tesla vs. John Dillinger |
| Pythagoras vs. Caligula |
+--------------------------+
Returned 2 row(s) in 0.47s
Readers demanded more action, so we added elements of time travel and space travel so that any hero could face any
villain. Prior to Impala 1.2.2, this type of query was impossible because all joins had to reference matching values
between the two tables:
With Impala 1.2.2, we rewrite the query slightly to use CROSS JOIN rather than JOIN, and now the result set
includes all combinations:
The full combination of rows from both tables is known as the Cartesian product. This type of result set is often
used for creating grid data structures. You can also filter the result set by including WHERE clauses that do not
explicitly compare columns between the two tables. The following example shows how you might produce a list of
combinations of year and quarter for use in a chart, and then a shorter list with only selected quarters.
$ cd airlines_parquet/
$ du -kch *.parq
253M 4345e5eef217aa1b-c8f16177f35fd983_1150363067_data.0.parq
14M 4345e5eef217aa1b-c8f16177f35fd983_1150363067_data.1.parq
253M 4345e5eef217aa1b-c8f16177f35fd984_501176748_data.0.parq
64M 4345e5eef217aa1b-c8f16177f35fd984_501176748_data.1.parq
184M 4345e5eef217aa1b-c8f16177f35fd985_1199995767_data.0.parq
241M 4345e5eef217aa1b-c8f16177f35fd986_2086627597_data.0.parq
212M 4345e5eef217aa1b-c8f16177f35fd987_1048668565_data.0.parq
152M 4345e5eef217aa1b-c8f16177f35fd988_1432111844_data.0.parq
1.4G total
Next, we put the Parquet data files in HDFS, all together in a single directory, with permissions on the directory and
the files so that the impala user will be able to read them.
After unpacking, we saw the largest Parquet file was 253 MB. When copying Parquet files into HDFS for
Impala to use, for maximum query performance, make sure that each file resides in a single HDFS data block.
Therefore, we pick a size larger than any single file and specify that as the block size, using the argument -
Ddfs.block.size=253m on the hdfs dfs -put command.
• The LIKE PARQUET 'path_to_any_parquet_file' clause means we skip the list of column names
and types; Impala automatically gets the column names and data types straight from the data files. (Currently, this
technique only works for Parquet files.)
• Ignore the warning about lack of READ_WRITE access to the files in HDFS; the impala user can read the files,
which will be sufficient for us to experiment with queries and perform some copy and transform operations into
other tables.
$ impala-shell
> CREATE DATABASE airlines_data;
USE airlines_data;
CREATE EXTERNAL TABLE airlines_external
LIKE PARQUET 'hdfs:staging/airlines/4345e5eef217aa1b-
c8f16177f35fd983_1150363067_data.0.parq'
STORED AS PARQUET LOCATION 'hdfs:staging/airlines';
WARNINGS: Impala does not have READ_WRITE access to path 'hdfs://
myhost.com:8020/user/impala/staging'
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd983_1150363067_data.0.parq | 252.99MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd983_1150363067_data.1.parq | 13.43MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd984_501176748_data.0.parq | 252.84MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd984_501176748_data.1.parq | 63.92MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd985_1199995767_data.0.parq | 183.64MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd986_2086627597_data.0.parq | 240.04MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd987_1048668565_data.0.parq | 211.35MB | |
| /user/impala/staging/airlines/4345e5eef217aa1b-
c8f16177f35fd988_1432111844_data.0.parq | 151.46MB | |
+---------------------------------------------------------------------------------------
+----------+-----------+
Analyze Data
Now that we are confident that the connections are solid between the Impala table and the underlying Parquet files,
we run some initial queries to understand the characteristics of the data: the overall number of rows, and the ranges
and how many different values are in certain columns.
The NDV() function returns a number of distinct values, which, for performance reasons, is an estimate when there
are lots of different values in the column, but is precise when the cardinality is less than 16 K. Use NDV() function
| Impala Tutorials | 56
for this kind of exploration rather than COUNT(DISTINCT colname), because Impala can evaluate multiple
NDV() functions in a single query, but only a single instance of COUNT DISTINCT.
With the above queries, we see that there are modest numbers of different airlines, flight numbers, and origin and
destination airports. Two things jump out from this query: the number of tail_num values is much smaller than we
might have expected, and there are more destination airports than origin airports. Let's dig further. What we find is
that most tail_num values are NULL. It looks like this was an experimental column that wasn't filled in accurately.
We make a mental note that if we use this data as a starting point, we'll ignore this column. We also find that certain
airports are represented in the ORIGIN column but not the DEST column; now we know that we cannot rely on the
assumption that those sets of airport codes are identical.
Note: The first SELECT DISTINCT DEST query takes almost 40 seconds. We expect all queries on such a small
data set, less than 2 GB, to take a few seconds at most. The reason is because the expression NOT IN (SELECT
origin FROM airlines_external) produces an intermediate result set of 123 million rows, then runs 123
million comparisons on each data node against the tiny set of destination airports. The way the NOT IN operator
| Impala Tutorials | 57
works internally means that this intermediate result set with 123 million rows might be transmitted across the
network to each data node in the cluster. Applying another DISTINCT inside the NOT IN subquery means that the
intermediate result set is only 340 items, resulting in much less network traffic and fewer comparison operations. The
more efficient query with the added DISTINCT is approximately 7 times as fast.
Next, we try doing a simple calculation, with results broken down by year. This reveals that some years have no data
in the airtime column. That means we might be able to use that column in queries involving certain date ranges,
but we cannot count on it to always be reliable. The question of whether a column contains any NULL values, and if
so what is their number, proportion, and distribution, comes up again and again when doing initial exploration of a
data set.
With the notion of NULL values in mind, let's come back to the tail_num column that we discovered had a lot
of NULLs. Let's quantify the NULL and non-NULL values in that column for better understanding. First, we just
count the overall number of rows versus the non-NULL values in that column. That initial result gives the appearance
of relatively few non-NULL values, but we can break it down more clearly in a single query. Once we have the
COUNT(*) and the COUNT(colname) numbers, we can encode that initial query in a WITH clause, then run
a follow-on query that performs multiple arithmetic operations on those values. Seeing that only one-third of one
percent of all rows have non-NULL values for the tail_num column clearly illustrates that column is not of much
use.
> WITH t1 AS
(SELECT COUNT(*) AS 'rows', COUNT(tail_num) AS 'nonnull'
FROM airlines_external)
SELECT `rows`, `nonnull`, `rows` - `nonnull` AS 'nulls',
| Impala Tutorials | 58
By examining other columns using these techniques, we can form a mental picture of the way data is distributed
throughout the table, and which columns are most significant for query purposes. For this tutorial, we focus
mostly on the fields likely to hold discrete values, rather than columns such as actual_elapsed_time whose
names suggest they hold measurements. We would dig deeper into those columns once we had a clear picture of
which questions were worthwhile to ask, and what kinds of trends we might look for. For the final piece of initial
exploration, let's look at the year column. A simple GROUP BY query shows that it has a well-defined range, a
manageable number of distinct values, and relatively even distribution of rows across the different years.
We could go quite far with the data in this initial raw format, just as we downloaded it from the web. If the data set
proved to be useful and worth persisting in Impala for extensive queries, we might want to copy it to an internal table,
letting Impala manage the data files and perhaps reorganizing a little for higher efficiency. In this next stage of the
tutorial, we copy the original data into a partitioned table, still in Parquet format. Partitioning based on the year
column lets us run queries with clauses such as WHERE year = 2001 or WHERE year BETWEEN 1989 AND
1999, which can dramatically cut down on I/O by ignoring all the data from years outside the desired range. Rather
than reading all the data and then deciding which rows are in the matching years, Impala can zero in on only the data
files from specific year partitions. To do this, Impala physically reorganizes the data files, putting the rows from
| Impala Tutorials | 59
each year into data files in a separate HDFS directory for each year value. Along the way, we'll also get rid of the
tail_num column that proved to be almost entirely NULL.
The first step is to create a new table with a layout very similar to the original airlines_external table. We'll
do that by reverse-engineering a CREATE TABLE statement for the first table, then tweaking it slightly to include a
PARTITION BY clause for year, and excluding the tail_num column. The SHOW CREATE TABLE statement
gives us the starting point.
Although we could edit that output into a new SQL statement, all the ASCII box characters make such editing
inconvenient. To get a more stripped-down CREATE TABLE to start with, we restart the impala-shell command
with the -B option, which turns off the box-drawing behavior.
After copying and pasting the CREATE TABLE statement into a text editor for fine-tuning, we quit and restart
impala-shell without the -B option, to switch back to regular output.
Next we run the CREATE TABLE statement that we adapted from the SHOW CREATE TABLE output. We kept
the STORED AS PARQUET clause because we want to rearrange the data somewhat but still keep it in the high-
performance Parquet format. The LOCATION and TBLPROPERTIES clauses are not relevant for this new table, so
| Impala Tutorials | 60
we edit those out. Because we are going to partition the new table based on the year column, we move that column
name (and its type) into a new PARTITIONED BY clause.
Next, we copy all the rows from the original table into this new one with an INSERT statement. (We edited
the CREATE TABLE statement to make an INSERT statement with the column names in the same order.)
The only change is to add a PARTITION(year) clause, and move the year column to the very end of the
SELECT list of the INSERT statement. Specifying PARTITION(year), rather than a fixed value such as
PARTITION(year=2000), means that Impala figures out the partition value for each row based on the value of
the very last column in the SELECT list. This is the first SQL statement that legitimately takes any substantial time,
because the rows from different years are shuffled around the cluster; the rows that go into each partition are collected
on one node, before being written to one or more new data files.
dest,
distance,
taxi_in,
taxi_out,
cancelled,
cancellation_code,
diverted,
carrier_delay,
weather_delay,
nas_delay,
security_delay,
late_aircraft_delay,
year
FROM airlines_data.airlines_external;
Once partitioning or join queries come into play, it's important to have statistics that Impala can use to optimize
queries on the corresponding tables. The COMPUTE INCREMENTAL STATS statement is the way to collect
statistics for partitioned tables. Then the SHOW TABLE STATS statement confirms that the statistics are in place for
each partition, and also illustrates how many files and how much raw data is in each partition.
At this point, we sanity check the partitioning we did. All the partitions have exactly one file, which is on the low
side. A query that includes a clause WHERE year=2004 will only read a single data block; that data block will
be read and processed by a single data node; therefore, for a query targeting a single year, all the other nodes in the
cluster will sit idle while all the work happens on a single machine. It's even possible that by chance (depending
on HDFS replication factor and the way data blocks are distributed across the cluster), that multiple year partitions
selected by a filter such as WHERE year BETWEEN 1999 AND 2001 could all be read and processed by the
same data node. The more data files each partition has, the more parallelism you can get and the less probability of
“hotspots” occurring on particular nodes, therefore a bigger performance boost by having a big cluster.
However, the more data files, the less data goes in each one. The overhead of dividing the work in a parallel query
might not be worth it if each node is only reading a few megabytes. 50 or 100 megabytes is a decent size for a Parquet
data block; 9 or 37 megabytes is on the small side. Which is to say, the data distribution we ended up with based on
| Impala Tutorials | 63
this partitioning scheme is on the borderline between sensible (reasonably large files) and suboptimal (few files in
each partition). The way to see how well it works in practice is to run the same queries against the original flat table
and the new partitioned table, and compare times.
Spoiler: in this case, with my particular 4-node cluster with its specific distribution of data blocks and my particular
exploratory queries, queries against the partitioned table do consistently run faster than the same queries against
the unpartitioned table. But I could not be sure that would be the case without some real measurements. Here are
some queries I ran to draw that conclusion, first against airlines_external (no partitioning), then against
AIRLINES (partitioned by year). The AIRLINES queries are consistently faster. Changing the volume of data,
changing the size of the cluster, running queries that did or didn't refer to the partition key columns, or other factors
could change the results to favor one table layout or the other.
Note: If you find the volume of each partition is only in the low tens of megabytes, consider lowering the granularity
of partitioning. For example, instead of partitioning by year, month, and day, partition by year and month or even just
by year. The ideal layout to distribute work efficiently in a parallel query is many tens or even hundreds of megabytes
per Parquet file, and the number of Parquet files in each partition somewhat higher than the number of data nodes.
Now we can finally analyze this data set that from the raw data files and we didn't know what columns they
contained. Let's see whether the airtime of a flight tends to be different depending on the day of the week. We can
see that the average is a little higher on day number 6; perhaps Saturday is a busy flying day and planes have to circle
for longer at the destination airport before landing.
To see if the apparent trend holds up over time, let's do the same breakdown by day of week, but also split up by year.
Now we can see that day number 6 consistently has a higher average air time in each year. We can also see that the
| Impala Administration | 64
average air time increased over time across the board. And the presence of NULL for this column in years 1987 to
1994 shows that queries involving this column need to be restricted to a date range of 1995 and higher.
Impala Administration
As an administrator, you monitor Impala's use of resources and take action when necessary to keep Impala running
smoothly and avoid conflicts with other Hadoop components running on the same cluster. When you detect that an
issue has happened or could happen in the future, you reconfigure Impala or other components such as HDFS or even
the hardware of the cluster itself to resolve or avoid problems.
Related tasks:
As an administrator, you can expect to perform installation, upgrade, and configuration tasks for Impala on all
machines in a cluster. See Installing Impala on page 24, Upgrading Impala on page 27, and Managing
Impala on page 25 for details.
| Impala Administration | 65
For security tasks typically performed by administrators, see Impala Security on page 73.
Administrators also decide how to allocate cluster resources so that all Hadoop components can run smoothly
together. For Impala, this task primarily involves:
• Deciding how many Impala queries can run concurrently and with how much memory, through the admission
control feature. See Admission Control and Query Queuing on page 650 for details.
• Dividing cluster resources such as memory between Impala and other components, using YARN for overall
resource management, and Llama to mediate resource requests from Impala to YARN. See Resource Management
on page 650 for details.
See Scalability Considerations for the Impala Statestore on page 634 for more details about statestore operation
and settings on clusters with a large number of Impala-related objects such as tables and partitions.
Setting the Idle Query and Idle Session Timeouts for impalad
To keep long-running queries or idle sessions from tying up cluster resources, you can set timeout intervals for both
individual queries, and entire sessions.
Note:
The timeout clock for queries and sessions only starts ticking when the query or session is idle.
For queries, this means the query has results ready but is waiting for a client to fetch the data. A query can run for an
arbitrary time without triggering a timeout, because the query is computing results rather than sitting idle waiting for
the results to be fetched. The timeout period is intended to prevent unclosed queries from consuming resources and
taking up slots in the admission count of running queries, potentially preventing other queries from starting.
For sessions, this means that no query has been submitted for some period of time.
Use the following startup options for the impalad daemon to specify timeout values:
• --idle_query_timeout
Specifies the time in seconds after which an idle query is cancelled. This could be a query whose results were all
fetched but was never closed, or one whose results were partially fetched and then the client program stopped
requesting further results. This condition is most likely to occur in a client program using the JDBC or ODBC
interfaces, rather than in the interactive impala-shell interpreter. Once a query is cancelled, the client
program cannot retrieve any further results from the query.
| Impala Administration | 66
You can reduce the idle query timeout by using the QUERY_TIMEOUT_S query option. Any non-zero
value specified for the --idle_query_timeout startup option serves as an upper limit for the
QUERY_TIMEOUT_S query option. See QUERY_TIMEOUT_S Query Option (Impala 2.0 or higher only) on
page 373 about the query option.
A zero value for --idle_query_timeout disables query timeouts.
Cancelled queries remain in the open state but use only the minimal resources.
• --idle_session_timeout
Specifies the time in seconds after which an idle session expires. A session is idle when no activity is occurring for
any of the queries in that session, and the session has not started any new queries. Once a session is expired, you
cannot issue any new query requests to it. The session remains open, but the only operation you can perform is to
close it.
The default value of 0 specifies sessions never expire.
You can override the --idle_session_timeout value with the IDLE_SESSION_TIMEOUT Query Option
(Impala 2.12 or higher only) on page 350 at the session level.
For instructions on changing impalad startup options, see Modifying Impala Startup Options on page 31.
Note:
Impala checks periodically for idle sessions and queries to cancel. The actual idle time before cancellation might be
up to 50% greater than the specified configuration setting. For example, if the timeout setting was 60, the session or
query might be cancelled after being idle between 60 and 90 seconds.
Setting Timeout and Retries for Thrift Connections to the Backend Client
Impala connections to the backend client are subject to failure in cases when the network is momentarily overloaded.
To avoid failed queries due to transient network problems, you can configure the number of Thrift connection retries
using the following option:
• The --backend_client_connection_num_retries option specifies the number of times Impala will
try connecting to the backend client after the first connection attempt fails. By default, impalad will attempt
three re-connections before it returns a failure.
You can configure timeouts for sending and receiving data from the backend client. Therefore, if for some reason a
query hangs, instead of waiting indefinitely for a response, Impala will terminate the connection after a configurable
timeout.
• The --backend_client_rpc_timeout_ms option can be used to specify the number of milliseconds
Impala should wait for a response from the backend client before it terminates the connection and signals a failure.
The default value for this property is 300000 milliseconds, or 5 minutes.
Cancelling a Query
Occasionally, an Impala query might run for an unexpectedly long time, tying up resources in the cluster. This section
describes the options to terminate such runaway queries.
5. For any scripts, jobs, or configuration settings for applications that formerly connected to a specific DataNode to
run Impala SQL statements, change the connection information (such as the -i option in impala-shell) to
point to the load balancer instead.
Note: The following sections use the HAProxy software as a representative example of a load balancer that you can
use with Impala.
You might need to perform benchmarks and load testing to determine which setting is optimal for your use case.
Always set up using two load-balancing algorithms: Source IP Persistence for Hue and Leastconn for others.
See impala-shell Configuration Options on page 755 for information about the option.
To clarify that the load-balancing proxy server is legitimate, perform these extra Kerberos setup steps:
1. This section assumes you are starting with a Kerberos-enabled cluster. See Enabling Kerberos Authentication
for Impala on page 84 for instructions for setting up Impala with Kerberos. See the documentation for your
Apache Hadoop distribution for general steps to set up Kerberos.
2. Choose the host you will use for the proxy server. Based on the Kerberos setup procedure, it should already have
an entry impala/proxy_host@realm in its keytab. If not, go back over the initial Kerberos configuration
steps for the keytab on each host running the impalad daemon.
3. Copy the keytab file from the proxy host to all other hosts in the cluster that run the impalad daemon. (For
optimal performance, impalad should be running on all DataNodes in the cluster.) Put the keytab file in a secure
location on each of these other hosts.
4. Add an entry impala/actual_hostname@realm to the keytab on each host running the impalad daemon.
5. For each impalad node, merge the existing keytab with the proxy’s keytab using ktutil, producing a new
keytab file. For example:
$ ktutil
ktutil: read_kt proxy.keytab
ktutil: read_kt impala.keytab
ktutil: write_kt proxy_impala.keytab
ktutil: quit
6. To verify that the keytabs are merged, run the command:
klist -k keytabfile
which lists the credentials for both principal and be_principal on all nodes.
7. Make sure that the impala user has permission to read this merged keytab file.
8. Change the following configuration settings for each host in the cluster that participates in the load balancing:
• In the impalad option definition, add:
--principal=impala/proxy_host@realm
--be_principal=impala/actual_host@realm
--keytab_file=path_to_merged_keytab
Note: Every host has different --be_principal because the actual hostname is different on each host.
Specify the fully qualified domain name (FQDN) for the proxy host, not the IP address. Use the exact FQDN
as returned by a reverse DNS lookup for the associated IP address.
• Modify the startup options. See Modifying Impala Startup Options on page 31 for the procedure to modify
the startup options.
9. Restart Impala to make the changes take effect. Restart the impalad daemons on all hosts in the cluster, as well
as the statestored and catalogd daemons.
balance a TLS/SSL-enabled Impala installation without additional configuration, you see a certificate mismatch error
when a client attempts to connect to the load balancing proxy host.
You can configure a proxy server in several ways to load balance TLS/SSL enabled Impala:
Refer to your load balancer documentation for the steps to set up Impala and the load balancer using one of the
options above.
/usr/sbin/haproxy –f /etc/haproxy/haproxy.cfg
• In impala-shell, JDBC applications, or ODBC applications, connect to the listener port of the proxy host,
rather than port 21000 or 21050 on a host actually running impalad. The sample configuration file sets haproxy
to listen on port 25003, therefore you would send all requests to haproxy_host:25003.
This is the sample haproxy.cfg used in this example:
global
# To have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
| Impala Administration | 71
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#
# You might need to adjust timing values to prevent timeouts.
#
# The timeout values should be dependant on how you use the cluster
# and how long your queries run.
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
maxconn 3000
timeout connect 5000
timeout client 3600s
timeout server 3600s
#
# This sets up the admin page for HA Proxy at port 25002.
#
listen stats :25002
balance
mode http
stats enable
stats auth username:password
Important: Hue requires the check option at end of each line in the above file to ensure HAProxy can detect any
unreachable Impalad server, and failover can be successful. Without the TCP check, you may hit an error when the
impalad daemon to which Hue tries to connect is down.
Note: If your JDBC or ODBC application connects to Impala through a load balancer such as haproxy, be cautious
about reusing the connections. If the load balancer has set up connection timeout values, either check the connection
frequently so that it never sits idle longer than the load balancer timeout value, or check the connection validity before
using it and create a new one if the connection has been closed.
• Drop all tables in a database before dropping the database itself. See DROP DATABASE Statement on page 270
for details.
• Clean up temporary files after failed INSERT statements. If an INSERT statement encounters an error, and you
see a directory named .impala_insert_staging or _impala_insert_staging left behind in the
data directory for the table, it might contain temporary data files taking up space in HDFS. You might be able to
salvage these data files, for example if they are complete but could not be moved into place due to a permission
error. Or, you might delete those files through commands such as hadoop fs or hdfs dfs, to reclaim space
before re-trying the INSERT. Issue DESCRIBE FORMATTED table_name to see the HDFS path where you
can check for temporary files.
• By default, intermediate files used during large sort, join, aggregation, or analytic function operations are stored
in the directory /tmp/impala-scratch . These files are removed when the operation finishes. (Multiple
concurrent queries can perform operations that use the “spill to disk” technique, without any name conflicts
for these temporary files.) You can specify a different location by starting the impalad daemon with the
‑‑scratch_dirs="path_to_directory" configuration option. You can specify a single directory, or a comma-
separated list of directories. The scratch directories must be on the local filesystem, not in HDFS. You might
specify different directory paths for different hosts, depending on the capacity and speed of the available storage
devices. In Impala 2.3 or higher, Impala successfully starts (with a warning Impala successfully starts (with a
warning written to the log) if it cannot create or read and write files in one of the scratch directories. If there is less
than 1 GB free on the filesystem where that directory resides, Impala still runs, but writes a warning message to
its log. If Impala encounters an error reading or writing files in a scratch directory during a query, Impala logs the
error and the query fails.
• If you use the Amazon Simple Storage Service (S3) as a place to offload data to reduce the volume of local
storage, Impala 2.2.0 and higher can query the data directly from S3. See Using Impala with the Amazon S3
Filesystem on page 732 for details.
Impala Security
Impala includes a fine-grained authorization framework for Hadoop, based on Apache Sentry. Sentry authorization
was added in Impala 1.1.0. Together with the Kerberos authentication framework, Sentry takes Hadoop security to
a new level needed for the requirements of highly regulated industries such as healthcare, financial services, and
government. Impala also includes an auditing capability which was added in Impala 1.1.1; Impala generates the audit
data which can be consumed, filtered, and visualized by cluster-management components focused on governance.
The Impala security features have several objectives. At the most basic level, security prevents accidents or mistakes
that could disrupt application processing, delete or corrupt data, or reveal data to unauthorized users. More advanced
security features and practices can harden the system against malicious users trying to gain unauthorized access
or perform other disallowed operations. The auditing feature provides a way to confirm that no unauthorized
access occurred, and detect whether any such attempts were made. This is a critical set of features for production
deployments in large organizations that handle important or sensitive data. It sets the stage for multi-tenancy, where
multiple applications run concurrently and are prevented from interfering with each other.
The material in this section presumes that you are already familiar with administering secure Linux systems. That
is, you should know the general security practices for Linux and Hadoop, and their associated commands and
configuration files. For example, you should know how to create Linux users and groups, manage Linux group
membership, set Linux and HDFS file permissions and ownership, and designate the default permissions and
ownership for new files. You should be familiar with the configuration of the nodes in your Hadoop cluster, and know
how to apply configuration changes or run a set of commands across all the nodes.
The security features are divided into these broad categories:
• Enable authorization by running the impalad daemons with the -server_name and -
authorization_policy_file options on all nodes. (The authorization feature does not apply to the
statestored daemon, which has no access to schema objects or data files.)
• Set up authentication using Kerberos, to make sure users really are who they say they are.
Password-protect the Impala web UI that listens on port 25000 by default. Set up a .htpasswd file in
the $IMPALA_HOME directory, or start both the impalad and statestored daemons with the --
webserver_password_file option to specify a different location (including the filename).
This file should only be readable by the Impala process and machine administrators, because it contains (hashed)
versions of passwords. The username / password pairs are not derived from Unix usernames, Kerberos users, or
any other system. The domain field in the password file must match the domain supplied to Impala by the new
command-line option --webserver_authentication_domain. The default is mydomain.com.
Impala also supports using HTTPS for secure web traffic. To do so, set --webserver_certificate_file
to refer to a valid .pem TLS/SSL certificate file. Impala will automatically start using HTTPS once the TLS/
SSL certificate has been read and validated. A .pem file is basically a private key, followed by a signed TLS/SSL
certificate; make sure to concatenate both parts when constructing the .pem file.
If Impala cannot find or parse the .pem file, it prints an error message and quits.
Note:
If the private key is encrypted using a passphrase, Impala will ask for that passphrase on startup, which is not
useful for a large cluster. In that case, remove the passphrase and make the .pem file readable only by Impala and
administrators.
When you turn on TLS/SSL for the Impala web UI, the associated URLs change from http:// prefixes to
https://. Adjust any bookmarks or application code that refers to those URLs.
--ssl_cipher_list="RC4-SHA,RC4-MD5"
By default, the cipher list is empty, and Impala uses the default cipher list for the underlying platform. See the output
of man ciphers for the full set of keywords and notation allowed in the argument string.
Impala Authorization
Authorization determines which users are allowed to access which resources, and what operations they are allowed
to perform. You use Apache Sentry or Apache Ranger for authorization. By default, when authorization is not
enabled, Impala does all read and write operations with the privileges of the impala user, which is suitable for a
development/test environment but not for a secure production environment. When authorization is enabled, Impala
uses the OS user ID of the user who runs impala-shell or other client programs, and associates various privileges
with each user.
Server
| Impala Security | 78
URI
Database
Table
Column
The table-level privileges apply to views as well. Anywhere you specify a table name, you can specify a view name
instead.
In Impala 2.3 and higher, you can specify privileges for individual columns.
The table below lists the minimum level of privileges and the scope required to execute SQL statements in Impala 3.0
and higher. The following notations are used:
• ANY denotes the SELECT, INSERT, CREATE, ALTER, DROP, or REFRESH privilege.
• ALL privilege denotes the SELECT, INSERT, CREATE, ALTER, DROP, and REFRESH privileges.
• The owner of an object effectively has the ALL privilege on the object.
• The parent levels of the specified scope are implicitly supported where a scope refers to the specific level in the
object hierarchy that the privilege is granted. For example, if a privilege is listed with the TABLE scope, the same
privilege granted on DATABASE and SERVER will allow the user to execute the specified SQL statement.
ALL URI
CREATE VIEW CREATE DATABASE
SELECT TABLE
ALTER DATABASE SET ALL WITH GRANT DATABASE
OWNER
ALTER TABLE ALTER TABLE
ALTER TABLE SET LOCATION ALTER TABLE
ALL URI
ALTER TABLE RENAME CREATE DATABASE
ALL TABLE
ALTER TABLE SET OWNER ALL WITH GRANT TABLE
ALTER VIEW ALTER TABLE
SELECT TABLE
ALTER VIEW RENAME CREATE DATABASE
ALL TABLE
ALTER VIEW SET OWNER ALL WITH GRANT VIEW
DROP DATABASE DROP DATABASE
DROP TABLE DROP TABLE
DROP VIEW DROP TABLE
CREATE FUNCTION CREATE DATABASE
ALL URI
DROP FUNCTION DROP DATABASE
COMPUTE STATS ALTER and SELECT TABLE
DROP STATS ALTER TABLE
INVALIDATE METADATA REFRESH SERVER
INVALIDATE METADATA REFRESH TABLE
<table>
REFRESH <table> REFRESH TABLE
REFRESH AUTHORIZATION REFRESH SERVER
REFRESH FUNCTIONS REFRESH DATABASE
COMMENT ON DATABASE ALTER DATABASE
COMMENT ON TABLE ALTER TABLE
COMMENT ON VIEW ALTER TABLE
COMMENT ON COLUMN ALTER TABLE
DESCRIBE DATABASE SELECT, INSERT, or REFRESH DATABASE
DESCRIBE <table/view> SELECT, INSERT, or REFRESH TABLE
| Impala Security | 80
Privileges are managed via the GRANT and REVOKE SQL statements that require the Sentry or Ranger service
enabled.
If you change privileges outside of Impala, e.g. adding a user, removing a user, modifying privileges, you must
clear the Impala Catalog server cache by running the REFRESH AUTHORIZATION statement. REFRESH
AUTHORIZATION is not required if you make the changes to privileges within Impala.
An ownership can be transferred to another user or role via the ALTER DATABASE, ALTER TABLE, or ALTER
VIEW with the SET OWNER clause.
Managing Privileges
You set up privileges through the GRANT and REVOKE statements in either Impala or Hive. Then both components
use those same privileges automatically.
For information about using the Impala GRANT and REVOKE statements, see GRANT Statement (Impala 2.0 or higher
only) on page 284 and REVOKE Statement (Impala 2.0 or higher only) on page 305.
URIs must start with hdfs://, s3a://, adl://, or file://. If a URI starts with an absolute path, the path will
be appended to the default filesystem prefix. For example, if you specify:
The above statement effectively becomes the following where the default filesystem is HDFS.
When defining URIs for HDFS, you must also specify the NameNode. For example:
Warning: Because the NameNode host and port must be specified, it is strongly recommended that you use
High Availability (HA). This ensures that the URI will remain constant even if the NameNode changes. For
example:
Impala Authentication
Authentication is the mechanism to ensure that only specified hosts and users can connect to Impala. It also verifies
that when clients connect to Impala, they are connected to a legitimate server. This feature prevents spoofing such
as impersonation (setting up a phony client system with the same account and group names as a legitimate user) and
man-in-the-middle attacks (intercepting application requests before they reach Impala and eavesdropping on sensitive
information in the requests or the results).
Impala supports authentication using either Kerberos or LDAP.
You can also make proxy connections to Impala through Apache Knox.
Note: Regardless of the authentication mechanism used, Impala always creates HDFS directories and data files
owned by the same user (typically impala). To implement user-level access to different databases, tables, columns,
partitions, and so on, use the Sentry authorization feature, as explained in Impala Authorization on page 77.
Once you are finished setting up authentication, move on to authorization, which involves specifying what databases,
tables, HDFS directories, and so on can be accessed by particular users when they connect through Impala. See
Impala Authorization on page 77 for details.
Important:
If you plan to use Impala in your cluster, you must configure your KDC to allow tickets to be renewed, and
you must configure krb5.conf to request renewable tickets. Typically, you can do this by adding the
max_renewable_life setting to your realm in kdc.conf, and by adding the renew_lifetime parameter
to the libdefaults section of krb5.conf. For more information about renewable tickets, see the Kerberos
documentation.
Start all impalad and statestored daemons with the ‑‑principal and ‑‑keytab-file flags set to the principal
and full path name of the keytab file containing the credentials for the principal.
To enable Kerberos in the Impala shell, start the impala-shell command using the -k flag.
To enable Impala to work with Kerberos security on your Hadoop cluster, make sure you perform the installation
and configuration steps in Authentication in Hadoop. Note that when Kerberos security is enabled in Impala, a web
browser that supports Kerberos HTTP SPNEGO is required to access the Impala web console (for example, Firefox,
Internet Explorer, or Chrome).
If the NameNode, Secondary NameNode, DataNode, JobTracker, TaskTrackers, ResourceManager, NodeManagers,
HttpFS, Oozie, Impala, or Impala statestore services are configured to use Kerberos HTTP SPNEGO authentication,
and two or more of these services are running on the same host, then all of the running services must use the same
HTTP principal and keytab file used for their HTTP endpoints.
$ kadmin
kadmin: addprinc -requires_preauth -randkey impala/
[email protected]
2. Create an HTTP service principal. For example:
Note: The HTTP component of the service principal must be uppercase as shown in the preceding example.
3. Create keytab files with both principals. For example:
4. Use ktutil to read the contents of the two keytab files and then write those contents to a new file. For example:
$ ktutil
ktutil: rkt impala.keytab
ktutil: rkt http.keytab
ktutil: wkt impala-http.keytab
ktutil: quit
5. (Optional) Test that credentials in the merged keytab file are valid, and that the “renew until” date is in the future.
For example:
$ klist -e -k -t impala-http.keytab
6. Copy the impala-http.keytab file to the Impala configuration directory. Change the permissions to be only
read for the file owner and change the file owner to the impala user. By default, the Impala user and group are
both named impala. For example:
$ cp impala-http.keytab /etc/impala/conf
$ cd /etc/impala/conf
$ chmod 400 impala-http.keytab
$ chown impala:impala impala-http.keytab
7. Add Kerberos options to the Impala defaults file, /etc/default/impala. Add the options
for both the impalad and statestored daemons, using the IMPALA_SERVER_ARGS and
IMPALA_STATE_STORE_ARGS variables. For example, you might add:
-kerberos_reinit_interval=60
-principal=impala_1/[email protected]
-keytab_file=/path/to/impala.keytab
For more information on changing the Impala defaults specified in /etc/default/impala, see Modifying
Impala Startup Options.
Note: Restart impalad and statestored for these configuration changes to take effect.
An alternative form of authentication you can use is Kerberos, described in Enabling Kerberos Authentication for
Impala on page 84.
The above options are mutually exclusive, and Impala does not start if more than one of these options are specified.
-u Sets the user. Per Active Directory, the user is the short
username, not the full LDAP distinguished name. If
your LDAP settings include a search base, use the --
ldap_bind_pattern on the impalad daemon to
translate the short user name from impala-shell
automatically to the fully qualified name.
These login details are only used by Impala to authenticate to LDAP. The Impala service trusts Hue to have already
validated the user being impersonated, rather than simply passing on the credentials.
LDAP authentication, Impala automatically handled the process of coordinating the work across multiple nodes and
transmitting intermediate results back to the coordinator node.
‑‑authorized_proxy_user_config=authenticated_user1=delegated_user1,delegated_user2,...;authenti
‑‑authorized_proxy_group_config=authenticated_user1=delegated_group1,delegated_group2,...;authe
• Queries that Impala can analyze and parse to determine that they are authorized. The audit data is recorded
immediately after Impala finishes its analysis, before the query is actually executed.
The audit log does not contain entries for queries that could not be parsed and analyzed. For example, a query that
fails due to a syntax error is not recorded in the audit log. The audit log also does not contain queries that fail due to a
reference to a table that does not exist, if you would be authorized to access the table if it did exist.
Certain statements in the impala-shell interpreter, such as CONNECT, SUMMARY, PROFILE, SET, and QUIT,
do not correspond to actual SQL queries, and these statements are not reflected in the audit log.
Column Lineage
Column lineage tracks information in fine detail, at the level of particular columns rather than entire tables.
For example, if you have a table with information derived from web logs, you might copy that data into other tables
as part of the ETL process. The ETL operations might involve transformations through expressions and function calls,
and rearranging the columns into more or fewer tables (normalizing or denormalizing the data). Then for reporting,
you might issue queries against multiple tables and views. In this example, column lineage helps you determine that
data that entered the system as RAW_LOGS.FIELD1 was then turned into WEBSITE_REPORTS.IP_ADDRESS
through an INSERT ... SELECT statement. Or, conversely, you could start with a reporting query against a view,
and trace the origin of the data in a field such as TOP_10_VISITORS.USER_ID back to the underlying table and
even further back to the point where the data was first loaded into Impala.
When you have tables where you need to track or control access to sensitive information at the column level, see
Impala Authorization on page 77 for how to implement column-level security. You set up authorization using the
Sentry framework, create views that refer to specific sets of columns, and then assign authorization privileges to those
views rather than the underlying tables.
Comments
Impala supports the familiar styles of SQL comments:
• All text from a -- sequence to the end of the line is considered a comment and ignored. This type of comment can
occur on a single line by itself, or after all or part of a statement.
• All text from a /* sequence to the next */ sequence is considered a comment and ignored. This type of comment
can stretch over multiple lines. This type of comment can occur on one or more lines by itself, in the middle of a
statement, or before or after a statement.
For example:
/*
This is a multi-line comment about a query.
*/
select ...;
Data Types
Impala supports a set of data types that you can use for table columns, expression values, and function arguments and
return values.
| Impala SQL Language Reference | 95
Note: Currently, Impala supports only scalar types, not composite or nested types. Accessing a table containing any
columns with unsupported types causes an error.
For the notation to write literals of each of these data types, see Literals on page 165.
See SQL Differences Between Impala and Hive on page 583 for differences between Impala and Hive data types.
Usage notes:
Because complex types are often used in combination, for example an ARRAY of STRUCT elements, if you are
unfamiliar with the Impala complex types, start with Complex Types (Impala 2.3 or higher only) on page 134 for
background information and usage examples.
The elements of the array have no names. You refer to the value of the array item using the ITEM pseudocolumn,
or its position in the array with the POS pseudocolumn. See ITEM and POS Pseudocolumns on page 148 for
information about these pseudocolumns.
Each row can have a different number of elements (including none) in the array for that row.
When an array contains items of scalar types, you can use aggregation functions on the array elements without using
join notation. For example, you can find the COUNT(), AVG(), SUM(), and so on of numeric array elements, or
the MAX() and MIN() of any scalar array elements by referring to table_name.array_column in the FROM
clause of the query. When you need to cross-reference values from the array with scalar values from the same row,
such as by including a GROUP BY clause to produce a separate aggregated result for each row, then the join clause is
required.
A common usage pattern with complex types is to have an array as the top-level type for the column: an array of
structs, an array of maps, or an array of arrays. For example, you can model a denormalized table by creating a
column that is an ARRAY of STRUCT elements; each item in the array represents a row from a table that would
normally be used in a join query. This kind of data structure lets you essentially denormalize tables by associating
multiple rows from one table with the matching row in another table.
You typically do not create more than one top-level ARRAY column, because if there is some relationship between
the elements of multiple arrays, it is convenient to model the data as an array of another complex type element (either
STRUCT or MAP).
You can pass a multi-part qualified name to DESCRIBE to specify an ARRAY, STRUCT, or MAP column and
visualize its structure as if it were a table. For example, if table T1 contains an ARRAY column A1, you could issue
the statement DESCRIBE t1.a1. If table T1 contained a STRUCT column S1, and a field F1 within the STRUCT
was a MAP, you could issue the statement DESCRIBE t1.s1.f1. An ARRAY is shown as a two-column table, with
ITEM and POS columns. A STRUCT is shown as a table with each field representing a column in the table. A MAP is
shown as a two-column table, with KEY and VALUE columns.
Added in: Impala 2.3.0
Restrictions:
• Columns with this data type can only be used in tables or partitions with the Parquet or ORC file format.
• Columns with this data type cannot be used as partition key columns in a partitioned table.
• The COMPUTE STATS statement does not produce any statistics for columns of this data type.
| Impala SQL Language Reference | 96
• The maximum length of the column definition for any complex type, including declarations for any nested types,
is 4000 characters.
• See Limitations and Restrictions for Complex Types on page 138 for a full list of limitations and associated
guidelines about complex type columns.
Kudu considerations:
Currently, the data types CHAR, VARCHAR, ARRAY, MAP, and STRUCT cannot be used with Kudu tables.
Examples:
Note: Many of the complex type examples refer to tables such as CUSTOMER and REGION adapted from the tables
used in the TPC-H benchmark. See Sample Schema and Data for Experimenting with Impala Complex Types on page
157 for the table definitions.
The following example shows how to construct a table with various kinds of ARRAY columns, both at the top level
and nested within other complex types. Whenever the ARRAY consists of a scalar value, such as in the PETS column
or the CHILDREN field, you can see that future expansion is limited. For example, you could not easily evolve the
schema to record the kind of pet or the child's birthday alongside the name. Therefore, it is more common to use an
ARRAY whose elements are of STRUCT type, to associate multiple fields with each array element.
Note: Practice the CREATE TABLE and query notation for complex type columns using empty tables, until you can
visualize a complex data structure and construct corresponding SQL statements reliably.
The following example shows how to examine the structure of a table containing one or more ARRAY columns by
using the DESCRIBE statement. You can visualize each ARRAY as its own two-column table, with columns ITEM
and POS.
DESCRIBE array_demo;
+--------------+---------------------------+
| name | type |
+--------------+---------------------------+
| id | bigint |
| Impala SQL Language Reference | 97
| name | string |
| pets | array<string> |
| marriages | array<struct< |
| | spouse:string, |
| | children:array<string> |
| | >> |
| places_lived | array<struct< |
| | place:string, |
| | start_year:int |
| | >> |
| ancestors | map<string,array<string>> |
+--------------+---------------------------+
DESCRIBE array_demo.pets;
+------+--------+
| name | type |
+------+--------+
| item | string |
| pos | bigint |
+------+--------+
DESCRIBE array_demo.marriages;
+------+--------------------------+
| name | type |
+------+--------------------------+
| item | struct< |
| | spouse:string, |
| | children:array<string> |
| | > |
| pos | bigint |
+------+--------------------------+
DESCRIBE array_demo.places_lived;
+------+------------------+
| name | type |
+------+------------------+
| item | struct< |
| | place:string, |
| | start_year:int |
| | > |
| pos | bigint |
+------+------------------+
DESCRIBE array_demo.ancestors;
+-------+---------------+
| name | type |
+-------+---------------+
| key | string |
| value | array<string> |
+-------+---------------+
The following example shows queries involving ARRAY columns containing elements of scalar or complex types.
You “unpack” each ARRAY column by referring to it in a join query, as if it were a separate table with ITEM and
POS columns. If the array element is a scalar type, you refer to its value using the ITEM pseudocolumn. If the array
element is a STRUCT, you refer to the STRUCT fields using dot notation and the field names. If the array element is
another ARRAY or a MAP, you use another level of join to unpack the nested collection elements.
-- Array of structs.
-- Now each array element has named fields, possibly of different types.
-- You can consider an ARRAY of STRUCT to represent a table inside another
table.
SELECT id, name, places_lived.pos, places_lived.item.place,
places_lived.item.start_year
FROM array_demo, array_demo.places_lived;
-- The .ITEM name is optional for array elements that are structs.
-- The following query is equivalent to the previous one, with .ITEM
-- removed from the column references.
SELECT id, name, places_lived.pos, places_lived.place,
places_lived.start_year
FROM array_demo, array_demo.places_lived;
-- To filter specific items from the array, do comparisons against the .POS
or .ITEM
-- pseudocolumns, or names of struct fields, in the WHERE clause.
SELECT id, name, pets.item FROM array_demo, array_demo.pets
WHERE pets.pos in (0, 1, 3);
Related information:
Complex Types (Impala 2.3 or higher only) on page 134, STRUCT Complex Type (Impala 2.3 or higher only) on
page 119, MAP Complex Type (Impala 2.3 or higher only) on page 111
column_name BIGINT
Usage notes:
BIGINT is a convenient type to use for column declarations because you can use any kind of integer values in
INSERT statements and they are promoted to BIGINT where necessary. However, BIGINT also requires the most
| Impala SQL Language Reference | 99
bytes of any integer type on disk and in memory, meaning your queries are not as efficient and scalable as possible if
you overuse this type. Therefore, prefer to use the smallest integer type with sufficient range to hold all input values,
and CAST() when necessary to the appropriate type.
For a convenient and automated way to check the bounds of the BIGINT type, call the functions MIN_BIGINT()
and MAX_BIGINT().
If an integer value is too large to be represented as a BIGINT, use a DECIMAL instead with sufficient digits of
precision.
NULL considerations: Casting any non-numeric value to this type produces a NULL value.
Partitioning: Prefer to use this type for a partition key column. Impala can process the numeric type more efficiently
than a STRING representation of the value.
HBase considerations: This data type is fully compatible with HBase tables.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Internal details: Represented in memory as an 8-byte value.
Added in: Available in all versions of Impala.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Related information:
Numeric Literals on page 165, TINYINT Data Type on page 131, SMALLINT Data Type on page 116, INT
Data Type on page 111, BIGINT Data Type on page 98, DECIMAL Data Type (Impala 3.0 or higher only) on
page 103, Impala Mathematical Functions on page 418
column_name BOOLEAN
Range: TRUE or FALSE. Do not use quotation marks around the TRUE and FALSE literal values. You can write
the literal values in uppercase, lowercase, or mixed case. The values queried from a table are always returned in
lowercase, true or false.
Conversions: Impala does not automatically convert any other type to BOOLEAN. All conversions must use an
explicit call to the CAST() function.
You can use CAST() to convert any integer or floating-point type to BOOLEAN: a value of 0 represents false, and
any non-zero value is converted to true.
When you cast the opposite way, from BOOLEAN to a numeric type, the result becomes either 1 or 0:
You can cast DECIMAL values to BOOLEAN, with the same treatment of zero and non-zero values as the other
numeric types. You cannot cast a BOOLEAN to a DECIMAL.
You cannot cast a STRING value to BOOLEAN, although you can cast a BOOLEAN value to STRING, returning '1'
for true values and '0' for false values.
Although you can cast a TIMESTAMP to a BOOLEAN or a BOOLEAN to a TIMESTAMP, the results are unlikely to
be useful. Any non-zero TIMESTAMP (that is, any value other than 1970-01-01 00:00:00) becomes TRUE
when converted to BOOLEAN, while 1970-01-01 00:00:00 becomes FALSE. A value of FALSE becomes
1970-01-01 00:00:00 when converted to BOOLEAN, and TRUE becomes one second past this epoch date, that
is, 1970-01-01 00:00:01.
NULL considerations: An expression of this type produces a NULL value if any argument of the expression is NULL.
Partitioning:
Do not use a BOOLEAN column as a partition key. Although you can create such a table, subsequent operations
produce errors:
Examples:
SELECT 1 < 2;
SELECT 2 = 5;
SELECT 100 < NULL, 100 > NULL;
CREATE TABLE assertions (claim STRING, really BOOLEAN);
INSERT INTO assertions VALUES
("1 is less than 2", 1 < 2),
("2 is the same as 5", 2 = 5),
("Grass is green", true),
("The moon is made of green cheese", false);
SELECT claim FROM assertions WHERE really = TRUE;
HBase considerations: This data type is fully compatible with HBase tables.
Parquet considerations: This type is fully compatible with Parquet tables.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Kudu considerations:
Currently, the data types BOOLEAN, FLOAT, and DOUBLE cannot be used for primary key columns in Kudu tables.
Related information: Boolean Literals on page 168, SQL Operators on page 170, Impala Conditional
Functions on page 481
| Impala SQL Language Reference | 101
column_name CHAR(length)
The Avro specification allows string values up to 2**64 bytes in length. Impala queries for Avro tables use 32-bit
integers to hold string lengths. In Impala 2.5 and higher, Impala truncates CHAR and VARCHAR values in Avro tables
to (2**31)-1 bytes. If a query encounters a STRING value longer than (2**31)-1 bytes in an Avro table, the query
fails. In earlier releases, encountering such long values in an Avro table could cause a crash.
Compatibility:
This type is available using Impala 2.0 or higher.
Some other database systems make the length specification optional. For Impala, the length is required.
Internal details: Represented in memory as a byte array with the same size as the length specification. Values that
are shorter than the specified length are padded on the right with trailing spaces.
Added in: Impala 2.0.0
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
UDF considerations: This type cannot be used for the argument or return type of a user-defined function (UDF) or
user-defined aggregate function (UDA).
Kudu considerations:
Currently, the data types CHAR, VARCHAR, ARRAY, MAP, and STRUCT cannot be used with Kudu tables.
Performance consideration:
The CHAR type currently does not have the Impala Codegen support, and we recommend using VARCHAR or
STRING over CHAR as the performance gain of Codegen outweighs the benefits of fixed width CHAR.
Restrictions:
Because the blank-padding behavior requires allocating the maximum length for each value in memory, for scalability
reasons, you should avoid declaring CHAR columns that are much longer than typical values in that column.
All data in CHAR and VARCHAR columns must be in a character encoding that is compatible with UTF-8. If you have
binary data from another database system (that is, a BLOB type), use a STRING column to hold it.
When an expression compares a CHAR with a STRING or VARCHAR, the CHAR value is implicitly converted to
STRING first, with trailing spaces preserved.
This behavior differs from other popular database systems. To get the expected result of TRUE, cast the expressions
on both sides to CHAR values of the appropriate length. For example:
Parquet uses DATE logical type for dates. The DATE logical type annotates an INT32 that stores the number of days
from the Unix epoch, January 1, 1970. This representation introduces a parquet interoperability issue between Impala
and older versions of Hive:
If Hive versions lower than 3.1 wrote dates earlier than 1582-10-15 to a parquet table, those dates will be read back
incorrectly by Impala and vice versa. In Hive 3.1 and higher, this is no longer an issue.
Explicit casting between DATE and other data types:
DATE type can only be converted to/from DATE, TIMESTAMP, or STRING types as described below.
DATE TIMESTAMP The year, month, and day of the DATE is returned along with the
time of day component set to 00:00:00.
DATE STRING The STRING value, 'yyyy-MM-dd', is returned.
DECIMAL[(precision[, scale])]
Precision:
precision represents the total number of digits that can be represented regardless of the location of the decimal point.
This value must be between 1 and 38, specified as an integer literal.
The default precision is 9.
Scale:
scale represents the number of fractional digits.
This value must be less than or equal to the precision, specified as an integer literal.
The default scale is 0.
| Impala SQL Language Reference | 104
When the precision and the scale are omitted, a DECIMAL is treated as DECIMAL(9, 0).
Range:
The range of DECIMAL type is -10^38 +1 through 10^38 –1.
The largest value is represented by DECIMAL(38, 0).
The most precise fractional value (between 0 and 1, or 0 and -1) is represented by DECIMAL(38, 38), with 38
digits to the right of the decimal point. The value closest to 0 would be .0000...1 (37 zeros and the final 1). The value
closest to 1 would be .999... (9 repeated 38 times).
Memory and disk storage:
Only the precision determines the storage size for DECIMAL values, and the scale setting has no effect on the storage
size. The following table describes the in-memory storage once the values are loaded into memory.
The on-disk representation varies depending on the file format of the table.
Text, RCFile, and SequenceFile tables use ASCII-based formats as below:
• Leading zeros are not stored.
• Trailing zeros are stored.
• Each DECIMAL value takes up as many bytes as the precision of the value, plus:
• One extra byte if the decimal point is present.
• One extra byte for negative values.
Parquet and Avro tables use binary formats and offer more compact storage for DECIMAL values. In these tables,
Impala stores each value in fewer bytes where possible depending on the precision specified for the DECIMAL
column. To conserve space in large tables, use the smallest-precision DECIMAL type.
Precision and scale in arithmetic operations:
For all arithmetic operations, the resulting precision is at most 38.
If the resulting precision would be greater than 38, Impala truncates the result from the back, but keeps at least 6
fractional digits in scale and rounds.
For example, DECIMAL(38, 20) * DECIMAL(38, 20) returns DECIMAL(38, 6). According to the table
below, the resulting precision and scale would be (77, 40), but they are higher than the maximum precision and
scale for DECIMAL. So, Impala sets the precision to the maximum allowed 38, and truncates the scale to 6.
When you use DECIMAL values in arithmetic operations, the precision and scale of the result value are determined as
follows. For better readability, the following terms are used in the table below:
• P1, P2: Input precisions
• S1, S2: Input scales
• L1, L2: Leading digits in input DECIMALs, i.e., L1 = P1 - S1 and L2 = P2 - S2
1 is for carry-over.
Multiplication P1 + P2 + 1 S1 + S2
| Impala SQL Language Reference | 105
Result: 9999999933815812510711506376257961984
The result has a loss of information due to implicit casting. This is why we discourage using the DOUBLE and
FLOAT types in general.
• DOUBLE and FLOAT cannot be implicitly converted to DECIMAL. An error is returned.
• DECIMAL is implicitly converted to DECIMAL if all digits fit in the resulting DECIMAL.
For example, the following query returns an error because the resulting type that guarantees that all digits fit
cannot be determined .
The above INSERT statement fails because TINYINT requires room for 3 digits to the left of the decimal point in
the DECIMAL.
The above INSERT statement succeeds because there is enough room for 3 digits to the left of the decimal point
that TINYINT requires.
In UNION, the resulting precision and scales are determined as follows.
• Precision: max (L1, L2) + max (S1, S2)
If the resulting type does not fit in the DECIMAL type, an error is returned. See the first example below.
• Scale: max (S1, S2)
Examples for UNION:
• DECIMAL(20, 0) UNION DECIMAL(20, 20) would require a DECIMAL(40, 20) to fit all the digits.
Since this is larger than the max precision for DECIMAL, Impala returns an error. One way to fix the error is to
cast both operands to the desired type, for example DECIMAL(38, 18).
• DECIMAL(20, 2) UNION DECIMAL(8, 6) returns DECIMAL(24, 6).
• INT UNION DECIMAL(9, 4) returns DECIMAL(14, 4).
INT has the precision 10 and the scale 0, so it is treated as DECIMAL(10, 0) UNION DECIMAL(9. 4).
Casting between DECIMAL and other data types:
To avoid potential conversion errors, use CAST to explicitly convert between DECIMAL and other types in decimal
assignments like in INSERT and UNION statements, or in functions like COALESCE:
• You can cast the following types to DECIMAL: FLOAT, TINYINT, SMALLINT, INT, BIGINT, STRING
• You can cast DECIMAL to the following types: FLOAT, TINYINT, SMALLINT, INT, BIGINT, STRING,
BOOLEAN, TIMESTAMP
Impala performs CAST between DECIMAL and other numeric types as below:
• Precision: If you cast a value with bigger precision than the precision of the destination type, Impala returns an
error. For example, CAST(123456 AS DECIMAL(3,0)) returns an error because all digits do not fit into
DECIMAL(3, 0)
• Scale: If you cast a value with more fractional digits than the scale of the destination type, the fractional digits are
rounded. For example, CAST(1.239 AS DECIMAL(3, 2)) returns 1.24.
Casting STRING to DECIMAL:
You can cast STRING of numeric characters in columns, literals, or expressions to DECIMAL as long as number fits
within the specified target DECIMAL type without overflow.
• If scale in STRING > scale in DECIMAL, the fractional digits are rounded to the DECIMAL scale.
For example, CAST('98.678912' AS DECIMAL(15, 1)) returns 98.7.
• If # leading digits in STRING > # leading digits in DECIMAL, an error is returned.
For example, CAST('123.45' AS DECIMAL(2, 2)) returns an error.
Exponential notation is supported when casting from STRING.
For example, CAST('1.0e6' AS DECIMAL(32, 0)) returns 1000000.
Casting any non-numeric value, such as 'ABC' to the DECIMAL type returns an error.
Casting DECIMAL to TIMESTAMP:
Casting a DECIMAL value N to TIMESTAMP produces a value that is N seconds past the start of the epoch date
(January 1, 1970).
| Impala SQL Language Reference | 107
• If the metadata in the data files disagrees with the metadata in the metastore database, Impala cancels the
query.
Partitioning:
Using a DECIMAL column as a partition key provides you a better match between the partition key values and the
HDFS directory names than using a DOUBLE or FLOAT partitioning column.
Column statistics considerations:
Because the DECIMAL type has a fixed size, the maximum and average size fields are always filled in for column
statistics, even before you run the COMPUTE STATS statement.
Compatibility with older version of DECIMAL:
This version of DECIMAL type is the default in Impala 3.0 and higher. The key differences between this version of
DECIMAL and the previous DECIMAL V1 in Impala 2.x include the following.
If you need to continue using the first version of the DECIMAL type for the backward compatibility of your queries,
set the DECIMAL_V2 query option to FALSE:
SET DECIMAL_V2=FALSE;
column_name DOUBLE
Examples:
Partitioning: Because fractional values of this type are not always represented precisely, when this type is used for a
partition key column, the underlying HDFS directories might not be named exactly as you expect. Prefer to partition
on a DECIMAL column instead.
HBase considerations: This data type is fully compatible with HBase tables.
Parquet considerations: This type is fully compatible with Parquet tables.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Internal details: Represented in memory as an 8-byte value.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Restrictions:
Due to the way arithmetic on FLOAT and DOUBLE columns uses high-performance hardware instructions, and
distributed queries can perform these operations in different order for each query, results can vary slightly for
aggregate function calls such as SUM() and AVG() for FLOAT and DOUBLE columns, particularly on large data
sets where millions or billions of values are summed or averaged. For perfect consistency and repeatability, use the
DECIMAL data type for such operations instead of FLOAT or DOUBLE.
The inability to exactly represent certain floating-point values means that DECIMAL is sometimes a better choice than
DOUBLE or FLOAT when precision is critical, particularly when transferring data from other database systems that
use different representations or file formats.
Kudu considerations:
Currently, the data types BOOLEAN, FLOAT, and DOUBLE cannot be used for primary key columns in Kudu tables.
Related information:
Numeric Literals on page 165, Impala Mathematical Functions on page 418, FLOAT Data Type on page 109
column_name FLOAT
Precision: 6 to 9 significant digits, depending on usage. The number of significant digits does not depend on the
position of the decimal point.
Representation: The values are stored in 4 bytes, using IEEE 754 Single Precision Binary Floating Point format.
Conversions: Impala automatically converts FLOAT to more precise DOUBLE values, but not the other way around.
You can use CAST() to convert FLOAT values to TINYINT, SMALLINT, INT, BIGINT, STRING, TIMESTAMP,
or BOOLEAN. You can use exponential notation in FLOAT literals or when casting from STRING, for example
1.0e6 to represent one million. Casting an integer or floating-point value N to TIMESTAMP produces a value that is
N seconds past the start of the epoch date (January 1, 1970). By default, the result value represents a date and time in
the UTC time zone. If the setting ‑‑use_local_tz_for_unix_timestamp_conversions=true is in effect, the resulting
TIMESTAMP represents a date and time in the local time zone.
Usage notes:
Impala does not evaluate NaN (not a number) as equal to any other numeric values, including other NaN values. For
example, the following statement, which evaluates equality between two NaN values, returns false:
Examples:
Partitioning: Because fractional values of this type are not always represented precisely, when this type is used for a
partition key column, the underlying HDFS directories might not be named exactly as you expect. Prefer to partition
on a DECIMAL column instead.
HBase considerations: This data type is fully compatible with HBase tables.
Parquet considerations: This type is fully compatible with Parquet tables.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Internal details: Represented in memory as a 4-byte value.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Restrictions:
Due to the way arithmetic on FLOAT and DOUBLE columns uses high-performance hardware instructions, and
distributed queries can perform these operations in different order for each query, results can vary slightly for
aggregate function calls such as SUM() and AVG() for FLOAT and DOUBLE columns, particularly on large data
sets where millions or billions of values are summed or averaged. For perfect consistency and repeatability, use the
DECIMAL data type for such operations instead of FLOAT or DOUBLE.
The inability to exactly represent certain floating-point values means that DECIMAL is sometimes a better choice than
DOUBLE or FLOAT when precision is critical, particularly when transferring data from other database systems that
use different representations or file formats.
Kudu considerations:
Currently, the data types BOOLEAN, FLOAT, and DOUBLE cannot be used for primary key columns in Kudu tables.
Related information:
Numeric Literals on page 165, Impala Mathematical Functions on page 418, DOUBLE Data Type on page
108
| Impala SQL Language Reference | 111
column_name INT
Partitioning: Prefer to use this type for a partition key column. Impala can process the numeric type more efficiently
than a STRING representation of the value.
HBase considerations: This data type is fully compatible with HBase tables.
Parquet considerations:
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Internal details: Represented in memory as a 4-byte value.
Added in: Available in all versions of Impala.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Related information:
Numeric Literals on page 165, TINYINT Data Type on page 131, SMALLINT Data Type on page 116, INT
Data Type on page 111, BIGINT Data Type on page 98, DECIMAL Data Type (Impala 3.0 or higher only) on
page 103, Impala Mathematical Functions on page 418
Usage notes:
Because complex types are often used in combination, for example an ARRAY of STRUCT elements, if you are
unfamiliar with the Impala complex types, start with Complex Types (Impala 2.3 or higher only) on page 134 for
background information and usage examples.
The MAP complex data type represents a set of key-value pairs. Each element of the map is indexed by a primitive
type such as BIGINT or STRING, letting you define sequences that are not continuous or categories with arbitrary
names. You might find it convenient for modelling data produced in other languages, such as a Python dictionary or
Java HashMap, where a single scalar value serves as the lookup key.
In a big data context, the keys in a map column might represent a numeric sequence of events during a manufacturing
process, or TIMESTAMP values corresponding to sensor observations. The map itself is inherently unordered, so you
choose whether to make the key values significant (such as a recorded TIMESTAMP) or synthetic (such as a random
global universal ID).
Note: Behind the scenes, the MAP type is implemented in a similar way as the ARRAY type. Impala does not enforce
any uniqueness constraint on the KEY values, and the KEY values are processed by looping through the elements
of the MAP rather than by a constant-time lookup. Therefore, this type is primarily for ease of understanding when
importing data and algorithms from non-SQL contexts, rather than optimizing the performance of key lookups.
You can pass a multi-part qualified name to DESCRIBE to specify an ARRAY, STRUCT, or MAP column and
visualize its structure as if it were a table. For example, if table T1 contains an ARRAY column A1, you could issue
the statement DESCRIBE t1.a1. If table T1 contained a STRUCT column S1, and a field F1 within the STRUCT
was a MAP, you could issue the statement DESCRIBE t1.s1.f1. An ARRAY is shown as a two-column table, with
ITEM and POS columns. A STRUCT is shown as a table with each field representing a column in the table. A MAP is
shown as a two-column table, with KEY and VALUE columns.
Added in: Impala 2.3.0
Restrictions:
• Columns with this data type can only be used in tables or partitions with the Parquet or ORC file format.
• Columns with this data type cannot be used as partition key columns in a partitioned table.
• The COMPUTE STATS statement does not produce any statistics for columns of this data type.
• The maximum length of the column definition for any complex type, including declarations for any nested types,
is 4000 characters.
• See Limitations and Restrictions for Complex Types on page 138 for a full list of limitations and associated
guidelines about complex type columns.
Kudu considerations:
Currently, the data types CHAR, VARCHAR, ARRAY, MAP, and STRUCT cannot be used with Kudu tables.
Examples:
Note: Many of the complex type examples refer to tables such as CUSTOMER and REGION adapted from the tables
used in the TPC-H benchmark. See Sample Schema and Data for Experimenting with Impala Complex Types on page
157 for the table definitions.
The following example shows a table with various kinds of MAP columns, both at the top level and nested within
other complex types. Each row represents information about a specific country, with complex type fields of various
levels of nesting to represent different information associated with the country: factual measurements such as area
and population, notable people in different categories, geographic features such as cities, points of interest within
each city, and mountains with associated facts. Practice the CREATE TABLE and query notation for complex type
columns using empty tables, until you can visualize a complex data structure and construct corresponding SQL
statements reliably.
country_id BIGINT,
-- MAP that is an element within an ARRAY. The MAP is inside a STRUCT field
to associate
-- the mountain name with all the facts about the mountain.
-- The "key" of the map (the first STRING field) represents the name of some
fact whose value
-- can be expressed as an integer, such as 'Height', 'Year First Climbed',
and so on.
mountains ARRAY < STRUCT < name: STRING, facts: MAP <STRING, INT > > >
)
STORED AS PARQUET;
DESCRIBE map_demo;
+------------+------------------------------------------------+
| name | type |
+------------+------------------------------------------------+
| country_id | bigint |
| metrics | map<string,bigint> |
| notables | map<string,array<string>> |
| cities | array<struct< |
| | name:string, |
| | points_of_interest:map<string,array<string>> |
| | >> |
| mountains | array<struct< |
| | name:string, |
| | facts:map<string,int> |
| | >> |
+------------+------------------------------------------------+
DESCRIBE map_demo.metrics;
+-------+--------+
| name | type |
+-------+--------+
| key | string |
| Impala SQL Language Reference | 114
| value | bigint |
+-------+--------+
DESCRIBE map_demo.notables;
+-------+---------------+
| name | type |
+-------+---------------+
| key | string |
| value | array<string> |
+-------+---------------+
DESCRIBE map_demo.notables.value;
+------+--------+
| name | type |
+------+--------+
| item | string |
| pos | bigint |
+------+--------+
DESCRIBE map_demo.cities;
+------+------------------------------------------------+
| name | type |
+------+------------------------------------------------+
| item | struct< |
| | name:string, |
| | points_of_interest:map<string,array<string>> |
| | > |
| pos | bigint |
+------+------------------------------------------------+
DESCRIBE map_demo.cities.item.points_of_interest;
+-------+---------------+
| name | type |
+-------+---------------+
| key | string |
| value | array<string> |
+-------+---------------+
DESCRIBE map_demo.cities.item.points_of_interest.value;
+------+--------+
| name | type |
+------+--------+
| item | string |
| pos | bigint |
+------+--------+
DESCRIBE map_demo.mountains;
+------+-------------------------+
| name | type |
+------+-------------------------+
| item | struct< |
| | name:string, |
| | facts:map<string,int> |
| | > |
| pos | bigint |
+------+-------------------------+
DESCRIBE map_demo.mountains.item.facts;
+-------+--------+
| name | type |
+-------+--------+
| key | string |
| value | int |
+-------+--------+
| Impala SQL Language Reference | 115
The following example shows a table that uses a variety of data types for the MAP “key” field. Typically, you use
BIGINT or STRING to use numeric or character-based keys without worrying about exceeding any size or length
constraints.
CREATE TABLE celebrities (name STRING, birth_year MAP < STRING, SMALLINT >)
STORED AS PARQUET;
-- A typical row might represent values with 2 different birth years, such
as:
-- ("Joe Movie Star", { "real": 1972, "claimed": 1977 })
CREATE TABLE countries (name STRING, famous_leaders MAP < INT, STRING >)
STORED AS PARQUET;
-- A typical row might represent values with different leaders, with key
values corresponding to their numeric sequence, such as:
-- ("United States", { 1: "George Washington", 3: "Thomas Jefferson", 16:
"Abraham Lincoln" })
CREATE TABLE airlines (name STRING, special_meals MAP < STRING, MAP <
STRING, STRING > >) STORED AS PARQUET;
-- A typical row might represent values with multiple kinds of meals, each
with several components:
-- ("Elegant Airlines",
-- {
-- "vegetarian": { "breakfast": "pancakes", "snack": "cookies",
"dinner": "rice pilaf" },
-- "gluten free": { "breakfast": "oatmeal", "snack": "fruit", "dinner":
"chicken" }
-- } )
Related information:
Complex Types (Impala 2.3 or higher only) on page 134, ARRAY Complex Type (Impala 2.3 or higher only) on
page 95, STRUCT Complex Type (Impala 2.3 or higher only) on page 119
These examples show how you can use the type names REAL and DOUBLE interchangeably, and behind the scenes
Impala treats them always as DOUBLE.
column_name SMALLINT
Parquet considerations:
Physically, Parquet files represent TINYINT and SMALLINT values as 32-bit integers. Although Impala rejects
attempts to insert out-of-range values into such columns, if you create a new table with the CREATE TABLE ...
LIKE PARQUET syntax, any TINYINT or SMALLINT columns in the original table turn into INT columns in the
new table.
Partitioning: Prefer to use this type for a partition key column. Impala can process the numeric type more efficiently
than a STRING representation of the value.
HBase considerations: This data type is fully compatible with HBase tables.
| Impala SQL Language Reference | 117
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Internal details: Represented in memory as a 2-byte value.
Added in: Available in all versions of Impala.
Column statistics considerations: Because this type has a fixed size, the maximum and average size fields are
always filled in for column statistics, even before you run the COMPUTE STATS statement.
Related information:
Numeric Literals on page 165, TINYINT Data Type on page 131, SMALLINT Data Type on page 116, INT
Data Type on page 111, BIGINT Data Type on page 98, DECIMAL Data Type (Impala 3.0 or higher only) on
page 103, Impala Mathematical Functions on page 418
column_name STRING
Length:
If you need to manipulate string values with precise or maximum lengths, in Impala 2.0 and higher you can declare
columns as VARCHAR(max_length) or CHAR(length), but for best performance use STRING where practical.
Take the following considerations for STRING lengths:
• The hard limit on the size of a STRING and the total size of a row is 2 GB.
If a query tries to process or create a string larger than this limit, it will return an error to the user.
• The limit is 1 GB on STRING when writing to Parquet files.
• Queries operating on strings with 32 KB or less will work reliably and will not hit significant performance or
memory problems (unless you have very complex queries, very many columns, etc.)
• Performance and memory consumption may degrade with strings larger than 32 KB.
• The row size, i.e. the total size of all string and other columns, is subject to lower limits at various points in query
execution that support spill-to-disk. A few examples for lower row size limits are:
• Rows coming from the right side of any hash join
• Rows coming from either side of a hash join that spills to disk
• Rows being sorted by the SORT operator without a limit
• Rows in a grouping aggregation
In Impala 2.9 and lower, the default limit of the row size in the above cases is 8 MB.
In Impala 2.10 and higher, the max row size is configurable on a per-query basis with the MAX_ROW_SIZE query
option. Rows up to MAX_ROW_SIZE (which defaults to 512 KB) can always be processed in the above cases.
Rows larger than MAX_ROW_SIZE are processed on a best-effort basis. See MAX_ROW_SIZE for more details.
Character sets:
For full support in all Impala subsystems, restrict string values to the ASCII character set. Although some UTF-8
character data can be stored in Impala and retrieved through queries, UTF-8 strings containing non-ASCII characters
are not guaranteed to work properly in combination with many SQL aspects, including but not limited to:
• String manipulation functions.
• Comparison operators.
• The ORDER BY clause.
• Values in partition key columns.
| Impala SQL Language Reference | 118
For any national language aspects such as collation order or interpreting extended ASCII variants such as ISO-8859-1
or ISO-8859-2 encodings, Impala does not include such metadata with the table definition. If you need to sort,
manipulate, or display data depending on those national language characteristics of string data, use logic on the
application side.
Conversions:
• Impala does not automatically convert STRING to any numeric type. Impala does automatically convert STRING
to TIMESTAMP if the value matches one of the accepted TIMESTAMP formats; see TIMESTAMP Data Type on
page 125 for details.
• You can use CAST() to convert STRING values to TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, or
TIMESTAMP.
• You cannot directly cast a STRING value to BOOLEAN. You can use a CASE expression to evaluate string values
such as 'T', 'true', and so on and return Boolean true and false values as appropriate.
• You can cast a BOOLEAN value to STRING, returning '1' for true values and '0' for false values.
Partitioning:
Although it might be convenient to use STRING columns for partition keys, even when those columns contain
numbers, for performance and scalability it is much better to use numeric columns as partition keys whenever
practical. Although the underlying HDFS directory name might be the same in either case, the in-memory storage
for the partition key columns is more compact, and computations are faster, if partition key columns such as YEAR,
MONTH, DAY and so on are declared as INT, SMALLINT, and so on.
Zero-length strings: For purposes of clauses such as DISTINCT and GROUP BY, Impala considers zero-length
strings (""), NULL, and space to all be different values.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
Avro considerations:
The Avro specification allows string values up to 2**64 bytes in length. Impala queries for Avro tables use 32-bit
integers to hold string lengths. In Impala 2.5 and higher, Impala truncates CHAR and VARCHAR values in Avro tables
to (2**31)-1 bytes. If a query encounters a STRING value longer than (2**31)-1 bytes in an Avro table, the query
fails. In earlier releases, encountering such long values in an Avro table could cause a crash.
Column statistics considerations: Because the values of this type have variable size, none of the column statistics
fields are filled in until you run the COMPUTE STATS statement.
Examples:
The following examples demonstrate double-quoted and single-quoted string literals, and required escaping for
quotation marks within string literals:
The following examples demonstrate calls to string manipulation functions to concatenate strings, convert numbers to
strings, or pull out substrings:
SELECT CONCAT("Once upon a time, there were ", CAST(3 AS STRING), ' little
pigs.');
SELECT SUBSTR("hello world",7,5);
The following examples show how to perform operations on STRING columns within a table:
Related information:
String Literals on page 167, CHAR Data Type (Impala 2.0 or higher only) on page 101, VARCHAR Data
Type (Impala 2.0 or higher only) on page 132, Impala String Functions on page 488, Impala Date and Time
Functions on page 457
The names and number of fields within the STRUCT are fixed. Each field can be a different type. A field within a
STRUCT can also be another STRUCT, or an ARRAY or a MAP, allowing you to create nested data structures with a
maximum nesting depth of 100.
A STRUCT can be the top-level type for a column, or can itself be an item within an ARRAY or the value part of the
key-value pair in a MAP.
When a STRUCT is used as an ARRAY element or a MAP value, you use a join clause to bring the ARRAY or MAP
elements into the result set, and then refer to array_name.ITEM.field or map_name.VALUE.field. In the
case of a STRUCT directly inside an ARRAY or MAP, you can omit the .ITEM and .VALUE pseudocolumns and refer
directly to array_name.field or map_name.field.
Usage notes:
Because complex types are often used in combination, for example an ARRAY of STRUCT elements, if you are
unfamiliar with the Impala complex types, start with Complex Types (Impala 2.3 or higher only) on page 134 for
background information and usage examples.
A STRUCT is similar conceptually to a table row: it contains a fixed number of named fields, each with a predefined
type. To combine two related tables, while using complex types to minimize repetition, the typical way to represent
that data is as an ARRAY of STRUCT elements.
Because a STRUCT has a fixed number of named fields, it typically does not make sense to have a STRUCT as the
type of a table column. In such a case, you could just make each field of the STRUCT into a separate column of the
table. The STRUCT type is most useful as an item of an ARRAY or the value part of the key-value pair in a MAP. A
nested type column with a STRUCT at the lowest level lets you associate a variable number of row-like objects with
each row of the table.
The STRUCT type is straightforward to reference within a query. You do not need to include the STRUCT column
in a join clause or give it a table alias, as is required for the ARRAY and MAP types. You refer to the individual fields
using dot notation, such as struct_column_name.field_name, without any pseudocolumn such as ITEM or
VALUE.
You can pass a multi-part qualified name to DESCRIBE to specify an ARRAY, STRUCT, or MAP column and
visualize its structure as if it were a table. For example, if table T1 contains an ARRAY column A1, you could issue
the statement DESCRIBE t1.a1. If table T1 contained a STRUCT column S1, and a field F1 within the STRUCT
was a MAP, you could issue the statement DESCRIBE t1.s1.f1. An ARRAY is shown as a two-column table, with
ITEM and POS columns. A STRUCT is shown as a table with each field representing a column in the table. A MAP is
shown as a two-column table, with KEY and VALUE columns.
Internal details:
| Impala SQL Language Reference | 120
Within the Parquet data file, the values for each STRUCT field are stored adjacent to each other, so that they can be
encoded and compressed using all the Parquet techniques for storing sets of similar or repeated values. The adjacency
applies even when the STRUCT values are part of an ARRAY or MAP. During a query, Impala avoids unnecessary I/O
by reading only the portions of the Parquet data file containing the requested STRUCT fields.
Added in: Impala 2.3.0
Restrictions:
• Columns with this data type can only be used in tables or partitions with the Parquet or ORC file format.
• Columns with this data type cannot be used as partition key columns in a partitioned table.
• The COMPUTE STATS statement does not produce any statistics for columns of this data type.
• The maximum length of the column definition for any complex type, including declarations for any nested types,
is 4000 characters.
• See Limitations and Restrictions for Complex Types on page 138 for a full list of limitations and associated
guidelines about complex type columns.
Kudu considerations:
Currently, the data types CHAR, VARCHAR, ARRAY, MAP, and STRUCT cannot be used with Kudu tables.
Examples:
Note: Many of the complex type examples refer to tables such as CUSTOMER and REGION adapted from the tables
used in the TPC-H benchmark. See Sample Schema and Data for Experimenting with Impala Complex Types on page
157 for the table definitions.
The following example shows a table with various kinds of STRUCT columns, both at the top level and nested within
other complex types. Practice the CREATE TABLE and query notation for complex type columns using empty tables,
until you can visualize a complex data structure and construct corresponding SQL statements reliably.
The following example shows how to examine the structure of a table containing one or more STRUCT columns by
using the DESCRIBE statement. You can visualize each STRUCT as its own table, with columns named the same as
each field of the STRUCT. If the STRUCT is nested inside another complex type, such as ARRAY, you can extend the
qualified name passed to DESCRIBE until the output shows just the STRUCT fields.
DESCRIBE struct_demo;
+-------------------+--------------------------+
| Impala SQL Language Reference | 121
| name | type |
+-------------------+--------------------------+
| id | bigint |
| name | string |
| employee_info | struct< |
| | employer:string, |
| | id:bigint, |
| | address:string |
| | > |
| places_lived | array<struct< |
| | street:string, |
| | city:string, |
| | country:string |
| | >> |
| memorable_moments | map<string,struct< |
| | year:int, |
| | place:string, |
| | details:string |
| | >> |
| current_address | struct< |
| | street_address:struct< |
| | street_number:int, |
| | street_name:string, |
| | street_type:string |
| | >, |
| | country:string, |
| | postal_code:string |
| | > |
+-------------------+--------------------------+
DESCRIBE struct_demo.employee_info;
+----------+--------+
| name | type |
+----------+--------+
| employer | string |
| id | bigint |
| address | string |
+----------+--------+
Because PLACES_LIVED is a STRUCT inside an ARRAY, the initial DESCRIBE shows the structure of the ARRAY:
DESCRIBE struct_demo.places_lived;
+------+------------------+
| name | type |
+------+------------------+
| item | struct< |
| | street:string, |
| | city:string, |
| | country:string |
| | > |
| pos | bigint |
+------+------------------+
Ask for the details of the ITEM field of the ARRAY to see just the layout of the STRUCT:
DESCRIBE struct_demo.places_lived.item;
| Impala SQL Language Reference | 122
+---------+--------+
| name | type |
+---------+--------+
| street | string |
| city | string |
| country | string |
+---------+--------+
Likewise, MEMORABLE_MOMENTS has a STRUCT inside a MAP, which requires an extra level of qualified name to
see just the STRUCT part:
DESCRIBE struct_demo.memorable_moments;
+-------+------------------+
| name | type |
+-------+------------------+
| key | string |
| value | struct< |
| | year:int, |
| | place:string, |
| | details:string |
| | > |
+-------+------------------+
For a MAP, ask to see the VALUE field to see the corresponding STRUCT fields in a table-like structure:
DESCRIBE struct_demo.memorable_moments.value;
+---------+--------+
| name | type |
+---------+--------+
| year | int |
| place | string |
| details | string |
+---------+--------+
For a STRUCT inside a STRUCT, we can see the fields of the outer STRUCT:
DESCRIBE struct_demo.current_address;
+----------------+-----------------------+
| name | type |
+----------------+-----------------------+
| street_address | struct< |
| | street_number:int, |
| | street_name:string, |
| | street_type:string |
| | > |
| country | string |
| postal_code | string |
+----------------+-----------------------+
Then we can use a further qualified name to see just the fields of the inner STRUCT:
DESCRIBE struct_demo.current_address.street_address;
+---------------+--------+
| name | type |
+---------------+--------+
| street_number | int |
| street_name | string |
| street_type | string |
| Impala SQL Language Reference | 123
+---------------+--------+
The following example shows how to examine the structure of a table containing one or more STRUCT columns by
using the DESCRIBE statement. You can visualize each STRUCT as its own table, with columns named the same as
each field of the STRUCT. If the STRUCT is nested inside another complex type, such as ARRAY, you can extend the
qualified name passed to DESCRIBE until the output shows just the STRUCT fields.
DESCRIBE struct_demo;
+-------------------+--------------------------+---------+
| name | type | comment |
+-------------------+--------------------------+---------+
| id | bigint | |
| name | string | |
| employee_info | struct< | |
| | employer:string, | |
| | id:bigint, | |
| | address:string | |
| | > | |
| places_lived | array<struct< | |
| | street:string, | |
| | city:string, | |
| | country:string | |
| | >> | |
| memorable_moments | map<string,struct< | |
| | year:int, | |
| | place:string, | |
| | details:string | |
| | >> | |
| current_address | struct< | |
| | street_address:struct< | |
| | street_number:int, | |
| | street_name:string, | |
| | street_type:string | |
| | >, | |
| | country:string, | |
| | postal_code:string | |
| | > | |
+-------------------+--------------------------+---------+
For example, this table uses a struct that encodes several data values for each phone number associated with a person.
Each person can have a variable-length array of associated phone numbers, and queries can refer to the category field
to locate specific home, work, mobile, and so on kinds of phone numbers.
Because structs are naturally suited to composite values where the fields have different data types, you might use
them to decompose things such as addresses:
In a big data context, splitting out data fields such as the number part of the address and the street name could let
you do analysis on each field independently. For example, which streets have the largest number range of addresses,
what are the statistical properties of the street names, which areas have a higher proportion of “Roads”, “Courts” or
“Boulevards”, and so on.
| Impala SQL Language Reference | 125
Related information:
Complex Types (Impala 2.3 or higher only) on page 134, ARRAY Complex Type (Impala 2.3 or higher only) on
page 95, MAP Complex Type (Impala 2.3 or higher only) on page 111
column_name TIMESTAMP
In STRING to TIMESTAMP conversions, leading and trailing white spaces, such as a space, a tab, a newline, or
a carriage return, are ignored. For example, Impala treats the following as equivalent: '1999‑12‑01 01:02:03 ',
' 1999‑12‑01 01:02:03', '1999‑12‑01 01:02:03\r\n\t'.
When you convert or cast a STRING literal to TIMESTAMP, you can use the following separators between the date
part and the time part:
• One or more space characters
Example: CAST('2001-01-09 01:05:01' AS TIMESTAMP)
• The character “T”
Example: CAST('2001-01-09T01:05:01' AS TIMESTAMP)
Casting an integer or floating-point value N to TIMESTAMP produces a value that is N seconds past the start of the
epoch date (January 1, 1970). By default, the result value represents a date and time in the UTC time zone. If the
setting ‑‑use_local_tz_for_unix_timestamp_conversions=true is in effect, the resulting TIMESTAMP represents a
date and time in the local time zone.
In Impala 1.3 and higher, the FROM_UNIXTIME() and UNIX_TIMESTAMP() functions allow a wider range of
format strings, with more flexibility in element order, repetition of letter placeholders, and separator characters. In
Impala 2.3 and higher, the UNIX_TIMESTAMP() function also allows a numeric timezone offset to be specified as
part of the input string. See Impala Date and Time Functions on page 457 for details.
In Impala 2.2.0 and higher, built-in functions that accept or return integers representing TIMESTAMP values use the
BIGINT type for parameters and return values, rather than INT. This change lets the date and time functions avoid
an overflow error that would otherwise occur on January 19th, 2038 (known as the “Year 2038 problem” or “Y2K38
problem”). This change affects the FROM_UNIXTIME() and UNIX_TIMESTAMP() functions. You might need to
change application code that interacts with these functions, change the types of columns that store the return values,
or add CAST() calls to SQL statements that call these functions.
Partitioning:
Although you cannot use a TIMESTAMP column as a partition key, you can extract the individual years, months,
days, hours, and so on and partition based on those columns. Because the partition key column values are
represented in HDFS directory names, rather than as fields in the data files themselves, you can also keep the original
TIMESTAMP values if desired, without duplicating data or wasting storage space. See Partition Key Columns on page
667 for more details on partitioning with date and time values.
NULL considerations: Casting any unrecognized STRING value to this type produces a NULL value.
HBase considerations: This data type is fully compatible with HBase tables.
Parquet consideration: INT96 encoded Parquet timestamps are supported in Impala. INT64 timestamps are
supported in Impala 3.2 and higher.
• Any nanoseconds in the original 96-bit value produced by Impala are not stored, because Kudu represents date/
time columns using 64-bit values. The nanosecond portion of the value is rounded, not truncated. Therefore, a
TIMESTAMP value that you store in a Kudu table might not be bit-for-bit identical to the value returned by a
query.
• The conversion between the Impala 96-bit representation and the Kudu 64-bit representation introduces some
performance overhead when reading or writing TIMESTAMP columns. You can minimize the overhead during
writes by performing inserts through the Kudu API. Because the overhead during reads applies to each query, you
might continue to use a BIGINT column to represent date/time values in performance-critical applications.
• The Impala TIMESTAMP type has a narrower range for years than the underlying Kudu data type. Impala
can represent years 1400-9999. If year values outside this range are written to a Kudu table by a non-Impala
client, Impala returns NULL by default when reading those TIMESTAMP values during a query. Or, if the
ABORT_ON_ERROR query option is enabled, the query fails when it encounters a value with an out-of-range year.
Restrictions:
If you cast a STRING with an unrecognized format to a TIMESTAMP, the result is NULL rather than an error. Make
sure to test your data pipeline to be sure any textual date and time values are in a format that Impala TIMESTAMP can
recognize.
Currently, Avro tables cannot contain TIMESTAMP columns. If you need to store date and time values in Avro
tables, as a workaround you can use a STRING representation of the values, convert the values to BIGINT with the
UNIX_TIMESTAMP() function, or create separate numeric columns for individual date and time fields using the
EXTRACT() function.
Examples:
The following examples demonstrate using TIMESTAMP values with built-in functions:
The following examples demonstrate using TIMESTAMP values with HDFS-backed tables:
The following examples demonstrate using TIMESTAMP values with Kudu tables:
-- Values with 1-499 nanoseconds are rounded down in the Kudu TIMESTAMP
column.
insert into timestamp_t values (2, cast(now() + interval 100 nanoseconds as
string), now() + interval 100 nanoseconds, unix_timestamp(now() + interval
100 nanoseconds));
insert into timestamp_t values (3, cast(now() + interval 499 nanoseconds as
string), now() + interval 499 nanoseconds, unix_timestamp(now() + interval
499 nanoseconds));
-- The string representation shows how underlying Impala TIMESTAMP can have
nanosecond precision.
-- The TIMESTAMP column shows how timestamps in a Kudu table are rounded to
microsecond precision.
-- The BIGINT column represents seconds past the epoch and so if not
affected much by nanoseconds.
select s, t, b from timestamp_t order by t;
+-------------------------------+-------------------------------
+------------+
| s | t | b
|
+-------------------------------+-------------------------------
+------------+
| 2017-05-31 15:30:05.107157000 | 2017-05-31 15:30:05.107157000 | 1496244605
|
| 2017-05-31 15:30:28.868151100 | 2017-05-31 15:30:28.868151000 | 1496244628
|
| 2017-05-31 15:34:33.674692499 | 2017-05-31 15:34:33.674692000 | 1496244873
|
| 2017-05-31 15:35:04.769166500 | 2017-05-31 15:35:04.769167000 | 1496244904
|
| 2017-05-31 15:35:33.033082501 | 2017-05-31 15:35:33.033083000 | 1496244933
|
+-------------------------------+-------------------------------
+------------+
• By default, Impala uses the OS’s time zone database located in /usr/share/zoneinfo. This directory
contains the IANA timezone database in a compiled binary format. The contents of the zoneinfo directory is
controlled by the OS’s package manager.
• Use the following start-up flags to customize the time zone definitions.
• ‑‑hdfs_zone_info_zip: This flag allows Impala administrators to specify a custom timezone database. The flag
should be set to a shared (not necessarily HDFS) path that points to a zip archive of a custom IANA timezone
database. The timezone database is expected to be in a compiled binary format. If the startup flag is set, Impala
will use the specified timezone database instead of the default /usr/share/zoneinfo database. The
timezone db upgrade process is described in detail below.
• ‑‑hdfs_zone_alias_conf: This flag allows Impala administrators to specify definitions for custom timezone
aliases. The flag should be set to a shared (not necessarily HDFS) path that specifies a config file containing
custom timezone alias definitions. This config file can be used as a workaround for users who want to keep
using their legacy timezone names. Configuring custom aliases is described in detail below.
Upgrading custom IANA time zone database:
1. Download latest IANA time zone database distribution:
https://www.iana.org/time-zones/repository
2. Build timezone tools:
cd tz
make TOPDIR=tzdata install
3. Generate the compiled binary time zone database:
pushd ./tzdata/etc
zip -r zoneinfo.zip zoneinfo
popd
5. Copy the time zone database to HDFS:
#
# Define aliases for existing timezone names:
#
Universal Coordinated Time = UTC
Mideast/Riyadh89 = Asia/Riyadh
PDT = America/Los_Angeles
#
| Impala SQL Language Reference | 131
column_name TINYINT
Parquet considerations:
Physically, Parquet files represent TINYINT and SMALLINT values as 32-bit integers. Although Impala rejects
attempts to insert out-of-range values into such columns, if you create a new table with the CREATE TABLE ...
LIKE PARQUET syntax, any TINYINT or SMALLINT columns in the original table turn into INT columns in the
new table.
HBase considerations: This data type is fully compatible with HBase tables.
Text table considerations: Values of this type are potentially larger in text tables than in tables using Parquet or
other binary formats.
| Impala SQL Language Reference | 132
column_name VARCHAR(max_length)
| [b] |
| [hello] |
| [world] |
+---------+
The following example shows how identical VARCHAR values compare as equal, even if the columns are defined with
different maximum lengths. Both tables contain 'a' and 'b' values. The longer 'hello' and 'world' values
from the VARCHAR_20 table were truncated when inserted into the VARCHAR_1 table.
The following examples show how VARCHAR values are freely interchangeable with STRING values in contexts such
as comparison operators and built-in functions:
UDF considerations: This type cannot be used for the argument or return type of a user-defined function (UDF) or
user-defined aggregate function (UDA).
Related information:
STRING Data Type on page 117, CHAR Data Type (Impala 2.0 or higher only) on page 101, String Literals on
page 167, Impala String Functions on page 488
Once you understand the basics of complex types, refer to the individual type topics when you need to refresh your
memory about syntax and examples:
• ARRAY Complex Type (Impala 2.3 or higher only) on page 95
• STRUCT Complex Type (Impala 2.3 or higher only) on page 119
• MAP Complex Type (Impala 2.3 or higher only) on page 111
• Your data model originates with a non-SQL programming language or a NoSQL data management system.
For example, if you are representing Python data expressed as nested lists, dictionaries, and tuples, those data
structures correspond closely to Impala ARRAY, MAP, and STRUCT types.
• Your analytic queries involving multiple tables could benefit from greater locality during join processing. By
packing more related data items within each HDFS data block, complex types let join queries avoid the network
overhead of the traditional Hadoop shuffle or broadcast join techniques.
The Impala complex type support produces result sets with all scalar values, and the scalar components of complex
types can be used with all SQL clauses, such as GROUP BY, ORDER BY, all kinds of joins, subqueries, and inline
views. The ability to process complex type data entirely in SQL reduces the need to write application-specific code in
Java or other programming languages to deconstruct the underlying data structures.
The one exception to the preceding rule is COUNT(*) queries on RCFile tables that include complex types. Such
queries are allowed in Impala 2.6 and higher.
You can perform DDL operations for tables involving complex types in most file formats other than Parquet or ORC.
You cannot create tables in Impala with complex types using text files.
You can have a partitioned table with complex type columns that uses a format other than Parquet or ORC, and use
ALTER TABLE to change the file format to Parquet/ORC for individual partitions. When you put Parquet/ORC files
into those partitions, Impala can execute queries against that data as long as the query does not involve any of the
non-Parquet and non-ORC partitions.
If you use the parquet-tools command to examine the structure of a Parquet data file that includes complex
types, you see that both ARRAY and MAP are represented as a Bag in Parquet terminology, with all fields marked
Optional because Impala allows any column to be nullable.
Impala supports either 2-level and 3-level encoding within each Parquet data file. When constructing Parquet data
files outside Impala, use either encoding style but do not mix 2-level and 3-level encoding within the same data file.
Choosing Between Complex Types and Normalized Tables
Choosing between multiple normalized fact and dimension tables, or a single table containing complex types, is an
important design decision.
• If you are coming from a traditional database or data warehousing background, you might be familiar with how to
split up data between tables. Your business intelligence tools might already be optimized for dealing with this kind
of multi-table scenario through join queries.
• If you are pulling data from Impala into an application written in a programming language that has data structures
analogous to the complex types, such as Python or Java, complex types in Impala could simplify data interchange
and improve understandability and reliability of your program logic.
• You might already be faced with existing infrastructure or receive high volumes of data that assume one layout
or the other. For example, complex types are popular with web-oriented applications, for example to keep
information about an online user all in one place for convenient lookup and analysis, or to deal with sparse or
constantly evolving data fields.
• If some parts of the data change over time while related data remains constant, using multiple normalized tables
lets you replace certain parts of the data without reloading the entire data set. Conversely, if you receive related
data all bundled together, such as in JSON files, using complex types can save the overhead of splitting the related
items across multiple tables.
• From a performance perspective:
• In Parquet or ORC tables, Impala can skip columns that are not referenced in a query, avoiding the I/O penalty
of reading the embedded data. When complex types are nested within a column, the data is physically divided
at a very granular level; for example, a query referring to data nested multiple levels deep in a complex type
column does not have to read all the data from that column, only the data for the relevant parts of the column
type hierarchy.
• Complex types avoid the possibility of expensive join queries when data from fact and dimension tables is
processed in parallel across multiple hosts. All the information for a row containing complex types is typically
to be in the same data block, and therefore does not need to be transmitted across the network when joining
fields that are all part of the same row.
• The tradeoff with complex types is that fewer rows fit in each data block. Whether it is better to have more
data blocks with fewer rows, or fewer data blocks with many rows, depends on the distribution of your data
and the characteristics of your query workload. If the complex columns are rarely referenced, using them
might lower efficiency. If you are seeing low parallelism due to a small volume of data (relatively few data
blocks) in each table partition, increasing the row size by including complex columns might produce more data
blocks and thus spread the work more evenly across the cluster. See Scalability Considerations for Impala on
page 633 for more on this advanced topic.
| Impala SQL Language Reference | 138
Currently, Impala built-in functions and user-defined functions cannot accept complex types as parameters or produce
them as function return values. (When the complex type values are materialized in an Impala result set, the result set
contains the scalar components of the values, such as the POS or ITEM for an ARRAY, the KEY or VALUE for a MAP,
or the fields of a STRUCT; these scalar data items can be used with built-in functions and UDFs as usual.)
Impala currently cannot write new data files containing complex type columns. Therefore, although the SELECT
statement works for queries involving complex type columns, you cannot use a statement form that writes data to
complex type columns, such as CREATE TABLE AS SELECT or INSERT ... SELECT. To create data files
containing complex type data, use the Hive INSERT statement, or another ETL mechanism such as MapReduce jobs,
Spark jobs, Pig, and so on.
Currently, Impala can query complex type columns only from Parquet/ORC tables or Parquet/ORC partitions within
partitioned tables. Although you can use complex types in tables with Avro, text, and other file formats as part of your
ETL pipeline, for example as intermediate tables populated through Hive, doing analytics through Impala requires
that the data eventually ends up in a Parquet/ORC table. The requirement for Parquet/ORC data files means that you
can use complex types with Impala tables hosted on other kinds of file storage systems such as Isilon and Amazon S3,
but you cannot use Impala to query complex types from HBase tables. See File Format Support for Impala Complex
Types on page 136 for more details.
primitive_type
| array_type
| map_type
| struct_type
Note:
Once you have a table set up with complex type columns, use the DESCRIBE and SHOW CREATE TABLE
statements to see the correct notation with < and > delimiters and comma and colon separators within the complex
type definitions. If you do not have existing data with the same layout as the table, you can query the empty table to
practice with the notation for the SELECT statement. In the SELECT list, you use dot notation and pseudocolumns
such as ITEM, KEY, and VALUE for referring to items within the complex type columns. In the FROM clause, you use
join notation to construct table aliases for any referenced ARRAY and MAP columns.
For example, when defining a table that holds contact information, you might represent phone numbers differently
depending on the expected layout and relationships of the data, and how well you can predict those properties in
advance.
Here are different ways that you might represent phone numbers in a traditional relational schema, with equivalent
representations using complex types.
The traditional, simplest way to represent phone numbers in a relational table is to store all contact info in a single
table, with all columns having scalar types, and each potential phone number represented as a separate column. In this
example, each person can only have these 3 types of phone numbers. If the person does not have a particular kind of
phone number, the corresponding column is NULL for that row.
Using a complex type column to represent the phone numbers adds some extra flexibility. Now there could be an
unlimited number of phone numbers. Because the array elements have an order but not symbolic names, you could
decide in advance that phone_number[0] is the home number, [1] is the work number, [2] is the mobile number, and
so on. (In subsequent examples, you will see how to create a more flexible naming scheme using other complex type
variations, such as a MAP or an ARRAY where each element is a STRUCT.)
Another way to represent an arbitrary set of phone numbers is with a MAP column. With a MAP, each element is
associated with a key value that you specify, which could be a numeric, string, or other scalar type. This example uses
a STRING key to give each phone number a name, such as 'home' or 'mobile'. A query could filter the data
based on the key values, or display the key values in reports.
) STORED AS PARQUET;
If you are an experienced database designer, you already know how to work around the limitations of the single-
table schema from Figure 1: Traditional Relational Representation of Phone Numbers: Single Table on page 140.
By normalizing the schema, with the phone numbers in their own table, you can associate an arbitrary set of phone
numbers with each person, and associate additional details with each phone number, such as whether it is a home,
work, or mobile phone.
The flexibility of this approach comes with some drawbacks. Reconstructing all the data for a particular person
requires a join query, which might require performance tuning on Hadoop because the data from each table might be
transmitted from a different host. Data management tasks such as backups and refreshing the data require dealing with
multiple tables instead of a single table.
This example illustrates a traditional database schema to store contact info normalized across 2 tables. The fact table
establishes the identity and basic information about person. A dimension table stores information only about phone
numbers, using an ID value to associate each phone number with a person ID from the fact table. Each person can
have 0, 1, or many phones; the categories are not restricted to a few predefined ones; and the phone table can contain
as many columns as desired, to represent all sorts of details about each phone number.
CREATE TABLE fact_contacts (id BIGINT, name STRING, address STRING) STORED
AS PARQUET;
CREATE TABLE dim_phones
(
contact_id BIGINT
, category STRING
, international_code STRING
, area_code STRING
, exchange STRING
, extension STRING
, mobile BOOLEAN
, carrier STRING
, current BOOLEAN
, service_start_date TIMESTAMP
, service_end_date TIMESTAMP
)
STORED AS PARQUET;
To represent a schema equivalent to the one from Figure 4: Traditional Relational Representation of Phone Numbers:
Normalized Tables on page 141 using complex types, this example uses an ARRAY where each array element is
a STRUCT. As with the earlier complex type examples, each person can have an arbitrary set of associated phone
numbers. Making each array element into a STRUCT lets us associate multiple data items with each phone number,
and give a separate name and type to each data item. The STRUCT fields of the ARRAY elements reproduce the
columns of the dimension table from the previous example.
You can do all the same kinds of queries with the complex type schema as with the normalized schema from the
previous example. The advantages of the complex type design are in the areas of convenience and performance.
Now your backup and ETL processes only deal with a single table. When a query uses a join to cross-reference the
information about a person with their associated phone numbers, all the relevant data for each row resides in the same
HDFS data block, meaning each row can be processed on a single host without requiring network transmission.
category: STRING
, international_code: STRING
, area_code: STRING
, exchange: STRING
, extension: STRING
, mobile: BOOLEAN
, carrier: STRING
, current: BOOLEAN
, service_start_date: TIMESTAMP
, service_end_date: TIMESTAMP
>>
) STORED AS PARQUET;
-- What goes inside the < > for an ARRAY is a single type, either a scalar
or another
-- complex type (ARRAY, STRUCT, or MAP).
CREATE TABLE array_t
(
id BIGINT,
a1 ARRAY <STRING>,
a2 ARRAY <BIGINT>,
a3 ARRAY <TIMESTAMP>,
a4 ARRAY <STRUCT <f1: STRING, f2: INT, f3: BOOLEAN>>
)
STORED AS PARQUET;
-- What goes inside the < > for a MAP is two comma-separated types
specifying the types of the key-value pair:
-- a scalar type representing the key, and a scalar or complex type
representing the value.
CREATE TABLE map_t
(
id BIGINT,
m1 MAP <STRING, STRING>,
m2 MAP <STRING, BIGINT>,
m3 MAP <BIGINT, STRING>,
m4 MAP <BIGINT, BIGINT>,
m5 MAP <STRING, ARRAY <STRING>>
)
STORED AS PARQUET;
-- What goes inside the < > for a STRUCT is a comma-separated list of
fields, each field defined as
| Impala SQL Language Reference | 143
-- name:type. The type can be a scalar or a complex type. The field names
for each STRUCT do not clash
-- with the names of table columns or fields in other STRUCTs. A STRUCT is
most often used inside
-- an ARRAY or a MAP rather than as a top-level column.
CREATE TABLE struct_t
(
id BIGINT,
s1 STRUCT <f1: STRING, f2: BIGINT>,
s2 ARRAY <STRUCT <f1: INT, f2: TIMESTAMP>>,
s3 MAP <BIGINT, STRUCT <name: STRING, birthday: TIMESTAMP>>
)
STORED AS PARQUET;
-- When we SELECT * from that table, only the scalar columns come back in
the result set.
CREATE TABLE select_star_customer STORED AS PARQUET AS SELECT * FROM
customer;
+------------------------+
| summary |
+------------------------+
| Inserted 150000 row(s) |
+------------------------+
-- The c_orders column, being of complex type, was not included in the
SELECT * result set.
| Impala SQL Language Reference | 144
DESC select_star_customer;
+--------------+---------------+
| name | type |
+--------------+---------------+
| c_custkey | bigint |
| c_name | string |
| c_address | string |
| c_nationkey | smallint |
| c_phone | string |
| c_acctbal | decimal(12,2) |
| c_mktsegment | string |
| c_comment | string |
+--------------+---------------+
References to fields within STRUCT columns use dot notation. If the field name is unambiguous, you can omit
qualifiers such as table name, column name, or even the ITEM or VALUE pseudocolumn names for STRUCT elements
inside an ARRAY or a MAP.
References to fields within MAP columns use the KEY and VALUE pseudocolumns. In this example, once the query
establishes the alias MAP_FIELD for a MAP column with a STRING key and an INT value, the query can refer
to MAP_FIELD.KEY and MAP_FIELD.VALUE, which have zero, one, or many instances for each row from the
containing table.
DESCRIBE table_0;
+---------+-----------------------+
| name | type |
+---------+-----------------------+
| field_0 | string |
| field_1 | map<string,int> |
...
| v | hw | 16 |
| v | 65l388pyt | 29 |
| v | 03k68g91z | 30 |
| v | r2hlg5b | NULL |
+---------+-----------+-------+
When complex types are nested inside each other, you use a combination of joins, pseudocolumn names, and dot
notation to refer to specific fields at the appropriate level. This is the most frequent form of query syntax for complex
columns, because the typical use case involves two levels of complex types, such as an ARRAY of STRUCT elements.
You can express relationships between ARRAY and MAP columns at different levels as joins. You include comparison
operators between fields at the top level and within the nested type columns so that Impala can do the appropriate join
operation.
Note: Many of the complex type examples refer to tables such as CUSTOMER and REGION adapted from the tables
used in the TPC-H benchmark. See Sample Schema and Data for Experimenting with Impala Complex Types on page
157 for the table definitions.
For example, the following queries work equivalently. They each return customer and order data for customers that
have at least one order.
The following query using an outer join returns customers that have orders, plus customers with no orders (no entries
in the C_ORDERS array):
+-----------+------------+
The following query returns only customers that have no orders. (With LEFT ANTI JOIN or LEFT SEMI JOIN,
the query can only refer to columns from the left-hand table, because by definition there is no matching information in
the right-hand table.)
You can also perform correlated subqueries to examine the properties of complex type columns for each row in the
result set.
Count the number of orders per customer. Note the correlated reference to the table alias C. The COUNT(*) operation
applies to all the elements of the C_ORDERS array for the corresponding row, avoiding the need for a GROUP BY
clause.
Count the number of orders per customer, ignoring any customers that have not placed any orders:
Count the number of line items in each order. The reference to C.C_ORDERS in the FROM clause is needed because
the O_ORDERKEY field is a member of the elements in the C_ORDERS array. The subquery labelled SUBQ1 is
correlated: it is re-evaluated for the C_ORDERS.O_LINEITEMS array from each row of the CUSTOMERS table.
Get the number of orders, the average order price, and the maximum items in any order per customer. For this
example, the subqueries labelled SUBQ1 and SUBQ2 are correlated: they are re-evaluated for each row from the
original CUSTOMER table, and only apply to the complex columns associated with that row.
For example, these queries show how to access information about the ARRAY elements within the CUSTOMER table
from the “nested TPC-H” schema, starting with the initial ARRAY elements and progressing to examine the STRUCT
fields of the ARRAY, and then the elements nested within another ARRAY of STRUCT:
This example shows a table with two ARRAY columns whose elements are of the scalar type STRING. When referring
to the values of the array elements in the SELECT list, WHERE clause, or ORDER BY clause, you use the ITEM
pseudocolumn because within the array, the individual elements have no defined names.
Because an array is inherently an ordered data structure, Impala recognizes the special name array_column.POS
to represent the numeric position of each element within the array. The POS pseudocolumn lets you filter or reorder
the result set based on the sequence of array elements.
The following example uses a table from a flattened version of the TPC-H schema. The REGION table only has a few
rows, such as one row for Europe and one for Asia. The row for each region represents all the countries in that region
as an ARRAY of STRUCT elements:
To find the countries within a specific region, you use a join query. To find out the order of elements in the array, you
also refer to the POS pseudocolumn in the select list:
| ASIA | JAPAN | 2 |
| ASIA | INDONESIA | 3 |
| ASIA | INDIA | 4 |
+--------+-----------+-----+
Once you know the positions of the elements, you can use that information in subsequent queries, for example to
change the ordering of results from the complex type column or to filter certain elements from the array:
(
dream_id BIGINT,
details MAP <STRING,STRING>
)
STORED AS PARQUET;
The following example shows a more elaborate version of the previous table, where the VALUE part of the MAP entry
is a STRUCT rather than a scalar type. Now instead of referring to the VALUE pseudocolumn directly, you use dot
notation to refer to the STRUCT fields inside it.
-- Remember that if the ITEM or VALUE contains a STRUCT, you can reference
-- the STRUCT fields directly without the .ITEM or .VALUE qualifier.
SELECT dream_id, details.KEY AS "Dream Type", details.summary AS "Dream
Summary", details.duration AS "Duration"
FROM better_dream_journal, better_dream_journal.details
WHERE
details.KEY IN ('Anxiety', 'Nightmare')
AND details.duration > 60
AND details.woke_up = TRUE;
| Impala SQL Language Reference | 152
It is common to make the value portion of each key-value pair in a MAP a STRUCT, ARRAY of STRUCT, or other
complex type variation. That way, each key in the MAP can be associated with a flexible and extensible data structure.
The key values are not predefined ahead of time (other than by specifying their data type). Therefore, the MAP can
accomodate a rapidly evolving schema, or sparse data structures where each row contains only a few data values
drawn from a large set of possible choices.
Although you can use an ARRAY of scalar values as the top-level column in a table, such a simple array is typically of
limited use for analytic queries. The only property of the array elements, aside from the element value, is the ordering
sequence available through the POS pseudocolumn. To record any additional item about each array element, such as a
TIMESTAMP or a symbolic name, you use an ARRAY of STRUCT rather than of scalar values.
If you are considering having multiple ARRAY or MAP columns, with related items under the same position in each
ARRAY or the same key in each MAP, prefer to use a STRUCT to group all the related items into a single ARRAY or
MAP. Doing so avoids the additional storage overhead and potential duplication of key values from having an extra
complex type column. Also, because each ARRAY or MAP that you reference in the query SELECT list requires an
additional join clause, minimizing the number of complex type columns also makes the query easier to read and
maintain, relying more on dot notation to refer to the relevant fields rather than a sequence of join clauses.
For example, here is a table with several complex type columns all at the top level and containing only scalar types.
To retrieve every data item for the row requires a separate join for each ARRAY or MAP column. The fields of the
STRUCT can be referenced using dot notation, but there is no real advantage to using the STRUCT at the top level
rather than just making separate columns FIELD1 and FIELD2.
describe complex_types_top_level;
+------+-----------------+
| name | type |
+------+-----------------+
| id | bigint |
| a1 | array<int> |
| a2 | array<string> |
| s | struct< |
| | field1:int, |
| | field2:string |
| | > |
| m1 | map<int,string> |
| m2 | map<string,int> |
+------+-----------------+
select
id,
a1.item,
a2.item,
s.field1,
s.field2,
m1.key,
m1.value,
m2.key,
m2.value
from
| Impala SQL Language Reference | 154
complex_types_top_level,
complex_types_top_level.a1,
complex_types_top_level.a2,
complex_types_top_level.m1,
complex_types_top_level.m2;
For example, here is a table with columns containing an ARRAY of STRUCT, a MAP where each key value is a
STRUCT, and a MAP where each key value is an ARRAY of STRUCT.
The DESCRIBE statement rearranges the < and > separators and the field names within each STRUCT for easy
readability:
DESCRIBE nesting_demo;
+----------------+-----------------------------+
| name | type |
+----------------+-----------------------------+
| user_id | bigint |
| family_members | array<struct< |
| | name:string, |
| | email:string, |
| | date_joined:timestamp |
| | >> |
| foo | map<string,struct< |
| | f1:int, |
| | f2:int, |
| | f3:timestamp, |
| | f4:boolean |
| | >> |
| gameplay | map<string,array<struct< |
| | name:string, |
| | highest:bigint, |
| | lives_used:int, |
| | total_spent:decimal(16,2) |
| | >>> |
+----------------+-----------------------------+
To query the complex type columns, you use join notation to refer to the lowest-level scalar values. If the value is an
ARRAY element, the fully qualified name includes the ITEM pseudocolumn. If the value is inside a MAP, the fully
qualified name includes the KEY or VALUE pseudocolumn. Each reference to a different ARRAY or MAP (even if
nested inside another complex type) requires an additional join clause.
SELECT
-- The lone scalar field doesn't require any dot notation or join clauses.
user_id
-- Retrieve the fields of a STRUCT inside an ARRAY.
| Impala SQL Language Reference | 155
Once you understand the notation to refer to a particular data item in the SELECT list, you can use the same qualified
name to refer to that data item in other parts of the query, such as the WHERE clause, ORDER BY or GROUP BY
clauses, or calls to built-in functions. For example, you might frequently retrieve the VALUE part of each MAP item in
the SELECT list, while choosing the specific MAP items by running comparisons against the KEY part in the WHERE
clause.
Accessing Complex Type Data in Flattened Form Using Views
The layout of complex and nested types is largely a physical consideration. The complex type columns reside in
the same data files rather than in separate normalized tables, for your convenience in managing related data sets
and performance in querying related data sets. You can use views to treat tables with complex types as if they were
flattened. By putting the join logic and references to the complex type columns in the view definition, you can query
the same tables using existing queries intended for tables containing only scalar columns. This technique also lets
you use tables with complex types with BI tools that are not aware of the data types and query notation for accessing
complex type columns.
For example, the variation of the TPC-H schema containing complex types has a table REGION. This table has 5
rows, corresponding to 5 regions such as NORTH AMERICA and AFRICA. Each row has an ARRAY column, where
each array item is a STRUCT containing details about a country in that region.
DESCRIBE region;
+-------------+-------------------------+
| name | type |
+-------------+-------------------------+
| r_regionkey | smallint |
| r_name | string |
| r_comment | string |
| r_nations | array<struct< |
| Impala SQL Language Reference | 156
| | n_nationkey:smallint, |
| | n_name:string, |
| | n_comment:string |
| | >> |
+-------------+-------------------------+
The same data could be represented in traditional denormalized form, as a single table where the information about
each region is repeated over and over, alongside the information about each country. The nested complex types let us
avoid the repetition, while still keeping the data in a single table rather than normalizing across multiple tables.
To use this table with a JDBC or ODBC application that expected scalar columns, we could create a view that
represented the result set as a set of scalar columns (three columns from the original table, plus three more from the
STRUCT fields of the array elements). In the following examples, any column with an R_* prefix is taken unchanged
from the original table, while any column with an N_* prefix is extracted from the STRUCT inside the ARRAY.
Then we point the application queries at the view rather than the original table. From the perspective of the view,
there are 25 rows in the result set, one for each nation in each region, and queries can refer freely to fields related to
the region or the nation.
-- Retrieve info such as the nation name from the original R_NATIONS array
elements.
select n_name from region_view where r_name in ('EUROPE', 'ASIA');
+----------------+
| n_name |
+----------------+
| UNITED KINGDOM |
| RUSSIA |
| ROMANIA |
| GERMANY |
| FRANCE |
| VIETNAM |
| CHINA |
| JAPAN |
| INDONESIA |
| INDIA |
+----------------+
-- For conciseness, we only list some view columns in the SELECT list.
-- SELECT * would bring back all the data, unlike SELECT *
-- queries on the original table with complex type columns.
SELECT r_regionkey, r_name, n_nationkey, n_name FROM region_view LIMIT 7;
| Impala SQL Language Reference | 157
+-------------+--------+-------------+----------------+
| r_regionkey | r_name | n_nationkey | n_name |
+-------------+--------+-------------+----------------+
| 3 | EUROPE | 23 | UNITED KINGDOM |
| 3 | EUROPE | 22 | RUSSIA |
| 3 | EUROPE | 19 | ROMANIA |
| 3 | EUROPE | 7 | GERMANY |
| 3 | EUROPE | 6 | FRANCE |
| 2 | ASIA | 21 | VIETNAM |
| 2 | ASIA | 18 | CHINA |
+-------------+--------+-------------+----------------+
Sample Schema and Data for Experimenting with Impala Complex Types
The tables used for earlier examples of complex type syntax are trivial ones with no actual data. The more substantial
examples of the complex type feature use these tables, adapted from the schema used for TPC-H testing:
SHOW TABLES;
+----------+
| name |
+----------+
| customer |
| part |
| region |
| supplier |
+----------+
DESCRIBE customer;
+--------------+------------------------------------+
| name | type |
+--------------+------------------------------------+
| c_custkey | bigint |
| c_name | string |
| c_address | string |
| c_nationkey | smallint |
| c_phone | string |
| c_acctbal | decimal(12,2) |
| c_mktsegment | string |
| c_comment | string |
| c_orders | array<struct< |
| | o_orderkey:bigint, |
| | o_orderstatus:string, |
| | o_totalprice:decimal(12,2), |
| | o_orderdate:string, |
| | o_orderpriority:string, |
| | o_clerk:string, |
| | o_shippriority:int, |
| | o_comment:string, |
| | o_lineitems:array<struct< |
| | l_partkey:bigint, |
| | l_suppkey:bigint, |
| | l_linenumber:int, |
| | l_quantity:decimal(12,2), |
| | l_extendedprice:decimal(12,2), |
| | l_discount:decimal(12,2), |
| | l_tax:decimal(12,2), |
| | l_returnflag:string, |
| | l_linestatus:string, |
| | l_shipdate:string, |
| Impala SQL Language Reference | 158
| | l_commitdate:string, |
| | l_receiptdate:string, |
| | l_shipinstruct:string, |
| | l_shipmode:string, |
| | l_comment:string |
| | >> |
| | >> |
+--------------+------------------------------------+
DESCRIBE part;
+---------------+---------------+
| name | type |
+---------------+---------------+
| p_partkey | bigint |
| p_name | string |
| p_mfgr | string |
| p_brand | string |
| p_type | string |
| p_size | int |
| p_container | string |
| p_retailprice | decimal(12,2) |
| p_comment | string |
+---------------+---------------+
DESCRIBE region;
+-------------
+--------------------------------------------------------------------+
| name | type
|
+-------------
+--------------------------------------------------------------------+
| r_regionkey | smallint
|
| r_name | string
|
| r_comment | string
|
| r_nations |
array<struct<n_nationkey:smallint,n_name:string,n_comment:string>> |
+-------------
+--------------------------------------------------------------------+
DESCRIBE supplier;
+-------------+----------------------------------------------+
| name | type |
+-------------+----------------------------------------------+
| s_suppkey | bigint |
| s_name | string |
| s_address | string |
| s_nationkey | smallint |
| s_phone | string |
| s_acctbal | decimal(12,2) |
| s_comment | string |
| s_partsupps | array<struct<ps_partkey:bigint, |
| | ps_availqty:int,ps_supplycost:decimal(12,2), |
| | ps_comment:string>> |
+-------------+----------------------------------------------+
| count(*) |
+----------+
| 150000 |
+----------+
REFRESH complex_array;
SELECT country, city.item FROM complex_array, complex_array.city
+---------+-------------+
| country | item |
+---------+-------------+
| Canada | Toronto |
| Canada | Vancouver |
| Canada | St. John's |
| Canada | Saint John |
| Canada | Montreal |
| Canada | Halifax |
| Canada | Winnipeg |
| Canada | Calgary |
| Canada | Saskatoon |
| Canada | Ottawa |
| Canada | Yellowknife |
| France | Paris |
| France | Nice |
| France | Marseilles |
| France | Cannes |
| Greece | Athens |
| Greece | Piraeus |
| Greece | Hania |
| Greece | Heraklion |
| Greece | Rethymnon |
| Greece | Fira |
+---------+-------------+
Create table with STRUCT and ARRAY in Impala, load data in Hive, query in Impala:
This example shows the cycle of creating the tables and querying the complex data in Impala, and using Hive (either
the hive shell or beeline) for the data loading step. The data starts in flattened, denormalized form in a text table.
Hive writes the corresponding Parquet data, including a STRUCT column with an ARRAY field. Then Impala can run
analytic queries on the Parquet table, using join notation to unpack the ARRAY field from the STRUCT column.
REFRESH complex_struct_array;
SELECT t1.continent, t1.country.name, t2.item
FROM complex_struct_array t1, t1.country.city t2
+---------------+--------------+-------------+
| continent | country.name | item |
+---------------+--------------+-------------+
| Europe | France | Paris |
| Europe | France | Nice |
| Europe | France | Marseilles |
| Europe | France | Cannes |
| Europe | Greece | Athens |
| Europe | Greece | Piraeus |
| Europe | Greece | Hania |
| Europe | Greece | Heraklion |
| Europe | Greece | Rethymnon |
| Europe | Greece | Fira |
| North America | Canada | Toronto |
| North America | Canada | Vancouver |
| North America | Canada | St. John's |
| North America | Canada | Saint John |
| North America | Canada | Montreal |
| North America | Canada | Halifax |
| North America | Canada | Winnipeg |
| North America | Canada | Calgary |
| North America | Canada | Saskatoon |
| North America | Canada | Ottawa |
| North America | Canada | Yellowknife |
+---------------+--------------+-------------+
In this flattening scenario, you might frequently use a column that is an ARRAY consisting of STRUCT elements,
where each field within the STRUCT corresponds to a column name from the table that you are combining.
The following example shows a traditional normalized layout using two tables, and then an equivalent layout using
complex types in a single table.
-- This table just stores numbers, allowing us to look up details about the
employee
-- and details about their vacation time using a three-table join query.
CREATE table employee_vacations
(
employee_id BIGINT,
vacation_id BIGINT
)
STORED AS PARQUET;
-- For analytic queries using complex types, we can bundle the dimension
table
-- and multiple fact tables into a single table.
CREATE TABLE employee_vacations_nested_types
(
-- We might still use the employee_id for other join queries.
-- The table needs at least one scalar column to serve as an identifier
-- for the complex type columns.
employee_id BIGINT,
Interchanging Complex Type Tables and Data Files with Hive and Other Components
You can produce Parquet data files through several Hadoop components and APIs.
If you have a Hive-created Parquet table that includes ARRAY, STRUCT, or MAP columns, Impala can query that same
table in Impala 2.3 and higher, subject to the usual restriction that all other columns are of data types supported by
Impala, and also that the file type of the table must be Parquet.
If you have a Parquet data file produced outside of Impala, Impala can automatically deduce the appropriate table
structure using the syntax CREATE TABLE ... LIKE PARQUET 'hdfs_path_of_parquet_file'. In
Impala 2.3 and higher, this feature works for Parquet files that include ARRAY, STRUCT, or MAP types.
# In the Unix shell, find the path of any Parquet data file in that HDFS
directory.
$ hdfs dfs -ls hdfs://localhost:20500/test-warehouse/tpch_nested_parquet.db/
customer
Found 4 items
-rwxr-xr-x 3 dev supergroup 171298918 2015-09-22 23:30 hdfs://
localhost:20500/blah/tpch_nested_parquet.db/customer/000000_0
...
/* Back in impala-shell, use the HDFS path in a CREATE TABLE LIKE PARQUET
statement. */
CREATE TABLE customer_ctlp
LIKE PARQUET 'hdfs://localhost:20500/blah/tpch_nested_parquet.db/
customer/000000_0'
STORED AS PARQUET;
/* Confirm that old and new tables have the same column layout, including
complex types. */
DESCRIBE tpch_nested_parquet.customer
+--------------+------------------------------------+---------+
| name | type | comment |
+--------------+------------------------------------+---------+
| c_custkey | bigint | |
| c_name | string | |
| c_address | string | |
| c_nationkey | smallint | |
| c_phone | string | |
| c_acctbal | decimal(12,2) | |
| c_mktsegment | string | |
| c_comment | string | |
| c_orders | array<struct< | |
| Impala SQL Language Reference | 164
| | o_orderkey:bigint, | |
| | o_orderstatus:string, | |
| | o_totalprice:decimal(12,2), | |
| | o_orderdate:string, | |
| | o_orderpriority:string, | |
| | o_clerk:string, | |
| | o_shippriority:int, | |
| | o_comment:string, | |
| | o_lineitems:array<struct< | |
| | l_partkey:bigint, | |
| | l_suppkey:bigint, | |
| | l_linenumber:int, | |
| | l_quantity:decimal(12,2), | |
| | l_extendedprice:decimal(12,2), | |
| | l_discount:decimal(12,2), | |
| | l_tax:decimal(12,2), | |
| | l_returnflag:string, | |
| | l_linestatus:string, | |
| | l_shipdate:string, | |
| | l_commitdate:string, | |
| | l_receiptdate:string, | |
| | l_shipinstruct:string, | |
| | l_shipmode:string, | |
| | l_comment:string | |
| | >> | |
| | >> | |
+--------------+------------------------------------+---------+
describe customer_ctlp;
+--------------+------------------------------------
+-----------------------------+
| name | type | comment
|
+--------------+------------------------------------
+-----------------------------+
| c_custkey | bigint | Inferred from Parquet
file. |
| c_name | string | Inferred from Parquet
file. |
| c_address | string | Inferred from Parquet
file. |
| c_nationkey | int | Inferred from Parquet
file. |
| c_phone | string | Inferred from Parquet
file. |
| c_acctbal | decimal(12,2) | Inferred from Parquet
file. |
| c_mktsegment | string | Inferred from Parquet
file. |
| c_comment | string | Inferred from Parquet
file. |
| c_orders | array<struct< | Inferred from Parquet
file. |
| | o_orderkey:bigint, |
|
| | o_orderstatus:string, |
|
| | o_totalprice:decimal(12,2), |
|
| | o_orderdate:string, |
|
| | o_orderpriority:string, |
|
| Impala SQL Language Reference | 165
| | o_clerk:string, |
|
| | o_shippriority:int, |
|
| | o_comment:string, |
|
| | o_lineitems:array<struct< |
|
| | l_partkey:bigint, |
|
| | l_suppkey:bigint, |
|
| | l_linenumber:int, |
|
| | l_quantity:decimal(12,2), |
|
| | l_extendedprice:decimal(12,2), |
|
| | l_discount:decimal(12,2), |
|
| | l_tax:decimal(12,2), |
|
| | l_returnflag:string, |
|
| | l_linestatus:string, |
|
| | l_shipdate:string, |
|
| | l_commitdate:string, |
|
| | l_receiptdate:string, |
|
| | l_shipinstruct:string, |
|
| | l_shipmode:string, |
|
| | l_comment:string |
|
| | >> |
|
| | >> |
|
+--------------+------------------------------------
+-----------------------------+
Literals
Each of the Impala data types has corresponding notation for literal values of that type. You specify literal values in
SQL statements, such as in the SELECT list or WHERE clause of a query, or as an argument to a function call. See
Data Types on page 94 for a complete list of types, ranges, and conversion rules.
Numeric Literals
To write literals for the integer types (TINYINT, SMALLINT, INT, and BIGINT), use a sequence of digits with
optional leading zeros.
To write literals for the floating-point types (DECIMAL, FLOAT, and DOUBLE), use a sequence of digits with an
optional decimal point (. character). To preserve accuracy during arithmetic expressions, Impala interprets floating-
| Impala SQL Language Reference | 166
point literals as the DECIMAL type with the smallest appropriate precision and scale, until required by the context to
convert the result to FLOAT or DOUBLE.
Integer values are promoted to floating-point when necessary, based on the context.
You can also use exponential notation by including an e character. For example, 1e6 is 1 times 10 to the power of 6
(1 million). A number in exponential notation is always interpreted as floating-point.
When Impala encounters a numeric literal, it considers the type to be the “smallest” that can accurately represent the
value. The type is promoted to larger or more accurate types if necessary, based on subsequent parts of an expression.
For example, you can see by the types Impala defines for the following table columns how it interprets the
corresponding numeric literals:
| x | decimal(4,3) | |
+------+--------------+---------+
String Literals
String literals are quoted using either single or double quotation marks. You can use either kind of quotes for string
literals, even both kinds for different literals within the same statement.
Quoted literals are considered to be of type STRING. To use quoted literals in contexts requiring a CHAR or
VARCHAR value, CAST() the literal to a CHAR or VARCHAR of the appropriate length.
Escaping special characters:
To encode special characters within a string literal, precede them with the backslash (\) escape character:
• \t represents a tab.
• \n represents a newline or linefeed. This might cause extra line breaks in impala-shell output.
• \r represents a carriage return. This might cause unusual formatting (making it appear that some content is
overwritten) in impala-shell output.
• \b represents a backspace. This might cause unusual formatting (making it appear that some content is
overwritten) in impala-shell output.
• \0 represents an ASCII nul character (not the same as a SQL NULL). This might not be visible in impala-
shell output.
• \Z represents a DOS end-of-file character. This might not be visible in impala-shell output.
• \% and \_ can be used to escape wildcard characters within the string passed to the LIKE operator.
• \ followed by 3 octal digits represents the ASCII code of a single character; for example, \101 is ASCII 65, the
character A.
• Use two consecutive backslashes (\\) to prevent the backslash from being interpreted as an escape character.
• Use the backslash to escape single or double quotation mark characters within a string literal, if the literal is
enclosed by the same type of quotation mark.
• If the character following the \ does not represent the start of a recognized escape sequence, the character is
passed through unchanged.
Quotes within quotes:
To include a single quotation character within a string value, enclose the literal with either single or double quotation
marks, and optionally escape the single quote as a \' sequence. Earlier releases required escaping a single quote
inside double quotes. Continue using escape sequences in this case if you also need to run your SQL code on older
versions of Impala.
To include a double quotation character within a string value, enclose the literal with single quotation marks, no
escaping is necessary in this case. Or, enclose the literal with double quotation marks and escape the double quote as a
\" sequence.
Boolean Literals
For BOOLEAN values, the literals are TRUE and FALSE, with no quotation marks and case-insensitive.
Examples:
select true;
select * from t1 where assertion = false;
select case bool_col when true then 'yes' when false 'no' else 'null' end
from t1;
Timestamp Literals
Impala automatically converts STRING literals of the correct format into TIMESTAMP values. Timestamp values are
accepted in the format 'yyyy‑MM‑dd HH:mm:ss.SSSSSS', and can consist of just the date, or just the time, with
or without the fractional second portion. For example, you can specify TIMESTAMP values such as '1966‑07‑30',
'08:30:00', or '1985‑09‑25 17:45:30.005'.
Leading zeroes are not required in the numbers representing the date component, such as month and date, or the
time component, such as hour, minute, and second. For example, Impala accepts both '2018‑1‑1 01:02:03' and
'2018‑01‑01 1:2:3' as valid.
In STRING to TIMESTAMP conversions, leading and trailing white spaces, such as a space, a tab, a newline, or
a carriage return, are ignored. For example, Impala treats the following as equivalent: '1999‑12‑01 01:02:03 ',
' 1999‑12‑01 01:02:03', '1999‑12‑01 01:02:03\r\n\t'.
When you convert or cast a STRING literal to TIMESTAMP, you can use the following separators between the date
part and the time part:
• One or more space characters
Example: CAST('2001-01-09 01:05:01' AS TIMESTAMP)
• The character “T”
Example: CAST('2001-01-09T01:05:01' AS TIMESTAMP)
You can also use INTERVAL expressions to add or subtract from timestamp literal values, such as
CAST('1966‑07‑30' AS TIMESTAMP) + INTERVAL 5 YEARS + INTERVAL 3 DAYS. See TIMESTAMP
Data Type on page 125 for details.
Depending on your data pipeline, you might receive date and time data as text, in notation that does not exactly match
the format for Impala TIMESTAMP literals. See Impala Date and Time Functions on page 457 for functions that
can convert between a variety of string literals (including different field order, separators, and timezone notation) and
equivalent TIMESTAMP or numeric values.
| Impala SQL Language Reference | 169
Date Literals
The DATE literals are in the form of DATE'YYYY-MM-DD'. For example, DATE '2013-01-01'
NULL
The notion of NULL values is familiar from all kinds of database systems, but each SQL dialect can have its own
behavior and restrictions on NULL values. For Big Data processing, the precise semantics of NULL values are
significant: any misunderstanding could lead to inaccurate results or misformatted data, that could be time-consuming
to correct for large data sets.
• NULL is a different value than an empty string. The empty string is represented by a string literal with nothing
inside, "" or ''.
• In a delimited text file, the NULL value is represented by the special token \N.
• When Impala inserts data into a partitioned table, and the value of one of the partitioning columns is NULL or
the empty string, the data is placed in a special partition that holds only these two kinds of values. When these
values are returned in a query, the result is NULL whether the value was originally NULL or an empty string. This
behavior is compatible with the way Hive treats NULL values in partitioned tables. Hive does not allow empty
strings as partition keys, and it returns a string value such as __HIVE_DEFAULT_PARTITION__ instead of
NULL when such values are returned from a query. For example:
In addition to columns with the NOT NULL attribute, Kudu tables also have restrictions on NULL values in columns
that are part of the primary key for a table. No column that is part of the primary key in a Kudu table can contain any
NULL values.
SQL Operators
SQL operators are a class of comparison functions that are widely used within the WHERE clauses of SELECT
statements.
Arithmetic Operators
The arithmetic operators use expressions with a left-hand argument, the operator, and then (in most cases) a right-
hand argument.
Syntax:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
| Impala SQL Language Reference | 172
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| EUROPE | 5 | 77 | 15.4 | FRANCE | UNITED KINGDOM | 5
|
| MIDDLE EAST | 5 | 58 | 11.6 | EGYPT | SAUDI ARABIA | 5
|
+-------------+-------+-----+------+-----------+----------------
+---------------+
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
The following example shows how to do an arithmetic operation using a numeric field of a STRUCT type that is
an item within an ARRAY column. Once the scalar numeric value R_NATIONKEY is extracted, it can be used in an
arithmetic expression, such as multiplying by 10:
| AMERICA | ARGENTINA | 10 |
| MIDDLE EAST | EGYPT | 40 |
| AFRICA | ALGERIA | 0 |
+-------------+-------------+------------------------------+
BETWEEN Operator
In a WHERE clause, compares an expression to both a lower and upper bound. The comparison is successful is the
expression is greater than or equal to the lower bound, and less than or equal to the upper bound. If the bound values
are switched, so the lower bound is greater than the upper bound, does not match any values.
Syntax:
Data types: Typically used with numeric data types. Works with any data type, although not very practical for
BOOLEAN values. (BETWEEN false AND true will match all BOOLEAN values.) Use CAST() if necessary
to ensure the lower and upper bound values are compatible types. Call string or date/time functions if necessary to
extract or transform the relevant portion to compare, especially if the value can be transformed into a number.
Usage notes:
Be careful when using short string operands. A longer string that starts with the upper bound value will not be
included, because it is considered greater than the upper bound. For example, BETWEEN 'A' and 'M' would not
match the string value 'Midway'. Use functions such as upper(), lower(), substr(), trim(), and so on if
necessary to ensure the comparison works as expected.
Complex type considerations:
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
Examples:
-- Retrieve data for names beginning with 'A' through 'M' inclusive.
-- Only test the first letter to ensure all the values starting with 'M' are
matched.
-- Do a case-insensitive comparison to match names with various
capitalization conventions.
select last_name from customers where upper(substr(last_name,1,1)) between
'A' and 'M';
The following example shows how to do a BETWEEN comparison using a numeric field of a STRUCT type that is
an item within an ARRAY column. Once the scalar numeric value R_NATIONKEY is extracted, it can be used in a
comparison operator:
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
Comparison Operators
Impala supports the familiar comparison operators for checking equality and sort order for the column data types:
Syntax:
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
EXISTS Operator
The EXISTS operator tests whether a subquery returns any results. You typically use it to find values from one table
that have corresponding values in another table.
The converse, NOT EXISTS, helps to find all the values from one table that do not have any corresponding values in
another table.
Syntax:
EXISTS (subquery)
NOT EXISTS (subquery)
Usage notes:
The subquery can refer to a different table than the outer query block, or the same table. For example, you might use
EXISTS or NOT EXISTS to check the existence of parent/child relationships between two columns of the same
table.
You can also use operators and function calls within the subquery to test for other kinds of relationships other than
strict equality. For example, you might use a call to COUNT() in the subquery to check whether the number of
matching values is higher or lower than some limit. You might call a UDF in the subquery to check whether values in
one table matches a hashed representation of those same values in a different table.
NULL considerations:
If the subquery returns any value at all (even NULL), EXISTS returns TRUE and NOT EXISTS returns false.
The following example shows how even when the subquery returns only NULL values, EXISTS still returns TRUE
and thus matches all the rows from the table in the outer query block.
However, if the table in the subquery is empty and so the subquery returns an empty result set, EXISTS returns
FALSE:
The following example shows a correlated subquery that finds all the values in one table that exist in another table.
For each value X from T1, the query checks if the Y column of T2 contains an identical value, and the EXISTS
operator returns TRUE or FALSE as appropriate in each case.
An uncorrelated query is less interesting in this case. Because the subquery always returns TRUE, all rows from T1
are returned. If the table contents where changed so that the subquery did not match any rows, none of the rows from
T1 would be returned.
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+---+
The following example shows how an uncorrelated subquery can test for the existence of some condition within a
table. By using LIMIT 1 or an aggregate function, the query returns a single result or no result based on whether the
subquery matches any rows. Here, we know that T1 and T2 contain some even numbers, but T3 does not.
The following example finds numbers in one table that are 1 greater than numbers from another table. The EXISTS
notation is simpler than an equivalent CROSS JOIN between the tables. (The example then also illustrates how the
same test could be performed using an IN operator.)
The following example finds values from one table that do not exist in another table.
The following example uses the NOT EXISTS operator to find all the leaf nodes in tree-structured data. This
simplified “tree of life” has multiple levels (class, order, family, and so on), with each item pointing upward through
a PARENT pointer. The example runs an outer query and a subquery on the same table, returning only those items
whose ID value is not referenced by the PARENT of any other item.
[localhost:21000] > create table tree (id int, parent int, name string);
[localhost:21000] > insert overwrite tree values
> (0, null, "animals"),
> (1, 0, "placentals"),
> (2, 0, "marsupials"),
> (3, 1, "bats"),
> (4, 1, "cats"),
> (5, 2, "kangaroos"),
> (6, 4, "lions"),
> (7, 4, "tigers"),
> (8, 5, "red kangaroo"),
> (9, 2, "wallabies");
[localhost:21000] > select name as "leaf node" from tree one
> where not exists (select parent from tree two where
one.id = two.parent);
+--------------+
| leaf node |
+--------------+
| bats |
| lions |
| tigers |
| red kangaroo |
| wallabies |
+--------------+
Related information:
Subqueries in Impala SELECT Statements on page 325
ILIKE Operator
A case-insensitive comparison operator for STRING data, with basic wildcard capability using _ to match a single
character and % to match multiple characters. The argument expression must match the entire string value. Typically,
it is more efficient to put any % wildcard match at the end of the string.
This operator, available in Impala 2.5 and higher, is the equivalent of the LIKE operator, but with case-insensitive
comparisons.
Syntax:
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
Examples:
In the following examples, strings that are the same except for differences in uppercase and lowercase match
successfully with ILIKE, but do not match with LIKE:
Related information:
For case-sensitive comparisons, see LIKE Operator on page 186. For a more general kind of search operator using
regular expressions, see REGEXP Operator on page 190 or its case-insensitive counterpart IREGEXP Operator on
page 182.
IN Operator
The IN operator compares an argument value to a set of values, and returns TRUE if the argument matches any value
in the set. The NOT IN operator reverses the comparison, and checks if the argument value is not part of a set of
values.
| Impala SQL Language Reference | 180
Syntax:
The left-hand expression and the set of comparison values must be of compatible types.
The left-hand expression must consist only of a single value, not a tuple. Although the left-hand expression is
typically a column name, it could also be some other value. For example, the WHERE clauses WHERE id IN (5)
and WHERE 5 IN (id) produce the same results.
The set of values to check against can be specified as constants, function calls, column names, or other expressions in
the query text. The maximum number of expressions in the IN list is 9999. (The maximum number of elements of a
single expression is 10,000 items, and the IN operator itself counts as one.)
In Impala 2.0 and higher, the set of values can also be generated by a subquery. IN can evaluate an unlimited number
of results using a subquery.
Usage notes:
Any expression using the IN operator could be rewritten as a series of equality tests connected with OR, but the IN
syntax is often clearer, more concise, and easier for Impala to optimize. For example, with partitioned tables, queries
frequently use IN clauses to filter data by comparing the partition key columns to specific values.
NULL considerations:
If there really is a matching non-null value, IN returns TRUE:
If the searched value is not found in the comparison values, and the comparison values include NULL, the result is
NULL:
+-------------+
[localhost:21000] > select 1 not in (null);
+-----------------+
| 1 not in (null) |
+-----------------+
| NULL |
+-----------------+
If the left-hand argument is NULL, IN always returns NULL. This rule applies even if the comparison values include
NULL.
Added in: Available in earlier Impala releases, but new capabilities were added in Impala 2.0.0
Complex type considerations:
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
The following example shows how to do an arithmetic operation using a numeric field of a STRUCT type that is
an item within an ARRAY column. Once the scalar numeric value R_NATIONKEY is extracted, it can be used in an
arithmetic expression, such as multiplying by 10:
+-------------+-------------------------+---------+
Restrictions:
Correlated subqueries used in EXISTS and IN operators cannot include a LIMIT clause.
Examples:
Related information:
Subqueries in Impala SELECT Statements on page 325
IREGEXP Operator
Tests whether a value matches a regular expression, using case-insensitive string comparisons. Uses the POSIX
regular expression syntax where ^ and $ match the beginning and end of the string, . represents any single character,
* represents a sequence of zero or more items, + represents a sequence of one or more items, ? produces a non-
greedy match, and so on.
This operator, available in Impala 2.5 and higher, is the equivalent of the REGEXP operator, but with case-insensitive
comparisons.
Syntax:
Usage notes:
The | symbol is the alternation operator, typically used within () to match different sequences. The () groups do
not allow backreferences. To retrieve the part of a value matched within a () section, use the regexp_extract()
built-in function. (Currently, there is not any case-insensitive equivalent for the regexp_extract() function.)
In Impala 1.3.1 and higher, the REGEXP and RLIKE operators now match a regular expression string that occurs
anywhere inside the target string, the same as if the regular expression was enclosed on each side by .*. See
REGEXP Operator on page 190 for examples. Previously, these operators only succeeded when the regular
expression matched the entire target string. This change improves compatibility with the regular expression
support for popular database systems. There is no change to the behavior of the regexp_extract() and
regexp_replace() built-in functions.
| Impala SQL Language Reference | 183
In Impala 2.0 and later, the Impala regular expression syntax conforms to the POSIX Extended Regular Expression
syntax used by the Google RE2 library. For details, see the RE2 documentation. It has most idioms familiar from
regular expressions in Perl, Python, and so on, including .*? for non-greedy matches.
In Impala 2.0 and later, a change in the underlying regular expression library could cause changes in the way regular
expressions are interpreted by this function. Test any queries that use regular expressions and adjust the expression
patterns if necessary. See Incompatible Changes Introduced in Impala 2.0.0 on page 832 for details.
Complex type considerations:
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
Examples:
The following examples demonstrate the syntax for the IREGEXP operator.
Related information:
REGEXP Operator on page 190
The operator <=> is an alias for IS NOT DISTINCT FROM. It is typically used as a NULL-safe equality operator in
join queries. That is, A <=> B is true if A equals B or if both A and B are NULL.
Usage notes:
This operator provides concise notation for comparing two values and always producing a true or false result,
without treating NULL as a special case. Otherwise, to unambiguously distinguish between two values requires a
compound expression involving IS [NOT] NULL tests of both operands in addition to the = or != operator.
The <=> operator, used like an equality operator in a join query, is more efficient than the equivalent clause: A = B
OR (A IS NULL AND B IS NULL). The <=> operator can use a hash join, while the OR expression cannot.
Examples:
The following examples show how IS DISTINCT FROM gives output similar to the != operator, and IS NOT
DISTINCT FROM gives output similar to the = operator. The exception is when the expression involves a NULL
| Impala SQL Language Reference | 184
value on one side or both sides, where != and = return NULL but the IS [NOT] DISTINCT FROM operators still
return true or false.
The following example shows how IS DISTINCT FROM considers CHAR values to be the same (not distinct from
each other) if they only differ in the number of trailing spaces. Therefore, sometimes the result of an IS [NOT]
DISTINCT FROM operator differs depending on whether the values are STRING/VARCHAR or CHAR.
select
'x' is distinct from 'x ' as string_with_trailing_spaces,
cast('x' as char(5)) is distinct from cast('x ' as char(5)) as
char_with_trailing_spaces;
+-----------------------------+---------------------------+
| string_with_trailing_spaces | char_with_trailing_spaces |
+-----------------------------+---------------------------+
| true | false |
+-----------------------------+---------------------------+
IS NULL Operator
The IS NULL operator, and its converse the IS NOT NULL operator, test whether a specified value is NULL.
Because using NULL with any of the other comparison operators such as = or != also returns NULL rather than TRUE
or FALSE, you use a special-purpose comparison operator to check for this special condition.
In Impala 2.11 and higher, you can use the operators IS UNKNOWN and IS NOT UNKNOWN as synonyms for IS
NULL and IS NOT NULL, respectively.
Syntax:
expression IS NULL
expression IS NOT NULL
expression IS UNKNOWN
expression IS NOT UNKNOWN
Usage notes:
In many cases, NULL values indicate some incorrect or incomplete processing during data ingestion or conversion.
You might check whether any values in a column are NULL, and if so take some followup action to fill them in.
With sparse data, often represented in “wide” tables, it is common for most values to be NULL with only an
occasional non-NULL value. In those cases, you can use the IS NOT NULL operator to identify the rows containing
any data at all for a particular column, regardless of the actual value.
With a well-designed database schema, effective use of NULL values and IS NULL and IS NOT NULL operators
can save having to design custom logic around special values such as 0, -1, 'N/A', empty string, and so on. NULL
lets you distinguish between a value that is known to be 0, false, or empty, and a truly unknown value.
Complex type considerations:
The IS [NOT] UNKNOWN operator, as with the IS [NOT] NULL operator, is not applicable to complex type
columns (STRUCT, ARRAY, or MAP). Using a complex type column with this operator causes a query error.
Examples:
IS TRUE Operator
This variation of the IS operator tests for truth or falsity, with right-hand arguments [NOT] TRUE, [NOT] FALSE,
and [NOT] UNKNOWN.
Syntax:
expression IS TRUE
expression IS NOT TRUE
expression IS FALSE
expression IS NOT FALSE
Usage notes:
This IS TRUE and IS FALSE forms are similar to doing equality comparisons with the Boolean values TRUE
and FALSE, except that IS TRUE and IS FALSE always return either TRUE or FALSE, even if the left-hand side
expression returns NULL
These operators let you simplify Boolean comparisons that must also check for NULL, for example X != 10 AND
X IS NOT NULL is equivalent to (X != 10) IS TRUE.
In Impala 2.11 and higher, you can use the operators IS [NOT] TRUE and IS [NOT] FALSE as equivalents for
the built-in functions ISTRUE(), ISNOTTRUE(), ISFALSE(), and ISNOTFALSE().
Complex type considerations:
The IS [NOT] TRUE and IS [NOT] FALSE operators are not applicable to complex type columns (STRUCT,
ARRAY, or MAP). Using a complex type column with these operators causes a query error.
Added in: Impala 2.11.0
Examples:
LIKE Operator
A comparison operator for STRING data, with basic wildcard capability using the underscore (_) to match a single
character and the percent sign (%) to match multiple characters. The argument expression must match the entire string
value. Typically, it is more efficient to put any % wildcard match at the end of the string.
Syntax:
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
Examples:
For case-insensitive comparisons, see ILIKE Operator on page 178. For a more general kind of search operator
using regular expressions, see REGEXP Operator on page 190 or its case-insensitive counterpart IREGEXP
Operator on page 182.
Logical Operators
Logical operators return a BOOLEAN value, based on a binary or unary logical operation between arguments that are
also Booleans. Typically, the argument expressions use comparison operators.
Syntax:
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
Examples:
These examples demonstrate the AND operator:
+-----------------------+
| true |
+-----------------------+
REGEXP Operator
Tests whether a value matches a regular expression. Uses the POSIX regular expression syntax where ^ and $ match
the beginning and end of the string, . represents any single character, * represents a sequence of zero or more items,
+ represents a sequence of one or more items, ? produces a non-greedy match, and so on.
Syntax:
Usage notes:
The RLIKE operator is a synonym for REGEXP.
The | symbol is the alternation operator, typically used within () to match different sequences. The () groups do
not allow backreferences. To retrieve the part of a value matched within a () section, use the regexp_extract()
built-in function.
In Impala 1.3.1 and higher, the REGEXP and RLIKE operators now match a regular expression string that occurs
anywhere inside the target string, the same as if the regular expression was enclosed on each side by .*. See
REGEXP Operator on page 190 for examples. Previously, these operators only succeeded when the regular
expression matched the entire target string. This change improves compatibility with the regular expression
support for popular database systems. There is no change to the behavior of the regexp_extract() and
regexp_replace() built-in functions.
In Impala 2.0 and later, the Impala regular expression syntax conforms to the POSIX Extended Regular Expression
syntax used by the Google RE2 library. For details, see the RE2 documentation. It has most idioms familiar from
regular expressions in Perl, Python, and so on, including .*? for non-greedy matches.
In Impala 2.0 and later, a change in the underlying regular expression library could cause changes in the way regular
expressions are interpreted by this function. Test any queries that use regular expressions and adjust the expression
patterns if necessary. See Incompatible Changes Introduced in Impala 2.0.0 on page 832 for details.
Complex type considerations:
You cannot refer to a column with a complex data type (ARRAY, STRUCT, or MAP directly in an operator. You can
apply operators only to scalar values that make up a complex type (the fields of a STRUCT, the items of an ARRAY,
or the key or value portion of a MAP) as part of a join query that refers to the scalar value using the appropriate dot
notation or ITEM, KEY, or VALUE pseudocolumn names.
Examples:
| Impala SQL Language Reference | 191
The following examples demonstrate the identical syntax for the REGEXP and RLIKE operators.
-- Find all customers whose first name starts with 'J', followed by 0 or
more of any character.
select c_first_name, c_last_name from customer where c_first_name regexp
'^J.*';
select c_first_name, c_last_name from customer where c_first_name rlike
'^J.*';
-- Find 'Macdonald', where the first 'a' is optional and the 'D' can be
upper- or lowercase.
-- The ^...$ are required, to match the start and end of the value.
select c_first_name, c_last_name from customer where c_last_name regexp
'^Ma?c[Dd]onald$';
select c_first_name, c_last_name from customer where c_last_name rlike '^Ma?
c[Dd]onald$';
-- Find names starting with 'S', then one or more vowels, then 'r', then any
other characters.
-- Matches 'Searcy', 'Sorenson', 'Sauer'.
select c_first_name, c_last_name from customer where c_last_name regexp
'^S[aeiou]+r.*$';
select c_first_name, c_last_name from customer where c_last_name rlike
'^S[aeiou]+r.*$';
-- Find names that end with 2 or more vowels: letters from the set
a,e,i,o,u.
select c_first_name, c_last_name from customer where c_last_name regexp
'.*[aeiou]{2,}$';
select c_first_name, c_last_name from customer where c_last_name rlike
'.*[aeiou]{2,}$';
-- You can use letter ranges in the [] blocks, for example to find names
starting with A, B, or C.
select c_first_name, c_last_name from customer where c_last_name regexp
'^[A-C].*';
select c_first_name, c_last_name from customer where c_last_name rlike '^[A-
C].*';
-- If you are not sure about case, leading/trailing spaces, and so on, you
can process the
-- column using string functions first.
select c_first_name, c_last_name from customer where
lower(trim(c_last_name)) regexp '^de.*';
select c_first_name, c_last_name from customer where
lower(trim(c_last_name)) rlike '^de.*';
Related information:
For regular expression matching with case-insensitive comparisons, see IREGEXP Operator on page 182.
RLIKE Operator
Synonym for the REGEXP operator. See REGEXP Operator on page 190 for details.
Examples:
| Impala SQL Language Reference | 192
The following examples demonstrate the identical syntax for the REGEXP and RLIKE operators.
-- Find all customers whose first name starts with 'J', followed by 0 or
more of any character.
select c_first_name, c_last_name from customer where c_first_name regexp
'^J.*';
select c_first_name, c_last_name from customer where c_first_name rlike
'^J.*';
-- Find 'Macdonald', where the first 'a' is optional and the 'D' can be
upper- or lowercase.
-- The ^...$ are required, to match the start and end of the value.
select c_first_name, c_last_name from customer where c_last_name regexp
'^Ma?c[Dd]onald$';
select c_first_name, c_last_name from customer where c_last_name rlike '^Ma?
c[Dd]onald$';
-- Find names starting with 'S', then one or more vowels, then 'r', then any
other characters.
-- Matches 'Searcy', 'Sorenson', 'Sauer'.
select c_first_name, c_last_name from customer where c_last_name regexp
'^S[aeiou]+r.*$';
select c_first_name, c_last_name from customer where c_last_name rlike
'^S[aeiou]+r.*$';
-- Find names that end with 2 or more vowels: letters from the set
a,e,i,o,u.
select c_first_name, c_last_name from customer where c_last_name regexp
'.*[aeiou]{2,}$';
select c_first_name, c_last_name from customer where c_last_name rlike
'.*[aeiou]{2,}$';
-- You can use letter ranges in the [] blocks, for example to find names
starting with A, B, or C.
select c_first_name, c_last_name from customer where c_last_name regexp
'^[A-C].*';
select c_first_name, c_last_name from customer where c_last_name rlike '^[A-
C].*';
-- If you are not sure about case, leading/trailing spaces, and so on, you
can process the
-- column using string functions first.
select c_first_name, c_last_name from customer where
lower(trim(c_last_name)) regexp '^de.*';
select c_first_name, c_last_name from customer where
lower(trim(c_last_name)) rlike '^de.*';
Within a table, partitions can also be considered a kind of object. Partitioning is an important subject for Impala,
with its own documentation section covering use cases and performance considerations. See Partitioning for Impala
Tables on page 661 for details.
Impala does not have a counterpart of the “tablespace” notion from some database systems. By default, all the data
files for a database, table, or partition are located within nested folders within the HDFS file system. You can also
specify a particular HDFS location for a given Impala table or partition. The raw data for these objects is represented
as a collection of data files, providing the flexibility to load data by simply moving files into the expected HDFS
location.
Information about the schema objects is held in the metastore database. This database is shared between Impala
and Hive, allowing each to create, drop, and query each other's databases, tables, and so on. When Impala makes a
change to schema objects through a CREATE, ALTER, DROP, INSERT, or LOAD DATA statement, it broadcasts
those changes to all nodes in the cluster through the catalog service. When you make such changes through Hive or
directly through manipulating HDFS files, you use the REFRESH or INVALIDATE METADATA statements on the
Impala side to recognize the newly loaded data, new tables, and so on.
-- The table aliases assigned in the FROM clause can be used both earlier
-- in the query (the SELECT list) and later (the WHERE clause).
SELECT one.name, two.address, three.phone
FROM census one, building_directory two, phonebook three
WHERE one.id = two.id and two.id = three.id;
-- The aliases c1 and c2 let the query handle columns with the same names
from 2 joined tables.
-- The aliases t1 and t2 let the query abbreviate references to long or
cryptically named tables.
SELECT t1.column_n AS c1, t2.column_n AS c2 FROM long_name_table AS t1,
very_long_name_table2 AS t2
WHERE c1 = c2;
SELECT t1.column_n c1, t2.column_n c2 FROM table1 t1, table2 t2
WHERE c1 = c2;
From Impala 3.0, the alias substitution logic in the GROUP BY, HAVING, and ORDER BY clauses has become
more consistent with standard SQL behavior, as follows. Aliases are now only legal at the top level, and not in
subexpressions. The following statements are allowed:
SELECT int_col / 2 AS x
FROM t
GROUP BY x;
SELECT int_col / 2 AS x
FROM t
ORDER BY x;
SELECT int_col / 2 AS x
FROM t
GROUP BY x / 2;
SELECT int_col / 2 AS x
FROM t
ORDER BY -x;
SELECT int_col / 2 AS x
FROM t
GROUP BY x
HAVING x > 3;
To use an alias name that matches one of the Impala reserved keywords (listed in Impala Reserved Words on page
783), surround the identifier with either single or double quotation marks, or `` characters (backticks).
Aliases follow the same rules as identifiers when it comes to case insensitivity. Aliases can be longer than identifiers
(up to the maximum length of a Java string) and can include additional characters such as spaces and dashes when
they are quoted using backtick characters.
Complex type considerations:
| Impala SQL Language Reference | 195
Queries involving the complex types (ARRAY, STRUCT, and MAP), typically make extensive use of table aliases.
These queries involve join clauses where the complex type column is treated as a joined table. To construct two-part
or three-part qualified names for the complex column elements in the FROM list, sometimes it is syntactically required
to construct a table alias for the complex column where it is referenced in the join clause. See Complex Types (Impala
2.3 or higher only) on page 134 for details and examples.
Alternatives:
Another way to define different names for the same tables or columns is to create views. See Overview of Impala
Views on page 200 for details.
Related statements:
CREATE DATABASE Statement on page 231, DROP DATABASE Statement on page 270, USE Statement on
page 406, SHOW DATABASES on page 386
| CANADA | 25000000 |
| GUATEMALA | 16000000 |
+-------------+------------+
Aggregate functions combine the results from multiple rows: either a single result for the entire table, or a separate
result for each group of rows. Aggregate functions are frequently used in combination with GROUP BY and HAVING
clauses in the SELECT statement. See Impala Aggregate Functions on page 516.
User-defined functions (UDFs) let you code your own logic. They can be either scalar or aggregate functions. UDFs
let you implement important business or scientific logic using high-performance code for Impala to automatically
parallelize. You can also use UDFs to implement convenience functions to simplify reporting or porting SQL from
other database systems. See User-Defined Functions (UDFs) on page 566.
Each function is associated with a specific database. For example, if you issue a USE somedb statement followed
by CREATE FUNCTION somefunc, the new function is created in the somedb database, and you could refer to
it through the fully qualified name somedb.somefunc. You could then issue another USE statement and create a
function with the same name in a different database.
Impala built-in functions are associated with a special database named _impala_builtins, which lets you refer
to them from any database without qualifying the name.
+-------------+-----------------------------------+
Related statements: CREATE FUNCTION Statement on page 233, DROP FUNCTION Statement on page 272
[localhost:21000] >
Important: Because the list of reserved words grows over time as new SQL syntax is added, consider adopting
coding conventions (especially for any automated scripts or in packaged applications) to always quote all
identifiers with backticks. Quoting all identifiers protects your SQL from compatibility issues if new reserved
words are added in later releases.
• Impala identifiers are always case-insensitive. That is, tables named t1 and T1 always refer to the same table,
regardless of quote characters. Internally, Impala always folds all specified table and column names to lowercase.
This is why the column headers in query output are always displayed in lowercase.
See Overview of Impala Aliases on page 193 for how to define shorter or easier-to-remember aliases if the
original names are long or cryptic identifiers. Aliases follow the same rules as identifiers when it comes to case
insensitivity. Aliases can be longer than identifiers (up to the maximum length of a Java string) and can include
additional characters such as spaces and dashes when they are quoted using backtick characters.
Another way to define different names for the same tables or columns is to create views. See Overview of Impala
Views on page 200 for details.
| Impala SQL Language Reference | 198
Tables are the primary containers for data in Impala. They have the familiar row and column layout similar to other
database systems, plus some features such as partitioning often associated with higher-end data warehouse systems.
Logically, each table has a structure based on the definition of its columns, partitions, and other properties.
Physically, each table that uses HDFS storage is associated with a directory in HDFS. The table data consists of all
the data files underneath that directory:
• Internal tables are managed by Impala, and use directories inside the designated Impala work area.
• External tables use arbitrary HDFS directories, where the data files are typically shared between different Hadoop
components.
• Large-scale data is usually handled by partitioned tables, where the data files are divided among different HDFS
subdirectories.
Impala tables can also represent data that is stored in HBase, or in the Amazon S3 filesystem (Impala 2.2 or higher),
or on Isilon storage devices (Impala 2.2.3 or higher). See Using Impala to Query HBase Tables on page 722, Using
Impala with the Amazon S3 Filesystem on page 732, and Using Impala with Isilon Storage on page 748 for
details about those special kinds of tables.
Impala queries ignore files with extensions commonly used for temporary work files by Hadoop tools. Any files with
extensions .tmp or .copying are not considered part of the Impala table. The suffix matching is case-insensitive,
so for example Impala ignores both .copying and .COPYING suffixes.
Related statements: CREATE TABLE Statement on page 240, DROP TABLE Statement on page 278, ALTER
TABLE Statement on page 207 INSERT Statement on page 288, LOAD DATA Statement on page 299,
SELECT Statement on page 306
Internal Tables
The default kind of table produced by the CREATE TABLE statement is known as an internal table. (Its counterpart is
the external table, produced by the CREATE EXTERNAL TABLE syntax.)
• Impala creates a directory in HDFS to hold the data files.
• You can create data in internal tables by issuing INSERT or LOAD DATA statements.
• If you add or replace data using HDFS operations, issue the REFRESH command in impala-shell so that
Impala recognizes the changes in data files, block locations, and so on.
• When you issue a DROP TABLE statement, Impala physically removes all the data files from the directory.
• To see whether a table is internal or external, and its associated HDFS location, issue the statement DESCRIBE
FORMATTED table_name. The Table Type field displays MANAGED_TABLE for internal tables and
EXTERNAL_TABLE for external tables. The Location field displays the path of the table directory as an HDFS
URI.
• When you issue an ALTER TABLE statement to rename an internal table, all data files are moved into the new
HDFS directory for the table. The files are moved even if they were formerly in a directory outside the Impala
data directory, for example in an internal table with a LOCATION attribute pointing to an outside HDFS directory.
Examples:
You can switch a table from internal to external, or from external to internal, by using the ALTER TABLE statement:
If the Kudu service is integrated with the Hive Metastore, the above operations are not supported.
Related information:
| Impala SQL Language Reference | 199
External Tables on page 199, CREATE TABLE Statement on page 240, DROP TABLE Statement on page
278, ALTER TABLE Statement on page 207, DESCRIBE Statement on page 258
External Tables
The syntax CREATE EXTERNAL TABLE sets up an Impala table that points at existing data files, potentially in
HDFS locations outside the normal Impala data directories.. This operation saves the expense of importing the data
into a new table when you already have the data files in a known location in HDFS, in the desired file format.
• You can use Impala to query the data in this table.
• You can create data in external tables by issuing INSERT or LOAD DATA statements.
• If you add or replace data using HDFS operations, issue the REFRESH command in impala-shell so that
Impala recognizes the changes in data files, block locations, and so on.
• When you issue a DROP TABLE statement in Impala, that removes the connection that Impala has with the
associated data files, but does not physically remove the underlying data. You can continue to use the data files
with other Hadoop components and HDFS operations.
• To see whether a table is internal or external, and its associated HDFS location, issue the statement DESCRIBE
FORMATTED table_name. The Table Type field displays MANAGED_TABLE for internal tables and
EXTERNAL_TABLE for external tables. The Location field displays the path of the table directory as an HDFS
URI.
• When you issue an ALTER TABLE statement to rename an external table, all data files are left in their original
locations.
• You can point multiple external tables at the same HDFS directory by using the same LOCATION attribute for
each one. The tables could have different column definitions, as long as the number and types of columns are
compatible with the schema evolution considerations for the underlying file type. For example, for text data files,
one table might define a certain column as a STRING while another defines the same column as a BIGINT.
Examples:
You can switch a table from internal to external, or from external to internal, by using the ALTER TABLE statement:
If the Kudu service is integrated with the Hive Metastore, the above operations are not supported.
Related information:
Internal Tables on page 198, CREATE TABLE Statement on page 240, DROP TABLE Statement on page
278, ALTER TABLE Statement on page 207, DESCRIBE Statement on page 258
File Formats
Each table has an associated file format, which determines how Impala interprets the associated data files. See How
Impala Works with Hadoop File Formats on page 671 for details.
You set the file format during the CREATE TABLE statement, or change it later using the ALTER TABLE statement.
Partitioned tables can have a different file format for individual partitions, allowing you to change the file format used
in your ETL process for new data without going back and reconverting all the existing data in the same table.
Any INSERT statements produce new data files with the current file format of the table. For existing data files,
changing the file format of the table does not automatically do any data conversion. You must use TRUNCATE
TABLE or INSERT OVERWRITE to remove any previous data files that use the old file format. Then you use the
LOAD DATA statement, INSERT ... SELECT, or other mechanism to put data files of the correct format into the
table.
| Impala SQL Language Reference | 200
The default file format, text, is the most flexible and easy to produce when you are just getting started with Impala.
The Parquet file format offers the highest query performance and uses compression to reduce storage requirements;
therefore, where practical, use Parquet for Impala tables with substantial amounts of data. Also, the complex types
(ARRAY, STRUCT, and MAP) available in Impala 2.3 and higher are currently only supported with the Parquet file
type. Based on your existing ETL workflow, you might use other file formats such as Avro, possibly doing a final
conversion step to Parquet to take advantage of its performance for analytic queries.
Kudu Tables
By default, tables stored in Apache Kudu are treated specially, because Kudu manages its data independently of
HDFS files.
All metadata that Impala needs is stored in the HMS.
When Kudu is not integrated with the HMS, when you create a Kudu table through Impala, the table is assigned an
internal Kudu table name of the form impala::db_name.table_name. You can see the Kudu-assigned name in
the output of DESCRIBE FORMATTED, in the kudu.table_name field of the table properties.
For Impala-Kudu managed tables, ALTER TABLE ... RENAME renames both the Impala and the Kudu table.
For Impala-Kudu external tables, ALTER TABLE ... RENAME renames just the Impala table. To
change the Kudu table that an Impala external table points to, use ALTER TABLE impala_name SET
TBLPROPERTIES('kudu.table_name' = 'different_kudu_table_name'). The underlying Kudu
table must already exist.
In practice, external tables are typically used to access underlying Kudu tables that were created outside of Impala,
that is, through the Kudu API.
The SHOW TABLE STATS output for a Kudu table shows Kudu-specific details about the layout of the table. Instead
of information about the number and sizes of files, the information is divided by the Kudu tablets. For each tablet,
the output includes the fields # Rows (although this number is not currently computed), Start Key, Stop Key,
Leader Replica, and # Replicas. The output of SHOW COLUMN STATS, illustrating the distribution of
values within each column, is the same for Kudu tables as for HDFS-backed tables.
If the Kudu service is not integrated with the Hive Metastore, the distinction between internal and external tables
has some special details for Kudu tables. Tables created entirely through Impala are internal tables. The table name
as represented within Kudu includes notation such as an impala:: prefix and the Impala database name. External
Kudu tables are those created by a non-Impala mechanism, such as a user application calling the Kudu APIs. For
these tables, the CREATE EXTERNAL TABLE syntax lets you establish a mapping from Impala to the existing Kudu
table:
External Kudu tables differ in one important way from other external tables: adding or dropping a column or range
partition changes the data in the underlying Kudu table, in contrast to an HDFS-backed external table where existing
data files are left untouched.
-- ... and now you can produce the report with 1 line of code.
select * from v1;
• Reduce maintenance, by avoiding the duplication of complicated queries across multiple applications in multiple
languages:
This technique lets you build up several more or less granular variations of the same query, and switch between
them when appropriate.
• Set up aliases with intuitive names for tables, columns, result sets from joins, and so on:
-- The original tables might have cryptic names inherited from a legacy
system.
create view action_items as select rrptsk as assignee, treq as due_date,
dmisc as notes from vxy_t1_br;
-- You can leave original names for compatibility, build new applications
using more intuitive ones.
select assignee, due_date, notes from action_items;
• Swap tables with others that use different file formats, partitioning schemes, and so on without any downtime for
data copying or conversion:
The SQL statements that configure views are CREATE VIEW Statement on page 254, ALTER VIEW Statement on
page 222, and DROP VIEW Statement on page 280. You can specify view names when querying data (SELECT
Statement on page 306) and copying data from one table to another (INSERT Statement on page 288). The
WITH clause creates an inline view, that only exists for the duration of a single query.
The previous example uses descending order for ORDERED_RESULTS because in the sample TPCD-H data, there
are some rows with empty strings for both C_FIRST_NAME and C_LAST_NAME, making the lowest-ordered names
unuseful in a sample query.
Usage notes:
To see the definition of a view, issue a DESCRIBE FORMATTED statement, which shows the query from the original
CREATE VIEW statement:
Prior to Impala 1.4.0, it was not possible to use the CREATE TABLE LIKE view_name syntax. In Impala 1.4.0
and higher, you can create a table with the same column definitions as a view using the CREATE TABLE LIKE
technique. Although CREATE TABLE LIKE normally inherits the file format of the original table, a view has no
underlying file format, so CREATE TABLE LIKE view_name produces a text table by default. To specify a
different file format, include a STORED AS file_format clause at the end of the CREATE TABLE LIKE
statement.
Complex type considerations:
For tables containing complex type columns (ARRAY, STRUCT, or MAP), you typically use join queries to refer to the
complex values. You can use views to hide the join notation, making such tables seem like traditional denormalized
tables, and making those tables queryable by business intelligence tools that do not have built-in support for those
complex types. See Accessing Complex Type Data in Flattened Form Using Views on page 155 for details.
The STRAIGHT_JOIN hint affects the join order of table references in the query block containing the hint. It does
not affect the join order of nested queries, such as views, inline views, or WHERE-clause subqueries. To use this hint
for performance tuning of complex queries, apply the hint to all query blocks that need a fixed join order.
Restrictions:
• You cannot insert into an Impala view. (In some database systems, this operation is allowed and inserts rows into
the base table.) You can use a view name on the right-hand side of an INSERT statement, in the SELECT part.
• If a view applies to a partitioned table, any partition pruning considers the clauses on both the original query and
any additional WHERE predicates in the query that refers to the view. Prior to Impala 1.4, only the WHERE clauses
on the original query from the CREATE VIEW statement were used for partition pruning.
• An ORDER BY clause without an additional LIMIT clause is ignored in any view definition. If you need to
sort the entire result set from a view, use an ORDER BY clause in the SELECT statement that queries the view.
You can still make a simple “top 10” report by combining the ORDER BY and LIMIT clauses in the same view
definition:
+---+
| x |
+---+
| 1 |
| 9 |
| 3 |
| 7 |
| 5 |
| 8 |
| 4 |
| 6 |
| 2 |
+---+
[localhost:21000] > select x from sorted_view order by x; -- View query
requires ORDER BY at outermost level.
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
[localhost:21000] > create view top_3_view as select x from unsorted order
by x limit 3;
[localhost:21000] > select x from top_3_view; -- ORDER BY and LIMIT
together in view definition are preserved.
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
• The TABLESAMPLE clause of the SELECT statement does not apply to a table reference derived from a view, a
subquery, or anything other than a real base table. This clause only works for tables backed by HDFS or HDFS-
like data files, therefore it does not apply to Kudu or HBase tables.
Related statements: CREATE VIEW Statement on page 254, ALTER VIEW Statement on page 222, DROP
VIEW Statement on page 280
Impala Transactions
A transaction is a single logical operation on the data. Impala supports transactions that satisfy a level of consistency
that improves the integrity and reliability of the data before and after a transaction.
Specifically, Impala provides atomicity and isolation of insert operations on transactional tables. A single table insert
is either committed in full or not committed, and the results of the insert operation are not visible to other query
operations until the operation is committed.
For single table, the inserts are ordered, so if Impala doesn't see a committed insert, it won't see any insert committed
after it.
For insert-only transactional tables, you can perform the following statements: CREATE TABLE, DROP TABLE,
INSERT, SELECT
| Impala SQL Language Reference | 205
All transactions in Impala automatically commit at the end of the statement. Currently, Impala does not support multi-
statement transactions.
Insert-only tables must be the managed and file-format based tables, such as Parquet, Avro, and text.
Note: Impala does not support changing transactional properties of tables. For example, you cannot alter a
transactional table to a non-transactional table.
DDL Statements
DDL refers to “Data Definition Language”, a subset of SQL statements that change the structure of the database
schema in some way, typically by creating, deleting, or modifying schema objects such as databases, tables, and
views. Most Impala DDL statements start with the keywords CREATE, DROP, or ALTER.
The Impala DDL statements are:
• ALTER TABLE Statement on page 207
• ALTER VIEW Statement on page 222
• COMPUTE STATS Statement on page 223
• CREATE DATABASE Statement on page 231
• CREATE FUNCTION Statement on page 233
• CREATE ROLE Statement (Impala 2.0 or higher only) on page 239
• CREATE TABLE Statement on page 240
• CREATE VIEW Statement on page 254
• DROP DATABASE Statement on page 270
• DROP FUNCTION Statement on page 272
• DROP ROLE Statement (Impala 2.0 or higher only) on page 273
• DROP TABLE Statement on page 278
• DROP VIEW Statement on page 280
• GRANT Statement (Impala 2.0 or higher only) on page 284
• REVOKE Statement (Impala 2.0 or higher only) on page 305
After Impala executes a DDL command, information about available tables, columns, views, partitions, and so
on is automatically synchronized between all the Impala nodes in a cluster. (Prior to Impala 1.2, you had to issue
a REFRESH or INVALIDATE METADATA statement manually on the other nodes to make them aware of the
changes.)
If the timing of metadata updates is significant, for example if you use round-robin scheduling where each query
could be issued through a different Impala node, you can enable the SYNC_DDL query option to make the DDL
statement wait until all nodes have been notified about the metadata changes.
See Using Impala with the Amazon S3 Filesystem on page 732 for details about how Impala DDL statements
interact with tables and partitions stored in the Amazon S3 filesystem.
Although the INSERT statement is officially classified as a DML (data manipulation language) statement, it also
involves metadata changes that must be broadcast to all Impala nodes, and so is also affected by the SYNC_DDL
query option.
| Impala SQL Language Reference | 206
Because the SYNC_DDL query option makes each DDL operation take longer than normal, you might only enable
it before the last DDL operation in a sequence. For example, if you are running a script that issues multiple of DDL
operations to set up an entire new schema, add several new partitions, and so on, you might minimize the performance
overhead by enabling the query option only before the last CREATE, DROP, ALTER, or INSERT statement. The
script only finishes when all the relevant metadata changes are recognized by all the Impala nodes, so you could
connect to any node and issue queries through it.
The classification of DDL, DML, and other statements is not necessarily the same between Impala and Hive. Impala
organizes these statements in a way intended to be familiar to people familiar with relational databases or data
warehouse products. Statements that modify the metastore database, such as COMPUTE STATS, are classified
as DDL. Statements that only query the metastore database, such as SHOW or DESCRIBE, are put into a separate
category of utility statements.
Note: The query types shown in the Impala debug web user interface might not match exactly the categories listed
here. For example, currently the USE statement is shown as DDL in the debug web UI. The query types shown in the
debug web UI are subject to change, for improved consistency.
Related information:
The other major classifications of SQL statements are data manipulation language (see DML Statements on page
206) and queries (see SELECT Statement on page 306).
DML Statements
DML refers to “Data Manipulation Language”, a subset of SQL statements that modify the data stored in tables.
Because Impala focuses on query performance and leverages the append-only nature of HDFS storage, currently
Impala only supports a small set of DML statements:
• DELETE Statement (Impala 2.8 or higher only) on page 256. Works for Kudu tables only.
• INSERT Statement on page 288.
• LOAD DATA Statement on page 299. Does not apply for HBase or Kudu tables.
• UPDATE Statement (Impala 2.8 or higher only) on page 404. Works for Kudu tables only.
• UPSERT Statement (Impala 2.8 or higher only) on page 405. Works for Kudu tables only.
INSERT in Impala is primarily optimized for inserting large volumes of data in a single statement, to make effective
use of the multi-megabyte HDFS blocks. This is the way in Impala to create new data files. If you intend to insert one
or a few rows at a time, such as using the INSERT ... VALUES syntax, that technique is much more efficient for
Impala tables stored in HBase. See Using Impala to Query HBase Tables on page 722 for details.
LOAD DATA moves existing data files into the directory for an Impala table, making them immediately available for
Impala queries. This is one way in Impala to work with data files produced by other Hadoop components. (CREATE
EXTERNAL TABLE is the other alternative; with external tables, you can query existing data files, while the files
remain in their original location.)
In Impala 2.8 and higher, Impala does support the UPDATE, DELETE, and UPSERT statements for Kudu tables. For
HDFS or S3 tables, to simulate the effects of an UPDATE or DELETE statement in other database systems, typically
you use INSERT or CREATE TABLE AS SELECT to copy data from one table to another, filtering out or changing
the appropriate rows during the copy operation.
You can also achieve a result similar to UPDATE by using Impala tables stored in HBase. When you insert a row into
an HBase table, and the table already contains a row with the same value for the key column, the older row is hidden,
effectively the same as a single-row UPDATE.
Impala can perform DML operations for tables or partitions stored in the Amazon S3 filesystem with Impala 2.6 and
higher. See Using Impala with the Amazon S3 Filesystem on page 732 for details.
Related information:
The other major classifications of SQL statements are data definition language (see DDL Statements on page 205)
and queries (see SELECT Statement on page 306).
| Impala SQL Language Reference | 207
ALTER TABLE name ADD [IF NOT EXISTS] COLUMNS (col_spec[, col_spec ...])
ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])
table; you must prepare any Parquet data files containing complex types outside Impala, and bring them into the table
using LOAD DATA or updating the table's LOCATION property. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types.
Usage notes:
Whenever you specify partitions in an ALTER TABLE statement, through the PARTITION (partition_spec)
clause, you must include all the partitioning columns in the specification.
Most of the ALTER TABLE operations work the same for internal tables (managed by Impala) as for external tables
(with data files located in arbitrary locations). The exception is renaming a table; for an external table, the underlying
data directory is not renamed or moved.
To drop or alter multiple partitions:
In Impala 2.8 and higher, the expression for the partition clause with a DROP or SET operation can include
comparison operators such as <, IN, or BETWEEN, and Boolean operators such as AND and OR.
For example, you might drop a group of partitions corresponding to a particular date range after the data “ages out”:
For tables with multiple partition keys columns, you can specify multiple conditions separated by commas, and the
operation only applies to the partitions that match all the conditions (similar to using an AND clause):
alter table historical_data drop partition (year < 1995, last_name like 'A
%');
This technique can also be used to change the file format of groups of partitions, as part of an ETL pipeline that
periodically consolidates and rewrites the underlying data files in a different file format:
Note:
The extended syntax involving comparison operators and multiple partitions applies to the SET FILEFORMAT, SET
TBLPROPERTIES, SET SERDEPROPERTIES, and SET [UN]CACHED clauses. You can also use this syntax
with the PARTITION clause in the COMPUTE INCREMENTAL STATS statement, and with the PARTITION clause
of the SHOW FILES statement. Some forms of ALTER TABLE still only apply to one partition at a time: the SET
LOCATION and ADD PARTITION clauses. The PARTITION clauses in the LOAD DATA and INSERT statements
also only apply to one partition at a time.
A DDL statement that applies to multiple partitions is considered successful (resulting in no changes) even if no
partitions match the conditions. The results are the same as if the IF EXISTS clause was specified.
The performance and scalability of this technique is similar to issuing a sequence of single-partition ALTER TABLE
statements in quick succession. To minimize bottlenecks due to communication with the metastore database, or
causing other DDL operations on the same table to wait, test the effects of performing ALTER TABLE statements
that affect large numbers of partitions.
Amazon S3 considerations:
You can specify an s3a:// prefix on the LOCATION attribute of a table or partition to make Impala query data
from the Amazon S3 filesystem. In Impala 2.6 and higher, Impala automatically handles creating or removing the
| Impala SQL Language Reference | 210
associated folders when you issue ALTER TABLE statements with the ADD PARTITION or DROP PARTITION
clauses.
In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP
DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove
folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala
database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with
the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with Impala.
HDFS caching (CACHED IN clause):
If you specify the CACHED IN clause, any existing or future data files in the table directory or the partition
subdirectories are designated to be loaded into memory with the HDFS caching mechanism. See Using HDFS
Caching with Impala (Impala 2.1 or higher only) on page 622 for details about using the HDFS caching feature.
In Impala 2.2 and higher, the optional WITH REPLICATION clause for CREATE TABLE and ALTER TABLE lets
you specify a replication factor, the number of hosts on which to cache the same data blocks. When Impala processes
a cached data block, where the cache replication factor is greater than 1, Impala randomly selects a host that has a
cached copy of that data block. This optimization avoids excessive CPU usage on a single host when the same cached
data block is processed multiple times. Where practical, specify a value greater than or equal to the HDFS block
replication factor.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
To rename a table (RENAME TO clause):
The RENAME TO clause lets you change the name of an existing table, and optionally which database it is located in.
For internal tables, this operation physically renames the directory within HDFS that contains the data files; the
original directory name no longer exists. By qualifying the table names with database names, you can use this
technique to move an internal table (and its associated data directory) from one database to another. For example:
The table owner is originally set to the user who creates the table. When object ownership is enabled in Sentry,
an owner of a table can have the ALL with GRANT or ALL without GRANT privilege. The term OWNER is used to
differentiate between the ALL privilege that is explicitly granted via the GRANT statement and a privilege that is
implicitly granted by the CREATE TABLE statement.
Use the ALTER TABLE SET OWNER to transfer the ownership from the current owner to another user or a role.
To change the physical location where Impala looks for data files associated with a table or partition:
The path you specify is the full HDFS path where the data files reside, or will be created. Impala does not create any
additional subdirectory named after the table. Impala does not move any data files to this new location or change any
data files that might already exist in that directory.
To set the location for a single partition, include the PARTITION clause. Specify all the same partitioning columns
for the table, with a constant value for each, to precisely identify the single partition affected by the statement:
Note: If you are creating a partition for the first time and specifying its location, for maximum efficiency, use a
single ALTER TABLE statement including both the ADD PARTITION and LOCATION clauses, rather than separate
statements with ADD PARTITION and SET LOCATION clauses.
To automatically detect new partition directories added through Hive or HDFS operations:
In Impala 2.3 and higher, the RECOVER PARTITIONS clause scans a partitioned table to detect if any new partition
directories were added outside of Impala, such as by Hive ALTER TABLE statements or by hdfs dfs or hadoop
fs commands. The RECOVER PARTITIONS clause automatically recognizes any data files present in these new
directories, the same as the REFRESH statement does.
For example, here is a sequence of examples showing how you might create a partitioned table in Impala, create new
partitions through Hive, copy data files into the new partitions with the hdfs command, and have Impala recognize
the new partitions and new data:
In Impala, create the table, and a single partition for demonstration purposes:
In Hive, create some new partitions. In a real use case, you might create the partitions and populate them with data as
the final stages of an ETL pipeline.
OK
hive> alter table t1 add partition (yy = 2016, mm = 3);
OK
hive> quit;
For demonstration purposes, manually copy data (a single row) into these new partitions, using manual HDFS
operations:
In Impala, initially the partitions and data are not visible. Running ALTER TABLE with the RECOVER
PARTITIONS clause scans the table data directory to find any new partition directories, and the data files inside
them:
The TBLPROPERTIES clause is primarily a way to associate arbitrary user-specified data items with a particular
table.
The SERDEPROPERTIES clause sets up metadata defining how tables are read or written, needed in some cases by
Hive but not used extensively by Impala. You would use this clause primarily to change the delimiter in an existing
text table or partition, by setting the 'serialization.format' and 'field.delim' property values to the
new delimiter character: The SERDEPROPERTIES clause does not change the existing data in the table. The change
only affects the future insert into the table.
Use the DESCRIBE FORMATTED statement to see the current values of these properties for an existing table. See
CREATE TABLE Statement on page 240 for more details about these clauses.
To manually set or update table or column statistics:
Although for most tables the COMPUTE STATS or COMPUTE INCREMENTAL STATS statement is all you need
to keep table and column statistics up to date for a table, sometimes for a very large table or one that is updated
frequently, the length of time to recompute all the statistics might make it impractical to run those statements as often
as needed. As a workaround, you can use the ALTER TABLE statement to set table statistics at the level of the entire
table or a single partition, or column statistics at the level of the entire table.
You can set the numrows value for table statistics by changing the TBLPROPERTIES setting for a table or partition.
For example:
In Impala 2.6 and higher, you can use the SET COLUMN STATS clause to set a specific stats value for a particular
column.
You specify a case-insensitive symbolic name for the kind of statistics: numDVs, numNulls, avgSize, maxSize.
The key names and values are both quoted. This operation applies to an entire table, not a specific partition. For
example:
| x | INT | -1 | -1 | 4 | 4 |
| s | STRING | -1 | -1 | -1 | -1 |
+--------+--------+------------------+--------+----------+----------+
alter table t1 set column stats x ('numDVs'='2','numNulls'='0');
alter table t1 set column stats s ('numdvs'='3','maxsize'='4');
show column stats t1;
+--------+--------+------------------+--------+----------+----------+
| Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
+--------+--------+------------------+--------+----------+----------+
| x | INT | 2 | 0 | 4 | 4 |
| s | STRING | 3 | -1 | 4 | -1 |
+--------+--------+------------------+--------+----------+----------+
You might use the CHANGE clause to rename a single column, or to treat an existing column as a different type than
before, such as to switch between treating a column as STRING and TIMESTAMP, or between INT and BIGINT.
You can only drop a single column at a time; to drop multiple columns, issue multiple ALTER TABLE statements, or
define the new set of columns with a single ALTER TABLE ... REPLACE COLUMNS statement.
The following examples show some safe operations to drop or change columns. Dropping the final column in a table
lets Impala ignore the data causing any disruption to existing data files. Changing the type of a column works if
existing data values can be safely converted to the new type. The type conversion rules depend on the file format of
the underlying table. For example, in a text table, the same value can be interpreted as a STRING or a numeric value,
| Impala SQL Language Reference | 215
while in a binary format such as Parquet, the rules are stricter and type conversions only work between certain sizes of
integers.
-- When the last column in the table is dropped, Impala ignores the
-- values that are no longer needed. (Dropping A1 but leaving A2
-- would cause problems, as we will see in a subsequent example.)
alter table optional_columns drop column a2;
alter table optional_columns drop column a1;
-- Once the type is changed, we can insert non-integer values into the X
column
-- and treat that column as a string, for example by uppercasing or
concatenating.
insert into int_to_string values ('three', 'trois');
select s, upper(x) from int_to_string;
+-------+----------+
| s | upper(x) |
+-------+----------+
| one | 1 |
| two | 2 |
| three | TROIS |
+-------+----------+
Remember that Impala does not actually do any conversion for the underlying data files as a result of ALTER TABLE
statements. If you use ALTER TABLE to create a table layout that does not agree with the contents of the underlying
files, you must replace the files yourself, such as using LOAD DATA to load a new set of data files, or INSERT
OVERWRITE to copy from another table and replace the original data.
The following example shows what happens if you delete the middle column from a Parquet table containing three
columns. The underlying data files still contain three columns of data. Because the columns are interpreted based
on their positions in the data file instead of the specific column names, a SELECT * query now reads the first and
second columns from the data file, potentially leading to unexpected results or conversion errors. For this reason,
if you expect to someday drop a column, declare it as the last column in the table, where its data can be ignored by
queries after the column is dropped. Or, re-run your ETL process and create new data files if you drop or change the
type of a column in a way that causes problems with existing data files.
In Impala 2.6 and higher, if an Avro table is created without column definitions in the CREATE TABLE statement,
and columns are later added through ALTER TABLE, the resulting table is now queryable. Missing values from the
newly added columns now default to NULL.
To change the file format that Impala expects data to be in, for a table or partition:
| Impala SQL Language Reference | 217
Use an ALTER TABLE ... SET FILEFORMAT clause. You can include an optional PARTITION
(col1=val1, col2=val2, ... clause so that the file format is changed for a specific partition rather than the
entire table.
Because this operation only changes the table metadata, you must do any conversion of existing data using regular
Hadoop techniques outside of Impala. Any new data created by the Impala INSERT statement will be in the new
format. You cannot specify the delimiter for Text files; the data files must be comma-delimited.
To set the file format for a single partition, include the PARTITION clause. Specify all the same partitioning columns
for the table, with a constant value for each, to precisely identify the single partition affected by the statement:
speed up queries on partitioned tables, particularly join queries. Here is an example showing the ADD PARTITION
and DROP PARTITION clauses.
To avoid errors while adding or dropping partitions whose existence is not certain, add the optional IF [NOT]
EXISTS clause between the ADD or DROP keyword and the PARTITION keyword. That is, the entire clause
becomes ADD IF NOT EXISTS PARTITION or DROP IF EXISTS PARTITION. The following example
shows how partitions can be created automatically through INSERT statements, or manually through ALTER TABLE
statements. The IF [NOT] EXISTS clauses let the ALTER TABLE statements succeed even if a new requested
partition already exists, or a partition to be dropped does not exist.
Inserting 2 year values creates 2 partitions:
Without the IF NOT EXISTS clause, an attempt to add a new partition might fail:
The IF NOT EXISTS clause makes the statement succeed whether or not there was already a partition with the
specified key value:
Likewise, the IF EXISTS clause lets DROP PARTITION succeed whether or not the partition is already in the
table:
The optional PURGE keyword, available in Impala 2.3 and higher, is used with the DROP PARTITION clause to
remove associated HDFS data files immediately rather than going through the HDFS trashcan mechanism. Use this
keyword when dropping a partition if it is crucial to remove the data as quickly as possible to free up space, or if there
is a problem with the trashcan, such as the trash cannot being configured or being in a different HDFS encryption
zone than the data files.
-- If the data files for subsequent months were in a different file format,
-- you could set a different file format for the new partition as you create
it.
alter table part_t add partition (month=3) set fileformat=parquet;
The value specified for a partition key can be an arbitrary constant expression, without any references to columns. For
example:
Note:
An alternative way to reorganize a table and its associated data files is to use CREATE TABLE to create a variation
of the original table, then use INSERT to copy the transformed or reordered data to the new table. The advantage of
| Impala SQL Language Reference | 220
ALTER TABLE is that it avoids making a duplicate copy of the data files, allowing you to reorganize huge volumes
of data in a space-efficient way using familiar Hadoop techniques.
To switch a table between internal and external:
You can switch a table from internal to external, or from external to internal, by using the ALTER TABLE statement:
If the Kudu service is integrated with the Hive Metastore, the above operations are not supported.
Cancellation: Cannot be cancelled.
HDFS permissions:
Most ALTER TABLE clauses do not actually read or write any HDFS files, and so do not depend on specific HDFS
permissions. For example, the SET FILEFORMAT clause does not actually check the file format existing data files or
convert them to the new format, and the SET LOCATION clause does not require any special permissions on the new
location. (Any permission-related failures would come later, when you actually query or insert into the table.)
In general, ALTER TABLE clauses that do touch HDFS files and directories require the same HDFS permissions
as corresponding CREATE, INSERT, or SELECT statements. The permissions allow the user ID that the impalad
daemon runs under, typically the impala user, to read or write files or directories, or (in the case of the execute
bit) descend into a directory. The RENAME TO clause requires read, write, and execute permission in the source and
destination database directories and in the table data directory, and read and write permission for the data files within
the table. The ADD PARTITION and DROP PARTITION clauses require write and execute permissions for the
associated partition directory.
Kudu considerations:
Because of the extra constraints and features of Kudu tables, such as the NOT NULL and DEFAULT attributes for
columns, ALTER TABLE has specific requirements related to Kudu tables:
• In an ADD COLUMNS operation, you can specify the NULL, NOT NULL, and DEFAULT default_value
column attributes.
• In Impala 2.9 and higher, you can also specify the ENCODING, COMPRESSION, and BLOCK_SIZE attributes
when adding a column.
• If you add a column with a NOT NULL attribute, it must also have a DEFAULT attribute, so the default value can
be assigned to that column for all existing rows.
• The DROP COLUMN clause works the same for a Kudu table as for other kinds of tables.
• Although you can change the name of a column with the CHANGE clause, you cannot change the type of a column
in a Kudu table.
• You cannot change the nullability of existing columns in a Kudu table.
• In Impala 2.10, you can change the default value, encoding, compression, or block size of existing columns in a
Kudu table by using the SET clause.
• You cannot use the REPLACE COLUMNS clause with a Kudu table.
• The RENAME TO clause for a Kudu table only affects the name stored in the metastore database
that Impala uses to refer to the table. To change which underlying Kudu table is associated
with an Impala table name, you must change the TBLPROPERTIES property of the table: SET
TBLPROPERTIES('kudu.table_name'='kudu_tbl_name). You can only change underlying Kudu
tables for the external tables.
The following are some examples of using the ADD COLUMNS clause for a Kudu table:
The following are some examples of modifying column defaults and storage attributes for a Kudu table:
-- You can change the default value for a column, which affects any rows
-- inserted after this change is made.
alter table kt alter column s set default 'no';
-- You can remove the default value for a column, which affects any rows
-- inserted after this change is made. If the column is nullable, any
-- future inserts default to NULL for this column. If the column is marked
-- NOT NULL, any future inserts must specify a value for the column.
alter table kt alter column s drop default;
desc kt;
+------+-----------+---------+-------------+----------+---------------
+-----------------+---------------------+------------+
| name | type | comment | primary_key | nullable | default_value |
encoding | compression | block_size |
+------+-----------+---------+-------------+----------+---------------
+-----------------+---------------------+------------+
| x | bigint | | true | false | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 2048 |
| s | string | | false | true | |
PREFIX_ENCODING | DEFAULT_COMPRESSION | 0 |
| t | timestamp | | false | true | |
AUTO_ENCODING | ZLIB | 0 |
+------+-----------+---------+-------------+----------+---------------
+-----------------+---------------------+------------+
Kudu tables all use an underlying partitioning mechanism. The partition syntax is different than for non-Kudu tables.
You can use the ALTER TABLE statement to add and drop range partitions from a Kudu table. Any new range
| Impala SQL Language Reference | 222
must not overlap with any existing ranges. Dropping a range removes all the associated rows from the table. See
Partitioning for Kudu Tables on page 714 for details.
Related information:
Overview of Impala Tables on page 198, CREATE TABLE Statement on page 240, DROP TABLE Statement on
page 278, Partitioning for Impala Tables on page 661, Internal Tables on page 198, External Tables on page
199
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Overview of Impala Views on page 200, CREATE VIEW Statement on page 254, DROP VIEW Statement on
page 280
COMMENT Statement
The COMMENT statement adds, changes, or removes a comment about a database, a table, or a column.
You can alternatively use the CREATE and ALTER statements to add comments to the objects.
You can view the comment on a database, a table, or a column using the SHOW or DESCRIBE statement.
Syntax:
Parameters:
• db_name: Specify the database name if not for the current database.
• NULL: If given for the comment, removes the existing comment.
• The comment string can be up to 256 characters long.
Privileges required:
To add a comment, the ALTER privilege on the object is required.
To view a comment, the SELECT, INSERT, or REFRESH on the object is required.
Usage notes:
Added in: Impala 3.1
The PARTITION clause is only allowed in combination with the INCREMENTAL clause. It is optional for COMPUTE
INCREMENTAL STATS, and required for DROP INCREMENTAL STATS. Whenever you specify partitions
through the PARTITION (partition_spec) clause in a COMPUTE INCREMENTAL STATS or DROP
INCREMENTAL STATS statement, you must include all the partitioning columns in the specification, and specify
constant values for all the partition key columns.
Usage notes:
Originally, Impala relied on users to run the Hive ANALYZE TABLE statement, but that method of gathering
statistics proved unreliable and difficult to use. The Impala COMPUTE STATS statement was built to improve the
reliability and user-friendliness of this operation. COMPUTE STATS does not require any setup steps or special
configuration. You only run a single Impala COMPUTE STATS statement to gather both table and column statistics,
rather than separate Hive ANALYZE TABLE statements for each kind of statistics.
For non-incremental COMPUTE STATS statement, the columns for which statistics are computed can be specified
with an optional comma-separate list of columns.
If no column list is given, the COMPUTE STATS statement computes column-level statistics for all columns of the
table. This adds potentially unneeded work for columns whose stats are not needed by queries. It can be especially
costly for very wide tables and unneeded large string fields.
COMPUTE STATS returns an error when a specified column cannot be analyzed, such as when the column does not
exist, the column is of an unsupported type for COMPUTE STATS, e.g. colums of complex types, or the column is a
partitioning column.
If an empty column list is given, no column is analyzed by COMPUTE STATS.
In Impala 2.12 and higher, an optional TABLESAMPLE clause immediately after a table reference specifies that the
COMPUTE STATS operation only processes a specified percentage of the table data. For tables that are so large that
a full COMPUTE STATS operation is impractical, you can use COMPUTE STATS with a TABLESAMPLE clause to
extrapolate statistics from a sample of the table data. See Table and Column Statisticsabout the experimental stats
extrapolation and sampling features.
The COMPUTE INCREMENTAL STATS variation is a shortcut for partitioned tables that works on a subset of
partitions rather than the entire table. The incremental nature makes it suitable for large tables with many partitions,
where a full COMPUTE STATS operation takes too long to be practical each time a partition is added or dropped. See
#unique_304 for full usage details.
Important:
For a particular table, use either COMPUTE STATS or COMPUTE INCREMENTAL STATS, but never combine the
two or alternate between them. If you switch from COMPUTE STATS to COMPUTE INCREMENTAL STATS during
the lifetime of a table, or vice versa, drop all statistics by running DROP STATS before making the switch.
When you run COMPUTE INCREMENTAL STATS on a table for the first time, the statistics are computed again
from scratch regardless of whether the table already has statistics. Therefore, expect a one-time resource-intensive
operation for scanning the entire table when running COMPUTE INCREMENTAL STATS for the first time on a
given table.
In Impala 3.0 and lower, approximately 400 bytes of metadata per column per partition are needed for caching. Tables
with a big number of partitions and many columns can add up to a significant memory overhead as the metadata must
be cached on the catalogd host and on every impalad host that is eligible to be a coordinator. If this metadata for
all tables exceeds 2 GB, you might experience service downtime. In Impala 3.1 and higher, the issue was alleviated
with an improved handling of incremental stats.
| Impala SQL Language Reference | 225
COMPUTE INCREMENTAL STATS only applies to partitioned tables. If you use the INCREMENTAL clause for an
unpartitioned table, Impala automatically uses the original COMPUTE STATS statement. Such tables display false
under the Incremental stats column of the SHOW TABLE STATS output.
Note:
Because many of the most performance-critical and resource-intensive operations rely on table and column statistics
to construct accurate and efficient plans, COMPUTE STATS is an important step at the end of your ETL process. Run
COMPUTE STATS on all tables as your first step during performance tuning for slow queries, or troubleshooting for
out-of-memory conditions:
• Accurate statistics help Impala construct an efficient query plan for join queries, improving performance and
reducing memory usage.
• Accurate statistics help Impala distribute the work effectively for insert operations into Parquet tables, improving
performance and reducing memory usage.
• Accurate statistics help Impala estimate the memory required for each query, which is important when you use
resource management features, such as admission control and the YARN resource management framework. The
statistics help Impala to achieve high concurrency, full utilization of available memory, and avoid contention with
workloads from other Hadoop components.
• In Impala 2.8 and higher, when you run the COMPUTE STATS or COMPUTE INCREMENTAL STATS statement
against a Parquet table, Impala automatically applies the query option setting MT_DOP=4 to increase the amount
of intra-node parallelism during this CPU-intensive operation. See MT_DOP Query Option on page 362 for
details about what this query option does and how to use it with CPU-intensive SELECT statements.
Computing stats for groups of partitions:
In Impala 2.8 and higher, you can run COMPUTE INCREMENTAL STATS on multiple partitions, instead of the
entire table or one partition at a time. You include comparison operators other than = in the PARTITION clause, and
the COMPUTE INCREMENTAL STATS statement applies to all partitions that match the comparison expression.
For example, the INT_PARTITIONS table contains 4 partitions. The following COMPUTE INCREMENTAL
STATS statements affect some but not all partitions, as indicated by the Updated n partition(s) messages.
The partitions that are affected depend on values in the partition key column X that match the comparison expression
in the PARTITION clause.
includes physical measurements such as the number of files, the total size, and size measurements for fixed-length
columns such as with the INT type. Unknown values are represented by -1. After running COMPUTE STATS for
each table, much more information is available through the SHOW STATS statements. If you were running a join
query involving both of these tables, you would need statistics for both tables to get the most effective optimization
for the query.
+--------+--------+------------------+--------+----------+----------+
Returned 2 row(s) in 0.02s
[localhost:21000] > compute stats t2;
Query: compute stats t2
+-----------------------------------------+
| summary |
+-----------------------------------------+
| Updated 1 partition(s) and 2 column(s). |
+-----------------------------------------+
Returned 1 row(s) in 5.70s
[localhost:21000] > show table stats t2;
Query: show table stats t2
+-------+--------+----------+--------+
| #Rows | #Files | Size | Format |
+-------+--------+----------+--------+
| 98304 | 1 | 960.00KB | TEXT |
+-------+--------+----------+--------+
Returned 1 row(s) in 0.03s
[localhost:21000] > show column stats t2;
Query: show column stats t2
+--------+--------+------------------+--------+----------+----------+
| Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
+--------+--------+------------------+--------+----------+----------+
| parent | INT | 3 | -1 | 4 | 4 |
| s | STRING | 6 | -1 | 14 | 9.3 |
+--------+--------+------------------+--------+----------+----------+
Returned 2 row(s) in 0.01s
The following example shows how to use the INCREMENTAL clause, available in Impala 2.1.0 and higher. The
COMPUTE INCREMENTAL STATS syntax lets you collect statistics for newly added or changed partitions, without
rescanning the entire table.
+-------------------------------------------+
| Updated 10 partition(s) and 21 column(s). |
+-------------------------------------------+
show table stats item_partitioned;
+-------------+-------+--------+----------+--------------+---------
+------------------
| i_category | #Rows | #Files | Size | Bytes Cached | Format |
Incremental stats
+-------------+-------+--------+----------+--------------+---------
+------------------
| Books | 1733 | 1 | 223.74KB | NOT CACHED | PARQUET | true
| Children | 1786 | 1 | 230.05KB | NOT CACHED | PARQUET | true
| Electronics | 1812 | 1 | 232.67KB | NOT CACHED | PARQUET | true
| Home | 1807 | 1 | 232.56KB | NOT CACHED | PARQUET | true
| Jewelry | 1740 | 1 | 223.72KB | NOT CACHED | PARQUET | true
| Men | 1811 | 1 | 231.25KB | NOT CACHED | PARQUET | true
| Music | 1860 | 1 | 237.90KB | NOT CACHED | PARQUET | true
| Shoes | 1835 | 1 | 234.90KB | NOT CACHED | PARQUET | true
| Sports | 1783 | 1 | 227.97KB | NOT CACHED | PARQUET | true
| Women | 1790 | 1 | 226.27KB | NOT CACHED | PARQUET | true
| Total | 17957 | 10 | 2.25MB | 0B | |
+-------------+-------+--------+----------+--------------+---------
+------------------
your monitoring and diagnostic displays. The same factors that affect the performance, scalability, and execution of
other queries (such as parallel execution, memory usage, admission control, and timeouts) also apply to the queries
run by the COMPUTE STATS statement.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read permission for all
affected files in the source directory: all files in the case of an unpartitioned table or a partitioned table in the case of
COMPUTE STATS; or all the files in partitions without incremental stats in the case of COMPUTE INCREMENTAL
STATS. It must also have read and execute permissions for all relevant directories holding the data files. (Essentially,
COMPUTE STATS requires the same permissions as the underlying SELECT queries it runs against the table.)
Kudu considerations:
The COMPUTE STATS statement applies to Kudu tables. Impala does not compute the number of rows for each
partition for Kudu tables. Therefore, you do not need to re-run the operation when you see -1 in the # Rows column
of the output from SHOW TABLE STATS. That column always shows -1 for all Kudu tables.
Related information:
DROP STATS Statement on page 274, SHOW TABLE STATS Statement on page 392, SHOW COLUMN STATS
Statement on page 393, Table and Column Statistics on page 601
After creating a database, your impala-shell session or another impala-shell connected to the same node
can immediately access that database. To access the database through the Impala daemon on a different node, issue
the INVALIDATE METADATA statement first while connected to that other node.
| Impala SQL Language Reference | 232
Setting the LOCATION attribute for a new database is a way to work with sets of files in an HDFS directory structure
outside the default Impala data directory, as opposed to setting the LOCATION attribute for each individual table.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Hive considerations:
When you create a database in Impala, the database can also be used by Hive. When you create a database in Hive,
issue an INVALIDATE METADATA statement in Impala to make Impala permanently aware of the new database.
The SHOW DATABASES statement lists all databases, or the databases whose name matches a wildcard pattern.
In Impala 2.5 and higher, the SHOW DATABASES output includes a second column that displays the associated
comment, if any, for each database.
Amazon S3 considerations:
To specify that any tables created within a database reside on the Amazon S3 system, you can include an s3a://
prefix on the LOCATION attribute. In Impala 2.6 and higher, Impala automatically creates any required folders as the
databases, tables, and partitions are created, and removes them when they are dropped.
In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP
DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove
folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala
database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with
the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with Impala.
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have write permission for the
parent HDFS directory under which the database is located.
Examples:
-- Before dropping a database, first drop all the tables inside it,
-- or in Impala 2.3 and higher use the CASCADE clause.
drop database temp;
ERROR: ImpalaRuntimeException: Error making 'dropDatabase' RPC to Hive
Metastore:
CAUSED BY: InvalidOperationException: Database temp is not empty
show tables in temp;
+------+
| name |
+------+
| t3 |
+------+
-- Earlier releases:
drop table temp.t3;
drop database temp;
Related information:
Overview of Impala Databases on page 195, DROP DATABASE Statement on page 270, USE Statement on page
406, SHOW DATABASES on page 386, Overview of Impala Tables on page 198
To create a persistent UDA, which must be written in C++, issue a CREATE AGGREGATE FUNCTION statement:
[INIT_FN='function]
UPDATE_FN='function
MERGE_FN='function
[PREPARE_FN='function]
[CLOSEFN='function]
[SERIALIZE_FN='function]
[FINALIZE_FN='function]
See Variable-Length Argument Lists on page 572 for how to code a C++ UDF to accept variable-length argument
lists.
Scalar and aggregate functions:
The simplest kind of user-defined function returns a single scalar value each time it is called, typically once for
each row in the result set. This general kind of function is what is usually meant by UDF. User-defined aggregate
functions (UDAs) are a specialized kind of UDF that produce a single value based on the contents of multiple rows.
You usually use UDAs in combination with a GROUP BY clause to condense a large result set into a smaller one, or
even a single row summarizing column values across an entire table.
You create UDAs by using the CREATE AGGREGATE FUNCTION syntax. The clauses INIT_FN, UPDATE_FN,
MERGE_FN, SERIALIZE_FN, FINALIZE_FN, and INTERMEDIATE only apply when you create a UDA rather
than a scalar UDF.
The *_FN clauses specify functions to call at different phases of function processing.
• Initialize: The function you specify with the INIT_FN clause does any initial setup, such as initializing member
variables in internal data structures. This function is often a stub for simple UDAs. You can omit this clause and a
default (no-op) function will be used.
• Update: The function you specify with the UPDATE_FN clause is called once for each row in the original result
set, that is, before any GROUP BY clause is applied. A separate instance of the function is called for each different
value returned by the GROUP BY clause. The final argument passed to this function is a pointer, to which you
write an updated value based on its original value and the value of the first argument.
• Merge: The function you specify with the MERGE_FN clause is called an arbitrary number of times, to combine
intermediate values produced by different nodes or different threads as Impala reads and processes data files in
parallel. The final argument passed to this function is a pointer, to which you write an updated value based on its
original value and the value of the first argument.
• Serialize: The function you specify with the SERIALIZE_FN clause frees memory allocated to intermediate
results. It is required if any memory was allocated by the Allocate function in the Init, Update, or Merge functions,
or if the intermediate type contains any pointers. See the UDA code samples for details.
| Impala SQL Language Reference | 235
• Finalize: The function you specify with the FINALIZE_FN clause does any required teardown for resources
acquired by your UDF, such as freeing memory, closing file handles if you explicitly opened any files, and so on.
This function is often a stub for simple UDAs. You can omit this clause and a default (no-op) function will be
used. It is required in UDAs where the final return type is different than the intermediate type. or if any memory
was allocated by the Allocate function in the Init, Update, or Merge functions. See the UDA code samples for
details.
If you use a consistent naming convention for each of the underlying functions, Impala can automatically determine
the names based on the first such clause, so the others are optional.
For end-to-end examples of UDAs, see User-Defined Functions (UDFs) on page 566.
Complex type considerations:
Currently, Impala UDFs cannot accept arguments or return values of the Impala complex types (STRUCT, ARRAY, or
MAP).
Usage notes:
• When authorization is enabled, the CREATE FUNCTION statement requires:
• The CREATE privilege on the database.
• The ALL privilege on URI where URI is the value you specified for the LOCATION in the CREATE
FUNCTION statement.
• You can write Impala UDFs in either C++ or Java. C++ UDFs are new to Impala, and are the recommended
format for high performance utilizing native code. Java-based UDFs are compatible between Impala and Hive, and
are most suited to reusing existing Hive UDFs. (Impala can run Java-based Hive UDFs but not Hive UDAs.)
• Impala 2.5 introduces UDF improvements to persistence for both C++ and Java UDFs, and better compatibility
between Impala and Hive for Java UDFs. See User-Defined Functions (UDFs) on page 566 for details.
• The body of the UDF is represented by a .so or .jar file, which you store in HDFS and the CREATE
FUNCTION statement distributes to each Impala node.
• Impala calls the underlying code during SQL statement evaluation, as many times as needed to process all the
rows from the result set. All UDFs are assumed to be deterministic, that is, to always return the same result when
passed the same argument values. Impala might or might not skip some invocations of a UDF if the result value
is already known from a previous call. Therefore, do not rely on the UDF being called a specific number of times,
and do not return different result values based on some external factor such as the current time, a random number
function, or an external data source that could be updated while an Impala query is in progress.
• The names of the function arguments in the UDF are not significant, only their number, positions, and data types.
• You can overload the same function name by creating multiple versions of the function, each with a different
argument signature. For security reasons, you cannot make a UDF with the same name as any built-in function.
• In the UDF code, you represent the function return result as a struct. This struct contains 2 fields. The first
field is a boolean representing whether the value is NULL or not. (When this field is true, the return value is
interpreted as NULL.) The second field is the same type as the specified function return type, and holds the return
value when the function returns something other than NULL.
• In the UDF code, you represent the function arguments as an initial pointer to a UDF context structure, followed
by references to zero or more structs, corresponding to each of the arguments. Each struct has the same 2
fields as with the return value, a boolean field representing whether the argument is NULL, and a field of the
appropriate type holding any non-NULL argument value.
• For sample code and build instructions for UDFs, see the sample UDFs in the Impala github repo.
• Because the file representing the body of the UDF is stored in HDFS, it is automatically available to all the Impala
nodes. You do not need to manually copy any UDF-related files between servers.
• Because Impala currently does not have any ALTER FUNCTION statement, if you need to rename a function,
move it to a different database, or change its signature or other properties, issue a DROP FUNCTION statement
for the original function followed by a CREATE FUNCTION with the desired properties.
• Because each UDF is associated with a particular database, either issue a USE statement before doing any
CREATE FUNCTION statements, or specify the name of the function as db_name.function_name.
| Impala SQL Language Reference | 236
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Compatibility:
Impala can run UDFs that were created through Hive, as long as they refer to Impala-compatible data types (not
composite or nested column types). Hive can run Java-based UDFs that were created through Impala, but not Impala
UDFs written in C++.
The Hive current_user() function cannot be called from a Java UDF through Impala.
Persistence:
In Impala 2.5 and higher, Impala UDFs and UDAs written in C++ are persisted in the metastore database. Java UDFs
are also persisted, if they were created with the new CREATE FUNCTION syntax for Java UDFs, where the Java
function argument and return types are omitted. Java-based UDFs created with the old CREATE FUNCTION syntax
do not persist across restarts because they are held in the memory of the catalogd daemon. Until you re-create
such Java UDFs using the new CREATE FUNCTION syntax, you must reload those Java-based UDFs by running the
original CREATE FUNCTION statements again each time you restart the catalogd daemon. Prior to Impala 2.5 the
requirement to reload functions after a restart applied to both C++ and Java functions.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Examples:
For additional examples of all kinds of user-defined functions, see User-Defined Functions (UDFs) on page 566.
The following example shows how to take a Java jar file and make all the functions inside one of its classes into
UDFs under a single (overloaded) function name in Impala. Each CREATE FUNCTION or DROP FUNCTION
statement applies to all the overloaded Java functions with the same name. This example uses the signatureless syntax
for CREATE FUNCTION and DROP FUNCTION, which is available in Impala 2.5 and higher.
At the start, the jar file is in the local filesystem. Then it is copied into HDFS, so that it is available for Impala to
reference through the CREATE FUNCTION statement and queries that refer to the Impala function name.
In impala-shell, the CREATE FUNCTION refers to the HDFS path of the jar file and the fully qualified class
name inside the jar. Each of the functions inside the class becomes an Impala function, each one overloaded under the
specified Impala function name.
These are all simple functions that return their single arguments, or sum, concatenate, and so on their multiple
arguments. Impala determines which overloaded function to use based on the number and types of the arguments.
The previous example used the same Impala function name as the name of the class. This example shows how the
Impala function name is independent of the underlying Java class or function names. A second CREATE FUNCTION
statement results in a set of overloaded functions all named my_func, to go along with the overloaded functions all
named testudf.
show functions;
+-------------+---------------------------------------+-------------
+---------------+
| return type | signature | binary type | is
persistent |
+-------------+---------------------------------------+-------------
+---------------+
| Impala SQL Language Reference | 239
The corresponding DROP FUNCTION statement with no signature drops all the overloaded functions with that name.
The signatureless CREATE FUNCTION syntax for Java UDFs ensures that the functions shown in this example
remain available after the Impala service (specifically, the Catalog Server) are restarted.
Related information:
User-Defined Functions (UDFs) on page 566 for more background information, usage instructions, and examples
for Impala UDFs; DROP FUNCTION Statement on page 272
Required privileges:
Only administrative users (those with ALL privileges on the server, defined in the Sentry policy file) can use this
statement.
Compatibility:
Impala makes use of any roles and privileges specified by the GRANT and REVOKE statements in Hive, and Hive
makes use of any roles and privileges specified by the GRANT and REVOKE statements in Impala. The Impala GRANT
| Impala SQL Language Reference | 240
and REVOKE statements for privileges do not require the ROLE keyword to be repeated before each role name, unlike
the equivalent Hive statements.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Impala Authorization on page 77, GRANT Statement (Impala 2.0 or higher only) on page 284, REVOKE
Statement (Impala 2.0 or higher only) on page 305, DROP ROLE Statement (Impala 2.0 or higher only) on page
273, SHOW Statement on page 381
primitive_type:
TINYINT
| SMALLINT
| Impala SQL Language Reference | 241
| INT
| BIGINT
| BOOLEAN
| FLOAT
| DOUBLE
| DECIMAL
| STRING
| CHAR
| VARCHAR
| TIMESTAMP
complex_type:
struct_type
| array_type
| map_type
row_format:
DELIMITED [FIELDS TERMINATED BY 'char' [ESCAPED BY 'char']]
[LINES TERMINATED BY 'char']
file_format:
PARQUET
| TEXTFILE
| AVRO
| SEQUENCEFILE
| RCFILE
ctas_file_format:
PARQUET
| TEXTFILE
Kudu tables:
[, ...]
[PRIMARY KEY (col_name[, ...])]
)
[PARTITION BY kudu_partition_clause]
[COMMENT 'table_comment']
STORED AS KUDU
[TBLPROPERTIES ('key1'='value1', 'key2'='value2', ...)]
PRIMARY KEY
| [NOT] NULL
| ENCODING codec
| COMPRESSION algorithm
| DEFAULT constant
| BLOCK_SIZE number
kudu_partition_clause:
hash_clause ::=
HASH [ (pk_col [, ...]) ]
PARTITIONS n
range_clause ::=
RANGE [ (pk_col [, ...]) ]
(
{
PARTITION constant_expression range_comparison_operator
VALUES range_comparison_operator constant_expression
| PARTITION VALUE = constant_expression_or_tuple
}
[, ...]
)
Depending on the form of the CREATE TABLE statement, the column definitions are required or not allowed.
With the CREATE TABLE AS SELECT and CREATE TABLE LIKE syntax, you do not specify the columns at
all; the column names and types are derived from the source table, query, or data file.
With the basic CREATE TABLE syntax, you must list one or more columns, its name, type, and optionally a
comment, in addition to any columns used as partitioning keys. There is one exception where the column list is not
required: when creating an Avro table with the STORED AS AVRO clause, you can omit the list of columns and
specify the same metadata as part of the TBLPROPERTIES clause.
Complex type considerations:
The Impala complex types (STRUCT, ARRAY, or MAP) are available in Impala 2.3 and higher. Because you can nest
these types (for example, to make an array of maps or a struct with an array field), these types are also sometimes
referred to as nested types. See Complex Types (Impala 2.3 or higher only) on page 134 for usage details.
Impala can create tables containing complex type columns, with any supported file format. Because currently Impala
can only query complex type columns in Parquet tables, creating tables with complex type columns and other file
formats such as text is of limited use. For example, you might create a text table including some columns with
complex types with Impala, and use Hive as part of your to ingest the nested type data and copy it to an identical
Parquet table. Or you might create a partitioned table containing complex type columns using one file format, and use
ALTER TABLE to change the file format of individual partitions to Parquet; Impala can then query only the Parquet-
format partitions in that table.
Partitioned tables can contain complex type columns. All the partition key columns must be scalar types.
Internal and external tables (EXTERNAL and LOCATION clauses):
By default, Impala creates an “internal” table, where Impala manages the underlying data files for the table, and
physically deletes the data files when you drop the table. If you specify the EXTERNAL clause, Impala treats the
table as an “external” table, where the data files are typically produced outside Impala and queried from their original
locations in HDFS, and Impala leaves the data files in place when you drop the table. For details about internal and
external tables, see Overview of Impala Tables on page 198.
Typically, for an external table you include a LOCATION clause to specify the path to the HDFS directory where
Impala reads and writes files for the table. For example, if your data pipeline produces Parquet files in the HDFS
directory /user/etl/destination, you might create an external table as follows:
Although the EXTERNAL and LOCATION clauses are often specified together, LOCATION is optional for external
tables, and you can also specify LOCATION for internal tables. The difference is all about whether Impala “takes
control” of the underlying data files and moves them when you rename the table, or deletes them when you drop the
table. For more about internal and external tables and how they interact with the LOCATION attribute, see Overview
of Impala Tables on page 198.
Partitioned tables (PARTITIONED BY clause):
The PARTITIONED BY clause divides the data files based on the values from one or more specified columns.
Impala queries can use the partition metadata to minimize the amount of data that is read from disk or transmitted
across the network, particularly during join queries. For details about partitioning, see Partitioning for Impala Tables
on page 661.
Note:
All Kudu tables require partitioning, which involves different syntax than non-Kudu tables. See the PARTITION BY
clause, rather than PARTITIONED BY, for Kudu tables.
In Impala 2.10 and higher, the PARTITION BY clause is optional for Kudu tables. If the clause is omitted, Impala
automatically constructs a single partition that is not connected to any column. Because such a table cannot take
advantage of Kudu features for parallelized queries and query optimizations, omitting the PARTITION BY clause is
only appropriate for small lookup tables.
| Impala SQL Language Reference | 244
Prior to Impala 2.5, you could use a partitioned table as the source and copy data from it, but could not specify any
partitioning clauses for the new table. In Impala 2.5 and higher, you can now use the PARTITIONED BY clause
with a CREATE TABLE AS SELECT statement. See the examples under the following discussion of the CREATE
TABLE AS SELECT syntax variation.
Sorted tables (SORT BY clause):
The optional SORT BY clause lets you specify zero or more columns that are sorted in the data files created by each
Impala INSERT or CREATE TABLE AS SELECT operation. Creating data files that are sorted is most useful
for Parquet tables, where the metadata stored inside each file includes the minimum and maximum values for each
column in the file. (The statistics apply to each row group within the file; for simplicity, Impala writes a single row
group in each file.) Grouping data values together in relatively narrow ranges within each data file makes it possible
for Impala to quickly skip over data files that do not contain value ranges indicated in the WHERE clause of a query,
and can improve the effectiveness of Parquet encoding and compression.
This clause is not applicable for Kudu tables or HBase tables. Although it works for other HDFS file formats besides
Parquet, the more efficient layout is most evident with Parquet tables, because each Parquet data file includes
statistics about the data values in that file.
The SORT BY columns cannot include any partition key columns for a partitioned table, because those column values
are not represented in the underlying data files.
Because data files can arrive in Impala tables by mechanisms that do not respect the SORT BY clause, such as LOAD
DATA or ETL tools that create HDFS files, Impala does not guarantee or rely on the data being sorted. The sorting
aspect is only used to create a more efficient layout for Parquet files generated by Impala, which helps to optimize
the processing of those Parquet files during Impala queries. During an INSERT or CREATE TABLE AS SELECT
operation, the sorting occurs when the SORT BY clause applies to the destination table for the data, regardless of
whether the source table has a SORT BY clause.
For example, when creating a table intended to contain census data, you might define sort columns such as last name
and state. If a data file in this table contains a narrow range of last names, for example from Smith to Smythe,
Impala can quickly detect that this data file contains no matches for a WHERE clause such as WHERE last_name =
'Jones' and avoid reading the entire file.
Likewise, if an existing table contains data without any sort order, you can reorganize the data in a more efficient way
by using INSERT or CREATE TABLE AS SELECT to copy that data into a new table with a SORT BY clause:
The metadata for the SORT BY clause is stored in the TBLPROPERTIES fields for the table. Other SQL engines that
can interoperate with Impala tables, such as Hive and Spark SQL, do not recognize this property when inserting into a
table that has a SORT BY clause.
Transactional tables:
In the version 3.3 and higher, when integrated with Hive 3, Impala can create, read, and insert into transactional
tables.
| Impala SQL Language Reference | 245
To create a table that supports transactions, use the TBLPROPERTIES clause and set the 'transactional' and
'transactional_properties' as below. Currently, Impala only supports insert-only transactional tables.
TBLPROPERTIES('transactional'='true',
'transactional_properties'='insert_only')
When integrated with Hive3 and the DEFAULT_TRANSACTIONAL_TYPE query option is set to INSERT_ONLY,
tables are created as insert-only transactional table by default.
Transactional tables are not supported for Kudu and HBase.
Kudu considerations:
Because Kudu tables do not support clauses related to HDFS and S3 data files and partitioning mechanisms, the
syntax associated with the STORED AS KUDU clause is shown separately in the above syntax descriptions. Kudu
tables have their own syntax for CREATE TABLE, CREATE EXTERNAL TABLE, and CREATE TABLE AS
SELECT. Prior to Impala 2.10, all internal Kudu tables require a PARTITION BY clause, different than the
PARTITIONED BY clause for HDFS-backed tables.
Here are some examples of creating empty Kudu tables:
-- The CTAS statement defines the primary key and partitioning scheme.
-- The rest of the column definitions are derived from the select list.
CREATE TABLE ctas_t1
PRIMARY KEY (id) PARTITION BY HASH (id) PARTITIONS 10
STORED AS KUDU
AS SELECT id, s FROM kudu_t1;
The following CREATE TABLE clauses are not supported for Kudu tables:
• PARTITIONED BY (Kudu tables use the clause PARTITION BY instead)
• LOCATION
• ROWFORMAT
• CACHED IN | UNCACHED
• WITH SERDEPROPERTIES
For more on the PRIMARY KEY clause, see Primary Key Columns for Kudu Tables on page 709 and PRIMARY
KEY Attribute on page 710.
For more on creating a Kudu table with a specific replication factor, see Kudu Replication Factor on page 709.
For more on the NULL and NOT NULL attributes, see NULL | NOT NULL Attribute on page 711.
For more on the ENCODING attribute, see ENCODING Attribute on page 712.
For more on the COMPRESSION attribute, see COMPRESSION Attribute on page 713.
For more on the DEFAULT attribute, see DEFAULT Attribute on page 712.
For more on the BLOCK_SIZE attribute, see BLOCK_SIZE Attribute on page 714.
Partitioning for Kudu tables (PARTITION BY clause)
For Kudu tables, you specify logical partitioning across one or more columns using the PARTITION BY clause. In
contrast to partitioning for HDFS-based tables, multiple values for a partition key column can be located in the same
partition. The optional HASH clause lets you divide one or a set of partition key columns into a specified number of
buckets. You can use more than one HASH clause, specifying a distinct set of partition key columns for each. The
optional RANGE clause further subdivides the partitions, based on a set of comparison operations for the partition key
columns.
Here are some examples of the PARTITION BY HASH syntax:
-- When the column list is omitted, apply hash function to all primary key
columns.
create table hash_t4 (x bigint, y bigint, s string, primary key (x,y))
partition by hash partitions 10
stored as kudu;
| Impala SQL Language Reference | 247
-- A range can also specify a single specific value, using the keyword VALUE
-- with an = comparison.
create table range_t3 (x bigint, s string, s2 string, primary key (x, s))
partition by range (s)
(
partition value = 'Yes', partition value = 'No', partition value =
'Maybe'
)
stored as kudu;
-- Using multiple columns in the RANGE clause and tuples inside the
partition spec
-- only works for partitions specified with the VALUE= syntax.
create table range_t4 (x bigint, s string, s2 string, primary key (x, s))
partition by range (x,s)
(
partition value = (0,'zero'), partition value = (1,'one'), partition
value = (2,'two')
)
stored as kudu;
Here are some examples combining both HASH and RANGE syntax for the PARTITION BY clause:
-- Values from each range partition are hashed into 10 associated buckets.
-- Total number of partitions in this case is 10 x 2.
create table combined_t1 (x bigint, s string, s2 string, primary key (x, s))
partition by hash (x) partitions 10, range (x)
(
partition 0 <= values <= 49, partition 50 <= values <= 100
)
| Impala SQL Language Reference | 248
stored as kudu;
For more usage details and examples of the Kudu partitioning syntax, see Using Impala to Query Kudu Tables on
page 708.
Specifying file format (STORED AS and ROW FORMAT clauses):
The STORED AS clause identifies the format of the underlying data files. Currently, Impala can query more types
of file formats than it can create or insert into. Use Hive to perform any create or data load operations that are not
currently available in Impala. For example, Impala can create an Avro, SequenceFile, or RCFile table but cannot
insert data into it. There are also Impala-specific procedures for using compression with each kind of file format. For
details about working with data files of various formats, see How Impala Works with Hadoop File Formats on page
671.
Note: In Impala 1.4.0 and higher, Impala can create Avro tables, which formerly required doing the CREATE
TABLE statement in Hive. See Using the Avro File Format with Impala Tables on page 697 for details and
examples.
By default (when no STORED AS clause is specified), data files in Impala tables are created as text files with Ctrl-
A (hex 01) characters as the delimiter. Specify the ROW FORMAT DELIMITED clause to produce or ingest data
files that use a different delimiter character such as tab or |, or a different line end character such as carriage return
or newline. When specifying delimiter and line end characters with the FIELDS TERMINATED BY and LINES
TERMINATED BY clauses, use '\t' for tab, '\n' for newline or linefeed, '\r' for carriage return, and \0 for
ASCII nul (hex 00). For more examples of text tables, see Using Text Data Files with Impala Tables on page 673.
The ESCAPED BY clause applies both to text files that you create through an INSERT statement to an Impala
TEXTFILE table, and to existing data files that you put into an Impala table directory. (You can ingest existing data
files either by creating the table with CREATE EXTERNAL TABLE ... LOCATION, the LOAD DATA statement,
or through an HDFS operation such as hdfs dfs -put file hdfs_path.) Choose an escape character that
is not used anywhere else in the file, and put it in front of each instance of the delimiter character that occurs within
a field value. Surrounding field values with quotation marks does not help Impala to parse fields with embedded
delimiter characters; the quotation marks are considered to be part of the column value. If you want to use \ as the
escape character, specify the clause in impala-shell as ESCAPED BY '\\'.
Note: The CREATE TABLE clauses FIELDS TERMINATED BY, ESCAPED BY, and LINES TERMINATED BY
have special rules for the string literal used for their argument, because they all require a single character. You can use
a regular character surrounded by single or double quotation marks, an octal sequence such as '\054' (representing
a comma), or an integer in the range '-127'..'128' (with quotation marks but no backslash), which is interpreted as a
single-byte ASCII character. Negative values are subtracted from 256; for example, FIELDS TERMINATED BY
'-2' sets the field delimiter to ASCII code 254, the “Icelandic Thorn” character used as a delimiter by some data
formats.
Cloning tables (LIKE clause):
To create an empty table with the same columns, comments, and other attributes as another table, use the following
variation. The CREATE TABLE ... LIKE form allows a restricted set of clauses, currently only the LOCATION,
COMMENT, and STORED AS clauses.
[COMMENT 'table_comment']
[STORED AS file_format]
[LOCATION 'hdfs_path']
Note:
To clone the structure of a table and transfer data into it in a single operation, use the CREATE TABLE AS SELECT
syntax described in the next subsection.
When you clone the structure of an existing table using the CREATE TABLE ... LIKE syntax, the new table
keeps the same file format as the original one, so you only need to specify the STORED AS clause if you want to use
a different file format, or when specifying a view as the original table. (Creating a table “like” a view produces a text
table by default.)
Although normally Impala cannot create an HBase table directly, Impala can clone the structure of an existing HBase
table with the CREATE TABLE ... LIKE syntax, preserving the file format and metadata from the original table.
There are some exceptions to the ability to use CREATE TABLE ... LIKE with an Avro table. For example, you
cannot use this technique for an Avro table that is specified with an Avro schema but no columns. When in doubt,
check if a CREATE TABLE ... LIKE operation works in Hive; if not, it typically will not work in Impala either.
If the original table is partitioned, the new table inherits the same partition key columns. Because the new table is
initially empty, it does not inherit the actual partitions that exist in the original one. To create partitions in the new
table, insert data or issue ALTER TABLE ... ADD PARTITION statements.
Prior to Impala 1.4.0, it was not possible to use the CREATE TABLE LIKE view_name syntax. In Impala 1.4.0
and higher, you can create a table with the same column definitions as a view using the CREATE TABLE LIKE
technique. Although CREATE TABLE LIKE normally inherits the file format of the original table, a view has no
underlying file format, so CREATE TABLE LIKE view_name produces a text table by default. To specify a
different file format, include a STORED AS file_format clause at the end of the CREATE TABLE LIKE
statement.
Because CREATE TABLE ... LIKE only manipulates table metadata, not the physical data of the table, issue
INSERT INTO TABLE statements afterward to copy any data from the original table into the new one, optionally
converting the data to a new file format. (For some file formats, Impala can do a CREATE TABLE ... LIKE to
create the table, but Impala cannot insert data in that file format; in these cases, you must load the data in Hive. See
How Impala Works with Hadoop File Formats on page 671 for details.)
CREATE TABLE AS SELECT:
The CREATE TABLE AS SELECT syntax is a shorthand notation to create a table based on column definitions
from another table, and copy data from the source table to the destination table without issuing any separate INSERT
statement. This idiom is so popular that it has its own acronym, “CTAS”.
The following examples show how to copy data from a source table T1 to a variety of destinations tables, applying
various transformations to the table properties, table layout, or the data itself as part of the operation:
-- Clone all the columns and data from one table to another.
CREATE TABLE clone_of_t1 AS SELECT * FROM t1;
+-------------------+
| summary |
+-------------------+
| Inserted 3 row(s) |
+-------------------+
-- Clone the columns and data, and convert the data to a different file
format.
CREATE TABLE parquet_version_of_t1 STORED AS PARQUET AS SELECT * FROM t1;
| Impala SQL Language Reference | 250
+-------------------+
| summary |
+-------------------+
| Inserted 3 row(s) |
+-------------------+
-- Same idea as CREATE TABLE LIKE: clone table layout but do not copy any
data.
CREATE TABLE empty_clone_of_t1 AS SELECT * FROM t1 WHERE 1=0;
+-------------------+
| summary |
+-------------------+
| Inserted 0 row(s) |
+-------------------+
See SELECT Statement on page 306 for details about query syntax for the SELECT portion of a CREATE TABLE
AS SELECT statement.
The newly created table inherits the column names that you select from the original table, which you can override by
specifying column aliases in the query. Any column or table comments from the original table are not carried over to
the new table.
Note: When using the STORED AS clause with a CREATE TABLE AS SELECT statement, the destination table
must be a file format that Impala can write to: currently, text or Parquet. You cannot specify an Avro, SequenceFile,
or RCFile table as the destination table for a CTAS operation.
Prior to Impala 2.5 you could use a partitioned table as the source and copy data from it, but could not specify any
partitioning clauses for the new table. In Impala 2.5 and higher, you can now use the PARTITIONED BY clause with
a CREATE TABLE AS SELECT statement. The following example demonstrates how you can copy data from an
unpartitioned table in a CREATE TABLE AS SELECT operation, creating a new partitioned table in the process.
The main syntax consideration is the column order in the PARTITIONED BY clause and the select list: the partition
key columns must be listed last in the select list, in the same order as in the PARTITIONED BY clause. Therefore, in
this case, the column order in the destination table is different from the source table. You also only specify the column
names in the PARTITIONED BY clause, not the data types or column comments.
-- Create new table with partitions based on column values from source
table.
create table partitions_yes partitioned by (year, month)
as select s, year, month from partitions_no;
+-------------------+
| summary |
+-------------------+
| Inserted 3 row(s) |
+-------------------+
The most convenient layout for partitioned tables is with all the partition key columns at the end. The CTAS
PARTITIONED BY syntax requires that column order in the select list, resulting in that same column order in the
destination table.
describe partitions_no;
+-------+----------+---------+
| name | type | comment |
+-------+----------+---------+
| year | smallint | |
| month | tinyint | |
| s | string | |
+-------+----------+---------+
-- The CTAS operation forced us to put the partition key columns last.
-- Having those columns last works better with idioms such as SELECT *
-- for partitioned tables.
describe partitions_yes;
+-------+----------+---------+
| name | type | comment |
+-------+----------+---------+
| s | string | |
| year | smallint | |
| month | tinyint | |
+-------+----------+---------+
Attempting to use a select list with the partition key columns not at the end results in an error due to a column name
mismatch:
For example, the following statements show how you can clone all the data in a table, or a subset of the columns and/
or rows, or reorder columns, rename them, or construct them out of expressions:
As part of a CTAS operation, you can convert the data to any file format that Impala can write (currently, TEXTFILE
and PARQUET). You cannot specify the lower-level properties of a text table, such as the delimiter.
Sorting considerations: Although you can specify an ORDER BY clause in an INSERT ... SELECT statement,
any ORDER BY clause is ignored and the results are not necessarily sorted. An INSERT ... SELECT operation
potentially creates many different data files, prepared by different executor Impala daemons, and therefore the notion
of the data being stored in sorted order is impractical.
CREATE TABLE LIKE PARQUET:
The variation CREATE TABLE ... LIKE PARQUET 'hdfs_path_of_parquet_file' lets you skip
the column definitions of the CREATE TABLE statement. The column names and data types are automatically
configured based on the organization of the specified Parquet data file, which must already reside in HDFS. You can
use a data file located outside the Impala database directories, or a file from an existing Impala Parquet table; either
way, Impala only uses the column definitions from the file and does not use the HDFS location for the LOCATION
attribute of the new table. (Although you can also specify the enclosing directory with the LOCATION attribute, to
both use the same schema as the data file and point the Impala table at the associated directory for querying.)
The following considerations apply when you use the CREATE TABLE LIKE PARQUET technique:
• Any column comments from the original table are not preserved in the new table. Each column in the new table
has a comment stating the low-level Parquet field type used to deduce the appropriate SQL column type.
• If you use a data file from a partitioned Impala table, any partition key columns from the original table are left
out of the new table, because they are represented in HDFS directory names rather than stored in the data file.
To preserve the partition information, repeat the same PARTITION clause as in the original CREATE TABLE
statement.
• The file format of the new table defaults to text, as with other kinds of CREATE TABLE statements. To make the
new table also use Parquet format, include the clause STORED AS PARQUET in the CREATE TABLE LIKE
PARQUET statement.
• If the Parquet data file comes from an existing Impala table, currently, any TINYINT or SMALLINT columns are
turned into INT columns in the new table. Internally, Parquet stores such values as 32-bit integers.
• When the destination table uses the Parquet file format, the CREATE TABLE AS SELECT and INSERT ...
SELECT statements always create at least one data file, even if the SELECT part of the statement does not match
any rows. You can use such an empty Parquet data file as a template for subsequent CREATE TABLE LIKE
PARQUET statements.
For more details about creating Parquet tables, and examples of the CREATE TABLE LIKE PARQUET syntax, see
Using the Parquet File Format with Impala Tables on page 681.
Visibility and Metadata (TBLPROPERTIES and WITH SERDEPROPERTIES clauses):
You can associate arbitrary items of metadata with a table by specifying the TBLPROPERTIES clause. This clause
takes a comma-separated list of key-value pairs and stores those items in the metastore database. You can also
change the table properties later with an ALTER TABLE statement. You can observe the table properties for different
delimiter and escape characters using the DESCRIBE FORMATTED command, and change those settings for an
existing table with ALTER TABLE ... SET TBLPROPERTIES.
You can also associate SerDes properties with the table by specifying key-value pairs through the WITH
SERDEPROPERTIES clause. This metadata is not used by Impala, which has its own built-in serializer and
deserializer for the file formats it supports. Particular property values might be needed for Hive compatibility with
certain variations of file formats, particularly Avro.
Some DDL operations that interact with other Hadoop components require specifying particular values in the
SERDEPROPERTIES or TBLPROPERTIES fields, such as creating an Avro table or an HBase table. (You typically
create HBase tables in Hive, because they require additional clauses not currently available in Impala.)
| Impala SQL Language Reference | 253
To see the column definitions and column comments for an existing table, for example before issuing a CREATE
TABLE ... LIKE or a CREATE TABLE ... AS SELECT statement, issue the statement DESCRIBE
table_name. To see even more detail, such as the location of data files and the values for clauses such as
ROW FORMAT and STORED AS, issue the statement DESCRIBE FORMATTED table_name. DESCRIBE
FORMATTED is also needed to see any overall table comment (as opposed to individual column comments).
After creating a table, your impala-shell session or another impala-shell connected to the same node can
immediately query that table. There might be a brief interval (one statestore heartbeat) before the table can be queried
through a different Impala node. To make the CREATE TABLE statement return only when the table is recognized by
all Impala nodes in the cluster, enable the SYNC_DDL query option.
HDFS caching (CACHED IN clause):
If you specify the CACHED IN clause, any existing or future data files in the table directory or the partition
subdirectories are designated to be loaded into memory with the HDFS caching mechanism. See Using HDFS
Caching with Impala (Impala 2.1 or higher only) on page 622 for details about using the HDFS caching feature.
In Impala 2.2 and higher, the optional WITH REPLICATION clause for CREATE TABLE and ALTER TABLE lets
you specify a replication factor, the number of hosts on which to cache the same data blocks. When Impala processes
a cached data block, where the cache replication factor is greater than 1, Impala randomly selects a host that has a
cached copy of that data block. This optimization avoids excessive CPU usage on a single host when the same cached
data block is processed multiple times. Where practical, specify a value greater than or equal to the HDFS block
replication factor.
Column order:
If you intend to use the table to hold data files produced by some external source, specify the columns in the same
order as they appear in the data files.
If you intend to insert or copy data into the table through Impala, or if you have control over the way externally
produced data files are arranged, use your judgment to specify columns in the most convenient order:
• If certain columns are often NULL, specify those columns last. You might produce data files that omit these
trailing columns entirely. Impala automatically fills in the NULL values if so.
• If an unpartitioned table will be used as the source for an INSERT ... SELECT operation into a partitioned
table, specify last in the unpartitioned table any columns that correspond to partition key columns in the
partitioned table, and in the same order as the partition key columns are declared in the partitioned table. This
technique lets you use INSERT ... SELECT * when copying data to the partitioned table, rather than
specifying each column name individually.
• If you specify columns in an order that you later discover is suboptimal, you can sometimes work around the
problem without recreating the table. You can create a view that selects columns from the original table in a
permuted order, then do a SELECT * from the view. When inserting data into a table, you can specify a permuted
order for the inserted columns to match the order in the destination table.
Hive considerations:
Impala queries can make use of metadata about the table and columns, such as the number of rows in a table or the
number of different values in a column. Prior to Impala 1.2.2, to create this metadata, you issued the ANALYZE
TABLE statement in Hive to gather this information, after creating the table and loading representative data into it. In
Impala 1.2.2 and higher, the COMPUTE STATS statement produces these statistics within Impala, without needing to
use Hive at all.
HBase considerations:
Note:
The Impala CREATE TABLE statement cannot create an HBase table, because it currently does not support the
STORED BY clause needed for HBase tables. Create such tables in Hive, then query them through Impala. For
information on using Impala with HBase tables, see Using Impala to Query HBase Tables on page 722.
Amazon S3 considerations:
| Impala SQL Language Reference | 254
To create a table where the data resides in the Amazon Simple Storage Service (S3), specify a s3a:// prefix
LOCATION attribute pointing to the data files in S3.
In Impala 2.6 and higher, you can use this special LOCATION syntax as part of a CREATE TABLE AS SELECT
statement.
In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP
DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove
folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala
database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with
the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with Impala.
Sorting considerations: Although you can specify an ORDER BY clause in an INSERT ... SELECT statement,
any ORDER BY clause is ignored and the results are not necessarily sorted. An INSERT ... SELECT operation
potentially creates many different data files, prepared by different executor Impala daemons, and therefore the notion
of the data being stored in sorted order is impractical.
HDFS considerations:
The CREATE TABLE statement for an internal table creates a directory in HDFS. The CREATE EXTERNAL
TABLE statement associates the table with an existing HDFS directory, and does not create any new directory in
HDFS. To locate the HDFS data directory for a table, issue a DESCRIBE FORMATTED table statement. To
examine the contents of that HDFS directory, use an OS command such as hdfs dfs -ls hdfs://path, either
from the OS command line or through the shell or ! commands in impala-shell.
The CREATE TABLE AS SELECT syntax creates data files under the table data directory to hold any data copied
by the INSERT portion of the statement. (Even if no data is copied, Impala might create one or more empty data
files.)
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have both execute and write
permission for the database directory where the table is being created.
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Cancellation: Certain multi-stage statements (CREATE TABLE AS SELECT and COMPUTE STATS) can be
cancelled during some stages, when running INSERT or SELECT operations internally. To cancel this statement, use
Ctrl-C from the impala-shell interpreter, the Cancel button from the Watch page in Hue, or Cancel from the list
of in-flight queries (for a particular node) on the Queries tab in the Impala web UI (port 25000).
Related information:
Overview of Impala Tables on page 198, ALTER TABLE Statement on page 207, DROP TABLE Statement on
page 278, Partitioning for Impala Tables on page 661, Internal Tables on page 198, External Tables on page
199, COMPUTE STATS Statement on page 223, SYNC_DDL Query Option on page 379, SHOW TABLES
Statement on page 387, SHOW CREATE TABLE Statement on page 389, DESCRIBE Statement on page 258
The more complicated and hard-to-read the original query, the more benefit there is to simplifying the query using
a view.
• To hide the underlying table and column names, to minimize maintenance problems if those names change. In
that case, you re-create the view using the new names, and all queries that use the view rather than the underlying
tables keep running with no changes.
• To experiment with optimization techniques and make the optimized queries available to all applications. For
example, if you find a combination of WHERE conditions, join order, join hints, and so on that works the best for a
class of queries, you can establish a view that incorporates the best-performing techniques. Applications can then
make relatively simple queries against the view, without repeating the complicated and optimized logic over and
over. If you later find a better way to optimize the original query, when you re-create the view, all the applications
immediately take advantage of the optimized base query.
• To simplify a whole class of related queries, especially complicated queries involving joins between multiple
tables, complicated expressions in the column list, and other SQL syntax that makes the query difficult to
understand and debug. For example, you might create a view that joins several tables, filters using several WHERE
conditions, and selects several columns from the result set. Applications might issue queries against this view that
only vary in their LIMIT, ORDER BY, and similar simple clauses.
For queries that require repeating complicated clauses over and over again, for example in the select list, ORDER BY,
and GROUP BY clauses, you can use the WITH clause as an alternative to creating a view.
You can optionally specify the table-level and the column-level comments as in the CREATE TABLE statement.
Complex type considerations:
For tables containing complex type columns (ARRAY, STRUCT, or MAP), you typically use join queries to refer to the
complex values. You can use views to hide the join notation, making such tables seem like traditional denormalized
tables, and making those tables queryable by business intelligence tools that do not have built-in support for those
complex types. See Accessing Complex Type Data in Flattened Form Using Views on page 155 for details.
Because you cannot directly issue SELECT col_name against a column of complex type, you cannot use a view or
a WITH clause to “rename” a column by selecting it with a column alias.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
| Impala SQL Language Reference | 256
Examples:
-- Create a view that includes only certain columns from the underlying
table.
CREATE VIEW v2 AS SELECT c1, c3, c7 FROM t1;
-- Create a view that filters the values from the underlying table.
CREATE VIEW v3 AS SELECT DISTINCT c1, c3, c7 FROM t1 WHERE c1 IS NOT NULL
AND c5 > 0;
-- Create a view that that reorders and renames columns from the underlying
table.
CREATE VIEW v4 AS SELECT c4 AS last_name, c6 AS address, c2 AS birth_date
FROM t1;
Related information:
Overview of Impala Views on page 200, ALTER VIEW Statement on page 222, DROP VIEW Statement on page
280
The first form evaluates rows from one table against an optional WHERE clause, and deletes all the rows that match
the WHERE conditions, or all rows if WHERE is omitted.
The second form evaluates one or more join clauses, and deletes all matching rows from one of the tables. The join
clauses can include non-Kudu tables, but the table from which the rows are deleted must be a Kudu table. The FROM
keyword is required in this case, to separate the name of the table whose rows are being deleted from the table names
of the join clauses.
Usage notes:
The conditions in the WHERE clause are the same ones allowed for the SELECT statement. See SELECT Statement on
page 306 for details.
The conditions in the WHERE clause can refer to any combination of primary key columns or other columns.
Referring to primary key columns in the WHERE clause is more efficient than referring to non-primary key columns.
| Impala SQL Language Reference | 257
If the WHERE clause is omitted, all rows are removed from the table.
Because Kudu currently does not enforce strong consistency during concurrent DML operations, be aware that the
results after this statement finishes might be different than you intuitively expect:
• If some rows cannot be deleted because their some primary key columns are not found, due to their being deleted
by a concurrent DELETE operation, the statement succeeds but returns a warning.
• A DELETE statement might also overlap with INSERT, UPDATE, or UPSERT statements running concurrently on
the same table. After the statement finishes, there might be more or fewer rows than expected in the table because
it is undefined whether the DELETE applies to rows that are inserted or updated while the DELETE is in progress.
The number of affected rows is reported in an impala-shell message and in the query profile.
Statement type: DML
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
The following examples show how to delete rows from a specified table, either all rows or rows that match a WHERE
clause:
-- Does not delete any rows, because the WHERE condition is always false.
DELETE FROM kudu_table WHERE 1 = 0;
The following examples show how to delete rows that are part of the result set from a join:
-- The tables can be joined in any order as long as the Kudu table
-- is specified as the deletion target.
DELETE t2 FROM non_kudu_table t1 JOIN kudu_table t2 ON t1.x = t2.x;
Related information:
Using Impala to Query Kudu Tables on page 708, INSERT Statement on page 288, UPDATE Statement (Impala
2.8 or higher only) on page 404, UPSERT Statement (Impala 2.8 or higher only) on page 405
DESCRIBE Statement
The DESCRIBE statement displays metadata about a table, such as the column names and their data types. In Impala
2.3 and higher, you can specify the name of a complex type column, which takes the form of a dotted path. The path
might include multiple components in the case of a nested type definition. In Impala 2.5 and higher, the DESCRIBE
DATABASE form can display information about a database.
Syntax:
object_name ::=
[db_name.]table_name[.complex_col_name ...]
| db_name
You can use the abbreviation DESC for the DESCRIBE statement.
The DESCRIBE FORMATTED variation displays additional information, in a format familiar to users of Apache
Hive. The extra information includes low-level details such as whether the table is internal or external, when it was
created, the file format, the location of the data in HDFS, whether the object is a table or a view, and (for views) the
text of the query from the view definition.
Note: The Compressed field is not a reliable indicator of whether the table contains compressed data. It typically
always shows No, because the compression settings only apply during the session that loads data and are not stored
persistently with the table metadata.
Describing databases:
By default, the DESCRIBE output for a database includes the location and the comment, which can be set by the
LOCATION and COMMENT clauses on the CREATE DATABASE statement.
The additional information displayed by the FORMATTED or EXTENDED keyword includes the HDFS user ID that
is considered the owner of the database, and any optional database properties. The properties could be specified by
the WITH DBPROPERTIES clause if the database is created using a Hive CREATE DATABASE statement. Impala
currently does not set or do any special processing based on those properties.
The following examples show the variations in syntax and output for describing databases. This feature is available in
Impala 2.5 and higher.
+---------+----------------------+-----------------------+
Describing tables:
If the DATABASE keyword is omitted, the default for the DESCRIBE statement is to refer to a table.
If you have the SELECT privilege on a subset of the table columns and no other relevant table/database/server-level
privileges, DESCRIBE returns the data from the columns you have access to.
If you have the SELECT privilege on a subset of the table columns and no other relevant table/database/server-level
privileges, DESCRIBE FORMATTED/EXTENDED does not return the LOCATION field. The LOCATION data is
shown if you have any privilege on the table, the containing database or the server.
describe t1;
+------+-----------------+---------+
| name | type | comment |
+------+-----------------+---------+
| x | int | |
| a | array<int> | |
| s | struct< | |
| | f1:string, | |
| | f2:bigint | |
| | > | |
| m | map<string,int> | |
+------+-----------------+---------+
Here are examples showing how to “drill down” into the layouts of complex types, including using multi-part names
to examine the definitions of nested types. The < > delimiters identify the columns with complex types; these are
the columns where you can descend another level to see the parts that make up the complex type. This technique
helps you to understand the multi-part names you use as table references in queries involving complex types, and the
corresponding column names you refer to in the SELECT list. These tables are from the “nested TPC-H” schema,
shown in detail in Sample Schema and Data for Experimenting with Impala Complex Types on page 157.
The REGION table contains an ARRAY of STRUCT elements:
• The first DESCRIBE specifies the table name, to display the definition of each top-level column.
• The second DESCRIBE specifies the name of a complex column, REGION.R_NATIONS, showing that when you
include the name of an ARRAY column in a FROM clause, that table reference acts like a two-column table with
columns ITEM and POS.
• The final DESCRIBE specifies the fully qualified name of the ITEM field, to display the layout of its underlying
STRUCT type in table format, with the fields mapped to column names.
+-------------+----------+---------+
| name | type | comment |
+-------------+----------+---------+
| n_nationkey | smallint | |
| n_name | string | |
| n_comment | string | |
+-------------+----------+---------+
The CUSTOMER table contains an ARRAY of STRUCT elements, where one field in the STRUCT is another ARRAY of
STRUCT elements:
• Again, the initial DESCRIBE specifies only the table name.
• The second DESCRIBE specifies the qualified name of the complex column, CUSTOMER.C_ORDERS, showing
how an ARRAY is represented as a two-column table with columns ITEM and POS.
• The third DESCRIBE specifies the qualified name of the ITEM of the ARRAY column, to see the structure of the
nested ARRAY. Again, it has has two parts, ITEM and POS. Because the ARRAY contains a STRUCT, the layout of
the STRUCT is shown.
• The fourth and fifth DESCRIBE statements drill down into a STRUCT field that is itself a complex type, an
ARRAY of STRUCT. The ITEM portion of the qualified name is only required when the ARRAY elements
are anonymous. The fields of the STRUCT give names to any other complex types nested inside the
STRUCT. Therefore, the DESCRIBE parameters CUSTOMER.C_ORDERS.ITEM.O_LINEITEMS and
CUSTOMER.C_ORDERS.O_LINEITEMS are equivalent. (For brevity, leave out the ITEM portion of a qualified
name when it is not required.)
• The final DESCRIBE shows the layout of the deeply nested STRUCT type. Because there are no more complex
types nested inside this STRUCT, this is as far as you can drill down into the layout for this table.
+--------------+------------------------------------+
-- #4: The ARRAY nested inside the STRUCT elements of the first ARRAY.
describe customer.c_orders.item.o_lineitems;
+------+----------------------------------+
| name | type |
+------+----------------------------------+
| item | struct< |
| | l_partkey:bigint, |
| | l_suppkey:bigint, |
... more struct fields ...
| | l_comment:string |
| | > |
| pos | bigint |
+------+----------------------------------+
-- #5: Shorter form of the previous DESCRIBE. Omits the .ITEM portion of the
name
-- because O_LINEITEMS and other field names provide a way to refer to
things
-- inside the ARRAY element.
describe customer.c_orders.o_lineitems;
+------+----------------------------------+
| Impala SQL Language Reference | 264
| name | type |
+------+----------------------------------+
| item | struct< |
| | l_partkey:bigint, |
| | l_suppkey:bigint, |
... more struct fields ...
| | l_comment:string |
| | > |
| pos | bigint |
+------+----------------------------------+
Usage notes:
After the impalad daemons are restarted, the first query against a table can take longer than subsequent queries,
because the metadata for the table is loaded before the query is processed. This one-time delay for each table can
cause misleading results in benchmark tests or cause unnecessary concern. To “warm up” the Impala metadata cache,
you can issue a DESCRIBE statement in advance for each table you intend to access later.
When you are dealing with data files stored in HDFS, sometimes it is important to know details such as the path
of the data files for an Impala table, and the hostname for the namenode. You can get this information from the
DESCRIBE FORMATTED output. You specify HDFS URIs or path specifications with statements such as LOAD
DATA and the LOCATION clause of CREATE TABLE or ALTER TABLE. You might also use HDFS URIs or paths
with Linux commands such as hadoop and hdfs to copy, rename, and so on, data files in HDFS.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Each table can also have associated table statistics and column statistics. To see these categories of information, use
the SHOW TABLE STATS table_name and SHOW COLUMN STATS table_name statements. See SHOW
Statement on page 381 for details.
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
The following example shows the results of both a standard DESCRIBE and DESCRIBE FORMATTED for different
kinds of schema objects:
• DESCRIBE for a table or a view returns the name, type, and comment for each of the columns. For a view, if the
column value is computed by an expression, the column name is automatically generated as _c0, _c1, and so on
depending on the ordinal number of the column.
| Impala SQL Language Reference | 265
• A table created with no special format or storage clauses is designated as a MANAGED_TABLE (an “internal table”
in Impala terminology). Its data files are stored in an HDFS directory under the default Hive data directory. By
default, it uses Text data format.
• A view is designated as VIRTUAL_VIEW in DESCRIBE FORMATTED output. Some of its properties are NULL
or blank because they are inherited from the base table. The text of the query that defines the view is part of the
DESCRIBE FORMATTED output.
• A table with additional clauses in the CREATE TABLE statement has differences in DESCRIBE FORMATTED
output. The output for T2 includes the EXTERNAL_TABLE keyword because of the CREATE EXTERNAL
TABLE syntax, and different InputFormat and OutputFormat fields to reflect the Parquet file format.
| y | int
| None |
| s | string
| None |
| | NULL
| NULL |
| # Detailed Table Information | NULL
| NULL |
| Database: | describe_formatted
| NULL |
| Owner: | doc_demo
| NULL |
| CreateTime: | Mon Jul 22 17:01:47 EDT 2013
| NULL |
| LastAccessTime: | UNKNOWN
| NULL |
| Protect Mode: | None
| NULL |
| Retention: | 0
| NULL |
| Location: | hdfs://127.0.0.1:8020/user/doc_demo/
sample_data | NULL |
| Table Type: | EXTERNAL_TABLE
| NULL |
| Table Parameters: | NULL
| NULL |
| | EXTERNAL
| TRUE |
| | transient_lastDdlTime
| 1374526907 |
| | NULL
| NULL |
| # Storage Information | NULL
| NULL |
| SerDe Library: |
org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe | NULL |
| InputFormat: |
org.apache.impala.hive.serde.ParquetInputFormat | NULL |
| OutputFormat: |
org.apache.impala.hive.serde.ParquetOutputFormat | NULL |
| Compressed: | No
| NULL |
| Num Buckets: | 0
| NULL |
| Bucket Columns: | []
| NULL |
| Sort Columns: | []
| NULL |
+------------------------------
+----------------------------------------------------+------------+
Returned 27 row(s) in 0.17s
• Whether or not the column is part of the primary key. Every Kudu table has a true value here for at least one
column. There could be multiple true values, for tables with composite primary keys.
• Whether or not the column is nullable. Specified by the NULL or NOT NULL attributes on the CREATE TABLE
statement. Columns that are part of the primary key are automatically non-nullable.
• The default value, if any, for the column. Specified by the DEFAULT attribute on the CREATE TABLE statement.
If the default value is NULL, that is not indicated in this column. It is implied by nullable being true and no
other default value specified.
• The encoding used for values in the column. Specified by the ENCODING attribute on the CREATE TABLE
statement.
• The compression used for values in the column. Specified by the COMPRESSION attribute on the CREATE
TABLE statement.
• The block size (in bytes) used for the underlying Kudu storage layer for the column. Specified by the
BLOCK_SIZE attribute on the CREATE TABLE statement.
The following example shows DESCRIBE output for a simple Kudu table, with a single-column primary key and all
column attributes left with their default values:
describe million_rows;
+------+--------+---------+-------------+----------+---------------
+---------------+---------------------+------------+
| name | type | comment | primary_key | nullable | default_value |
encoding | compression | block_size |
+------+--------+---------+-------------+----------+---------------
+---------------+---------------------+------------+
| id | string | | true | false | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 0 |
| s | string | | false | false | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 0 |
+------+--------+---------+-------------+----------+---------------
+---------------+---------------------+------------+
The following example shows DESCRIBE output for a Kudu table with a two-column primary key, and Kudu-
specific attributes applied to some columns:
describe kudu_describe_example;
+------+--------+---------+-------------+----------+---------------
+---------------+---------------------+------------+
| name | type | comment | primary_key | nullable | default_value |
encoding | compression | block_size |
+------+--------+---------+-------------+----------+---------------
+---------------+---------------------+------------+
| c1 | int | | true | false | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 0 |
| c2 | int | | true | false | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 0 |
| c3 | string | | false | true | |
AUTO_ENCODING | DEFAULT_COMPRESSION | 0 |
| Impala SQL Language Reference | 270
Related information:
Overview of Impala Tables on page 198, CREATE TABLE Statement on page 240, SHOW TABLES Statement
on page 387, SHOW CREATE TABLE Statement on page 389
See CREATE DATABASE Statement on page 231 for examples covering CREATE DATABASE, USE, and DROP
DATABASE.
Amazon S3 considerations:
In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP
DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove
folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala
database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with
the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with Impala.
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have write permission for the
directory associated with the database.
Examples:
-- Before dropping a database, first drop all the tables inside it,
-- or in Impala 2.3 and higher use the CASCADE clause.
drop database temp;
ERROR: ImpalaRuntimeException: Error making 'dropDatabase' RPC to Hive
Metastore:
CAUSED BY: InvalidOperationException: Database temp is not empty
show tables in temp;
+------+
| name |
+------+
| t3 |
+------+
-- Earlier releases:
drop table temp.t3;
drop database temp;
Related information:
Overview of Impala Databases on page 195, CREATE DATABASE Statement on page 231, USE Statement on
page 406, SHOW DATABASES on page 386, DROP TABLE Statement on page 278
Note:
The preceding syntax, which includes the function signature, also applies to Java UDFs that were created using the
corresponding CREATE FUNCTION syntax that includes the argument and return types. After upgrading to Impala
2.5 or higher, consider re-creating all Java UDFs with the CREATE FUNCTION syntax that does not include the
function signature. Java UDFs created this way are now persisted in the metastore database and do not need to be re-
created after an Impala restart.
To drop Java UDFs (created using the CREATE FUNCTION syntax with no function signature):
that name. (See CREATE FUNCTION Statement on page 233 for a longer example showing how to set up such
functions in the first place.)
show functions;
+-------------+---------------------------------------+-------------
+---------------+
| return type | signature | binary type | is
persistent |
+-------------+---------------------------------------+-------------
+---------------+
| BIGINT | my_func(BIGINT) | JAVA | true
|
| BOOLEAN | my_func(BOOLEAN) | JAVA | true
|
| BOOLEAN | my_func(BOOLEAN, BOOLEAN) | JAVA | true
|
...
| BIGINT | testudf(BIGINT) | JAVA | true
|
| BOOLEAN | testudf(BOOLEAN) | JAVA | true
|
| BOOLEAN | testudf(BOOLEAN, BOOLEAN) | JAVA | true
|
...
Related information:
User-Defined Functions (UDFs) on page 566, CREATE FUNCTION Statement on page 233
Required privileges:
| Impala SQL Language Reference | 274
Only administrative users (initially, a predefined set of users specified in the Sentry service configuration file) can use
this statement.
Compatibility:
Impala makes use of any roles and privileges specified by the GRANT and REVOKE statements in Hive, and Hive
makes use of any roles and privileges specified by the GRANT and REVOKE statements in Impala. The Impala GRANT
and REVOKE statements for privileges do not require the ROLE keyword to be repeated before each role name, unlike
the equivalent Hive statements.
Related information:
Impala Authorization on page 77, GRANT Statement (Impala 2.0 or higher only) on page 284 REVOKE
Statement (Impala 2.0 or higher only) on page 305, CREATE ROLE Statement (Impala 2.0 or higher only) on page
239, SHOW Statement on page 381
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
The PARTITION clause is only allowed in combination with the INCREMENTAL clause. It is optional for COMPUTE
INCREMENTAL STATS, and required for DROP INCREMENTAL STATS. Whenever you specify partitions
through the PARTITION (partition_spec) clause in a COMPUTE INCREMENTAL STATS or DROP
INCREMENTAL STATS statement, you must include all the partitioning columns in the specification, and specify
constant values for all the partition key columns.
DROP STATS removes all statistics from the table, whether created by COMPUTE STATS or COMPUTE
INCREMENTAL STATS.
DROP INCREMENTAL STATS only affects incremental statistics for a single partition, specified through the
PARTITION clause. The incremental stats are marked as outdated, so that they are recomputed by the next COMPUTE
INCREMENTAL STATS statement.
Usage notes:
You typically use this statement when the statistics for a table or a partition have become stale due to data files being
added to or removed from the associated HDFS data directories, whether by manual HDFS operations or INSERT,
INSERT OVERWRITE, or LOAD DATA statements, or adding or dropping partitions.
When a table or partition has no associated statistics, Impala treats it as essentially zero-sized when constructing the
execution plan for a query. In particular, the statistics influence the order in which tables are joined in a join query.
To ensure proper query planning and good query performance and scalability, make sure to run COMPUTE STATS or
COMPUTE INCREMENTAL STATS on the table or partition after removing any stale statistics.
Dropping the statistics is not required for an unpartitioned table or a partitioned table covered by the original type of
statistics. A subsequent COMPUTE STATS statement replaces any existing statistics with new ones, for all partitions,
regardless of whether the old ones were outdated. Therefore, this statement was rarely used before the introduction of
incremental statistics.
| Impala SQL Language Reference | 275
Dropping the statistics is required for a partitioned table containing incremental statistics, to make a subsequent
COMPUTE INCREMENTAL STATS statement rescan an existing partition. See Table and Column Statistics on page
601 for information about incremental statistics, a new feature available in Impala 2.1.0 and higher.
Statement type: DDL
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, does not need any particular HDFS
permissions to perform this statement. All read and write operations are on the metastore database, not HDFS files
and directories.
Examples:
The following example shows a partitioned table that has associated statistics produced by the COMPUTE
INCREMENTAL STATS statement, and how the situation evolves as statistics are dropped from specific partitions,
then the entire table.
Initially, all table and column statistics are filled in.
To remove statistics for particular partitions, use the DROP INCREMENTAL STATS statement. After removing
statistics for two partitions, the table-level statistics reflect that change in the #Rows and Incremental stats
fields. The counts, maximums, and averages of the column-level statistics are unaffected.
Note: (It is possible that the row count might be preserved in future after a DROP INCREMENTAL STATS
statement. Check the resolution of the issue IMPALA-1615.)
To remove all statistics from the table, whether produced by COMPUTE STATS or COMPUTE INCREMENTAL
STATS, use the DROP STATS statement without the INCREMENTAL clause). Now, both table-level and column-
level statistics are reset.
| i_item_sk | INT | -1 | -1 | 4 | 4
|
| i_item_id | STRING | -1 | -1 | -1 | -1
|
| i_rec_start_date | TIMESTAMP | -1 | -1 | 16 | 16
|
| i_rec_end_date | TIMESTAMP | -1 | -1 | 16 | 16
|
| i_item_desc | STRING | -1 | -1 | -1 | -1
|
| i_current_price | FLOAT | -1 | -1 | 4 | 4
|
| i_wholesale_cost | FLOAT | -1 | -1 | 4 | 4
|
| i_brand_id | INT | -1 | -1 | 4 | 4
|
| i_brand | STRING | -1 | -1 | -1 | -1
|
| i_class_id | INT | -1 | -1 | 4 | 4
|
| i_class | STRING | -1 | -1 | -1 | -1
|
| i_category_id | INT | -1 | -1 | 4 | 4
|
| i_manufact_id | INT | -1 | -1 | 4 | 4
|
| i_manufact | STRING | -1 | -1 | -1 | -1
|
| i_size | STRING | -1 | -1 | -1 | -1
|
| i_formulation | STRING | -1 | -1 | -1 | -1
|
| i_color | STRING | -1 | -1 | -1 | -1
|
| i_units | STRING | -1 | -1 | -1 | -1
|
| i_container | STRING | -1 | -1 | -1 | -1
|
| i_manager_id | INT | -1 | -1 | 4 | 4
|
| i_product_name | STRING | -1 | -1 | -1 | -1
|
| i_category | STRING | 10 | 0 | -1 | -1
|
+------------------+-----------+------------------+--------+----------
+----------+
Related information:
COMPUTE STATS Statement on page 223, SHOW TABLE STATS Statement on page 392, SHOW COLUMN
STATS Statement on page 393, Table and Column Statistics on page 601
IF EXISTS clause:
| Impala SQL Language Reference | 279
The optional IF EXISTS clause makes the statement succeed whether or not the table exists. If the table does exist,
it is dropped; if it does not exist, the statement has no effect. This capability is useful in standardized setup scripts
that remove existing schema objects and create new ones. By using some combination of IF EXISTS for the DROP
statements and IF NOT EXISTS clauses for the CREATE statements, the script can run successfully the first time
you run it (when the objects do not exist yet) and subsequent times (when some or all of the objects do already exist).
PURGE clause:
The optional PURGE keyword, available in Impala 2.3 and higher, causes Impala to remove the associated HDFS data
files immediately, rather than going through the HDFS trashcan mechanism. Use this keyword when dropping a table
if it is crucial to remove the data as quickly as possible to free up space, or if there is a problem with the trashcan,
such as the trash cannot being configured or being in a different HDFS encryption zone than the data files.
Statement type: DDL
Usage notes:
By default, Impala removes the associated HDFS directory and data files for the table. If you issue a DROP TABLE
and the data files are not deleted, it might be for the following reasons:
• If the table was created with the EXTERNAL clause, Impala leaves all files and directories untouched. Use external
tables when the data is under the control of other Hadoop components, and Impala is only used to query the data
files from their original locations.
• Impala might leave the data files behind unintentionally, if there is no HDFS location available to hold the HDFS
trashcan for the impala user. See User Account Requirements on page 22 for the procedure to set up the
required HDFS home directory.
Make sure that you are in the correct database before dropping a table, either by issuing a USE statement first or by
using a fully qualified name db_name.table_name.
If you intend to issue a DROP DATABASE statement, first issue DROP TABLE statements to remove all the tables in
that database.
Examples:
For other tips about managing and reclaiming Impala disk space, see Managing Disk Space for Impala Data on page
72.
Amazon S3 considerations:
The DROP TABLE statement can remove data files from S3 if the associated S3 table is an internal table. In Impala
2.6 and higher, as part of improved support for writing to S3, Impala also removes the associated folder when
dropping an internal table that resides on S3. See Using Impala with the Amazon S3 Filesystem on page 732 for
details about working with S3 tables.
For best compatibility with the S3 write support in Impala 2.6 and higher:
• Use native Hadoop techniques to create data files in S3 for querying through Impala.
• Use the PURGE clause of DROP TABLE when dropping internal (managed) tables.
| Impala SQL Language Reference | 280
By default, when you drop an internal (managed) table, the data files are moved to the HDFS trashcan. This operation
is expensive for tables that reside on the Amazon S3 filesystem. Therefore, for S3 tables, prefer to use DROP TABLE
table_name PURGE rather than the default DROP TABLE statement. The PURGE clause makes Impala delete the
data files immediately, skipping the HDFS trashcan. For the PURGE clause to work effectively, you must originally
create the data files on S3 using one of the tools from the Hadoop ecosystem, such as hadoop fs -cp, or INSERT
in Impala or Hive.
In Impala 2.6 and higher, Impala DDL statements such as CREATE DATABASE, CREATE TABLE, DROP
DATABASE CASCADE, DROP TABLE, and ALTER TABLE [ADD|DROP] PARTITION can create or remove
folders as needed in the Amazon S3 system. Prior to Impala 2.6, you had to create folders yourself and point Impala
database, tables, or partitions at them, and manually remove folders when no longer needed. See Using Impala with
the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with Impala.
Cancellation: Cannot be cancelled.
HDFS permissions:
For an internal table, the user ID that the impalad daemon runs under, typically the impala user, must have write
permission for all the files and directories that make up the table.
For an external table, dropping the table only involves changes to metadata in the metastore database. Because Impala
does not remove any HDFS files or directories when external tables are dropped, no particular permissions are needed
for the associated HDFS files or directories.
Kudu considerations:
Kudu tables can be managed or external, the same as with HDFS-based tables. For a managed table, the underlying
Kudu table and its data are removed by DROP TABLE. For an external table, the underlying Kudu table and its data
remain after a DROP TABLE.
Related information:
Overview of Impala Tables on page 198, ALTER TABLE Statement on page 207, CREATE TABLE Statement on
page 240, Partitioning for Impala Tables on page 661, Internal Tables on page 198, External Tables on page
199
USE db1;
-- Create a view in a different database.
CREATE VIEW db2.v1 AS SELECT * FROM db2.foo;
-- Switch into the other database and drop the view.
USE db2;
DROP VIEW v1;
USE db1;
-- Create a view in a different database.
CREATE VIEW db2.v1 AS SELECT * FROM db2.foo;
-- Drop a view in the other database.
DROP VIEW db2.v1;
Related information:
Overview of Impala Views on page 200, CREATE VIEW Statement on page 254, ALTER VIEW Statement on
page 222
EXPLAIN Statement
Returns the execution plan for a statement, showing the low-level mechanisms that Impala will use to read the data,
divide the work among nodes in the cluster, and transmit intermediate and final results across the network. Use
explain followed by a complete SELECT query. For example:
Syntax:
The select_query is a SELECT statement, optionally prefixed by a WITH clause. See SELECT Statement on page
306 for details.
The insert_stmt is an INSERT statement that inserts into or overwrites an existing table. It can use either the
INSERT ... SELECT or INSERT ... VALUES syntax. See INSERT Statement on page 288 for details.
The ctas_stmt is a CREATE TABLE statement using the AS SELECT clause, typically abbreviated as a “CTAS”
operation. See CREATE TABLE Statement on page 240 for details.
Usage notes:
You can interpret the output to judge whether the query is performing efficiently, and adjust the query and/or the
schema if not. For example, you might change the tests in the WHERE clause, add hints to make join operations more
efficient, introduce subqueries, change the order of tables in a join, add or change partitioning for a table, collect
column statistics and/or table statistics in Hive, or any other performance tuning steps.
The EXPLAIN output reminds you if table or column statistics are missing from any table involved in the query.
These statistics are important for optimizing queries involving large tables or multi-table joins. See COMPUTE
STATS Statement on page 223 for how to gather statistics, and Table and Column Statistics on page 601 for how
to use this information for query tuning.
Read the EXPLAIN plan from bottom to top:
• The last part of the plan shows the low-level details such as the expected amount of data that will be read, where
you can judge the effectiveness of your partitioning strategy and estimate how long it will take to scan a table
based on total data size and the size of the cluster.
• As you work your way up, next you see the operations that will be parallelized and performed on each Impala
node.
• At the higher levels, you see how data flows when intermediate result sets are combined and transmitted from one
node to another.
| Impala SQL Language Reference | 282
• See EXPLAIN_LEVEL Query Option on page 346 for details about the EXPLAIN_LEVEL query option, which
lets you customize how much detail to show in the EXPLAIN plan depending on whether you are doing high-level
or low-level tuning, dealing with logical or physical aspects of the query.
If you come from a traditional database background and are not familiar with data warehousing, keep in mind that
Impala is optimized for full table scans across very large tables. The structure and distribution of this data is typically
not suitable for the kind of indexing and single-row lookups that are common in OLTP environments. Seeing a query
scan entirely through a large table is common, not necessarily an indication of an inefficient query. Of course, if you
can reduce the volume of scanned data by orders of magnitude, for example by using a query that affects only certain
partitions within a partitioned table, then you might be able to optimize a query so that it executes in seconds rather
than minutes.
The EXPLAIN output becomes more accurate and informative as statistics are gathered by the COMPUTE STATS
statement. Initially, the information about data size and distribution, such as the number of rows or number of distinct
values for each column, is marked "unavailable". The COMPUTE STATS statement performs the analysis, so a
subsequent EXPLAIN statement has additional information to use in deciding how to optimize the distributed query.
For more information and examples to help you interpret EXPLAIN output, see Using the EXPLAIN Plan for
Performance Tuning on page 631.
Extended EXPLAIN output:
For performance tuning of complex queries, and capacity planning (such as using the admission control and resource
management features), you can enable more detailed and informative output for the EXPLAIN statement. In the
impala-shell interpreter, issue the command SET EXPLAIN_LEVEL=level, where level is MINIMAL,
STANDARD, EXTENDED, or VERBOSE.
When extended EXPLAIN output is enabled, EXPLAIN statements print information about estimated memory
requirements, minimum number of virtual cores, and so on.
Starting in Impala 3.2, if the EXPLAIN_LEVEL option is set to EXTENDED level or VERBOSE, the output contains
the following additional information.
• The analyzed query, in the output header.
The analyzed query may have been rewritten to include various optimizations and implicit casts. See the example
below.
• The predicates in the plan output includes the same implicit casts and literals printed with a cast to show the type.
See EXPLAIN_LEVEL Query Option on page 346 for details and examples.
Examples:
This example shows how the standard EXPLAIN output moves from the lowest (physical) level to the higher (logical)
levels.
1. The query begins by scanning a certain amount of data; each node performs an aggregation operation (evaluating
COUNT(*)) on some subset of data that is local to that node.
2. The intermediate results are transmitted back to the coordinator node (labelled here as the EXCHANGE node).
3. Lastly, the intermediate results are summed to display the final result.
| | |
| 00:SCAN HDFS [default.customer_address] |
| partitions=1/1 size=5.25MB |
+----------------------------------------------------------+
The following example shows an extended EXPLAIN output. Note that the analyzed query was rewritten to include:
• The 'constant folding' optimization, which simplified the expression in the original query, '1000 / 100' to '10'.
• The implicit casts in the WHERE clause.
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read and execute
permissions for all applicable directories in all source tables for the query that is being explained. (A SELECT
operation could read files from multiple different HDFS directories if the source table is partitioned.)
Kudu considerations:
The EXPLAIN statement displays equivalent plan information for queries against Kudu tables as for queries against
HDFS-based tables.
To see which predicates Impala can “push down” to Kudu for efficient evaluation, without transmitting unnecessary
rows back to Impala, look for the kudu predicates item in the scan phase of the query. The label kudu
predicates indicates a condition that can be evaluated efficiently on the Kudu side. The label predicates in a
SCAN KUDU node indicates a condition that is evaluated by Impala. For example, in a table with primary key column
X and non-primary key column Y, you can see that some operators in the WHERE clause are evaluated immediately by
Kudu and others are evaluated later by Impala:
Only binary predicates, IS NULL and IS NOT NULL (in Impala 2.9 and higher), and IN predicates containing
literal values that exactly match the types in the Kudu table, and do not require any casting, can be pushed to Kudu.
| Impala SQL Language Reference | 284
Related information:
SELECT Statement on page 306, INSERT Statement on page 288, CREATE TABLE Statement on page 240,
Understanding Impala Query Performance - EXPLAIN Plans and Query Profiles on page 630
The following syntax is supported when Impala is using Ranger to manage authorization.
• The parent levels of the specified scope are implicitly supported where a scope refers to the specific level in the
object hierarchy that the privilege is granted. For example, if a privilege is listed with the TABLE scope, the same
privilege granted on DATABASE and SERVER will allow the user to execute the specified SQL statement.
ALL TABLE
ALTER TABLE SET OWNER ALL WITH GRANT TABLE
ALTER VIEW ALTER TABLE
SELECT TABLE
ALTER VIEW RENAME CREATE DATABASE
ALL TABLE
ALTER VIEW SET OWNER ALL WITH GRANT VIEW
DROP DATABASE DROP DATABASE
DROP TABLE DROP TABLE
DROP VIEW DROP TABLE
CREATE FUNCTION CREATE DATABASE
ALL URI
DROP FUNCTION DROP DATABASE
COMPUTE STATS ALTER and SELECT TABLE
DROP STATS ALTER TABLE
INVALIDATE METADATA REFRESH SERVER
INVALIDATE METADATA REFRESH TABLE
<table>
REFRESH <table> REFRESH TABLE
REFRESH AUTHORIZATION REFRESH SERVER
REFRESH FUNCTIONS REFRESH DATABASE
COMMENT ON DATABASE ALTER DATABASE
COMMENT ON TABLE ALTER TABLE
COMMENT ON VIEW ALTER TABLE
COMMENT ON COLUMN ALTER TABLE
DESCRIBE DATABASE SELECT, INSERT, or REFRESH DATABASE
DESCRIBE <table/view> SELECT, INSERT, or REFRESH TABLE
If the user has the SELECT SELECT COLUMN
privilege at the COLUMN level,
only the columns the user has
access will show.
USE ANY TABLE
SHOW DATABASES ANY TABLE
SHOW TABLES ANY TABLE
SHOW FUNCTIONS SELECT, INSERT, or REFRESH DATABASE
SHOW PARTITIONS SELECT, INSERT, or REFRESH TABLE
SHOW TABLE STATS SELECT, INSERT, or REFRESH TABLE
| Impala SQL Language Reference | 287
Compatibility:
• The Impala GRANT and REVOKE statements are available in Impala 2.0 and later.
• In Impala 1.4 and later, Impala can make use of any roles and privileges specified by the GRANT and REVOKE
statements in Hive, when your system is configured to use the Sentry service instead of the file-based policy
mechanism.
• The Impala GRANT and REVOKE statements for privileges do not require the ROLE keyword to be repeated before
each role name, unlike the equivalent Hive statements.
• Currently, each Impala GRANT or REVOKE statement can only grant or revoke a single privilege to or from a
single role.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Kudu considerations:
Access to Kudu tables must be granted to and revoked from roles with the following considerations:
• Only users with the ALL privilege on SERVER can create external Kudu tables.
• The ALL privileges on SERVER is required to specify the kudu.master_addresses property in the CREATE
TABLE statements for managed tables as well as external tables.
• Access to Kudu tables is enforced at the table level and at the column level.
• The SELECT- and INSERT-specific permissions are supported.
• The DELETE, UPDATE, and UPSERT operations require the ALL privilege.
Because non-SQL APIs can access Kudu data without going through Sentry authorization, currently the Sentry
support is considered preliminary and subject to change.
Related information:
Impala Authorization on page 77, REVOKE Statement (Impala 2.0 or higher only) on page 305, CREATE
ROLE Statement (Impala 2.0 or higher only) on page 239, DROP ROLE Statement (Impala 2.0 or higher only) on
page 273, SHOW Statement on page 381
| Impala SQL Language Reference | 288
INSERT Statement
Impala supports inserting into tables and partitions that you create with the Impala CREATE TABLE statement, or
pre-defined tables and partitions created through Hive.
Syntax:
[with_clause]
INSERT [hint_clause] { INTO | OVERWRITE } [TABLE] table_name
[(column_list)]
[ PARTITION (partition_clause)]
{
[hint_clause] select_statement
| VALUES (value [, value ...]) [, (value [, value ...]) ...]
}
hint_clause ::=
hint_with_dashes |
hint_with_cstyle_delimiters |
hint_with_brackets
Note: The square bracket style of hint is now deprecated and might be removed in a future release. For that reason,
any newly added hints are not available with the square bracket syntax.
Appending or replacing (INTO and OVERWRITE clauses):
The INSERT INTO syntax appends data to a table. The existing data files are left as-is, and the inserted data is put
into one or more new data files.
The INSERT OVERWRITE syntax replaces the data in a table. Currently, the overwritten data files are deleted
immediately; they do not go through the HDFS trash mechanism.
Complex type considerations:
The INSERT statement currently does not support writing data files containing complex types (ARRAY, STRUCT,
and MAP). To prepare Parquet data for such tables, you generate the data files outside Impala and then use LOAD
DATA or CREATE EXTERNAL TABLE to associate those data files with the table. Currently, such tables must use
the Parquet file format. See Complex Types (Impala 2.3 or higher only) on page 134 for details about working with
complex types.
Kudu considerations:
Currently, the INSERT OVERWRITE syntax cannot be used with Kudu tables.
Kudu tables require a unique primary key for each row. If an INSERT statement attempts to insert a row with the
same values for the primary key columns as an existing row, that row is discarded and the insert operation continues.
When rows are discarded due to duplicate primary keys, the statement finishes with a warning, not an error. (This is
a change from early releases of Kudu where the default was to return in error in such cases, and the syntax INSERT
IGNORE was required to make the statement succeed. The IGNORE clause is no longer part of the INSERT syntax.)
For situations where you prefer to replace rows with duplicate primary key values, rather than discarding the new
data, you can use the UPSERT statement instead of INSERT. UPSERT inserts rows that are entirely new, and for
rows that match an existing primary key in the table, the non-primary-key columns are updated to reflect the values in
the “upserted” data.
| Impala SQL Language Reference | 289
If you really want to store new rows, not replace existing ones, but cannot do so because of the primary key
uniqueness constraint, consider recreating the table with additional columns included in the primary key.
See Using Impala to Query Kudu Tables on page 708 for more details about using Impala with Kudu.
Usage notes:
Impala currently supports:
• Copy data from another table using SELECT query. In Impala 1.2.1 and higher, you can combine CREATE
TABLE and INSERT operations into a single step with the CREATE TABLE AS SELECT syntax, which
bypasses the actual INSERT keyword.
• An optional WITH clause before the INSERT keyword, to define a subquery referenced in the SELECT portion.
• Create one or more new rows using constant expressions through VALUES clause. (The VALUES clause was
added in Impala 1.0.1.)
• By default, the first column of each newly inserted row goes into the first column of the table, the second column
into the second column, and so on.
You can also specify the columns to be inserted, an arbitrarily ordered subset of the columns in the destination
table, by specifying a column list immediately after the name of the destination table. This feature lets you adjust
the inserted columns to match the layout of a SELECT statement, rather than the other way around. (This feature
was added in Impala 1.1.)
The number of columns mentioned in the column list (known as the “column permutation”) must match the
number of columns in the SELECT list or the VALUES tuples. The order of columns in the column permutation
can be different than in the underlying table, and the columns of each input row are reordered to match. If the
number of columns in the column permutation is less than in the destination table, all unmentioned columns are
set to NULL.
• An optional hint clause immediately either before the SELECT keyword or after the INSERT keyword, to fine-
tune the behavior when doing an INSERT ... SELECT operation into partitioned Parquet tables. The hint
clause cannot be specified in multiple places. The hint keywords are [SHUFFLE] and [NOSHUFFLE], including
the square brackets. Inserting into partitioned Parquet tables can be a resource-intensive operation because it
potentially involves many files being written to HDFS simultaneously, and separate large memory buffers being
allocated to buffer the data for each partition. For usage details, see Loading Data into Parquet Tables on page
682.
Note:
• Insert commands that partition or add files result in changes to Hive metadata. Because Impala uses Hive
metadata, such changes may necessitate a metadata refresh. For more information, see the REFRESH function.
• Currently, Impala can only insert data into tables that use the text and Parquet formats. For other file formats,
insert the data using Hive and use Impala to query it.
• As an alternative to the INSERT statement, if you have existing data files elsewhere in HDFS, the LOAD DATA
statement can move those files into a table. This statement works with tables of any file format.
Statement type: DML (but still affected by SYNC_DDL query option)
Usage notes:
When you insert the results of an expression, particularly of a built-in function call, into a small numeric column such
as INT, SMALLINT, TINYINT, or FLOAT, you might need to use a CAST() expression to coerce values into the
appropriate type. Impala does not automatically convert from a larger type to a smaller one. For example, to insert
cosine values into a FLOAT column, write CAST(COS(angle) AS FLOAT) in the INSERT statement to make
the conversion explicit.
File format considerations:
Because Impala can read certain file formats that it cannot write, the INSERT statement does not work for all kinds of
Impala tables. See How Impala Works with Hadoop File Formats on page 671 for details about what file formats
are supported by the INSERT statement.
| Impala SQL Language Reference | 290
Any INSERT statement for a Parquet table requires enough free space in the HDFS filesystem to write one block.
Because Parquet data files use a block size of 1 GB by default, an INSERT might fail (even for a very small amount
of data) if your HDFS is running low on space.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
The following example sets up new tables with the same definition as the TAB1 table from the Tutorial section, using
different file formats, and demonstrates inserting data into the tables created with the STORED AS TEXTFILE and
STORED AS PARQUET clauses:
With the INSERT INTO TABLE syntax, each new set of inserted rows is appended to any existing data in the
table. This is how you would record small amounts of data that arrive continuously, or ingest new batches of data
alongside the existing data. For example, after running 2 INSERT INTO TABLE statements with 5 rows each, the
table contains 10 rows total:
With the INSERT OVERWRITE TABLE syntax, each new set of inserted rows replaces any existing data in the
table. This is how you load data to query in a data warehousing scenario where you analyze just the data for a
particular day, quarter, and so on, discarding the previous data each time. You might keep the entire set of data in
one raw table, and transfer and transform certain rows into a more compact and efficient form to perform intensive
analysis on that subset.
| Impala SQL Language Reference | 291
For example, here we insert 5 rows into a table using the INSERT INTO clause, then replace the data by inserting 3
rows with the INSERT OVERWRITE clause. Afterward, the table only contains the 3 rows from the final INSERT
statement.
The VALUES clause lets you insert one or more rows by specifying constant values for all the columns. The number,
types, and order of the expressions must match the table definition.
Note: The INSERT ... VALUES technique is not suitable for loading large quantities of data into HDFS-
based tables, because the insert operations cannot be parallelized, and each one produces a separate data file. Use
it for setting up small dimension tables or tiny amounts of data for experimenting with SQL syntax, or with HBase
tables. Do not use it for large ETL jobs or benchmark tests for load operations. Do not run scripts with thousands
of INSERT ... VALUES statements that insert a single row each time. If you do run INSERT ... VALUES
operations to load data into a staging table as one stage in an ETL pipeline, include multiple row values if possible
within each VALUES clause, and use a separate database to make cleanup easier if the operation does produce many
tiny files.
The following example shows how to insert one row or multiple rows, with expressions of different types, using
literal values, expressions, and function return values:
These examples show the type of “not implemented” error that you see when attempting to insert data into a table
with a file format that Impala currently does not write to:
The following examples show how you can copy the data in all the columns from one table to another, copy the data
from only some columns, or specify the columns in the select list in a different order than they actually appear in the
table:
-- The column names can be entirely different in the source and destination
tables.
-- You can copy any columns, not just the corresponding ones, from the
source table.
-- But the number and type of selected columns must match the columns
mentioned in the () part.
alter table t2 replace columns (x int, y int);
insert into t2 (y) select c1 from t1;
Sorting considerations: Although you can specify an ORDER BY clause in an INSERT ... SELECT statement,
any ORDER BY clause is ignored and the results are not necessarily sorted. An INSERT ... SELECT operation
potentially creates many different data files, prepared by different executor Impala daemons, and therefore the notion
of the data being stored in sorted order is impractical.
Concurrency considerations: Each INSERT operation creates new data files with unique names, so you can run
multiple INSERT INTO statements simultaneously without filename conflicts. While data is being inserted into an
Impala table, the data is staged temporarily in a subdirectory inside the data directory; during this period, you cannot
issue queries against that table in Hive. If an INSERT operation fails, the temporary data file and the subdirectory
could be left behind in the data directory. If so, remove the relevant subdirectory and any data files it contains
manually, by issuing an hdfs dfs -rm -r command, specifying the full path of the work subdirectory, whose
name ends in _dir.
VALUES Clause
The VALUES clause is a general-purpose way to specify the columns of one or more rows, typically within an
INSERT statement.
Note: The INSERT ... VALUES technique is not suitable for loading large quantities of data into HDFS-
based tables, because the insert operations cannot be parallelized, and each one produces a separate data file. Use
it for setting up small dimension tables or tiny amounts of data for experimenting with SQL syntax, or with HBase
tables. Do not use it for large ETL jobs or benchmark tests for load operations. Do not run scripts with thousands
of INSERT ... VALUES statements that insert a single row each time. If you do run INSERT ... VALUES
operations to load data into a staging table as one stage in an ETL pipeline, include multiple row values if possible
within each VALUES clause, and use a separate database to make cleanup easier if the operation does produce many
tiny files.
The following examples illustrate:
• How to insert a single row using a VALUES clause.
• How to insert multiple rows using a VALUES clause.
| Impala SQL Language Reference | 293
• How the row or rows from a VALUES clause can be appended to a table through INSERT INTO, or replace the
contents of the table through INSERT OVERWRITE.
• How the entries in a VALUES clause can be literals, function results, or any other kind of expression. See Literals
on page 165 for the notation to use for literal values, especially String Literals on page 167 for quoting
and escaping conventions for strings. See SQL Operators on page 170 and Impala Built-In Functions on page
412 for other things you can include in expressions with the VALUES clause.
When used in an INSERT statement, the Impala VALUES clause can specify some or all of the columns in the
destination table, and the columns can be specified in a different order than they actually appear in the table. To
specify a different set or order of columns than in the table, use the syntax:
Any columns in the table that are not listed in the INSERT statement are set to NULL.
To use a VALUES clause like a table in other statements, wrap it in parentheses and use AS clauses to specify aliases
for the entire object and any columns you need to refer to:
| c1 | c2 | c3 |
+-----+-------+-----+
| 1 | true | abc |
| 100 | false | xyz |
+-----+-------+-----+
For example, you might use a tiny table constructed like this from constant literals or function return values as part of
a longer statement involving joins or UNION ALL.
HDFS considerations:
Impala physically writes all inserted files under the ownership of its default user, typically impala. Therefore, this
user must have HDFS write permission in the corresponding table directory.
The permission requirement is independent of the authorization performed by the Sentry framework. (If the connected
user is not authorized to insert into a table, Sentry blocks that operation immediately, regardless of the privileges
available to the impala user.) Files created by Impala are not owned by and do not inherit permissions from the
connected user.
The number of data files produced by an INSERT statement depends on the size of the cluster, the number of data
blocks that are processed, the partition key columns in a partitioned table, and the mechanism Impala uses for
dividing the work in parallel. Do not assume that an INSERT statement will produce some particular number of
output files. In case of performance issues with data written by Impala, check that the output files do not suffer from
issues such as many tiny files or many tiny partitions. (In the Hadoop context, even files or partitions of a few tens of
megabytes are considered “tiny”.)
The INSERT statement has always left behind a hidden work directory inside the data directory of the table.
Formerly, this hidden work directory was named .impala_insert_staging . In Impala 2.0.1 and later, this
directory name is changed to _impala_insert_staging . (While HDFS tools are expected to treat names
beginning either with underscore and dot as hidden, in practice names beginning with an underscore are more widely
supported.) If you have any scripts, cleanup jobs, and so on that rely on the name of this work directory, adjust them
to use the new name.
HBase considerations:
You can use the INSERT statement with HBase tables as follows:
• You can insert a single row or a small set of rows into an HBase table with the INSERT ... VALUES syntax.
This is a good use case for HBase tables with Impala, because HBase tables are not subject to the same kind of
fragmentation from many small insert operations as HDFS tables are.
• You can insert any number of rows at once into an HBase table using the INSERT ... SELECT syntax.
• If more than one inserted row has the same value for the HBase key column, only the last inserted row with that
value is visible to Impala queries. You can take advantage of this fact with INSERT ... VALUES statements to
effectively update rows one at a time, by inserting new rows with the same key values as existing rows. Be aware
that after an INSERT ... SELECT operation copying from an HDFS table, the HBase table might contain
fewer rows than were inserted, if the key column in the source table contained duplicate values.
• You cannot INSERT OVERWRITE into an HBase table. New rows are always appended.
• When you create an Impala or Hive table that maps to an HBase table, the column order you specify with the
INSERT statement might be different than the order you declare with the CREATE TABLE statement. Behind
the scenes, HBase arranges the columns based on how they are divided into column families. This might cause a
mismatch during insert operations, especially if you use the syntax INSERT INTO hbase_table SELECT
* FROM hdfs_table. Before inserting data, verify the column order by issuing a DESCRIBE statement for
the table, and adjust the order of the select list in the INSERT statement.
See Using Impala to Query HBase Tables on page 722 for more details about using Impala with HBase.
Amazon S3 considerations:
In Impala 2.6 and higher, the Impala DML statements (INSERT, LOAD DATA, and CREATE TABLE AS SELECT)
can write data into a table or partition that resides in the Amazon Simple Storage Service (S3). The syntax of the
DML statements is the same as for any other tables, because the S3 location for tables and partitions is specified by an
s3a:// prefix in the LOCATION attribute of CREATE TABLE or ALTER TABLE statements. If you bring data into
| Impala SQL Language Reference | 295
S3 using the normal S3 transfer mechanisms instead of Impala DML statements, issue a REFRESH statement for the
table before using Impala to query the S3 data.
Because of differences between S3 and traditional filesystems, DML operations for S3 tables can take longer
than for tables on HDFS. For example, both the LOAD DATA statement and the final stage of the INSERT and
CREATE TABLE AS SELECT statements involve moving files from one directory to another. (In the case of
INSERT and CREATE TABLE AS SELECT, the files are moved from a temporary staging directory to the final
destination directory.) Because S3 does not support a “rename” operation for existing objects, in these cases Impala
actually copies the data files from one location to another and then removes the original files. In Impala 2.6, the
S3_SKIP_INSERT_STAGING query option provides a way to speed up INSERT statements for S3 tables and
partitions, with the tradeoff that a problem during statement execution could leave data in an inconsistent state. It does
not apply to INSERT OVERWRITE or LOAD DATA statements. See S3_SKIP_INSERT_STAGING Query Option
(Impala 2.6 or higher only) on page 376 for details.
See Using Impala with the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with
Impala.
ADLS considerations:
In Impala 2.9 and higher, the Impala DML statements (INSERT, LOAD DATA, and CREATE TABLE AS SELECT)
can write data into a table or partition that resides in the Azure Data Lake Store (ADLS). ADLS Gen2 is supported in
Impala 3.1 and higher.
In theCREATE TABLE or ALTER TABLE statements, specify the ADLS location for tables and partitions with the
adl:// prefix for ADLS Gen1 and abfs:// or abfss:// for ADLS Gen2 in the LOCATION attribute.
If you bring data into ADLS using the normal ADLS transfer mechanisms instead of Impala DML statements, issue a
REFRESH statement for the table before using Impala to query the ADLS data.
See Using Impala with the Azure Data Lake Store (ADLS) on page 740 for details about reading and writing ADLS
data with Impala.
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Cancellation: Can be cancelled. To cancel this statement, use Ctrl-C from the impala-shell interpreter, the
Cancel button from the Watch page in Hue, or Cancel from the list of in-flight queries (for a particular node) on the
Queries tab in the Impala web UI (port 25000).
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read permission for
the files in the source directory of an INSERT ... SELECT operation, and write permission for all affected
directories in the destination table. (An INSERT operation could write files to multiple different HDFS directories if
the destination table is partitioned.) This user must also have write permission to create a temporary work directory
in the top-level HDFS directory of the destination table. An INSERT OVERWRITE operation does not require write
permission on the original data files in the table, only on the table directories themselves.
Restrictions:
For INSERT operations into CHAR or VARCHAR columns, you must cast all STRING literals or expressions returning
STRING to to a CHAR or VARCHAR type with the appropriate length.
Related startup options:
By default, if an INSERT statement creates any new subdirectories underneath a partitioned table, those
subdirectories are assigned default HDFS permissions for the impala user. To make each subdirectory have the
same permissions as its parent directory in HDFS, specify the ‑‑insert_inherit_permissions startup option for the
impalad daemon.
| Impala SQL Language Reference | 296
• When a partition clause is specified but the non-partition columns are not specified in the INSERT statement,
as in the first example below, the non-partition columns are treated as though they had been specified before the
PARTITION clause in the SQL.
Example: These three statements are equivalent, inserting 1 to w, 2 to x, and ‘c’ to y columns.
• The PARTITION clause is not required for dynamic partition, but all the partition columns must be explicitly
present in the INSERT statement in the column list or in the PARTITION clause. The partition columns cannot be
defaulted to NULL.
Example:
The following statements are valid because the partition columns, x and y, are present in the INSERT statements,
either in the PARTITION clause or in the column list.
The following statement is not valid for the partitioned table as defined above because the partition columns, x
and y, are not present in the INSERT statement.
If there is no table specified, the cached metadata for all tables is flushed and synced with Hive Metastore (HMS). If
tables were dropped from the HMS, they will be removed from the catalog, and if new tables were added, they will
show up in the catalog.
If you specify a table name, only the metadata for that one table is flushed and synced with the HMS.
Usage notes:
| Impala SQL Language Reference | 298
To return accurate query results, Impala need to keep the metadata current for the databases and tables queried.
Therefore, if some other entity modifies information used by Impala in the metastore, the information cached by
Impala must be updated via INVALIDATE METADATA or REFRESH.
INVALIDATE METADATA and REFRESH are counterparts:
• INVALIDATE METADATA is an asynchronous operations that simply discards the loaded metadata from the
catalog and coordinator caches. After that operation, the catalog and all the Impala coordinators only know about
the existence of databases and tables and nothing more. Metadata loading for tables is triggered by any subsequent
queries.
• REFRESH reloads the metadata synchronously. REFRESH is more lightweight than doing a full metadata load
after a table has been invalidated. REFRESH cannot detect changes in block locations triggered by operations like
HDFS balancer, hence causing remote reads during query execution with negative performance implications.
Use REFRESH after invalidating a specific table to separate the metadata load from the first query that's run against
that table.
Examples:
This example illustrates creating a new database and new table in Hive, then doing an INVALIDATE METADATA
statement in Impala using the fully qualified table name, after which both the new table and the new database are
visible to Impala.
Before the INVALIDATE METADATA statement was issued, Impala would give a “not found” error if you tried to
refer to those database or table names.
$ hive
hive> CREATE DATABASE new_db_from_hive;
hive> CREATE TABLE new_db_from_hive.new_table_from_hive (x INT);
hive> quit;
$ impala-shell
> REFRESH new_db_from_hive.new_table_from_hive;
ERROR: AnalysisException: Database does not exist: new_db_from_hive
For more examples of using INVALIDATE METADATA with a combination of Impala and Hive operations, see
Switching Back and Forth Between Impala and Hive on page 49.
HDFS considerations:
By default, the INVALIDATE METADATA command checks HDFS permissions of the underlying data files and
directories, caching this information so that a statement can be cancelled immediately if for example the impala
user does not have permission to write to the data directory for the table. (This checking does not apply when the
catalogd configuration option --load_catalog_in_background is set to false, which it is by default.)
Impala reports any lack of write permissions as an INFO message in the log file.
| Impala SQL Language Reference | 299
If you change HDFS permissions to make data readable or writeable by the Impala user, issue another INVALIDATE
METADATA to make Impala aware of the change.
Kudu considerations:
By default, much of the metadata for Kudu tables is handled by the underlying storage layer. Kudu tables have less
reliance on the Metastore database, and require less metadata caching on the Impala side. For example, information
about partitions in Kudu tables is managed by Kudu, and Impala does not cache any block locality metadata for Kudu
tables. If the Kudu service is not integrated with the Hive Metastore, Impala will manage Kudu table metadata in the
Hive Metastore.
The REFRESH and INVALIDATE METADATA statements are needed less frequently for Kudu tables than for
HDFS-backed tables. Neither statement is needed when data is added to, removed, or updated in a Kudu table, even if
the changes are made directly to Kudu through a client program using the Kudu API. Run REFRESH table_name
or INVALIDATE METADATA table_name for a Kudu table only after making a change to the Kudu table
schema, such as adding or dropping a column.
Related information:
Overview of Impala Metadata and the Metastore on page 20, REFRESH Statement on page 302
When the LOAD DATA statement operates on a partitioned table, it always operates on one partition at a time. Specify
the PARTITION clauses and list all the partition key columns, with a constant value specified for each.
Statement type: DML (but still affected by SYNC_DDL query option)
Usage notes:
• The loaded data files are moved, not copied, into the Impala data directory.
• You can specify the HDFS path of a single file to be moved, or the HDFS path of a directory to move all the files
inside that directory. You cannot specify any sort of wildcard to take only some of the files from a directory.
When loading a directory full of data files, keep all the data files at the top level, with no nested directories
underneath.
• Currently, the Impala LOAD DATA statement only imports files from HDFS, not from the local filesystem. It does
not support the LOCAL keyword of the Hive LOAD DATA statement. You must specify a path, not an hdfs://
URI.
• In the interest of speed, only limited error checking is done. If the loaded files have the wrong file format,
different columns than the destination table, or other kind of mismatch, Impala does not raise any error for the
LOAD DATA statement. Querying the table afterward could produce a runtime error or unexpected results.
Currently, the only checking the LOAD DATA statement does is to avoid mixing together uncompressed and LZO-
compressed text files in the same table.
• When you specify an HDFS directory name as the LOAD DATA argument, any hidden files in that directory (files
whose names start with a .) are not moved to the Impala data directory.
• The operation fails if the source directory contains any non-hidden directories. Prior to Impala 2.5 if the source
directory contained any subdirectory, even a hidden one such as _impala_insert_staging, the LOAD
DATA statement would fail. In Impala 2.5 and higher, LOAD DATA ignores hidden subdirectories in the source
directory, and only fails if any of the subdirectories are non-hidden.
| Impala SQL Language Reference | 300
• The loaded data files retain their original names in the new location, unless a name conflicts with an existing
data file, in which case the name of the new file is modified slightly to be unique. (The name-mangling is a slight
difference from the Hive LOAD DATA statement, which replaces identically named files.)
• By providing an easy way to transport files from known locations in HDFS into the Impala data directory
structure, the LOAD DATA statement lets you avoid memorizing the locations and layout of HDFS directory tree
containing the Impala databases and tables. (For a quick way to check the location of the data files for an Impala
table, issue the statement DESCRIBE FORMATTED table_name.)
• The PARTITION clause is especially convenient for ingesting new data for a partitioned table. As you receive
new data for a time period, geographic region, or other division that corresponds to one or more partitioning
columns, you can load that data straight into the appropriate Impala data directory, which might be nested several
levels down if the table is partitioned by multiple columns. When the table is partitioned, you must specify
constant values for all the partitioning columns.
Complex type considerations:
Because Impala currently cannot create Parquet data files containing complex types (ARRAY, STRUCT, and MAP),
the LOAD DATA statement is especially important when working with tables containing complex type columns.
You create the Parquet data files outside Impala, then use either LOAD DATA, an external table, or HDFS-level file
operations followed by REFRESH to associate the data files with the corresponding table. See Complex Types (Impala
2.3 or higher only) on page 134 for details about using complex types.
If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can
enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed
metadata has been received by all the Impala nodes. See SYNC_DDL Query Option on page 379 for details.
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
First, we use a trivial Python script to write different numbers of strings (one per line) into files stored in the
doc_demo HDFS user account. (Substitute the path for your own HDFS user account when doing hdfs dfs
operations like these.)
Next, we create a table and load an initial set of data into it. Remember, unless you specify a STORED AS clause,
Impala tables default to TEXTFILE format with Ctrl-A (hex 01) as the field delimiter. This example uses a single-
column table, so the delimiter is not significant. For large-scale ETL jobs, you would typically use binary format data
files such as Parquet or Avro, and load them into Impala tables that use the corresponding file format.
+------+
| 1000 |
+------+
Returned 1 row(s) in 0.67s
[localhost:21000] > load data inpath '/user/doc_demo/thousand_strings.txt'
into table t1;
ERROR: AnalysisException: INPATH location '/user/doc_demo/
thousand_strings.txt' does not exist.
As indicated by the message at the end of the previous example, the data file was moved from its original location.
The following example illustrates how the data file was moved into the Impala data directory for the destination table,
keeping its original filename:
The following example demonstrates the difference between the INTO TABLE and OVERWRITE TABLE clauses.
The table already contains 1000 rows. After issuing the LOAD DATA statement with the INTO TABLE clause, the
table contains 100 more rows, for a total of 1100. After issuing the LOAD DATA statement with the OVERWRITE
INTO TABLE clause, the former contents are gone, and now the table only contains the 10 rows from the just-loaded
data file.
Amazon S3 considerations:
| Impala SQL Language Reference | 302
In Impala 2.6 and higher, the Impala DML statements (INSERT, LOAD DATA, and CREATE TABLE AS SELECT)
can write data into a table or partition that resides in the Amazon Simple Storage Service (S3). The syntax of the
DML statements is the same as for any other tables, because the S3 location for tables and partitions is specified by an
s3a:// prefix in the LOCATION attribute of CREATE TABLE or ALTER TABLE statements. If you bring data into
S3 using the normal S3 transfer mechanisms instead of Impala DML statements, issue a REFRESH statement for the
table before using Impala to query the S3 data.
Because of differences between S3 and traditional filesystems, DML operations for S3 tables can take longer
than for tables on HDFS. For example, both the LOAD DATA statement and the final stage of the INSERT and
CREATE TABLE AS SELECT statements involve moving files from one directory to another. (In the case of
INSERT and CREATE TABLE AS SELECT, the files are moved from a temporary staging directory to the final
destination directory.) Because S3 does not support a “rename” operation for existing objects, in these cases Impala
actually copies the data files from one location to another and then removes the original files. In Impala 2.6, the
S3_SKIP_INSERT_STAGING query option provides a way to speed up INSERT statements for S3 tables and
partitions, with the tradeoff that a problem during statement execution could leave data in an inconsistent state. It does
not apply to INSERT OVERWRITE or LOAD DATA statements. See S3_SKIP_INSERT_STAGING Query Option
(Impala 2.6 or higher only) on page 376 for details.
See Using Impala with the Amazon S3 Filesystem on page 732 for details about reading and writing S3 data with
Impala.
ADLS considerations:
In Impala 2.9 and higher, the Impala DML statements (INSERT, LOAD DATA, and CREATE TABLE AS SELECT)
can write data into a table or partition that resides in the Azure Data Lake Store (ADLS). ADLS Gen2 is supported in
Impala 3.1 and higher.
In theCREATE TABLE or ALTER TABLE statements, specify the ADLS location for tables and partitions with the
adl:// prefix for ADLS Gen1 and abfs:// or abfss:// for ADLS Gen2 in the LOCATION attribute.
If you bring data into ADLS using the normal ADLS transfer mechanisms instead of Impala DML statements, issue a
REFRESH statement for the table before using Impala to query the ADLS data.
See Using Impala with the Azure Data Lake Store (ADLS) on page 740 for details about reading and writing ADLS
data with Impala.
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read and write permissions
for the files in the source directory, and write permission for the destination directory.
Kudu considerations:
The LOAD DATA statement cannot be used with Kudu tables.
HBase considerations:
The LOAD DATA statement cannot be used with HBase tables.
Related information:
The LOAD DATA statement is an alternative to the INSERT statement. Use LOAD DATA when you have the data
files in HDFS but outside of any Impala table.
The LOAD DATA statement is also an alternative to the CREATE EXTERNAL TABLE statement. Use LOAD DATA
when it is appropriate to move the data files under Impala control rather than querying them from their original
location. See External Tables on page 199 for information about working with external tables.
REFRESH Statement
The REFRESH statement reloads the metadata for the table from the metastore database and does an incremental
reload of the file and block metadata from the HDFS NameNode. REFRESH is used to avoid inconsistencies between
Impala and external metadata sources, namely Hive Metastore (HMS) and NameNodes.
| Impala SQL Language Reference | 303
The REFRESH statement is only required if you load data from outside of Impala. Updated metadata, as a result of
running REFRESH, is broadcast to all Impala coordinators.
See Overview of Impala Metadata and the Metastore on page 20 for the information about the way Impala uses
metadata and how it shares the same metastore database as Hive.
Once issued, the REFRESH statement cannot be cancelled.
Syntax:
Usage notes:
The table name is a required parameter, and the table must already exist and be known to Impala.
Only the metadata for the specified table is reloaded.
Use the REFRESH statement to load the latest metastore metadata for a particular table after one of the following
scenarios happens outside of Impala:
• Deleting, adding, or modifying files.
For example, after loading new data files into the HDFS data directory for the table, appending to an existing
HDFS file, inserting data from Hive via INSERT or LOAD DATA.
• Deleting, adding, or modifying partitions.
For example, after issuing ALTER TABLE or other table-modifying SQL statement in Hive
Note:
In Impala 2.3 and higher, the ALTER TABLE table_name RECOVER PARTITIONS statement is a faster
alternative to REFRESH when you are only adding new partition directories through Hive or manual HDFS
operations. See ALTER TABLE Statement on page 207 for details.
INVALIDATE METADATA and REFRESH are counterparts:
• INVALIDATE METADATA is an asynchronous operations that simply discards the loaded metadata from the
catalog and coordinator caches. After that operation, the catalog and all the Impala coordinators only know about
the existence of databases and tables and nothing more. Metadata loading for tables is triggered by any subsequent
queries.
• REFRESH reloads the metadata synchronously. REFRESH is more lightweight than doing a full metadata load
after a table has been invalidated. REFRESH cannot detect changes in block locations triggered by operations like
HDFS balancer, hence causing remote reads during query execution with negative performance implications.
Refreshing a single partition:
In Impala 2.7 and higher, the REFRESH statement can apply to a single partition at a time, rather than the whole table.
Include the optional PARTITION (partition_spec) clause and specify values for each of the partition key
columns.
The following rules apply:
• The PARTITION clause of the REFRESH statement must include all the partition key columns.
• The order of the partition key columns does not have to match the column order in the table.
• Specifying a nonexistent partition does not cause an error.
• The partition can be one that Impala created and is already aware of, or a new partition created through Hive.
The following examples demonstrates the above rules.
For examples of using REFRESH and INVALIDATE METADATA with a combination of Impala and Hive operations,
see Switching Back and Forth Between Impala and Hive on page 49.
Related impala-shell options:
Due to the expense of reloading the metadata for all tables, the impala-shell -r option is not recommended.
HDFS permissions:
All HDFS and Sentry permissions and privilege requirements are the same whether you refresh the entire table or a
single partition.
HDFS considerations:
The REFRESH statement checks HDFS permissions of the underlying data files and directories, caching this
information so that a statement can be cancelled immediately if for example the impala user does not have
permission to write to the data directory for the table. Impala reports any lack of write permissions as an INFO
message in the log file.
If you change HDFS permissions to make data readable or writeable by the Impala user, issue another REFRESH to
make Impala aware of the change.
Kudu considerations:
By default, much of the metadata for Kudu tables is handled by the underlying storage layer. Kudu tables have less
reliance on the Metastore database, and require less metadata caching on the Impala side. For example, information
about partitions in Kudu tables is managed by Kudu, and Impala does not cache any block locality metadata for Kudu
tables. If the Kudu service is not integrated with the Hive Metastore, Impala will manage Kudu table metadata in the
Hive Metastore.
The REFRESH and INVALIDATE METADATA statements are needed less frequently for Kudu tables than for
HDFS-backed tables. Neither statement is needed when data is added to, removed, or updated in a Kudu table, even if
the changes are made directly to Kudu through a client program using the Kudu API. Run REFRESH table_name
or INVALIDATE METADATA table_name for a Kudu table only after making a change to the Kudu table
schema, such as adding or dropping a column.
Related information:
Overview of Impala Metadata and the Metastore on page 20, INVALIDATE METADATA Statement on page
297
REFRESH AUTHORIZATION
| Impala SQL Language Reference | 305
The following syntax is supported when Impala is using Ranger to manage authorization.
Usage notes:
See GRANT Statement (Impala 2.0 or higher only) for the required privileges and the scope for SQL operations.
The ALL privilege is a distinct privilege and not a union of all other privileges. Revoking SELECT, INSERT, etc.
from a role that only has the ALL privilege has no effect. To reduce the privileges of that role you must REVOKE
ALL and GRANT the desired privileges.
You cannot revoke a privilege granted with the WITH GRANT OPTION. If a privilege is granted with the WITH
GRANT OPTION, first revoke the grant option, and then revoke the privilege.
| Impala SQL Language Reference | 306
For example:
SELECT Statement
The SELECT statement performs queries, retrieving data from one or more tables and producing result sets consisting
of rows and columns.
The Impala INSERT statement also typically ends with a SELECT statement, to define data to copy from one table to
another.
| Impala SQL Language Reference | 307
Syntax:
Impala queries ignore files with extensions commonly used for temporary work files by Hadoop tools. Any files with
extensions .tmp or .copying are not considered part of the Impala table. The suffix matching is case-insensitive,
so for example Impala ignores both .copying and .COPYING suffixes.
Security considerations:
If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers,
Impala can redact this sensitive information when displaying the statements in log files and other administrative
contexts. See the documentation for your Apache Hadoop distribution for details.
Amazon S3 considerations:
In Impala 2.6 and higher, Impala queries are optimized for files stored in Amazon S3. For Impala tables that use the
file formats Parquet, ORC, RCFile, SequenceFile, Avro, and uncompressed text, the setting fs.s3a.block.size
in the core-site.xml configuration file determines how Impala divides the I/O work of reading the data files.
This configuration setting is specified in bytes. By default, this value is 33554432 (32 MB), meaning that Impala
parallelizes S3 read operations on the files as if they were made up of 32 MB blocks. For example, if your S3 queries
primarily access Parquet files written by MapReduce or Hive, increase fs.s3a.block.size to 134217728 (128
MB) to match the row group size of those files. If most S3 queries involve Parquet files written by Impala, increase
fs.s3a.block.size to 268435456 (256 MB) to match the row group size produced by Impala.
Cancellation: Can be cancelled. To cancel this statement, use Ctrl-C from the impala-shell interpreter, the
Cancel button from the Watch page in Hue, or Cancel from the list of in-flight queries (for a particular node) on the
Queries tab in the Impala web UI (port 25000).
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read permissions for
the files in all applicable directories in all source tables, and read and execute permissions for the relevant data
directories. (A SELECT operation could read files from multiple different HDFS directories if the source table is
partitioned.) If a query attempts to read a data file and is unable to because of an HDFS permission error, the query
halts and does not return any further results.
Related information:
The SELECT syntax is so extensive that it forms its own category of statements: queries. The other major
classifications of SQL statements are data definition language (see DDL Statements on page 205) and data
manipulation language (see DML Statements on page 206).
Because the focus of Impala is on fast queries with interactive response times over huge data sets, query performance
and scalability are important considerations. See Tuning Impala for Performance on page 590 and Scalability
Considerations for Impala on page 633 for details.
The ON clause is a general way to compare columns across the two tables, even if the column names are different.
The USING clause is a shorthand notation for specifying the join columns, when the column names are the same in
both tables. You can code equivalent WHERE clauses that compare the columns, instead of ON or USING clauses, but
that practice is not recommended because mixing the join comparisons with other filtering clauses is typically less
readable and harder to maintain.
Queries with a comma-separated list of tables and subqueries are known as SQL-89 style joins. In these queries,
the equality comparisons between columns of the joined tables go in the WHERE clause alongside other kinds of
comparisons. This syntax is easy to learn, but it is also easy to accidentally remove a WHERE clause needed for the
join to work correctly.
Self-joins:
Impala can do self-joins, for example to join on two different columns in the same table to represent parent-child
relationships or other tree-structured data. There is no explicit syntax for this; just use the same table name for both
the left-hand and right-hand table, and assign different table aliases to use when referring to the fully qualified column
names:
column with the same name occurs in both tables, use a fully qualified name or a column alias to refer to the column
in the select list or other clauses. Impala performs inner joins by default for both SQL-89 and SQL-92 join syntax:
An outer join retrieves all rows from the left-hand table, or the right-hand table, or both; wherever there is no
matching data in the table on the other side of the join, the corresponding columns in the result set are set to NULL. To
perform an outer join, include the OUTER keyword in the join operator, along with either LEFT, RIGHT, or FULL:
For outer joins, Impala requires SQL-92 syntax; that is, the JOIN keyword instead of comma-separated table names.
Impala does not support vendor extensions such as (+) or *= notation for doing outer joins with SQL-89 query
syntax.
Equijoins and Non-Equijoins:
By default, Impala requires an equality comparison between the left-hand and right-hand tables, either through ON,
USING, or WHERE clauses. These types of queries are classified broadly as equijoins. Inner, outer, full, and semi joins
can all be equijoins based on the presence of equality tests between columns in the left-hand and right-hand tables.
In Impala 1.2.2 and higher, non-equijoin queries are also possible, with comparisons such as != or < between the join
columns. These kinds of queries require care to avoid producing huge result sets that could exceed resource limits.
Once you have planned a non-equijoin query that produces a result set of acceptable size, you can code the query
using the CROSS JOIN operator, and add the extra comparisons in the WHERE clause:
In Impala 2.3 and higher, additional non-equijoin queries are possible due to the addition of nested loop joins. These
queries typically involve SEMI JOIN, ANTI JOIN, or FULL OUTER JOIN clauses. Impala sometimes also uses
nested loop joins internally when evaluating OUTER JOIN queries involving complex type columns. Query phases
involving nested loop joins do not use the spill-to-disk mechanism if they exceed the memory limit. Impala decides
internally when to use each join mechanism; you cannot specify any query hint to choose between the nested loop
join or the original hash join algorithm.
Semi-joins:
Semi-joins are a relatively rarely used variation. With the left semi-join, only data from the left-hand table is returned,
for rows where there is matching data in the right-hand table, based on comparisons between join columns in ON or
WHERE clauses. Only one instance of each row from the left-hand table is returned, regardless of how many matching
rows exist in the right-hand table. A right semi-join (available in Impala 2.0 and higher) reverses the comparison and
returns data from the right-hand table.
SELECT t1.c1, t1.c2, t1.c2 FROM t1 LEFT SEMI JOIN t2 ON t1.id = t2.id;
If you do have any queries that use NATURAL JOIN, make sure to rewrite them with explicit USING clauses,
because Impala could interpret the NATURAL keyword as a table alias:
-- If you expect the tables to have identically named columns with matching
values,
-- list the corresponding column names in a USING clause.
SELECT t1.c1, t2.c2 FROM t1 JOIN t2 USING (id, type_flag, name, address);
When comparing columns with the same names in ON or WHERE clauses, use the fully qualified names such as
db_name.table_name, or assign table aliases, column aliases, or both to make the code more compact and
understandable:
Note:
Performance for join queries is a crucial aspect for Impala, because complex join queries are resource-intensive
operations. An efficient join query produces much less network traffic and CPU overhead than an inefficient one. For
best results:
• Make sure that both table and column statistics are available for all the tables involved in a join query, and
especially for the columns referenced in any join conditions. Impala uses the statistics to automatically deduce an
efficient join order. Use SHOW TABLE STATS table_name and SHOW COLUMN STATS table_name
to check if statistics are already present. Issue the COMPUTE STATS table_name for a nonpartitioned table,
or (in Impala 2.1.0 and higher) COMPUTE INCREMENTAL STATS table_name for a partitioned table,
to collect the initial statistics at both the table and column levels, and to keep the statistics up to date after any
substantial INSERT or LOAD DATA operations.
• If table or column statistics are not available, join the largest table first. You can check the existence of statistics
with the SHOW TABLE STATS table_name and SHOW COLUMN STATS table_name statements.
• If table or column statistics are not available, join subsequent tables according to which table has the most
selective filter, based on overall size and WHERE clauses. Joining the table with the most selective filter results in
the fewest number of rows being returned.
For more information and examples of performance for join queries, see Performance Considerations for Join
Queries on page 594.
To control the result set from a join query, include the names of corresponding column names in both tables in an ON
or USING clause, or by coding equality comparisons for those columns in the WHERE clause.
One potential downside of joins is the possibility of excess resource usage in poorly constructed queries. Impala
imposes restrictions on join queries to guard against such issues. To minimize the chance of runaway queries on
large data sets, Impala requires every join query to contain at least one equality predicate between the columns of the
various tables. For example, if T1 contains 1000 rows and T2 contains 1,000,000 rows, a query SELECT columns
FROM t1 JOIN t2 could return up to 1 billion rows (1000 * 1,000,000); Impala requires that the query include a
clause such as ON t1.c1 = t2.c2 or WHERE t1.c1 = t2.c2.
| Impala SQL Language Reference | 313
Because even with equality clauses, the result set can still be large, as we saw in the previous example, you might use
a LIMIT clause to return a subset of the results:
Or you might use additional comparison operators or aggregation functions to condense a large result set into a
smaller set of values:
[localhost:21000] > -- See how many different customers in this town have
names starting with "A".
[localhost:21000] > select count(distinct c_last_name) from customer,
customer_address where
c_customer_sk = ca_address_sk
and ca_city = "Green Acres"
and substr(c_last_name,1,1) = "A";
+-----------------------------+
| count(distinct c_last_name) |
+-----------------------------+
| 12 |
+-----------------------------+
Returned 1 row(s) in 1.00s
| Impala SQL Language Reference | 314
Because a join query can involve reading large amounts of data from disk, sending large amounts of data across
the network, and loading large amounts of data into memory to do the comparisons and filtering, you might do
benchmarking, performance analysis, and query tuning to find the most efficient join queries for your data set,
hardware capacity, network configuration, and cluster workload.
The two categories of joins in Impala are known as partitioned joins and broadcast joins. If inaccurate table
or column statistics, or some quirk of the data distribution, causes Impala to choose the wrong mechanism for a
particular join, consider using query hints as a temporary workaround. For details, see Optimizer Hints on page
407.
Handling NULLs in Join Columns:
By default, join key columns do not match if either one contains a NULL value. To treat such columns as equal if
both contain NULL, you can use an expression such as A = B OR (A IS NULL AND B IS NULL). In Impala
2.5 and higher, the <=> operator (shorthand for IS NOT DISTINCT FROM) performs the same comparison in a
concise and efficient form. The <=> operator is more efficient in for comparing join keys in a NULL-safe manner,
because the operator can use a hash join while the OR expression cannot.
Examples:
The following examples refer to these simple tables containing small sets of integers:
The following example demonstrates an anti-join, returning the values from T1 that do not exist in T2 (in this case,
the odd numbers 1, 3, and 5):
Related information:
See these tutorials for examples of different kinds of joins:
• Cross Joins and Cartesian Products with the CROSS JOIN Operator on page 49
ORDER BY Clause
The ORDER BY clause of a SELECT statement sorts the result set based on the values from one or more columns.
First, data is sorted locally by each impalad daemon, then streamed to the coordinator daemon, which merges
the sorted result sets. For distributed queries, this is a relatively expensive operation and can require more memory
capacity than a query without ORDER BY. Even if the query takes approximately the same time to finish with or
without the ORDER BY clause, subjectively it can appear slower because no results are available until all processing
is finished, rather than results coming back gradually as rows matching the WHERE clause are found. Therefore, if
you only need the first N results from the sorted result set, also include the LIMIT clause, which reduces network
overhead and the memory requirement on the coordinator node.
Syntax:
| Impala SQL Language Reference | 315
ORDER BY col_ref [, col_ref ...] [ASC | DESC] [NULLS FIRST | NULLS LAST]
Although the most common usage is ORDER BY column_name, you can also specify ORDER BY 1 to sort by the
first column of the result set, ORDER BY 2 to sort by the second column, and so on. The number must be a numeric
literal, not some other kind of constant expression. (If the argument is some other expression, even a STRING value,
the query succeeds but the order of results is undefined.)
ORDER BY column_number can only be used when the query explicitly lists the columns in the SELECT list, not
with SELECT * queries.
Ascending and descending sorts:
The default sort order (the same as using the ASC keyword) puts the smallest values at the start of the result set, and
the largest values at the end. Specifying the DESC keyword reverses that order.
Sort order for NULL values:
See NULL on page 169 for details about how NULL values are positioned in the sorted result set, and how to use
the NULLS FIRST and NULLS LAST clauses. (The sort position for NULL values in ORDER BY ... DESC
queries is changed in Impala 1.2.1 and higher to be more standards-compliant, and the NULLS FIRST and NULLS
LAST keywords are new in Impala 1.2.1.)
Prior to Impala 1.4.0, Impala required any query including an ORDER BY clause to also use a LIMIT clause. In
Impala 1.4.0 and higher, the LIMIT clause is optional for ORDER BY queries. In cases where sorting a huge result
set requires enough memory to exceed the Impala memory limit for a particular executor Impala daemon, Impala
automatically uses a temporary disk work area to perform the sort operation.
Complex type considerations:
In Impala 2.3 and higher, the complex data types STRUCT, ARRAY, and MAP are available. These columns cannot
be referenced directly in the ORDER BY clause. When you query a complex type column, you use join notation
to “unpack” the elements of the complex type, and within the join query you can include an ORDER BY clause to
control the order in the result set of the scalar elements from the complex type. See Complex Types (Impala 2.3 or
higher only) on page 134 for details about Impala support for complex types.
The following query shows how a complex type column cannot be directly used in an ORDER BY clause:
CREATE TABLE games (id BIGINT, score ARRAY <BIGINT>) STORED AS PARQUET;
...use LOAD DATA to load externally created Parquet files into the table...
SELECT id FROM games ORDER BY score DESC;
ERROR: AnalysisException: ORDER BY expression 'score' with complex type
'ARRAY<BIGINT>' is not supported.
Examples:
The following query retrieves the user ID and score, only for scores greater than one million, with the highest scores
for each user listed first. Because the individual array elements are now represented as separate rows in the result
set, they can be used in the ORDER BY clause, referenced using the ITEM pseudo-column that represents each array
element.
The following queries use similar ORDER BY techniques with variations of the GAMES table, where the complex type
is an ARRAY containing STRUCT or MAP elements to represent additional details about each game that was played.
| Impala SQL Language Reference | 316
For an array of structures, the fields of the structure are referenced as ITEM.field_name. For an array of maps,
the keys and values within each array element are referenced as ITEM.KEY and ITEM.VALUE.
CREATE TABLE games2 (id BIGINT, play array < struct <game_name: string,
score: BIGINT, high_score: boolean> >) STORED AS PARQUET
...use LOAD DATA to load externally created Parquet files into the table...
SELECT id, item.game_name, item.score FROM games2, games2.play
WHERE item.score > 1000000
ORDER BY id, item.score DESC;
CREATE TABLE games3 (id BIGINT, play ARRAY < MAP <STRING, BIGINT> >) STORED
AS PARQUET;
...use LOAD DATA to load externally created Parquet files into the table...
SELECT id, info.key AS k, info.value AS v from games3, games3.play AS plays,
games3.play.item AS info
WHERE info.KEY = 'score' AND info.VALUE > 1000000
ORDER BY id, info.value desc;
Usage notes:
Although the LIMIT clause is now optional on ORDER BY queries, if your query only needs some number of rows
that you can predict in advance, use the LIMIT clause to reduce unnecessary processing. For example, if the query
has a clause LIMIT 10, each executor Impala daemon sorts its portion of the relevant result set and only returns
10 rows to the coordinator node. The coordinator node picks the 10 highest or lowest row values out of this small
intermediate result set.
If an ORDER BY clause is applied to an early phase of query processing, such as a subquery or a view definition,
Impala ignores the ORDER BY clause. To get ordered results from a subquery or view, apply an ORDER BY clause to
the outermost or final SELECT level.
ORDER BY is often used in combination with LIMIT to perform “top-N” queries:
ORDER BY is sometimes used in combination with OFFSET and LIMIT to paginate query results, although it is
relatively inefficient to issue multiple queries like this against the large tables typically used with Impala:
Internal details:
Impala sorts the intermediate results of an ORDER BY clause in memory whenever practical. In a cluster of N
executor Impala daemons, each daemon sorts roughly 1/Nth of the result set, the exact proportion varying depending
on how the data matching the query is distributed in HDFS.
If the size of the sorted intermediate result set on any executor Impala daemon would cause the query to exceed the
Impala memory limit, Impala sorts as much as practical in memory, then writes partially sorted data to disk. (This
technique is known in industry terminology as “external sorting” and “spilling to disk”.) As each 8 MB batch of data
is written to disk, Impala frees the corresponding memory to sort a new 8 MB batch of data. When all the data has
| Impala SQL Language Reference | 317
been processed, a final merge sort operation is performed to correctly order the in-memory and on-disk results as
the result set is transmitted back to the coordinator node. When external sorting becomes necessary, Impala requires
approximately 60 MB of RAM at a minimum for the buffers needed to read, write, and sort the intermediate results. If
more RAM is available on the Impala daemon, Impala will use the additional RAM to minimize the amount of disk I/
O for sorting.
This external sort technique is used as appropriate on each Impala daemon (possibly including the coordinator node)
to sort the portion of the result set that is processed on that node. When the sorted intermediate results are sent back
to the coordinator node to produce the final result set, the coordinator node uses a merge sort technique to produce a
final sorted result set without using any extra resources on the coordinator node.
Configuration for disk usage:
By default, intermediate files used during large sort, join, aggregation, or analytic function operations are stored
in the directory /tmp/impala-scratch . These files are removed when the operation finishes. (Multiple
concurrent queries can perform operations that use the “spill to disk” technique, without any name conflicts
for these temporary files.) You can specify a different location by starting the impalad daemon with the
‑‑scratch_dirs="path_to_directory" configuration option. You can specify a single directory, or a comma-
separated list of directories. The scratch directories must be on the local filesystem, not in HDFS. You might specify
different directory paths for different hosts, depending on the capacity and speed of the available storage devices. In
Impala 2.3 or higher, Impala successfully starts (with a warning Impala successfully starts (with a warning written to
the log) if it cannot create or read and write files in one of the scratch directories. If there is less than 1 GB free on the
filesystem where that directory resides, Impala still runs, but writes a warning message to its log. If Impala encounters
an error reading or writing files in a scratch directory during a query, Impala logs the error and the query fails.
Sorting considerations: Although you can specify an ORDER BY clause in an INSERT ... SELECT statement,
any ORDER BY clause is ignored and the results are not necessarily sorted. An INSERT ... SELECT operation
potentially creates many different data files, prepared by different executor Impala daemons, and therefore the notion
of the data being stored in sorted order is impractical.
An ORDER BY clause without an additional LIMIT clause is ignored in any view definition. If you need to sort the
entire result set from a view, use an ORDER BY clause in the SELECT statement that queries the view. You can still
make a simple “top 10” report by combining the ORDER BY and LIMIT clauses in the same view definition:
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
[localhost:21000] > create view top_3_view as select x from unsorted order
by x limit 3;
[localhost:21000] > select x from top_3_view; -- ORDER BY and LIMIT together
in view definition are preserved.
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
With the lifting of the requirement to include a LIMIT clause in every ORDER BY query (in Impala 1.4 and higher):
• Now the use of scratch disk space raises the possibility of an “out of disk space” error on a particular Impala
daemon, as opposed to the previous possibility of an “out of memory” error. Make sure to keep at least 1 GB free
on the filesystem used for temporary sorting work.
In Impala 1.2.1 and higher, all NULL values come at the end of the result set for ORDER BY ... ASC queries,
and at the beginning of the result set for ORDER BY ... DESC queries. In effect, NULL is considered greater
than all other values for sorting purposes. The original Impala behavior always put NULL values at the end, even
for ORDER BY ... DESC queries. The new behavior in Impala 1.2.1 makes Impala more compatible with other
popular database systems. In Impala 1.2.1 and higher, you can override or specify the sorting behavior for NULL by
adding the clause NULLS FIRST or NULLS LAST at the end of the ORDER BY clause.
| NULL |
+------+
[localhost:21000] > select x from numbers order by x desc nulls last;
+------+
| x |
+------+
| 3 |
| 2 |
| 1 |
| NULL |
| NULL |
+------+
Related information:
See SELECT Statement on page 306 for further examples of queries with the ORDER BY clause.
Analytic functions use the ORDER BY clause in a different context to define the sequence in which rows are
analyzed. See Impala Analytic Functions on page 547 for details.
GROUP BY Clause
Specify the GROUP BY clause in queries that use aggregation functions, such as COUNT(), SUM(), AVG(), MIN(),
and MAX(). Specify in the GROUP BY clause the names of all the columns that do not participate in the aggregation
operation.
Complex type considerations:
In Impala 2.3 and higher, the complex data types STRUCT, ARRAY, and MAP are available. These columns cannot
be referenced directly in the ORDER BY clause. When you query a complex type column, you use join notation
to “unpack” the elements of the complex type, and within the join query you can include an ORDER BY clause to
control the order in the result set of the scalar elements from the complex type. See Complex Types (Impala 2.3 or
higher only) on page 134 for details about Impala support for complex types.
Zero-length strings: For purposes of clauses such as DISTINCT and GROUP BY, Impala considers zero-length
strings (""), NULL, and space to all be different values.
Examples:
For example, the following query finds the 5 items that sold the highest total quantity (using the SUM() function,
and also counts the number of sales transactions for those items (using the COUNT() function). Because the column
representing the item IDs is not used in any aggregation functions, we specify that column in the GROUP BY clause.
select
ss_item_sk as Item,
count(ss_item_sk) as Times_Purchased,
sum(ss_quantity) as Total_Quantity_Purchased
from store_sales
group by ss_item_sk
order by sum(ss_quantity) desc
limit 5;
+-------+-----------------+--------------------------+
| item | times_purchased | total_quantity_purchased |
+-------+-----------------+--------------------------+
| 9325 | 372 | 19072 |
| 4279 | 357 | 18501 |
| 7507 | 371 | 18475 |
| 5953 | 369 | 18451 |
| 16753 | 375 | 18446 |
+-------+-----------------+--------------------------+
| Impala SQL Language Reference | 320
The HAVING clause lets you filter the results of aggregate functions, because you cannot refer to those expressions in
the WHERE clause. For example, to find the 5 lowest-selling items that were included in at least 100 sales transactions,
we could use this query:
select
ss_item_sk as Item,
count(ss_item_sk) as Times_Purchased,
sum(ss_quantity) as Total_Quantity_Purchased
from store_sales
group by ss_item_sk
having times_purchased >= 100
order by sum(ss_quantity)
limit 5;
+-------+-----------------+--------------------------+
| item | times_purchased | total_quantity_purchased |
+-------+-----------------+--------------------------+
| 13943 | 105 | 4087 |
| 2992 | 101 | 4176 |
| 4773 | 107 | 4204 |
| 14350 | 103 | 4260 |
| 11956 | 102 | 4275 |
+-------+-----------------+--------------------------+
When performing calculations involving scientific or financial data, remember that columns with type FLOAT or
DOUBLE are stored as true floating-point numbers, which cannot precisely represent every possible fractional value.
Thus, if you include a FLOAT or DOUBLE column in a GROUP BY clause, the results might not precisely match
literal values in your query or from an original Text data file. Use rounding operations, the BETWEEN operator, or
another arithmetic technique to match floating-point values that are “near” literal values you expect. For example,
this query on the ss_wholesale_cost column returns cost values that are close but not identical to the original
figures that were entered as decimal fractions.
Notice how wholesale cost values originally entered as decimal fractions such as 96.94 and 98.38 are slightly
larger or smaller in the result set, due to precision limitations in the hardware floating-point types. The imprecise
representation of FLOAT and DOUBLE values is why financial data processing systems often store currency using
data types that are less space-efficient but avoid these types of rounding errors.
Related information:
SELECT Statement on page 306, Impala Aggregate Functions on page 516
HAVING Clause
Performs a filter operation on a SELECT query, by examining the results of aggregation functions rather than testing
each individual table row. Therefore, it is always used in conjunction with a function such as COUNT(), SUM(),
AVG(), MIN(), or MAX(), and typically with the GROUP BY clause also.
| Impala SQL Language Reference | 321
Restrictions:
The filter expression in the HAVING clause cannot include a scalar subquery.
Related information:
SELECT Statement on page 306, GROUP BY Clause on page 319, Impala Aggregate Functions on page 516
LIMIT Clause
The LIMIT clause in a SELECT query sets a maximum number of rows for the result set. Pre-selecting the maximum
size of the result set helps Impala to optimize memory usage while processing a distributed query.
Syntax:
LIMIT constant_integer_expression
The argument to the LIMIT clause must evaluate to a constant value. It can be a numeric literal, or another kind
of numeric expression involving operators, casts, and function return values. You cannot refer to a column or use a
subquery.
Usage notes:
This clause is useful in contexts such as:
• To return exactly N items from a top-N query, such as the 10 highest-rated items in a shopping category or the 50
hostnames that refer the most traffic to a web site.
• To demonstrate some sample values from a table or a particular query. (To display some arbitrary items, use a
query with no ORDER BY clause. An ORDER BY clause causes additional memory and/or disk usage during the
query.)
• To keep queries from returning huge result sets by accident if a table is larger than expected, or a WHERE clause
matches more rows than expected.
Originally, the value for the LIMIT clause had to be a numeric literal. In Impala 1.2.1 and higher, it can be a numeric
expression.
Prior to Impala 1.4.0, Impala required any query including an ORDER BY clause to also use a LIMIT clause. In
Impala 1.4.0 and higher, the LIMIT clause is optional for ORDER BY queries. In cases where sorting a huge result
set requires enough memory to exceed the Impala memory limit for a particular executor Impala daemon, Impala
automatically uses a temporary disk work area to perform the sort operation.
See ORDER BY Clause on page 314 for details.
In Impala 1.2.1 and higher, you can combine a LIMIT clause with an OFFSET clause to produce a small result
set that is different from a top-N query, for example, to return items 11 through 20. This technique can be used to
simulate “paged” results. Because Impala queries typically involve substantial amounts of I/O, use this technique
only for compatibility in cases where you cannot rewrite the application logic. For best performance and scalability,
wherever practical, query as many items as you expect to need, cache them on the application side, and display small
groups of results to users using application logic.
Restrictions:
Correlated subqueries used in EXISTS and IN operators cannot include a LIMIT clause.
Examples:
The following example shows how the LIMIT clause caps the size of the result set, with the limit being applied after
any other clauses such as WHERE.
+---+
| x |
+---+
| 1 |
| 3 |
| 4 |
| 5 |
| 2 |
+---+
Returned 5 row(s) in 0.26s
[localhost:21000] > select x from numbers limit 3;
+---+
| x |
+---+
| 1 |
| 3 |
| 4 |
+---+
Returned 3 row(s) in 0.27s
[localhost:21000] > select x from numbers where x > 2 limit 2;
+---+
| x |
+---+
| 3 |
| 4 |
+---+
Returned 2 row(s) in 0.27s
For top-N and bottom-N queries, you use the ORDER BY and LIMIT clauses together:
[localhost:21000] > select x as "Top 3" from numbers order by x desc limit
3;
+-------+
| top 3 |
+-------+
| 5 |
| 4 |
| 3 |
+-------+
[localhost:21000] > select x as "Bottom 3" from numbers order by x limit 3;
+----------+
| bottom 3 |
+----------+
| 1 |
| 2 |
| 3 |
+----------+
You can use constant values besides integer literals as the LIMIT argument:
OFFSET Clause
The OFFSET clause in a SELECT query causes the result set to start some number of rows after the logical first item.
The result set is numbered starting from zero, so OFFSET 0 produces the same result as leaving out the OFFSET
clause. Always use this clause in combination with ORDER BY (so that it is clear which item should be first, second,
and so on) and LIMIT (so that the result set covers a bounded range, such as items 0-9, 100-199, and so on).
| Impala SQL Language Reference | 323
In Impala 1.2.1 and higher, you can combine a LIMIT clause with an OFFSET clause to produce a small result
set that is different from a top-N query, for example, to return items 11 through 20. This technique can be used to
simulate “paged” results. Because Impala queries typically involve substantial amounts of I/O, use this technique
only for compatibility in cases where you cannot rewrite the application logic. For best performance and scalability,
wherever practical, query as many items as you expect to need, cache them on the application side, and display small
groups of results to users using application logic.
Examples:
The following example shows how you could run a “paging” query originally written for a traditional database
application. Because typical Impala queries process megabytes or gigabytes of data and read large data files from
disk each time, it is inefficient to run a separate query to retrieve each small group of items. Use this technique only
for compatibility while porting older applications, then rewrite the application code to use a single query with a large
result set, and display pages of results from the cached result set.
UNION Clause
The UNION clause lets you combine the result sets of multiple queries. By default, the result sets are combined as if
the DISTINCT operator was applied.
Syntax:
Usage notes:
The UNION keyword by itself is the same as UNION DISTINCT. Because eliminating duplicates can be a memory-
intensive process for a large result set, prefer UNION ALL where practical. (That is, when you know the different
queries in the union will not produce any duplicates, or where the duplicate values are acceptable.)
When an ORDER BY clause applies to a UNION ALL or UNION query, in Impala 1.4 and higher, the LIMIT clause
is no longer required. To make the ORDER BY and LIMIT clauses apply to the entire result set, turn the UNION
query into a subquery, SELECT from the subquery, and put the ORDER BY clause at the end, outside the subquery.
Examples:
First, set up some sample data, including duplicate 1 values:
[localhost:21000] > insert into few_ints values (1), (1), (2), (3);
[localhost:21000] > set default_order_by_limit=1000;
This example shows how UNION ALL returns all rows from both queries, without any additional filtering to
eliminate duplicates. For the large result sets common with Impala queries, this is the most memory-efficient
technique.
This example shows how the UNION clause without the ALL keyword condenses the result set to eliminate all
duplicate values, making the query take more time and potentially more memory. The extra processing typically
makes this technique not recommended for queries that return result sets with millions or billions of values.
[localhost:21000] > select x from few_ints union select x+1 from few_ints;
+---+
| x |
+---+
| 3 |
| 4 |
| 1 |
| 2 |
+---+
Returned 4 row(s) in 0.51s
[localhost:21000] > select x from few_ints union select 10;
+----+
| x |
+----+
| 2 |
| 10 |
| 1 |
| 3 |
+----+
Returned 4 row(s) in 0.49s
[localhost:21000] > select * from (select x from few_ints union select x
from few_ints) as t1 order by x;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
Returned 3 row(s) in 0.53s
Uncorrelated subqueries do not refer to any tables from the outer block of the query. The same value or set of values
produced by the subquery is used when evaluating each row from the outer query block. In this example, the subquery
returns an arbitrary number of values from T2.Y, and each value of T1.X is tested for membership in that same set
of values:
Correlated subqueries compare one or more values from the outer query block to values referenced in the WHERE
clause of the subquery. Each row evaluated by the outer WHERE clause can be evaluated using a different set of
| Impala SQL Language Reference | 326
values. These kinds of subqueries are restricted in the kinds of comparisons they can do between columns of the inner
and outer tables. (See the following Restrictions item.)
For example, the following query finds all the employees with salaries that are higher than average for their
department. The subquery potentially computes a different AVG() value for each employee.
Syntax:
Subquery in the FROM clause:
comparison_operator is a numeric comparison such as =, <=, !=, and so on, or a string comparison operator
such as LIKE or REGEXP.
Although you can use non-equality comparison operators such as < or >=, the subquery must include at least one
equality comparison between the columns of the inner and outer query blocks.
All syntax is available for both correlated and uncorrelated queries, except that the NOT EXISTS clause cannot be
used with an uncorrelated subquery.
Impala subqueries can be nested arbitrarily deep.
Standards compliance: Introduced in SQL:1999.
Examples:
This example illustrates how subqueries can be used in the FROM clause to organize the table names, column names,
and column values by producing intermediate result sets, especially for join queries.
These examples show how a query can test for the existence of values in a separate table using the EXISTS()
operator with a subquery.
The following examples show how a value can be compared against a set of values returned by a subquery.
The following examples demonstrate scalar subqueries. When a subquery is known to return a single value, you can
substitute it where you would normally put a constant value.
Usage notes:
If the same table is referenced in both the outer and inner query blocks, construct a table alias in the outer query block
and use a fully qualified name to distinguish the inner and outer table references:
SELECT * FROM t1 one WHERE id IN (SELECT parent FROM t1 two WHERE t1.parent
= t2.id);
The STRAIGHT_JOIN hint affects the join order of table references in the query block containing the hint. It does
not affect the join order of nested queries, such as views, inline views, or WHERE-clause subqueries. To use this hint
for performance tuning of complex queries, apply the hint to all query blocks that need a fixed join order.
Internal details:
Internally, subqueries involving IN, NOT IN, EXISTS, or NOT EXISTS clauses are rewritten into join queries.
Depending on the syntax, the subquery might be rewritten to an outer join, semi join, cross join, or anti join.
A query is processed differently depending on whether the subquery calls any aggregation functions. There are
correlated and uncorrelated forms, with and without calls to aggregation functions. Each of these four categories is
rewritten differently.
Column statistics considerations:
Because queries that include correlated and uncorrelated subqueries in the WHERE clause are written into join queries,
to achieve best performance, follow the same guidelines for running the COMPUTE STATS statement as you do for
tables involved in regular join queries. Run the COMPUTE STATS statement for each associated tables after loading
or substantially changing the data in that table. See Table and Column Statistics on page 601 for details.
Added in: Subqueries are substantially enhanced starting in Impala 2.0. Now, they can be used in the WHERE clause,
in combination with clauses such as EXISTS and IN, rather than just in the FROM clause.
Restrictions:
The initial Impala support for nested subqueries addresses the most common use cases. Some restrictions remain:
• Although you can use subqueries in a query involving UNION or UNION ALL in Impala 2.1.0 and higher,
currently you cannot construct a union of two subqueries (for example, in the argument of an IN or EXISTS
operator).
• Subqueries returning scalar values cannot be used with the operators ANY or ALL. (Impala does not currently have
a SOME operator, but if it did, the same restriction would apply.)
• For the EXISTS and NOT EXISTS clauses, any subquery comparing values from the outer query block to
another table must use at least one equality comparison, not exclusively other kinds of comparisons such as less
than, greater than, BETWEEN, or !=.
• Currently, a scalar subquery cannot be used as the first or second argument to the BETWEEN operator.
• A subquery cannot be used inside an OR conjunction. Expressions inside a subquery, for example in the WHERE
clause, can use OR conjunctions; the restriction only applies to parts of the query “above” the subquery.
• Scalar subqueries are only supported in numeric contexts. You cannot use a scalar subquery as an argument to
the LIKE, REGEXP, or RLIKE operators, or compare it to a value of a non-numeric type such as TIMESTAMP or
BOOLEAN.
• You cannot use subqueries with the CASE function to generate the comparison value, the values to be compared
against, or the return value.
• A subquery is not allowed in the filter condition for the HAVING clause. (Strictly speaking, a subquery cannot
appear anywhere outside the WITH, FROM, and WHERE clauses.)
| Impala SQL Language Reference | 328
TABLESAMPLE Clause
Specify the TABLESAMPLE clause in cases where you need to explore the data distribution within the table, the table
is very large, and it is impractical or unnecessary to process all the data from the table or selected partitions.
The clause makes the query process a randomized set of data files from the table, so that the total volume of data is
greater than or equal to the specified percentage of data bytes within that table. (Or the data bytes within the set of
partitions that remain after partition pruning is performed.)
Syntax:
The TABLESAMPLE clause comes immediately after a table name or table alias.
The SYSTEM keyword represents the sampling method. Currently, Impala only supports a single sampling method
named SYSTEM.
The percentage argument is an integer literal from 0 to 100. A percentage of 0 produces an empty result set for a
particular table reference, while a percentage of 100 uses the entire contents. Because the sampling works by selecting
a random set of data files, the proportion of sampled data from the table may be greater than the specified percentage,
based on the number and sizes of the underlying data files. See the usage notes for details.
The optional REPEATABLE keyword lets you specify an arbitrary positive integer seed value that ensures that when
the query is run again, the sampling selects the same set of data files each time. REPEATABLE does not have a
default value. If you omit the REPEATABLE keyword, the random seed is derived from the current time.
Added in: Impala 2.9.0
See COMPUTE STATS Statement for the TABLESAMPLE clause used in the COMPUTE STATS statement.
Usage notes:
You might use this clause with aggregation queries, such as finding the approximate average, minimum, or maximum
where exact precision is not required. You can use these findings to plan the most effective strategy for constructing
queries against the full table or designing a partitioning strategy for the data.
Some other database systems have a TABLESAMPLE clause. The Impala syntax for this clause is modeled on the
syntax for popular relational databases, not the Hive TABLESAMPLE clause. For example, there is no BUCKETS
keyword as in HiveQL.
The precision of the percentage threshold depends on the number and sizes of the underlying data files. Impala brings
in additional data files, one at a time, until the number of bytes exceeds the specified percentage based on the total
| Impala SQL Language Reference | 329
number of bytes for the entire set of table data. The precision of the percentage threshold is higher when the table
contains many data files with consistent sizes. See the code listings later in this section for examples.
When you estimate characteristics of the data distribution based on sampling a percentage of the table data, be aware
that the data might be unevenly distributed between different files. Do not assume that the percentage figure reflects
the percentage of rows in the table. For example, one file might contain all blank values for a STRING column, while
another file contains long strings in that column; therefore, one file could contain many more rows than another.
Likewise, a table created with the SORT BY clause might contain narrow ranges of values for the sort columns,
making it impractical to extrapolate the number of distinct values for those columns based on sampling only some of
the data files.
Because a sample of the table data might not contain all values for a particular column, if the TABLESAMPLE is used
in a join query, the key relationships between the tables might produce incomplete result sets compared to joins using
all the table data. For example, if you join 50% of table A with 50% of table B, some values in the join columns might
not match between the two tables, even though overall there is a 1:1 relationship between the tables.
The REPEATABLE keyword makes identical queries use a consistent set of data files when the query is repeated.
You specify an arbitrary integer key that acts as a seed value when Impala randomly selects the set of data files to
use in the query. This technique lets you verify correctness, examine performance, and so on for queries using the
TABLESAMPLE clause without the sampled data being different each time. The repeatable aspect is reset (that is, the
set of selected data files may change) any time the contents of the table change. The statements or operations that can
make sampling results non-repeatable are:
• INSERT.
• TRUNCATE TABLE.
• LOAD DATA.
• REFRESH or INVALIDATE METADATA after files are added or removed by a non-Impala mechanism.
•
This clause is similar in some ways to the LIMIT clause, because both serve to limit the size of the intermediate data
and final result set. LIMIT 0 is more efficient than TABLESAMPLE SYSTEM(0) for verifying that a query can
execute without producing any results. TABLESAMPLE SYSTEM(n) often makes query processing more efficient
than using a LIMIT clause by itself, because all phases of query execution use less data overall. If the intent is to
retrieve some representative values from the table in an efficient way, you might combine TABLESAMPLE, ORDER
BY, and LIMIT clauses within a single query.
Partitioning:
When you query a partitioned table, any partition pruning happens before Impala selects the data files to sample. For
example, in a table partitioned by year, a query with WHERE year = 2017 and a TABLESAMPLE SYSTEM(10)
clause would sample data files representing at least 10% of the bytes present in the 2017 partition.
Amazon S3 considerations:
This clause applies to S3 tables the same way as tables with data files stored on HDFS.
ADLS considerations:
This clause applies to ADLS tables the same way as tables with data files stored on HDFS.
Kudu considerations:
This clause does not apply to Kudu tables.
HBase considerations:
This clause does not apply to HBase tables.
Performance considerations:
From a performance perspective, the TABLESAMPLE clause is especially valuable for exploratory queries on text,
Avro, or other file formats other than Parquet. Text-based or row-oriented file formats must process substantial
amounts of redundant data for queries that derive aggregate results such as MAX(), MIN(), or AVG() for a single
| Impala SQL Language Reference | 330
column. Therefore, you might use TABLESAMPLE early in the ETL pipeline, when data is still in raw text format and
has not been converted to Parquet or moved into a partitioned table.
Restrictions:
This clause applies only to tables that use a storage layer with underlying raw data files, such as HDFS, Amazon S3,
or Microsoft ADLS.
This clause does not apply to table references that represent views. A query that applies the TABLESAMPLE clause to
a view or a subquery fails with a semantic error.
Because the sampling works at the level of entire data files, it is by nature coarse-grained. It is possible to specify a
small sample percentage but still process a substantial portion of the table data if the table contains relatively few data
files, if each data file is very large, or if the data files vary substantially in size. Be sure that you understand the data
distribution and physical file layout so that you can verify if the results are suitable for extrapolation. For example,
if the table contains only a single data file, the “sample” will consist of all the table data regardless of the percentage
you specify. If the table contains data files of 1 GiB, 1 GiB, and 1 KiB, when you specify a sampling percentage of
50 you would either process slightly more than 50% of the table (1 GiB + 1 KiB) or almost the entire table (1 GiB + 1
GiB), depending on which data files were selected for sampling.
If data files are added by a non-Impala mechanism, and the table metadata is not updated by a REFRESH or
INVALIDATE METADATA statement, the TABLESAMPLE clause does not consider those new files when computing
the number of bytes in the table or selecting which files to sample.
If data files are removed by a non-Impala mechanism, and the table metadata is not updated by a REFRESH or
INVALIDATE METADATA statement, the query fails if the TABLESAMPLE clause attempts to reference any of the
missing files.
Examples:
The following examples demonstrate the TABLESAMPLE clause. These examples intentionally use very small
data sets to illustrate how the number of files, size of each file, and overall size of data in the table interact with the
percentage specified in the clause.
These examples use an unpartitioned table, containing several files of roughly the same size:
A query that samples 50% of the table must process at least 17 bytes of data. Based on the sizes of the data files, we
can predict that each such query uses 3 arbitrary files. Any 1 or 2 files are not enough to reach 50% of the total data in
the table (34 bytes), so the query adds more files until it passes the 50% threshold:
To help run reproducible experiments, the REPEATABLE clause causes Impala to choose the same set of files for
each query. Although the data set being considered is deterministic, the order of results varies (in the absence of an
ORDER BY clause) because of the way distributed queries are processed:
The following examples show how uneven data distribution affects which data is sampled. Adding another data file
containing a long string value changes the threshold for 50% of the total data in the table:
Even though the queries do not refer to the S column containing the long value, all the sampling queries include the
data file containing the column value X=1000, because the query cannot reach the 50% threshold (115 bytes) without
including that file. The large file might be considered first, in which case it is the only file processed by the query. Or
an arbitrary set of other files might be considered first.
The following examples demonstrate how the TABLESAMPLE clause interacts with other table aspects, such as
partitioning and file format:
If the query does not involve any partition pruning, the sampling applies to the data volume of the entire table:
-- 18 rows total.
select count(*) from sample_demo_partitions;
+----------+
| count(*) |
+----------+
| 18 |
+----------+
If the query only processes certain partitions, the query computes the sampling threshold based on the data size and
set of files only from the relevant partitions:
Related information:
SELECT Statement on page 306
WITH Clause
A clause that can be added before a SELECT statement, to define aliases for complicated expressions that are
referenced multiple times within the body of the SELECT. Similar to CREATE VIEW, except that the table and
column names defined in the WITH clause do not persist after the query finishes, and do not conflict with names used
in actual tables or views. Also known as “subquery factoring”.
You can rewrite a query using subqueries to work the same as with the WITH clause. The purposes of the WITH
clause are:
• Convenience and ease of maintenance from less repetition with the body of the query. Typically used with queries
involving UNION, joins, or aggregation functions where the similar complicated expressions are referenced
multiple times.
• SQL code that is easier to read and understand by abstracting the most complex part of the query into a separate
block.
• Improved compatibility with SQL from other database systems that support the same clause (primarily Oracle
Database).
Note:
The Impala WITH clause does not support recursive queries in the WITH, which is supported in some other
database systems.
Standards compliance: Introduced in SQL:1999.
Examples:
-- Define one subquery at the outer level, and another at the inner level as
part of the
-- initial stage of the UNION ALL query.
with t1 as (select 1) (with t2 as (select 2) select * from t2) union all
select * from t1;
| Impala SQL Language Reference | 335
DISTINCT Operator
The DISTINCT operator in a SELECT statement filters the result set to remove duplicates.
You can use DISTINCT in combination with an aggregation function, typically COUNT(), to find how many
different values a column contains.
Zero-length strings: For purposes of clauses such as DISTINCT and GROUP BY, Impala considers zero-length
strings (""), NULL, and space to all be different values.
Note:
In contrast with some database systems that always return DISTINCT values in sorted order, Impala does not do
any ordering of DISTINCT values. Always include an ORDER BY clause if you need the values in alphabetical or
numeric sorted order.
SET Statement
The SET statement specifies values for query options that control the runtime behavior of other statements within the
same session.
When issued in impala-shell, the SET command is interpreted as an impala-shell command that has
differences from the SQL SET statement. See impala-shell Command Reference on page 763 for the information
about the SET command in impala-shell.
Syntax:
SET
SET ALL
SET query_option=option_value
SET query_option=""
SET and SET ALL with no arguments return a result set consisting of all the applicable query options and their
current values.
The query_option and option_value are case-insensitive.
Unlike the impala-shell command version of SET, when used as a SQL statement, the string values for
option_value need to be quoted, e.g. SET option="new_value".
The SET query_option = "" statement unsets the value of the query_option in the current session, reverting it
to the default state. In impala-shell, use the UNSET command to set a query option to it default.
Each query option has a specific allowed notation for its arguments. See Query Options for the SET Statement on
page 336 for the details of each query option.
Usage notes:
In Impala 2.11 and higher, the outputs of the SET and SET ALL statements were reorganized as below:
| Impala SQL Language Reference | 336
• The options are divided into groups: Regular Query Options, Advanced Query Options,
Development Query Options, and Deprecated Query Options.
• The advanced options are intended for use in specific kinds of performance tuning and debugging scenarios.
• The development options are related to internal development of Impala or features that are not yet finalized.
These options might be changed or removed without notice.
• The deprecated options are related to features that are removed or changed so that the options no longer have
any purpose. These options might be removed in future versions.
• By default, only the first two groups, regular and advanced, are displayed by the SET command. Use SET ALL to
see all groups of options.
• impala-shell options and user-specified variables are always displayed at the end of the list of query options,
after all appropriate option groups.
Added in: Impala 2.0.0
SET has always been available as an impala-shell command. Promoting it to a SQL statement lets you use this
feature in client applications through the JDBC and ODBC APIs.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
See Query Options for the SET Statement on page 336 for the query options you can adjust using this statement.
Related information:
SET Statement on page 335
ABORT_ON_ERROR Query Option
When this option is enabled, Impala cancels a query immediately when any of the nodes encounters an error, rather
than continuing and possibly returning incomplete results. This option is disabled by default, to help gather maximum
diagnostic information when an error occurs, for example, whether the same problem occurred on all nodes or only a
single node. Currently, the errors that Impala can skip over involve data corruption, such as a column that contains a
string value when expected to contain an integer value.
To control how much logging Impala does for non-fatal errors when ABORT_ON_ERROR is turned off, use the
MAX_ERRORS option.
Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false
Default: false (shown as 0 in output of SET statement)
Related information:
| Impala SQL Language Reference | 337
MAX_ERRORS Query Option on page 355, Using Impala Logging on page 749
ALLOW_ERASURE_CODED_FILES Query Option
Use the ALLOW_ERASURE_CODED_FILES query option to enable or disable the support of erasure coded files
in Impala. Until Impala is fully tested and certified with erasure coded files, this query option is set to FALSE by
default.
When the ALLOW_ERASURE_CODED_FILES query option is set to FALSE, Impala returns an error when a query
requires scanning an erasure coded file.
Type: BOOLEAN
Default: FALSE
Added in: Impala 3.1
ALLOW_UNSUPPORTED_FORMATS Query Option
Note: This query option was removed in Impala 3.1 and no longer has any effect. Do not use.
Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false
Default: false (shown as 0 in output of SET statement)
APPX_COUNT_DISTINCT Query Option (Impala 2.0 or higher only)
When the APPX_COUNT_DISTINCT query option is set to TRUE, Impala implicitly converts COUNT(DISTINCT)
operations to the NDV() function calls. The resulting count is approximate rather than precise. Enable the query
option when a tolerable amount of error is acceptable in order to obtain faster query results than with a COUNT
(DISTINCT) queries.
Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false
Default: false (shown as 0 in output of SET statement)
Related information:
COUNT Function on page 523, DISTINCT Operator on page 335, NDV Function on page 537
BATCH_SIZE Query Option
Number of rows evaluated at a time by SQL operators. Unspecified or a size of 0 uses a predefined default size. Using
a large number improves responsiveness, especially for scan operations, at the cost of a higher memory footprint.
This option is primarily for testing during Impala development, or for use under the direction of the appropriate
support channel.
Type: numeric
Default: 0 (meaning the predefined default of 1024)
Range: 0-65536. The value of 0 still has the special meaning of “use the default”, so the effective range is 1-65536.
The maximum applies in Impala 2.11 and higher.
BUFFER_POOL_LIMIT Query Option
Defines a limit on the amount of memory that a query can allocate from the internal buffer pool. The value for this
limit applies to the memory on each host, not the aggregate memory across the cluster. Typically not changed by
users, except during diagnosis of out-of-memory errors during queries.
Type: integer
Default:
The default setting for this option is the lower of 80% of the MEM_LIMIT setting, or the MEM_LIMIT setting minus
100 MB.
Added in: Impala 2.10.0
Usage notes:
| Impala SQL Language Reference | 338
If queries encounter out-of-memory errors, consider decreasing the BUFFER_POOL_LIMIT setting to less than 80%
of the MEM_LIMIT setting.
Examples:
Related information:
DEFAULT_SPILLABLE_BUFFER_SIZE Query Option on page 341, MAX_ROW_SIZE Query Option on page
355, MIN_SPILLABLE_BUFFER_SIZE Query Option on page 361, Scalability Considerations for Impala on
page 633
COMPRESSION_CODEC Query Option (Impala 2.0 or higher only)
When Impala writes Parquet data files using the INSERT statement, the underlying compression is controlled by the
COMPRESSION_CODEC query option.
Note: Prior to Impala 2.0, this option was named PARQUET_COMPRESSION_CODEC. In Impala 2.0 and later, the
PARQUET_COMPRESSION_CODEC name is not recognized. Use the more general name COMPRESSION_CODEC
for new code.
Syntax:
The allowed values for this query option are SNAPPY (the default), GZIP, ZSTD, LZ4, and NONE.
ZSTD also supports setting a compression level. The lower the level, the faster the speed at the cost of compression
ratio. Compression levels from 1 up to 22 are supported for ZSTD. The default compression level 3 is used, if one is
not passed using the compression_codec query option.
Note: A Parquet file created with COMPRESSION_CODEC=NONE is still typically smaller than the original data,
due to encoding schemes such as run-length encoding and dictionary encoding that are applied separately from
compression.
set compression_codec=lz4;
insert into parquet_table_lz4_compressed select * from t1;
set compression_codec=gzip;
insert into parquet_table_highly_compressed select * from t1;
set compression_codec=snappy;
insert into parquet_table_compression_plus_fast_queries select * from t1;
set compression_codec=none;
insert into parquet_table_no_compression select * from t1;
set compression_codec=foo;
select * from t1 limit 5;
ERROR: Invalid compression codec: foo
Related information:
For information about how compressing Parquet data files affects query performance, see Compressions for Parquet
Data Files on page 684.
COMPUTE_STATS_MIN_SAMPLE_SIZE Query Option
The COMPUTE_STATS_MIN_SAMPLE_SIZE query option specifies the minimum number of bytes that will be
scanned in COMPUTE STATS TABLESAMPLE, regardless of the user-supplied sampling percent. This query option
prevents sampling for very small tables where accurate stats can be obtained cheaply without sampling because the
minimum sample size is required to get meaningful stats.
Type: integer
Default: 1GB
Added in: Impala 2.12
Usage notes:
DEBUG_ACTION Query Option
Introduces artificial problem conditions within queries. For internal debugging and troubleshooting.
Type: STRING
Default: empty string
DECIMAL_V2 Query Option
A query option that changes behavior related to the DECIMAL data type. Set this option to FALSE for backward
compatibility to Impala 2.x.
Type: Boolean
Default: TRUE
DEFAULT_FILE_FORMAT Query Option
Use the DEFAULT_FILE_FORMAT query option to set the default table file format. The following values are
supported:
• TEXT (0)
• RC_FILE (1)
• SEQUENCE_FILE (2)
• AVRO (3)
• PARQUET (4)
• KUDU (5)
• ORC (6)
In impala-shell or Hue, the SET DEFAULT_FILE_FORMAT statement will not return an error when the option was
set to an unsupported value. Impala validates the value and returns an error when you submitted the next query.
| Impala SQL Language Reference | 340
SET DEFAULT_HINTS_INSERT_STATEMENT=CLUSTERED;
SET DEFAULT_HINTS_INSERT_STATEMENT=SHUFFLE;
SET DEFAULT_HINTS_INSERT_STATEMENT=NOCLUSTERED:NOSHUFFLE;
The default hints apply to the HDFS and Kudu table formats and are ignored for the HBase table format.
Type: STRING
Default: none
Added in: Impala 3.3
DEFAULT_JOIN_DISTRIBUTION_MODE Query Option
This option determines the join distribution that Impala uses when any of the tables involved in a join query is
missing statistics.
Impala optimizes join queries based on the presence of table statistics, which are produced by the Impala COMPUTE
STATS statement. By default, when a table involved in the join query does not have statistics, Impala uses the
“broadcast” technique that transmits the entire contents of the table to all executor nodes participating in the query. If
one table involved in a join has statistics and the other does not, the table without statistics is broadcast. If both tables
are missing statistics, the table that is referenced second in the join order is broadcast. This behavior is appropriate
when the table involved is relatively small, but can lead to excessive network, memory, and CPU overhead if the table
being broadcast is large.
Because Impala queries frequently involve very large tables, and suboptimal joins for such tables could result in
spilling or out-of-memory errors, the setting DEFAULT_JOIN_DISTRIBUTION_MODE=SHUFFLE lets you
override the default behavior. The shuffle join mechanism divides the corresponding rows of each table involved in a
join query using a hashing algorithm, and transmits subsets of the rows to other nodes for processing. Typically, this
kind of join is more efficient for joins between large tables of similar size.
The setting DEFAULT_JOIN_DISTRIBUTION_MODE=SHUFFLE is recommended when setting up and deploying
new clusters, because it is less likely to result in serious consequences such as spilling or out-of-memory errors if
the query plan is based on incomplete information. This setting is not the default, to avoid changing the performance
characteristics of join queries for clusters that are already tuned for their existing workloads.
Type: integer
The allowed values are BROADCAST (equivalent to 0) or SHUFFLE (equivalent to 1).
Examples:
The following examples demonstrate appropriate scenarios for each setting of this query option.
Related information:
COMPUTE STATS Statement on page 223, Joins in Impala SELECT Statements on page 308, Performance
Considerations for Join Queries on page 594
DEFAULT_SPILLABLE_BUFFER_SIZE Query Option
Specifies the default size for a memory buffer used when the spill-to-disk mechanism is activated, for example for
queries against a large table with no statistics, or large join operations.
Type: integer
Default:
2097152 (2 MB)
Units: A numeric argument represents a size in bytes; you can also use a suffix of m or mb for megabytes, or g or gb
for gigabytes. If you specify a value with unrecognized formats, subsequent queries fail with an error.
Added in: Impala 2.10.0
Usage notes:
This query option sets an upper bound on the size of the internal buffer size that can be used during spill-to-disk
operations. The actual size of the buffer is chosen by the query planner.
If overall query performance is limited by the time needed for spilling, consider increasing the
DEFAULT_SPILLABLE_BUFFER_SIZE setting. Larger buffer sizes result in Impala issuing larger I/O requests to
storage devices, which might result in higher throughput, particularly on rotational disks.
The tradeoff with a large value for this setting is increased memory usage during spill-to-disk operations. Reducing
this value may reduce memory consumption.
To determine if the value for this setting is having an effect by capping the spillable buffer size, you can see the buffer
size chosen by the query planner for a particular query. EXPLAIN the query while the setting EXPLAIN_LEVEL=2
is in effect.
| Impala SQL Language Reference | 342
Examples:
set default_spillable_buffer_size=4MB;
Related information:
BUFFER_POOL_LIMIT Query Option on page 337, MAX_ROW_SIZE Query Option on page 355,
MIN_SPILLABLE_BUFFER_SIZE Query Option on page 361, Scalability Considerations for Impala on page
633
DEFAULT_TRANSACTIONAL_TYPE Query Option
Use the DEFAULT_TRANSACTION_TYPE query option to create insert-only transactional tables by default.
Note: The DEFAULT_TRANSACTION_TYPE query option applies only when integrated with Hive 3.
The supported values are:
• NONE: The table will not be created as transactional.
• INSERT_ONLY: The table will be created as transactional. See Impala Transactions on page 204 for details.
If either table properties, transactional or transactional_properties, are set, this query option is
ignored.
Note: The DEFAULT_TRANSACTION_TYPE query option does not affect external, Kudu, or HBase tables as these
cannot be transactional.
Type: Enum as specified above
Default: NONE
Added in: Impala 3.3
DISABLE_CODEGEN Query Option
The DISABLE_CODEGEN is a debug option, and it's used to work around any issues with Impala's runtime
code generation. If a query fails with an “illegal instruction” or other hardware-specific message, try
setting DISABLE_CODEGEN=true and running the query again. If the query succeeds only when the
DISABLE_CODEGEN option is turned on, submit the problem to the appropriate support channel and include that
detail in the problem report.
Most queries will run significantly slower with DISABLE_CODEGEN=true.
In Impala 2.10 and higher, the DISABLE_CODEGEN_ROWS_THRESHOLD optimisation automatically disables
codegen for small queries because short-running queries may run faster without the overhead of codegen.
The following values are supported:
• TRUE or 1: Disables codegen.
• FALSE or 0: Enables codegen.
Type: Boolean
Default: false (shown as 0 in output of SET statement)
DISABLE_CODEGEN_ROWS_THRESHOLD Query Option (Impala 2.10 or higher only)
This setting controls the cutoff point (in terms of number of rows processed per Impala daemon) below which Impala
disables native code generation for the whole query. Native code generation is very beneficial for queries that process
many rows because it reduces the time taken to process of each row. However, generating the native code adds
latency to query startup. Therefore, automatically disabling codegen for queries that process relatively small amounts
of data can improve query response time.
| Impala SQL Language Reference | 343
Syntax:
SET DISABLE_CODEGEN_ROWS_THRESHOLD=number_of_rows
Type: numeric
Default: 50000
Usage notes: Typically, you increase the default value to make this optimization apply to more queries. If incorrect
or corrupted table and column statistics cause Impala to apply this optimization incorrectly to queries that actually
involve substantial work, you might see the queries being slower as a result of codegen being disabled. In that case,
recompute statistics with the COMPUTE STATS or COMPUTE INCREMENTAL STATS statement. If there is a
problem collecting accurate statistics, you can turn this feature off by setting the value to 0.
Internal details:
This setting applies to queries where the number of rows processed can be accurately determined, either through table
and column statistics, or by the presence of a LIMIT clause. If Impala cannot accurately estimate the number of rows,
then this setting does not apply.
If a query uses the complex data types STRUCT, ARRAY, or MAP, then codegen is never automatically disabled
regardless of the DISABLE_CODEGEN_ROWS_THRESHOLD setting.
Added in: Impala 2.10.0
DISABLE_ROW_RUNTIME_FILTERING Query Option (Impala 2.5 or higher only)
The DISABLE_ROW_RUNTIME_FILTERING query option reduces the scope of the runtime filtering feature.
Queries still dynamically prune partitions, but do not apply the filtering logic to individual rows within partitions.
Only applies to queries against Parquet tables. For other file formats, Impala only prunes at the level of partitions, not
individual rows.
Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false
Default: false
Added in: Impala 2.5.0
Usage notes:
Impala automatically evaluates whether the per-row filters are being effective at reducing the amount of intermediate
data. Therefore, this option is typically only needed for the rare case where Impala cannot accurately determine how
effective the per-row filtering is for a query.
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Because this setting only improves query performance in very specific circumstances, depending on the query
characteristics and data distribution, only use it when you determine through benchmarking that it improves
performance of specific expensive queries. Consider setting this query option immediately before the expensive query
and unsetting it immediately afterward.
File format considerations:
This query option only applies to queries against HDFS-based tables using the Parquet file format.
Kudu considerations:
When applied to a query involving a Kudu table, this option turns off all runtime filtering for the Kudu table.
Related information:
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617, RUNTIME_FILTER_MODE Query
Option (Impala 2.5 or higher only) on page 376
| Impala SQL Language Reference | 344
SET EXEC_SINGLE_NODE_ROWS_THRESHOLD=number_of_rows
Type: numeric
Default: 100
Usage notes: Typically, you increase the default value to make this optimization apply to more queries. If incorrect
or corrupted table and column statistics cause Impala to apply this optimization incorrectly to queries that actually
involve substantial work, you might see the queries being slower as a result of remote reads. In that case, recompute
statistics with the COMPUTE STATS or COMPUTE INCREMENTAL STATS statement. If there is a problem
collecting accurate statistics, you can turn this feature off by setting the value to -1.
Internal details:
This setting applies to queries where the number of rows processed can be accurately determined, either through table
and column statistics, or by the presence of a LIMIT clause. If Impala cannot accurately estimate the number of rows,
then this setting does not apply.
In Impala 2.3 and higher, where Impala supports the complex data types STRUCT, ARRAY, and MAP, if a query
refers to any column of those types, the small-query optimization is turned off for that query regardless of the
EXEC_SINGLE_NODE_ROWS_THRESHOLD setting.
For a query that is determined to be “small”, all work is performed on the coordinator node. This might result in some
I/O being performed by remote reads. The savings from not distributing the query work and not generating native
code are expected to outweigh any overhead from the remote reads.
Added in: Impala 2.10
Examples:
A common use case is to query just a few rows from a table to inspect typical data values. In this example, Impala
does not parallelize the query or perform native code generation because the result set is guaranteed to be smaller than
the threshold value from this query option:
SET EXEC_SINGLE_NODE_ROWS_THRESHOLD=500;
SELECT * FROM enormous_table LIMIT 300;
cancellation until the client has fetched all result rows. DML queries are eligible for automatic cancellation until the
DML statement has finished.
Syntax:
SET EXEC_TIME_LIMIT_S=seconds;
Type: numeric
Default: 0 (no time limit )
Added in: Impala 2.12
Related information:
Setting Timeout Periods for Daemons, Queries, and Sessions on page 65
EXPLAIN_LEVEL Query Option
Controls the amount of detail provided in the output of the EXPLAIN statement. The basic output can help you
identify high-level performance issues such as scanning a higher volume of data or more partitions than you expect.
The higher levels of detail show how intermediate results flow between nodes and how different SQL operations such
as ORDER BY, GROUP BY, joins, and WHERE clauses are implemented within a distributed query.
Type: STRING or INT
Default: 1
Arguments:
The allowed range of numeric values for this option is 0 to 3:
• 0 or MINIMAL: A barebones list, one line per operation. Primarily useful for checking the join order in very long
queries where the regular EXPLAIN output is too long to read easily.
• 1 or STANDARD: The default level of detail, showing the logical way that work is split up for the distributed
query.
• 2 or EXTENDED: Includes additional detail about how the query planner uses statistics in its decision-making
process, to understand how a query could be tuned by gathering statistics, using query hints, adding or removing
predicates, and so on. In Impala 3.2 and higher, the output also includes the analyzed query with the cast
information in the output header, and the implicit cast info in the Predicate section.
• 3 or VERBOSE: The maximum level of detail, showing how work is split up within each node into “query
fragments” that are connected in a pipeline. This extra detail is primarily useful for low-level performance testing
and tuning within Impala itself, rather than for rewriting the SQL code at the user level.
Note: Prior to Impala 1.3, the allowed argument range for EXPLAIN_LEVEL was 0 to 1: level 0 had the mnemonic
NORMAL, and level 1 was VERBOSE. In Impala 1.3 and higher, NORMAL is not a valid mnemonic value, and
VERBOSE still applies to the highest level of detail but now corresponds to level 3. You might need to adjust the
values if you have any older impala-shell script files that set the EXPLAIN_LEVEL query option.
Changing the value of this option controls the amount of detail in the output of the EXPLAIN statement. The
extended information from level 2 or 3 is especially useful during performance tuning, when you need to confirm
whether the work for the query is distributed the way you expect, particularly for the most resource-intensive
operations such as join queries against large tables, queries against tables with large numbers of partitions, and insert
operations for Parquet tables. The extended information also helps to check estimated resource usage when you use
the admission control or resource management features explained in Resource Management on page 650. See
EXPLAIN Statement on page 281 for the syntax of the EXPLAIN statement, and Using the EXPLAIN Plan for
Performance Tuning on page 631 for details about how to use the extended information.
Usage notes:
As always, read the EXPLAIN output from bottom to top. The lowest lines represent the initial work of the query
(scanning data files), the lines in the middle represent calculations done on each node and how intermediate results
are transmitted from one node to another, and the topmost lines represent the final results being sent back to the
coordinator node.
| Impala SQL Language Reference | 347
The numbers in the left column are generated internally during the initial planning phase and do not represent the
actual order of operations, so it is not significant if they appear out of order in the EXPLAIN output.
At all EXPLAIN levels, the plan contains a warning if any tables in the query are missing statistics. Use the
COMPUTE STATS statement to gather statistics for each table and suppress this warning. See Table and Column
Statistics on page 601 for details about how the statistics help query performance.
The PROFILE command in impala-shell always starts with an explain plan showing full detail, the same as
with EXPLAIN_LEVEL=3. After the explain plan comes the executive summary, the same output as produced by the
SUMMARY command in impala-shell.
Examples:
These examples use a trivial, empty table to illustrate how the essential aspects of query planning are shown in
EXPLAIN output:
As the warning message demonstrates, most of the information needed for Impala to do efficient query planning, and
for you to understand the performance characteristics of the query, requires running the COMPUTE STATS statement
for the table:
+------------------------------------------------------------------------+
Joins and other complicated, multi-part queries are the ones where you most commonly need to examine the
EXPLAIN output and customize the amount of detail in the output. This example shows the default EXPLAIN output
for a three-way join query, then the equivalent output with a [SHUFFLE] hint to change the join mechanism between
the first two tables from a broadcast join to a shuffle join.
| 05:EXCHANGE [PARTITION=HASH(one.x)] |
| | |
| 00:SCAN HDFS [explain_plan.t1 one] |
| partitions=1/1 size=0B |
+---------------------------------------------------------+
For a join involving many different tables, the default EXPLAIN output might stretch over several pages, and the
only details you care about might be the join order and the mechanism (broadcast or shuffle) for joining each pair of
tables. In that case, you might set EXPLAIN_LEVEL to its lowest value of 0, to focus on just the join order and join
mechanism for each stage. The following example shows how the rows from the first and second joined tables are
hashed and divided among the nodes of the cluster for further filtering; then the entire contents of the third table are
broadcast to all nodes for the final stage of join processing.
new queries. Once a session is expired, you cannot issue any new query requests to it. The session remains open, but
the only operation you can perform is to close it.
The IDLE_SESSION_TIMEOUT query option overrides the ‑‑idle_session_timeout startup option. See Setting
Timeout Periods for Daemons, Queries, and Sessions on page 65 for the ‑‑idle_session_timeout startup option.
The IDLE_SESSION_TIMEOUT query option allows JDBC/ODBC connections to set the session timeout as a query
option with the SET statement.
Syntax:
SET IDLE_SESSION_TIMEOUT=seconds;
Type: numeric
Default: 0
• If ‑‑idle_session_timeout is not set, the session never expires.
• If ‑‑idle_session_timeout is set, use that timeout value.
Added in: Impala 2.12
Related information:
Setting Timeout Periods for Daemons, Queries, and Sessions on page 65
KUDU_READ_MODE Query Option (Impala 3.1 or higher only)
The KUDU_READ_MODE query option allows you to set a desired consistency level for scans of Kudu tables.
Type: String
Default: "DEFAULT"
Added in: Impala 3.1
Usage notes:
The following values are supported for the query option:
• "DEFAULT": The value of the startup flag, ‑‑kudu_read_mode, is used.
• "READ_LATEST": Commonly known as the Read Committed isolation mode, in this mode, Kudu provides no
consistency guarantees for this mode, except that all returned rows were committed at some point.
• "READ_AT_SNAPSHOT": Kudu will take a snapshot of the current state of the data and perform the scan
over the snapshot, possibly after briefly waiting for ongoing writes to complete. This provides "Read Your
Writes" consistency within a single Impala session, except in the case of a Kudu leader change. See the Kudu
documentation for more details.
LIVE_PROGRESS Query Option (Impala 2.3 or higher only)
When the LIVE_PROGRESS query option is set to TRUE, Impala displays an interactive progress bar showing
roughly what percentage of processing has been completed for queries submitted through the impala-shell
command. When the query finishes, the progress bar is erased from the impala-shell console output.
Starting in Impala 3.1, the summary output also includes the queuing status consisting of whether the query was
queued and what was the latest queuing reason.
Type: Boolean
Default: FALSE (0)
Command-line equivalent:
You can enable this query option within impala-shell by starting the shell with the --live_progress
command-line option. You can still turn this setting off and on again within the shell through the SET command.
Usage notes:
| Impala SQL Language Reference | 352
The output from this query option is printed to standard error. The output is only displayed in interactive mode, that
is, not when the -q or -f options are used.
For a more detailed way of tracking the progress of an interactive query through all phases of processing, see
LIVE_SUMMARY Query Option (Impala 2.3 or higher only) on page 352.
Restrictions:
Because the percentage complete figure is calculated using the number of issued and completed “scan ranges”,
which occur while reading the table data, the progress bar might reach 100% before the query is entirely finished.
For example, the query might do work to perform aggregations after all the table data has been read. If many of
your queries fall into this category, consider using the LIVE_SUMMARY option instead for more granular progress
reporting.
The LIVE_PROGRESS and LIVE_SUMMARY query options currently do not produce any output during COMPUTE
STATS operations.
Because the LIVE_PROGRESS and LIVE_SUMMARY query options are available only within the impala-shell
interpreter:
• You cannot change these query options through the SQL SET statement using the JDBC or ODBC interfaces. The
SET command in impala-shell recognizes these names as shell-only options.
• Be careful when using impala-shell on a pre-Impala 2.3 system to connect to a system running Impala 2.3 or
higher. The older impala-shell does not recognize these query option names. Upgrade impala-shell on
the systems where you intend to use these query options.
• Likewise, the impala-shell command relies on some information only available in Impala 2.3 and higher to
prepare live progress reports and query summaries. The LIVE_PROGRESS and LIVE_SUMMARY query options
have no effect when impala-shell connects to a cluster running an older version of Impala.
Added in: Impala 2.3.0
Examples:
To see how the LIVE_PROGRESS and LIVE_SUMMARY query options work in real time, see this animated demo.
LIVE_SUMMARY Query Option (Impala 2.3 or higher only)
When the LIVE_SUMMARY query option is set to TRUE, Impala displays the same output as the SUMMARY command
for queries submitted through the impala-shell command, with the measurements updated in real time as the
query progresses. When the query finishes, the final SUMMARY output remains visible in the impala-shell
console output.
Starting in Impala 3.1, the summary output also includes the queuing status consisting of whether the query was
queued and what was the latest queuing reason.
Type: Boolean
Default: FALSE (0)
| Impala SQL Language Reference | 353
Command-line equivalent:
You can enable this query option within impala-shell by starting the shell with the --live_summary
command-line option. You can still turn this setting off and on again within the shell through the SET command.
Usage notes:
The live summary output can be useful for evaluating long-running queries, to evaluate which phase of execution
takes up the most time, or if some hosts take much longer than others for certain operations, dragging overall
performance down. By making the information available in real time, this feature lets you decide what action to take
even before you cancel a query that is taking much longer than normal.
For example, you might see the HDFS scan phase taking a long time, and therefore revisit performance-related
aspects of your schema design such as constructing a partitioned table, switching to the Parquet file format, running
the COMPUTE STATS statement for the table, and so on. Or you might see a wide variation between the average
and maximum times for all hosts to perform some phase of the query, and therefore investigate if one particular host
needed more memory or was experiencing a network problem.
The output from this query option is printed to standard error. The output is only displayed in interactive mode, that
is, not when the -q or -f options are used.
For a simple and concise way of tracking the progress of an interactive query, see LIVE_PROGRESS Query Option
(Impala 2.3 or higher only) on page 351.
Restrictions:
The LIVE_PROGRESS and LIVE_SUMMARY query options currently do not produce any output during COMPUTE
STATS operations.
Because the LIVE_PROGRESS and LIVE_SUMMARY query options are available only within the impala-shell
interpreter:
• You cannot change these query options through the SQL SET statement using the JDBC or ODBC interfaces. The
SET command in impala-shell recognizes these names as shell-only options.
• Be careful when using impala-shell on a pre-Impala 2.3 system to connect to a system running Impala 2.3 or
higher. The older impala-shell does not recognize these query option names. Upgrade impala-shell on
the systems where you intend to use these query options.
• Likewise, the impala-shell command relies on some information only available in Impala 2.3 and higher to
prepare live progress reports and query summaries. The LIVE_PROGRESS and LIVE_SUMMARY query options
have no effect when impala-shell connects to a cluster running an older version of Impala.
Added in: Impala 2.3.0
Examples:
The following example shows a series of LIVE_SUMMARY reports that are displayed during the course of a query,
showing how the numbers increase to show the progress of different phases of the distributed query. When you do
the same in impala-shell, only a single report is displayed at any one time, with each update overwriting the
previous numbers.
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
| Operator | #Hosts | Avg Time | Max Time | #Rows | Est. #Rows
| Peak Mem | Est. Peak Mem | Detail |
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
| 06:AGGREGATE | 0 | 0ns | 0ns | 0 | 1
| 0 B | -1 B | FINALIZE |
| 05:EXCHANGE | 0 | 0ns | 0ns | 0 | 1
| 0 B | -1 B | UNPARTITIONED |
| 03:AGGREGATE | 1 | 0ns | 0ns | 0 | 1
| 20.00 KB | 10.00 MB | |
| 02:NESTED LOOP JOIN | 1 | 17.62s | 17.62s | 81.14M | 22.50B
| 3.23 MB | 0 B | CROSS JOIN, BROADCAST |
| |--04:EXCHANGE | 1 | 26.29ms | 26.29ms | 150.00K | 150.00K
| 0 B | 0 B | BROADCAST |
| | 01:SCAN HDFS | 1 | 503.57ms | 503.57ms | 150.00K | 150.00K
| 24.09 MB | 64.00 MB | tpch.customer t2 |
| 00:SCAN HDFS | 1 | 247.53ms | 247.53ms | 1.02K | 150.00K
| 24.39 MB | 64.00 MB | tpch.customer t1 |
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
| Operator | #Hosts | Avg Time | Max Time | #Rows | Est. #Rows
| Peak Mem | Est. Peak Mem | Detail |
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
| 06:AGGREGATE | 0 | 0ns | 0ns | 0 | 1
| 0 B | -1 B | FINALIZE |
| 05:EXCHANGE | 0 | 0ns | 0ns | 0 | 1
| 0 B | -1 B | UNPARTITIONED |
| 03:AGGREGATE | 1 | 0ns | 0ns | 0 | 1
| 20.00 KB | 10.00 MB | |
| 02:NESTED LOOP JOIN | 1 | 61.85s | 61.85s | 283.43M | 22.50B
| 3.23 MB | 0 B | CROSS JOIN, BROADCAST |
| |--04:EXCHANGE | 1 | 26.29ms | 26.29ms | 150.00K | 150.00K
| 0 B | 0 B | BROADCAST |
| | 01:SCAN HDFS | 1 | 503.57ms | 503.57ms | 150.00K | 150.00K
| 24.09 MB | 64.00 MB | tpch.customer t2 |
| 00:SCAN HDFS | 1 | 247.59ms | 247.59ms | 2.05K | 150.00K
| 24.39 MB | 64.00 MB | tpch.customer t1 |
+---------------------+--------+----------+----------+---------+------------
+----------+---------------+-----------------------+
To see how the LIVE_PROGRESS and LIVE_SUMMARY query options work in real time, see this animated demo.
| Impala SQL Language Reference | 355
Specifying a value that is substantially higher than actually needed can cause Impala to reserve more memory than is
necessary to execute the query.
In a Hadoop cluster with highly concurrent workloads and queries that process high volumes of data, traditional SQL
tuning advice about minimizing wasted memory is worth remembering. For example, if a table has STRING columns
where a single value might be multiple megabytes, make sure that the SELECT lists in queries only refer to columns
that are actually needed in the result set, instead of using the SELECT * shorthand.
Examples:
The following examples show the kinds of situations where it is necessary to adjust the MAX_ROW_SIZE setting.
First, we create a table containing some very long values in STRING columns:
-- Turn off compression to more easily reason about data volume by doing
SHOW TABLE STATS.
-- Does not actually affect query success or failure, because MAX_ROW_SIZE
applies when
-- column values are materialized in memory.
set compression_codec=none;
set;
...
MAX_ROW_SIZE: [524288]
...
With the default MAX_ROW_SIZE setting, different queries succeed or fail based on which column values have to be
materialized during query processing:
-- The 5 MiB string is too large to materialize. The message explains the
size of the result
-- set row the query is attempting to materialize.
select count(distinct(s1)) from big_strings;
WARNINGS: Row of size 4.77 MB could not be materialized in plan node with id
1.
| Impala SQL Language Reference | 357
-- If more columns are involved, the result set row being materialized is
bigger.
select count(distinct s1, s2, s3) from big_strings;
WARNINGS: Row of size 9.54 MB could not be materialized in plan node with id
1.
Increase the max_row_size query option (currently 512.00 KB) to process
larger rows.
-- Queries that do not materialize the big column values are OK.
select count(*) from big_strings;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
The following examples show how adjusting MAX_ROW_SIZE upward allows queries involving the long string
columns to succeed:
-- Boosting MAX_ROW_SIZE to larger than the largest row in the table allows
-- all queries to complete successfully.
set max_row_size=12mb;
The following examples show how to reason about appropriate values for MAX_ROW_SIZE, based on the
characteristics of the columns containing the long values:
-- We can also examine the 'Max Size' for each column after computing stats.
compute stats big_strings;
show column stats big_strings;
+--------+--------+------------------+--------+----------+-----------+
| Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
+--------+--------+------------------+--------+----------+-----------+
| s1 | STRING | 2 | -1 | 5000000 | 2500002.5 |
| s2 | STRING | 2 | -1 | 10 | 7.5 |
| s3 | STRING | 2 | -1 | 5000000 | 2500005 |
+--------+--------+------------------+--------+----------+-----------+
Related information:
BUFFER_POOL_LIMIT Query Option on page 337, DEFAULT_SPILLABLE_BUFFER_SIZE Query Option on
page 341, MIN_SPILLABLE_BUFFER_SIZE Query Option on page 361, Scalability Considerations for Impala
on page 633
MAX_NUM_RUNTIME_FILTERS Query Option (Impala 2.5 or higher only)
The MAX_NUM_RUNTIME_FILTERS query option sets an upper limit on the number of runtime filters that can be
produced for each query.
Type: integer
Default: 10
Added in: Impala 2.5.0
Usage notes:
Each runtime filter imposes some memory overhead on the query. Depending on the setting of the
RUNTIME_BLOOM_FILTER_SIZE query option, each filter might consume between 1 and 16 megabytes per plan
fragment. There are typically 5 or fewer filters per plan fragment.
Impala evaluates the effectiveness of each filter, and keeps the ones that eliminate the largest number of partitions
or rows. Therefore, this setting can protect against potential problems due to excessive memory overhead for filter
production, while still allowing a high level of optimization for suitable queries.
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Kudu considerations:
This query option affects only Bloom filters, not the min/max filters that are applied to Kudu tables. Therefore, it does
not affect the performance of queries against Kudu tables.
Related information:
| Impala SQL Language Reference | 359
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617,
RUNTIME_BLOOM_FILTER_SIZE Query Option (Impala 2.5 or higher only) on page 374,
RUNTIME_FILTER_MODE Query Option (Impala 2.5 or higher only) on page 376
MAX_SCAN_RANGE_LENGTH Query Option
Maximum length of the scan range. Interacts with the number of HDFS blocks in the table to determine how many
CPU cores across the cluster are involved with the processing for a query. (Each core processes one scan range.)
Lowering the value can sometimes increase parallelism if you have unused CPU capacity, but a too-small value can
limit query performance because each scan range involves extra overhead.
Only applicable to HDFS tables. Has no effect on Parquet tables. Unspecified or 0 indicates backend default, which is
the same as the HDFS block size for each table.
Although the scan range can be arbitrarily long, Impala internally uses an 8 MB read buffer so that it can query tables
with huge block sizes without allocating equivalent blocks of memory.
Type: numeric
In Impala 2.7 and higher, the argument value can include unit specifiers, such as 100m or 100mb. In previous
versions, Impala interpreted such formatted values as 0, leading to query failures.
Default: 0
MEM_LIMIT Query Option
The MEM_LIMIT query option defines the maximum amount of memory a query can allocate on each node. The
total memory that can be used by a query is the MEM_LIMIT times the number of nodes.
There are two levels of memory limit for Impala. The ‑‑mem_limit startup option sets an overall limit for the
impalad process (which handles multiple queries concurrently). That process memory limit can be expressed either
as a percentage of RAM available to the process such as ‑‑mem_limit=70% or as a fixed amount of memory, such as
100gb. The memory available to the process is based on the host's physical memory and, since Impala 3.2, memory
limits from Linux Control Groups. E.g. if an impalad process is running in a Docker container on a host with
100GB of memory, the memory available is 100GB or the Docker container's memory limit, whichever is less.
The MEM_LIMIT query option, which you set through impala-shell or the SET statement in a JDBC or ODBC
application, applies to each individual query. The MEM_LIMIT query option is usually expressed as a fixed size such
as 10gb, and must always be less than the impalad memory limit.
If query processing exceeds the specified memory limit on any node, either the per-query limit or the impalad limit,
Impala cancels the query automatically. Memory limits are checked periodically during query processing, so the
actual memory in use might briefly exceed the limit without the query being cancelled.
Type: numeric
Units: A numeric argument represents memory size in bytes; you can also use a suffix of m or mb for megabytes, or
more commonly g or gb for gigabytes. If you specify a value with unrecognized formats, subsequent queries fail with
an error.
Default: 0 (unlimited)
Usage notes:
The MEM_LIMIT setting is primarily useful in a high-concurrency setting, or on a cluster with a workload shared
between Impala and other data processing components. You can prevent any query from accidentally using much
more memory than expected, which could negatively impact other Impala queries.
Use the output of the SUMMARY command in impala-shell to get a report of memory used for each phase of
your most heavyweight queries on each node, and then set a MEM_LIMIT somewhat higher than that. See Using the
SUMMARY Report for Performance Tuning on page 632 for usage information about the SUMMARY command.
Examples:
| Impala SQL Language Reference | 360
The following examples show how to set the MEM_LIMIT query option using a fixed number of bytes, or suffixes
representing gigabytes or megabytes.
The following examples show how unrecognized MEM_LIMIT values lead to errors for subsequent queries.
The following examples shows the automatic query cancellation when the MEM_LIMIT value is exceeded on any
host involved in the Impala query. First it runs a successful query and checks the largest amount of memory used on
any node for any stage of the query. Then it sets an artificially low MEM_LIMIT setting so that the same query cannot
run.
set min_spillable_buffer_size=128KB;
Related information:
BUFFER_POOL_LIMIT Query Option on page 337, DEFAULT_SPILLABLE_BUFFER_SIZE Query Option on
page 341, MAX_ROW_SIZE Query Option on page 355, Scalability Considerations for Impala on page 633
MT_DOP Query Option
Sets the degree of intra-node parallelism used for certain operations that can benefit from multithreaded execution.
You can specify values higher than zero to find the ideal balance of response time, memory usage, and CPU usage
during statement processing.
Note:
The Impala execution engine is being revamped incrementally to add additional parallelism within a single host for
certain statements and kinds of operations. The setting MT_DOP=0 uses the “old” code path with limited intra-node
parallelism.
Currently, the operations affected by the MT_DOP query option are:
• COMPUTE [INCREMENTAL] STATS. Impala automatically sets MT_DOP=4 for COMPUTE STATS and
COMPUTE INCREMENTAL STATS statements on Parquet tables.
• Queries with execution plans containing only scan and aggregation operators, or local joins that do not need
data exchanges (such as for nested types). Other queries produce an error if MT_DOP is set to a non-zero value.
Therefore, this query option is typically only set for the duration of specific long-running, CPU-intensive queries.
Type: integer
Default: 0
Because COMPUTE STATS and COMPUTE INCREMENTAL STATS statements for Parquet tables benefit
substantially from extra intra-node parallelism, Impala automatically sets MT_DOP=4 when computing stats for
Parquet tables.
Range: 0 to 64
Examples:
Note:
| Impala SQL Language Reference | 363
Any timing figures in the following examples are on a small, lightly loaded development cluster. Your mileage may
vary. Speedups depend on many factors, including the number of rows, columns, and partitions within each table.
The following example shows how to run a COMPUTE STATS statement against a Parquet table with or without an
explicit MT_DOP setting:
The following example shows the effects of setting MT_DOP for a query involving only scan and aggregation
operations for a Parquet table:
set mt_dop = 0;
MT_DOP set to 0
The following example shows how queries that are not compatible with non-zero MT_DOP settings produce an error
when MT_DOP is set:
set mt_dop=1;
MT_DOP set to 1
Related information:
COMPUTE STATS Statement on page 223, Impala Aggregate Functions on page 516
NUM_NODES Query Option
Limit the number of nodes that process a query, typically during debugging.
Type: numeric
Allowed values: Only accepts the values 0 (meaning all nodes) or 1 (meaning all work is done on the coordinator
node).
Default: 0
Usage notes:
If you are diagnosing a problem that you suspect is due to a timing issue due to distributed query processing, you can
set NUM_NODES=1 to verify if the problem still occurs when all the work is done on a single node.
You might set the NUM_NODES option to 1 briefly, during INSERT or CREATE TABLE AS SELECT statements.
Normally, those statements produce one or more data files per data node. If the write operation involves small
amounts of data, a Parquet table, and/or a partitioned table, the default behavior could produce many small files when
intuitively you might expect only a single output file. SET NUM_NODES=1 turns off the “distributed” aspect of the
write operation, making it more likely to produce only one or a few data files.
Warning:
Because this option results in increased resource utilization on a single host, it could cause problems due to
contention with other Impala statements or high resource usage. Symptoms could include queries running
slowly, exceeding the memory limit, or appearing to hang. Use it only in a single-user development/test
environment; do not use it in a production environment or in a cluster with a high-concurrency or high-
volume or performance-critical workload.
| Impala SQL Language Reference | 365
The following example shows initial schema setup and the default behavior of queries that return just the partition key
column for a table:
-- The aggregation operation means the query does not need to read
-- the data within each partition: the result set contains exactly 1 row
-- per partition, derived from the partition key column value.
-- By default, Impala still includes a 'scan' operation in the query.
explain select distinct year from t1;
+------------------------------------------------------------------------------------
+
| Explain String
|
+------------------------------------------------------------------------------------
+
| Estimated Per-Host Requirements: Memory=0B VCores=0
|
|
|
| 01:AGGREGATE [FINALIZE]
|
| | group by: year
|
| |
|
| 00:SCAN HDFS [key_cols.t1]
|
| partitions=0/0 files=0 size=0B
|
+------------------------------------------------------------------------------------
+
The following examples show how the plan is made more efficient when the
OPTIMIZE_PARTITION_KEY_SCANS option is enabled:
set optimize_partition_key_scans=1;
OPTIMIZE_PARTITION_KEY_SCANS set to 1
ParquetSchemaExampleA {
optional group single_element_groups (LIST) {
repeated group single_element_group {
required int64 count;
}
}
}
SET PARQUET_ARRAY_RESOLUTION=TWO_LEVEL;
SELECT ITEM.f1 FROM t.col1;
| Impala SQL Language Reference | 369
SET PARQUET_ARRAY_RESOLUTION=THREE_LEVEL;
SELECT ITEM FROM t.col1
EXAMPLE B: The following Parquet schema of a file can be only be successfully interpreted as a 2-level:
ParquetSchemaExampleB {
required group list_of_ints (LIST) {
repeated int32 list_of_ints_tuple;
}
}
SET PARQUET_ARRAY_RESOLUTION=TWO_LEVEL;
SELECT ITEM FROM t.col1
Unsuccessful query with a 3-level interpretation. The query returns NULLs as if the column was missing in the file:
SET PARQUET_ARRAY_RESOLUTION=THREE_LEVEL;
SELECT ITEM FROM t.col1
Note that row groups can be filtered out by Parquet statistics, and in such cases, dictionary filtering will not be
considered.
The supported values for the query option are:
• true (1): Use dictionary filtering.
• false (0): Do not use dictionary filtering
• Any other values are treated as false.
Type: Boolean
Default: true (1)
Added in: Impala 2.9.0
PARQUET_FALLBACK_SCHEMA_RESOLUTION Query Option (Impala 2.6 or higher only)
The PARQUET_FALLBACK_SCHEMA_RESOLUTION query option allows Impala to look up columns within
Parquet files by column name, rather than column order, when necessary. The allowed values are:
• POSITION (0)
• NAME (1)
Usage notes:
By default, Impala looks up columns within a Parquet file based on the order of columns in the table.
The name setting for this option enables behavior for Impala queries similar to the Hive setting
parquet.column.index access=false. It also allows Impala to query Parquet files created by Hive with
the parquet.column.index.access=false setting in effect.
Type: integer or string
Added in: Impala 2.6.0
Related information:
Schema Evolution for Parquet Tables on page 692
PARQUET_FILE_SIZE Query Option
Specifies the maximum size of each Parquet data file produced by Impala INSERT statements.
Syntax:
Specify the size in bytes, or with a trailing m or g character to indicate megabytes or gigabytes. For example:
-- 128 megabytes.
set PARQUET_FILE_SIZE=134217728
INSERT OVERWRITE parquet_table SELECT * FROM text_table;
-- 512 megabytes.
set PARQUET_FILE_SIZE=512m;
INSERT OVERWRITE parquet_table SELECT * FROM text_table;
-- 1 gigabyte.
set PARQUET_FILE_SIZE=1g;
INSERT OVERWRITE parquet_table SELECT * FROM text_table;
Usage notes:
With tables that are small or finely partitioned, the default Parquet block size (formerly 1 GB, now 256 MB in Impala
2.0 and later) could be much larger than needed for each data file. For INSERT operations into such tables, you can
increase parallelism by specifying a smaller PARQUET_FILE_SIZE value, resulting in more HDFS blocks that can
be processed by different nodes.
Type: numeric, with optional unit specifier
| Impala SQL Language Reference | 371
Important:
Currently, the maximum value for this setting is 1 gigabyte (1g). Setting a value higher than 1 gigabyte could result
in errors during an INSERT operation.
Default: 0 (produces files with a target size of 256 MB; files might be larger for very wide tables)
Because ADLS does not expose the block sizes of data files the way HDFS does, any Impala INSERT or CREATE
TABLE AS SELECT statements use the PARQUET_FILE_SIZE query option setting to define the size of Parquet
data files. (Using a large block size is more important for Parquet tables than for tables that use other file formats.)
Isilon considerations:
Because the EMC Isilon storage devices use a global value for the block size rather than a configurable value for each
file, the PARQUET_FILE_SIZE query option has no effect when Impala inserts data into a table or partition residing
on Isilon storage. Use the isi command to set the default block size globally on the Isilon device. For example, to set
the Isilon default block size to 256 MB, the recommended size for Parquet data files for Impala, issue the following
command:
Related information:
For information about the Parquet file format, and how the number and size of data files affects query performance,
see Using the Parquet File Format with Impala Tables on page 681.
PARQUET_PAGE_ROW_COUNT_LIMIT Query Option
Use the PARQUET_PAGE_ROW_COUNT_LIMIT query option to set the maximum number of rows that can be
written on a single Parquet data page. By default there is no row count limit.
Type: INT
Allowed values: Positive integers
Added in: Impala 3.3
PARQUET_READ_STATISTICS Query Option (Impala 2.9 or higher only)
The PARQUET_READ_STATISTICS query option controls whether to read statistics from Parquet files and use
them during query processing.
Parquet stores min/max stats which can be used to skip reading row groups if they don't qualify a certain predicate.
When this query option is set to true, Impala reads the Parquet statistics and skips reading row groups that do not
match the conditions in the WHERE clause.
Impala supports filtering based on Parquet statistics:
• Of the numerical types for the old version of the statistics: Boolean, Integer, Float
• Of the types for the new version of the statistics (starting in IMPALA 2.8): Boolean, Integer, Float, Decimal,
String, Timestamp
• For simple predicates of the forms: <slot> <op> <constant> or <constant> <op> <slot>, where
<op> is LT, LE, GE, GT, and EQ
The PARQUET_READ_STATISTICS option provides a workaround when dealing with files that have corrupt
Parquet statistics and unknown errors.
In the query runtime profile output for each Impalad instance, the NumStatsFilteredRowGroups field in the
SCAN node section shows the number of row groups that were skipped based on Parquet statistics.
The supported values for the query option are:
• true (1): Read statistics from Parquet files and use them in query processing.
• false (0): Do not use Parquet read statistics.
• Any other values are treated as false.
| Impala SQL Language Reference | 372
Type: Boolean
Default: true
Added in: Impala 2.9.0
PARQUET_READ_PAGE_INDEX Query Option
Use the PARQUET_READ_PAGE_INDEX query option to disable or enable using the Parquet page index during
scans. The page index contains min/max statistics at the page-level granularity. It can be used to skip pages and rows
that do not match the conditions in the WHERE clause.
This option enables the same optimization as the PARQUET_READ_STATISTICS at the finer grained page level.
Impala supports filtering based on Parquet statistics:
• Of the types: Boolean, Integer, Decimal, String, Timestamp
• For simple predicates of the forms: <slot> <op> <constant> or <constant> <op> <slot>, where
<op> is LT, LE, GE, GT, and EQ
The supported values for the query option are:
• true (1): Read the page-level statistics from the Parquet page index during query processing and filter out pages
based on the statistics.
• false (0): Do not use the Parquet page index.
• Any other values are treated as false.
Type: Boolean
Default: TRUE
PARQUET_WRITE_PAGE_INDEX Query Option
The PARQUET_WRITE_PAGE_INDEX query option to disable or enable the Parquet page index writing.
Impala writes page-level statistics into the Parquet page index of the types: Boolean, Integer, Decimal, String,
Timestamp
The supported values for the query option are:
• true (1): Write the Parquet page index when creating Parquet files.
• false (0): Do not write the Parquet page index when creating Parquet files.
• Any other values are treated as false.
Type: Boolean
Default: TRUE
Added in: Impala 3.3
PREFETCH_MODE Query Option (Impala 2.6 or higher only)
Determines whether the prefetching optimization is applied during join query processing.
Type: numeric (0, 1) or corresponding mnemonic strings (NONE, HT_BUCKET).
Default: 1 (equivalent to HT_BUCKET)
Added in: Impala 2.6.0
Usage notes:
The default mode is 1, which means that hash table buckets are prefetched during join query processing.
Related information:
Joins in Impala SELECT Statements on page 308, Performance Considerations for Join Queries on page 594.
| Impala SQL Language Reference | 373
SET QUERY_TIMEOUT_S=seconds;
Type: numeric
Default: 0 (no timeout if --idle_query_timeout not in effect; otherwise, use --idle_query_timeout
value)
Added in: Impala 2.0.0
Related information:
Setting Timeout Periods for Daemons, Queries, and Sessions on page 65
REPLICA_PREFERENCE Query Option (Impala 2.7 or higher only)
The REPLICA_PREFERENCE query option lets you distribute the work more evenly if hotspots and bottlenecks
persist. It causes the access cost of all replicas of a data block to be considered equal to or worse than the configured
value. This allows Impala to schedule reads to suboptimal replicas (e.g. local in the presence of cached ones) in order
to distribute the work across more executor nodes.
Allowed values are: CACHE_LOCAL (0), DISK_LOCAL (2), REMOTE (4)
Type: Enum
Default: CACHE_LOCAL (0)
Added in: Impala 2.7.0
Usage Notes:
By default Impala selects the best replica it can find in terms of access cost. The preferred order is cached, local,
and remote. With REPLICA_PREFERENCE, the preference of all replicas are capped at the selected value. For
example, when REPLICA_PREFERENCE is set to DISK_LOCAL, cached and local replicas are treated with the
equal preference. When set to REMOTE, all three types of replicas, cached, local, remote, are treated with equal
preference.
Related information:
Using HDFS Caching with Impala (Impala 2.1 or higher only) on page 622, SCHEDULE_RANDOM_REPLICA
Query Option (Impala 2.5 or higher only) on page 378
REQUEST_POOL Query Option
The pool or queue name that queries should be submitted to. Only applies when you enable the Impala admission
control feature. Specifies the name of the pool used by requests from Impala to the resource manager.
Type: STRING
| Impala SQL Language Reference | 374
Default: empty (use the user-to-pool mapping defined by an impalad startup option in the Impala configuration
file)
Related information:
Admission Control and Query Queuing on page 650
RESOURCE_TRACE_RATIO Query Option (Impala 3.2 or higher only)
The RESOURCE_TRACE_RATIO query option specifies the ratio of queries where the CPU usage info will be
included in the profiles. Collecting CPU usage and sending it around adds a slight overhead during query execution.
This query option lets you control whether to collect additional information to diagnose the resource usage.
For example, setting RESOURCE_TRACE_RATIO=1 adds a trace of the CPU usage to the profile of each query.
Setting RESOURCE_TRACE_RATIO=0.5 means that randomly selected half of all queries will have that
information collected by the coordinator and included in the profiles.
Setting RESOURCE_TRACE_RATIO=0 means that CPU usage will not be tracked and included in the profiles.
Values from 0 to 1 are allowed.
Type: Number
Default: 0
Added in: Impala 3.2
RUNTIME_BLOOM_FILTER_SIZE Query Option (Impala 2.5 or higher only)
Size (in bytes) of Bloom filter data structure used by the runtime filtering feature.
Important:
In Impala 2.6 and higher, this query option only applies as a fallback, when statistics are not available. By default,
Impala estimates the optimal size of the Bloom filter structure regardless of the setting for this option. (This is a
change from the original behavior in Impala 2.5.)
In Impala 2.6 and higher, when the value of this query option is used for query planning, it is
constrained by the minimum and maximum sizes specified by the RUNTIME_FILTER_MIN_SIZE and
RUNTIME_FILTER_MAX_SIZE query options. The filter size is adjusted upward or downward if necessary to fit
within the minimum/maximum range.
Type: integer
Default: 1048576 (1 MB)
Maximum: 16 MB
Added in: Impala 2.5.0
Usage notes:
This setting affects optimizations for large and complex queries, such as dynamic partition pruning for partitioned
tables, and join optimization for queries that join large tables. Larger filters are more effective at handling higher
cardinality input sets, but consume more memory per filter.
If your query filters on high-cardinality columns (for example, millions of different values) and you do not get the
expected speedup from the runtime filtering mechanism, consider doing some benchmarks with a higher value for
RUNTIME_BLOOM_FILTER_SIZE. The extra memory devoted to the Bloom filter data structures can help make
the filtering more accurate.
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Because the effectiveness of this setting depends so much on query characteristics and data distribution, you typically
only use it for specific queries that need some extra tuning, and the ideal value depends on the query. Consider setting
this query option immediately before the expensive query and unsetting it immediately afterward.
| Impala SQL Language Reference | 375
Kudu considerations:
This query option affects only Bloom filters, not the min/max filters that are applied to Kudu tables. Therefore, it does
not affect the performance of queries against Kudu tables.
Related information:
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617, RUNTIME_FILTER_MODE Query
Option (Impala 2.5 or higher only) on page 376, RUNTIME_FILTER_MIN_SIZE Query Option (Impala 2.6 or
higher only) on page 375, RUNTIME_FILTER_MAX_SIZE Query Option (Impala 2.6 or higher only) on page
375
RUNTIME_FILTER_MAX_SIZE Query Option (Impala 2.6 or higher only)
The RUNTIME_FILTER_MAX_SIZE query option adjusts the settings for the runtime filtering feature. This option
defines the maximum size for a filter, no matter what the estimates produced by the planner are. This value also
overrides any lower number specified for the RUNTIME_BLOOM_FILTER_SIZE query option. Filter sizes are
rounded up to the nearest power of two.
Type: integer
Default: 0 (meaning use the value from the corresponding impalad startup option)
Added in: Impala 2.6.0
Usage notes:
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Kudu considerations:
This query option affects only Bloom filters, not the min/max filters that are applied to Kudu tables. Therefore, it does
not affect the performance of queries against Kudu tables.
Related information:
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617, RUNTIME_FILTER_MODE Query
Option (Impala 2.5 or higher only) on page 376, RUNTIME_FILTER_MIN_SIZE Query Option (Impala 2.6 or
higher only) on page 375, RUNTIME_BLOOM_FILTER_SIZE Query Option (Impala 2.5 or higher only) on page
374
RUNTIME_FILTER_MIN_SIZE Query Option (Impala 2.6 or higher only)
The RUNTIME_FILTER_MIN_SIZE query option adjusts the settings for the runtime filtering feature. This option
defines the minimum size for a filter, no matter what the estimates produced by the planner are. This value also
overrides any lower number specified for the RUNTIME_BLOOM_FILTER_SIZE query option. Filter sizes are
rounded up to the nearest power of two.
Type: integer
Default: 0 (meaning use the value from the corresponding impalad startup option)
Added in: Impala 2.6.0
Usage notes:
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Kudu considerations:
This query option affects only Bloom filters, not the min/max filters that are applied to Kudu tables. Therefore, it does
not affect the performance of queries against Kudu tables.
Related information:
| Impala SQL Language Reference | 376
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617, RUNTIME_FILTER_MODE Query
Option (Impala 2.5 or higher only) on page 376, RUNTIME_FILTER_MAX_SIZE Query Option (Impala 2.6 or
higher only) on page 375, RUNTIME_BLOOM_FILTER_SIZE Query Option (Impala 2.5 or higher only) on page
374
RUNTIME_FILTER_MODE Query Option (Impala 2.5 or higher only)
The RUNTIME_FILTER_MODE query option adjusts the settings for the runtime filtering feature. It turns this feature
on and off, and controls how extensively the filters are transmitted between hosts.
Type: numeric (0, 1, 2) or corresponding mnemonic strings (OFF, LOCAL, GLOBAL).
Default: 2 (equivalent to GLOBAL); formerly was 1 / LOCAL, in Impala 2.5
Added in: Impala 2.5.0
Usage notes:
In Impala 2.6 and higher, the default is GLOBAL. This setting is recommended for a wide variety of workloads, to
provide best performance with “out of the box” settings.
The lowest setting of LOCAL does a similar level of optimization (such as partition pruning) as in earlier Impala
releases. This setting was the default in Impala 2.5, to allow for a period of post-upgrade testing for existing
workloads. This setting is suitable for workloads with non-performance-critical queries, or if the coordinator node is
under heavy CPU or memory pressure.
You might change the setting to OFF if your workload contains many queries involving partitioned tables or joins that
do not experience a performance increase from the runtime filters feature. If the overhead of producing the runtime
filters outweighs the performance benefit for queries, you can turn the feature off entirely.
Related information:
Partitioning for Impala Tables on page 661 for details about runtime filtering.
DISABLE_ROW_RUNTIME_FILTERING Query Option (Impala 2.5 or higher only) on page 343,
RUNTIME_BLOOM_FILTER_SIZE Query Option (Impala 2.5 or higher only) on page 374,
RUNTIME_FILTER_WAIT_TIME_MS Query Option (Impala 2.5 or higher only) on page 376, and
MAX_NUM_RUNTIME_FILTERS Query Option (Impala 2.5 or higher only) on page 358 for tuning options for
runtime filtering.
RUNTIME_FILTER_WAIT_TIME_MS Query Option (Impala 2.5 or higher only)
The RUNTIME_FILTER_WAIT_TIME_MS query option adjusts the settings for the runtime filtering feature. It
specifies a time in milliseconds that each scan node waits for runtime filters to be produced by other plan fragments.
Type: integer
Default: 0 (meaning use the value from the corresponding impalad startup option)
Added in: Impala 2.5.0
Usage notes:
Because the runtime filtering feature applies mainly to resource-intensive and long-running queries, only adjust this
query option when tuning long-running queries involving some combination of large partitioned tables and joins
involving large tables.
Related information:
Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page 617, RUNTIME_FILTER_MODE Query
Option (Impala 2.5 or higher only) on page 376
S3_SKIP_INSERT_STAGING Query Option (Impala 2.6 or higher only)
Speeds up INSERT operations on tables or partitions residing on the Amazon S3 filesystem. The tradeoff is the
possibility of inconsistent data left behind if an error occurs partway through the operation.
| Impala SQL Language Reference | 377
By default, Impala write operations to S3 tables and partitions involve a two-stage process. Impala writes intermediate
files to S3, then (because S3 does not provide a “rename” operation) those intermediate files are copied to their final
location, making the process more expensive as on a filesystem that supports renaming or moving files. This query
option makes Impala skip the intermediate files, and instead write the new data directly to the final destination.
Usage notes:
Important:
If a host that is participating in the INSERT operation fails partway through the query, you might be left with a table
or partition that contains some but not all of the expected data files. Therefore, this option is most appropriate for a
development or test environment where you have the ability to reconstruct the table if a problem during INSERT
leaves the data in an inconsistent state.
The timing of file deletion during an INSERT OVERWRITE operation makes it impractical to write new files to S3
and delete the old files in a single operation. Therefore, this query option only affects regular INSERT statements that
add to the existing data in a table, not INSERT OVERWRITE statements. Use TRUNCATE TABLE if you need to
remove all contents from an S3 table before performing a fast INSERT with this option enabled.
Performance improvements with this option enabled can be substantial. The speed increase might be more noticeable
for non-partitioned tables than for partitioned tables.
Type: Boolean; recognized values are 1 and 0, or true and false; any other value interpreted as false
Default: true (shown as 1 in output of SET statement)
Added in: Impala 2.6.0
Related information:
Using Impala with the Amazon S3 Filesystem on page 732
SCAN_BYTES_LIMIT Query Option (Impala 3.1 or higher only)
The SCAN_BYTES_LIMIT query option sets a limit on the bytes scanned by HDFS and HBase SCAN operations. If
a query is still executing when the query’s coordinator detects that it has exceeded the limit, the query is terminated
with an error. The option is intended to prevent runaway queries that scan more data than is intended.
For example, an Impala administrator could set a default value of SCAN_BYTES_LIMIT=100GB for a resource pool
to automatically kill queries that scan more than 100 GB of data (see Impala Admission Control and Query Queuing
for information about default query options). If a user accidentally omits a partition filter in a WHERE clause and
runs a large query that scans a lot of data, the query will be automatically terminated after it scans more data than the
SCAN_BYTES_LIMIT.
You can override the default value per-query or per-session, in the same way as other query options, if you do not
want the default SCAN_BYTES_LIMIT value to apply to a specific query or session.
Note:
• Only data actually read from the underlying storage layer is counted towards the limit. E.g. Impala’s Parquet
scanner employs several techniques to skip over data in a file that is not relevant to a specific query, so often only
a fraction of the file size is counted towards SCAN_BYTES_LIMIT.
• As of Impala 3.1, bytes scanned by Kudu tablet servers are not counted towards the limit.
Because the checks are done periodically, the query may scan over the limit at times.
Syntax: SET SCAN_BYTES_LIMIT=bytes;
Type: numeric
Units:
• A numeric argument represents memory size in bytes.
• Specify a suffix of m or mb for megabytes.
• Specify a suffix of g or gb for gigabytes.
• If you specify a suffix with unrecognized formats, subsequent queries fail with an error.
| Impala SQL Language Reference | 378
-- 128 megabytes.
set SCRATCH_LIMIT=134217728
-- 512 megabytes.
set SCRATCH_LIMIT=512m;
-- 1 gigabyte.
set SCRATCH_LIMIT=1g;
Usage notes:
A value of zero turns off the spill to disk feature for queries in the current session, causing them to fail immediately if
they exceed the memory limit.
The amount of memory used per host for a query is limited by the MEM_LIMIT query option.
The more Impala daemon hosts in the cluster, the less memory is used on each host, and therefore also less scratch
space is required for queries that exceed the memory limit.
Type: numeric, with optional unit specifier
Default: -1 (amount of spill space is unlimited)
Related information:
SQL Operations that Spill to Disk on page 635, MEM_LIMIT Query Option on page 359
| Impala SQL Language Reference | 379
time zone can be a canonical code or a time zone name defined in IANA Time Zone Database. The value is case-
sensitive.
Leading/trailing quotes (') and double quotes (") are stripped.
If time zone is an empty string, the time zone for the query is set to the default time zone of the Impalad Coordinator.
If time zone is NULL or a space character, Impala returns an error when the query is executed.
Type: String
Default: The system time zone where the Coordinator Impalad runs
Examples:
SET TIMEZONE=UTC;
SET TIMEZONE="Europe/Budapest";
SET TOPN_BYTES_LIMIT=limit
Type: Number
Default: 536870912 (512 MB)
Added in: Impala 3.1
SHOW Statement
The SHOW statement is a flexible way to get information about different types of Impala objects.
Syntax:
SHOW ROLES
SHOW CURRENT ROLES
SHOW ROLE GRANT GROUP group_name
The following statements are supported only when Impala uses Sentry to manage authorization.
The following statements are supported only when Impala uses Ranger to manage authorization.
Issue a SHOW object_type statement to see the appropriate objects in the current database, or SHOW
object_type IN database_name to see objects in a specific database.
The optional pattern argument is a quoted string literal, using Unix-style * wildcards and allowing | for alternation.
The preceding LIKE keyword is also optional. All object names are stored in lowercase, so use all lowercase letters in
the pattern string. For example:
Note: This statement applies to tables and partitions stored on HDFS, or in the Amazon Simple Storage System (S3).
It does not apply to views. It does not apply to tables mapped onto HBase or Kudu, because those data management
systems do not use the same file-based storage layout.
Usage notes:
You can use this statement to verify the results of your ETL process: that is, that the expected files are present, with
the expected sizes. You can examine the file information to detect conditions such as empty files, missing files, or
inefficient layouts due to a large number of small files. When you use INSERT statements to copy from one table to
another, you can see how the file layout changes due to file format conversions, compaction of small input files into
large data blocks, and multiple output files from parallel queries and partitioned inserts.
The output from this statement does not include files that Impala considers to be hidden or invisible, such as those
whose names start with a dot or an underscore, or that end with the suffixes .copying or .tmp.
The information for partitioned tables complements the output of the SHOW PARTITIONS statement, which
summarizes information about each partition. SHOW PARTITIONS produces some output for each partition, while
SHOW FILES does not produce any output for empty partitions because they do not include any data files.
HDFS permissions:
| Impala SQL Language Reference | 383
The user ID that the impalad daemon runs under, typically the impala user, must have read permission for all the
table files, read and execute permission for all the directories that make up the table, and execute permission for the
database directory and all its parent directories.
Examples:
The following example shows a SHOW FILES statement for an unpartitioned table using text format:
This example illustrates how, after issuing some INSERT ... VALUES statements, the table now contains some
tiny files of just a few bytes. Such small files could cause inefficient processing of parallel queries that are expecting
multi-megabyte input files. The example shows how you might compact the small files by doing an INSERT ...
SELECT into a different table, possibly converting the data to Parquet in the process:
The following example shows a SHOW FILES statement for a partitioned text table with data in two different
partitions, and two empty partitions. The partitions with no data are not represented in the SHOW FILES output.
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=1/80732d9dc80689f_1418645991_data.0. | 5.77MB |
year=2014/month=1/day=1 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=1/80732d9dc8068a0_1418645991_data.0. | 6.25MB |
year=2014/month=1/day=1 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=1/80732d9dc8068a1_147082319_data.0. | 7.16MB |
year=2014/month=1/day=1 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=1/80732d9dc8068a2_2111411753_data.0. | 5.98MB |
year=2014/month=1/day=1 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=2/21a828cf494b5bbb_501271652_data.0. | 6.42MB |
year=2014/month=1/day=2 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=2/21a828cf494b5bbc_501271652_data.0. | 6.62MB |
year=2014/month=1/day=2 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=2/21a828cf494b5bbd_1393490200_data.0. | 6.98MB |
year=2014/month=1/day=2 |
| hdfs://impala_data_dir/show_files.db/part_text/year=2014/month=1/day=2/21a828cf494b5bbe_1393490200_data.0. | 6.20MB |
year=2014/month=1/day=2 |
+---------------------------------------------------------------------------------------------------------+--------
+-------------------------+
The following example shows a SHOW FILES statement for a partitioned Parquet table. The number and sizes of
files are different from the equivalent partitioned text table used in the previous example, because INSERT operations
for Parquet tables are parallelized differently than for text tables. (Also, the amount of data is so small that it can be
written to Parquet without involving all the hosts in this 4-node cluster.)
[localhost:21000] > create table part_parq (x bigint, y int, s string) partitioned by (year bigint, month bigint, day bigint)
stored as parquet;
[localhost:21000] > insert into part_parq partition (year,month,day) select x, y, s, year, month, day from partitioned_text;
[localhost:21000] > show partitions part_parq;
+-------+-------+-----+-------+--------+---------+--------------+-------------------+---------+-------------------+
| year | month | day | #Rows | #Files | Size | Bytes Cached | Cache Replication | Format | Incremental stats |
+-------+-------+-----+-------+--------+---------+--------------+-------------------+---------+-------------------+
| 2014 | 1 | 1 | -1 | 3 | 17.89MB | NOT CACHED | NOT CACHED | PARQUET | false |
| 2014 | 1 | 2 | -1 | 3 | 17.89MB | NOT CACHED | NOT CACHED | PARQUET | false |
| Total | | | -1 | 6 | 35.79MB | 0B | | | |
+-------+-------+-----+-------+--------+---------+--------------+-------------------+---------+-------------------+
[localhost:21000] > show files in part_parq;
+-----------------------------------------------------------------------------------------------+--------
+-------------------------+
| path | size | partition
|
+-----------------------------------------------------------------------------------------------+--------
+-------------------------+
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=1/1134113650_data.0.parq | 4.49MB | year=2014/month=1/
day=1 |
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=1/617567880_data.0.parq | 5.14MB | year=2014/month=1/
day=1 |
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=1/2099499416_data.0.parq | 8.27MB | year=2014/month=1/
day=1 |
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=2/945567189_data.0.parq | 8.80MB | year=2014/month=1/
day=2 |
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=2/2145850112_data.0.parq | 4.80MB | year=2014/month=1/
day=2 |
| hdfs://impala_data_dir/show_files.db/part_parq/year=2014/month=1/day=2/665613448_data.0.parq | 4.29MB | year=2014/month=1/
day=2 |
+-----------------------------------------------------------------------------------------------+--------
+-------------------------+
The following example shows output from the SHOW FILES statement for a table where the data files are stored in
Amazon S3:
Depending on the roles set up within your organization by the CREATE ROLE statement, the output might look
something like this:
show roles;
+-----------+
| role_name |
+-----------+
| analyst |
| role1 |
| sales |
| superuser |
| test_role |
+-----------+
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Impala Authorization on page 77
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Impala Authorization on page 77
When authorization is enabled, the output of the SHOW statement only shows those objects for which you have the
privilege to view. If you believe an object exists but you cannot see it in the SHOW output, check with the system
administrator if you need to be granted a new privilege for that object. See Impala Authorization on page 77 for
how to set up authorization and add privileges for specific objects.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Impala Authorization on page 77
SHOW DATABASES
The SHOW DATABASES statement is often the first one you issue when connecting to an instance for the first time.
You typically issue SHOW DATABASES to see the names you can specify in a USE db_name statement, then
after switching to a database you issue SHOW TABLES to see the names you can specify in SELECT and INSERT
statements.
In Impala 2.5 and higher, the output includes a second column showing any associated comment for each database.
The output of SHOW DATABASES includes the special _impala_builtins database, which lets you view
definitions of built-in functions, as described under SHOW FUNCTIONS.
| Impala SQL Language Reference | 387
Security considerations:
When authorization is enabled, the output of the SHOW statement only shows those objects for which you have the
privilege to view. If you believe an object exists but you cannot see it in the SHOW output, check with the system
administrator if you need to be granted a new privilege for that object. See Impala Authorization on page 77 for
how to set up authorization and add privileges for specific objects.
Examples:
This example shows how you might locate a particular table on an unfamiliar system. The DEFAULT database is the
one you initially connect to; a database with that name is present on every system. You can issue SHOW TABLES IN
db_name without going into a database, or SHOW TABLES once you are inside a particular database.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Overview of Impala Databases on page 195, CREATE DATABASE Statement on page 231, DROP DATABASE
Statement on page 270, USE Statement on page 406 SHOW TABLES Statement on page 387, SHOW
FUNCTIONS Statement on page 399
The user ID that the impalad daemon runs under, typically the impala user, must have read and execute
permissions for all directories that are part of the table. (A table could span multiple different HDFS directories if it
is partitioned. The directories could be widely scattered because a partition can reside in an arbitrary HDFS directory
based on its LOCATION attribute.)
Examples:
The following examples demonstrate the SHOW TABLES statement. If the database contains no tables, the result
set is empty. If the database does contain tables, SHOW TABLES IN db_name lists all the table names. SHOW
TABLES with no qualifiers lists all the table names in the current database.
use full_db;
show tables;
+------+
| name |
+------+
| t1 |
| t2 |
+------+
This example demonstrates how SHOW TABLES LIKE 'wildcard_pattern' lists table names that match
a pattern, or multiple alternative patterns. The ability to do wildcard matches for table names makes it helpful to
establish naming conventions for tables to conveniently locate a group of related tables.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
Overview of Impala Tables on page 198, CREATE TABLE Statement on page 240, ALTER TABLE Statement
on page 207, DROP TABLE Statement on page 278, DESCRIBE Statement on page 258, SHOW CREATE
TABLE Statement on page 389, SHOW TABLE STATS Statement on page 392, SHOW DATABASES on page
386, SHOW FUNCTIONS Statement on page 399
| result
|
+---------------------------------------------------------------------------------------
+
| CREATE TABLE user.numeric_grades_default_letter (
|
| score TINYINT NOT NULL ENCODING AUTO_ENCODING COMPRESSION
DEFAULT_COMPRESSION, |
| letter_grade STRING NULL ENCODING AUTO_ENCODING COMPRESSION
DEFAULT_COMPRESSION DEFAULT '-', |
| student STRING NULL ENCODING AUTO_ENCODING COMPRESSION
DEFAULT_COMPRESSION, |
| PRIMARY KEY (score)
|
| )
|
| PARTITION BY RANGE (score) (...)
|
| STORED AS KUDU
|
| TBLPROPERTIES ('kudu.master_addresses'='vd0342.example.com:7051')
|
+---------------------------------------------------------------------------------------
+
Examples:
The following example shows how various clauses from the CREATE TABLE statement are represented in the output
of SHOW CREATE TABLE.
| PARTITIONED BY (
|
| year SMALLINT
|
| )
|
| STORED AS PARQUET
|
| LOCATION 'hdfs://127.0.0.1:8020/user/hive/warehouse/scratch.db/
show_create_table_demo' |
| TBLPROPERTIES ('transient_lastDdlTime'='1418152582')
|
+---------------------------------------------------------------------------------------
+
The following example shows how, after a sequence of ALTER TABLE statements, the output from SHOW CREATE
TABLE represents the current state of the table. This output could be used to create a matching table rather than
executing the original CREATE TABLE and sequence of ALTER TABLE statements.
Related information:
CREATE TABLE Statement on page 240, DESCRIBE Statement on page 258, SHOW TABLES Statement on
page 387
Impala does not compute the number of rows for each partition for Kudu tables. Therefore, you do not need to re-run
COMPUTE STATS when you see -1 in the # Rows column of the output from SHOW TABLE STATS. That column
always shows -1 for all Kudu tables.
Examples:
The following examples show how the SHOW TABLE STATS statement displays physical information about a table
and the associated data files:
The following example shows how, after a COMPUTE STATS or COMPUTE INCREMENTAL STATS statement,
the #Rows field is now filled in. Because the STORE_SALES table in this example is not partitioned, the COMPUTE
INCREMENTAL STATS statement produces regular stats rather than incremental stats, therefore the Incremental
stats field remains false.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read and execute
permissions for all directories that are part of the table. (A table could span multiple different HDFS directories if it
is partitioned. The directories could be widely scattered because a partition can reside in an arbitrary HDFS directory
based on its LOCATION attribute.) The Impala user must also have execute permission for the database directory, and
any parent directories of the database directory in HDFS.
Related information:
COMPUTE STATS Statement on page 223, SHOW COLUMN STATS Statement on page 393
See Table and Column Statistics on page 601 for usage information and examples.
administrator if you need to be granted a new privilege for that object. See Impala Authorization on page 77 for
how to set up authorization and add privileges for specific objects.
The output for SHOW COLUMN STATS includes the relevant information for Kudu tables. The information for
column statistics that originates in the underlying Kudu storage layer is also represented in the metastore database that
Impala uses.
Examples:
The following examples show the output of the SHOW COLUMN STATS statement for some tables, before the
COMPUTE STATS statement is run. Impala deduces some information, such as maximum and average size for fixed-
length columns, and leaves and unknown values as -1.
+-----------------------+-------+------------------+--------+----------
+----------+
| ss_sold_date_sk | INT | -1 | -1 | 4 | 4
|
| ss_sold_time_sk | INT | -1 | -1 | 4 | 4
|
| ss_item_sk | INT | -1 | -1 | 4 | 4
|
| ss_customer_sk | INT | -1 | -1 | 4 | 4
|
| ss_cdemo_sk | INT | -1 | -1 | 4 | 4
|
| ss_hdemo_sk | INT | -1 | -1 | 4 | 4
|
| ss_addr_sk | INT | -1 | -1 | 4 | 4
|
| ss_store_sk | INT | -1 | -1 | 4 | 4
|
| ss_promo_sk | INT | -1 | -1 | 4 | 4
|
| ss_ticket_number | INT | -1 | -1 | 4 | 4
|
| ss_quantity | INT | -1 | -1 | 4 | 4
|
| ss_wholesale_cost | FLOAT | -1 | -1 | 4 | 4
|
| ss_list_price | FLOAT | -1 | -1 | 4 | 4
|
| ss_sales_price | FLOAT | -1 | -1 | 4 | 4
|
| ss_ext_discount_amt | FLOAT | -1 | -1 | 4 | 4
|
| ss_ext_sales_price | FLOAT | -1 | -1 | 4 | 4
|
| ss_ext_wholesale_cost | FLOAT | -1 | -1 | 4 | 4
|
| ss_ext_list_price | FLOAT | -1 | -1 | 4 | 4
|
| ss_ext_tax | FLOAT | -1 | -1 | 4 | 4
|
| ss_coupon_amt | FLOAT | -1 | -1 | 4 | 4
|
| ss_net_paid | FLOAT | -1 | -1 | 4 | 4
|
| ss_net_paid_inc_tax | FLOAT | -1 | -1 | 4 | 4
|
| ss_net_profit | FLOAT | -1 | -1 | 4 | 4
|
+-----------------------+-------+------------------+--------+----------
+----------+
The following examples show the output of the SHOW COLUMN STATS statement for some tables, after the
COMPUTE STATS statement is run. Now most of the -1 values are changed to reflect the actual table data. The
#Nulls column remains -1 because Impala does not use the number of NULL values to influence query planning.
+------------------------------------------+
| summary |
+------------------------------------------+
| Updated 1 partition(s) and 23 column(s). |
+------------------------------------------+
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read and execute
permissions for all directories that are part of the table. (A table could span multiple different HDFS directories if it
is partitioned. The directories could be widely scattered because a partition can reside in an arbitrary HDFS directory
based on its LOCATION attribute.) The Impala user must also have execute permission for the database directory, and
any parent directories of the database directory in HDFS.
Related information:
COMPUTE STATS Statement on page 223, SHOW TABLE STATS Statement on page 392
See Table and Column Statistics on page 601 for usage information and examples.
creating all the partitions. See COMPUTE STATS Statement on page 223 for details. For example, on a CENSUS
table partitioned on the YEAR column:
Security considerations:
When authorization is enabled, the output of the SHOW statement only shows those objects for which you have the
privilege to view. If you believe an object exists but you cannot see it in the SHOW output, check with the system
administrator if you need to be granted a new privilege for that object. See Impala Authorization on page 77 for
how to set up authorization and add privileges for specific objects.
Kudu considerations:
The optional RANGE clause only applies to Kudu tables. It displays only the partitions defined by the RANGE clause
of CREATE TABLE or ALTER TABLE.
Although you can specify < or <= comparison operators when defining range partitions for Kudu tables, Kudu
rewrites them if necessary to represent each range as low_bound <= VALUES < high_bound. This rewriting
might involve incrementing one of the boundary values or appending a \0 for string values, so that the partition
covers the same range as originally specified.
Examples:
The following example shows the output for a Parquet, text, or other HDFS-backed table partitioned on the YEAR
column:
The following example shows the output for a Kudu table using the hash partitioning mechanism. The number of
rows in the result set corresponds to the values used in the PARTITIONS N clause of CREATE TABLE.
+--------+-----------+----------+-----------------------+--
| # Rows | Start Key | Stop Key | Leader Replica | # Replicas
+--------+-----------+----------+-----------------------+--
| -1 | | 00000001 | n236.example.com:7050 | 3
| -1 | 00000001 | 00000002 | n236.example.com:7050 | 3
| -1 | 00000002 | 00000003 | n336.example.com:7050 | 3
| -1 | 00000003 | 00000004 | n238.example.com:7050 | 3
| -1 | 00000004 | 00000005 | n338.example.com:7050 | 3
....
| -1 | 0000002E | 0000002F | n240.example.com:7050 | 3
| -1 | 0000002F | 00000030 | n336.example.com:7050 | 3
| -1 | 00000030 | 00000031 | n240.example.com:7050 | 3
| -1 | 00000031 | | n334.example.com:7050 | 3
+--------+-----------+----------+-----------------------+--
Fetched 50 row(s) in 0.05s
| Impala SQL Language Reference | 399
The following example shows the output for a Kudu table using the range partitioning mechanism:
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have read and execute
permissions for all directories that are part of the table. (A table could span multiple different HDFS directories if it
is partitioned. The directories could be widely scattered because a partition can reside in an arbitrary HDFS directory
based on its LOCATION attribute.) The Impala user must also have execute permission for the database directory, and
any parent directories of the database directory in HDFS.
Related information:
See Table and Column Statistics on page 601 for usage information and examples.
SHOW TABLE STATS Statement on page 392, Partitioning for Impala Tables on page 661
Related information:
Overview of Impala Functions on page 195, Impala Built-In Functions on page 412, User-Defined Functions
(UDFs) on page 566, SHOW DATABASES on page 386, SHOW TABLES Statement on page 387
SHUTDOWN Statement
The SHUTDOWN statement performs a graceful shutdown of Impala Daemon. The Impala daemon will notify other
Impala daemons that it is shutting down, wait for a grace period, then shut itself down once no more queries or
fragments are executing on that daemon. The --shutdown_grace_period_s flag determines the duration of the
grace period in seconds.
Syntax:
:SHUTDOWN()
:SHUTDOWN([host_name[:port_number] )
:SHUTDOWN(deadline)
:SHUTDOWN([host_name[:port_number], deadline)
Usage notes:
All arguments are optional for SHUTDOWN.
Take the following points into consideration when running the SHUTDOWN statement:
• A client can shut down the coordinator impalad that it is connected to via :SHUTDOWN().
• A client can remotely shut down any impalad via :SHUTDOWN('hostname').
• The shutdown time limit can be overridden to force a quicker or slower shutdown by specifying a deadline. The
default deadline is determined by the --shutdown_deadline_s flag, which defaults to 1 hour.
• Executors can be shut down without disrupting running queries. Short-running queries will finish, and long-
running queries will continue until the deadline is reached.
• If queries are submitted to a coordinator after shutdown of that coordinator has started, they will fail.
• Long running queries or other issues, such as stuck fragments, will slow down but not prevent eventual shutdown.
Security considerations:
The ALL privilege is required on the server.
Cancellation: Cannot be cancelled.
Examples:
:SHUTDOWN(); -- Shut down the current impalad with the default deadline.
:SHUTDOWN('hostname'); -- Shut down impalad running on hostname with the
default deadline.
:SHUTDOWN(\"hostname:1234\"); -- Shut down impalad running on host at port
1234 with the default deadline.
:SHUTDOWN(10); - Shut down the current impalad after 10 seconds.
:SHUTDOWN('hostname', 10); - Shut down impalad running on hostname when all
queries running on hostname finish, or after 10 seconds.
:SHUTDOWN('hostname:11', 10 * 60); -- Shut down impalad running on hostname
at port 11 when all queries running on hostname finish, or after 600
seconds.
:SHUTDOWN(0); -- Perform an immdediate shutdown of the current impalad.
Usage notes:
Often used to empty tables that are used during ETL cycles, after the data has been copied to another table for the
next stage of processing. This statement is a low-overhead alternative to dropping and recreating the table, or using
INSERT OVERWRITE to replace the data during the next ETL cycle.
This statement removes all the data and associated data files in the table. It can remove data files from internal tables,
external tables, partitioned tables, and tables mapped to HBase or the Amazon Simple Storage Service (S3). The data
removal applies to the entire table, including all partitions of a partitioned table.
Any statistics produced by the COMPUTE STATS statement are reset when the data is removed.
Make sure that you are in the correct database before truncating a table, either by issuing a USE statement first or by
using a fully qualified name db_name.table_name.
The optional TABLE keyword does not affect the behavior of the statement.
The optional IF EXISTS clause makes the statement succeed whether or not the table exists. If the table does exist,
it is truncated; if it does not exist, the statement has no effect. This capability is useful in standardized setup scripts
that are might be run both before and after some of the tables exist. This clause is available in Impala 2.5 and higher.
For other tips about managing and reclaiming Impala disk space, see Managing Disk Space for Impala Data on page
72.
Amazon S3 considerations:
Although Impala cannot write new data to a table stored in the Amazon S3 filesystem, the TRUNCATE TABLE
statement can remove data files from S3. See Using Impala with the Amazon S3 Filesystem on page 732 for details
about working with S3 tables.
Cancellation: Cannot be cancelled.
HDFS permissions:
The user ID that the impalad daemon runs under, typically the impala user, must have write permission for all the
files and directories that make up the table.
Kudu considerations:
Currently, the TRUNCATE TABLE statement cannot be used with Kudu tables.
Examples:
The following example shows a table containing some data and with table and column statistics. After the TRUNCATE
TABLE statement, the data is removed and the statistics are reset.
+-------+--------+------+--------------+-------------------+--------
+-------------------+
| 4 | 1 | 8B | NOT CACHED | NOT CACHED | TEXT | false
|
+-------+--------+------+--------------+-------------------+--------
+-------------------+
SHOW COLUMN STATS truncate_demo;
+--------+------+------------------+--------+----------+----------+
| Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
+--------+------+------------------+--------+----------+----------+
| x | INT | 4 | -1 | 4 | 4 |
+--------+------+------------------+--------+----------+----------+
-- After this statement, the data and the table/column stats will be gone.
TRUNCATE TABLE truncate_demo;
The following example shows how the IF EXISTS clause allows the TRUNCATE TABLE statement to be run
without error whether or not the table exists:
-- Our ETL process involves removing all data from several staging tables
-- even though some might be already dropped, or not created yet.
Related information:
Overview of Impala Tables on page 198, ALTER TABLE Statement on page 207, CREATE TABLE Statement on
page 240, Partitioning for Impala Tables on page 661, Internal Tables on page 198, External Tables on page
199
Usage notes:
None of the columns that make up the primary key can be updated by the SET clause.
The conditions in the WHERE clause are the same ones allowed for the SELECT statement. See SELECT Statement on
page 306 for details.
If the WHERE clause is omitted, all rows in the table are updated.
The conditions in the WHERE clause can refer to any combination of primary key columns or other columns.
Referring to primary key columns in the WHERE clause is more efficient than referring to non-primary key columns.
Because Kudu currently does not enforce strong consistency during concurrent DML operations, be aware that the
results after this statement finishes might be different than you intuitively expect:
• If some rows cannot be updated because their some primary key columns are not found, due to their being deleted
by a concurrent DELETE operation, the statement succeeds but returns a warning.
• An UPDATE statement might also overlap with INSERT, UPDATE, or UPSERT statements running concurrently
on the same table. After the statement finishes, there might be more or fewer matching rows than expected in the
table because it is undefined whether the UPDATE applies to rows that are inserted or updated while the UPDATE
is in progress.
The number of affected rows is reported in an impala-shell message and in the query profile.
The optional FROM clause lets you restrict the updates to only the rows in the specified table that are part of the result
set for a join query. The join clauses can include non-Kudu tables, but the table from which the rows are deleted must
be a Kudu table.
Statement type: DML
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
The following examples show how to perform a simple update on a table, with or without a WHERE clause:
-- Does not update any rows, because the WHERE condition is always false.
UPDATE kudu_table SET c3 = 'impossible' WHERE 1 = 0;
The following examples show how to perform an update using the FROM keyword with a join clause:
Related information:
Using Impala to Query Kudu Tables on page 708, INSERT Statement on page 288, DELETE Statement (Impala
2.8 or higher only) on page 256, UPSERT Statement (Impala 2.8 or higher only) on page 405
The select_statement clause can use the full syntax, such as WHERE and join clauses, as SELECT Statement on page
306.
Statement type: DML
Usage notes:
If you specify a column list, any omitted columns in the inserted or updated rows are set to their default value (if the
column has one) or NULL (if the column does not have a default value). Therefore, if a column is not nullable and has
no default value, it must be included in the column list for any UPSERT statement. Because all primary key columns
meet these conditions, all the primary key columns must be specified in every UPSERT statement.
Because Kudu tables can efficiently handle small incremental changes, the VALUES clause is more practical to use
with Kudu tables than with HDFS-based tables.
Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS
statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD
DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a
REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join
queries, or both.
Examples:
UPSERT INTO kudu_table (pk, c1, c2, c3) VALUES (0, 'hello', 50, true), (1,
'world', -1, false);
UPSERT INTO production_table SELECT * FROM staging_table;
UPSERT INTO production_table SELECT * FROM staging_table WHERE c1 IS NOT
NULL AND c2 > 0;
Related information:
Using Impala to Query Kudu Tables on page 708, INSERT Statement on page 288, UPDATE Statement (Impala
2.8 or higher only) on page 404
USE Statement
Switches the current session to a specified database. The current database is where any CREATE TABLE, INSERT,
SELECT, or other statements act when you specify a table or other object name, without prefixing it with a database
name. The new current database applies for the duration of the session or unti another USE statement is executed.
Syntax:
USE db_name
By default, when you connect to an Impala instance, you begin in a database named default.
Usage notes:
Switching the default database is convenient in the following situations:
• To avoid qualifying each reference to a table with the database name. For example, SELECT * FROM t1
JOIN t2 rather than SELECT * FROM db.t1 JOIN db.t2.
• To do a sequence of operations all within the same database, such as creating a table, inserting data, and querying
the table.
To start the impala-shell interpreter and automatically issue a USE statement for a particular database, specify
the option -d db_name for the impala-shell command. The -d option is useful to run SQL scripts, such as
setup or test scripts, against multiple databases without hardcoding a USE statement into the SQL source.
Examples:
| Impala SQL Language Reference | 407
See CREATE DATABASE Statement on page 231 for examples covering CREATE DATABASE, USE, and DROP
DATABASE.
Cancellation: Cannot be cancelled.
HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are
required.
Related information:
CREATE DATABASE Statement on page 231, DROP DATABASE Statement on page 270, SHOW DATABASES
on page 386
Optimizer Hints
The Impala SQL supports query hints, for fine-tuning the inner workings of queries. Specify hints as a temporary
workaround for expensive queries, where missing statistics or other factors cause inefficient performance.
Hints are most often used for the resource-intensive Impala queries, such as:
• Join queries involving large tables, where intermediate result sets are transmitted across the network to evaluate
the join conditions.
• Inserting into partitioned Parquet tables, where many memory buffers could be allocated on each host to hold
intermediate results for each partition.
Syntax:
In Impala 2.0 and higher, you can specify the hints inside comments that use either the /* */ or -- notation.
Specify a + symbol immediately before the hint name. Recently added hints are only available using the /* */ and
-- notation. For clarity, the /* */ and -- styles are used in the syntax and examples throughout this section. With
the /* */ or -- notation for hints, specify a + symbol immediately before the first hint name. Multiple hints can be
specified separated by commas, for example /* +clustered,shuffle */
INSERT insert_clauses
/* +SHUFFLE|NOSHUFFLE */
SELECT remainder_of_query;
INSERT insert_clauses
-- +SHUFFLE|NOSHUFFLE
SELECT remainder_of_query;
INSERT /* +SHUFFLE|NOSHUFFLE */
insert_clauses
SELECT remainder_of_query;
INSERT -- +SHUFFLE|NOSHUFFLE
insert_clauses
SELECT remainder_of_query;
| Impala SQL Language Reference | 408
UPSERT /* +SHUFFLE|NOSHUFFLE */
upsert_clauses
SELECT remainder_of_query;
UPSERT -- +SHUFFLE|NOSHUFFLE
upsert_clauses
SELECT remainder_of_query;
SELECT select_list
FROM
table_ref
/* +{SCHEDULE_CACHE_LOCAL | SCHEDULE_DISK_LOCAL | SCHEDULE_REMOTE}
[,RANDOM_REPLICA] */
remainder_of_query;
INSERT insert_clauses
-- +CLUSTERED
SELECT remainder_of_query;
INSERT insert_clauses
/* +CLUSTERED */
SELECT remainder_of_query;
INSERT -- +CLUSTERED
insert_clauses
SELECT remainder_of_query;
INSERT /* +CLUSTERED */
insert_clauses
SELECT remainder_of_query;
UPSERT -- +CLUSTERED
upsert_clauses
SELECT remainder_of_query;
UPSERT /* +CLUSTERED */
upsert_clauses
SELECT remainder_of_query;
CREATE /* +SHUFFLE|NOSHUFFLE */
table_clauses
AS SELECT remainder_of_query;
CREATE -- +SHUFFLE|NOSHUFFLE
table_clauses
AS SELECT remainder_of_query;
CREATE /* +CLUSTERED|NOCLUSTERED */
table_clauses
AS SELECT remainder_of_query;
CREATE -- +CLUSTERED|NOCLUSTERED
table_clauses
AS SELECT remainder_of_query;
The square bracket style hints are supported for backward compatibility, but the syntax is deprecated and will be
removed in a future release. For that reason, any newly added hints are not available with the square bracket syntax.
remainder_of_query;
INSERT insert_clauses
[{ /* +SHUFFLE */ | /* +NOSHUFFLE */ }]
[/* +CLUSTERED */]
SELECT remainder_of_query;
Usage notes:
With both forms of hint syntax, include the STRAIGHT_JOIN keyword immediately after the SELECT and any
DISTINCT or ALL keywords to prevent Impala from reordering the tables in a way that makes the join-related hints
ineffective.
The STRAIGHT_JOIN hint affects the join order of table references in the query block containing the hint. It does
not affect the join order of nested queries, such as views, inline views, or WHERE-clause subqueries. To use this hint
for performance tuning of complex queries, apply the hint to all query blocks that need a fixed join order.
To reduce the need to use hints, run the COMPUTE STATS statement against all tables involved in joins, or used as
the source tables for INSERT ... SELECT operations where the destination is a partitioned Parquet table. Do this
operation after loading data or making substantial changes to the data within each table. Having up-to-date statistics
helps Impala choose more efficient query plans without the need for hinting. See Table and Column Statistics on page
601 for details and examples.
To see which join strategy is used for a particular query, examine the EXPLAIN output for that query. See Using the
EXPLAIN Plan for Performance Tuning on page 631 for details and examples.
Hints for join queries:
The /* +BROADCAST */ and /* +SHUFFLE */ hints control the execution strategy for join queries. Specify
one of the following constructs immediately after the JOIN keyword in a query:
• /* +SHUFFLE */ makes that join operation use the “partitioned” technique, which divides up corresponding
rows from both tables using a hashing algorithm, sending subsets of the rows to other nodes for processing. (The
keyword SHUFFLE is used to indicate a “partitioned join”, because that type of join is not related to “partitioned
tables”.) Since the alternative “broadcast” join mechanism is the default when table and index statistics are
unavailable, you might use this hint for queries where broadcast joins are unsuitable; typically, partitioned joins
are more efficient for joins between large tables of similar size.
• /* +BROADCAST */ makes that join operation use the “broadcast” technique that sends the entire contents
of the right-hand table to all nodes involved in processing the join. This is the default mode of operation when
table and index statistics are unavailable, so you would typically only need it if stale metadata caused Impala to
mistakenly choose a partitioned join operation. Typically, broadcast joins are more efficient in cases where one
table is much smaller than the other. (Put the smaller table on the right side of the JOIN operator.)
Hints for INSERT ... SELECT and CREATE TABLE AS SELECT (CTAS):
When inserting into partitioned tables, such as using the Parquet file format, you can include a hint in the INSERT
or CREATE TABLE AS SELECT(CTAS) statements to fine-tune the overall performance of the operation and its
resource usage.
You would only use hints if an INSERT or CTAS into a partitioned table was failing due to capacity limits, or if such
an operation was succeeding but with less-than-optimal performance.
• /* +SHUFFLE */ and /* +NOSHUFFLE */ Hints
• /* +SHUFFLE */ adds an exchange node, before writing the data, which re-partitions the result of the
SELECT based on the partitioning columns of the target table. With this hint, only one node writes to a
partition at a time, minimizing the global number of simultaneous writes and the number of memory buffers
| Impala SQL Language Reference | 410
holding data for individual partitions. This also reduces fragmentation, resulting in fewer files. Thus it
reduces overall resource usage of the INSERT or CTAS operation and allows some operations to succeed that
otherwise would fail. It does involve some data transfer between the nodes so that the data files for a particular
partition are all written on the same node.
Use /* +SHUFFLE */ in cases where an INSERT or CTAS statement fails or runs inefficiently due to all
nodes attempting to write data for all partitions.
If the table is unpartitioned or every partitioning expression is constant, then /* +SHUFFLE */ will cause
every write to happen on the coordinator node.
• /* +NOSHUFFLE */ does not add exchange node before inserting to partitioned tables and disables re-
partitioning. So the selected execution plan might be faster overall, but might also produce a larger number of
small data files or exceed capacity limits, causing the INSERT or CTAS operation to fail.
Impala automatically uses the /* +SHUFFLE */ method if any partition key column in the source table,
mentioned in the SELECT clause, does not have column statistics. In this case, use the /* +NOSHUFFLE */
hint if you want to override this default behavior.
• If column statistics are available for all partition key columns in the source table mentioned in the
INSERT ... SELECT or CTAS query, Impala chooses whether to use the /* +SHUFFLE */ or /*
+NOSHUFFLE */ technique based on the estimated number of distinct values in those columns and the
number of nodes involved in the operation. In this case, you might need the /* +SHUFFLE */ or the /*
+NOSHUFFLE */ hint to override the execution plan selected by Impala.
• /* +CLUSTERED */ and /* +NOCLUSTERED */ Hints
• /* +CLUSTERED */ sorts data by the partition columns before inserting to ensure that only one partition is
written at a time per node. Use this hint to reduce the number of files kept open and the number of buffers kept
in memory simultaneously. This technique is primarily useful for inserts into Parquet tables, where the large
block size requires substantial memory to buffer data for multiple output files at once. This hint is available in
Impala 2.8 or higher.
Starting in Impala 3.0, /* +CLUSTERED */ is the default behavior for HDFS tables.
• /* +NOCLUSTERED */ does not sort by primary key before insert. This hint is available in Impala 2.8 or
higher.
Use this hint when inserting to Kudu tables.
In the versions lower than Impala 3.0, /* +NOCLUSTERED */ is the default in HDFS tables.
Kudu consideration:
Starting from Impala 2.9, the INSERT or UPSERT operations into Kudu tables automatically add an exchange
and a sort node to the plan that partitions and sorts the rows according to the partitioning/primary key scheme of
the target table (unless the number of rows to be inserted is small enough to trigger single node execution). Since
Kudu partitions and sorts rows on write, pre-partitioning and sorting takes some of the load off of Kudu and helps
large INSERT operations to complete without timing out. However, this default behavior may slow down the
end-to-end performance of the INSERT or UPSERT operations. Starting fromImpala 2.10, you can use the /*
+NOCLUSTERED */ and /* +NOSHUFFLE */ hints together to disable partitioning and sorting before the
rows are sent to Kudu. Additionally, since sorting may consume a large amount of memory, consider setting the
MEM_LIMIT query option for those queries.
Hints for scheduling of scan ranges (HDFS data blocks or Kudu tablets)
The hints /* +SCHEDULE_CACHE_LOCAL */, /* +SCHEDULE_DISK_LOCAL */, and /*
+SCHEDULE_REMOTE */ have the same effect as specifying the REPLICA_PREFERENCE query option with the
respective option settings of CACHE_LOCAL, DISK_LOCAL, or REMOTE.
Specifying the replica preference as a query hint always overrides the query option setting.
The hint /* +RANDOM_REPLICA */ is the same as enabling the SCHEDULE_RANDOM_REPLICA query option.
You can use these hints in combination by separating them with commas, for example, /*
+SCHEDULE_CACHE_LOCAL,RANDOM_REPLICA */. See REPLICA_PREFERENCE Query Option (Impala 2.7
or higher only) on page 373 and SCHEDULE_RANDOM_REPLICA Query Option (Impala 2.5 or higher only) on
| Impala SQL Language Reference | 411
page 378 for information about how these settings influence the way Impala processes HDFS data blocks or Kudu
tablets.
Specifying either the SCHEDULE_RANDOM_REPLICA query option or the corresponding RANDOM_REPLICA
query hint enables the random tie-breaking behavior when processing data blocks during the query.
Suggestions versus directives:
In early Impala releases, hints were always obeyed and so acted more like directives. Once Impala gained join order
optimizations, sometimes join queries were automatically reordered in a way that made a hint irrelevant. Therefore,
the hints act more like suggestions in Impala 1.2.2 and higher.
To force Impala to follow the hinted execution mechanism for a join query, include the STRAIGHT_JOIN keyword
in the SELECT statement. See Overriding Join Reordering with STRAIGHT_JOIN on page 595 for details. When
you use this technique, Impala does not reorder the joined tables at all, so you must be careful to arrange the join
order to put the largest table (or subquery result set) first, then the smallest, second smallest, third smallest, and so on.
This ordering lets Impala do the most I/O-intensive parts of the query using local reads on the DataNodes, and then
reduce the size of the intermediate result set as much as possible as each subsequent table or subquery result set is
joined.
Restrictions:
Queries that include subqueries in the WHERE clause can be rewritten internally as join queries. Currently, you cannot
apply hints to the joins produced by these types of queries.
Because hints can prevent queries from taking advantage of new metadata or improvements in query planning, use
them only when required to work around performance issues, and be prepared to remove them when they are no
longer required, such as after a new Impala release or bug fix.
In particular, the /* +BROADCAST */ and /* +SHUFFLE */ hints are expected to be needed much less
frequently in Impala 1.2.2 and higher, because the join order optimization feature in combination with the COMPUTE
STATS statement now automatically choose join order and join mechanism without the need to rewrite the query and
add hints. See Performance Considerations for Join Queries on page 594 for details.
Compatibility:
The hints embedded within -- comments are compatible with Hive queries. The hints embedded within /* */
comments or [ ] square brackets are not recognized by or not compatible with Hive. For example, Hive raises an
error for Impala hints within /* */ comments because it does not recognize the Impala hint names.
Considerations for views:
If you use a hint in the query that defines a view, the hint is preserved when you query the view. Impala internally
rewrites all hints in views to use the -- comment notation, so that Hive can query such views without errors due to
unrecognized hint names.
Examples:
For example, this query joins a large customer table with a small lookup table of less than 100 rows. The right-hand
table can be broadcast efficiently to all nodes involved in the join. Thus, you would use the /* +broadcast */
hint to force a broadcast join strategy:
This query joins two large tables of unpredictable size. You might benchmark the query with both kinds of hints and
find that it is more efficient to transmit portions of each table to other nodes for processing. Thus, you would use the
/* +shuffle */ hint to force a partitioned join strategy:
For joins involving three or more tables, the hint applies to the tables on either side of that specific JOIN keyword.
The STRAIGHT_JOIN keyword ensures that joins are processed in a predictable order from left to right. For
example, this query joins t1 and t2 using a partitioned join, then joins that result set to t3 using a broadcast join:
Related information:
For more background information about join queries, see Joins in Impala SELECT Statements on page 308. For
performance considerations, see Performance Considerations for Join Queries on page 594.
ABS
ACOS
ADD_MONTHS
ADDDATE
APPX_MEDIAN
ASCII
ASIN
ATAN
ATAN2
AVG
AVG - Analytic Function
BASE64DECODE
BASE64ENCODE
BITAND
BIN
BITNOT
| Impala SQL Language Reference | 413
BITOR
BITXOR
BTRIM
CASE
CASE WHEN
CAST
CEIL, CEILING, DCEIL
CHAR_LENGTH
CHR
COALESCE
CONCAT
CONCAT_WS
CONV
COS
COSH
COT
COUNT
COUNT - Analytic Function
COUNTSET
CUME_DIST
CURRENT_DATABASE
CURRENT_TIMESTAMP
DATE_ADD
DATE_PART
DATE_SUB
DATE_TRUNC
DATEDIFF
DAY
DAYNAME
DAYOFWEEK
DAYOFYEAR
DAYS_ADD
DAYS_SUB
DECODE
DEGREES
DENSE_RANK
E
| Impala SQL Language Reference | 414
EFFECTIVE_USER
EXP
EXTRACT
FACTORIAL
FIND_IN_SET
FIRST_VALUE
FLOOR, DFLOOR
FMOD
FNV_HASH
FROM_UNIXTIME
FROM_TIMESTAMP
FROM_UTC_TIMESTAMP
GET_JSON_OBJECT
GETBIT
GREATEST
GROUP_CONCAT
GROUP_CONCAT - Analytic Function
HEX
HOUR
HOURS_ADD
HOURS_SUB
IF
IFNULL
INITCAP
INSTR
INT_MONTHS_BETWEEN
IS_INF
IS_NAN
ISFALSE
ISNOTFALSE
ISNOTTRUE
ISNULL
ISTRUE
LAG
LAST_VALUE
LEAD
LEAST
| Impala SQL Language Reference | 415
LEFT
LENGTH
LN
LOCATE
LOG
LOG10
LOG2
LOWER, LCASE
LPAD
LTRIM
MAX
MAX - Analytic Function
MAX_INT, MAX_TINYINT, MAX_SMALLINT, MAX_BIGINT
MICROSECONDS_ADD
MICROSECONDS_SUB
MILLISECOND
MILLISECONDS_ADD
MILLISECONDS_SUB
MIN
MIN - Analytic Function
MIN_INT, MIN_TINYINT, MIN_SMALLINT, MIN_BIGINT
MINUTE
MINUTES_ADD
MINUTES_SUB
MOD
MONTH
MONTHNAME
MONTHS_ADD
MONTHS_BETWEEN
MONTHS_SUB
MURMUR_HASH
NANOSECONDS_ADD
NANOSECONDS_SUB
NDV
NEGATIVE
NEXT_DAY
NONNULLVALUE
| Impala SQL Language Reference | 416
NOW
NTILE
NULLIF
NULLIFZERO
NULLVALUE
NVL
NVL2
OVER Clause
PARSE_URL
PERCENT_RANK
PI
PID
PMOD
POSITIVE
POW, POWER, DPOW, FPOW
PRECISION
QUARTER
QUOTIENT
RADIANS
RAND, RANDOM
RANK
REGEXP_ESCAPE
REGEXP_EXTRACT
REGEXP_LIKE
REGEXP_REPLACE
REPEAT
REPLACE
REVERSE
RIGHT
ROTATELEFT
ROTATERIGHT
ROUND, DROUND
ROW_NUMBER
RPAD
RTRIM
SCALE
SECOND
| Impala SQL Language Reference | 417
SECONDS_ADD
SECONDS_SUB
SETBIT
SHIFTLEFT
SHIFTRIGHT
SIGN
SIN
SINH
SLEEP
SPACE
SPLIT_PART
SQRT
STDDEV, STDDEV_SAMP, STDDEV_POP
STRLEFT
STRRIGHT
SUBDATE
SUBSTR, SUBSTRING
SUM
SUM - Analytic Function
TAN
TANH
TIMEOFDAY
TIMESTAMP_CMP
TO_DATE
TO_TIMESTAMP
TO_UTC_TIMESTAMP
TRANSLATE
TRIM
TRUNC
TRUNCATE, DTRUNC, TRUNC
TYPEOF
UNHEX
UNIX_TIMESTAMP
UPPER, UCASE
USER
UTC_TIMESTAMP
UUID
| Impala SQL Language Reference | 418
• HEX
• IS_INF
• IS_NAN
• LEAST
• LN
• LOG
• LOG10
• LOG2
• MAX_INT, MAX_TINYINT, MAX_SMALLINT, MAX_BIGINT
• MIN_INT, MIN_TINYINT, MIN_SMALLINT, MIN_BIGINT
• MOD
• MURMUR_HASH
• NEGATIVE
• PI
• PMOD
• POSITIVE
• POW, POWER, DPOW, FPOW
• PRECISION
• QUOTIENT
• RADIANS
• RAND, RANDOM
• ROUND, DROUND
• SCALE
• SIGN
• SIN
• SINH
• SQRT
• TAN
• TANH
• TRUNCATE, DTRUNC, TRUNC
• UNHEX
• WIDTH_BUCKET
CEIL(DOUBLE a), CEIL(DECIMAL(p,s) a), Purpose: Returns the smallest integer that is greater than
CEILING(DOUBLE a), CEILING(DECIMAL(p,s) or equal to the argument.
a), DCEIL(DOUBLE a), DCEIL(DECIMAL(p,s) a)
Return type: Same as the input value
CONV(BIGINT n, INT from_base, INT to_base), Purpose: Returns a string representation of the first
CONV(STRING s, INT from_base, INT to_base) argument converted from from_base to to_base.
The first argument can be specified as a number or
a string. For example, CONV(100, 2, 10) and
CONV('100', 2, 10) both return '4'.
Return type: STRING
Usage notes:
If to_base is negative, the first argument is treated
as signed, and otherwise, it is treated as unsigned. For
example:
• conv(-17, 10, -2) returns '-10001', -17
in base 2.
• conv(-17, 10, 10) returns
'18446744073709551599'. -17 is interpreted
as an unsigned, 2^64-17, and then the value is
returned in base 10.
The function returns NULL when the following illegal
arguments are specified:
• Any argument is NULL.
• from_base or to_base is below -36 or above
36.
• from_base or to_base is -1, 0, or 1.
• The first argument represents a positive number and
from_base is a negative number.
If the first argument represents a negative number and
from_base is a negative number, the function returns
0.
If the first argument represents a number larger than the
maximum bigint, the function returns:
• The string representation of -1 in to_base if
to_base is negative.
• The string representation of
18446744073709551615' (2^64 - 1) in to_base if
to_base is positive.
If the first argument does not represent a valid number
in from_base, e.g. 3 in base 2 or '1a23' in base 10,
| Impala SQL Language Reference | 421
select factorial(5);
+--------------+
| factorial(5) |
+--------------+
| 120 |
+--------------+
select 5!;
+-----+
| 5! |
| Impala SQL Language Reference | 422
+-----+
| 120 |
+-----+
select factorial(0);
+--------------+
| factorial(0) |
+--------------+
| 1 |
+--------------+
select factorial(-100);
+-----------------+
| factorial(-100) |
+-----------------+
| 1 |
+-----------------+
FLOOR(DOUBLE a), FLOOR(DECIMAL(p,s) a), Purpose: Returns the largest integer that is less than or
DFLOOR(DOUBLE a), DFLOOR(DECIMAL(p,s) a) equal to the argument.
Return type: Same as the input type
select fmod(10,3);
+-------------+
| fmod(10, 3) |
+-------------+
| 1 |
+-------------+
select fmod(5.5,2);
+--------------+
| fmod(5.5, 2) |
+--------------+
| 1.5 |
+--------------+
select 10 % 3;
+--------+
| 10 % 3 |
+--------+
| 1 |
| Impala SQL Language Reference | 423
+--------+
select 5.5 % 2;
+---------+
| 5.5 % 2 |
+---------+
| 1.5 |
+---------+
select fmod(9.9,3.0);
+--------------------+
| fmod(9.9, 3.0) |
+--------------------+
| 0.8999996185302734 |
+--------------------+
select fmod(9.9,3.3);
+-------------------+
| fmod(9.9, 3.3) |
+-------------------+
| 3.299999713897705 |
+-------------------+
GREATEST(BIGINT a[, BIGINT b ...]), Purpose: Returns the largest value from a list of
GREATEST(DOUBLE a[, DOUBLE b ...]), expressions.
GREATEST(DECIMAL(p,s) a[, DECIMAL(p,s)
Return type: same as the initial argument value,
b ...]), GREATEST(STRING a[, STRING b ...]),
except that integer values are promoted to BIGINT
GREATEST(TIMESTAMP a[, TIMESTAMP b ...])
and floating-point values are promoted to DOUBLE; use
CAST() when inserting into a smaller numeric column
LEAST(BIGINT a[, BIGINT b ...]), Purpose: Returns the smallest value from a list of
LEAST(DOUBLE a[, DOUBLE b ...]), expressions.
LEAST(DECIMAL(p,s) a[, DECIMAL(p,s)
Return type: same as the initial argument value,
b ...]), LEAST(STRING a[, STRING b ...]),
except that integer values are promoted to BIGINT
LEAST(TIMESTAMP a[, TIMESTAMP b ...])
and floating-point values are promoted to DOUBLE; use
CAST() when inserting into a smaller numeric column
LN(DOUBLE a), DLOG1(DOUBLE a) Purpose: Returns the natural logarithm of the argument.
Return type: DOUBLE
LOG(DOUBLE base, DOUBLE a) Purpose: Returns the logarithm of the second argument
to the specified base.
Return type: DOUBLE
LOG10(DOUBLE a), DLOG10(DOUBLE a) Purpose: Returns the logarithm of the argument to the
base 10.
Return type: DOUBLE
MIN_INT(), MIN_TINYINT(), MIN_SMALLINT(), Purpose: Returns the smallest value of the associated
MIN_BIGINT() integral type (a negative number).
Return type: The same as the integral type being
checked.
Usage notes: Use the corresponding min_ and max_
functions to check if all values in a column are within the
allowed range, before copying data or altering column
definitions. If not, switch to the next higher integral type
or to a DECIMAL with sufficient precision.
select mod(10,3);
+-------------+
| mod(10, 3) |
+-------------+
| 1 |
+-------------+
select mod(5.5,2);
+--------------+
| mod(5.5, 2) |
+--------------+
| 1.5 |
+--------------+
select 10 % 3;
+--------+
| 10 % 3 |
+--------+
| 1 |
+--------+
select 5.5 % 2;
+---------+
| 5.5 % 2 |
+---------+
| 1.5 |
+---------+
select mod(9.9,3.3);
+---------------+
| mod(9.9, 3.3) |
+---------------+
| 0.0 |
+---------------+
select mod(9.9,3);
+--------------------+
| mod(9.9, 3) |
+--------------------+
| 0.8999996185302734 |
+--------------------+
+-----------------------------------
+
| mod(9.9, cast(3 as decimal(2,1)))
|
+-----------------------------------
+
| 0.9
|
+-----------------------------------
+
select mod(9.9,3.0);
+---------------+
| mod(9.9, 3.0) |
+---------------+
| 0.9 |
+---------------+
| 0 | 6960269033020761575
|
| 1 | -780611581681153783
|
| 1234567890 | -5754914572385924334
|
+------------+----------------------
+
[localhost:21000] > select s,
murmur_hash(s) from h;
+------------------------------
+----------------------+
| s |
murmur_hash(s) |
+------------------------------
+----------------------+
| hello |
2191231550387646743 |
| world |
5568329560871645431 |
| antidisestablishmentarianism |
-2261804666958489663 |
+------------------------------
+----------------------+
PMOD(BIGINT a, BIGINT b), PMOD(DOUBLE a, Purpose: Returns the positive modulus of a number.
DOUBLE b) Primarily for HiveQL compatibility.
| Impala SQL Language Reference | 430
select fmod(-5,2);
+-------------+
| fmod(-5, 2) |
+-------------+
| -1 |
+-------------+
select pmod(-5,2);
+-------------+
| pmod(-5, 2) |
+-------------+
| 1 |
+-------------+
select fmod(-5,-2);
+--------------+
| fmod(-5, -2) |
+--------------+
| -1 |
+--------------+
select pmod(-5,-2);
+--------------+
| pmod(-5, -2) |
+--------------+
| -1 |
+--------------+
select fmod(5,-2);
+-------------+
| fmod(5, -2) |
+-------------+
| 1 |
+-------------+
select pmod(5,-2);
+-------------+
| pmod(5, -2) |
+-------------+
| -1 |
+-------------+
POW(DOUBLE a, DOUBLE p), POWER(DOUBLE Purpose: Returns the first argument raised to the power
a, DOUBLE p), DPOW(DOUBLE a, DOUBLE p), of the second argument.
FPOW(DOUBLE a, DOUBLE p)
Return type: DOUBLE
QUOTIENT(BIGINT numerator, BIGINT Purpose: Returns the first argument divided by the
denominator), QUOTIENT(DOUBLE numerator, second argument, discarding any fractional part. Avoids
DOUBLE denominator) promoting integer arguments to DOUBLE as happens
with the / SQL operator. Also includes an overload
that accepts DOUBLE arguments, discards the fractional
part of each argument value before dividing, and again
| Impala SQL Language Reference | 432
RAND(), RAND(BIGINT seed), RANDOME(), Purpose: Returns a random value between 0 and 1. After
RANDOME(BIGINT seed) RAND() is called with a seed argument, it produces a
consistent random sequence based on the seed value.
Return type: DOUBLE
Usage notes: Currently, the random sequence is reset
after each query, and multiple calls to RAND() within
the same query return the same value each time. For
different number sequences that are different for each
query, pass a unique seed value to each call to RAND().
For example, SELECT RAND(UNIX_TIMESTAMP())
FROM ...
Examples:
The following examples show how rand() can produce
sequences of varying predictability, so that you can
reproduce query results involving random values or
generate unique sequences of random values for each
query. When rand() is called with no argument,
it generates the same sequence of values each time,
regardless of the ordering of the result set. When
rand() is called with a constant integer, it generates
a different sequence of values, but still always the
same sequence for the same seed value. If you pass in
a seed value that changes, such as the return value of
the expression unix_timestamp(now()), each
query will use a different sequence of random values,
potentially more useful in probability calculations
although more difficult to reproduce at a later time.
Therefore, the final two examples with an unpredictable
seed value also include the seed in the result set, to make
it possible to reproduce the same random sequence later.
+---+-----------------------+
select x, unix_timestamp(now()),
rand(unix_timestamp(now()))
from three_rows order by x;
+---+-----------------------
+-----------------------------+
| x | unix_timestamp(now()) |
rand(unix_timestamp(now())) |
+---+-----------------------
+-----------------------------+
| 1 | 1440777752 |
0.002051228658320023 |
| 2 | 1440777752 |
0.5098743483004506 |
| 3 | 1440777752 |
0.9517714925817081 |
+---+-----------------------
+-----------------------------+
select x, unix_timestamp(now()),
rand(unix_timestamp(now()))
from three_rows order by x desc;
+---+-----------------------
+-----------------------------+
| x | unix_timestamp(now()) |
rand(unix_timestamp(now())) |
+---+-----------------------
+-----------------------------+
| 3 | 1440777761 |
0.9985985015512437 |
| 2 | 1440777761 |
0.3251255333074953 |
| 1 | 1440777761 |
0.02422675025846192 |
+---+-----------------------
+-----------------------------+
SQRT(DOUBLE a), DSQRT(DOUBLE a) Purpose: Returns the square root of the argument.
Return type: DOUBLE
select truncate(3.45);
+----------------+
| truncate(3.45) |
+----------------+
| 3 |
+----------------+
select truncate(-3.45);
+-----------------+
| truncate(-3.45) |
+-----------------+
| -3 |
+-----------------+
select truncate(3.456,1);
+--------------------+
| truncate(3.456, 1) |
+--------------------+
| 3.4 |
+--------------------+
select dtrunc(3.456,1);
+------------------+
| dtrunc(3.456, 1) |
+------------------+
| 3.4 |
+------------------+
select truncate(3.456,2);
+--------------------+
| truncate(3.456, 2) |
+--------------------+
| 3.45 |
+--------------------+
select truncate(3.456,7);
+--------------------+
| truncate(3.456, 7) |
+--------------------+
| 3.4560000 |
+--------------------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----------+
| 3.33 | 15 | 2 |
| 4.44 | 15 | 2 |
| 5.55 | 15 | 2 |
| 8888.88 | 15 | 2 |
| 9999.99 | 15 | 2 |
+-------------+----+---+
WIDTH_BUCKET(DECIMAL expr, DECIMAL Purpose: Returns the bucket number in which the
min_value, DECIMAL max_value, INT expr value would fall in the histogram where its range
num_buckets) between min_value and max_value is divided into
num_buckets buckets of identical sizes.
The function returns:
• NULL if any argument is NULL.
• 0 if expr < min_value.
• num_buckets + 1 if expr >= max_val.
• If none of the above, the bucket number where expr
falls.
Arguments:The following rules apply to the arguments.
• min_val is the minimum value of the histogram
range.
• max_val is the maximum value of the histogram
range.
• num_buckets must be greater than 0.
• min_value must be less than max_value.
Usage notes:
Each bucket contains values equal to or greater
than the base value of that bucket and less than the
| Impala SQL Language Reference | 439
width_bucket(8, 1, 20, 3)
• COUNTSET
• GETBIT
• ROTATELEFT
• ROTATERIGHT
• SETBIT
• SHIFTLEFT
• SHIFTRIGHT
| 4 |
+---------------+
| 15 |
+---------------+
COUNTSET(integer_type a [, INT zero_or_one]) Purpose: By default, returns the number of 1 bits in the
specified integer value. If the optional second argument
is set to zero, it returns the number of 0 bits instead.
Usage notes:
In discussions of information theory, this operation is
referred to as the “population count” or “popcount”.
Return type: Same as the input value
Added in: Impala 2.3.0
Examples:
The following examples show how to count the number
of 1 bits in an integer value.
+--------------+
Examples:
select rotateleft(-1,155); /*
11111111 -> 11111111 */
+---------------------+
| rotateleft(-1, 155) |
+---------------------+
| -1 |
+---------------------+
select rotateleft(-128,1); /*
10000000 -> 00000001 */
+---------------------+
| rotateleft(-128, 1) |
+---------------------+
| 1 |
+---------------------+
select rotateleft(-127,3); /*
10000001 -> 00001100 */
+---------------------+
| rotateleft(-127, 3) |
+---------------------+
| 12 |
+---------------------+
Examples:
select rotateright(16,4); /*
00010000 -> 00000001 */
+--------------------+
| rotateright(16, 4) |
+--------------------+
| 1 |
+--------------------+
select rotateright(-1,155); /*
11111111 -> 11111111 */
+----------------------+
| rotateright(-1, 155) |
+----------------------+
| -1 |
+----------------------+
select rotateright(-128,1); /*
10000000 -> 01000000 */
+----------------------+
| rotateright(-128, 1) |
+----------------------+
| 64 |
+----------------------+
select rotateright(-127,3); /*
10000001 -> 00110000 */
+----------------------+
| rotateright(-127, 3) |
+----------------------+
| 48 |
+----------------------+
SETBIT(integer_type a, INT position [, INT Purpose: By default, changes a bit at a specified position
zero_or_one]) to a 1, if it is not already. If the optional third argument is
set to zero, the specified bit is set to 0 instead.
Usage notes:
If the bit at the specified position was already 1 (by
default) or 0 (with a third argument of zero), the return
value is the same as the first argument. The positions are
numbered right to left, starting at zero. (Therefore, the
return value could be different from the first argument
even if the position argument is zero.) The position
argument cannot be negative.
When you use a literal input value, it is treated as an
8-bit, 16-bit, and so on value, the smallest type that is
appropriate. The type of the input value limits the range
of the positions. Cast the input value to the appropriate
type if you need to ensure it is treated as a 64-bit, 32-bit,
and so on value.
Return type: Same as the input value
Added in: Impala 2.3.0
Examples:
+--------------+
| setbit(0, 0) |
+--------------+
| 1 |
+--------------+
select setbit(cast(0 as
bigint),32); /* For BIGINT, the
position can be 0..63. */
+-------------------------------+
| setbit(cast(0 as bigint), 32) |
+-------------------------------+
| 4294967296 |
+-------------------------------+
| -32 |
+-------------------+
| shiftright(-1, 1) |
+-------------------+
| 127 |
+-------------------+
Related information:
For details of casts from each kind of data type, see
the description of the appropriate type: TINYINT Data
Type on page 131, SMALLINT Data Type on page
116, INT Data Type on page 111, BIGINT Data
Type on page 98, FLOAT Data Type on page 109,
DOUBLE Data Type on page 108, DECIMAL Data
Type, STRING Data Type on page 117, CHAR Data
Type, VARCHAR Data Type, TIMESTAMP Data Type on
page 125, BOOLEAN Data Type on page 99
CAST(expression AS type FORMAT pattern) Purpose: Returns expression converted to the type data
type based on the pattern format string. This signature
of CAST() with the FORMAT clause is supported only
for casts between STRING / CHAR / VARCHAR types and
TIMESTAMP / DATE types.
Note: The patterns allowed in the FORMAT clause
support ISO SQL:2016 standard patterns. Those patterns
are not the same format patterns used with the other
Impala conversion functions, e.g. TO_TIMESTAMP()
and FROM_TIMESTAMP().
The following rules apply to pattern. Any exceptions to
these rules are noted in the Details column of the table
below.
• pattern is a case-insensitive STRING.
• If pattern is NULL, an empty string, or a number, an
error returns.
• A fewer digits in expression than specified in the
pattern is accepted if a separator is correctly specified
in the pattern. For example, CAST('5-01-2017'
AS DATE FORMAT 'MM-dd-yyyy') returns
DATE'2017-05-01'.
• If fewer number of digits are in expression
than specified in the pattern, the current date is
used to complete the year pattern. For example,
CAST('19/05' AS DATE FORMAT 'YYYY/
MM') will return DATE'2019-05-01' when
executed on August 8, 2019.
The following format patterns are supported in the
FORMAT clause.
Pattern
Description
Details
YYYY4-digit
year.
YYY Last 3
digits of
a year.
YY Last 2
digits of
a year.
| Impala SQL Language Reference | 453
Pattern
Description
Details
Y Last
digit of
a year
RRRR4-digit If 1,3 or 4-digit year values are
round provided in expression, treated as
year YYYY.
If 2-digit years are provided in
expression, treated as RR.
For datetime to string conversions,
treated as YYYY.
If YYYY, YYY, YY, Y, or RR is given
in the same pattern for a string to
datetime conversion, an error returns.
| Impala SQL Language Reference | 454
Pattern
Description
Details
RR 2-digit • For datetime to string conversion,
round same as YY.
year. • For string to datetime
conversions, the first 2 digits
of the year in the return value
depends on the specified two-
digit year and the last two digits
of the current year as follows:
• If the specified 2-digit year is
00 to 49:
• If the last 2 digits of the
current year are 00 to 49,
the returned year has the
same first 2 digits as the
current year.
• If the last 2 digits of the
current year are 50 to 99,
the first 2 digits of the
returned year are 1 greater
than the first 2 digits of the
current year.
• If the specified 2-digit year is
50 to 99:
• If the last 2 digits of the
current year are 00 to 49,
the first 2 digits of the
returned year are 1 less
than the first 2 digits of the
current year.
• If the last 2 digits of the
current year are 50 to 99,
the returned year has the
same first 2 digits as the
current year.
If YYYY, YYY, YY, Y, or RR is given
in the same pattern for a string to
datetime conversion, an error returns.
If 1-digit year values are provided in
expression, it is treated as YYYY.
Pattern
Description
Details
HH Hour In datetime to string conversions, 1-
of day digit hours are prefixed with a zero.
HH12
(1-12)
If provided hour in expression is not
between 1 and 12, returns an error.
If no AM/PM is provided in
expression, the default is AM.
Pattern
Description
Details
- Separator For string to datetime conversions,
. any separator character in the pattern
string would match any separator
/ character in the input expression.
, For example, CAST(“20191010”
' AS DATE FORMAT “YYYY-
MM-DD”) returns an error, but
; CAST("2019-.;10 10" AS
: DATE FORMAT "YYYY-MM-DD")
succeeds.
<space>
Examples:
Input Output
CAST("02-11-2014" 2014-11-02
AS DATE FORMAT
'dd-mm-yyyy')
CAST("365 2014" 2014-12-31
AS DATE FORMAT
'DDD-YYYY')
CAST("5-01-26" AS Executed at 2019-01-01
DATE FORMAT 'YY- 11:11:11:
MM-DD')
2015-01-26
CAST('2018-11-10T15:11:04Z'
2018-11-10 15:11:04
AS TIMESTAMP
FORMAT 'YYYY-MM-
DDTHH24:MI:SSZ')
Input Output
CAST("49-01-15" Round year when last 2
AS DATE FORMAT digits of current year is
'RR-MM-DD') greater than 49.
Executed at 2099-01-01
11:11:11:
2149-01-15
CAST('2019.10.10 2019-10-10
13:30:40.123456 13:30:40.123456000
+01:30'
AS TIMESTAMP
FORMAT 'YYYY-MM-
DD HH24:MI:SS.FF9
TZH:TZM')
• ADD_MONTHS
• ADDDATE
• CURRENT_DATE
• CURRENT_TIMESTAMP
• DATE_ADD
• DATE_PART
• DATE_SUB
• DATE_TRUNC
• DATEDIFF
• DAY
• DAYNAME
• DAYOFWEEK
• DAYOFYEAR
• DAYS_ADD
• DAYS_SUB
• EXTRACT
• FROM_TIMESTAMP
• FROM_UNIXTIME
• FROM_UTC_TIMESTAMP
• HOUR
• HOURS_ADD
• HOURS_SUB
• INT_MONTHS_BETWEEN
• MICROSECONDS_ADD
• MICROSECONDS_SUB
• MILLISECOND
• MILLISECONDS_ADD
• MILLISECONDS_SUB
• MINUTE
• MINUTES_ADD
• MINUTES_SUB
• MONTH
• MONTHNAME
• MONTHS_ADD
• MONTHS_BETWEEN
• MONTHS_SUB
• NANOSECONDS_ADD
• NANOSECONDS_SUB
• NEXT_DAY
• NOW
• QUARTER
• SECOND
• SECONDS_ADD
• SECONDS_SUB
• SUBDATE
• TIMEOFDAY
• TIMESTAMP_CMP
• TO_DATE
• TO_TIMESTAMP
• TO_UTC_TIMESTAMP
| Impala SQL Language Reference | 459
• TRUNC
• UNIX_TIMESTAMP
• UTC_TIMESTAMP
• WEEKOFYEAR
• WEEKS_ADD
• WEEKS_SUB
• YEAR
• YEARS_ADD
• YEARS_SUB
ADD_MONTHS(TIMESTAMP / DATE date, INT Purpose: Adds months to date and returns the new date
months), ADD_MONTHS(TIMESTAMP / DATE value.
date, BIGINT months)
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
Usage notes:
Same as MONTHS_ADD(). Available in Impala 1.4 and
higher. For compatibility when porting code with vendor
extensions.
ADDDATE(TIMESTAMP / DATE date, INT / Purpose: Adds days to date and returns the new date
BIGINT days) value.
The days value can be negative, which gives the same
result as the SUBDATE() function.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
DATE_ADD(TIMESTAMP / DATE date, INT / Purpose: Adds a specified number of days to the date
BIGINT days), DATE_ADD(TIMESTAMP / DATE argument.
date, interval_expression) With an INTERVAL expression as the second argument,
you can calculate a delta value using other units
such as weeks, years, hours, seconds, and so on; see
TIMESTAMP Data Type on page 125 for details.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
| Impala SQL Language Reference | 460
select date_add(cast('2016-01-31' as
timestamp), interval 3 months) as
'april_31st';
+---------------------+
| april_31st |
+---------------------+
| 2016-04-30 00:00:00 |
+---------------------+
DATE_CMP(DATE date1, DATE date2) Purpose: Compares date1 and date2 and returns:
• 0 if the dates are identical.
• 1 if date1 > date2.
• -1 if date1 < date2.
• NULL if date1 or date2 is NULL.
Return type: INT
DATE_PART(STRING part, TIMESTAMP / DATE Purpose: Similar to EXTRACT(), with the argument
date) order reversed. Supports the same date and time units
| Impala SQL Language Reference | 461
| right_now |
6_hours_ago |
+-------------------------------
+-------------------------------+
| 2016-05-20 11:23:35.439631000 |
2016-05-20 05:23:35.439631000 |
+-------------------------------
+-------------------------------+
select date_sub(cast('2016-05-31' as
timestamp), interval 1 months) as
'april_31st';
+---------------------+
| april_31st |
+---------------------+
| 2016-04-30 00:00:00 |
+---------------------+
DATE_TRUNC(STRING unit, TIMESTAMP / Purpose: Returns the ts value truncated to the specified
DATE ts) unit.
Argument: The unit argument is not case-sensitive.
This argument string can be one of:
DATEDIFF(TIMESTAMP / DATE enddate, Purpose: Returns the number of days from startdate to
TIMESTAMP / DATE startdate) enddate.
If enddate > startdate, the return value is positive.
If enddate < startdate, the return value is negative.
If enddate = startdate, the return value is zero.
Return type: INT
Usage notes:
The time portions of the enddate and startdate values are
ignored. For example, 11:59 PM on one day and 12:01
AM on the next day represent a DATEDIFF() of -1
because the date/time values represent different days,
even though the TIMESTAMP values differ by only 2
minutes.
DAY(TIMESTAMP / DATE date), Purpose: Returns the day value from the date argument.
DAYOFMONTH(TIMESTAMP / DATE date) The value represents the day of the month, therefore is in
the range 1-31, or less for months without 31 days.
Returns NULL for nonexistent dates, e.g. Feb 30, or
misformatted date strings, e.g. '1999-02-013'.
Return type: INT
DAYNAME(TIMESTAMP / DATE date) Purpose: Returns the day name of the date argument.
The range of return values is 'Sunday' to
'Saturday'. Used in report-generating queries, as
an alternative to calling DAYOFWEEK() and turning
that numeric return value into a string using a CASE
expression.
Return type: STRING
| Impala SQL Language Reference | 464
DAYOFWEEK(TIMESTAMP / DATE date) Purpose: Returns the day field of the date arguement,
corresponding to the day of the week. The range of return
values is 1 (Sunday) to 7 (Saturday).
Return type: INT
DAYOFYEAR(TIMESTAMP / DATE date) Purpose: Returns the day field from the date argument,
corresponding to the day of the year. The range of return
values is 1 (January 1) to 366 (December 31 of a leap
year).
Return type: INT
DAYS_ADD(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of days
BIGINT days) added to date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
DAYS_SUB(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of days
BIGINT days) subtracted from date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
EXTRACT(TIMESTAMP / DATE ts, STRING unit), Purpose: Returns one of the numeric date or time fields,
EXTRACT(unit FROM TIMESTAMP / DATE ts) specified by unit, from ts.
Argument: The unit argument value is not case-
sensitive. The unit string can be one of:
Usage notes:
Typically used in GROUP BY queries to arrange results
by hour, day, month, and so on. You can also use this
function in an INSERT ... SELECT statement to
insert into a partitioned table to split up TIMESTAMP
values into individual parts, if the partitioned table has
| Impala SQL Language Reference | 465
FROM_UNIXTIME(BIGINT unixtime[, STRING Purpose: Converts the number of seconds from the Unix
format]) epoch to the specified time into a string in the local time
zone.
Return type: STRING
In Impala 2.2.0 and higher, built-in functions that accept
or return integers representing TIMESTAMP values
use the BIGINT type for parameters and return values,
rather than INT. This change lets the date and time
functions avoid an overflow error that would otherwise
occur on January 19th, 2038 (known as the “Year 2038
problem” or “Y2K38 problem”). This change affects
the FROM_UNIXTIME() and UNIX_TIMESTAMP()
functions. You might need to change application code
that interacts with these functions, change the types of
columns that store the return values, or add CAST()
calls to SQL statements that call these functions.
Usage notes:
| Impala SQL Language Reference | 466
HOURS_ADD(TIMESTAMP date, INT hours), Purpose: Returns the specified date and time plus some
HOURS_ADD(TIMESTAMP date, BIGINT hours) number of hours.
Return type: TIMESTAMP
HOURS_SUB(TIMESTAMP date, INT hours), Purpose: Returns the specified date and time minus
HOURS_SUB(TIMESTAMP date, BIGINT hours) some number of hours.
Return type: TIMESTAMP
LAST_DAY(TIMESTAMP / DATE ts) Purpose: Returns the beginning of the last calendar day
in the same month of ts.
Return type:
• Returns TIMESTAMP if ts is of the TIMESTAMP
type.
• Returns DATE if ts is of the DATE type.
Added in: Impala 2.9.0
Usage notes:
If the input argument does not represent a valid Impala
TIMESTAMP including both date and time portions,
the function returns NULL. For example, if the input
| Impala SQL Language Reference | 468
MICROSECONDS_ADD(TIMESTAMP Purpose: Returns the specified date and time plus some
date, INT microseconds), number of microseconds.
MICROSECONDS_ADD(TIMESTAMP date,
Return type: TIMESTAMP
BIGINT microseconds)
MICROSECONDS_SUB(TIMESTAMP Purpose: Returns the specified date and time minus
date, INT microseconds), some number of microseconds.
MICROSECONDS_SUB(TIMESTAMP date,
Return type: TIMESTAMP
BIGINT microseconds)
MILLISECOND(TIMESTAMP ts) Purpose: Returns the millisecond portion of a
TIMESTAMP value.
Return type: INT
Added in: Impala 2.5.0
Usage notes:
The millisecond value is truncated, not rounded, if the
TIMESTAMP value contains more than 3 significant
digits to the right of the decimal point.
MILLISECONDS_ADD(TIMESTAMP date, INT Purpose: Returns the specified date and time plus some
milliseconds), MILLISECONDS_ADD(TIMESTAMP number of milliseconds.
date, BIGINT milliseconds)
Return type: TIMESTAMP
MILLISECONDS_SUB(TIMESTAMP date, INT Purpose: Returns the specified date and time minus
milliseconds), MILLISECONDS_SUB(TIMESTAMP some number of milliseconds.
date, BIGINT milliseconds)
Return type: TIMESTAMP
MINUTES_ADD(TIMESTAMP date, INT minutes), Purpose: Returns the specified date and time plus some
MINUTES_ADD(TIMESTAMP date, BIGINT number of minutes.
minutes)
Return type: TIMESTAMP
MINUTES_SUB(TIMESTAMP date, INT minutes), Purpose: Returns the specified date and time minus
MINUTES_SUB(TIMESTAMP date, BIGINT some number of minutes.
minutes)
Return type: TIMESTAMP
MONTHNAME(TIMESTAMP / DATE date) Purpose: Returns the month name of the date argument.
Return type: STRING
MONTHS_ADD(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of months
BIGINT months) added to date.
| Impala SQL Language Reference | 469
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
Usage notes:
If date is the last day of a month, the return date
will fall on the last day of the target month, e.g.
MONTHS_ADD(DATE'2019-01-31', 1) returns
DATE'2019-02-28'.
MONTHS_SUB(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of months
BIGINT months) subtracted from date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
Usage notes:
If date is the last day of a month, the return date
will fall on the last day of the target month, e.g.
MONTHS_SUB(DATE'2019-02-28', 1) returns
DATE'2019-01-31'.
| Impala SQL Language Reference | 470
NANOSECONDS_ADD(TIMESTAMP date, INT Purpose: Returns the specified date and time plus some
nanoseconds), NANOSECONDS_ADD(TIMESTAMP number of nanoseconds.
date, BIGINT nanoseconds)
Return type: TIMESTAMP
Kudu considerations:
The nanosecond portion of an Impala TIMESTAMP value
is rounded to the nearest microsecond when that value is
stored in a Kudu table.
NANOSECONDS_SUB(TIMESTAMP date, INT Purpose: Returns the specified date and time minus
nanoseconds), NANOSECONDS_SUB(TIMESTAMP some number of nanoseconds.
date, BIGINT nanoseconds)
Return type: TIMESTAMP
Kudu considerations:
The nanosecond portion of an Impala TIMESTAMP value
is rounded to the nearest microsecond when that value is
stored in a Kudu table.
NEXT_DAY(TIMESTAMP / DATE date, STRING Purpose: Returns the date of the weekday that follows
weekday) the specified date.
Argument: The weekday is not case-sensitive.
The following values are accepted for weekday:
"Sunday"/"Sun", "Monday"/"Mon",
"Tuesday"/"Tue", "Wednesday"/"Wed",
"Thursday"/"Thu", "Friday"/"Fri",
"Saturday"/"Sat"
Return type:
• Returns TIMESTAMP if date is of the TIMESTAMP
type.
• Returns DATE if date is of the DATE type.
Examples:
NEXT_DAY('2013-12-25','Saturday')
returns '2013-12-28 00:00:00' which is the first
Saturday after December 25, 2013.
NOW() Purpose: Returns the current date and time (in the local
time zone) as a TIMESTAMP value.
Return type: TIMESTAMP
Usage notes:
To find a date/time value in the future or the past
relative to the current date and time, add or subtract an
INTERVAL expression to the return value of NOW().
See TIMESTAMP Data Type on page 125 for
examples.
To produce a TIMESTAMP representing the current
date and time that can be shared or stored without
interoperability problems due to time zone differences,
use the TO_UTC_TIMESTAMP() function and specify
the time zone of the server. When TIMESTAMP data
is stored in UTC form, any application that queries
| Impala SQL Language Reference | 471
QUARTER(TIMESTAMP / DATE date) Purpose: Returns the quarter in the input date argument
as an integer value, 1, 2, 3, or 4, where 1 represents
January 1 through March 31.
Return type: INT
SECONDS_ADD(TIMESTAMP date, INT seconds), Purpose: Returns the specified date and time plus some
SECONDS_ADD(TIMESTAMP date, BIGINT number of seconds.
seconds)
Return type: TIMESTAMP
SECONDS_SUB(TIMESTAMP date, INT seconds), Purpose: Returns the specified date and time minus
SECONDS_SUB(TIMESTAMP date, BIGINT some number of seconds.
seconds)
Return type: TIMESTAMP
SUBDATE(TIMESTAMP / DATE date, INT / Purpose: Subtracts days from date and returns the new
BIGINT days) date value.
The days value can be negative, which gives the same
result as the ADDDATE() function.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
select to_timestamp('1984/09/25',
'yyyy/MM/dd');
+------------------------------------------
+
| to_timestamp('1984/09/25', 'yyyy/
mm/dd') |
+------------------------------------------
+
| 1984-09-25 00:00:00
|
+------------------------------------------
+
+-------------------------------
+----------------
+---------------------+
TRUNC(TIMESTAMP / DATE ts, STRING unit) Purpose: Returns the ts truncated to the unit specified.
Argument: The unit argument is not case-sensitive. This
argument string can be one of:
'YYYY'
'YEAR'
'SYEAR'
'YYY'
'YY'
'Y'
'MON'
'MM'
'RM'
'DD'
'J'
TRUNC(DATE'2019-05-08','YEAR') returns
2019-01-01.
TRUNC(DATE'2019-05-08', 'QUARTER')
returns 2019-04-01.
SELECT
FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()
+ interval 3 days),
'yyyy/MM/dd HH:mm') AS
yyyy_mm_dd_hh_mm;
+------------------+
| yyyy_mm_dd_hh_mm |
| Impala SQL Language Reference | 478
+------------------+
| 2016/06/03 11:38 |
+------------------+
select unix_timestamp('2015-05-15
12:00:00');
+---------------------------------------
+
| unix_timestamp('2015-05-15
12:00:00') |
+---------------------------------------
+
| 1431691200
|
+---------------------------------------
+
select unix_timestamp('2015-05-15
12:00:00Z');
+----------------------------------------
+
| unix_timestamp('2015-05-15
12:00:00z') |
+----------------------------------------
+
| 1431691200
|
| Impala SQL Language Reference | 479
+----------------------------------------
+
select unix_timestamp
(
'May 15, 2015 12:00:00',
'MMM dd, yyyy HH:mm:ss'
) as may_15_month_day_year;
+-----------------------+
| may_15_month_day_year |
+-----------------------+
| 1431691200 |
+-----------------------+
select unix_timestamp
(
'2015-05-15 12:00:00-07:00',
'yyyy-MM-dd HH:mm:ss-hh:mm'
) as may_15_year_month_day;
+-----------------------+
| may_15_year_month_day |
+-----------------------+
| 1431716400 |
+-----------------------+
select unix_timestamp
(to_utc_timestamp(
'2015-05-15 12:00:00',
'PDT')
) as may_15_pdt;
+------------+
| may_15_pdt |
+------------+
| 1431716400 |
+------------+
UNIX_MICROS_TO_UTC_TIMESTAMP() and
UTC_TO_UNIX_MICROS() functions. The integer
values represent the number of microseconds since the
Unix epoch (midnight on January 1, 1970).
Examples:
The following example shows how NOW() and
CURRENT_TIMESTAMP() represent the current date/
time in the local time zone (in this case, UTC-7), while
utc_timestamp() represents the same date/time in
the standardized UTC time zone:
select current_timestamp(),
utc_timestamp();
+-------------------------------
+-------------------------------+
| current_timestamp() |
utc_timestamp() |
+-------------------------------
+-------------------------------+
| 2017-10-01 23:34:07.400642000 |
2017-10-02 06:34:07.400642000 |
+-------------------------------
+-------------------------------+
WEEK(TIMESTAMP / DATE date), Purpose: Returns the corresponding week (1-53) from
WEEKOFYEAR(TIMESTAMP / DATE date) the date argument.
Return type: INT
WEEKS_ADD(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of weeks
BIGINT weeks) added to date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
WEEKS_SUB(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of weeks
BIGINT weeks) subtracted from date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
YEAR(TIMESTAMP / DATE date) Purpose: Returns the year field from the date argument.
Return type: INT
| Impala SQL Language Reference | 481
YEARS_ADD(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of years
BIGINT years) added to date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
Usage notes:
If the equivalent date does not exist in the year of the
result due to a leap year, the date is changed to the last
day of the appropriate month.
YEARS_SUB(TIMESTAMP / DATE date, INT / Purpose: Returns the value with the number of years
BIGINT years) subtracted from date.
Return type:
• If date is TIMESTAMP, returns TIMESTAMP.
• If date is DATE, returns DATE.
Usage notes:
If the equivalent date does not exist in the year of the
result due to a leap year, the date is changed to the last
day of the appropriate month.
CASE a WHEN b THEN c [WHEN d THEN e]... Purpose: Compares an expression to one or more
[ELSE f] END possible values, and returns a corresponding result when
a match is found.
Return type: same as the initial argument value,
except that integer values are promoted to BIGINT
| Impala SQL Language Reference | 482
select case x
when 1 then 'one'
when 2 then 'two'
when 0 then 'zero'
else 'out of range'
end
from t1;
CASE WHEN a THEN b [WHEN c THEN d]... Purpose: Tests whether any of a sequence of expressions
[ELSE e] END is TRUE, and returns a corresponding result for the first
true expression.
Return type: same as the initial argument value,
except that integer values are promoted to BIGINT
and floating-point values are promoted to DOUBLE; use
CAST() when inserting into a smaller numeric column
Usage notes:
CASE expressions without an initial test value have
more flexibility. For example, they can test different
columns in different WHEN clauses, or use comparison
operators such as BETWEEN, IN and IS NULL rather
than comparing against discrete values.
CASE expressions are often the foundation of long
queries that summarize and format results for easy-
to-read reports. For example, you might use a CASE
function call to turn values from a numeric column into
category strings corresponding to integer values, or
labels such as “Small”, “Medium” and “Large” based
on ranges. Then subsequent parts of the query might
aggregate based on the transformed values, such as how
many values are classified as small, medium, or large.
You can also use CASE to signal problems with out-of-
bounds values, NULL values, and so on.
By using operators such as OR, IN, REGEXP, and so
on in CASE expressions, you can build extensive tests
and transformations into a single query. Therefore,
applications that construct SQL statements often rely
heavily on CASE calls in the generated SQL code.
| Impala SQL Language Reference | 483
select case
when dayname(now()) in
('Saturday','Sunday') then 'result
undefined on weekends'
when x > y then 'x greater than
y'
when x = y then 'x and y are
equal'
when x is null or y is null then
'one of the columns is null'
else null
end
from t1;
COALESCE(type v1, type v2, ...) Purpose: Returns the first specified argument that is not
NULL, or NULL if all arguments are NULL.
Return type: same as the initial argument value,
except that integer values are promoted to BIGINT
and floating-point values are promoted to DOUBLE; use
CAST() when inserting into a smaller numeric column
DECODE(type expression, type search1, type result1 Purpose: Compares the first argument, expression,
[, type search2, type result2 ...] [, type default] ) to the search expressions using the IS NOT
DISTINCT operator, and returns:
• The corresponding result when a match is found.
• The first corresponding result if there are more
than one matching search expressions.
• The default expression if none of the
search expressions matches the first argument
expression.
• NULL if the final default expression is omitted
and none of the search expressions matches the
first argument.
Return type: Same as the first argument with the
following exceptions:
• Integer values are promoted to BIGINT.
• Floating-point values are promoted to DOUBLE.
| Impala SQL Language Reference | 484
IF(BOOLEAN condition, type ifTrue, type Purpose: Tests an expression and returns a
ifFalseOrNull) corresponding result depending on whether the result is
TRUE, FALSE, or NULL.
Return type: Same as the ifTrue argument value
IFNULL(type a, type ifNull) Purpose: Alias for the ISNULL() function, with the
same behavior. To simplify porting SQL with vendor
extensions to Impala.
Added in: Impala 1.3.0
NULLIF(type expr1, type expr2) Purpose: Returns NULL if the two specified arguments
are equal. If the specified arguments are not equal,
returns the value of expr1. The data types of the
expressions must be compatible, according to the
conversion rules from Data Types on page 94. You
cannot use an expression that evaluates to NULL for
expr1; that way, you can distinguish a return value of
NULL from an argument value of NULL, which would
never match expr2.
Usage notes: This function is effectively shorthand for a
CASE expression of the form:
CASE
WHEN expr1 = expr2 THEN NULL
ELSE expr1
END
NVL(type a, type ifNull) Purpose: Alias for the ISNULL() function. Returns the
first argument if the first argument is not NULL. Returns
the second argument if the first argument is NULL.
Equivalent to the NVL() function in Oracle Database or
IFNULL() in MySQL.
Return type: Same as the first argument value
Added in: Impala 1.1
NVL2(type a, type ifNotNull, type ifNull) Purpose: Returns the second argument, ifNotNull,
if the first argument is not NULL. Returns the third
argument, ifNull, if the first argument is NULL.
Equivalent to the NVL2() function in Oracle Database.
Return type: Same as the first argument value
Added in: Impala 2.9.0
Examples:
• TRANSLATE
• TRIM
• UPPER, UCASE
ASCII(STRING str) Purpose: Returns the numeric ASCII code of the first
character of the argument.
Return type: INT
select base64encode('circumflex
accents: â, ê, î, ô, û') as
encoded,
base64decode(base64encode('circumflex
accents: â, ê, î, ô, û')) as
decoded;
+-----------------------------------------------
+-----------------------------------
+
| encoded
| decoded
|
+-----------------------------------------------
+-----------------------------------
+
|
Y2lyY3VtZmxleCBhY2NlbnRzOiDDoiwgw6osIMOuLCDDtCw
| circumflex accents: â, ê, î, ô, û
|
+-----------------------------------------------
+-----------------------------------
+
select base64encode('circumflex
accents: â, ê, î, ô, û') as
encoded,
| Impala SQL Language Reference | 494
base64decode(base64encode('circumflex
accents: â, ê, î, ô, û')) as
decoded;
+-----------------------------------------------
+-----------------------------------
+
| encoded
| decoded
|
+-----------------------------------------------
+-----------------------------------
+
|
Y2lyY3VtZmxleCBhY2NlbnRzOiDDoiwgw6osIMOuLCDDtCw
| circumflex accents: â, ê, î, ô, û
|
+-----------------------------------------------
+-----------------------------------
+
BTRIM(STRING a), BTRIM(STRING a, STRING Purpose: Removes all instances of one or more
chars_to_trim) characters from the start and end of a STRING value. By
default, removes only spaces. If a non-NULL optional
second argument is specified, the function removes all
occurrences of characters in that second argument from
the beginning and end of the string.
Return type: STRING
Added in: Impala 2.3.0
Examples:
The following examples show the default BTRIM()
behavior, and what changes when you specify the
optional second argument. All the examples bracket the
output value with [ ] so that you can see any leading
or trailing spaces in the BTRIM() result. By default,
the function removes and number of both leading and
trailing spaces. When the second argument is specified,
any number of occurrences of any character in the second
argument are removed from the start and end of the input
string; in this case, spaces are not removed (unless they
are part of the second argument) and any instances of the
characters are not removed if they do not come right at
the beginning or end of the string.
('abc',cast('abc' as char(5))),
('hello',cast('hello' as
char(5)));
select concat('"',s,'"') as s,
concat('"',c,'"') as c,
length(s), length(c),
char_length(s), char_length(c)
from length_demo;
+---------+---------+-----------
+-----------+----------------
+----------------+
| s | c | length(s)
| length(c) | char_length(s) |
char_length(c) |
+---------+---------+-----------
+-----------+----------------
+----------------+
| "a" | "a " | 1 | 1
| 1 | 5
|
| "abc" | "abc " | 3 | 3
| 3 | 5
|
| "hello" | "hello" | 5 | 5
| 5 | 5
|
+---------+---------+-----------
+-----------+----------------
+----------------+
SELECT chr(65);
+---------+
| chr(65) |
+---------+
| A |
+---------+
SELECT chr(97);
+---------+
| chr(97) |
+---------+
| a |
| Impala SQL Language Reference | 497
+---------+
CONCAT(STRING a, STRING b...) Purpose: Returns a single string representing all the
argument values joined together.
Return type: STRING
Usage notes: concat() and concat_ws()
are appropriate for concatenating the values of
multiple columns within the same row, while
group_concat() joins together values from different
rows.
CONCAT_WS(STRING sep, STRING a, STRING Purpose: Returns a single string representing the second
b...) and following argument values joined together, delimited
by a specified separator.
Return type: STRING
Usage notes: concat() and concat_ws()
are appropriate for concatenating the values of
multiple columns within the same row, while
group_concat() joins together values from different
rows.
FIND_IN_SEt(STRING str, STRING strList) Purpose: Returns the position (starting from 1) of the
first occurrence of a specified string within a comma-
separated string. Returns NULL if either argument is
NULL, 0 if the search string is not found, or 0 if the
search string contains a comma.
Return type: INT
INITCAP(STRING str) Purpose: Returns the input string with the first letter of
each word capitalized and all other letters in lowercase.
Return type: STRING
| Impala SQL Language Reference | 498
Example:
INITCAP("i gOt mY ChiCkeNs in tHe
yard.") returns " I Got My Chickens In The
Yard.".
INSTR(STRING str, STRING substr [, BIGINT Purpose: Returns the position (starting from 1) of the
position [, BIGINT occurrence ] ]) first occurrence of a substring within a longer string.
Return type: INT
Usage notes:
If the substring is not present in the string, the function
returns 0:
+----------------------------------
+
+------------------------------------
+
select instr('aaaaaaaaa','aa', 1,
-1);
ERROR: UDF ERROR: Invalid
occurrence parameter to instr
function: -1
• If either of the optional arguments is NULL, the
function also returns NULL:
Examples:
The following example demonstrates how length()
and char_length() sometimes produce the
same result, and sometimes produce different results
depending on the type of the argument and the presence
of trailing spaces for CHAR values. The S and C values
are displayed with enclosing quotation marks to show
any trailing spaces.
select concat('"',s,'"') as s,
concat('"',c,'"') as c,
length(s), length(c),
char_length(s), char_length(c)
from length_demo;
+---------+---------+-----------
+-----------+----------------
+----------------+
| s | c | length(s)
| length(c) | char_length(s) |
char_length(c) |
+---------+---------+-----------
+-----------+----------------
+----------------+
| "a" | "a " | 1 | 1
| 1 | 5
|
| "abc" | "abc " | 3 | 3
| 3 | 5
|
| "hello" | "hello" | 5 | 5
| 5 | 5
|
+---------+---------+-----------
+-----------+----------------
+----------------+
LEVENSHTEIN(STRING str1, STRING str2), Purpose: Returns the Levenshtein distance between two
LE_DST(STRING str1, STRING str2) input strings. The Levenshtein distance between two
strings is the minimum number of single-character edits
required to transform one string to other. The function
indicates how different the input strings are.
Return type: INT
Usage notes:
If input strings are equal, the function returns 0.
If either input exceeds 255 characters, the function
returns an error.
If either input string is NULL, the function returns NULL.
| Impala SQL Language Reference | 503
LOCATE(STRING substr, STRING str[, INT pos]) Purpose: Returns the position (starting from 1) of the
first occurrence of a substring within a longer string,
optionally after a particular position.
Return type: INT
LOWER(STRING a), LCASE(STRING a) Purpose: Returns the argument string converted to all-
lowercase.
Return type: STRING
Usage notes:
In Impala 2.5 and higher, you can simplify queries
that use many UPPER() and LOWER() calls to do
case-insensitive comparisons, by using the ILIKE or
IREGEXP operators instead. See ILIKE Operator on
page 178 and IREGEXP Operator on page 182 for
details.
LPAD(STRING str, INT len, STRING pad) Purpose: Returns a string of a specified length, based
on the first argument string. If the specified string is too
short, it is padded on the left with a repeating sequence
of the characters from the pad string. If the specified
string is too long, it is truncated on the right.
Return type: STRING
LTRIM(STRING a [, STRING chars_to_trim]) Purpose: Returns the argument string with all
occurrences of characters specified by the second
argument removed from the left side. Removes spaces if
the second argument is not specified.
Return type: STRING
.\+*?[^]$(){}=!<>|:-
+-----------------------------------------------
+
| regexp_escape('Hello.world')
|
+-----------------------------------------------
+
| Hello\.world
|
+-----------------------------------------------
+
+-----------------------------------------------
+
| regexp_escape('a.b\\c+d*e?f[g]h
$i(j)k{l}m=n!o<p>q|r:s-t') |
| Impala SQL Language Reference | 505
+-----------------------------------------------
+
| a\.b\\c\+d\*e\?f\[g\]h\$i\(j\)k\{l
\}m\=n\!o\<p\>q\|r\:s\-t |
+-----------------------------------------------
+
REGEXP_EXTRACT(STRING subject, STRING Purpose: Returns the specified () group from a string
pattern, INT index) based on a regular expression pattern. Group 0 refers to
the entire extracted string, while group 1, 2, and so on
refers to the first, second, and so on (...) portion.
Return type: STRING
In Impala 2.0 and later, the Impala regular expression
syntax conforms to the POSIX Extended Regular
Expression syntax used by the Google RE2 library. For
details, see the RE2 documentation. It has most idioms
familiar from regular expressions in Perl, Python, and so
on, including .*? for non-greedy matches.
In Impala 2.0 and later, a change in the underlying
regular expression library could cause changes in the
way regular expressions are interpreted by this function.
Test any queries that use regular expressions and adjust
the expression patterns if necessary. See Incompatible
Changes Introduced in Impala 2.0.0 on page 832 for
details.
Because the impala-shell interpreter uses the \
character for escaping, use \\ to represent the regular
expression escape character in any regular expressions
that you submit through impala-shell . You might
prefer to use the equivalent character class names, such
as [[:digit:]] instead of \d which you would have
to escape as \\d.
Examples:
This example shows how group 0 matches the full
pattern string, including the portion outside any ()
group:
REGEXP_LIKE(STRING source, STRING pattern[, Purpose: Returns true or false to indicate whether
STRING options]) the source string contains anywhere inside it the regular
expression given by the pattern. The optional third
argument consists of letter flags that change how the
| Impala SQL Language Reference | 507
select regexp_like('foo','o$');
+--------------------------+
| regexp_like('foo', 'o$') |
+--------------------------+
| true |
+--------------------------+
select
regexp_like('foooooobar','fx*y*o*b');
+---------------------------------------
+
| regexp_like('foooooobar',
'fx*y*o*b') |
+---------------------------------------
+
| true
|
+---------------------------------------
+
| Impala SQL Language Reference | 509
REGEXP_REPLACE(STRING initial, STRING Purpose: Returns the initial argument with the regular
pattern, STRING replacement) expression pattern replaced by the final argument string.
Return type: STRING
In Impala 2.0 and later, the Impala regular expression
syntax conforms to the POSIX Extended Regular
Expression syntax used by the Google RE2 library. For
details, see the RE2 documentation. It has most idioms
familiar from regular expressions in Perl, Python, and so
on, including .*? for non-greedy matches.
In Impala 2.0 and later, a change in the underlying
regular expression library could cause changes in the
way regular expressions are interpreted by this function.
Test any queries that use regular expressions and adjust
the expression patterns if necessary. See Incompatible
Changes Introduced in Impala 2.0.0 on page 832 for
details.
Because the impala-shell interpreter uses the \
character for escaping, use \\ to represent the regular
expression escape character in any regular expressions
that you submit through impala-shell . You might
prefer to use the equivalent character class names, such
as [[:digit:]] instead of \d which you would have
to escape as \\d.
Examples:
These examples show how you can replace parts of a
string matching a pattern with replacement text, which
can include backreferences to any () groups in the
pattern string. The backreference numbers start at 1, and
any \ characters must be escaped as \\.
Replace a character pattern with new text:
+----------------------------------------------
+
| regexp_replace('aaabbbaaa', '(b
+)', '<\\1>') |
+----------------------------------------------
+
| aaa<bbb>aaa
|
+----------------------------------------------
+
Returned 1 row(s) in 0.11s
REPLACE(STRING initial, STRING target, Purpose: Returns the initial argument with all
STRING replacement) occurrences of the target string replaced by the
replacement string.
Return type: STRING
Usage notes:
Because this function does not use any regular
expression patterns, it is typically faster than
regexp_replace() for simple string substitutions.
If any argument is NULL, the return value is NULL.
Matching is case-sensitive.
If the replacement string contains another instance of
the target string, the expansion is only performed once,
instead of applying again to the newly constructed string.
Added in: Impala 2.9.0
Examples:
+------------------------------------------
+
| hello earth
|
+------------------------------------------
+
RTRIM(STRING a [, STRING chars_to_trim]) Purpose: Returns the argument string with all
occurrences of characters specified by the second
argument removed from the right side. Removes spaces
if the second argument is not specified.
Return type: STRING
SPLIT_PART(STRING source, STRING delimiter, Purpose: Returns the requested indexth part of the
BIGINT index) input source string split by the delimiter.
| Impala SQL Language Reference | 512
STRLEFT(STRING a, INT num_chars) Purpose: Returns the leftmost characters of the string.
Shorthand for a call to SUBSTR() with 2 arguments.
Return type: STRING
STRRIGHT(STRING a, INT num_chars) Purpose: Returns the rightmost characters of the string.
Shorthand for a call to SUBSTR() with 2 arguments.
Return type: STRING
SUBSTR(STRING a, INT start [, INT len]), Purpose: Returns the portion of the string starting at a
SUBSTRING(STRING a, INT start [, INT len]) specified point, optionally with a specified maximum
length. The characters in the string are indexed starting at
1.
Return type: STRING
TRANSLATE(STRING input, STRING from, Purpose: Returns the input string with each character
STRING to) in the from argument replaced with the corresponding
character in the to argument. The characters are matched
in the order they appear in from and to.
For example: translate ('hello
world','world','earth') returns 'hetta
earth'.
Return type: STRING
Usage notes:
If from contains more characters than to, the from
characters that are beyond the length of to are removed
in the result.
For example:
translate('abcdedg', 'bcd', '1') returns
'a1eg'.
| Impala SQL Language Reference | 513
TRIM(STRING a) Purpose: Returns the input string with both leading and
trailing spaces removed. The same as passing the string
through both LTRIM() and RTRIM().
Usage notes: Often used during data cleansing
operations during the ETL cycle, if input values might
still have surrounding spaces. For a more general-
purpose function that can remove other leading and
trailing characters besides spaces, see BTRIM().
Return type: STRING
UPPER(STRING a), UCASE(STRING a) Purpose: Returns the argument string converted to all-
uppercase.
Return type: STRING
Usage notes:
In Impala 2.5 and higher, you can simplify queries
that use many UPPER() and LOWER() calls to do
case-insensitive comparisons, by using the ILIKE or
IREGEXP operators instead. See ILIKE Operator on
page 178 and IREGEXP Operator on page 182 for
details.
GET_JSON_OBJECT(STRING json_str, STRING Purpose: Extracts JSON object from the json_str based
selector) on the selector JSON path and returns the string of the
extracted JSON object.
The function returns NULL if the input json_str is invalid
or if nothing is selected based on the selector JSON path.
The following characters are supported in the selector
JSON path:
• $ : Denotes the root object
• . : Denotes the child operator
• [] : Denotes the subscript operator for array
• * : Denotes the wildcard for [] or .
Return type: STRING
Examples:
---- QUERY
SELECT GET_JSON_OBJECT ('{"a":true,
"b":false, "c":true}', '$.*');
---- RESULTS
[true,false,true]
---- QUERY
SELECT GET_JSON_OBJECT(t.json,
'$.a.b.c') FROM (VALUES (
('{"a": {"b": {"c": 1}}}' AS json),
('{"a": {"b": {"c": 2}}}'),
('{"a": {"b": {"c": 3}}}')
)) t
---- RESULTS
'1'
'2'
'3'
---- QUERY
SELECT GET_JSON_OBJECT(t.json,
'$.a'),
GET_JSON_OBJECT(t.json, '$.b'),
GET_JSON_OBJECT(t.json, '$.c')
FROM (VALUES (
('{"a":1, "b":2, "c":3}' AS json),
('{"b":2, "c":3}'),
('{"c":3}')
)) t
---- RESULTS
'1','2','3'
'NULL','2','3'
| Impala SQL Language Reference | 515
'NULL','NULL','3'
---- QUERY
SELECT GET_JSON_OBJECT(t.json,
'$[1]'),
GET_JSON_OBJECT(t.json, '$[*]')
FROM (VALUES (
('["a", "b", "c"]' AS json),
('["a", "b"]'),
('["a"]')
)) t
---- RESULTS
'b','["a","b","c"]'
'b','["a","b"]'
'NULL','a'
Aggregate functions also ignore NULL values rather than returning a NULL result. For example, if some rows have
NULL for a particular column, those rows are ignored when computing the AVG() for that column. Likewise,
specifying COUNT(col_name) in a query counts only those rows where col_name contains a non-NULL value.
APPX_MEDIAN Function
An aggregate function that returns a value that is approximately the median (midpoint) of values in the set of input
values.
Syntax:
This function works with any input type, because the only requirement is that the type supports less-than and greater-
than comparison operators.
Usage notes:
Because the return value represents the estimated midpoint, it might not reflect the precise midpoint value, especially
if the cardinality of the input values is very high. If the cardinality is low (up to approximately 20,000), the result is
more accurate because the sampling considers all or almost all of the different values.
Return type: Same as the input value, except for CHAR and VARCHAR arguments which produce a STRING result
The return value is always the same as one of the input values, not an “in-between” value produced by averaging.
Restrictions:
This function cannot be used in an analytic context. That is, the OVER() clause is not allowed at all with this
function.
The APPX_MEDIAN function returns only the first 10 characters for string values (string, varchar, char). Additional
characters are truncated.
Examples:
The following example uses a table of a million random floating-point numbers ranging up to approximately 50,000.
The average is approximately 25,000. Because of the random distribution, we would expect the median to be close to
this same number. Computing the precise median is a more intensive operation than computing the average, because
it requires keeping track of every distinct value and how many times each occurs. The APPX_MEDIAN() function
uses a sampling algorithm to return an approximate result, which in this case is close to the expected value. To make
sure that the value is not substantially out of range due to a skewed distribution, subsequent queries confirm that there
are approximately 500,000 values higher than the APPX_MEDIAN() value, and approximately 500,000 values lower
than the APPX_MEDIAN() value.
+--------+
| 502013 |
+--------+
[localhost:21000] > select count(x) as lower from million_numbers where x <
(select appx_median(x) from million_numbers);
+--------+
| lower |
+--------+
| 497987 |
+--------+
The following example computes the approximate median using a subset of the values from the table, and then
confirms that the result is a reasonable estimate for the midpoint.
AVG Function
An aggregate function that returns the average value from a set of numbers or TIMESTAMP values. Its single
argument can be numeric column, or the numeric result of a function or expression applied to the column value. Rows
with a NULL value for the specified column are ignored. If the table is empty, or all the values supplied to AVG are
NULL, AVG returns NULL.
Syntax:
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Return type: DOUBLE for numeric values; TIMESTAMP for TIMESTAMP values
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
| Impala SQL Language Reference | 519
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
| Impala SQL Language Reference | 520
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| EUROPE | 5 | 77 | 15.4 | FRANCE | UNITED KINGDOM | 5
|
| MIDDLE EAST | 5 | 58 | 11.6 | EGYPT | SAUDI ARABIA | 5
|
+-------------+-------+-----+------+-----------+----------------
+---------------+
Examples:
The following examples show how to use AVG() in an analytic context. They use a table containing integers from 1
to 10. Notice how the AVG() is reported for each input value, as opposed to the GROUP BY clause which condenses
the result set.
| 1 | odd | 5 |
| 3 | odd | 5 |
| 5 | odd | 5 |
| 7 | odd | 5 |
| 9 | odd | 5 |
+----+----------+-----+
Adding an ORDER BY clause lets you experiment with results that are cumulative or apply to a moving set of rows
(the “window”). The following examples use AVG() in an analytic context (that is, with an OVER() clause) to
produce a running average of all the even values, then a running average of all the odd values. The basic ORDER BY
x clause implicitly activates a window clause of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW, which is effectively the same as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,
therefore all of these examples produce the same results:
select x, property,
avg(x) over (partition by property order by x) as 'cumulative average'
from int_t where property in ('odd','even');
+----+----------+--------------------+
| x | property | cumulative average |
+----+----------+--------------------+
| 2 | even | 2 |
| 4 | even | 3 |
| 6 | even | 4 |
| 8 | even | 5 |
| 10 | even | 6 |
| 1 | odd | 1 |
| 3 | odd | 2 |
| 5 | odd | 3 |
| 7 | odd | 4 |
| 9 | odd | 5 |
+----+----------+--------------------+
select x, property,
avg(x) over
(
partition by property
order by x
range between unbounded preceding and current row
) as 'cumulative average'
from int_t where property in ('odd','even');
+----+----------+--------------------+
| x | property | cumulative average |
+----+----------+--------------------+
| 2 | even | 2 |
| 4 | even | 3 |
| 6 | even | 4 |
| 8 | even | 5 |
| 10 | even | 6 |
| 1 | odd | 1 |
| 3 | odd | 2 |
| 5 | odd | 3 |
| 7 | odd | 4 |
| 9 | odd | 5 |
+----+----------+--------------------+
select x, property,
avg(x) over
(
partition by property
order by x
rows between unbounded preceding and current row
) as 'cumulative average'
| Impala SQL Language Reference | 522
The following examples show how to construct a moving window, with a running average taking into account 1 row
before and 1 row after the current row, within the same partition (all the even values or all the odd values). Because of
a restriction in the Impala RANGE syntax, this type of moving window is possible with the ROWS BETWEEN clause
but not the RANGE BETWEEN clause:
select x, property,
avg(x) over
(
partition by property
order by x
rows between 1 preceding and 1 following
) as 'moving average'
from int_t where property in ('odd','even');
+----+----------+----------------+
| x | property | moving average |
+----+----------+----------------+
| 2 | even | 3 |
| 4 | even | 4 |
| 6 | even | 6 |
| 8 | even | 8 |
| 10 | even | 9 |
| 1 | odd | 2 |
| 3 | odd | 3 |
| 5 | odd | 5 |
| 7 | odd | 7 |
| 9 | odd | 8 |
+----+----------+----------------+
Restrictions:
Due to the way arithmetic on FLOAT and DOUBLE columns uses high-performance hardware instructions, and
distributed queries can perform these operations in different order for each query, results can vary slightly for
aggregate function calls such as SUM() and AVG() for FLOAT and DOUBLE columns, particularly on large data
| Impala SQL Language Reference | 523
sets where millions or billions of values are summed or averaged. For perfect consistency and repeatability, use the
DECIMAL data type for such operations instead of FLOAT or DOUBLE.
Related information:
Impala Analytic Functions on page 547, MAX Function on page 529, MIN Function on page 533
COUNT Function
An aggregate function that returns the number of rows, or the number of non-NULL rows.
Syntax:
Depending on the argument, COUNT() considers rows that meet certain conditions:
• The notation COUNT(*) includes NULL values in the total.
• The notation COUNT(column_name) only considers rows where the column contains a non-NULL value.
• You can also combine COUNT with the DISTINCT operator to eliminate duplicates before counting, and to count
the combinations of values across multiple columns.
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Return type: BIGINT
Usage notes:
If you frequently run aggregate functions such as MIN(), MAX(), and COUNT(DISTINCT) on partition key
columns, consider enabling the OPTIMIZE_PARTITION_KEY_SCANS query option, which optimizes such queries.
This feature is available in Impala 2.5 and higher. See OPTIMIZE_PARTITION_KEY_SCANS Query Option (Impala
2.5 or higher only) on page 365 for the kinds of queries that this option applies to, and slight differences in how
partitions are evaluated when this query option is enabled.
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| EUROPE | 5 | 77 | 15.4 | FRANCE | UNITED KINGDOM | 5
|
| MIDDLE EAST | 5 | 58 | 11.6 | EGYPT | SAUDI ARABIA | 5
|
+-------------+-------+-----+------+-----------+----------------
+---------------+
| Impala SQL Language Reference | 525
Examples:
-- How many rows total are in the table, regardless of NULL values?
select count(*) from t1;
-- How many rows are in the table with non-NULL values for a column?
select count(c1) from t1;
-- Count the rows that meet certain conditions.
-- Again, * includes NULLs, so COUNT(*) might be greater than COUNT(col).
select count(*) from t1 where x > 10;
select count(c1) from t1 where x > 10;
-- Can also be used in combination with DISTINCT and/or GROUP BY.
-- Combine COUNT and DISTINCT to find the number of unique values.
-- Must use column names rather than * with COUNT(DISTINCT ...) syntax.
-- Rows with NULL values are not counted.
select count(distinct c1) from t1;
-- Rows with a NULL value in _either_ column are not counted.
select count(distinct c1, c2) from t1;
-- Return more than one result.
select month, year, count(distinct visitor_id) from web_stats group by
month, year;
The following examples show how to use COUNT() in an analytic context. They use a table containing integers
from 1 to 10. Notice how the COUNT() is reported for each input value, as opposed to the GROUP BY clause which
condenses the result set.
Adding an ORDER BY clause lets you experiment with results that are cumulative or apply to a moving set of rows
(the “window”). The following examples use COUNT() in an analytic context (that is, with an OVER() clause) to
produce a running count of all the even values, then a running count of all the odd values. The basic ORDER BY x
clause implicitly activates a window clause of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW, which is effectively the same as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,
therefore all of these examples produce the same results:
select x, property,
count(x) over (partition by property order by x) as 'cumulative count'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative count |
+----+----------+------------------+
| 2 | even | 1 |
| 4 | even | 2 |
| 6 | even | 3 |
| 8 | even | 4 |
| 10 | even | 5 |
| 1 | odd | 1 |
| 3 | odd | 2 |
| Impala SQL Language Reference | 526
| 5 | odd | 3 |
| 7 | odd | 4 |
| 9 | odd | 5 |
+----+----------+------------------+
select x, property,
count(x) over
(
partition by property
order by x
range between unbounded preceding and current row
) as 'cumulative total'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative count |
+----+----------+------------------+
| 2 | even | 1 |
| 4 | even | 2 |
| 6 | even | 3 |
| 8 | even | 4 |
| 10 | even | 5 |
| 1 | odd | 1 |
| 3 | odd | 2 |
| 5 | odd | 3 |
| 7 | odd | 4 |
| 9 | odd | 5 |
+----+----------+------------------+
select x, property,
count(x) over
(
partition by property
order by x
rows between unbounded preceding and current row
) as 'cumulative total'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative count |
+----+----------+------------------+
| 2 | even | 1 |
| 4 | even | 2 |
| 6 | even | 3 |
| 8 | even | 4 |
| 10 | even | 5 |
| 1 | odd | 1 |
| 3 | odd | 2 |
| 5 | odd | 3 |
| 7 | odd | 4 |
| 9 | odd | 5 |
+----+----------+------------------+
The following examples show how to construct a moving window, with a running count taking into account 1 row
before and 1 row after the current row, within the same partition (all the even values or all the odd values). Therefore,
the count is consistently 3 for rows in the middle of the window, and 2 for rows near the ends of the window, where
there is no preceding or no following row in the partition. Because of a restriction in the Impala RANGE syntax, this
type of moving window is possible with the ROWS BETWEEN clause but not the RANGE BETWEEN clause:
select x, property,
count(x) over
(
partition by property
order by x
| Impala SQL Language Reference | 527
Related information:
Impala Analytic Functions on page 547
GROUP_CONCAT Function
An aggregate function that returns a single string representing the argument value concatenated together for each row
of the result set. If the optional separator string is specified, the separator is added between each pair of concatenated
values. The default separator is a comma followed by a space.
Syntax:
Usage notes: concat() and concat_ws() are appropriate for concatenating the values of multiple columns
within the same row, while group_concat() joins together values from different rows.
By default, returns a single string covering the whole result set. To include other columns or values in the result set, or
to produce multiple concatenated strings for subsets of rows, include a GROUP BY clause in the query.
Return type: STRING
This function cannot be used in an analytic context. That is, the OVER() clause is not allowed at all with this
function.
Currently, Impala returns an error if the result value grows larger than 1 GiB.
Examples:
The following examples illustrate various aspects of the GROUP_CONCAT() function.
You can call the function directly on a STRING column. To use it with a numeric column, cast the value to STRING.
[localhost:21000] > insert into t1 values (1, "one"), (3, "three"), (2,
"two"), (1, "one");
[localhost:21000] > select group_concat(s) from t1;
+----------------------+
| group_concat(s) |
+----------------------+
| one, three, two, one |
+----------------------+
[localhost:21000] > select group_concat(cast(x as string)) from t1;
+---------------------------------+
| group_concat(cast(x as string)) |
+---------------------------------+
| 1, 3, 2, 1 |
+---------------------------------+
Specify the DISTINCT keyword to eliminate duplicate values from the concatenated result:
The optional separator lets you format the result in flexible ways. The separator can be an arbitrary string expression,
not just a single character.
The default separator is a comma followed by a space. To get a comma-delimited result without extra spaces, specify
a delimiter character that is only a comma.
Including a GROUP BY clause lets you produce a different concatenated result for each group in the result set.
In this example, the only X value that occurs more than once is 1, so that is the only row in the result set where
GROUP_CONCAT() returns a delimited value. For groups containing a single value, GROUP_CONCAT() returns the
original value of its STRING argument.
| 3 | three |
| 1 | one, one |
+---+-----------------+
MAX Function
An aggregate function that returns the maximum value from a set of numbers. Opposite of the MIN function. Its single
argument can be numeric column, or the numeric result of a function or expression applied to the column value. Rows
with a NULL value for the specified column are ignored. If the table is empty, or all the values supplied to MAX are
NULL, MAX returns NULL.
Syntax:
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Restrictions: In Impala 2.0 and higher, this function can be used as an analytic function, but with restrictions on
any window clause. For MAX() and MIN(), the window clause is only allowed if the start bound is UNBOUNDED
PRECEDING.
Return type: Same as the input value, except for CHAR and VARCHAR arguments which produce a STRING result
Usage notes:
If you frequently run aggregate functions such as MIN(), MAX(), and COUNT(DISTINCT) on partition key
columns, consider enabling the OPTIMIZE_PARTITION_KEY_SCANS query option, which optimizes such queries.
This feature is available in Impala 2.5 and higher. See OPTIMIZE_PARTITION_KEY_SCANS Query Option (Impala
2.5 or higher only) on page 365 for the kinds of queries that this option applies to, and slight differences in how
partitions are evaluated when this query option is enabled.
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
| r_name | item.n_nationkey |
+-------------+------------------+
| AFRICA | 0 |
| AFRICA | 5 |
| AFRICA | 14 |
| AFRICA | 15 |
| AFRICA | 16 |
| AMERICA | 1 |
| AMERICA | 2 |
| AMERICA | 3 |
| AMERICA | 17 |
| AMERICA | 24 |
| ASIA | 8 |
| ASIA | 9 |
| ASIA | 12 |
| ASIA | 18 |
| ASIA | 21 |
| EUROPE | 6 |
| EUROPE | 7 |
| EUROPE | 19 |
| EUROPE | 22 |
| EUROPE | 23 |
| MIDDLE EAST | 4 |
| MIDDLE EAST | 10 |
| MIDDLE EAST | 11 |
| MIDDLE EAST | 13 |
| MIDDLE EAST | 20 |
+-------------+------------------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| EUROPE | 5 | 77 | 15.4 | FRANCE | UNITED KINGDOM | 5
|
| MIDDLE EAST | 5 | 58 | 11.6 | EGYPT | SAUDI ARABIA | 5
|
+-------------+-------+-----+------+-----------+----------------
+---------------+
Examples:
The following examples show how to use MAX() in an analytic context. They use a table containing integers from 1
to 10. Notice how the MAX() is reported for each input value, as opposed to the GROUP BY clause which condenses
the result set.
Adding an ORDER BY clause lets you experiment with results that are cumulative or apply to a moving set of rows
(the “window”). The following examples use MAX() in an analytic context (that is, with an OVER() clause) to
display the smallest value of X encountered up to each row in the result set. The examples use two columns in the
ORDER BY clause to produce a sequence of values that rises and falls, to illustrate how the MAX() result only
increases or stays the same throughout each partition within the result set. The basic ORDER BY x clause implicitly
activates a window clause of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which is
effectively the same as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, therefore all of these
examples produce the same results:
select x, property,
max(x) over (order by property, x desc) as 'maximum to this point'
from int_t where property in ('prime','square');
+---+----------+-----------------------+
| x | property | maximum to this point |
+---+----------+-----------------------+
| 7 | prime | 7 |
| 5 | prime | 7 |
| 3 | prime | 7 |
| 2 | prime | 7 |
| 9 | square | 9 |
| 4 | square | 9 |
| 1 | square | 9 |
+---+----------+-----------------------+
select x, property,
max(x) over
(
| Impala SQL Language Reference | 532
select x, property,
max(x) over
(
order by property, x desc
range between unbounded preceding and current row
) as 'maximum to this point'
from int_t where property in ('prime','square');
+---+----------+-----------------------+
| x | property | maximum to this point |
+---+----------+-----------------------+
| 7 | prime | 7 |
| 5 | prime | 7 |
| 3 | prime | 7 |
| 2 | prime | 7 |
| 9 | square | 9 |
| 4 | square | 9 |
| 1 | square | 9 |
+---+----------+-----------------------+
The following examples show how to construct a moving window, with a running maximum taking into account all
rows before and 1 row after the current row. Because of a restriction in the Impala RANGE syntax, this type of moving
window is possible with the ROWS BETWEEN clause but not the RANGE BETWEEN clause. Because of an extra
Impala restriction on the MAX() and MIN() functions in an analytic context, the lower bound must be UNBOUNDED
PRECEDING.
select x, property,
max(x) over
(
order by property, x
rows between unbounded preceding and 1 following
) as 'local maximum'
from int_t where property in ('prime','square');
+---+----------+---------------+
| x | property | local maximum |
+---+----------+---------------+
| 2 | prime | 3 |
| 3 | prime | 5 |
| 5 | prime | 7 |
| 7 | prime | 7 |
| 1 | square | 7 |
| 4 | square | 9 |
| 9 | square | 9 |
+---+----------+---------------+
max(x) over
(
order by property, x
range between unbounded preceding and 1 following
) as 'local maximum'
from int_t where property in ('prime','square');
ERROR: AnalysisException: RANGE is only supported with both the lower and
upper bounds UNBOUNDED or one UNBOUNDED and the other CURRENT ROW.
Related information:
Impala Analytic Functions on page 547, MIN Function on page 533, AVG Function on page 518
MIN Function
An aggregate function that returns the minimum value from a set of numbers. Opposite of the MAX function. Its single
argument can be numeric column, or the numeric result of a function or expression applied to the column value. Rows
with a NULL value for the specified column are ignored. If the table is empty, or all the values supplied to MIN are
NULL, MIN returns NULL.
Syntax:
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Restrictions: In Impala 2.0 and higher, this function can be used as an analytic function, but with restrictions on
any window clause. For MAX() and MIN(), the window clause is only allowed if the start bound is UNBOUNDED
PRECEDING.
Return type: Same as the input value, except for CHAR and VARCHAR arguments which produce a STRING result
Usage notes:
If you frequently run aggregate functions such as MIN(), MAX(), and COUNT(DISTINCT) on partition key
columns, consider enabling the OPTIMIZE_PARTITION_KEY_SCANS query option, which optimizes such queries.
This feature is available in Impala 2.5 and higher. See OPTIMIZE_PARTITION_KEY_SCANS Query Option (Impala
2.5 or higher only) on page 365 for the kinds of queries that this option applies to, and slight differences in how
partitions are evaluated when this query option is enabled.
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| Impala SQL Language Reference | 534
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| EUROPE | 5 | 77 | 15.4 | FRANCE | UNITED KINGDOM | 5
|
| Impala SQL Language Reference | 535
Examples:
The following examples show how to use MIN() in an analytic context. They use a table containing integers from 1
to 10. Notice how the MIN() is reported for each input value, as opposed to the GROUP BY clause which condenses
the result set.
Adding an ORDER BY clause lets you experiment with results that are cumulative or apply to a moving set of rows
(the “window”). The following examples use MIN() in an analytic context (that is, with an OVER() clause) to
display the smallest value of X encountered up to each row in the result set. The examples use two columns in the
ORDER BY clause to produce a sequence of values that rises and falls, to illustrate how the MIN() result only
decreases or stays the same throughout each partition within the result set. The basic ORDER BY x clause implicitly
activates a window clause of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which is
effectively the same as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, therefore all of these
examples produce the same results:
| 9 | square | 2 |
| 4 | square | 2 |
| 1 | square | 1 |
+---+----------+-----------------------+
select x, property,
min(x) over
(
order by property, x desc
range between unbounded preceding and current row
) as 'minimum to this point'
from int_t where property in ('prime','square');
+---+----------+-----------------------+
| x | property | minimum to this point |
+---+----------+-----------------------+
| 7 | prime | 7 |
| 5 | prime | 5 |
| 3 | prime | 3 |
| 2 | prime | 2 |
| 9 | square | 2 |
| 4 | square | 2 |
| 1 | square | 1 |
+---+----------+-----------------------+
select x, property,
min(x) over
(
order by property, x desc
rows between unbounded preceding and current row
) as 'minimum to this point'
from int_t where property in ('prime','square');
+---+----------+-----------------------+
| x | property | minimum to this point |
+---+----------+-----------------------+
| 7 | prime | 7 |
| 5 | prime | 5 |
| 3 | prime | 3 |
| 2 | prime | 2 |
| 9 | square | 2 |
| 4 | square | 2 |
| 1 | square | 1 |
+---+----------+-----------------------+
The following examples show how to construct a moving window, with a running minimum taking into account all
rows before and 1 row after the current row. Because of a restriction in the Impala RANGE syntax, this type of moving
window is possible with the ROWS BETWEEN clause but not the RANGE BETWEEN clause. Because of an extra
Impala restriction on the MAX() and MIN() functions in an analytic context, the lower bound must be UNBOUNDED
PRECEDING.
select x, property,
min(x) over
(
order by property, x desc
rows between unbounded preceding and 1 following
) as 'local minimum'
from int_t where property in ('prime','square');
+---+----------+---------------+
| x | property | local minimum |
+---+----------+---------------+
| 7 | prime | 5 |
| 5 | prime | 3 |
| 3 | prime | 2 |
| Impala SQL Language Reference | 537
| 2 | prime | 2 |
| 9 | square | 2 |
| 4 | square | 1 |
| 1 | square | 1 |
+---+----------+---------------+
Related information:
Impala Analytic Functions on page 547, MAX Function on page 529, AVG Function on page 518
NDV Function
An aggregate function that returns an approximate value similar to the result of COUNT(DISTINCT col), the
“number of distinct values”. It is much faster than the combination of COUNT and DISTINCT, and uses a constant
amount of memory and thus is less memory-intensive for columns with high cardinality.
Syntax:
Usage notes:
This is the mechanism used internally by the COMPUTE STATS statement for computing the number of distinct
values in a column.
Because this number is an estimate, it might not reflect the precise number of different values in the column,
especially if the cardinality is very low or very high. If the estimated number is higher than the number of rows in the
table, Impala adjusts the value internally during query planning.
Return type: DOUBLE in Impala 2.0 and higher; STRING in earlier releases
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| Impala SQL Language Reference | 538
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| AMERICA | 5 | 47 | 9.4 | ARGENTINA | UNITED STATES | 5
|
| ASIA | 5 | 68 | 13.6 | CHINA | VIETNAM | 5
|
| Impala SQL Language Reference | 539
Restrictions:
This function cannot be used in an analytic context. That is, the OVER() clause is not allowed at all with this
function.
Examples:
The following example queries a billion-row table to illustrate the relative performance of COUNT(DISTINCT) and
NDV(). It shows how COUNT(DISTINCT) gives a precise answer, but is inefficient for large-scale data where an
approximate result is sufficient. The NDV() function gives an approximate result but is much faster.
The following example shows how you can code multiple NDV() calls in a single query, to easily learn which
columns have substantially more or fewer distinct values. This technique is faster than running a sequence of queries
with COUNT(DISTINCT) calls.
This example demonstrates that, because the return value of these aggregate functions is a STRING, you must
currently convert the result with CAST.
Restrictions:
This function cannot be used in an analytic context. That is, the OVER() clause is not allowed at all with this
function.
Related information:
The STDDEV(), STDDEV_POP(), and STDDEV_SAMP() functions compute the standard deviation (square root
of the variance) based on the results of VARIANCE(), VARIANCE_POP(), and VARIANCE_SAMP() respectively.
See VARIANCE, VARIANCE_SAMP, VARIANCE_POP, VAR_SAMP, VAR_POP Functions on page 546 for details
about the variance property.
SUM Function
An aggregate function that returns the sum of a set of numbers. Its single argument can be numeric column, or the
numeric result of a function or expression applied to the column value. Rows with a NULL value for the specified
column are ignored. If the table is empty, or all the values supplied to MIN are NULL, SUM returns NULL.
Syntax:
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Return type: BIGINT for integer arguments, DOUBLE for floating-point arguments
Complex type considerations:
To access a column with a complex type (ARRAY, STRUCT, or MAP) in an aggregation function, you unpack the
individual elements using join notation in the query, and then apply the function to the final scalar item, field, key, or
value at the bottom of any nested type hierarchy in the column. See Complex Types (Impala 2.3 or higher only) on
page 134 for details about using complex types in Impala.
The following example demonstrates calls to several aggregation functions using values from a column containing
nested complex types (an ARRAY of STRUCT items). The array is unpacked inside the query using join notation.
The array elements are referenced using the ITEM pseudocolumn, and the structure fields inside the array elements
are referenced using dot notation. Numeric values such as SUM() and AVG() are computed using the numeric
R_NATIONKEY field, and the general-purpose MAX() and MIN() values are computed from the string N_NAME
field.
describe region;
+-------------+-------------------------+---------+
| name | type | comment |
+-------------+-------------------------+---------+
| Impala SQL Language Reference | 542
| r_regionkey | smallint | |
| r_name | string | |
| r_comment | string | |
| r_nations | array<struct< | |
| | n_nationkey:smallint, | |
| | n_name:string, | |
| | n_comment:string | |
| | >> | |
+-------------+-------------------------+---------+
select
r_name,
count(r_nations.item.n_nationkey) as count,
sum(r_nations.item.n_nationkey) as sum,
avg(r_nations.item.n_nationkey) as avg,
min(r_nations.item.n_name) as minimum,
max(r_nations.item.n_name) as maximum,
ndv(r_nations.item.n_nationkey) as distinct_vals
from
region, region.r_nations as r_nations
group by r_name
order by r_name;
+-------------+-------+-----+------+-----------+----------------
+---------------+
| r_name | count | sum | avg | minimum | maximum |
distinct_vals |
+-------------+-------+-----+------+-----------+----------------
+---------------+
| AFRICA | 5 | 50 | 10 | ALGERIA | MOZAMBIQUE | 5
|
| Impala SQL Language Reference | 543
Examples:
The following example shows how to use SUM() to compute the total for all the values in the table, a subset of
values, or the sum for each combination of values in the GROUP BY clause:
The following examples show how to use SUM() in an analytic context. They use a table containing integers from 1
to 10. Notice how the SUM() is reported for each input value, as opposed to the GROUP BY clause which condenses
the result set.
Adding an ORDER BY clause lets you experiment with results that are cumulative or apply to a moving set of rows
(the “window”). The following examples use SUM() in an analytic context (that is, with an OVER() clause) to
produce a running total of all the even values, then a running total of all the odd values. The basic ORDER BY x
clause implicitly activates a window clause of RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
| Impala SQL Language Reference | 544
ROW, which is effectively the same as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,
therefore all of these examples produce the same results:
select x, property,
sum(x) over (partition by property order by x) as 'cumulative total'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative total |
+----+----------+------------------+
| 2 | even | 2 |
| 4 | even | 6 |
| 6 | even | 12 |
| 8 | even | 20 |
| 10 | even | 30 |
| 1 | odd | 1 |
| 3 | odd | 4 |
| 5 | odd | 9 |
| 7 | odd | 16 |
| 9 | odd | 25 |
+----+----------+------------------+
select x, property,
sum(x) over
(
partition by property
order by x
range between unbounded preceding and current row
) as 'cumulative total'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative total |
+----+----------+------------------+
| 2 | even | 2 |
| 4 | even | 6 |
| 6 | even | 12 |
| 8 | even | 20 |
| 10 | even | 30 |
| 1 | odd | 1 |
| 3 | odd | 4 |
| 5 | odd | 9 |
| 7 | odd | 16 |
| 9 | odd | 25 |
+----+----------+------------------+
select x, property,
sum(x) over
(
partition by property
order by x
rows between unbounded preceding and current row
) as 'cumulative total'
from int_t where property in ('odd','even');
+----+----------+------------------+
| x | property | cumulative total |
+----+----------+------------------+
| 2 | even | 2 |
| 4 | even | 6 |
| 6 | even | 12 |
| 8 | even | 20 |
| 10 | even | 30 |
| 1 | odd | 1 |
| 3 | odd | 4 |
| 5 | odd | 9 |
| Impala SQL Language Reference | 545
| 7 | odd | 16 |
| 9 | odd | 25 |
+----+----------+------------------+
Changing the direction of the ORDER BY clause causes the intermediate results of the cumulative total to be
calculated in a different order:
The following examples show how to construct a moving window, with a running total taking into account 1 row
before and 1 row after the current row, within the same partition (all the even values or all the odd values). Because of
a restriction in the Impala RANGE syntax, this type of moving window is possible with the ROWS BETWEEN clause
but not the RANGE BETWEEN clause:
select x, property,
sum(x) over
(
partition by property
order by x
rows between 1 preceding and 1 following
) as 'moving total'
from int_t where property in ('odd','even');
+----+----------+--------------+
| x | property | moving total |
+----+----------+--------------+
| 2 | even | 6 |
| 4 | even | 12 |
| 6 | even | 18 |
| 8 | even | 24 |
| 10 | even | 18 |
| 1 | odd | 4 |
| 3 | odd | 9 |
| 5 | odd | 15 |
| 7 | odd | 21 |
| 9 | odd | 16 |
+----+----------+--------------+
ERROR: AnalysisException: RANGE is only supported with both the lower and
upper bounds UNBOUNDED or one UNBOUNDED and the other CURRENT ROW.
Restrictions:
Due to the way arithmetic on FLOAT and DOUBLE columns uses high-performance hardware instructions, and
distributed queries can perform these operations in different order for each query, results can vary slightly for
aggregate function calls such as SUM() and AVG() for FLOAT and DOUBLE columns, particularly on large data
sets where millions or billions of values are summed or averaged. For perfect consistency and repeatability, use the
DECIMAL data type for such operations instead of FLOAT or DOUBLE.
Related information:
Impala Analytic Functions on page 547
| 811.438 |
+---------------------+
This example demonstrates that, because the return value of these aggregate functions is a STRING, you convert the
result with CAST if you need to do further calculations as a numeric value.
Restrictions:
This function cannot be used in an analytic context. That is, the OVER() clause is not allowed at all with this
function.
Related information:
The STDDEV(), STDDEV_POP(), and STDDEV_SAMP() functions compute the standard deviation (square root
of the variance) based on the results of VARIANCE(), VARIANCE_POP(), and VARIANCE_SAMP() respectively.
See STDDEV, STDDEV_SAMP, STDDEV_POP Functions on page 540 for details about the standard deviation
property.
sequence of rows (the “window”) that the function call applies to, particularly when the OVER clause includes a ROWS
or RANGE keyword.
The following sections describe the analytic query clauses and the pure analytic functions provided by Impala. For
usage information about aggregate functions in an analytic context, see Impala Aggregate Functions on page 516.
OVER Clause
The OVER clause is required for calls to pure analytic functions such as LEAD(), RANK(), and FIRST_VALUE().
When you include an OVER clause with calls to aggregate functions such as MAX(), COUNT(), or SUM(), they
operate as analytic functions.
Syntax:
PARTITION BY clause:
The PARTITION BY clause acts much like the GROUP BY clause in the outermost block of a query. It divides the
rows into groups containing identical values in one or more columns. These logical groups are known as partitions.
Throughout the discussion of analytic functions, “partitions” refers to the groups produced by the PARTITION BY
clause, not to partitioned tables. However, note the following limitation that applies specifically to analytic function
calls involving partitioned tables.
In queries involving both analytic functions and partitioned tables, partition pruning only occurs for columns named
in the PARTITION BY clause of the analytic function call. For example, if an analytic function query has a clause
such as WHERE year=2016, the way to make the query prune all other YEAR partitions is to include PARTITION
BY year in the analytic function call; for example, OVER (PARTITION BY year,other_columns
other_analytic_clauses).
The sequence of results from an analytic function “resets” for each new partition in the result set. That is, the set of
preceding or following rows considered by the analytic function always come from a single partition. Any MAX(),
SUM(), ROW_NUMBER(), and so on apply to each partition independently. Omit the PARTITION BY clause to
apply the analytic operation to all the rows in the table.
ORDER BY clause:
The ORDER BY clause works much like the ORDER BY clause in the outermost block of a query. It defines the order
in which rows are evaluated for the entire input set, or for each group produced by a PARTITION BY clause. You
can order by one or multiple expressions, and for each expression optionally choose ascending or descending order
and whether nulls come first or last in the sort order. Because this ORDER BY clause only defines the order in which
rows are evaluated, if you want the results to be output in a specific order, also include an ORDER BY clause in the
outer block of the query.
When the ORDER BY clause is omitted, the analytic function applies to all items in the group produced by the
PARTITION BY clause. When the ORDER BY clause is included, the analysis can apply to all or a subset of the
items in the group, depending on the optional window clause.
The order in which the rows are analyzed is only defined for those columns specified in ORDER BY clauses.
One difference between the analytic and outer uses of the ORDER BY clause: inside the OVER clause, ORDER BY 1
or other integer value is interpreted as a constant sort value (effectively a no-op) rather than referring to column 1.
Window clause:
The window clause is only allowed in combination with an ORDER BY clause. If the ORDER BY clause is specified
but the window clause is not, the default window is RANGE BETWEEN UNBOUNDED PRECEDING AND
CURRENT ROW. See Window Clause on page 549 for full details.
| Impala SQL Language Reference | 549
HBase considerations:
Because HBase tables are optimized for single-row lookups rather than full scans, analytic functions using the
OVER() clause are not recommended for HBase tables. Although such queries work, their performance is lower than
on comparable tables using HDFS data files.
Parquet considerations:
Analytic functions are very efficient for Parquet tables. The data that is examined during evaluation of the OVER()
clause comes from a specified set of columns, and the values for each column are arranged sequentially within each
data file.
Text table considerations:
Analytic functions are convenient to use with text tables for exploratory business intelligence. When the volume of
data is substantial, prefer to use Parquet tables for performance-critical analytic queries.
Added in: Impala 2.0.0
Examples:
The following example shows how to synthesize a numeric sequence corresponding to all the rows in a table. The
new table has the same columns as the old one, plus an additional column ID containing the integers 1, 2, 3, and so
on, corresponding to the order of a TIMESTAMP column in the original table.
The following example shows how to determine the number of rows containing each value for a column. Unlike a
corresponding GROUP BY query, this one can analyze a single column and still return all values (not just the distinct
ones) from the other columns.
SELECT x, y, z,
count() OVER (PARTITION BY x) AS how_many_x
FROM t1;
Restrictions:
You cannot directly combine the DISTINCT operator with analytic function calls. You can put the analytic function
call in a WITH clause or an inline view, and apply the DISTINCT operator to its result set.
Window Clause
Certain analytic functions accept an optional window clause, which makes the function analyze only certain rows
“around” the current row rather than all rows in the partition. For example, you can get a moving average by
specifying some number of preceding and following rows, or a running count or running total by specifying all rows
up to the current position. This clause can result in different analytic results for rows within the same partition.
The window clause is supported with the AVG(), COUNT(), FIRST_VALUE(), LAST_VALUE(), and SUM()
functions. For MAX() and MIN(), the window clause only allowed if the start bound is UNBOUNDED PRECEDING
Syntax:
ROWS BETWEEN defines the size of the window in terms of the indexes of the rows in the result set. The size of the
window is predictable based on the clauses the position within the result set.
RANGE BETWEEN does not currently support numeric arguments to define a variable-size sliding window.
Currently, Impala supports only some combinations of arguments to the RANGE clause:
• RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (the default when ORDER BY is
specified and the window clause is omitted)
• RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
• RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
When RANGE is used, CURRENT ROW includes not just the current row but all rows that are tied with the current row
based on the ORDER BY expressions.
Added in: Impala 2.0.0
Examples:
The following examples show financial data for a fictional stock symbol JDR. The closing price moves up and down
each day.
The queries use analytic functions with window clauses to compute moving averages of the closing price. For
example, ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING produces an average of the value from a 3-day
span, producing a different value for each row. The first row, which has no preceding row, only gets averaged with
the row following it. If the table contained more than one stock symbol, the PARTITION BY clause would limit the
window for the moving average to only consider the prices for a single stock.
The clause ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW produces a cumulative moving
average, from the earliest data up to the value for each day.
CUME_DIST (expr)
OVER ([partition_by_clause] order_by_clause)
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
Usage notes:
Within each partition of the result set, the CUME_DIST() value represents an ascending sequence that ends at 1.
Each value represents the proportion of rows in the partition whose values are less than or equal to the value in the
current row.
If the sequence of input values contains ties, the CUME_DIST() results are identical for the tied values.
Impala only supports the CUME_DIST() function in an analytic context, not as a regular aggregate function.
Examples:
This example uses a table with 9 rows. The CUME_DIST() function evaluates the entire table because there is no
PARTITION BY clause, with the rows ordered by the weight of the animal. the sequence of values shows that 1/9 of
the values are less than or equal to the lightest animal (mouse), 2/9 of the values are less than or equal to the second-
lightest animal, and so on up to the heaviest animal (elephant), where 9/9 of the rows are less than or equal to its
weight.
Using a PARTITION BY clause produces a separate sequence for each partition group, in this case one for mammals
and one for birds. Because there are 3 birds and 6 mammals, the sequence illustrates how 1/3 of the “Bird” rows
have a kilos value that is less than or equal to the lightest bird, 1/6 of the “Mammal” rows have a kilos value
that is less than or equal to the lightest mammal, and so on until both the heaviest bird and heaviest mammal have a
CUME_DIST() value of 1.
select name, kind, cume_dist() over (partition by kind order by kilos) from
animals
+------------+--------+-----------------------+
| name | kind | cume_dist() OVER(...) |
+------------+--------+-----------------------+
| Ostrich | Bird | 1 |
| Condor | Bird | 0.6666666666666666 |
| Owl | Bird | 0.3333333333333333 |
| Elephant | Mammal | 1 |
| Giraffe | Mammal | 0.8333333333333334 |
| Polar bear | Mammal | 0.6666666666666666 |
| Horse | Mammal | 0.5 |
| Housecat | Mammal | 0.3333333333333333 |
| Mouse | Mammal | 0.1666666666666667 |
+------------+--------+-----------------------+
We can reverse the ordering within each partition group by using an ORDER BY ... DESC clause within the
OVER() clause. Now the lightest (smallest value of kilos) animal of each kind has a CUME_DIST() value of 1.
select name, kind, cume_dist() over (partition by kind order by kilos desc)
from animals
+------------+--------+-----------------------+
| name | kind | cume_dist() OVER(...) |
+------------+--------+-----------------------+
| Owl | Bird | 1 |
| Condor | Bird | 0.6666666666666666 |
| Ostrich | Bird | 0.3333333333333333 |
| Mouse | Mammal | 1 |
| Housecat | Mammal | 0.8333333333333334 |
| Horse | Mammal | 0.6666666666666666 |
| Polar bear | Mammal | 0.5 |
| Giraffe | Mammal | 0.3333333333333333 |
| Elephant | Mammal | 0.1666666666666667 |
+------------+--------+-----------------------+
The following example manufactures some rows with identical values in the kilos column, to demonstrate how
the results look in case of tie values. For simplicity, it only shows the CUME_DIST() sequence for the “Bird” rows.
| Impala SQL Language Reference | 553
Now with 3 rows all with a value of 15, all of those rows have the same CUME_DIST() value. 4/5 of the rows have a
value for kilos that is less than or equal to 15.
select name, kind, cume_dist() over (order by kilos) from animals where kind
= 'Bird';
+-------------------+------+-----------------------+
| name | kind | cume_dist() OVER(...) |
+-------------------+------+-----------------------+
| Ostrich | Bird | 1 |
| Condor | Bird | 0.8 |
| California Condor | Bird | 0.8 |
| Andean Condor | Bird | 0.8 |
| Owl | Bird | 0.2 |
+-------------------+------+-----------------------+
The following example shows how to use an ORDER BY clause in the outer block to order the result set in case of
ties. Here, all the “Bird” rows are together, then in descending order by the result of the CUME_DIST() function, and
all tied CUME_DIST() values are ordered by the animal name.
DENSE_RANK Function
Returns an ascending sequence of integers, starting with 1. The output sequence produces duplicate integers for
duplicate values of the ORDER BY expressions. After generating duplicate output values for the “tied” input values,
the function continues the sequence with the next higher integer. Therefore, the sequence contains duplicates but no
gaps when the input contains duplicates. Starts the sequence over for each group produced by the PARTITIONED
BY clause.
Syntax:
The PARTITION BY clause is optional. The ORDER BY clause is required. The window clause is not allowed.
Usage notes:
Often used for top-N and bottom-N queries. For example, it could produce a “top 10” report including all the items
with the 10 highest values, even if several items tied for 1st place.
Similar to ROW_NUMBER and RANK. These functions differ in how they treat duplicate combinations of values.
Added in: Impala 2.0.0
Examples:
| Impala SQL Language Reference | 554
The following example demonstrates how the DENSE_RANK() function identifies where each value “places” in the
result set, producing the same result for duplicate values, but with a strict sequence from 1 to the number of groups.
For example, when results are ordered by the X column, both 1 values are tied for first; both 2 values are tied for
second; and so on.
The following examples show how the DENSE_RANK() function is affected by the PARTITION property within the
ORDER BY clause.
Partitioning by the PROPERTY column groups all the even, odd, and so on values together, and DENSE_RANK()
returns the place of each value within the group, producing several ascending sequences.
| 1 | 1 | square |
| 4 | 2 | square |
| 9 | 3 | square |
+----+------+----------+
Partitioning by the X column groups all the duplicate numbers together and returns the place each value within the
group; because each value occurs only 1 or 2 times, DENSE_RANK() designates each X value as either first or
second within its group.
The following example shows how DENSE_RANK() produces a continuous sequence while still allowing for ties.
In this case, Croesus and Midas both have the second largest fortune, while Crassus has the third largest. (In RANK
Function on page 562, you see a similar query with the RANK() function that shows that while Crassus has the
third largest fortune, he is the fourth richest person.)
Related information:
RANK Function on page 562, ROW_NUMBER Function on page 565
| Impala SQL Language Reference | 556
FIRST_VALUE Function
Returns the expression value from the first row in the window. The return value is NULL if the input expression is
NULL.
Syntax:
The PARTITION BY clause is optional. The ORDER BY clause is required. The window clause is optional.
Usage notes:
If any duplicate values occur in the tuples evaluated by the ORDER BY clause, the result of this function is not
deterministic. Consider adding additional ORDER BY columns to ensure consistent ordering.
Added in: Impala 2.0.0
Examples:
The following example shows a table with a wide variety of country-appropriate greetings. For consistency, we want
to standardize on a single greeting for each country. The FIRST_VALUE() function helps to produce a mail merge
report where every person from the same country is addressed with the same greeting.
Changing the order in which the names are evaluated changes which greeting is applied to each group.
Related information:
LAST_VALUE Function on page 558
LAG Function
This function returns the value of an expression using column values from a preceding row. You specify an integer
offset, which designates a row position some number of rows previous to the current row. Any column references in
the expression argument refer to column values from that prior row. Typically, the table contains a time sequence or
numeric sequence column that clearly distinguishes the ordering of the rows.
Syntax:
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
Usage notes:
Sometimes used an an alternative to doing a self-join.
Added in: Impala 2.0.0
Examples:
The following example uses the same stock data created in Window Clause on page 549. For each day, the query
prints the closing price alongside the previous day's closing price. The first row for each stock symbol has no previous
row, so that LAG() value is NULL.
The following example does an arithmetic operation between the current row and a value from the previous row, to
produce a delta value for each day. This example also demonstrates how ORDER BY works independently in the
different parts of the query. The ORDER BY closing_date in the OVER clause makes the query analyze the rows
in chronological order. Then the outer query block uses ORDER BY closing_date DESC to present the results
with the most recent date first.
)
as "change from yesterday"
from stock_ticker
order by closing_date desc;
+--------------+---------------------+---------------
+-----------------------+
| stock_symbol | closing_date | closing_price | change from yesterday
|
+--------------+---------------------+---------------
+-----------------------+
| JDR | 2014-09-19 00:00:00 | 13.98 | -0.76
|
| JDR | 2014-09-18 00:00:00 | 14.75 | 0.72
|
| JDR | 2014-09-17 00:00:00 | 14.03 | 1.47
|
| JDR | 2014-09-16 00:00:00 | 12.55 | -0.38
|
| JDR | 2014-09-15 00:00:00 | 12.94 | 0.04
|
| JDR | 2014-09-14 00:00:00 | 12.89 | 0.03
|
| JDR | 2014-09-13 00:00:00 | 12.86 | NULL
|
+--------------+---------------------+---------------
+-----------------------+
Related information:
This function is the converse of LEAD Function on page 559.
LAST_VALUE Function
Returns the expression value from the last row in the window. This same value is repeated for all result rows for the
group. The return value is NULL if the input expression is NULL.
Syntax:
The PARTITION BY clause is optional. The ORDER BY clause is required. The window clause is optional.
Usage notes:
If any duplicate values occur in the tuples evaluated by the ORDER BY clause, the result of this function is not
deterministic. Consider adding additional ORDER BY columns to ensure consistent ordering.
Added in: Impala 2.0.0
Examples:
The following example uses the same MAIL_MERGE table as in the example for FIRST_VALUE Function on page
556. Because the default window when ORDER BY is used is BETWEEN UNBOUNDED PRECEDING AND
CURRENT ROW, the query requires the UNBOUNDED FOLLOWING to look ahead to subsequent rows and find the
last value for each country.
+---------+---------+--------------+
| Germany | Boris | Guten morgen |
| Germany | Michael | Guten morgen |
| Sweden | Bjorn | Tja |
| Sweden | Mats | Tja |
| USA | John | Hello |
| USA | Pete | Hello |
+---------+---------+--------------+
Related information:
FIRST_VALUE Function on page 556
LEAD Function
This function returns the value of an expression using column values from a following row. You specify an integer
offset, which designates a row position some number of rows after to the current row. Any column references in
the expression argument refer to column values from that later row. Typically, the table contains a time sequence or
numeric sequence column that clearly distinguishes the ordering of the rows.
Syntax:
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
Usage notes:
Sometimes used an an alternative to doing a self-join.
Added in: Impala 2.0.0
Examples:
The following example uses the same stock data created in Window Clause on page 549. The query analyzes the
closing price for a stock symbol, and for each day evaluates if the closing price for the following day is higher or
lower.
Related information:
This function is the converse of LAG Function on page 557.
| Impala SQL Language Reference | 560
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
Usage notes:
The “ntile” name is derived from the practice of dividing result sets into fourths (quartile), tenths (decile), and so on.
The NTILE() function divides the result set based on an arbitrary percentile value.
The number of buckets must be a positive integer.
The number of items in each bucket is identical or almost so, varying by at most 1. If the number of items does not
divide evenly between the buckets, the remaining N items are divided evenly among the first N buckets.
If the number of buckets N is greater than the number of input rows in the partition, then the first N buckets each
contain one item, and the remaining buckets are empty.
Examples:
The following example shows divides groups of animals into 4 buckets based on their weight. The ORDER BY ...
DESC clause in the OVER() clause means that the heaviest 25% are in the first group, and the lightest 25% are in
the fourth group. (The ORDER BY in the outermost part of the query shows how you can order the final result set
independently from the order in which the rows are evaluated by the OVER() clause.) Because there are 9 rows in the
group, divided into 4 buckets, the first bucket receives the extra item.
| Elephant | 1 |
| Giraffe | 1 |
| Polar bear | 1 |
+------------+---------+
The following examples show how the PARTITION clause works for the NTILE() function. Here, we divide each
kind of animal (mammal or bird) into 2 buckets, the heavier half and the lighter half.
select name, kind, ntile(2) over (partition by kind order by kilos desc) as
half
from animals
order by kind;
+------------+--------+------+
| name | kind | half |
+------------+--------+------+
| Ostrich | Bird | 1 |
| Condor | Bird | 1 |
| Owl | Bird | 2 |
| Elephant | Mammal | 1 |
| Giraffe | Mammal | 1 |
| Polar bear | Mammal | 1 |
| Horse | Mammal | 2 |
| Housecat | Mammal | 2 |
| Mouse | Mammal | 2 |
+------------+--------+------+
Again, the result set can be ordered independently from the analytic evaluation. This next example lists all the animals
heaviest to lightest, showing that elephant and giraffe are in the “top half” of mammals by weight, while housecat and
mouse are in the “bottom half”.
select name, kind, ntile(2) over (partition by kind order by kilos desc) as
half
from animals
order by kilos desc;
+------------+--------+------+
| name | kind | half |
+------------+--------+------+
| Elephant | Mammal | 1 |
| Giraffe | Mammal | 1 |
| Polar bear | Mammal | 1 |
| Horse | Mammal | 2 |
| Ostrich | Bird | 1 |
| Condor | Bird | 1 |
| Housecat | Mammal | 2 |
| Owl | Bird | 2 |
| Mouse | Mammal | 2 |
+------------+--------+------+
PERCENT_RANK (expr)
OVER ([partition_by_clause] order_by_clause)
Calculates the rank, expressed as a percentage, of each row within a group of rows. If rank is the value for
that same row from the RANK() function (from 1 to the total number of rows in the partition group), then the
PERCENT_RANK() value is calculated as (rank - 1) / (rows_in_group - 1) . If there is only a single
item in the partition group, its PERCENT_RANK() value is 0.
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
| Impala SQL Language Reference | 562
Usage notes:
This function is similar to the RANK and CUME_DIST() functions: it returns an ascending sequence representing
the position of each row within the rows of the same partition group. The actual numeric sequence is calculated
differently, and the handling of duplicate (tied) values is different.
The return values range from 0 to 1 inclusive. The first row in each partition group always has the value 0. A NULL
value is considered the lowest possible value. In the case of duplicate input values, all the corresponding rows
in the result set have an identical value: the lowest PERCENT_RANK() value of those tied rows. (In contrast to
CUME_DIST(), where all tied rows have the highest CUME_DIST() value.)
Examples:
The following example uses the same ANIMALS table as the examples for CUME_DIST() and NTILE(), with a
few additional rows to illustrate the results where some values are NULL or there is only a single row in a partition
group.
As with CUME_DIST(), there is an ascending sequence for each kind of animal. For example, the “Birds” and
“Mammals” rows each have a PERCENT_RANK() sequence that ranges from 0 to 1. The “Reptile” row has a
PERCENT_RANK() of 0 because that partition group contains only a single item. Both “Mythical” animals have a
PERCENT_RANK() of 0 because a NULL is considered the lowest value within its partition group.
RANK Function
Returns an ascending sequence of integers, starting with 1. The output sequence produces duplicate integers for
duplicate values of the ORDER BY expressions. After generating duplicate output values for the “tied” input
values, the function increments the sequence by the number of tied values. Therefore, the sequence contains both
duplicates and gaps when the input contains duplicates. Starts the sequence over for each group produced by the
PARTITIONED BY clause.
Syntax:
The PARTITION BY clause is optional. The ORDER BY clause is required. The window clause is not allowed.
Usage notes:
| Impala SQL Language Reference | 563
Often used for top-N and bottom-N queries. For example, it could produce a “top 10” report including several items
that were tied for 10th place.
Similar to ROW_NUMBER and DENSE_RANK. These functions differ in how they treat duplicate combinations of
values.
Added in: Impala 2.0.0
Examples:
The following example demonstrates how the RANK() function identifies where each value “places” in the result
set, producing the same result for duplicate values, and skipping values in the sequence to account for the number of
duplicates. For example, when results are ordered by the X column, both 1 values are tied for first; both 2 values are
tied for third; and so on.
The following examples show how the RANK() function is affected by the PARTITION property within the ORDER
BY clause.
Partitioning by the PROPERTY column groups all the even, odd, and so on values together, and RANK() returns the
place of each value within the group, producing several ascending sequences.
| 3 | 2 | odd |
| 5 | 3 | odd |
| 7 | 4 | odd |
| 9 | 5 | odd |
| 6 | 1 | perfect |
| 2 | 1 | prime |
| 3 | 2 | prime |
| 5 | 3 | prime |
| 7 | 4 | prime |
| 10 | 1 | round |
| 1 | 1 | square |
| 4 | 2 | square |
| 9 | 3 | square |
+----+------+----------+
Partitioning by the X column groups all the duplicate numbers together and returns the place each value within the
group; because each value occurs only 1 or 2 times, RANK() designates each X value as either first or second within
its group.
The following example shows how a magazine might prepare a list of history's wealthiest people. Croesus and Midas
are tied for second, then Crassus is fourth.
select rank() over (order by net_worth desc) as rank, name, net_worth from
wealth order by rank, name;
+------+---------+---------------+
| rank | name | net_worth |
+------+---------+---------------+
| 1 | Solomon | 2000000000.00 |
| 2 | Croesus | 1000000000.00 |
| 2 | Midas | 1000000000.00 |
| 4 | Crassus | 500000000.00 |
| 5 | Scrooge | 80000000.00 |
+------+---------+---------------+
| Impala SQL Language Reference | 565
Related information:
DENSE_RANK Function on page 553, ROW_NUMBER Function on page 565
ROW_NUMBER Function
Returns an ascending sequence of integers, starting with 1. Starts the sequence over for each group produced by the
PARTITIONED BY clause. The output sequence includes different values for duplicate input values. Therefore, the
sequence never contains any duplicates or gaps, regardless of duplicate input values.
Syntax:
The ORDER BY clause is required. The PARTITION BY clause is optional. The window clause is not allowed.
Usage notes:
Often used for top-N and bottom-N queries where the input values are known to be unique, or precisely N rows are
needed regardless of duplicate values.
Because its result value is different for each row in the result set (when used without a PARTITION BY clause),
ROW_NUMBER() can be used to synthesize unique numeric ID values, for example for result sets involving unique
values or tuples.
Similar to RANK and DENSE_RANK. These functions differ in how they treat duplicate combinations of values.
Added in: Impala 2.0.0
Examples:
The following example demonstrates how ROW_NUMBER() produces a continuous numeric sequence, even though
some values of X are repeated.
The following example shows how a financial institution might assign customer IDs to some of history's wealthiest
figures. Although two of the people have identical net worth figures, unique IDs are required for this purpose.
ROW_NUMBER() produces a sequence of five different values for the five input rows.
Related information:
RANK Function on page 562, DENSE_RANK Function on page 553
UDF Concepts
Depending on your use case, you might write all-new functions, reuse Java UDFs that you have already written for
Hive, or port Hive Java UDF code to higher-performance native Impala UDFs in C++. You can code either scalar
functions for producing results one row at a time, or more complex aggregate functions for doing analysis across. The
following sections discuss these different aspects of working with UDFs.
| Impala SQL Language Reference | 567
-- Evaluates batches of rows and returns a separate value for each batch.
select most_profitable_location(store_id, sales, expenses, tax_rate,
depreciation) from franchise_data group by year;
• Currently, Impala does not support other categories of user-defined functions, such as user-defined table functions
(UDTFs) or window functions.
1. Take a copy of the Hive JAR file containing the Hive built-in functions. For example, the path might be like /
usr/lib/hive/lib/hive-exec-0.10.0.jar, with different version numbers corresponding to your
specific level of .
2. Use jar tf jar_file to see a list of the classes inside the JAR. You will see names like org/
apache/hadoop/hive/ql/udf/UDFLower.class and org/apache/hadoop/hive/ql/udf/
UDFOPNegative.class. Make a note of the names of the functions you want to experiment with. When
you specify the entry points for the Impala CREATE FUNCTION statement, change the slash characters to dots
and strip off the .class suffix, for example org.apache.hadoop.hive.ql.udf.UDFLower and
org.apache.hadoop.hive.ql.udf.UDFOPNegative.
3. Copy that file to an HDFS location that Impala can read. (In the examples here, we renamed the file to hive-
builtins.jar in HDFS for simplicity.)
4. For each Java-based UDF that you want to call through Impala, issue a CREATE FUNCTION statement, with a
LOCATION clause containing the full HDFS path of the JAR file, and a SYMBOL clause with the fully qualified
name of the class, using dots as separators and without the .class extension. Remember that user-defined
functions are associated with a particular database, so issue a USE statement for the appropriate database first,
or specify the SQL function name as db_name.function_name. Use completely new names for the SQL
functions, because Impala UDFs cannot have the same name as Impala built-in functions.
5. Call the function from your queries, passing arguments of the correct type to match the function signature. These
arguments could be references to columns, arithmetic or other kinds of expressions, the results of CAST functions
to ensure correct data types, and so on.
Note:
In Impala 2.9 and higher, you can refresh the user-defined functions (UDFs) that Impala recognizes, at the database
level, by running the REFRESH FUNCTIONS statement with the database name as an argument. Java-based UDFs
can be added to the metastore database through Hive CREATE FUNCTION statements, and made visible to Impala
by subsequently running REFRESH FUNCTIONS. For example:
+----------------------------------------------------+
Returned 1 row(s) in 0.11s
[localhost:21000] > create table t2 (s string);
[localhost:21000] > insert into t2 values ('lower'),('UPPER'),
('Init cap'),('CamelCase');
Inserted 4 rows in 2.28s
[localhost:21000] > select * from t2;
+-----------+
| s |
+-----------+
| lower |
| UPPER |
| Init cap |
| CamelCase |
+-----------+
Returned 4 row(s) in 0.47s
[localhost:21000] > select my_lower(s) from t2;
+------------------+
| udfs.my_lower(s) |
+------------------+
| lower |
| upper |
| init cap |
| camelcase |
+------------------+
Returned 4 row(s) in 0.54s
[localhost:21000] > select my_lower(concat('ABC ',s,' XYZ'))
from t2;
+------------------------------------------+
| udfs.my_lower(concat('abc ', s, ' xyz')) |
+------------------------------------------+
| abc lower xyz |
| abc upper xyz |
| abc init cap xyz |
| abc camelcase xyz |
+------------------------------------------+
Returned 4 row(s) in 0.22s
| udfs.my_neg(x) |
+----------------+
| -2 |
| -4 |
| -100 |
+----------------+
Returned 3 row(s) in 0.60s
[localhost:21000] > select my_neg(4.0);
ERROR: AnalysisException: No matching function with signature:
udfs.my_neg(FLOAT).
[localhost:21000] > select my_neg(cast(4.0 as int));
+-------------------------------+
| udfs.my_neg(cast(4.0 as int)) |
+-------------------------------+
| -4 |
+-------------------------------+
Returned 1 row(s) in 0.11s
[localhost:21000] > create function my_neg(double)
returns double location '/user/hive/udfs/hive.jar'
symbol='org.apache.hadoop.hive.ql.udf.UDFOPNegative';
[localhost:21000] > select my_neg(4.0);
+------------------+
| udfs.my_neg(4.0) |
+------------------+
| -4 |
+------------------+
Returned 1 row(s) in 0.11s
• Use best practices for function-oriented programming: choose arguments carefully, avoid side effects, make each
function do a single thing, and so on.
// This is the only Impala header required to develop UDFs and UDAs. This
header
// contains the types that need to be used and the FunctionContext object.
The context
// object serves as the interface object between the UDF/UDA and the impala
process.
For the basic declarations needed to write a scalar UDF, see the header file udf-sample.h within the sample build
environment, which defines a simple function named AddUdf():
#ifndef IMPALA_UDF_SAMPLE_UDF_H
#define IMPALA_UDF_SAMPLE_UDF_H
#include <impala_udf/udf.h>
#endif
For sample C++ code for a simple function named AddUdf(), see the source file udf-sample.cc within the
sample build environment:
#include "udf-sample.h"
// In this sample we are declaring a UDF that adds two ints and returns an
int.
IntVal AddUdf(FunctionContext* context, const IntVal& arg1, const IntVal&
arg2) {
if (arg1.is_null || arg2.is_null) return IntVal::null();
return IntVal(arg1.val + arg2.val);
}
• The built-in SQL comparison operators and clauses such as <, >=, BETWEEN, and ORDER BY all work
automatically based on the SQL return type of each UDF. For example, Impala knows how to evaluate BETWEEN
1 AND udf_returning_int(col1) or ORDER BY udf_returning_string(col2) without you
declaring any comparison operators within the UDF itself.
For convenience within your UDF code, each struct defines == and != operators for comparisons with
other structs of the same type. These are for typical C++ comparisons within your own code, not necessarily
reproducing SQL semantics. For example, if the is_null flag is set in both structs, they compare as equal. That
behavior of null comparisons is different from SQL (where NULL == NULL is NULL rather than true), but
more in line with typical C++ behavior.
• Each kind of struct has one or more constructors that define a filled-in instance of the struct, optionally with
default values.
• Impala cannot process UDFs that accept composite or nested types as arguments or return them as result values.
This limitation applies both to Impala UDFs written in C++ and Java-based Hive UDFs.
• You can overload functions by creating multiple functions with the same SQL name but different argument types.
For overloaded functions, you must use different C++ or Java entry point names in the underlying functions.
The data types defined on the C++ side (in /usr/include/impala_udf/udf.h) are:
• IntVal represents an INT column.
• BigIntVal represents a BIGINT column. Even if you do not need the full range of a BIGINT value, it can be
useful to code your function arguments as BigIntVal to make it convenient to call the function with different
kinds of integer columns and expressions as arguments. Impala automatically casts smaller integer types to larger
ones when appropriate, but does not implicitly cast large integer types to smaller ones.
• SmallIntVal represents a SMALLINT column.
• TinyIntVal represents a TINYINT column.
• StringVal represents a STRING column. It has a len field representing the length of the string, and a
ptr field pointing to the string data. It has constructors that create a new StringVal struct based on a null-
terminated C-style string, or a pointer plus a length; these new structs still refer to the original string data rather
than allocating a new buffer for the data. It also has a constructor that takes a pointer to a FunctionContext
struct and a length, that does allocate space for a new copy of the string data, for use in UDFs that return string
values.
• BooleanVal represents a BOOLEAN column.
• FloatVal represents a FLOAT column.
• DoubleVal represents a DOUBLE column.
• TimestampVal represents a TIMESTAMP column. It has a date field, a 32-bit integer representing the
Gregorian date, that is, the days past the epoch date. It also has a time_of_day field, a 64-bit integer
representing the current time of day in nanoseconds.
In the CREATE FUNCTION statement, after the type of the first optional argument, include ... to indicate it could
be followed by more arguments of the same type. For example, the following function accepts a STRING argument,
followed by one or more additional STRING arguments:
The call from the SQL query must pass at least one argument to the variable-length portion of the argument list.
When Impala calls the function, it fills in the initial set of required arguments, then passes the number of extra
arguments and a pointer to the first of those optional arguments.
void function_name(impala_udf::FunctionContext*,
impala_udf::FunctionContext::FunctionScope)
Currently, only THREAD_SCOPE is implemented, not FRAGMENT_SCOPE. See udf.h for details about the scope
values.
For a serious problem that requires cancelling the query, a UDF can set an error flag that prevents the query from
returning any results. The signature for this function is:
Then, unpack the sample code in udf_samples.tar.gz and use that as a template to set up your build
environment.
To build the original samples:
The sample code to examine, experiment with, and adapt is in these files:
• udf-sample.h: Header file that declares the signature for a scalar UDF (AddUDF).
• udf-sample.cc: Sample source for a simple UDF that adds two integers. Because Impala can reference
multiple function entry points from the same shared library, you could add other UDF functions in this file and
add their signatures to the corresponding header file.
• udf-sample-test.cc: Basic unit tests for the sample UDF.
| Impala SQL Language Reference | 576
• uda-sample.h: Header file that declares the signature for sample aggregate functions. The SQL functions
will be called COUNT, AVG, and STRINGCONCAT. Because aggregate functions require more elaborate coding
to handle the processing for multiple phases, there are several underlying C++ functions such as CountInit,
AvgUpdate, and StringConcatFinalize.
• uda-sample.cc: Sample source for simple UDAFs that demonstrate how to manage the state transitions as the
underlying functions are called during the different phases of query processing.
• The UDAF that imitates the COUNT function keeps track of a single incrementing number; the merge functions
combine the intermediate count values from each Impala node, and the combined number is returned verbatim
by the finalize function.
• The UDAF that imitates the AVG function keeps track of two numbers, a count of rows processed and the sum
of values for a column. These numbers are updated and merged as with COUNT, then the finalize function
divides them to produce and return the final average value.
• The UDAF that concatenates string values into a comma-separated list demonstrates how to manage storage
for a string that increases in length as the function is called for multiple rows.
• uda-sample-test.cc: basic unit tests for the sample UDAFs.
int index;
uint8_t *ptr;
{
return BooleanVal(true);
}
}
return BooleanVal(false);
}
int count;
int index;
uint8_t *ptr;
int index;
std::string original((const char *)arg1.ptr,arg1.len);
std::string shorter("");
We build a shared library, libudfsample.so, and put the library file into HDFS where Impala
can read it:
$ make
[ 0%] Generating udf_samples/uda-sample.ll
[ 16%] Built target uda-sample-ir
[ 33%] Built target udasample
[ 50%] Built target uda-sample-test
[ 50%] Generating udf_samples/udf-sample.ll
[ 66%] Built target udf-sample-ir
Scanning dependencies of target udfsample
[ 83%] Building CXX object CMakeFiles/udfsample.dir/udf-sample.o
Linking CXX shared library udf_samples/libudfsample.so
[ 83%] Built target udfsample
Linking CXX executable udf_samples/udf-sample-test
[100%] Built target udf-sample-test
$ hdfs dfs -put ./udf_samples/libudfsample.so /user/hive/udfs/
libudfsample.so
Finally, we go into the impala-shell interpreter where we set up some sample data, issue
CREATE FUNCTION statements to set up the SQL function names, and call the functions in some
queries:
We add the function bodies to a C++ source file (in this case, uda-sample.cc):
dst->val += src.val;
}
As with the sample UDF, we build a shared library and put it into HDFS:
$ make
[ 0%] Generating udf_samples/uda-sample.ll
[ 16%] Built target uda-sample-ir
Scanning dependencies of target udasample
[ 33%] Building CXX object CMakeFiles/udasample.dir/uda-sample.o
Linking CXX shared library udf_samples/libudasample.so
[ 33%] Built target udasample
Scanning dependencies of target uda-sample-test
[ 50%] Building CXX object CMakeFiles/uda-sample-test.dir/uda-
sample-test.o
Linking CXX executable udf_samples/uda-sample-test
[ 50%] Built target uda-sample-test
[ 50%] Generating udf_samples/udf-sample.ll
[ 66%] Built target udf-sample-ir
[ 83%] Built target udfsample
[100%] Built target udf-sample-test
$ hdfs dfs -put ./udf_samples/libudasample.so /user/hive/udfs/
libudasample.so
To create the SQL function, we issue a CREATE AGGREGATE FUNCTION statement and specify
the underlying C++ function names for the different phases:
+------------------------+
| 30 |
+------------------------+
Returned 1 row(s) in 0.35s
Until we create the overloaded version of the UDA, it can only handle a single data type. To allow
it to handle DOUBLE as well as BIGINT, we issue another CREATE AGGREGATE FUNCTION
statement:
Typically, you use a UDA in queries with GROUP BY clauses, to produce a result set with a
separate aggregate value for each combination of values from the GROUP BY clause. Let's change
our sample table to use 0 to indicate rows containing even values, and 1 to flag rows containing
odd values. Then the GROUP BY query can return two values, the sum of the squares for the even
values, and the sum of the squares for the odd values:
[localhost:21000] > insert overwrite sos values (1, 1), (2, 0),
(3, 1), (4, 0);
Inserted 4 rows in 1.24s
User-defined functions (UDFs) are supported starting in Impala 1.2. See User-Defined Functions (UDFs) on page
566 for full details on Impala UDFs.
• Impala supports high-performance UDFs written in C++, as well as reusing some Java-based Hive UDFs.
• Impala supports scalar UDFs and user-defined aggregate functions (UDAFs). Impala does not currently support
user-defined table generating functions (UDTFs).
• Only Impala-supported column types are supported in Java-based UDFs.
• The Hive current_user() function cannot be called from a Java UDF through Impala.
Impala does not currently support these HiveQL statements:
• ANALYZE TABLE (the Impala equivalent is COMPUTE STATS)
• DESCRIBE COLUMN
• DESCRIBE DATABASE
• EXPORT TABLE
• IMPORT TABLE
• SHOW TABLE EXTENDED
• SHOW TBLPROPERTIES
• SHOW INDEXES
• SHOW COLUMNS
• INSERT OVERWRITE DIRECTORY; use INSERT OVERWRITE table_name or CREATE TABLE AS
SELECT to materialize query results into the HDFS directory associated with an Impala table.
Impala respects the serialization.null.format table property only for TEXT tables and ignores the
property for Parquet and other formats. Hive respects the serialization.null.format property for Parquet
and other formats and converts matching values to NULL during the scan. See Using Text Data Files with Impala
Tables on page 673 for using the table property in Impala.
• Impala does not implicitly cast between string and numeric or Boolean types. Always use CAST() for these
conversions.
• Impala does perform implicit casts among the numeric types, when going from a smaller or less precise type
to a larger or more precise one. For example, Impala will implicitly convert a SMALLINT to a BIGINT or
FLOAT, but to convert from DOUBLE to FLOAT or INT to TINYINT requires a call to CAST() in the query.
• Impala does perform implicit casts from string to timestamp. Impala has a restricted set of literal formats for
the TIMESTAMP data type and the from_unixtime() format string; see TIMESTAMP Data Type on page
125 for details.
See Data Types on page 94 for full details on implicit and explicit casting for all types, and Impala Type
Conversion Functions on page 451 for details about the CAST() function.
• Impala does not store or interpret timestamps using the local timezone, to avoid undesired results from unexpected
time zone issues. Timestamps are stored and interpreted relative to UTC. This difference can produce different
results for some calls to similarly named date/time functions between Impala and Hive. See Impala Date and Time
Functions on page 457 for details about the Impala functions. See TIMESTAMP Data Type on page 125 for
a discussion of how Impala handles time zones, and configuration options you can use to make Impala match the
Hive behavior more closely when dealing with Parquet-encoded TIMESTAMP data or when converting between
the local time zone and UTC.
• The Impala TIMESTAMP type can represent dates ranging from 1400-01-01 to 9999-12-31. This is different from
the Hive date range, which is 0000-01-01 to 9999-12-31.
• Impala does not return column overflows as NULL, so that customers can distinguish between NULL data and
overflow conditions similar to how they do so with traditional database systems. Impala returns the largest or
smallest value in the range for the type. For example, valid values for a tinyint range from -128 to 127. In
Impala, a tinyint with a value of -200 returns -128 rather than NULL. A tinyint with a value of 200 returns
127.
Miscellaneous features:
• Impala does not provide virtual columns.
• Impala does not expose locking.
• Impala does not expose some configuration properties.
about the length of string values; it does not impose any length constraints or do any special processing (such
as blank-padding) for STRING columns. (In Impala 2.0 and higher, there are data types VARCHAR and CHAR,
with length constraints for both types and blank-padding for CHAR. However, for performance reasons, it is still
preferable to use STRING columns where practical.)
• For national language character types such as NCHAR, NVARCHAR, or NCLOB, be aware that while Impala can
store and query UTF-8 character data, currently some string manipulation operations only work correctly with
ASCII data. See STRING Data Type on page 117 for details.
• Change any DATE, DATETIME, or TIME columns to TIMESTAMP. Remove any precision constraints. Remove
any timezone clauses, and make sure your application logic or ETL process accounts for the fact that Impala
expects all TIMESTAMP values to be in Coordinated Universal Time (UTC). See TIMESTAMP Data Type on
page 125 for information about the TIMESTAMP data type, and Impala Date and Time Functions on page
457 for conversion functions for different date and time formats.
You might also need to adapt date- and time-related literal values and format strings to use the supported Impala
date and time formats. If you have date and time literals with different separators or different numbers of YY, MM,
and so on placeholders than Impala expects, consider using calls to regexp_replace() to transform those
values to the Impala-compatible format. See TIMESTAMP Data Type on page 125 for information about the
allowed formats for date and time literals, and Impala String Functions on page 488 for string conversion
functions such as regexp_replace().
Instead of SYSDATE, call the function NOW().
Instead of adding or subtracting directly from a date value to produce a value N days in the past or future, use an
INTERVAL expression, for example NOW() + INTERVAL 30 DAYS.
• Although Impala supports INTERVAL expressions for datetime arithmetic, as shown in TIMESTAMP Data Type
on page 125, INTERVAL is not available as a column data type in Impala. For any INTERVAL values stored in
tables, convert them to numeric values that you can add or subtract using the functions in Impala Date and Time
Functions on page 457. For example, if you had a table DEADLINES with an INT column TIME_PERIOD,
you could construct dates N days in the future like so:
| cast(127 * 2 as tinyint) |
+--------------------------+
| -2 |
+--------------------------+
[localhost:21000] > select cast(128 as tinyint);
+----------------------+
| cast(128 as tinyint) |
+----------------------+
| -128 |
+----------------------+
[localhost:21000] > select cast(127*2 as smallint);
+---------------------------+
| cast(127 * 2 as smallint) |
+---------------------------+
| 254 |
+---------------------------+
Impala does not support notation such as b'0101' for bit literals.
• For BLOB values, use STRING to represent CLOB or TEXT types (character based large objects) up to 32 KB in
size. Binary large objects such as BLOB, RAW BINARY, and VARBINARY do not currently have an equivalent in
Impala.
• For Boolean-like types such as BOOL, use the Impala BOOLEAN type.
• Because Impala currently does not support composite or nested types, any spatial data types in other database
systems do not have direct equivalents in Impala. You could represent spatial values in string format and write
UDFs to process them. See User-Defined Functions (UDFs) on page 566 for details. Where practical, separate
spatial types into separate tables so that Impala can still work with the non-spatial data.
• Take out any DEFAULT clauses. Impala can use data files produced from many different sources, such as Pig,
Hive, or MapReduce jobs. The fast import mechanisms of LOAD DATA and external tables mean that Impala is
flexible about the format of data files, and Impala does not necessarily validate or cleanse data before querying it.
When copying data through Impala INSERT statements, you can use conditional functions such as CASE or NVL
to substitute some other value for NULL fields; see Impala Conditional Functions on page 481 for details.
• Take out any constraints from your CREATE TABLE and ALTER TABLE statements, for example PRIMARY
KEY, FOREIGN KEY, UNIQUE, NOT NULL, UNSIGNED, or CHECK constraints. Impala can use data files
produced from many different sources, such as Pig, Hive, or MapReduce jobs. Therefore, Impala expects initial
data validation to happen earlier during the ETL or ELT cycle. After data is loaded into Impala tables, you
can perform queries to test for NULL values. When copying data through Impala INSERT statements, you can
use conditional functions such as CASE or NVL to substitute some other value for NULL fields; see Impala
Conditional Functions on page 481 for details.
Do as much verification as practical before loading data into Impala. After data is loaded into Impala, you can
do further verification using SQL queries to check if values have expected ranges, if values are NULL or not,
and so on. If there is a problem with the data, you will need to re-run earlier stages of the ETL process, or do an
INSERT ... SELECT statement in Impala to copy the faulty data to a new table and transform or filter out the
bad values.
• Take out any CREATE INDEX, DROP INDEX, and ALTER INDEX statements, and equivalent ALTER TABLE
statements. Remove any INDEX, KEY, or PRIMARY KEY clauses from CREATE TABLE and ALTER TABLE
statements. Impala is optimized for bulk read operations for data warehouse-style queries, and therefore does not
support indexes for its tables.
• Calls to built-in functions with out-of-range or otherwise incorrect arguments, return NULL in Impala as opposed
to raising exceptions. (This rule applies even when the ABORT_ON_ERROR=true query option is in effect.) Run
small-scale queries using representative data to doublecheck that calls to built-in functions are returning expected
values rather than NULL. For example, unsupported CAST operations do not raise an error in Impala:
+--------------------+
• For any other type not supported in Impala, you could represent their values in string format and write UDFs to
process them. See User-Defined Functions (UDFs) on page 566 for details.
• To detect the presence of unsupported or unconvertable data types in data files, do initial testing with the
ABORT_ON_ERROR=true query option in effect. This option causes queries to fail immediately if they
encounter disallowed type conversions. See ABORT_ON_ERROR Query Option on page 336 for details. For
example:
set abort_on_error=true;
select count(*) from (select * from t1);
-- The above query will fail if the data files for T1 contain any
-- values that can't be converted to the expected Impala data types.
-- For example, if T1.C1 is defined as INT but the column contains
-- floating-point values like 1.1, the query will return an error.
For example, without the alias contents_of_t1 at the end, the following query gives a syntax error:
Aliases are not required for the subqueries used in other parts of queries. For example:
An alias can be referenced again in the same query if not in the SELECT list. For example, the average alias can
be referenced twice as shown below:
compact data and high-performance queries. Especially see Using the Parquet File Format with Impala Tables on
page 681, for details about the file format most heavily optimized for large-scale data warehouse queries.
• Adapting tables that were already partitioned in a different database system could produce an Impala table with a
high number of partitions and not enough data in each one, leading to underutilization of Impala's parallel query
features.
See Partitioning for Impala Tables on page 661 for details about setting up partitioning and tuning the
performance of queries on partitioned tables.
• The INSERT ... VALUES syntax is suitable for setting up toy tables with a few rows for functional testing
when used with HDFS. Each such statement creates a separate tiny file in HDFS, and it is not a scalable technique
for loading megabytes or gigabytes (let alone petabytes) of data.
Consider revising your data load process to produce raw data files outside of Impala, then setting up Impala
external tables or using the LOAD DATA statement to use those data files instantly in Impala tables, with no
conversion or indexing stage. See External Tables on page 199 and LOAD DATA Statement on page 299 for
details about the Impala techniques for working with data files produced outside of Impala; see Data Loading and
Querying Examples on page 44 for examples of ETL workflow for Impala.
INSERT works fine for Kudu tables even though not particularly fast.
• If your ETL process is not optimized for Hadoop, you might end up with highly fragmented small data files, or a
single giant data file that cannot take advantage of distributed parallel queries or partitioning. In this case, use an
INSERT ... SELECT statement to copy the data into a new table and reorganize into a more efficient layout in
the same operation. See INSERT Statement on page 288 for details about the INSERT statement.
You can do INSERT ... SELECT into a table with a more efficient file format (see How Impala Works with
Hadoop File Formats on page 671) or from an unpartitioned table into a partitioned one. See Partitioning for
Impala Tables on page 661.
• Complex queries may have high codegen time. As a workaround, set the query option
DISABLE_CODEGEN=true if queries fail for this reason. See DISABLE_CODEGEN Query Option on page
342 for details.
• If practical, rewrite UNION queries to use the UNION ALL operator instead. Prefer UNION ALL over UNION
when you know the data sets are disjoint or duplicate values are not a problem; UNION ALL is more efficient
because it avoids materializing and sorting the entire result set to eliminate duplicate values.
more queries simultaneously. Sometimes, an optimization technique improves scalability more than performance.
For example, reducing memory usage for a query might not change the query performance much, but might improve
scalability by allowing more Impala queries or other kinds of jobs to run at the same time without running out of
memory.
Note:
Before starting any performance tuning or benchmarking, make sure your system is configured with all the
recommended minimum hardware requirements from Hardware Requirements on page 22 and software settings
from Post-Installation Configuration for Impala on page 25.
• Partitioning for Impala Tables on page 661. This technique physically divides the data based on the different
values in frequently queried columns, allowing queries to skip reading a large percentage of the data in a table.
• Performance Considerations for Join Queries on page 594. Joins are the main class of queries that you can tune
at the SQL level, as opposed to changing physical factors such as the file format or the hardware configuration.
The related topics Overview of Column Statistics on page 602 and Overview of Table Statistics on page 601
are also important primarily for join performance.
• Overview of Table Statistics on page 601 and Overview of Column Statistics on page 602. Gathering
table and column statistics, using the COMPUTE STATS statement, helps Impala automatically optimize the
performance for join queries, without requiring changes to SQL query statements. (This process is greatly
simplified in Impala 1.2.2 and higher, because the COMPUTE STATS statement gathers both kinds of statistics
in one operation, and does not require any setup and configuration as was previously necessary for the ANALYZE
TABLE statement in Hive.)
• Testing Impala Performance on page 629. Do some post-setup testing to ensure Impala is using optimal
settings for performance, before conducting any benchmark tests.
• Benchmarking Impala Queries on page 617. The configuration and sample data that you use for initial
experiments with Impala is often not appropriate for doing performance tests.
• Controlling Impala Resource Usage on page 617. The more memory Impala can utilize, the better query
performance you can expect. In a cluster running other kinds of workloads as well, you must make tradeoffs to
make sure all Hadoop components have enough memory to perform well, so you might cap the memory that
Impala can use.
• Using Impala with the Amazon S3 Filesystem on page 732. Queries against data stored in the Amazon Simple
Storage Service (S3) have different performance characteristics than when the data is stored in HDFS.
A good source of tips related to scalability and performance tuning is the Impala Cookbook presentation. These slides
are updated periodically as new features come out and new benchmarks are performed.
deployment or conducting benchmarks, always use realistic data volumes to get a true picture of performance and
scalability.
Gather statistics for all tables used in performance-critical or high-volume join queries
Gather the statistics with the COMPUTE STATS statement. See Performance Considerations for Join Queries on
page 594 for details.
Hotspot analysis
In the context of Impala, a hotspot is defined as “an Impala daemon that for a single query or a workload is spending a
far greater amount of time processing data relative to its neighbours”.
Before discussing the options to tackle this issue some background is first required to understand how this problem
can occur.
By default, the scheduling of scan based plan fragments is deterministic. This means that for multiple queries needing
to read the same block of data, the same node will be picked to host the scan. The default scheduling logic does
not take into account node workload from prior queries. The complexity of materializing a tuple depends on a few
factors, namely: decoding and decompression. If the tuples are densely packed into data pages due to good encoding/
compression ratios, there will be more work required when reconstructing the data. Each compression codec offers
different performance tradeoffs and should be considered before writing the data. Due to the deterministic nature of
the scheduler, single nodes can become bottlenecks for highly concurrent queries that use the same tables.
If, for example, a Parquet based dataset is tiny, e.g. a small dimension table, such that it fits into a single HDFS block
(Impala by default will create 256 MB blocks when Parquet is used, each containing a single row group) then there
are a number of options that can be considered to resolve the potential scheduling hotspots when querying this data:
• In Impala 2.5 and higher, the scheduler’s deterministic behaviour can be changed using the following query
options: REPLICA_PREFERENCE and RANDOM_REPLICA. For a detailed description of each of these modes
see IMPALA-2696.
• HDFS caching can be used to cache block replicas. This will cause the Impala scheduler to randomly pick (from
Impala 2.2 and higher) a node that is hosting a cached block replica for the scan. Note, although HDFS caching
has benefits, it serves only to help with the reading of raw block data and not cached tuple data, but with the right
number of cached replicas (by default, HDFS only caches one replica), even load distribution can be achieved for
smaller datasets.
• Do not compress the table data. The uncompressed table data spans more nodes and eliminates skew caused by
compression.
• Reduce the Parquet file size via the PARQUET_FILE_SIZE query option when writing the table data. Using this
approach the data will span more nodes. However it’s not recommended to drop the size below 32 MB.
When you use the STRAIGHT_JOIN technique, you must order the tables in the join query manually instead of
relying on the Impala optimizer. The optimizer uses sophisticated techniques to estimate the size of the result set at
each stage of the join. For manual ordering, use this heuristic approach to start with, and then experiment to fine-tune
the order:
• Specify the largest table first. This table is read from disk by each Impala node and so its size is not significant in
terms of memory usage during the query.
• Next, specify the smallest table. The contents of the second, third, and so on tables are all transmitted across the
network. You want to minimize the size of the result set from each subsequent stage of the join query. The most
likely approach involves joining a small table first, so that the result set remains small even as subsequent larger
tables are processed.
• Join the next smallest table, then the next smallest, and so on.
For example, if you had tables BIG, MEDIUM, SMALL, and TINY, the logical join order to try would be BIG, TINY,
SMALL, MEDIUM.
The terms “largest” and “smallest” refers to the size of the intermediate result set based on the number of rows and
columns from each table that are part of the result set. For example, if you join one table sales with another table
customers, a query might find results from 100 different customers who made a total of 5000 purchases. In that
case, you would specify SELECT ... FROM sales JOIN customers ..., putting customers on the
right side because it is smaller in the context of this query.
The Impala query planner chooses between different techniques for performing join queries, depending on the
absolute and relative sizes of the tables. Broadcast joins are the default, where the right-hand table is considered
to be smaller than the left-hand table, and its contents are sent to all the other nodes involved in the query. The
alternative technique is known as a partitioned join (not related to a partitioned table), which is more suitable for
large tables of roughly equal size. With this technique, portions of each table are sent to appropriate other nodes
where those subsets of rows can be processed in parallel. The choice of broadcast or partitioned join also depends on
statistics being available for all tables in the join, gathered by the COMPUTE STATS statement.
To see which join strategy is used for a particular query, issue an EXPLAIN statement for the query. If you find that a
query uses a broadcast join when you know through benchmarking that a partitioned join would be more efficient, or
vice versa, add a hint to the query to specify the precise join mechanism to use. See Optimizer Hints on page 407
for details.
select straight_join x from medium join small join (select * from big where
c1 < 10) as big
| Tuning Impala for Performance | 596
-- If the query contains [DISTINCT | ALL], the hint goes after those
keywords.
select distinct straight_join x from medium join small join (select * from
big where c1 < 10) as big
where medium.id = small.id and small.id = big.id;
For any kind of performance experimentation, use the EXPLAIN statement to see how any expensive query will be
performed without actually running it, and enable verbose EXPLAIN plans containing more performance-oriented
detail: The most interesting plan lines are highlighted in bold, showing that without statistics for the joined tables,
Impala cannot make a good estimate of the number of rows involved at each stage of processing, and is likely to stick
with the BROADCAST join mechanism that sends a complete copy of one of the tables to each node.
+----------------------------------------------------------+
| Explain String |
+----------------------------------------------------------+
| Estimated Per-Host Requirements: Memory=2.10GB VCores=2 |
| |
| PLAN FRAGMENT 0 |
| PARTITION: UNPARTITIONED |
| |
| 6:AGGREGATE (merge finalize) |
| | output: SUM(COUNT(*)) |
| | cardinality: 1 |
| | per-host memory: unavailable |
| | tuple ids: 2 |
| | |
| 5:EXCHANGE |
| cardinality: 1 |
| per-host memory: unavailable |
| tuple ids: 2 |
| |
| PLAN FRAGMENT 1 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 5 |
| UNPARTITIONED |
| |
| 3:AGGREGATE |
| | output: COUNT(*) |
| | cardinality: 1 |
| | per-host memory: 10.00MB |
| | tuple ids: 2 |
| | |
| 2:HASH JOIN |
| | join op: INNER JOIN (BROADCAST) |
| | hash predicates: |
| | big.id = medium.id |
| | cardinality: unavailable |
| | per-host memory: 2.00GB |
| | tuple ids: 0 1 |
| | |
| |----4:EXCHANGE |
| | cardinality: unavailable |
| | per-host memory: 0B |
| | tuple ids: 1 |
| | |
| 0:SCAN HDFS |
| table=join_order.big #partitions=1/1 size=23.12GB |
| table stats: unavailable |
| column stats: unavailable |
| cardinality: unavailable |
| per-host memory: 88.00MB |
| tuple ids: 0 |
| |
| PLAN FRAGMENT 2 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 4 |
| UNPARTITIONED |
| |
| 1:SCAN HDFS |
| table=join_order.medium #partitions=1/1 size=4.62GB |
| table stats: unavailable |
| Tuning Impala for Performance | 598
Gathering statistics for all the tables is straightforward, one COMPUTE STATS statement per table:
With statistics in place, Impala can choose a more effective join order rather than following the left-to-right sequence
of tables in the query, and can choose BROADCAST or PARTITIONED join strategies based on the overall sizes and
number of rows in the table:
[localhost:21000] > explain select count(*) from medium join big where
big.id = medium.id;
Query: explain select count(*) from medium join big where big.id = medium.id
+-----------------------------------------------------------+
| Explain String |
+-----------------------------------------------------------+
| Estimated Per-Host Requirements: Memory=937.23MB VCores=2 |
| |
| PLAN FRAGMENT 0 |
| PARTITION: UNPARTITIONED |
| |
| 6:AGGREGATE (merge finalize) |
| | output: SUM(COUNT(*)) |
| | cardinality: 1 |
| | per-host memory: unavailable |
| | tuple ids: 2 |
| | |
| 5:EXCHANGE |
| cardinality: 1 |
| per-host memory: unavailable |
| tuple ids: 2 |
| |
| PLAN FRAGMENT 1 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 5 |
| Tuning Impala for Performance | 599
| UNPARTITIONED |
| |
| 3:AGGREGATE |
| | output: COUNT(*) |
| | cardinality: 1 |
| | per-host memory: 10.00MB |
| | tuple ids: 2 |
| | |
| 2:HASH JOIN |
| | join op: INNER JOIN (BROADCAST) |
| | hash predicates: |
| | big.id = medium.id |
| | cardinality: 1443004441 |
| | per-host memory: 839.23MB |
| | tuple ids: 1 0 |
| | |
| |----4:EXCHANGE |
| | cardinality: 200000000 |
| | per-host memory: 0B |
| | tuple ids: 0 |
| | |
| 1:SCAN HDFS |
| table=join_order.big #partitions=1/1 size=23.12GB |
| table stats: 1000000000 rows total |
| column stats: all |
| cardinality: 1000000000 |
| per-host memory: 88.00MB |
| tuple ids: 1 |
| |
| PLAN FRAGMENT 2 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 4 |
| UNPARTITIONED |
| |
| 0:SCAN HDFS |
| table=join_order.medium #partitions=1/1 size=4.62GB |
| table stats: 200000000 rows total |
| column stats: all |
| cardinality: 200000000 |
| per-host memory: 88.00MB |
| tuple ids: 0 |
+-----------------------------------------------------------+
Returned 64 row(s) in 0.04s
[localhost:21000] > explain select count(*) from small join big where big.id
= small.id;
Query: explain select count(*) from small join big where big.id = small.id
+-----------------------------------------------------------+
| Explain String |
+-----------------------------------------------------------+
| Estimated Per-Host Requirements: Memory=101.15MB VCores=2 |
| |
| PLAN FRAGMENT 0 |
| PARTITION: UNPARTITIONED |
| |
| 6:AGGREGATE (merge finalize) |
| | output: SUM(COUNT(*)) |
| | cardinality: 1 |
| | per-host memory: unavailable |
| | tuple ids: 2 |
| | |
| 5:EXCHANGE |
| Tuning Impala for Performance | 600
| cardinality: 1 |
| per-host memory: unavailable |
| tuple ids: 2 |
| |
| PLAN FRAGMENT 1 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 5 |
| UNPARTITIONED |
| |
| 3:AGGREGATE |
| | output: COUNT(*) |
| | cardinality: 1 |
| | per-host memory: 10.00MB |
| | tuple ids: 2 |
| | |
| 2:HASH JOIN |
| | join op: INNER JOIN (BROADCAST) |
| | hash predicates: |
| | big.id = small.id |
| | cardinality: 1000000000 |
| | per-host memory: 3.15MB |
| | tuple ids: 1 0 |
| | |
| |----4:EXCHANGE |
| | cardinality: 1000000 |
| | per-host memory: 0B |
| | tuple ids: 0 |
| | |
| 1:SCAN HDFS |
| table=join_order.big #partitions=1/1 size=23.12GB |
| table stats: 1000000000 rows total |
| column stats: all |
| cardinality: 1000000000 |
| per-host memory: 88.00MB |
| tuple ids: 1 |
| |
| PLAN FRAGMENT 2 |
| PARTITION: RANDOM |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 4 |
| UNPARTITIONED |
| |
| 0:SCAN HDFS |
| table=join_order.small #partitions=1/1 size=17.93MB |
| table stats: 1000000 rows total |
| column stats: all |
| cardinality: 1000000 |
| per-host memory: 32.00MB |
| tuple ids: 0 |
+-----------------------------------------------------------+
Returned 64 row(s) in 0.03s
When queries like these are actually run, the execution times are relatively consistent regardless of the table order in
the query text. Here are examples using both the unique ID column and the VAL column containing duplicate values:
+----------+
| 1000000 |
+----------+
Returned 1 row(s) in 21.68s
[localhost:21000] > select count(*) from small join big on (big.id =
small.id);
Query: select count(*) from small join big on (big.id = small.id)
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
Returned 1 row(s) in 20.45s
Note: When examining the performance of join queries and the effectiveness of the join order optimization, make
sure the query involves enough data and cluster resources to see a difference depending on the query plan. For
example, a single data file of just a few megabytes will reside in a single HDFS block and be processed on a single
node. Likewise, if you use a single-node or two-node cluster, there might not be much difference in efficiency for the
broadcast or partitioned join strategies.
through the entire table, and so that value is displayed as -1. The COMPUTE STATS statement fills in any unknown
table stats values.
Impala performs some optimizations using this metadata on its own, and other optimizations by using a combination
of table and column statistics.
To check that table statistics are available for a table, and see the details of those statistics, use the statement SHOW
TABLE STATS table_name. See SHOW Statement on page 381 for details.
If you use the Hive-based methods of gathering statistics, see the Hive wiki for information about the required
configuration on the Hive side. Where practical, use the Impala COMPUTE STATS statement to avoid potential
configuration and scalability issues with the statistics-gathering process.
If you run the Hive statement ANALYZE TABLE COMPUTE STATISTICS FOR COLUMNS, Impala can only use
the resulting column statistics if the table is unpartitioned. Impala cannot use Hive-generated column statistics for a
partitioned table.
types, such as STRING. The COMPUTE STATS statement fills in most unknown column stats values. (It does not
record the number of NULL values, because currently Impala does not use that figure for query optimization.)
Note:
For column statistics to be effective in Impala, you also need to have table statistics for the applicable tables, as
described in Overview of Table Statistics on page 601. When you use the Impala COMPUTE STATS statement,
both table and column statistics are automatically gathered at the same time, for all columns in the table.
Note: Prior to Impala 1.4.0, COMPUTE STATS counted the number of NULL values in each column and recorded
that figure in the metastore database. Because Impala does not currently use the NULL count during query planning,
Impala 1.4.0 and higher speeds up the COMPUTE STATS statement by skipping this NULL counting.
To check whether column statistics are available for a particular set of columns, use the SHOW COLUMN STATS
table_name statement, or check the extended EXPLAIN output for a query against that table that refers to those
columns. See SHOW Statement on page 381 and EXPLAIN Statement on page 281 for details.
If you run the Hive statement ANALYZE TABLE COMPUTE STATISTICS FOR COLUMNS, Impala can only use
the resulting column statistics if the table is unpartitioned. Impala cannot use Hive-generated column statistics for a
partitioned table.
| Tuning Impala for Performance | 604
+-----------+---------+------------------+--------+----------
+-------------------+
If you run the Hive statement ANALYZE TABLE COMPUTE STATISTICS FOR COLUMNS, Impala can only use
the resulting column statistics if the table is unpartitioned. Impala cannot use Hive-generated column statistics for a
partitioned table.
COMPUTE STATS
The COMPUTE STATS command collects and sets the table-level and partition-level row counts as well as all column
statistics for a given table. The collection process is CPU-intensive and can take a long time to complete for very large
tables.
To speed up COMPUTE STATS consider the following options which can be combined.
• Limit the number of columns for which statistics are collected to increase the efficiency of COMPUTE STATS.
Queries benefit from statistics for those columns involved in filters, join conditions, group by or partition by
clauses. Other columns are good candidates to exclude from COMPUTE STATS. This feature is available since
Impala 2.12.
• Set the MT_DOP query option to use more threads within each participating impalad to compute the statistics
faster - but not more efficiently. Note that computing stats on a large table with a high MT_DOP value can
negatively affect other queries running at the same time if the COMPUTE STATS claims most CPU cycles. This
feature is available since Impala 2.8.
• Consider the experimental extrapolation and sampling features (see below) to further increase the efficiency of
computing stats.
COMPUTE STATS is intended to be run periodically, e.g. weekly, or on-demand when the contents of a table have
changed significantly. Due to the high resource utilization and long repsonse time of tCOMPUTE STATS, it is
most practical to run it in a scheduled maintnance window where the Impala cluster is idle enough to accommodate
the expensive operation. The degree of change that qualifies as “significant” depends on the query workload, but
typically, if 30% of the rows have changed then it is recommended to recompute statistics.
If you reload a complete new set of data for a table, but the number of rows and number of distinct values for each
column is relatively unchanged from before, you do not need to recompute stats for the table.
Experimental: Extrapolation and Sampling
Impala 2.12 and higher includes two experimental features to alleviate common issues for computing and maintaining
statistics on very large tables. The following shortcomings are improved upon:
• Newly added partitions do not have row count statistics. Table scans that only access those new partitions
are treated as not having stats. Similarly, table scans that access both new and old partitions estimate the scan
cardinality based on those old partitions that have stats, and the new partitions without stats are treated as having 0
rows.
• The row counts of existing partitions become stale when data is added or dropped.
• Computing stats for tables with a 100,000 or more partitions might fail or be very slow due to the high cost of
updating the partition metadata in the Hive Metastore.
| Tuning Impala for Performance | 607
• With transient compute resources it is important to minimize the time from starting a new cluster to successfully
running queries. Since the cluster might be relatively short-lived, users might prefer to quickly collect stats that are
"good enough" as opposed to spending a lot of time and resouces on computing full-fidelity stats.
For very large tables, it is often wasteful or impractical to run a full COMPUTE STATS to address the scenarios
above on a frequent basis.
The sampling feature makes COMPUTE STATS more efficient by processing a fraction of the table data, and the
extrapolation feature aims to reduce the frequency at which COMPUTE STATS needs to be re-run by estimating the
row count of new and modified partitions.
The sampling and extrapolation features are disabled by default. They can be enabled globally or for specific tables,
as follows. Set the impalad start-up configuration "--enable_stats_extrapolation" to enable the features globally. To
enable them only for a specific table, set the "impala.enable.stats.extrapolation" table property to "true" for the desired
table. The table-level property overrides the global setting, so it is also possible to enable sampling and extrapolation
globally, but disable it for specific tables by setting the table property to "false". Example: ALTER TABLE mytable
test_table SET TBLPROPERTIES("impala.enable.stats.extrapolation"="true")
Note: Why are these features experimental? Due to their probabilistic nature it is possible that these features perform
pathologically poorly on tables with extreme data/file/size distributions. Since it is not feasible for us to test all
possible scenarios we only cautiously advertise these new capabilities. That said, the features have been thoroughly
tested and are considered functionally stable. If you decide to give these features a try, please tell us about your
experience at [email protected]! We rely on user feedback to guide future inprovements in statistics collection.
Stats Extrapolation
The main idea of stats extrapolation is to estimate the row count of new and modified partitions based on the result
of the last COMPUTE STATS. Enabling stats extrapolation changes the behavior of COMPUTE STATS, as well as
the cardinality estimation of table scans. COMPUTE STATS no longer computes and stores per-partition row counts,
and instead, only computes a table-level row count together with the total number of file bytes in the table at that time.
No partition metadata is modified. The input cardinality of a table scan is estimated by converting the data volume
of relevant partitions to a row count, based on the table-level row count and file bytes statistics. It is assumed that
within the same table, different sets of files with the same data volume correspond to the similar number of rows on
average. With extrapolation enabled, the scan cardinality estimation ignores per-partition row counts. It only relies on
the table-level statistics and the scanned data volume.
The SHOW TABLE STATS and EXPLAIN commands distinguish between row counts stored in the Hive Metastore,
and the row counts extrapolated based on the above process. Consult the SHOW TABLE STATS and EXPLAIN
documentation for more details.
Sampling
A TABLESAMPLE clause may be added to COMPUTE STATS to limit the percentage of data to be processed. The
final statistics are obtained by extrapolating the statistics from the data sample over the entire table. The extrapolated
statistics are stored in the Hive Metastore, just as if no sampling was used. The following example runs COMPUTE
STATS over a 10 percent data sample: COMPUTE STATS test_table TABLESAMPLE SYSTEM(10)
We have found that a 10 percent sampling rate typically offers a good tradeoff between statistics accuracy and
execution cost. A sampling rate well below 10 percent has shown poor results and is not recommended.
Important: Sampling-based techniques sacrifice result accuracy for execution efficiency, so your mileage may vary
for different tables and columns depending on their data distribution. The extrapolation procedure Impala uses for
estimating the number of distinct values per column is inherently non-detetministic, so your results may even vary
between runs of COMPUTE STATS TABLESAMPLE, even if no data has changed.
When you compute incremental statistics for a partitioned table, by default Impala only processes those partitions that
do not yet have incremental statistics. By processing only newly added partitions, you can keep statistics up to date
without incurring the overhead of reprocessing the entire table each time.
You can also compute or drop statistics for a specified subset of partitions by including a PARTITION clause in the
COMPUTE INCREMENTAL STATS or DROP INCREMENTAL STATS statement.
Important:
In Impala 3.0 and lower, approximately 400 bytes of metadata per column per partition are needed for caching. Tables
with a big number of partitions and many columns can add up to a significant memory overhead as the metadata must
be cached on the catalogd host and on every impalad host that is eligible to be a coordinator. If this metadata for
all tables exceeds 2 GB, you might experience service downtime. In Impala 3.1 and higher, the issue was alleviated
with an improved handling of incremental stats.
When you run COMPUTE INCREMENTAL STATS on a table for the first time, the statistics are computed again
from scratch regardless of whether the table already has statistics. Therefore, expect a one-time resource-intensive
operation for scanning the entire table when running COMPUTE INCREMENTAL STATS for the first time on a
given table.
The metadata for incremental statistics is handled differently from the original style of statistics:
• Issuing a COMPUTE INCREMENTAL STATS without a partition clause causes Impala to compute incremental
stats for all partitions that do not already have incremental stats. This might be the entire table when running
the command for the first time, but subsequent runs should only update new partitions. You can force updating
a partition that already has incremental stats by issuing a DROP INCREMENTAL STATS before running
COMPUTE INCREMENTAL STATS.
• The SHOW TABLE STATS and SHOW PARTITIONS statements now include an additional column showing
whether incremental statistics are available for each column. A partition could already be covered by the original
type of statistics based on a prior COMPUTE STATS statement, as indicated by a value other than -1 under the
#Rows column. Impala query planning uses either kind of statistics when available.
• COMPUTE INCREMENTAL STATS takes more time than COMPUTE STATS for the same volume of data.
Therefore it is most suitable for tables with large data volume where new partitions are added frequently, making
it impractical to run a full COMPUTE STATS operation for each new partition. For unpartitioned tables, or
partitioned tables that are loaded once and not updated with new partitions, use the original COMPUTE STATS
syntax.
• COMPUTE INCREMENTAL STATS uses some memory in the catalogd process, proportional to the number
of partitions and number of columns in the applicable table. The memory overhead is approximately 400 bytes for
each column in each partition. This memory is reserved in the catalogd daemon, the statestored daemon,
and in each instance of the impalad daemon.
• In cases where new files are added to an existing partition, issue a REFRESH statement for the table, followed by a
DROP INCREMENTAL STATS and COMPUTE INCREMENTAL STATS sequence for the changed partition.
• The DROP INCREMENTAL STATS statement operates only on a single partition at a time. To remove
statistics (whether incremental or not) from all partitions of a table, issue a DROP STATS statement with no
INCREMENTAL or PARTITION clauses.
The following considerations apply to incremental statistics when the structure of an existing table is changed (known
as schema evolution):
• If you use an ALTER TABLE statement to drop a column, the existing statistics remain valid and COMPUTE
INCREMENTAL STATS does not rescan any partitions.
• If you use an ALTER TABLE statement to add a column, Impala rescans all partitions and fills in the appropriate
column-level values the next time you run COMPUTE INCREMENTAL STATS.
• If you use an ALTER TABLE statement to change the data type of a column, Impala rescans all partitions and fills
in the appropriate column-level values the next time you run COMPUTE INCREMENTAL STATS.
• If you use an ALTER TABLE statement to change the file format of a table, the existing statistics remain valid and
a subsequent COMPUTE INCREMENTAL STATS does not rescan any partitions.
See COMPUTE STATS Statement on page 223 and DROP STATS Statement on page 274 for syntax details.
| Tuning Impala for Performance | 609
In Impala 3.1 and higher, Impala improved how metadata is updated when executing COMPUTE INCREMENTAL
STATS, significantly reducing the need for --inc_stats_size_limit_bytes.
The following example shows a similar progression with a partitioned table. Initially, #Rows is -1. After a
COMPUTE STATS operation, #Rows changes to an accurate value. Any newly added partition starts with no
statistics, meaning that you must collect statistics after adding a new partition.
+-------+-------+--------+------+--------------+--------+-------------------
+
| year | #Rows | #Files | Size | Bytes Cached | Format | Incremental stats
|
+-------+-------+--------+------+--------------+--------+-------------------
+
| Total | -1 | 0 | 0B | 0B | |
|
+-------+-------+--------+------+--------------+--------+-------------------
+
[localhost:21000] > show partitions no_stats_partitioned;
+-------+-------+--------+------+--------------+--------+-------------------
+
| year | #Rows | #Files | Size | Bytes Cached | Format | Incremental stats
|
+-------+-------+--------+------+--------------+--------+-------------------
+
| Total | -1 | 0 | 0B | 0B | |
|
+-------+-------+--------+------+--------------+--------+-------------------
+
[localhost:21000] > alter table no_stats_partitioned add partition
(year=2013);
[localhost:21000] > compute stats no_stats_partitioned;
+-----------------------------------------+
| summary |
+-----------------------------------------+
| Updated 1 partition(s) and 1 column(s). |
+-----------------------------------------+
[localhost:21000] > alter table no_stats_partitioned add partition
(year=2014);
[localhost:21000] > show partitions no_stats_partitioned;
+-------+-------+--------+------+--------------+--------+-------------------
+
| year | #Rows | #Files | Size | Bytes Cached | Format | Incremental stats
|
+-------+-------+--------+------+--------------+--------+-------------------
+
| 2013 | 0 | 0 | 0B | NOT CACHED | TEXT | false
|
| 2014 | -1 | 0 | 0B | NOT CACHED | TEXT | false
|
| Total | 0 | 0 | 0B | 0B | |
|
+-------+-------+--------+------+--------------+--------+-------------------
+
Note: Because the default COMPUTE STATS statement creates and updates statistics for all partitions in a table, if
you expect to frequently add new partitions, use the COMPUTE INCREMENTAL STATS syntax instead, which lets
you compute stats for a single specified partition, or only for those partitions that do not already have incremental
stats.
If checking each individual table is impractical, due to a large number of tables or views that hide the underlying base
tables, you can also check for missing statistics for a particular query. Use the EXPLAIN statement to preview query
efficiency before actually running the query. Use the query profile output available through the PROFILE command
in impala-shell or the web UI to verify query execution and timing after running the query. Both the EXPLAIN
plan and the PROFILE output display a warning if any tables or partitions involved in the query do not have statistics.
| Explain String
|
+------------------------------------------------------------------------------------
+
| Estimated Per-Host Requirements: Memory=10.00MB VCores=1
|
| WARNING: The following tables are missing relevant table and/or column
statistics. |
| incremental_stats.no_stats
|
|
|
| 03:AGGREGATE [FINALIZE]
|
| | output: count:merge(*)
|
| |
|
| 02:EXCHANGE [UNPARTITIONED]
|
| |
|
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HDFS [incremental_stats.no_stats]
|
| partitions=1/1 files=0 size=0B
|
+------------------------------------------------------------------------------------
+
Because Impala uses the partition pruning technique when possible to only evaluate certain partitions, if you have a
partitioned table with statistics for some partitions and not others, whether or not the EXPLAIN statement shows the
warning depends on the actual partitions used by the query. For example, you might see warnings or not for different
queries against the same table:
-- No warning because all the partitions for the year 2012 have stats.
EXPLAIN SELECT ... FROM t1 WHERE year = 2012;
To confirm if any partitions at all in the table are missing statistics, you might explain a query that scans the entire
table, such as SELECT COUNT(*) FROM table_name.
produce a more efficient plan if the number of rows was different, you can manually set the number of rows through
an ALTER TABLE statement:
This statement avoids re-scanning any data files. (The requirement to include the
STATS_GENERATED_VIA_STATS_TASK property is relatively new, as a result of the issue HIVE-8648 for the
Hive metastore.)
For a partitioned table, update both the per-partition number of rows and the number of rows for the whole table:
In practice, the COMPUTE STATS statement, or COMPUTE INCREMENTAL STATS for a partitioned table, should
be fast and convenient enough that this technique is only useful for the very largest partitioned tables. Because the
column statistics might be left in a stale state, do not use this technique as a replacement for COMPUTE STATS. Only
use this technique if all other means of collecting statistics are impractical, or as a low-overhead operation that you
run in between periodic COMPUTE STATS or COMPUTE INCREMENTAL STATS operations.
| s_market_id | INT | -1 | -1 | 4 | 4
|
| s_geography_class | STRING | -1 | -1 | -1 | -1
|
| s_market_desc | STRING | -1 | -1 | -1 | -1
|
| s_market_manager | STRING | -1 | -1 | -1 | -1
|
| s_division_id | INT | -1 | -1 | 4 | 4
|
| s_division_name | STRING | -1 | -1 | -1 | -1
|
| s_company_id | INT | -1 | -1 | 4 | 4
|
| s_company_name | STRING | -1 | -1 | -1 | -1
|
| s_street_number | STRING | -1 | -1 | -1 | -1
|
| s_street_name | STRING | -1 | -1 | -1 | -1
|
| s_street_type | STRING | -1 | -1 | -1 | -1
|
| s_suite_number | STRING | -1 | -1 | -1 | -1
|
| s_city | STRING | -1 | -1 | -1 | -1
|
| s_county | STRING | -1 | -1 | -1 | -1
|
| s_state | STRING | -1 | -1 | -1 | -1
|
| s_zip | STRING | -1 | -1 | -1 | -1
|
| s_country | STRING | -1 | -1 | -1 | -1
|
| s_gmt_offset | FLOAT | -1 | -1 | 4 | 4
|
| s_tax_percentage | FLOAT | -1 | -1 | 4 | 4
|
+--------------------+-----------+------------------+--------+----------
+----------+
Returned 29 row(s) in 0.04s
With the Hive ANALYZE TABLE statement for column statistics, you had to specify each column for which to
gather statistics. The Impala COMPUTE STATS statement automatically gathers statistics for all columns, because
it reads through the entire table relatively quickly and can efficiently compute the values for all the columns. This
example shows how after running the COMPUTE STATS statement, statistics are filled in for both the table and all its
columns:
+--------------------+-----------+------------------+--------+----------
+-------------------+
| Column | Type | #Distinct Values | #Nulls | Max Size |
Avg Size |
+--------------------+-----------+------------------+--------+----------
+-------------------+
| s_store_sk | INT | 12 | -1 | 4 | 4
|
| s_store_id | STRING | 6 | -1 | 16 | 16
|
| s_rec_start_date | TIMESTAMP | 4 | -1 | 16 | 16
|
| s_rec_end_date | TIMESTAMP | 3 | -1 | 16 | 16
|
| s_closed_date_sk | INT | 3 | -1 | 4 | 4
|
| s_store_name | STRING | 8 | -1 | 5 |
4.25 |
| s_number_employees | INT | 9 | -1 | 4 | 4
|
| s_floor_space | INT | 10 | -1 | 4 | 4
|
| s_hours | STRING | 2 | -1 | 8 |
7.083300113677979 |
| s_manager | STRING | 7 | -1 | 15 | 12
|
| s_market_id | INT | 7 | -1 | 4 | 4
|
| s_geography_class | STRING | 1 | -1 | 7 | 7
|
| s_market_desc | STRING | 10 | -1 | 94 |
55.5 |
| s_market_manager | STRING | 7 | -1 | 16 | 14
|
| s_division_id | INT | 1 | -1 | 4 | 4
|
| s_division_name | STRING | 1 | -1 | 7 | 7
|
| s_company_id | INT | 1 | -1 | 4 | 4
|
| s_company_name | STRING | 1 | -1 | 7 | 7
|
| s_street_number | STRING | 9 | -1 | 3 |
2.833300113677979 |
| s_street_name | STRING | 12 | -1 | 11 |
6.583300113677979 |
| s_street_type | STRING | 8 | -1 | 9 |
4.833300113677979 |
| s_suite_number | STRING | 11 | -1 | 9 |
8.25 |
| s_city | STRING | 2 | -1 | 8 |
6.5 |
| s_county | STRING | 1 | -1 | 17 | 17
|
| s_state | STRING | 1 | -1 | 2 | 2
|
| s_zip | STRING | 2 | -1 | 5 | 5
|
| s_country | STRING | 1 | -1 | 13 | 13
|
| s_gmt_offset | FLOAT | 1 | -1 | 4 | 4
|
| s_tax_percentage | FLOAT | 5 | -1 | 4 | 4
|
| Tuning Impala for Performance | 616
+--------------------+-----------+------------------+--------+----------
+-------------------+
Returned 29 row(s) in 0.04s
The following example shows how statistics are represented for a partitioned table. In this case, we have set up a table
to hold the world's most trivial census data, a single STRING field, partitioned by a YEAR column. The table statistics
include a separate entry for each partition, plus final totals for the numeric fields. The column statistics include some
easily deducible facts for the partitioning column, such as the number of distinct values (the number of partition
subdirectories).
The following example shows how the statistics are filled in by a COMPUTE STATS statement in Impala.
+-------+-------+--------+------+---------+
Returned 8 row(s) in 0.02s
[localhost:21000] > show column stats census;
+--------+----------+------------------+--------+----------+----------+
| Column | Type | #Distinct Values | #Nulls | Max Size | Avg Size |
+--------+----------+------------------+--------+----------+----------+
| name | STRING | 4 | -1 | 5 | 4.5 |
| year | SMALLINT | 7 | -1 | 2 | 2 |
+--------+----------+------------------+--------+----------+----------+
Returned 2 row(s) in 0.02s
For examples showing how some queries work differently when statistics are available, see Examples of Join Order
Optimization on page 596. You can see how Impala executes a query differently in each case by observing the
EXPLAIN output before and after collecting statistics. Measure the before and after query times, and examine the
throughput numbers in before and after SUMMARY or PROFILE output, to verify how much the improved plan speeds
up performance.
This feature is primarily used to optimize queries against large partitioned tables (under the name dynamic partition
pruning) and joins of large tables. The information in this section includes concepts, internals, and troubleshooting
information for the entire runtime filtering feature. For specific tuning steps for partitioned tables, see Dynamic
Partition Pruning on page 665.
Important:
When this feature made its debut in Impala 2.5, the default setting was RUNTIME_FILTER_MODE=LOCAL. Now
the default is RUNTIME_FILTER_MODE=GLOBAL in Impala 2.6 and higher, which enables more wide-ranging and
ambitious query optimization without requiring you to explicitly set any query options.
SELECT s FROM yy WHERE year IN (SELECT year FROM yy2); -- Returns 3 rows
from yy
PROFILE;
The query profile (displayed by the PROFILE command in impala-shell) contains both the EXPLAIN plan
and more detailed information about the internal workings of the query. The profile output includes the Filter
routing table section with information about each filter based on its ID.
In this example, T1 is a table partitioned by year. The subquery on T2 produces multiple values, and transmits those
values as a filter to the plan fragments that are reading from T1. Any non-matching partitions in T1 are skipped.
Now the WHERE clause contains an additional test that does not apply to the partition key column. A filter on a
column that is not a partition key is called a per-row filter. Because per-row filters only apply for Parquet, T1 must be
a Parquet table.
The subqueries result in two filters being transmitted to the scan nodes that read from T1. The filter on YEAR helps
the query eliminate entire partitions based on non-matching years. The filter on C2 lets Impala discard rows with non-
matching C2 values immediately after reading them. Without runtime filtering, Impala would have to keep the non-
matching values in memory, assemble C1, C2, and C3 into rows in the intermediate result set, and transmit all the
intermediate rows back to the coordinator node, where they would be eliminated only at the very end of the query.
This example involves a broadcast join. The fact that the ON clause would return a small number of matching rows
(because there are not very many rows in TINY_T2) means that the corresponding filter is very selective. Therefore,
runtime filtering will probably be effective in optimizing this query.
This example involves a shuffle or partitioned join. Assume that most rows in HUGE_T1 have a corresponding row
in HUGE_T2. The fact that the ON clause could return a large number of matching rows means that the corresponding
filter would not be very selective. Therefore, runtime filtering might be less effective in optimizing this query.
lead to CPU contention if the same host processes each cached block. Increasing the replication factor lets Impala
choose different hosts to process different cached blocks, to better distribute the CPU load. Always use a WITH
REPLICATION setting of at least 3, and adjust upward if necessary to match the replication factor for the underlying
HDFS data files.
In Impala 2.5 and higher, Impala automatically randomizes which host processes a cached HDFS block, to
avoid CPU hotspots. For tables where HDFS caching is not applied, Impala designates which host to process
a data block using an algorithm that estimates the load on each host. If CPU hotspots still arise during queries,
you can enable additional randomization for the scheduling algorithm for non-HDFS cached data by setting the
SCHEDULE_RANDOM_REPLICA query option.
For background information about how to set up and manage HDFS caching for a cluster, see the documentation for
your Apache Hadoop distribution.
For details about the hdfs cacheadmin command, see the documentation for your Apache Hadoop
distribution.
Once HDFS caching is enabled and one or more pools are available, see Enabling HDFS Caching for Impala Tables
and Partitions on page 623 for how to choose which Impala data to load into the HDFS cache. On the Impala
side, you specify the cache pool name defined by the hdfs cacheadmin command in the Impala DDL statements
that enable HDFS caching for a table or partition, such as CREATE TABLE ... CACHED IN pool or ALTER
TABLE ... SET CACHED IN pool.
partitioned table CENSUS with a partition key column YEAR, you could choose to cache all or part of the data as
follows:
In Impala 2.2 and higher, the optional WITH REPLICATION clause for CREATE TABLE and ALTER TABLE lets
you specify a replication factor, the number of hosts on which to cache the same data blocks. When Impala processes
a cached data block, where the cache replication factor is greater than 1, Impala randomly selects a host that has a
cached copy of that data block. This optimization avoids excessive CPU usage on a single host when the same cached
data block is processed multiple times. Where practical, specify a value greater than or equal to the HDFS block
replication factor.
-- Cache the data from one partition on up to 4 hosts, to minimize CPU load
on any
-- single host when the same data block is processed multiple times.
alter table census partition (year=1970)
set cached in 'pool_name' with replication = 4;
• If you have designated a table or partition as cached through the CREATE TABLE or ALTER TABLE statements,
subsequent attempts to relocate the table or partition through an ALTER TABLE ... SET LOCATION
statement will fail. You must issue an ALTER TABLE ... SET UNCACHED statement for the table or
partition first. Otherwise, Impala would lose track of some cached data files and have no way to uncache them
later.
profile illustrates that all the data read during a particular phase of the query came from the HDFS cache, because the
BytesRead and BytesReadDataNodeCache values are identical.
For queries involving smaller amounts of data, or in single-user workloads, you might not notice a significant
difference in query response time with or without HDFS caching. Even with HDFS caching turned off, the data for
the query might still be in the Linux OS buffer cache. The benefits become clearer as data volume increases, and
especially as the system processes more concurrent queries. HDFS caching improves the scalability of the overall
system. That is, it prevents query performance from declining when the workload outstrips the capacity of the Linux
OS cache.
Due to a limitation of HDFS, zero-copy reads are not supported with encryption. Where practical, avoid HDFS
caching for Impala data files in encryption zones. The queries fall back to the normal read path during query
execution, which might cause some performance overhead.
SELECT considerations:
The Impala HDFS caching feature interacts with the SELECT statement and query performance as follows:
• Impala automatically reads from memory any data that has been designated as cached and actually loaded into
the HDFS cache. (It could take some time after the initial request to fully populate the cache for a table with large
size or many partitions.) The speedup comes from two aspects: reading from RAM instead of disk, and accessing
the data straight from the cache area instead of copying from one RAM area to another. This second aspect yields
further performance improvement over the standard OS caching mechanism, which still results in memory-to-
memory copying of cached data.
• For small amounts of data, the query speedup might not be noticeable in terms of wall clock time. The
performance might be roughly the same with HDFS caching turned on or off, due to recently used data being held
in the Linux OS cache. The difference is more pronounced with:
• Data volumes (for all queries running concurrently) that exceed the size of the Linux OS cache.
• A busy cluster running many concurrent queries, where the reduction in memory-to-memory copying and
overall memory usage during queries results in greater scalability and throughput.
• Thus, to really exercise and benchmark this feature in a development environment, you might need to simulate
realistic workloads and concurrent queries that match your production environment.
• One way to simulate a heavy workload on a lightly loaded system is to flush the OS buffer cache (on each
DataNode) between iterations of queries against the same tables or partitions:
$ sync
$ echo 1 > /proc/sys/vm/drop_caches
• Impala queries take advantage of HDFS cached data regardless of whether the cache directive was issued by
Impala or externally through the hdfs cacheadmin command, for example for an external table where the
cached data files might be accessed by several different Hadoop components.
• If your query returns a large result set, the time reported for the query could be dominated by the time needed to
print the results on the screen. To measure the time for the underlying query processing, query the COUNT() of
the big result set, which does all the same processing but only prints a single line to the screen.
| Tuning Impala for Performance | 628
earlier in the ETL pipeline by non-Impala components. In Impala 2.0 and later, the default Parquet block size is
256 MB, reduced from 1 GB, to improve parallelism for common cluster sizes and data volumes.
• Reduce the amount of compression applied to the data. For text data files, the highest degree of compression
(gzip) produces unsplittable files that are more difficult for Impala to process in parallel, and require extra memory
during processing to hold the compressed and uncompressed data simultaneously. For binary formats such as
Parquet and Avro, compression can result in fewer data blocks overall, but remember that when queries process
relatively few blocks, there is less opportunity for parallel execution and many nodes in the cluster might sit idle.
Note that when Impala writes Parquet data with the query option COMPRESSION_CODEC=NONE enabled, the
data is still typically compact due to the encoding schemes used by Parquet, independent of the final compression
step.
--data_cache=dir1,dir2,dir3,...:quota
The flag is set to a list of directories, separated by ,, followed by a :, and a capacity quota per directory.
If set to an empty string, data caching is disabled.
Cached data is stored in the specified directories.
The specified directories must exist in the local filesystem of each Impala Daemon, or Impala will fail to start.
In addition, the filesystem which the directory resides in must support hole punching.
The cache can consume up to the quota bytes for each of the directories specified.
The default setting for --data_cache is an empty string.
For example, with the following setting, the data cache may use up to 1 TB, with 500 GB max in /data/0 and /
data/1 respectively.
--data_cache=/data/0,/data/1:500GB
Note: In the preceding example, replace hostname and port with the name and port of your Impala server.
The default port is 25000.
2. Review the configured values.
For example, to check that your system is configured to use block locality tracking information, you would check
that the value for dfs.datanode.hdfs-blocks-metadata.enabled is true.
To check data locality:
1. Execute a query on a dataset that is available across multiple nodes. For example, for a table named MyTable
that has a reasonable chance of being spread across multiple DataNodes:
The presence of remote scans may indicate impalad is not running on the correct nodes. This can be because some
DataNodes do not have impalad running or it can be because the impalad instance that is starting the query is
unable to contact one or more of the impalad instances.
To understand the causes of this issue:
1. Connect to the debugging web server. By default, this server runs on port 25000. This page lists all impalad
instances running in your cluster. If there are fewer instances than you expect, this often indicates some
DataNodes are not running impalad. Ensure impalad is started on all DataNodes.
2. If you are using multi-homed hosts, ensure that the Impala daemon's hostname resolves to the interface on which
impalad is running. The hostname Impala is using is displayed when impalad starts. To explicitly set the
hostname, use the --hostname flag.
3. Check that statestored is running as expected. Review the contents of the state store log to ensure all
instances of impalad are listed as having connected to the state store.
Unknown disk id. This will negatively affect Tracking block locality is
performance. Check your hdfs settings to enable block not enabled.
location metadata
For an overview of the physical performance characteristics for a query, issue the SUMMARY statement in impala-
shell immediately after executing a query. This condensed information shows which phases of execution took the
most time, and how the estimates for memory usage and number of rows at each phase compare to the actual values.
To understand the detailed performance characteristics for a query, issue the PROFILE statement in impala-
shell immediately after executing a query. This low-level information includes physical details about memory,
CPU, I/O, and network usage, and thus is only available after the query is actually run.
Also, see Performance Considerations for the Impala-HBase Integration on page 724 and Understanding and
Tuning Impala Query Performance for S3 Data on page 738 for examples of interpreting EXPLAIN plans for
queries against HBase tables and data stored in the Amazon Simple Storage System (S3).
Notice how the longest initial phase of the query is measured in seconds (s), while later phases working on smaller
intermediate results are measured in milliseconds (ms) or even nanoseconds (ns).
Here is an example from a more complicated query, as it would appear in the PROFILE output:
A good source of tips related to scalability and performance tuning is the Impala Cookbook presentation. These slides
are updated periodically as new features come out and new benchmarks are performed.
2. Decide on a large enough value for the catalogd heap. You use the JAVA_TOOL_OPTIONS environment
variable to set the maximum heap size. For example, the following environment variable setting specifies the
maximum heap size of 8 GB.
JAVA_TOOL_OPTIONS="-Xmx8g"
3. On systems not using cluster management software, put this environment variable setting into the startup script for
the catalogd daemon, then restart the catalogd daemon.
4. Use the same jcmd and jmap commands as earlier to verify that the new settings are in effect.
If it takes a very long time for a cluster to start up, and impala-shell consistently displays This Impala
daemon is not ready to accept user requests, the statestore might be taking too long to send the
entire catalog topic to the cluster. In this case, consider adding --load_catalog_in_background=false to
your catalog service configuration. This setting stops the statestore from loading the entire catalog into memory at
cluster startup. Instead, metadata for each table is loaded when the table is accessed for the first time.
The result is a query that completes successfully, rather than failing with an out-of-memory error. The tradeoff is
decreased performance due to the extra disk I/O to write the temporary data and read it back in. The slowdown could
be potentially be significant. Thus, while this feature improves reliability, you should optimize your queries, system
parameters, and hardware configuration to make this spilling a rare occurrence.
Note:
In Impala 2.10 and higher, also see Scalability Considerations for Impala on page 633 for changes to Impala
memory allocation that might change the details of which queries spill to disk, and how much memory and disk space
is involved in the spilling operation.
What kinds of queries might spill to disk:
Several SQL clauses and constructs require memory allocations that could activat the spilling mechanism:
• when a query uses a GROUP BY clause for columns with millions or billions of distinct values, Impala keeps a
similar number of temporary results in memory, to accumulate the aggregate results for each value in the group.
• When large tables are joined together, Impala keeps the values of the join columns from one table in memory, to
compare them to incoming values from the other table.
• When a large result set is sorted by the ORDER BY clause, each node sorts its portion of the result set in memory.
• The DISTINCT and UNION operators build in-memory data structures to represent all values found so far, to
eliminate duplicates as the query progresses.
When the spill-to-disk feature is activated for a join node within a query, Impala does not produce any runtime filters
for that join operation on that host. Other join nodes within the query are not affected.
How Impala handles scratch disk space for spilling:
By default, intermediate files used during large sort, join, aggregation, or analytic function operations are stored
in the directory /tmp/impala-scratch . These files are removed when the operation finishes. (Multiple
concurrent queries can perform operations that use the “spill to disk” technique, without any name conflicts
for these temporary files.) You can specify a different location by starting the impalad daemon with the
‑‑scratch_dirs="path_to_directory" configuration option. You can specify a single directory, or a comma-
separated list of directories. The scratch directories must be on the local filesystem, not in HDFS. You might specify
different directory paths for different hosts, depending on the capacity and speed of the available storage devices. In
Impala 2.3 or higher, Impala successfully starts (with a warning Impala successfully starts (with a warning written to
the log) if it cannot create or read and write files in one of the scratch directories. If there is less than 1 GB free on the
filesystem where that directory resides, Impala still runs, but writes a warning message to its log. If Impala encounters
an error reading or writing files in a scratch directory during a query, Impala logs the error and the query fails.
Memory usage for SQL operators:
In Impala 2.10 and higher, the way SQL operators such as GROUP BY, DISTINCT, and joins, transition between
using additional memory or activating the spill-to-disk feature is changed. The memory required to spill to disk is
reserved up front, and you can examine it in the EXPLAIN plan when the EXPLAIN_LEVEL query option is set to 2
or higher.
The infrastructure of the spilling feature affects the way the affected SQL operators, such as GROUP BY, DISTINCT,
and joins, use memory. On each host that participates in the query, each such operator in a query requires memory to
store rows of data and other data structures. Impala reserves a certain amount of memory up front for each operator
that supports spill-to-disk that is sufficient to execute the operator. If an operator accumulates more data than can fit
in the reserved memory, it can either reserve more memory to continue processing data in memory or start spilling
data to temporary scratch files on disk. Thus, operators with spill-to-disk support can adapt to different memory
constraints by using however much memory is available to speed up execution, yet tolerate low memory conditions
by spilling data to disk.
The amount data depends on the portion of the data being handled by that host, and thus the operator may end up
consuming different amounts of memory on different hosts.
Added in: This feature was added to the ORDER BY clause in Impala 1.4. This feature was extended to cover join
queries, aggregation functions, and analytic functions in Impala 2.0. The size of the memory work area required by
| Scalability Considerations for Impala | 637
each operator that spills was reduced from 512 megabytes to 256 megabytes in Impala 2.2. The spilling mechanism
was reworked to take advantage of the Impala buffer pool feature and be more predictable and stable in Impala 2.10.
Avoiding queries that spill to disk:
Because the extra I/O can impose significant performance overhead on these types of queries, try to avoid this
situation by using the following steps:
1. Detect how often queries spill to disk, and how much temporary data is written. Refer to the following sources:
• The output of the PROFILE command in the impala-shell interpreter. This data shows the memory usage
for each host and in total across the cluster. The WriteIoBytes counter reports how much data was written
to disk for each operator during the query. (In Impala 2.9, the counter was named ScratchBytesWritten;
in Impala 2.8 and earlier, it was named BytesWritten.)
• The Queries tab in the Impala debug web user interface. Select the query to examine and click the
corresponding Profile link. This data breaks down the memory usage for a single host within the cluster, the
host whose web interface you are connected to.
2. Use one or more techniques to reduce the possibility of the queries spilling to disk:
• Increase the Impala memory limit if practical, for example, if you can increase the available memory by more
than the amount of temporary data written to disk on a particular node. Remember that in Impala 2.0 and later,
you can issue SET MEM_LIMIT as a SQL statement, which lets you fine-tune the memory usage for queries
from JDBC and ODBC applications.
• Increase the number of nodes in the cluster, to increase the aggregate memory available to Impala and reduce
the amount of memory required on each node.
• Add more memory to the hosts running Impala daemons.
• On a cluster with resources shared between Impala and other Hadoop components, use resource management
features to allocate more memory for Impala. See Resource Management on page 650 for details.
• If the memory pressure is due to running many concurrent queries rather than a few memory-intensive ones,
consider using the Impala admission control feature to lower the limit on the number of concurrent queries. By
spacing out the most resource-intensive queries, you can avoid spikes in memory usage and improve overall
response times. See Admission Control and Query Queuing on page 650 for details.
• Tune the queries with the highest memory requirements, using one or more of the following techniques:
• Run the COMPUTE STATS statement for all tables involved in large-scale joins and aggregation queries.
• Minimize your use of STRING columns in join columns. Prefer numeric values instead.
• Examine the EXPLAIN plan to understand the execution strategy being used for the most resource-
intensive queries. See Using the EXPLAIN Plan for Performance Tuning on page 631 for details.
• If Impala still chooses a suboptimal execution strategy even with statistics available, or if it is impractical
to keep the statistics up to date for huge or rapidly changing tables, add hints to the most resource-intensive
queries to select the right execution strategy. See Optimizer Hints on page 407 for details.
• If your queries experience substantial performance overhead due to spilling, enable the
DISABLE_UNSAFE_SPILLS query option. This option prevents queries whose memory usage is likely to be
exorbitant from spilling to disk. See DISABLE_UNSAFE_SPILLS Query Option (Impala 2.0 or higher only)
on page 344 for details. As you tune problematic queries using the preceding steps, fewer and fewer will be
cancelled by this option setting.
Testing performance implications of spilling to disk:
To artificially provoke spilling, to test this feature and understand the performance implications, use a test
environment with a memory limit of at least 2 GB. Issue the SET command with no arguments to check the current
setting for the MEM_LIMIT query option. Set the query option DISABLE_UNSAFE_SPILLS=true. This
option limits the spill-to-disk feature to prevent runaway disk usage from queries that are known in advance to be
suboptimal. Within impala-shell, run a query that you expect to be memory-intensive, based on the criteria
explained earlier. A self-join of a large table is a good candidate:
Issue the PROFILE command to get a detailed breakdown of the memory usage on each node during the query.
Set the MEM_LIMIT query option to a value that is smaller than the peak memory usage reported in the profile
output. Now try the memory-intensive query again.
Check if the query fails with a message like the following:
WARNINGS: Spilling has been disabled for plans that do not have stats and
are not hinted
to prevent potentially bad plans from using too many cluster resources.
Compute stats on
these tables, hint the plan or disable this behavior via query options to
enable spilling.
If so, the query could have consumed substantial temporary disk space, slowing down so much that it would not
complete in any reasonable time. Rather than rely on the spill-to-disk feature in this case, issue the COMPUTE
STATS statement for the table or tables in your sample query. Then run the query again, check the peak memory
usage again in the PROFILE output, and adjust the memory limit again if necessary to be lower than the peak
memory usage.
At this point, you have a query that is memory-intensive, but Impala can optimize it efficiently so that the memory
usage is not exorbitant. You have set an artificial constraint through the MEM_LIMIT option so that the query would
normally fail with an out-of-memory error. But the automatic spill-to-disk feature means that the query should
actually succeed, at the expense of some extra disk I/O to read and write temporary work data.
Try the query again, and confirm that it succeeds. Examine the PROFILE output again. This time, look for lines of
this form:
- SpilledPartitions: N
If you see any such lines with N greater than 0, that indicates the query would have failed in Impala releases
prior to 2.0, but now it succeeded because of the spill-to-disk feature. Examine the total time taken by the
AGGREGATION_NODE or other query fragments containing non-zero SpilledPartitions values. Compare the
times to similar fragments that did not spill, for example in the PROFILE output when the same query is run with a
higher memory limit. This gives you an idea of the performance penalty of the spill operation for a particular query
with a particular memory limit. If you make the memory limit just a little lower than the peak memory usage, the
query only needs to write a small amount of temporary data to disk. The lower you set the memory limit, the more
temporary data is written and the slower the query becomes.
Now repeat this procedure for actual queries used in your environment. Use the DISABLE_UNSAFE_SPILLS
setting to identify cases where queries used more memory than necessary due to lack of statistics on the relevant
tables and columns, and issue COMPUTE STATS where necessary.
When to use DISABLE_UNSAFE_SPILLS:
You might wonder, why not leave DISABLE_UNSAFE_SPILLS turned on all the time. Whether and how frequently
to use this option depends on your system environment and workload.
DISABLE_UNSAFE_SPILLS is suitable for an environment with ad hoc queries whose performance characteristics
and memory usage are not known in advance. It prevents “worst-case scenario” queries that use large amounts of
memory unnecessarily. Thus, you might turn this option on within a session while developing new SQL code, even
though it is turned off for existing applications.
Organizations where table and column statistics are generally up-to-date might leave this option turned on all the
time, again to avoid worst-case scenarios for untested queries or if a problem in the ETL pipeline results in a table
with no statistics. Turning on DISABLE_UNSAFE_SPILLS lets you “fail fast” in this case and immediately gather
statistics or tune the problematic queries.
Some organizations might leave this option turned off. For example, you might have tables large enough that the
COMPUTE STATS takes substantial time to run, making it impractical to re-run after loading new data. If you
have examined the EXPLAIN plans of your queries and know that they are operating efficiently, you might leave
| Scalability Considerations for Impala | 639
DISABLE_UNSAFE_SPILLS turned off. In that case, you know that any queries that spill will not go overboard
with their memory consumption.
While these authentication requests are being processed, any submitted Impala queries will fail. During this period,
the KDC and DNS may be slow to respond to requests from components other than Impala, so other secure services
might be affected temporarily.
In Impala 2.12 or earlier, to reduce the frequency of the kinit renewal that initiates a new set of authentication
requests, increase the kerberos_reinit_interval configuration setting for the impalad daemons.
Currently, the default is 60 minutes. Consider using a higher value such as 360 (6 hours).
The kerberos_reinit_interval configuration setting is removed in Impala 3.0, and the above step is no
longer needed.
Deployment Limits
• Number of Impalad Executors
• 80 nodes in Impala 2.11 and lower
• 150 nodes in Impala 2.12 and higher
• Number of Impalad Coordinators: 1 coordinator for at most every 50 executors
• The number of Impala clusters per deployment
• 1 Impala cluster in Impala 3.1 and lower
• Multiple clusters in Impala 3.2 and higher is generally safe.
| Scalability Considerations for Impala | 642
Security Limits
• Number of roles: 10,000 for Sentry
table partitions and data files, which requires coordinators to be configured with a large JVM heap. Executor-
only Impala daemons should be configured with the default JVM heaps, which leaves more memory available to
process joins, aggregations, and other operations performed by query executors.
• Having a large number of hosts act as coordinators can cause unnecessary network overhead, or even timeout
errors, as each of those hosts communicates with the Statestored daemon for metadata updates.
• The "soft limits" imposed by the admission control feature are more likely to be exceeded when there are a large
number of heavily loaded hosts acting as coordinators. Check IMPALA-3649 and IMPALA-6437 to see the
status of the enhancements to mitigate this issue.
The following factors can further exacerbate the above issues:
• High number of concurrent query fragments due to query concurrency and/or query complexity
• Large metadata topic size related to the number of partitions/files/blocks
• High number of coordinator nodes
• High number of coordinators used in the same resource pool
If such scalability bottlenecks occur, in Impala 2.9 and higher, you can assign one dedicated role to each Impala
daemon host, either as a coordinator or as an executor, to address the issues.
• All explicit or load-balanced client connections must go to the coordinator hosts. These hosts perform the network
communication to keep metadata up-to-date and route query results to the appropriate clients. The dedicated
coordinator hosts do not participate in I/O-intensive operations such as scans, and CPU-intensive operations such
as aggregations.
• The executor hosts perform the intensive I/O, CPU, and memory operations that make up the bulk of the work for
each query. The executors do communicate with the Statestored daemon for membership status, but the dedicated
executor hosts do not process the final result sets for queries.
Using dedicated coordinators offers the following benefits:
• Reduces memory usage by limiting the number of Impala nodes that need to cache metadata.
• Provides better concurrency by avoiding coordinator bottleneck.
• Eliminates query over-admission.
• Reduces resource, especially network, utilization on the Statestored daemon by limiting metadata broadcast to a
subset of nodes.
• Improves reliability and performance for highly concurrent workloads by reducing workload stress on
coordinators. Dedicated coordinators require 50% or fewer connections and threads.
• Reduces the number of explicit metadata refreshes required.
• Improves diagnosability if a bottleneck or other performance issue arises on a specific host, you can narrow down
the cause more easily because each host is dedicated to specific operations within the overall Impala workload.
In this configuration with dedicated coordinators / executors, you cannot connect to the dedicated executor hosts
through clients such as impala-shell or business intelligence tools as only the coordinator nodes support client
connections.
Advanced Tuning
Use the following guidelines to further tune the throughput and stability.
1. The concurrency of DML statements does not typically depend on the number of coordinators or size of the
cluster. Queries that return large result sets (10,000+ rows) consume more CPU and memory resources on the
coordinator. Add one or two coordinators if the workload has many such queries.
2. DDL queries, excluding COMPUTE STATS and CREATE TABLE AS SELECT, are executed only on
coordinators. If your workload contains many DDL queries running concurrently, you could add one coordinator.
3. The CPU contention on coordinators can slow down query executions when concurrency is high, especially for
very short queries (<10s). Add more coordinators to avoid CPU contention.
4. On a large cluster with 50+ nodes, the number of network connections from a coordinator to executors can grow
quickly as query complexity increases. The growth is much greater on coordinators than executors. Add a few
more coordinators if workloads are complex, i.e. (an average number of fragments * number of Impalad) > 500,
but with the low memory/CPU usage to share the load. Watch IMPALA-4603 and IMPALA-7213 to track the
progress on fixing this issue.
5. When using multiple coordinators for DML statements, divide queries to different groups (number of groups =
number of coordinators). Configure a separate dynamic resource pool for each group and direct each group of
query requests to a specific coordinator. This is to avoid query over admission.
6. The front-end connection requirement is not a factor in determining the number of dedicated coordinators.
Consider setting up a connection pool at the client side instead of adding coordinators. For a short-term solution,
you could increase the value of fe_service_threads on coordinators to allow more client connections.
7. In general, you should have a very small number of coordinators so storage capacity reduction is not a concern.
On a very small cluster (less than 10 nodes), deploy a dedicated coordinator on a DataNode to avoid storage
capacity reduction.
Query concurrency:
SELECT
total_impala_num_queries_registered_across_impa
WHERE entityName =
"IMPALA-1" AND category =
SERVICE
If usage of any of the above resources exceeds the safe range, add one more coordinator.
• ‑‑is_coordinator=false for each host that does not act as a coordinator for Impala queries. These hosts act
exclusively as executors. The number of hosts with this setting typically increases as the cluster grows larger and
handles more table partitions, data files, and concurrent queries. As the overhead for query coordination increases,
it becomes more important to centralize that work on dedicated hosts.
Metadata Management
This topic describes various knobs you can use to control how Impala manages its metadata in order to improve
performance and scalability.
On-demand Metadata
In previous versions of Impala, every coordinator kept a replica of all the cache in catalogd, consuming large
memory on each coordinator with no option to evict. Metadata always propagated through the statestored and
suffers from head-of-line blocking, for example, one user loading a big table blocking another user loading a small
table.
With this new feature, the coordinators pull metadata as needed from catalogd and cache it locally. The cached
metadata gets evicted automatically under memory pressure.
The granularity of on-demand metadata fetches is now at the partition level between the coordinator and catalogd.
Common use cases like add/drop partitions do not trigger unnecessary serialization/deserialization of large metadata.
This feature is disabled by default.
The feature can be used in either of the following modes.
Metadata on-demand mode In this mode, all coordinators use the metadata on-
demand.
Set the following on catalogd:
--catalog_topic_mode=minimal
--use_local_catalog=true
Mixed mode In this mode, only some coordinators are enabled to use
the metadata on-demand.
We recommend that you use the mixed mode only for
testing local catalog’s impact on heap usage.
Set the following on catalogd:
--catalog_topic_mode=mixed
--use_local_catalog=true
Limitation:
Global INVALIDATES are not supported when this feature is enabled. If your workload requires global
INVALIDATES, do not use this feature.
| Scalability Considerations for Impala | 647
Time-based cache invalidation Catalogd invalidates tables that are not recently used
in the specified time period (in seconds).
The ‑‑invalidate_tables_timeout_s flag needs to be
applied to both impalad and catalogd.
Memory-based cache invalidation When the memory pressure reaches 60% of JVM heap
size after a Java garbage collection in catalogd,
Impala invalidates 10% of the least recently used tables.
The ‑‑invalidate_tables_on_memory_pressure flag
needs to be applied to both impalad and catalogd.
Automatic invalidation of metadata provides more stability with lower chances of running out of memory, but the
feature could potentially cause performance issues and may require tuning.
It is recommended that you use the LOAD DATA command to do the data load in such cases, so that event
processor can act on the events generated by the LOAD command.
• The Spark API that saves data to a specified location does not generate events in HMS, thus is not supported. For
example:
This feature is turned off by default with the ‑‑hms_event_polling_interval_s flag set to 0.
<property>
<name>hive.metastore.transactional.event.listeners</name>
<value>org.apache.hive.hcatalog.listener.DbNotificationListener</
value>
<name>hive.metastore.dml.events</name>
<value>true</true>
</property>
2. Save hive-site.xml.
3. Set the hive.metastore.dml.events configuration key to true in HiveServer2 service's hive-
site.xml. This configuration key needs to be set to true in both Hive services, HiveServer2 and Hive
Metastore.
4. If applicable, set the hive.metastore.dml.events configuration key to true in hive-site.xml used
by the Spark applications (typically, /etc/hive/conf/hive-site.xml) so that the INSERT events are
generated when the Spark application inserts data into existing tables and partitions.
5. Restart the HiveServer2, Hive Metastore, and Spark (if applicable) services.
When both table and database level properties are set, the table level property takes precedence. If the table level
property is not set, then the database level property is used to evaluate if the event needs to be processed or not.
If the property is changed from true (meaning events are skipped) to false (meaning events are not skipped), you
need to issue a manual INVALIDATE METADATA command to reset event processor because it doesn't know how
many events have been skipped in the past and cannot know if the object in the event is the latest. In such a case, the
status of the event processor changes to NEEDS_INVALIDATE.
Name Description
events-processor.avg-events- Average duration to fetch a batch of events and process it.
fetch-duration
events-processor.avg-events- Average time taken to process a batch of events received from the Metastore.
process-duration
events-processor.events- Total number of the Metastore events received.
received
events-processor.events- Exponentially weighted moving average (EWMA) of number of events
received-15min-rate received in last 15 min.
This rate of events can be used to determine if there are spikes in event
processor activity during certain hours of the day.
Name Description
events-processor.events- Total number of the Metastore events skipped.
skipped
Events can be skipped based on certain flags are table and database level. You
can use this metric to make decisions, such as:
• If most of the events are being skipped, see if you might just turn off the
event processing.
• If most of the events are not skipped, see if you need to add flags on certain
databases.
events-processor.status Metastore event processor status to see if there are events being received or
not. Possible states are:
• PAUSED
The event processor is paused because catalog is being reset concurrently.
• ACTIVE
The event processor is scheduled at a given frequency.
• ERROR
• The event processor is in error state and event processing has stopped.
• NEEDS_INVALIDATE
The event processor could not resolve certain events and needs a manual
INVALIDATE command to reset the state.
• STOPPED
The event processing has been shutdown. No events will be processed.
• DISABLED
The event processor is not configured to run.
Resource Management
Impala includes the features that balance and maximize resources in your Apache Hadoop cluster. This topic
describes how you can improve efficiency of your a Apache Hadoop cluster using the admission control feature. See
the following topics for an overview and the configuration steps for admission control.
Queries, DML statements, and some DDL statements, including CREATE TABLE AS SELECT and COMPUTE
STATS are affected by admission control.
On a busy cluster, you might find there is an optimal number of Impala queries that run concurrently. For example,
when the I/O capacity is fully utilized by I/O-intensive queries, you might not find any throughput benefit in running
more concurrent queries. By allowing some queries to run at full speed while others wait, rather than having all
queries contend for resources and run slowly, admission control can result in higher overall throughput.
For another example, consider a memory-bound workload such as many large joins or aggregation queries. Each such
query could briefly use many gigabytes of memory to process intermediate results. Because Impala by default cancels
queries that exceed the specified memory limit, running multiple large-scale queries at once might require re-running
some queries that are cancelled. In this case, admission control improves the reliability and stability of the overall
workload by only allowing as many concurrent queries as the overall memory of the cluster can accommodate.
Max Running Queries Multiple This floating point number is multiplied by the current
total number of executors at runtime to give the
maximum number of concurrently running queries
allowed in the pool. The effect of this setting scales with
the number of executors in the resource pool.
This calculation is rounded up to the nearest integer, so
the result will always be at least one.
| Resource Management | 652
Max Queued Queries Maximum number of queries that can be queued in this
pool. The default value is 200 for Impala 2.1 or higher
and 50 for previous versions of Impala. (optional)
If Max Queued Queries Multiple is set, the Max
Queued Queries setting is ignored.
Max Queued Queries Multiple This floating point number is multiplied by the current
total number of executors at runtime to give the
maximum number of queries that can be queued in the
pool. The effect of this setting scales with the number of
executors in the resource pool.
This calculation is rounded up to the nearest integer, so
the result will always be at least one.
If set to zero or a negative number, the setting is ignored.
You can combine these settings with the memory-based approach described in Memory Limits and Admission
Control on page 653. If either the maximum number of or the expected memory usage of the concurrent queries is
exceeded, subsequent queries are queued until the concurrent workload falls below the threshold again.
| Resource Management | 653
Max Memory Multiple This number of bytes is multiplied by the current total
number of executors at runtime to give the maximum
memory available across the cluster for the pool. The
effect of this setting scales with the number of executors
in the resource pool.
If set to zero or a negative number, the setting is ignored.
| Resource Management | 654
Minimum Query Memory Limit and Maximum These two options determine the minimum and
Query Memory Limit maximum per-host memory limit that will be chosen by
Impala Admission control for queries in this resource
pool. If set, Impala Admission Control will choose a
memory limit between the minimum and maximum
values based on the per-host memory estimate for the
query. The memory limit chosen determines the amount
of memory that Impala Admission control will set aside
for this query on each host that the query is running on.
The aggregate memory across all of the hosts that the
query is running on is counted against the pool’s Max
Memory.
Minimum Query Memory Limit must be less than or
equal to Maximum Query Memory Limit and Max
Memory.
A user can override Impala’s choice of memory limit
by setting the MEM_LIMIT query option. If the Clamp
MEM_LIMIT Query Option setting is set to TRUE and
the user sets MEM_LIMIT to a value that is outside of the
range specified by these two options, then the effective
memory limit will be either the minimum or maximum,
depending on whether MEM_LIMIT is lower than or
higher the range.
For example, assume a resource pool with the following
parameters set:
• Minimum Query Memory Limit = 2GB
• Maximum Query Memory Limit = 10GB
If a user tries to submit a query with the MEM_LIMIT
query option set to 14 GB, the following would happen:
• If Clamp MEM_LIMIT Query Option = true,
admission controller would override MEM_LIMIT
with 10 GB and attempt admission using that value.
• If Clamp MEM_LIMIT Query Option = false, the
admission controller will retain the MEM_LIMIT of
14 GB set by the user and will attempt admission
using the value.
Default Query Memory Limit The default memory limit applied to queries executing in
this pool when no explicit MEM_LIMIT query option is
set. The memory limit chosen determines the amount of
memory that Impala Admission control will set aside for
this query on each host that the query is running on. The
aggregate memory across all of the hosts that the query
is running on is counted against the pool’s Max Memory.
This option is deprecated in Impala 3.1 and higher and
is replaced by Maximum Query Memory Limit and
Minimum Query Memory Limit. Do not set this if either
Maximum Query Memory Limit or Minimum Query
Memory Limit is set.
Clamp MEM_LIMIT Query Option If this field is not selected, the MEM_LIMIT query option
will not be bounded by the Maximum Query Memory
Limit and the Minimum Query Memory Limit values
| Resource Management | 655
The Impala admission control feature uses the same configuration mechanism as the YARN resource manager to map
users to pools and authenticate them.
Although the Impala admission control feature uses a fair-scheduler.xml configuration file behind the scenes,
this file does not depend on which scheduler is used for YARN. You still use this file even when YARN is using the
capacity scheduler.
How Admission Control works with Impala Clients (JDBC, ODBC, HiveServer2)
Most aspects of admission control work transparently with client interfaces such as JDBC and ODBC:
• If a SQL statement is put into a queue rather than running immediately, the API call blocks until the statement is
dequeued and begins execution. At that point, the client program can request to fetch results, which might also
block until results become available.
• If a SQL statement is cancelled because it has been queued for too long or because it exceeded the memory limit
during execution, the error is returned to the client program with a descriptive error message.
In Impala 2.0 and higher, you can submit a SQL SET statement from the client application to change the
REQUEST_POOL query option. This option lets you submit queries to different resource pools, as described in
REQUEST_POOL Query Option on page 373.
At any time, the set of queued queries could include queries submitted through multiple different Impala daemon
hosts. All the queries submitted through a particular host will be executed in order, so a CREATE TABLE followed
by an INSERT on the same table would succeed. Queries submitted through different hosts are not guaranteed to be
executed in the order they were received. Therefore, if you are using load-balancing or other round-robin scheduling
where different statements are submitted through different hosts, set up all table structures ahead of time so that the
statements controlled by the queuing system are primarily queries, where order is not significant. Or, if a sequence of
statements needs to happen in strict order (such as an INSERT followed by a SELECT), submit all those statements
through a single session, while connected to the same Impala daemon host.
Admission control has the following limitations or special behavior when used with JDBC or ODBC applications:
• The other resource-related query options, RESERVATION_REQUEST_TIMEOUT and V_CPU_CORES, are no
longer used. Those query options only applied to using Impala with Llama, which is no longer supported.
ensure that the query plans are efficient and the memory estimates are accurate. Understanding the nature of your
workload, and which queries are the most resource-intensive, helps you to plan how to divide the queries into
different pools and decide what limits to define for each pool.
For large tables, especially those involved in join queries, keep their statistics up to date after loading substantial
amounts of new data or adding new partitions. Use the COMPUTE STATS statement for unpartitioned tables, and
COMPUTE INCREMENTAL STATS for partitioned tables.
When you use dynamic resource pools with a Max Memory setting enabled, you typically override the memory
estimates that Impala makes based on the statistics from the COMPUTE STATS statement. You either set the
MEM_LIMIT query option within a particular session to set an upper memory limit for queries within that session, or
a default MEM_LIMIT setting for all queries processed by the impalad instance, or a default MEM_LIMIT setting
for all queries assigned to a particular dynamic resource pool. By designating a consistent memory limit for a set of
similar queries that use the same resource pool, you avoid unnecessary query queuing or out-of-memory conditions
that can arise during high-concurrency workloads when memory estimates for some queries are inaccurate.
Follow other steps from Tuning Impala for Performance on page 590 to tune your queries.
The configuration options for admission control range from the simple (a single resource pool with a single set of
options) to the complex (multiple resource pools with different options, each pool handling queries for a different set
of users and groups).
llama.am.throttling.maximum.placed.reservations.queue_name
llama.am.throttling.maximum.queued.reservations.queue_name
impala.admission-control.pool-default-query-options.queue_name
impala.admission-control.pool-queue-timeout-ms.queue_name
<allocations>
<queue name="root">
<aclSubmitApps> </aclSubmitApps>
| Resource Management | 659
<queue name="default">
<maxResources>50000 mb, 0 vcores</maxResources>
<aclSubmitApps>*</aclSubmitApps>
</queue>
<queue name="development">
<maxResources>200000 mb, 0 vcores</maxResources>
<aclSubmitApps>user1,user2 dev,ops,admin</aclSubmitApps>
</queue>
<queue name="production">
<maxResources>1000000 mb, 0 vcores</maxResources>
<aclSubmitApps> ops,admin</aclSubmitApps>
</queue>
</queue>
<queuePlacementPolicy>
<rule name="specified" create="false"/>
<rule name="default" />
</queuePlacementPolicy>
</allocations>
llama-site.xml:
See Attaching an External Partitioned Table to an HDFS Directory Structure on page 46 for an example that
illustrates the syntax for creating partitioned tables, the underlying directory structure in HDFS, and how to attach a
partitioned Impala external table to data files stored elsewhere in HDFS.
Parquet is a popular format for partitioned Impala tables because it is well suited to handle huge data volumes. See
Query Performance for Impala Parquet Tables on page 683 for performance considerations for partitioned Parquet
tables.
See NULL on page 169 for details about how NULL values are represented in partitioned tables.
See Using Impala with the Amazon S3 Filesystem on page 732 for details about setting up tables where some or all
partitions reside on the Amazon Simple Storage Service (S3).
By default, if an INSERT statement creates any new subdirectories underneath a partitioned table, those
subdirectories are assigned default HDFS permissions for the impala user. To make each subdirectory have the
same permissions as its parent directory in HDFS, specify the ‑‑insert_inherit_permissions startup option for the
impalad daemon.
• Although the syntax of the SELECT statement is the same whether or not the table is partitioned, the way queries
interact with partitioned tables can have a dramatic impact on performance and scalability. The mechanism that
lets queries skip certain partitions during a query is known as partition pruning; see Partition Pruning for Queries
on page 664 for details.
• In Impala 1.4 and later, there is a SHOW PARTITIONS statement that displays information about each partition
in a table. See SHOW Statement on page 381 for details.
When you specify some partition key columns in an INSERT statement, but leave out the values, Impala determines
which partition to insert. This technique is called dynamic partitioning:
The more key columns you specify in the PARTITION clause, the fewer columns you need in the SELECT list. The
trailing columns in the SELECT list are substituted in order for the partition key columns with no specified value.
For a report of the volume of data that was actually read and processed at each stage of the query, check the output of
the SUMMARY command immediately after running the query. For a more detailed analysis, look at the output of the
PROFILE command; it includes this same summary report near the start of the profile output.
indicating when the data was collected, which happens in 10-year intervals. Even though the query does not compare
the partition key column (YEAR) to a constant value, Impala can deduce that only the partition YEAR=2010 is
required, and again only reads 1 out of 3 partitions.
If a view applies to a partitioned table, any partition pruning considers the clauses on both the original query and any
additional WHERE predicates in the query that refers to the view. Prior to Impala 1.4, only the WHERE clauses on the
original query from the CREATE VIEW statement were used for partition pruning.
In queries involving both analytic functions and partitioned tables, partition pruning only occurs for columns named
in the PARTITION BY clause of the analytic function call. For example, if an analytic function query has a clause
such as WHERE year=2016, the way to make the query prune all other YEAR partitions is to include PARTITION
BY year in the analytic function call; for example, OVER (PARTITION BY year,other_columns
other_analytic_clauses).
For example, if partition key columns are compared to literal values in a WHERE clause, Impala can perform static
partition pruning during the planning phase to only read the relevant partitions:
-- The query only needs to read 3 partitions whose key values are known
ahead of time.
-- That's static partition pruning.
SELECT COUNT(*) FROM sales_table WHERE year IN (2005, 2010, 2015);
Dynamic partition pruning involves using information only available at run time, such as the result of a subquery. The
following example shows a simple dynamic partition pruning.
SELECT s FROM yy WHERE year IN (SELECT year FROM yy2); -- Returns 3 rows
from yy
PROFILE;
In the above example, Impala evaluates the subquery, sends the subquery results to all Impala nodes participating
in the query, and then each impalad daemon uses the dynamic partition pruning optimization to read only the
partitions with the relevant key values.
The output query plan from the EXPLAIN statement shows that runtime filters are enabled. The plan also shows that
it expects to read all 5 partitions of the yy table, indicating that static partition pruning will not happen.
The Filter summary in the PROFILE output shows that the scan node filtered out based on a runtime filter of dynamic
partition pruning.
- Files processed: 3
- Files rejected: 1 (1)
- Files total: 3 (3)
Dynamic partition pruning is especially effective for queries involving joins of several large partitioned tables.
Evaluating the ON clauses of the join predicates might normally require reading data from all partitions of certain
tables. If the WHERE clauses of the query refer to the partition key columns, Impala can now often skip reading many
of the partitions while evaluating the ON clauses. The dynamic partition pruning optimization reduces the amount of I/
O and the amount of intermediate data stored and transmitted across the network during the query.
When the spill-to-disk feature is activated for a join node within a query, Impala does not produce any runtime filters
for that join operation on that host. Other join nodes within the query are not affected.
Dynamic partition pruning is part of the runtime filtering feature, which applies to other kinds of queries in addition
to queries against partitioned tables. See Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page
617 for full details about this feature.
For example, here is how you might switch from text to Parquet data as you receive data for different years:
At this point, the HDFS directory for year=2012 contains a text-format data file, while the HDFS directory for
year=2013 contains a Parquet data file. As always, when loading non-trivial data, you would use INSERT ...
SELECT or LOAD DATA to import data in large batches, rather than INSERT ... VALUES which produces small
files that are inefficient for real-world queries.
For other file types that Impala cannot create natively, you can switch into Hive and issue the ALTER TABLE ...
SET FILEFORMAT statements and INSERT or LOAD DATA statements there. After switching back to Impala, issue
a REFRESH table_name statement so that Impala recognizes any partitions or new data added through Hive.
Managing Partitions
You can add, drop, set the expected file format, or set the HDFS location of the data files for individual partitions
within an Impala table. See ALTER TABLE Statement on page 207 for syntax details, and Setting Different File
Formats for Partitions on page 667 for tips on managing tables containing partitions with different file formats.
Note: If you are creating a partition for the first time and specifying its location, for maximum efficiency, use a
single ALTER TABLE statement including both the ADD PARTITION and LOCATION clauses, rather than separate
statements with ADD PARTITION and SET LOCATION clauses.
What happens to the data files when a partition is dropped depends on whether the partitioned table is designated as
internal or external. For an internal (managed) table, the data files are deleted. For example, if data in the partitioned
table is a copy of raw data files stored elsewhere, you might save disk space by dropping older partitions that are no
longer required for reporting, knowing that the original data is still available if needed later. For an external table, the
data files are left alone. For example, dropping a partition without deleting the associated files lets Impala consider
a smaller set of partitions, improving query efficiency and reducing overhead for DDL operations on the table; if the
data is needed again later, you can add the partition again. See Overview of Impala Tables on page 198 for details
and examples.
Bzip2 Supported for text, RC, and Sequence files in Impala 2.0
and higher.
giving performance much thought.) Either way, look for opportunities to use more efficient file formats for the tables
used in your most performance-critical queries.
For frequently queried data, you might load the original text data files into one Impala table, then use an INSERT
statement to transfer the data to another table that uses the Parquet file format; the data is converted automatically as it
is stored in the destination table.
For more compact data, consider using LZO compression for the text files. LZO is the only compression codec that
Impala supports for text data, because the “splittable” nature of LZO data files lets different nodes work on different
parts of the same file in parallel. See Using LZO-Compressed Text Files on page 677 for details.
In Impala 2.0 and later, you can also use text data compressed in the gzip, bzip2, or Snappy formats. Because these
compressed formats are not “splittable” in the way that LZO is, there is less opportunity for Impala to parallelize
queries on them. Therefore, use these types of compressed data only for convenience if that is the format in which you
receive the data. Prefer to use LZO compression for text data if you have the choice, or convert the data to Parquet
using an INSERT ... SELECT statement to copy the original data into a Parquet table.
Note:
Impala supports bzip files created by the bzip2 command, but not bzip files with multiple streams created by the
pbzip2 command. Impala decodes only the data from the first part of such files, leading to incomplete results.
The maximum size that Impala can accommodate for an individual bzip file is 1 GB (after uncompression).
In Impala 2.6 and higher, Impala queries are optimized for files stored in Amazon S3. For Impala tables that use the
file formats Parquet, ORC, RCFile, SequenceFile, Avro, and uncompressed text, the setting fs.s3a.block.size
in the core-site.xml configuration file determines how Impala divides the I/O work of reading the data files.
This configuration setting is specified in bytes. By default, this value is 33554432 (32 MB), meaning that Impala
parallelizes S3 read operations on the files as if they were made up of 32 MB blocks. For example, if your S3 queries
primarily access Parquet files written by MapReduce or Hive, increase fs.s3a.block.size to 134217728 (128
MB) to match the row group size of those files. If most S3 queries involve Parquet files written by Impala, increase
fs.s3a.block.size to 268435456 (256 MB) to match the row group size produced by Impala.
The data files created by any INSERT statements will use the Ctrl-A character (hex 01) as a separator between each
column value.
A common use case is to import existing text files into an Impala table. The syntax is more verbose; the significant
part is the FIELDS TERMINATED BY clause, which must be preceded by the ROW FORMAT DELIMITED clause.
The statement can end with a STORED AS TEXTFILE clause, but that clause is optional because text format tables
are the default. For example:
You can create tables with specific separator characters to import text files in familiar formats such as CSV, TSV,
or pipe-separated. You can also use these tables to produce output data files, by copying data into them through the
INSERT ... SELECT syntax and then extracting the data files from the Impala data directory.
In Impala 1.3.1 and higher, you can specify a delimiter character '\0' to use the ASCII 0 (nul) character for text
tables:
Note:
Do not surround string values with quotation marks in text data files that you construct. If you need to include the
separator character inside a field value, for example to put a string value with a comma inside a CSV-format data
file, specify an escape character on the CREATE TABLE statement with the ESCAPED BY clause, and insert that
character immediately before any separator characters that need escaping.
Issue a DESCRIBE FORMATTED table_name statement to see the details of how each table is represented
internally in Impala.
Complex type considerations: Although you can create tables in this file format using the complex types (ARRAY,
STRUCT, and MAP) available in Impala 2.3 and higher, currently, Impala can query these types only in Parquet tables.
The one exception to the preceding rule is COUNT(*) queries on RCFile tables that include complex types. Such
queries are allowed in Impala 2.6 and higher.
• Impala recognizes the literal string \N to represent NULL. When using Sqoop, specify the options --null-non-
string and --null-string to ensure all NULL values are represented correctly in the Sqoop output files. \N
needs to be escaped as in the below example:
-- Back in the shell, load data into the table with commands such as:
-- cat >data.csv
-- Name,Age
-- Alice,25
-- Bob,19
-- hdfs dfs -put data.csv /user/hive/warehouse/header_line
refresh header_line;
-- Once the table property is set, queries skip the specified number of
lines
-- at the beginning of each text data file. Therefore, all the files in
the table
-- should follow the same convention for header lines.
select * from header_line limit 10;
+------------+-----+
| first_name | age |
+------------+-----+
| Alice | 25 |
| Bob | 19 |
+------------+-----+
This can be a useful technique to see how Impala represents special values within a text-format data file. Use the
DESCRIBE FORMATTED statement to see the HDFS directory where the data files are stored, then use Linux
commands such as hdfs dfs -ls hdfs_directory and hdfs dfs -cat hdfs_file to display the
contents of an Impala-created text file.
To create a few rows in a text table for test purposes, you can use the INSERT ... VALUES syntax:
Note: Because Impala and the HDFS infrastructure are optimized for multi-megabyte files, avoid the INSERT ...
VALUES notation when you are inserting many rows. Each INSERT ... VALUES statement produces a new tiny
file, leading to fragmentation and reduced performance. When creating any substantial volume of new data, use one
of the bulk loading techniques such as LOAD DATA or INSERT ... SELECT. Or, use an HBase table for single-
row INSERT operations, because HBase tables are not subject to the same fragmentation issues as tables stored on
HDFS.
When you create a text file for use with an Impala text table, specify \N to represent a NULL value. For the
differences between NULL and empty strings, see NULL on page 169.
If a text file has fewer fields than the columns in the corresponding Impala table, all the corresponding columns are
set to NULL when the data in that file is read by an Impala query.
If a text file has more fields than the columns in the corresponding Impala table, the extra fields are ignored when the
data in that file is read by an Impala query.
You can also use manual HDFS operations such as hdfs dfs -put or hdfs dfs -cp to put data files in the
data directory for an Impala table. When you copy or move new data files into the HDFS directory for the Impala
table, issue a REFRESH table_name statement in impala-shell before issuing the next query against that
table, to make Impala recognize the newly added files.
1. Prepare your systems to work with LZO by downloading and installing the appropriate libraries:
Download and install the appropriate file to each machine on which you intend to use LZO with Impala.
2. Configure Impala to use LZO:
Use one of the following sets of commands to refresh your package management system's repository information,
install the base LZO support for Hadoop, and install the LZO support for Impala.
Note:
The name of the Hadoop LZO package changed in the distant past. Currently, the package name is hadoop-lzo.
For RHEL/CentOS systems:
Note:
The level of the impala-lzo package is closely tied to the version of Impala you use. Any time you upgrade
Impala, re-do the installation command for impala-lzo on each applicable machine to make sure you have the
appropriate version of that package.
3. For core-site.xml on the client and server (that is, in the configuration directories for both Impala and
Hadoop), append com.hadoop.compression.lzo.LzopCodec to the comma-separated list of codecs. For
example:
<property>
<name>io.compression.codecs</name>
<value>org.apache.hadoop.io.compress.DefaultCodec,org.apache.hadoop.io.compress.GzipC
org.apache.hadoop.io.compress.BZip2Codec,org.apache.hadoop.io.compress.DeflateCodec,
org.apache.hadoop.io.compress.SnappyCodec,com.hadoop.compression.lzo.LzopCodec</
value>
</property>
Note:
If this is the first time you have edited the Hadoop core-site.xml file, note that the /etc/hadoop/conf
directory is typically a symbolic link, so the canonical core-site.xml might reside in a different directory:
$ ls -l /etc/hadoop
total 8
lrwxrwxrwx. 1 root root 29 Feb 26 2013 conf -> /etc/alternatives/
hadoop-conf
lrwxrwxrwx. 1 root root 10 Feb 26 2013 conf.dist -> conf.empty
drwxr-xr-x. 2 root root 4096 Feb 26 2013 conf.empty
drwxr-xr-x. 2 root root 4096 Oct 28 15:46 conf.pseudo
| How Impala Works with Hadoop File Formats | 679
STORED AS
INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
Once you have created LZO-compressed text tables, you can convert data stored in other tables (regardless of file
format) by using the INSERT ... SELECT statement in Hive.
Files in an LZO-compressed table must use the .lzo extension. Examine the files in the HDFS data directory after
doing the INSERT in Hive, to make sure the files have the right extension. If the required settings are not in place,
you end up with regular uncompressed files, and Impala cannot access the table because it finds data files with the
wrong (uncompressed) format.
After loading data into an LZO-compressed text table, index the files so that they can be split. You index the files
by running a Java class, com.hadoop.compression.lzo.DistributedLzoIndexer, through the Linux
command line. This Java class is included in the hadoop-lzo package.
Run the indexer using a command like the following:
Note: If the path of the JAR file in the preceding example is not recognized, do a find command to locate
hadoop-lzo-*-gplextras.jar and use that path.
Indexed files have the same name as the file they index, with the .index extension. If the data files are not indexed,
Impala queries still work, but the queries read the data from remote DataNodes, which is very inefficient.
Once the LZO-compressed tables are created, and data is loaded and indexed, you can query them through Impala. As
always, the first time you start impala-shell after creating a table in Hive, issue an INVALIDATE METADATA
statement so that Impala recognizes the new table. (In Impala 1.2 and higher, you only have to run INVALIDATE
METADATA on one node, rather than on all the Impala nodes.)
Although it requires less I/O to read compressed text than the equivalent uncompressed text, files compressed by
these codecs are not “splittable” and therefore cannot take full advantage of the Impala parallel query capability.
As each bzip2- or Snappy-compressed text file is processed, the node doing the work reads the entire file into
memory and then decompresses it. Therefore, the node must have enough memory to hold both the compressed and
uncompressed data from the text file. The memory required to hold the uncompressed data is difficult to estimate in
advance, potentially causing problems on systems with low memory limits or with resource management enabled.
In Impala 2.1 and higher, this memory overhead is reduced for gzip-compressed text files. The gzipped data is
decompressed as it is read, rather than all at once.
To create a table to hold gzip, bzip2, or Snappy-compressed text, create a text table with no special compression
options. Specify the delimiter and escape character if required, using the ROW FORMAT clause.
Because Impala can query compressed text files but currently cannot write them, produce the compressed text files
outside Impala and use the LOAD DATA statement, manual HDFS commands to move them to the appropriate Impala
data directory. (Or, you can use CREATE EXTERNAL TABLE and point the LOCATION attribute at a directory
containing existing compressed text files.)
For Impala to recognize the compressed text files, they must have the appropriate file extension corresponding to the
compression codec, either .gz, .bz2, or .snappy. The extensions can be in uppercase or lowercase.
The following example shows how you can create a regular text table, put different kinds of compressed and
uncompressed files into it, and Impala automatically recognizes and decompresses each one based on their file
extensions:
Or, to clone the column names and data types of an existing table:
In Impala 1.4.0 and higher, you can derive column definitions from a raw Parquet data file, even without an existing
Impala table. For example, you can create an external table pointing to an HDFS directory, and base the column
definitions on one of the files in that directory:
Or, you can refer to an existing data file and create a new empty table with suitable column definitions. Then you can
use INSERT to create new data files or LOAD DATA to transfer existing data files into the new table.
The default properties of the newly created table are the same as for any other CREATE TABLE statement. For
example, the default file format is text; if you want the new table to use the Parquet file format, include the STORED
AS PARQUET file also.
In this example, the new table is partitioned by year, month, and day. These partition key columns are not part of the
data file, so you specify them in the CREATE TABLE statement:
See CREATE TABLE Statement on page 240 for more details about the CREATE TABLE LIKE PARQUET
syntax.
| How Impala Works with Hadoop File Formats | 682
Once you have created a table, to insert data into that table, use a command similar to the following, again with your
own table names:
If the Parquet table has a different number of columns or different column names than the other table, specify the
names of columns from the other table rather than * in the SELECT statement.
that uses the appropriate file format. Then, use an INSERT...SELECT statement to copy the data to the Parquet
table, converting to Parquet format as part of the process.
Loading data into Parquet tables is a memory-intensive operation, because the incoming data is buffered until it
reaches one data block in size, then that chunk of data is organized and compressed in memory before being written
out. The memory consumption can be larger when inserting data into partitioned Parquet tables, because a separate
data file is written for each combination of partition key column values, potentially requiring several large chunks to
be manipulated in memory at once.
When inserting into a partitioned Parquet table, Impala redistributes the data among the nodes to reduce memory
consumption. You might still need to temporarily increase the memory dedicated to Impala during the insert
operation, or break up the load operation into several INSERT statements, or both.
Note: All the preceding techniques assume that the data you are loading matches the structure of the destination
table, including column order, column names, and partition layout. To transform or reorganize the data, start by
loading the data into a Parquet table that matches the underlying structure of the data, then use one of the table-
copying techniques such as CREATE TABLE AS SELECT or INSERT ... SELECT to reorder or rename
columns, divide the data among multiple partitions, and so on. For example to take a single comprehensive Parquet
data file and load it into a partitioned table, you would use an INSERT ... SELECT statement with dynamic
partitioning to let Impala create separate data files with the appropriate partition values; for an example, see INSERT
Statement on page 288.
The query processes only 2 columns out of a large number of total columns. If the table is partitioned by the STATE
column, it is even more efficient because the query only has to read and decode 1 column from each data file, and it
can read only the data files in the partition directory for the state 'CA', skipping the data files for all the other states,
which will be physically located in other directories.
The following is a relatively inefficient query for a Parquet table:
Impala would have to read the entire contents of each large data file, and decompress the contents of each column
for each row group, negating the I/O optimizations of the column-oriented format. This query might still be faster
for a Parquet table than a table with some other file format, but it does not take advantage of the unique strengths of
Parquet data files.
Impala can optimize queries on Parquet tables, especially join queries, better when statistics are available for all
the tables. Issue the COMPUTE STATS statement for each table after substantial amounts of data are loaded into or
appended to it. See COMPUTE STATS Statement on page 223 for details.
The runtime filtering feature, available in Impala 2.5 and higher, works best with Parquet tables. The per-row filtering
aspect only applies to Parquet tables. See Runtime Filtering for Impala Queries (Impala 2.5 or higher only) on page
617 for details.
In Impala 2.6 and higher, Impala queries are optimized for files stored in Amazon S3. For Impala tables that use the
file formats Parquet, ORC, RCFile, SequenceFile, Avro, and uncompressed text, the setting fs.s3a.block.size
in the core-site.xml configuration file determines how Impala divides the I/O work of reading the data files.
This configuration setting is specified in bytes. By default, this value is 33554432 (32 MB), meaning that Impala
parallelizes S3 read operations on the files as if they were made up of 32 MB blocks. For example, if your S3 queries
primarily access Parquet files written by MapReduce or Hive, increase fs.s3a.block.size to 134217728 (128
| How Impala Works with Hadoop File Formats | 684
MB) to match the row group size of those files. If most S3 queries involve Parquet files written by Impala, increase
fs.s3a.block.size to 268435456 (256 MB) to match the row group size produced by Impala.
In Impala 2.9 and higher, Parquet files written by Impala include embedded metadata specifying the minimum
and maximum values for each column, within each row group and each data page within the row group. Impala-
written Parquet files typically contain a single row group; a row group can contain many data pages. Impala uses this
information (currently, only the metadata for each row group) when reading each Parquet data file during a query,
to quickly determine whether each row group within the file potentially includes any rows that match the conditions
in the WHERE clause. For example, if the column X within a particular Parquet file has a minimum value of 1 and a
maximum value of 100, then a query including the clause WHERE x > 200 can quickly determine that it is safe to
skip that particular file, instead of scanning all the associated column values. This optimization technique is especially
effective for tables that use the SORT BY clause for the columns most frequently checked in WHERE clauses, because
any INSERT operation on such tables produces Parquet data files with relatively narrow ranges of column values
within each file.
To disable Impala from writing the Parquet page index when creating Parquet files, set the
PARQUET_WRITE_PAGE_INDEX query option to FALSE.
for example after experimenting with other compression codecs, set the COMPRESSION_CODEC query option to
snappy before inserting the data:
Because Parquet data files are typically large, each directory will have a different number of data files and the row
groups will be arranged differently.
At the same time, the less agressive the compression, the faster the data can be decompressed. In this case using a
table with a billion rows, a query that evaluates all the values for a particular column runs faster with no compression
than with Snappy compression, and faster with Snappy compression than with Gzip compression. Query performance
depends on several other factors, so as always, run your own benchmarks with your own data to determine the ideal
tradeoff between data size, CPU efficiency, and speed of insert and query operations.
| name | string | |
| assertion | boolean | |
+-----------+---------+---------+
Returned 5 row(s) in 0.14s
[localhost:21000] > select avg(val) from parquet_snappy;
Query finished, fetching results ...
+-----------------+
| _c0 |
+-----------------+
| 250000.93577915 |
+-----------------+
Returned 1 row(s) in 4.29s
[localhost:21000] > select avg(val) from parquet_gzip;
Query finished, fetching results ...
+-----------------+
| _c0 |
+-----------------+
| 250000.93577915 |
+-----------------+
Returned 1 row(s) in 6.97s
[localhost:21000] > select avg(val) from parquet_none;
Query finished, fetching results ...
+-----------------+
| _c0 |
+-----------------+
| 250000.93577915 |
+-----------------+
Returned 1 row(s) in 3.67s
Then in the shell, we copy the relevant data files into the data directory for this new table. Rather than using hdfs
dfs -cp as with typical files, we use hadoop distcp -pb to ensure that the special block size of the Parquet
data files is preserved.
Back in the impala-shell interpreter, we use the REFRESH statement to alert the Impala server to the new data
files for this table, then we can run queries demonstrating that the data files represent 3 billion rows, and the values
for one of the numeric columns match what was in the original smaller tables:
If you are running a level of Impala that is older than 1.1.1, do the metadata update through Hive:
Impala 1.1.1 and higher can reuse Parquet data files created by Hive, without any action required.
Impala supports the scalar data types that you can encode in a Parquet data file, but not composite or nested types
such as maps or arrays. In Impala 2.2 and higher, Impala can query Parquet data files that include composite or nested
types, as long as the query only refers to columns with scalar types.
If you copy Parquet data files between nodes, or even between different directories on the same node, make sure
to preserve the block size by using the command hadoop distcp -pb. To verify that the block size was
preserved, issue the command hdfs fsck -blocks HDFS_path_of_impala_table_dir and check
that the average block size is at or near 256 MB (or whatever other size is defined by the PARQUET_FILE_SIZE
| How Impala Works with Hadoop File Formats | 688
query option).. (The hadoop distcp operation typically leaves some directories behind, with names matching
_distcp_logs_*, that you can delete from the destination directory afterward.) Issue the command hadoop
distcp for details about distcp command syntax.
Impala can query Parquet files that use the PLAIN, PLAIN_DICTIONARY, BIT_PACKED, and RLE encodings.
Currently, Impala does not support RLE_DICTIONARY encoding. When creating files outside of Impala
for use by Impala, make sure to use one of the supported encodings. In particular, for MapReduce jobs,
parquet.writer.version must not be defined (especially as PARQUET_2_0) for writing the configurations
of Parquet MR jobs. Use the default version (or format). The default format, 1.0, includes some enhancements that
are compatible with older versions. Data using the 2.0 format might not be consumable by Impala, due to use of the
RLE_DICTIONARY encoding.
To examine the internal structure and data of Parquet files, you can use the parquet-tools command. Make
sure this command is in your $PATH. (Typically, it is symlinked from /usr/bin; sometimes, depending on your
installation setup, you might need to locate it under an alternative bin directory.) The arguments to this command let
you perform operations such as:
• cat: Print a file's contents to standard out. In Impala 2.3 and higher, you can use the -j option to output JSON.
• head: Print the first few records of a file to standard output.
• schema: Print the Parquet schema for the file.
• meta: Print the file footer metadata, including key-value properties (like Avro schema), compression ratios,
encodings, compression used, and row group information.
• dump: Print all data and metadata.
Use parquet-tools -h to see usage information for all the arguments. Here are some examples showing
parquet-tools usage:
$ # Be careful doing this for a big file! Use parquet-tools head to be safe.
$ parquet-tools cat sample.parq
year = 1992
month = 1
day = 2
dayofweek = 4
dep_time = 748
crs_dep_time = 750
arr_time = 851
crs_arr_time = 846
carrier = US
flight_num = 53
actual_elapsed_time = 63
crs_elapsed_time = 56
arrdelay = 5
depdelay = -2
origin = CMH
dest = IND
distance = 182
cancelled = 0
diverted = 0
year = 1992
month = 1
day = 3
...
dayofweek = 4
dep_time = 748
crs_dep_time = 750
arr_time = 851
crs_arr_time = 846
carrier = US
flight_num = 53
actual_elapsed_time = 63
crs_elapsed_time = 56
arrdelay = 5
depdelay = -2
origin = CMH
dest = IND
distance = 182
cancelled = 0
diverted = 0
year = 1992
month = 1
day = 3
...
GZip, or no compression; the Parquet spec also allows LZO compression, but currently Impala does not support LZO-
compressed Parquet files.
RLE and dictionary encoding are compression techniques that Impala applies automatically to groups of Parquet data
values, in addition to any Snappy or GZip compression applied to the entire data files. These automatic optimizations
can save you time and planning that are normally needed for a traditional data warehouse. For example, dictionary
encoding reduces the need to create numeric IDs as abbreviations for longer string values.
Run-length encoding condenses sequences of repeated data values. For example, if many consecutive rows all contain
the same value for a country code, those repeating values can be represented by the value followed by a count of how
many times it appears consecutively.
Dictionary encoding takes the different values present in a column, and represents each one in compact 2-byte form
rather than the original value, which could be several bytes. (Additional compression is applied to the compacted
values, for extra space savings.) This type of encoding applies when the number of different values for a column
is less than 2**16 (16,384). It does not apply to columns of data type BOOLEAN, which are already very short.
TIMESTAMP columns sometimes have a unique value for each row, in which case they can quickly exceed the 2**16
limit on distinct values. The 2**16 limit on different values within a column is reset for each data file, so if several
different data files each contained 10,000 different city names, the city name column in each data file could still be
condensed using dictionary encoding.
Here are techniques to help you produce large data files in Parquet INSERT operations, and to compact existing too-
small data files:
• When inserting into a partitioned Parquet table, use statically partitioned INSERT statements where the partition
key values are specified as constant values. Ideally, use a separate INSERT statement for each partition.
• You might set the NUM_NODES option to 1 briefly, during INSERT or CREATE TABLE AS SELECT
statements. Normally, those statements produce one or more data files per data node. If the write operation
involves small amounts of data, a Parquet table, and/or a partitioned table, the default behavior could produce
many small files when intuitively you might expect only a single output file. SET NUM_NODES=1 turns off the
“distributed” aspect of the write operation, making it more likely to produce only one or a few data files.
• Be prepared to reduce the number of partition key columns from what you are used to with traditional analytic
database systems.
• Do not expect Impala-written Parquet files to fill up the entire Parquet block size. Impala estimates on the
conservative side when figuring out how much data to write to each Parquet file. Typically, the of uncompressed
data in memory is substantially reduced on disk by the compression and encoding techniques in the Parquet
| How Impala Works with Hadoop File Formats | 692
file format. The final data file size varies depending on the compressibility of the data. Therefore, it is not an
indication of a problem if 256 MB of text data is turned into 2 Parquet data files, each less than 256 MB.
• If you accidentally end up with a table with many small data files, consider using one or more of the preceding
techniques and copying all the data into a new Parquet table, either through CREATE TABLE AS SELECT or
INSERT ... SELECT statements.
To avoid rewriting queries to change table names, you can adopt a convention of always running important
queries against a view. Changing the view definition immediately switches any subsequent queries to use the new
underlying tables:
File 'schema_evolution.db/t2/45331705_data.0.parq'
has an incompatible Parquet schema for column 'schema_evolution.t2.c4'.
Column type: TIMESTAMP, Parquet schema: optional int32 c1 [i:0 d:1 r:0]
-- With the 'name' setting, Impala can read the Parquet data files
-- despite mismatching column order.
set PARQUET_FALLBACK_SCHEMA_RESOLUTION=name;
PARQUET_FALLBACK_SCHEMA_RESOLUTION set to name
See PARQUET_FALLBACK_SCHEMA_RESOLUTION Query Option (Impala 2.6 or higher only) on page 370 for
more details.
Primitive types
Logical types
Parquet uses type annotations to extend the types that it can store, by specifying how the primitive types should be
interpreted.
Complex types:
For the complex types (ARRAY, MAP, and STRUCT) available in Impala 2.3 and higher, Impala only supports queries
against those types in Parquet tables.
Because Impala can query some kinds of tables that it cannot currently write to, after creating tables of certain
file formats, you might use the Hive shell to load the data. See How Impala Works with Hadoop File Formats on
page 671 for details. After loading data into a table through Hive or other mechanism outside of Impala, issue
a REFRESH table_name statement the next time you connect to the Impala node, before querying the table, to
make Impala recognize the new data.
For example, here is how you might create some ORC tables in Impala (by specifying the columns explicitly, or
cloning the structure of another table), load data through Hive, and query them through Impala:
$ impala-shell -i localhost
[localhost:21000] default> CREATE TABLE orc_table (x INT) STORED AS ORC;
[localhost:21000] default> CREATE TABLE orc_clone LIKE some_other_table
STORED AS ORC;
[localhost:21000] default> quit;
$ hive
hive> INSERT INTO TABLE orc_table SELECT x FROM some_other_table;
3 Rows loaded to orc_table
Time taken: 4.169 seconds
hive> quit;
$ impala-shell -i localhost
[localhost:21000] default> SELECT * FROM orc_table;
Fetched 0 row(s) in 0.11s
[localhost:21000] default> -- Make Impala recognize the data loaded through
Hive;
[localhost:21000] default> REFRESH orc_table;
[localhost:21000] default> SELECT * FROM orc_table;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
| How Impala Works with Hadoop File Formats | 696
If you are converting partitioned tables, you must complete additional steps. In such a case, specify additional settings
similar to the following:
Remember that Hive does not require that you specify a source format for it. Consider the case of converting a table
with two partition columns called year and month to a Snappy compressed ORC table. Combining the components
outlined previously to complete this table conversion, you would specify settings similar to the following:
hive> CREATE TABLE tbl_orc (int_col INT, string_col STRING) STORED AS ORC;
hive> SET hive.exec.compress.output=true;
hive> SET orc.compress=SNAPPY;
hive> SET hive.exec.dynamic.partition.mode=nonstrict;
hive> SET hive.exec.dynamic.partition=true;
hive> INSERT OVERWRITE TABLE tbl_orc SELECT * FROM tbl;
To complete a similar process for a table that includes partitions, you would specify settings similar to the following:
Note:
The compression type is specified in the following command:
SET orc.compress=SNAPPY;
You could elect to specify alternative codecs such as NONE, GZIP, LZO here.
in the core-site.xml configuration file determines how Impala divides the I/O work of reading the data files.
This configuration setting is specified in bytes. By default, this value is 33554432 (32 MB), meaning that Impala
parallelizes S3 read operations on the files as if they were made up of 32 MB blocks. For example, if your S3 queries
primarily access Parquet files written by MapReduce or Hive, increase fs.s3a.block.size to 134217728 (128
MB) to match the row group size of those files. If most S3 queries involve Parquet files written by Impala, increase
fs.s3a.block.size to 268435456 (256 MB) to match the row group size produced by Impala.
Complex types:
In Impala 2.3 and higher, Impala supports the complex types ARRAY, STRUCT, and MAP. In Impala 3.2 and higher,
Impala also supports these complex types in ORC. See Complex Types (Impala 2.3 or higher only) on page 134
for details. These Complex types are currently supported only for the Parquet or ORC file formats. Because Impala
has better performance on Parquet than ORC, if you plan to use complex types, become familiar with the performance
and storage aspects of Parquet first.
definitions that match the fields specified in the Avro schema. With Hive, you can omit the columns and just specify
the Avro schema.
In Impala 2.3 and higher, the CREATE TABLE for Avro tables can include SQL-style column definitions rather than
specifying Avro notation through the TBLPROPERTIES clause. Impala issues warning messages if there are any
mismatches between the types specified in the SQL column definitions and the underlying types; for example, any
TINYINT or SMALLINT columns are treated as INT in the underlying Avro files, and therefore are displayed as
INT in any DESCRIBE or SHOW CREATE TABLE output.
Note:
Currently, Avro tables cannot contain TIMESTAMP columns. If you need to store date and time values in Avro
tables, as a workaround you can use a STRING representation of the values, convert the values to BIGINT with the
UNIX_TIMESTAMP() function, or create separate numeric columns for individual date and time fields using the
EXTRACT() function.
The following examples demonstrate creating an Avro table in Impala, using either an inline column specification or
one taken from a JSON file stored in HDFS:
> )
> STORED AS AVRO
> TBLPROPERTIES ('avro.schema.url'='hdfs://
localhost:8020/avro_schemas/alltypes.json');
Each field of the record becomes a column of the table. Note that any other information, such as the record name, is
ignored.
Note: For nullable Avro columns, make sure to put the "null" entry before the actual type name. In Impala,
all columns are nullable; Impala currently does not have a NOT NULL clause. Any non-nullable property is only
enforced on the Avro side.
Most column types map directly from Avro to Impala under the same names. These are the exceptions and special
cases to consider:
• The DECIMAL type is defined in Avro as a BYTE type with the logicalType property set to "decimal" and
a specified precision and scale.
• The Avro long type maps to BIGINT in Impala.
If you create the table through Hive, switch back to impala-shell and issue an INVALIDATE METADATA
table_name statement. Then you can run queries for that table through impala-shell.
In rare instances, a mismatch could occur between the Avro schema and the column definitions in the metastore
database. In Impala 2.3 and higher, Impala checks for such inconsistencies during a CREATE TABLE statement and
each time it loads the metadata for a table (for example, after INVALIDATE METADATA). Impala uses the following
rules to determine how to treat mismatching columns, a process known as schema reconciliation:
• If there is a mismatch in the number of columns, Impala uses the column definitions from the Avro schema.
• If there is a mismatch in column name or type, Impala uses the column definition from the Avro schema. Because
a CHAR or VARCHAR column in Impala maps to an Avro STRING, this case is not considered a mismatch and the
column is preserved as CHAR or VARCHAR in the reconciled schema. Prior to Impala 2.7 the column name and
comment for such CHAR and VARCHAR columns was also taken from the SQL column definition. In Impala 2.7
and higher, the column name and comment from the Avro schema file take precedence for such columns, and only
the CHAR or VARCHAR type is preserved from the SQL column definition.
• An Impala TIMESTAMP column definition maps to an Avro STRING and is presented as a STRING in the
reconciled schema, because Avro has no binary TIMESTAMP representation. As a result, no Avro table can have a
TIMESTAMP column; this restriction is the same as in earlier Impala releases.
| How Impala Works with Hadoop File Formats | 700
Complex type considerations: Although you can create tables in this file format using the complex types (ARRAY,
STRUCT, and MAP) available in Impala 2.3 and higher, currently, Impala can query these types only in Parquet tables.
The one exception to the preceding rule is COUNT(*) queries on RCFile tables that include complex types. Such
queries are allowed in Impala 2.6 and higher.
tblproperties ('avro.schema.url'='hdfs//your-name-node:port/path/to/
schema.json');
Once the Avro table is created and contains data, you can query it through the impala-shell command:
Now in the Hive shell, you change the type of a column and add a new column with a default value:
-- Promote column "a" from INT to FLOAT (no need to update Avro schema)
ALTER TABLE avro_table CHANGE A A FLOAT;
"type": "record",
"name": "my_record",
"fields": [
{"name": "a", "type": "int"},
{"name": "b", "type": "string"},
{"name": "c", "type": "int", "default": 10}
]}');
Once again in impala-shell, you can query the Avro table based on its latest schema definition. Because the table
metadata was changed outside of Impala, you issue a REFRESH statement first so that Impala has up-to-date metadata
for the table.
Logical Types
-------------
BYTES + logicalType = "decimal" -> DECIMAL
The Avro specification allows string values up to 2**64 bytes in length. Impala queries for Avro tables use 32-bit
integers to hold string lengths. In Impala 2.5 and higher, Impala truncates CHAR and VARCHAR values in Avro tables
to (2**31)-1 bytes. If a query encounters a STRING value longer than (2**31)-1 bytes in an Avro table, the query
fails. In earlier releases, encountering such long values in an Avro table could cause a crash.
| How Impala Works with Hadoop File Formats | 703
Because Impala can query some kinds of tables that it cannot currently write to, after creating tables of certain
file formats, you might use the Hive shell to load the data. See How Impala Works with Hadoop File Formats on
page 671 for details. After loading data into a table through Hive or other mechanism outside of Impala, issue
a REFRESH table_name statement the next time you connect to the Impala node, before querying the table, to
make Impala recognize the new data.
Important: See Known Issues and Workarounds in Impala on page 837 for potential compatibility issues with
RCFile tables created in Hive 0.12, due to a change in the default RCFile SerDe for Hive.
For example, here is how you might create some RCFile tables in Impala (by specifying the columns explicitly, or
cloning the structure of another table), load data through Hive, and query them through Impala:
$ impala-shell -i localhost
[localhost:21000] > create table rcfile_table (x int) stored as rcfile;
[localhost:21000] > create table rcfile_clone like some_other_table stored
as rcfile;
[localhost:21000] > quit;
| How Impala Works with Hadoop File Formats | 704
$ hive
hive> insert into table rcfile_table select x from some_other_table;
3 Rows loaded to rcfile_table
Time taken: 19.015 seconds
hive> quit;
$ impala-shell -i localhost
[localhost:21000] > select * from rcfile_table;
Returned 0 row(s) in 0.23s
[localhost:21000] > -- Make Impala recognize the data loaded through Hive;
[localhost:21000] > refresh rcfile_table;
[localhost:21000] > select * from rcfile_table;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
Returned 3 row(s) in 0.23s
Complex type considerations: Although you can create tables in this file format using the complex types (ARRAY,
STRUCT, and MAP) available in Impala 2.3 and higher, currently, Impala can query these types only in Parquet tables.
The one exception to the preceding rule is COUNT(*) queries on RCFile tables that include complex types. Such
queries are allowed in Impala 2.6 and higher.
If you are converting partitioned tables, you must complete additional steps. In such a case, specify additional settings
similar to the following:
Remember that Hive does not require that you specify a source format for it. Consider the case of converting a table
with two partition columns called year and month to a Snappy compressed RCFile. Combining the components
outlined previously to complete this table conversion, you would specify settings similar to the following:
hive> CREATE TABLE tbl_rc (int_col INT, string_col STRING) STORED AS RCFILE;
hive> SET hive.exec.compress.output=true;
hive> SET mapred.max.split.size=256000000;
hive> SET mapred.output.compression.type=BLOCK;
hive> SET
mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
hive> SET hive.exec.dynamic.partition.mode=nonstrict;
| How Impala Works with Hadoop File Formats | 705
To complete a similar process for a table that includes partitions, you would specify settings similar to the following:
Note:
The compression type is specified in the following command:
SET
mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
Because Impala can query some kinds of tables that it cannot currently write to, after creating tables of certain
file formats, you might use the Hive shell to load the data. See How Impala Works with Hadoop File Formats on
page 671 for details. After loading data into a table through Hive or other mechanism outside of Impala, issue
a REFRESH table_name statement the next time you connect to the Impala node, before querying the table, to
make Impala recognize the new data.
For example, here is how you might create some SequenceFile tables in Impala (by specifying the columns explicitly,
or cloning the structure of another table), load data through Hive, and query them through Impala:
$ impala-shell -i localhost
[localhost:21000] > create table seqfile_table (x int) stored as
sequencefile;
[localhost:21000] > create table seqfile_clone like some_other_table stored
as sequencefile;
[localhost:21000] > quit;
$ hive
hive> insert into table seqfile_table select x from some_other_table;
3 Rows loaded to seqfile_table
Time taken: 19.047 seconds
hive> quit;
$ impala-shell -i localhost
[localhost:21000] > select * from seqfile_table;
Returned 0 row(s) in 0.23s
[localhost:21000] > -- Make Impala recognize the data loaded through Hive;
[localhost:21000] > refresh seqfile_table;
[localhost:21000] > select * from seqfile_table;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
Returned 3 row(s) in 0.23s
Complex type considerations: Although you can create tables in this file format using the complex types (ARRAY,
STRUCT, and MAP) available in Impala 2.3 and higher, currently, Impala can query these types only in Parquet tables.
The one exception to the preceding rule is COUNT(*) queries on RCFile tables that include complex types. Such
queries are allowed in Impala 2.6 and higher.
hive> SET
mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
hive> insert overwrite table new_table select * from old_table;
If you are converting partitioned tables, you must complete additional steps. In such a case, specify additional settings
similar to the following:
Remember that Hive does not require that you specify a source format for it. Consider the case of converting a
table with two partition columns called year and month to a Snappy compressed SequenceFile. Combining
the components outlined previously to complete this table conversion, you would specify settings similar to the
following:
To complete a similar process for a table that includes partitions, you would specify settings similar to the following:
Note:
The compression type is specified in the following command:
SET
mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;
This configuration setting is specified in bytes. By default, this value is 33554432 (32 MB), meaning that Impala
parallelizes S3 read operations on the files as if they were made up of 32 MB blocks. For example, if your S3 queries
primarily access Parquet files written by MapReduce or Hive, increase fs.s3a.block.size to 134217728 (128
MB) to match the row group size of those files. If most S3 queries involve Parquet files written by Impala, increase
fs.s3a.block.size to 268435456 (256 MB) to match the row group size produced by Impala.
the partitions as new data arrives. For example, you can construct partitions that apply to date ranges rather than a
separate partition for each day or each hour.
• Data is physically divided based on units of storage called tablets. Tablets are stored by tablet servers. Each
tablet server can store multiple tablets, and each tablet is replicated across multiple tablet servers, managed
automatically by Kudu. Where practical, co-locate the tablet servers on the same hosts as the Impala daemons,
although that is not required.
Impala DDL Enhancements for Kudu Tables (CREATE TABLE and ALTER
TABLE)
You can use the Impala CREATE TABLE and ALTER TABLE statements to create and fine-tune the characteristics
of Kudu tables. Because Kudu tables have features and properties that do not apply to other kinds of Impala tables,
familiarize yourself with Kudu-related concepts and syntax first. For the general syntax of the CREATE TABLE
statement for Kudu tables, see CREATE TABLE Statement on page 240.
PRIMARY KEY
| [NOT] NULL
| ENCODING codec
| COMPRESSION algorithm
| DEFAULT constant_expression
| BLOCK_SIZE number
See the following sections for details about each column attribute.
When the primary key is a single column, these two forms are equivalent. If the primary key consists of more than
one column, you must specify the primary key using a separate entry in the column list:
The SHOW CREATE TABLE statement always represents the PRIMARY KEY specification as a separate item in the
column list:
+------------------------------------------------------------------------------
+
| result
|
+------------------------------------------------------------------------------
+
| CREATE TABLE user.inline_pk_rewritten (
|
| id BIGINT NOT NULL ENCODING AUTO_ENCODING COMPRESSION
DEFAULT_COMPRESSION, |
| s STRING NULL ENCODING AUTO_ENCODING COMPRESSION DEFAULT_COMPRESSION,
|
| PRIMARY KEY (id)
|
| )
|
| PARTITION BY HASH (id) PARTITIONS 2
|
| STORED AS KUDU
|
| TBLPROPERTIES ('kudu.master_addresses'='host.example.com')
|
+------------------------------------------------------------------------------
+
The notion of primary key only applies to Kudu tables. Every Kudu table requires a primary key. The primary key
consists of one or more columns. You must specify any primary key columns first in the column list.
The contents of the primary key columns cannot be changed by an UPDATE or UPSERT statement. Including too
many columns in the primary key (more than 5 or 6) can also reduce the performance of write operations. Therefore,
pick the most selective and most frequently tested non-null columns for the primary key specification. If a column
must always have a value, but that value might change later, leave it out of the primary key and use a NOT NULL
clause for that column instead. If an existing row has an incorrect or outdated key column value, delete the old row
and insert an entirely new row with the correct primary key.
During performance optimization, Kudu can use the knowledge that nulls are not allowed to skip certain checks on
each input row, speeding up queries and join operations. Therefore, specify NOT NULL constraints when appropriate.
The NULL clause is the default condition for all columns that are not part of the primary key. You can omit it, or
specify it to clarify that you have made a conscious design decision to allow nulls in a column.
Because primary key columns cannot contain any NULL values, the NOT NULL clause is not required for the primary
key columns, but you might still specify it to make your code self-describing.
DEFAULT Attribute
You can specify a default value for columns in Kudu tables. The default value can be any constant expression, for
example, a combination of literal values, arithmetic and string operations. It cannot contain references to columns or
non-deterministic function calls.
The following example shows different kinds of expressions for the DEFAULT clause. The requirement to use a
constant value means that you can fill in a placeholder value such as NULL, empty string, 0, -1, 'N/A' and so on,
but you cannot reference functions or column names. Therefore, you cannot use DEFAULT to do things such as
automatically making an uppercase copy of a string value, storing Boolean values based on tests of other columns, or
add or subtract one from another column representing a sequence number.
Note:
When designing an entirely new schema, prefer to use NULL as the placeholder for any unknown or missing values,
because that is the universal convention among database systems. Null values can be stored efficiently, and easily
checked with the IS NULL or IS NOT NULL operators. The DEFAULT attribute is appropriate when ingesting data
that already has an established convention for representing unknown or missing values, or where the vast majority of
rows have some common non-null value.
ENCODING Attribute
Each column in a Kudu table can optionally use an encoding, a low-overhead form of compression that reduces the
size on disk, then requires additional CPU cycles to reconstruct the original values during queries. Typically, highly
compressible data benefits from the reduced I/O to read the data back from disk.
The encoding keywords that Impala recognizes are:
• AUTO_ENCODING: use the default encoding based on the column type, which are bitshuffle for the numeric type
columns and dictionary for the string type columns.
• PLAIN_ENCODING: leave the value in its original binary format.
• RLE: compress repeated values (when sorted in primary key order) by including a count.
• DICT_ENCODING: when the number of different string values is low, replace the original string with a numeric
ID.
• BIT_SHUFFLE: rearrange the bits of the values to efficiently compress sequences of values that are identical or
vary only slightly based on primary key order. The resulting encoded data is also compressed with LZ4.
• PREFIX_ENCODING: compress common prefixes in string values; mainly for use internally within Kudu.
The following example shows the Impala keywords representing the encoding types. (The Impala keywords match
the symbolic names used within Kudu.) For usage guidelines on the different kinds of encoding, see the Kudu
| Using Impala to Query Kudu Tables | 713
documentation. The DESCRIBE output shows how the encoding is reported after the table is created, and that
omitting the encoding (in this case, for the ID column) is the same as specifying DEFAULT_ENCODING.
COMPRESSION Attribute
You can specify a compression algorithm to use for each column in a Kudu table. This attribute imposes more CPU
overhead when retrieving the values than the ENCODING attribute does. Therefore, use it primarily for columns with
long strings that do not benefit much from the less-expensive ENCODING attribute.
The choices for COMPRESSION are LZ4, SNAPPY, and ZLIB.
Note:
Columns that use the BITSHUFFLE encoding are already compressed using LZ4, and so typically do not need any
additional COMPRESSION attribute.
The following example shows design considerations for several STRING columns with different distribution
characteristics, leading to choices for both the ENCODING and COMPRESSION attributes. The country values
come from a specific set of strings, therefore this column is a good candidate for dictionary encoding. The post_id
column contains an ascending sequence of integers, where several leading bits are likely to be all zeroes, therefore
this column is a good candidate for bitshuffle encoding. The body column and the corresponding columns for
translated versions tend to be long unique strings that are not practical to use with any of the encoding schemes,
therefore they employ the COMPRESSION attribute instead. The ideal compression codec in each case would require
some experimentation to determine how much space savings it provided and how much CPU overhead it added, based
on real-world data.
BLOCK_SIZE Attribute
Although Kudu does not use HDFS files internally, and thus is not affected by the HDFS block size, it does have an
underlying unit of I/O called the block size. The BLOCK_SIZE attribute lets you set the block size for any column.
The block size attribute is a relatively advanced feature. Refer to the Kudu documentation for usage details.
Hash Partitioning
Hash partitioning is the simplest type of partitioning for Kudu tables. For hash-partitioned Kudu tables, inserted
rows are divided up between a fixed number of “buckets” by applying a hash function to the values of the columns
specified in the HASH clause. Hashing ensures that rows with similar values are evenly distributed, instead of
clumping together all in the same bucket. Spreading new rows across the buckets this way lets insertion operations
work in parallel across multiple tablet servers. Separating the hashed values can impose additional overhead on
queries, where queries with range-based predicates might have to read multiple tablets to retrieve all the relevant
values.
Note:
The largest number of buckets that you can create with a PARTITIONS clause varies depending on the number of
tablet servers in the cluster, while the smallest is 2. For simplicity, some of the simple CREATE TABLE statements
throughout this section use PARTITIONS 2 to illustrate the minimum requirements for a Kudu table. For large
tables, prefer to use roughly 10 partitions per server in the cluster.
| Using Impala to Query Kudu Tables | 715
Range Partitioning
Range partitioning lets you specify partitioning precisely, based on single values or ranges of values within one or
more columns. You add one or more RANGE clauses to the CREATE TABLE statement, following the PARTITION
BY clause.
Range-partitioned Kudu tables use one or more range clauses, which include a combination of constant expressions,
VALUE or VALUES keywords, and comparison operators. (This syntax replaces the SPLIT ROWS clause used with
early Kudu versions.) For the full syntax, see CREATE TABLE Statement on page 240.
-- Same as previous table, with an extra range covering the single key value
'00000'.
create table million_rows_three_ranges (id string primary key, s string)
partition by hash(id) partitions 50,
range (partition 'a' <= values < '{', partition 'A' <= values < '[',
partition value = '00000')
stored as kudu;
Note:
When defining ranges, be careful to avoid “fencepost errors” where values at the extreme ends might be included or
omitted by accident. For example, in the tables defined in the preceding code listings, the range "a" <= VALUES
< "{" ensures that any values starting with z, such as za or zzz or zzz-ZZZ, are all included, by using a less-than
operator for the smallest value after all the values starting with z.
For range-partitioned Kudu tables, an appropriate range must exist before a data value can be created in the table.
Any INSERT, UPDATE, or UPSERT statements fail if they try to create column values that fall outside the specified
ranges. The error checking for ranges is performed on the Kudu side; Impala passes the specified range information to
Kudu, and passes back any error or warning if the ranges are not valid. (A nonsensical range specification causes an
error for a DDL statement, but only a warning for a DML statement.)
| Using Impala to Query Kudu Tables | 716
partition by range (year) (partition 1885 <= values <= 1889, partition 1893
<= values <= 1897)
The ALTER TABLE statement with the ADD PARTITION or DROP PARTITION clauses can be used to add or
remove ranges from an existing Kudu table.
When a range is added, the new range must not overlap with any of the previous ranges; that is, it can only fill in gaps
within the previous ranges.
alter table year_ranges add range partition 1890 <= values < 1893;
When a range is removed, all the associated rows in the table are deleted. (This is true whether the table is internal or
external.)
alter table year_ranges drop range partition 1890 <= values < 1893;
Kudu tables can also use a combination of hash and range partitioning.
• Any nanoseconds in the original 96-bit value produced by Impala are not stored, because Kudu represents date/
time columns using 64-bit values. The nanosecond portion of the value is rounded, not truncated. Therefore, a
TIMESTAMP value that you store in a Kudu table might not be bit-for-bit identical to the value returned by a
query.
• The conversion between the Impala 96-bit representation and the Kudu 64-bit representation introduces some
performance overhead when reading or writing TIMESTAMP columns. You can minimize the overhead during
writes by performing inserts through the Kudu API. Because the overhead during reads applies to each query, you
might continue to use a BIGINT column to represent date/time values in performance-critical applications.
• The Impala TIMESTAMP type has a narrower range for years than the underlying Kudu data type. Impala
can represent years 1400-9999. If year values outside this range are written to a Kudu table by a non-Impala
client, Impala returns NULL by default when reading those TIMESTAMP values during a query. Or, if the
ABORT_ON_ERROR query option is enabled, the query fails when it encounters a value with an out-of-range year.
Because Kudu tables have some performance overhead to convert TIMESTAMP columns to the Impala 96-bit internal
representation, for performance-critical applications you might store date/time information as the number of seconds,
milliseconds, or microseconds since the Unix epoch date of January 1, 1970. Specify the column as BIGINT in the
Impala CREATE TABLE statement, corresponding to an 8-byte integer (an int64) in the underlying Kudu table).
Then use Impala date/time conversion functions as necessary to produce a numeric, TIMESTAMP, or STRING value
depending on the context.
For example, the unix_timestamp() function returns an integer result representing the number of seconds
past the epoch. The now() function produces a TIMESTAMP representing the current date and time, which can be
passed as an argument to unix_timestamp(). And string literals representing dates and date/times can be cast to
TIMESTAMP, and from there converted to numeric values. The following examples show how you might store a date/
time column as BIGINT in a Kudu table, but still use string literals and TIMESTAMP values for convenience.
-- now() returns a TIMESTAMP and shows the format for string literals you
can cast to TIMESTAMP.
select now();
+-------------------------------+
| now() |
+-------------------------------+
| 2017-01-25 23:50:10.132385000 |
+-------------------------------+
| Using Impala to Query Kudu Tables | 718
select unix_timestamp('2017-01-01');
+------------------------------+
| unix_timestamp('2017-01-01') |
+------------------------------+
| 1483228800 |
+------------------------------+
Note:
| Using Impala to Query Kudu Tables | 719
If you do high-precision arithmetic involving numeric date/time values, when dividing millisecond values by 1000, or
microsecond values by 1 million, always cast the integer numerator to a DECIMAL with sufficient precision and scale
to avoid any rounding or loss of precision.
See Overview of Impala Tables on page 198 for examples of how to change the name of the Impala table in the
metastore database, the name of the underlying Kudu table, or both.
The following are some of the changes you need to consider when working with Kudu services integrated with the
HMS.
• When Kudu is integrated with the Hive Metastore, Impala must be configured to use the same HMS as Kudu.
• Since there may be no one-to-one mapping between Kudu tables and external tables, only internal tables are
automatically synchronized.
• When you create a table in Kudu, Kudu will create an HMS entry for that table with the internal table type.
• When the Kudu service is integrated with the HMS, internal table entries will be created automatically in the
HMS when tables are created in Kudu without Impala. To access these tables through Impala, run INVALIDATE
METADATA statement so Impala picks up the latest metadata.
Note:
The LOAD DATA statement, which involves manipulation of HDFS data files, does not apply to Kudu tables.
Starting from Impala 2.9, the INSERT or UPSERT operations into Kudu tables automatically add an exchange
and a sort node to the plan that partitions and sorts the rows according to the partitioning/primary key scheme of
the target table (unless the number of rows to be inserted is small enough to trigger single node execution). Since
Kudu partitions and sorts rows on write, pre-partitioning and sorting takes some of the load off of Kudu and helps
large INSERT operations to complete without timing out. However, this default behavior may slow down the
end-to-end performance of the INSERT or UPSERT operations. Starting fromImpala 2.10, you can use the /*
+NOCLUSTERED */ and /* +NOSHUFFLE */ hints together to disable partitioning and sorting before the
rows are sent to Kudu. Additionally, since sorting may consume a large amount of memory, consider setting the
MEM_LIMIT query option for those queries.
<property>
<name>hbase.client.retries.number</name>
<value>3</value>
</property>
<property>
<name>hbase.rpc.timeout</name>
<value>3000</value>
</property>
should still define the column that corresponds to the HBase row key as a STRING, to allow fast lookups using those
columns.
describe hbase_table;
Query: describe hbase_table
+-----------------------+--------+---------+
| name | type | comment |
+-----------------------+--------+---------+
| cust_id | string | |
| birth_year | string | |
| never_logged_on | string | |
| private_email_address | string | |
| year_registered | int | |
+-----------------------+--------+---------+
| Using Impala to Query HBase Tables | 725
The best case for performance involves a single row lookup using an equality comparison on the
column defined as the row key:
Another type of efficient query involves a range lookup on the row key column, using SQL
operators such as greater than (or equal), less than (or equal), or BETWEEN. This example also
includes an equality test on a non-key column; because that column is a STRING, Impala can let
HBase perform that test, indicated by the hbase filters: line in the EXPLAIN output. Doing
the filtering within HBase is more efficient than transmitting all the data to Impala and doing the
filtering on the Impala side.
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HBASE [hbase.hbase_table]
|
| start key: a
|
| stop key: b\0
|
| hbase filters: cols:never_logged_on EQUAL 'true'
|
+---------------------------------------------------------------------------------
+
The query is less efficient if Impala has to evaluate any of the predicates, because Impala must
scan the entire HBase table. Impala can only push down predicates to HBase for columns declared
as STRING. This example tests a column declared as INT, and the predicates: line in the
EXPLAIN output indicates that the test is performed after the data is transmitted to Impala.
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HBASE [hbase.hbase_table]
|
| predicates: year_registered = 2010
|
+---------------------------------------------------------------------------------
+
The same inefficiency applies if the key column is compared to any non-constant value. Here, even
though the key column is a STRING, and is tested using an equality operator, Impala must scan
the entire HBase table because the key column is compared to another column value rather than a
constant.
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HBASE [hbase.hbase_table]
|
| predicates: cust_id = private_email_address
|
+---------------------------------------------------------------------------------
+
Currently, tests on the row key using OR or IN clauses are not optimized into direct lookups either.
Such limitations might be lifted in the future, so always check the EXPLAIN output to be sure
whether a particular SQL construct results in an efficient query or not for HBase tables.
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HBASE [hbase.hbase_table]
|
| predicates: cust_id = '[email protected]' OR cust_id =
'[email protected]' |
+---------------------------------------------------------------------------------
+
| 01:AGGREGATE
|
| | output: count(*)
|
| |
|
| 00:SCAN HBASE [hbase.hbase_table]
|
| predicates: cust_id IN ('[email protected]',
'[email protected]') |
| Using Impala to Query HBase Tables | 728
+---------------------------------------------------------------------------------
+
Either rewrite into separate queries for each value and combine the results in the application, or
combine the single-row queries using UNION ALL:
explain
select count(*) from hbase_table where cust_id =
'[email protected]'
union all
select count(*) from hbase_table where cust_id =
'[email protected]';
+---------------------------------------------------------------------------------
+
| Explain String
|
+---------------------------------------------------------------------------------
+
...
| | 04:AGGREGATE
|
| | | output: count(*)
|
| | |
|
| | 03:SCAN HBASE [hbase.hbase_table]
|
| | start key: [email protected]
|
| | stop key: [email protected]\0
|
| |
|
| 10:MERGE
|
...
| 02:AGGREGATE
|
| | output: count(*)
|
| |
|
| 01:SCAN HBASE [hbase.hbase_table]
|
| start key: [email protected]
|
| stop key: [email protected]\0
|
+---------------------------------------------------------------------------------
+
| Using Impala to Query HBase Tables | 729
Or update the impalad defaults file /etc/default/impala and include settings for
HBASE_CACHE_BLOCKS and/or HBASE_CACHING in the -default_query_options
setting for IMPALA_SERVER_ARGS. See Modifying Impala Startup Options on page 31 for
details.
Note: In Impala 2.0 and later, these options are settable through the JDBC or ODBC interfaces
using the SET statement.
Although Impala does not have an UPDATE statement, you can achieve the same effect by doing successive INSERT
statements using the same value for the key column each time:
$ hbase shell
15/02/10 16:07:45
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
...
Issue the following CREATE TABLE statement in the Hive shell. (The Impala CREATE TABLE statement currently
does not support the STORED BY clause, so you switch into Hive to create the table, then back to Impala and the
impala-shell interpreter to issue the queries.)
| Using Impala to Query HBase Tables | 731
This example creates an external table mapped to the HBase table, usable by both Impala and Hive. It is defined as an
external table so that when dropped by Impala or Hive, the original HBase table is not touched at all.
The WITH SERDEPROPERTIES clause specifies that the first column (ID) represents the row key, and maps
the remaining columns of the SQL table to HBase column families. The mapping relies on the ordinal order of the
columns in the table, not the column names in the CREATE TABLE statement. The first column is defined to be the
lookup key; the STRING data type produces the fastest key-based lookups for HBase tables.
Note: For Impala with HBase tables, the most important aspect to ensure good performance is to use a STRING
column as the row key, as shown in this example.
$ hive
...
hive> use hbase;
OK
Time taken: 4.095 seconds
hive> CREATE EXTERNAL TABLE hbasestringids (
> id string,
> bool_col boolean,
> tinyint_col tinyint,
> smallint_col smallint,
> int_col int,
> bigint_col bigint,
> float_col float,
> double_col double,
> date_string_col string,
> string_col string,
> timestamp_col timestamp)
> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> WITH SERDEPROPERTIES (
> "hbase.columns.mapping" =
>
":key,boolsCF:bool_col,intsCF:tinyint_col,intsCF:smallint_col,intsCF:int_col,intsCF:
\
>
bigint_col,floatsCF:float_col,floatsCF:double_col,stringsCF:date_string_col,
\
> stringsCF:string_col,stringsCF:timestamp_col"
> )
> TBLPROPERTIES("hbase.table.name" = "hbasealltypessmall");
OK
Time taken: 2.879 seconds
hive> quit;
Once you have established the mapping to an HBase table, you can issue DML statements and queries from Impala.
The following example shows a series of INSERT statements followed by a query. The ideal kind of query from
a performance standpoint retrieves a row from the table based on a row key mapped to a string column. An initial
INVALIDATE METADATA table_name statement makes the table created through Hive visible to Impala.
| double_col | double | |
| float_col | float | |
| bigint_col | bigint | |
| int_col | int | |
| smallint_col | smallint | |
| tinyint_col | tinyint | |
| date_string_col | string | |
| string_col | string | |
| timestamp_col | timestamp | |
+-----------------+-----------+---------+
Fetched 11 row(s) in 0.02s
[localhost:21000] > insert into hbasestringids values
('0001',true,3.141,9.94,1234567,32768,4000,76,'2014-12-31','Hello
world',now());
Inserted 1 row(s) in 0.26s
[localhost:21000] > insert into hbasestringids values
('0002',false,2.004,6.196,1500,8000,129,127,'2014-01-01','Foo bar',now());
Inserted 1 row(s) in 0.12s
[localhost:21000] > select * from hbasestringids where id = '0001';
+------+----------+------------+-------------------+------------
+---------+--------------+-------------+-----------------+-------------
+-------------------------------+
| id | bool_col | double_col | float_col | bigint_col | int_col |
smallint_col | tinyint_col | date_string_col | string_col | timestamp_col
|
+------+----------+------------+-------------------+------------
+---------+--------------+-------------+-----------------+-------------
+-------------------------------+
| 0001 | true | 3.141 | 9.939999580383301 | 1234567 | 32768
| 4000 | 76 | 2014-12-31 | Hello world | 2015-02-10
16:36:59.764838000 |
+------+----------+------------+-------------------+------------
+---------+--------------+-------------+-----------------+-------------
+-------------------------------+
Fetched 1 row(s) in 0.54s
Note: After you create a table in Hive, such as the HBase mapping table in this example, issue an INVALIDATE
METADATA table_name statement the next time you connect to Impala, make Impala aware of the new table.
(Prior to Impala 1.2.4, you could not specify the table name if Impala was not aware of the table yet; in Impala 1.2.4
and higher, specifying the table name avoids reloading the metadata for other tables that are not changed.)
<property>
<name>fs.s3a.access.key</name>
<value>your_access_key</value>
</property>
<property>
<name>fs.s3a.secret.key</name>
<value>your_secret_key</value>
</property>
After specifying the credentials, restart both the Impala and Hive services. (Restarting Hive is required because
Impala queries, CREATE TABLE statements, and so on go through the Hive metastore.)
Important:
Although you can specify the access key ID and secret key as part of the s3a:// URL in the LOCATION attribute,
doing so makes this sensitive information visible in many places, such as DESCRIBE FORMATTED output and
Impala log files. Therefore, specify this information centrally in the core-site.xml file, and restrict read access
to that file to only trusted users.
You point a nonpartitioned table or an individual partition at S3 by specifying a single directory path in S3, which
could be any arbitrary directory. To replicate the structure of an entire Impala partitioned table or database in S3
requires more care, with directories and subdirectories nested and named to match the equivalent directory tree
in HDFS. Consider setting up an empty staging area if necessary in HDFS, and recording the complete directory
structure so that you can replicate it in S3.
For convenience when working with multiple tables with data files stored in S3, you can create a database with a
LOCATION attribute pointing to an S3 path. Specify a URL of the form s3a://bucket/root/path/for/
database for the LOCATION attribute of the database. Any tables created inside that database automatically create
directories underneath the one specified by the database LOCATION attribute.
For example, the following session creates a partitioned table where only a single partition resides on S3. The
partitions for years 2013 and 2014 are located on HDFS. The partition for year 2015 includes a LOCATION attribute
with an s3a:// URL, and so refers to data residing on S3, under a specific path underneath the bucket impala-
demo.
The following session creates a database and two partitioned tables residing entirely on S3, one partitioned by a single
column and the other partitioned by multiple columns. Because a LOCATION attribute with an s3a:// URL is
specified for the database, the tables inside that database are automatically created on S3 underneath the database
directory. To see the names of the associated subdirectories, including the partition key values, we use an S3 client
tool to examine how the directory structure is organized on S3. For example, Impala partition directories such as
month=1 do not include leading zeroes, which sometimes appear in partition directories created through Hive.
The CREATE DATABASE and CREATE TABLE statements create the associated directory paths if they do not
already exist. You can specify multiple levels of directories, and the CREATE statement creates all appropriate levels,
similar to using mkdir -p.
Use the standard S3 file upload methods to actually put the data files into the right locations. You can also put
the directory paths and data files in place before creating the associated Impala databases or tables, and Impala
automatically uses the data from the appropriate location after the associated databases and tables are created.
You can switch whether an existing table or partition points to data in HDFS or S3. For example, if you have an
Impala table or partition pointing to data files in HDFS or S3, and you later transfer those data files to the other
filesystem, use an ALTER TABLE statement to adjust the LOCATION attribute of the corresponding table or partition
to reflect that change. Because Impala does not have an ALTER DATABASE statement, this location-switching
technique is not practical for entire databases that have a custom LOCATION attribute.
-- Now from a web browser, upload the same data file(s) to S3 as in the HDFS
table,
-- under the relevant bucket and path. If you already have the data in S3,
you would
-- point the table LOCATION at an existing path.
In this case, we have already uploaded a Parquet file with a million rows of data to the sample_data directory
underneath the impala-demo bucket on S3. This session creates a table with matching column settings pointing to
the corresponding location in S3, then queries the table. Because the data is already in place on S3 when the table is
created, no REFRESH statement is required.
CREATE TABLE AS SELECT statements involve moving files from one directory to another. (In the case of
INSERT and CREATE TABLE AS SELECT, the files are moved from a temporary staging directory to the final
destination directory.) Because S3 does not support a “rename” operation for existing objects, in these cases Impala
actually copies the data files from one location to another and then removes the original files. In Impala 2.6, the
S3_SKIP_INSERT_STAGING query option provides a way to speed up INSERT statements for S3 tables and
partitions, with the tradeoff that a problem during statement execution could leave data in an inconsistent state. It does
not apply to INSERT OVERWRITE or LOAD DATA statements. See S3_SKIP_INSERT_STAGING Query Option
(Impala 2.6 or higher only) on page 376 for details.
When optimizing aspects of for complex queries such as the join order, Impala treats tables on HDFS and S3 the
same way. Therefore, follow all the same tuning recommendations for S3 tables as for HDFS ones, such as using the
COMPUTE STATS statement to help Impala construct accurate estimates of row counts and cardinality. See Tuning
Impala for Performance on page 590 for details.
In query profile reports, the numbers for BytesReadLocal, BytesReadShortCircuit,
BytesReadDataNodeCached, and BytesReadRemoteUnexpected are blank because those metrics come
from HDFS. If you do see any indications that a query against an S3 table performed “remote read” operations, do not
be alarmed. That is expected because, by definition, all the I/O for S3 tables involves remote reads.
node fails during execution, the table could end up with inconsistent data. Set this option to false if stronger
consistency is required, however this setting will make the INSERT operations slower.
• Too many files in a table can make metadata loading and updating slow on S3. If too many requests are made to
S3, S3 has a back-off mechanism and responds slower than usual. You might have many small files because of:
• Too many partitions due to over-granular partitioning. Prefer partitions with many megabytes of data, so that
even a query against a single partition can be parallelized effectively.
• Many small INSERT queries. Prefer bulk INSERTs so that more data is written to fewer files.
Prerequisites
These procedures presume that you have already set up an Azure account, configured an ADLS store, and configured
your Hadoop cluster with appropriate credentials to be able to access ADLS. See the following resources for
information:
• Get started with Azure Data Lake Store using the Azure Portal
• Azure Data Lake Storage Gen2
• Hadoop Azure Data Lake Support
• The TRUNCATE TABLE Statement (Impala 2.3 or higher only) on page 401 statement always removes the
corresponding data files from ADLS when the table is truncated.
• The LOAD DATA Statement on page 299 can move data files residing in HDFS into an ADLS table.
• The INSERT Statement on page 288, or the CREATE TABLE AS SELECT form of the CREATE TABLE
statement, can copy data from an HDFS table or another ADLS table into an ADLS table.
For usage information about Impala SQL statements with ADLS tables, see Using Impala DML Statements for ADLS
Data on page 742.
<property>
<name>dfs.adls.oauth2.access.token.provider.type</name>
<value>ClientCredential</value>
</property>
<property>
<name>dfs.adls.oauth2.client.id</name>
<value>your_client_id</value>
</property>
<property>
<name>dfs.adls.oauth2.credential</name>
<value>your_client_secret</value>
</property>
<property>
<name>dfs.adls.oauth2.refresh.url</name>
<value>https://login.windows.net/your_azure_tenant_id/oauth2/token</
value>
</property>
<property>
<name>fs.azure.account.auth.type</name>
<value>OAuth</value>
</property>
<property>
<name>fs.azure.account.oauth.provider.type</name>
<value>org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider</
value>
</property>
<property>
<name>fs.azure.account.oauth2.client.id</name>
<value>your_client_id</value>
</property>
<property>
<name>fs.azure.account.oauth2.client.secret</name>
<value>your_client_secret</value>
</property>
<property>
<name>fs.azure.account.oauth2.client.endpoint</name>
| Using Impala with the Azure Data Lake Store (ADLS) | 742
<value>https://login.microsoftonline.com/your_azure_tenant_id/oauth2/
token</value>
</property>
Note:
Check if your Hadoop distribution or cluster management tool includes support for filling in and distributing
credentials across the cluster in an automated way.
After specifying the credentials, restart both the Impala and Hive services. Restarting Hive is required because certain
Impala queries, such as CREATE TABLE statements, go through the Hive metastore.
adl://account.azuredatalakestore.net/path/file
• For ADLS Gen2:
abfs://[email protected]/path/file
or
abfss://[email protected]/path/file
| Using Impala with the Azure Data Lake Store (ADLS) | 743
container denotes the parent location that holds the files and folders, which is the Containers in the Azure Storage
Blobs service.
account is the name given for your storage account.
Note:
By default, TLS is enabled both with abfs:// and abfss://.
When you set the fs.azure.always.use.https=false property, TLS is disabled with abfs://, and TLS
is enabled with abfss://
For a partitioned table, either specify a separate LOCATION clause for each new partition, or specify a base
LOCATION for the table and set up a directory structure in ADLS to mirror the way Impala partitioned tables are
structured in HDFS. Although, strictly speaking, ADLS filenames do not have directory paths, Impala treats ADLS
filenames with / characters the same as HDFS pathnames that include directories.
To point a nonpartitioned table or an individual partition at ADLS, specify a single directory path in ADLS, which
could be any arbitrary directory. To replicate the structure of an entire Impala partitioned table or database in ADLS
requires more care, with directories and subdirectories nested and named to match the equivalent directory tree
in HDFS. Consider setting up an empty staging area if necessary in HDFS, and recording the complete directory
structure so that you can replicate it in ADLS.
For example, the following session creates a partitioned table where only a single partition resides on ADLS.
The partitions for years 2013 and 2014 are located on HDFS. The partition for year 2015 includes a LOCATION
attribute with an adl:// URL, and so refers to data residing on ADLS, under a specific path underneath the store
impalademo.
For convenience when working with multiple tables with data files stored in ADLS, you can create a database with
a LOCATION attribute pointing to an ADLS path. Specify a URL of the form as shown above. Any tables created
inside that database automatically create directories underneath the one specified by the database LOCATION
attribute.
The following session creates a database and two partitioned tables residing entirely on ADLS, one partitioned by
a single column and the other partitioned by multiple columns. Because a LOCATION attribute with an adl://
URL is specified for the database, the tables inside that database are automatically created on ADLS underneath
the database directory. To see the names of the associated subdirectories, including the partition key values, we use
an ADLS client tool to examine how the directory structure is organized on ADLS. For example, Impala partition
directories such as month=1 do not include leading zeroes, which sometimes appear in partition directories created
through Hive.
The CREATE DATABASE and CREATE TABLE statements create the associated directory paths if they do not
already exist. You can specify multiple levels of directories, and the CREATE statement creates all appropriate levels,
similar to using mkdir -p.
Use the standard ADLS file upload methods to actually put the data files into the right locations. You can also put
the directory paths and data files in place before creating the associated Impala databases or tables, and Impala
automatically uses the data from the appropriate location after the associated databases and tables are created.
You can switch whether an existing table or partition points to data in HDFS or ADLS. For example, if you have
an Impala table or partition pointing to data files in HDFS or ADLS, and you later transfer those data files to the
other filesystem, use an ALTER TABLE statement to adjust the LOCATION attribute of the corresponding table or
partition to reflect that change. This location-switching technique is not practical for entire databases that have a
custom LOCATION attribute.
If the data on ADLS is only needed for querying by Impala and can be safely discarded once the Impala workflow
is complete, create the associated ADLS tables using the CREATE TABLE syntax, so that dropping the table also
deletes the corresponding data files on ADLS.
For example, this session creates a table in ADLS with the same column layout as a table in HDFS, then examines
the ADLS table and queries some data from it. The table in ADLS works the same as a table in HDFS as far as the
expected file format of the data, table and column statistics, and other table properties. The only indication that it
is not an HDFS table is the adl:// URL in the LOCATION property. Many data files can reside in the ADLS
directory, and their combined contents form the table data. Because the data in this example is uploaded after the table
is created, a REFRESH statement prompts Impala to update its cached information about the data files.
-- Now from a web browser, upload the same data file(s) to ADLS as in the
HDFS table,
-- under the relevant store and path. If you already have the data in ADLS,
you would
-- point the table LOCATION at an existing path.
| | NULL
| NULL |
| # Detailed Table Information | NULL
| NULL |
| Database: | adls_testing
| NULL |
| Owner: | jrussell
| NULL |
| CreateTime: | Mon Mar 16 11:36:25 PDT 2017
| NULL |
| LastAccessTime: | UNKNOWN
| NULL |
| Protect Mode: | None
| NULL |
| Retention: | 0
| NULL |
| Location: | adl://impalademo.azuredatalakestore.net/
usa_cities | NULL |
| Table Type: | MANAGED_TABLE
| NULL |
...
+------------------------------
+----------------------------------------------------+---------+
In this case, we have already uploaded a Parquet file with a million rows of data to the sample_data directory
underneath the impalademo store on ADLS. This session creates a table with matching column settings pointing to
the corresponding location in ADLS, then queries the table. Because the data is already in place on ADLS when the
table is created, no REFRESH statement is required.
• Authorization using the Sentry framework to control access to databases, tables, or columns works the same
whether the data is in HDFS or in ADLS.
• The catalogd daemon caches metadata for both HDFS and ADLS tables. Use REFRESH and INVALIDATE
METADATA for ADLS tables in the same situations where you would issue those statements for HDFS tables.
• Queries against ADLS tables are subject to the same kinds of admission control and resource management as
HDFS tables.
• Metadata about ADLS tables is stored in the same metastore database as for HDFS tables.
• You can set up views referring to ADLS tables, the same as for HDFS tables.
• The COMPUTE STATS, SHOW TABLE STATS, and SHOW COLUMN STATS statements work for ADLS tables
also.
The typical use case for Impala and Isilon together is to use Isilon for the default filesystem, replacing HDFS entirely.
In this configuration, when you create a database, table, or partition, the data always resides on Isilon storage and you
do not need to specify any special LOCATION attribute. If you do specify a LOCATION attribute, its value refers to a
path within the Isilon filesystem. For example:
Impala can write to, delete, and rename data files and database, table, and partition directories on Isilon storage.
Therefore, Impala statements such as CREATE TABLE, DROP TABLE, CREATE DATABASE, DROP DATABASE,
ALTER TABLE, and INSERT work the same with Isilon storage as with HDFS.
When the Impala spill-to-disk feature is activated by a query that approaches the memory limit, Impala writes all
the temporary data to a local (not Isilon) storage device. Because the I/O bandwidth for the temporary data depends
on the number of local disks, and clusters using Isilon storage might not have as many local disks attached, pay
special attention on Isilon-enabled clusters to any queries that use the spill-to-disk feature. Where practical, tune the
queries or allocate extra memory for Impala to avoid spilling. Although you can specify an Isilon storage device as
the destination for the temporary data for the spill-to-disk feature, that configuration is not recommended due to the
need to transfer the data both ways using remote I/O.
When tuning Impala queries on HDFS, you typically try to avoid any remote reads. When the data resides on Isilon
storage, all the I/O consists of remote reads. Do not be alarmed when you see non-zero numbers for remote read
measurements in query profile output. The benefit of the Impala and Isilon integration is primarily convenience of
not having to move or copy large volumes of data to HDFS, rather than raw query performance. You can increase
the performance of Impala I/O for Isilon systems by increasing the value for the ‑‑num_remote_hdfs_io_threads
startup option for the impalad daemon.
| Using Impala Logging | 749
Impala stores information using the glog_v logging system. You will see some messages referring to C++ file
names. Logging is affected by:
• The GLOG_v environment variable specifies which types of messages are logged. See Setting Logging Levels on
page 751 for details.
• The ‑‑logbuflevel startup flag for the impalad daemon specifies how often the log information is written to disk.
The default is 0, meaning that the log is immediately flushed to disk when Impala outputs an important messages
such as a warning or an error, but less important messages such as informational ones are buffered in memory
rather than being flushed to disk immediately.
You can view the contents of the impalad.INFO log file in the file system. With the default configuration settings,
the start of the log file appears as follows:
Note: The preceding example shows only a small part of the log file. Impala log files are often several megabytes in
size.
export GLOG_v=1
Note: For performance reasons, do not enable the most verbose logging level of 3 unless there is no other alternative
for troubleshooting.
For more information on how to configure GLOG, including how to set variable logging levels for different system
components, see documentation for the glog project on github.
| Impala Client Access | 752
Each impalad daemon process, running on separate nodes in a cluster, listens to several ports for incoming
requests:
• Requests from impala-shell and Hue are routed to the impalad daemons through the same port.
• The impalad daemons listen on separate ports for JDBC and ODBC requests.
For a list of the impala-shell command-line options, see impala-shell Configuration Options on page 755. For
reference information about the impala-shell interactive commands, see impala-shell Command Reference on
page 763.
--print_header print_header=true
-o filename or output_file=filename Stores all query results in the specified file. Typically
used to store the results of a single query issued from
--output_file filename
the command line with the -q option. Also works
for interactive sessions; you see the messages such
as number of rows fetched, but not the actual result
set. To suppress these incidental messages when
combining the -q and -o options, redirect stderr
to /dev/null. Added in Impala 1.0.1.
| Impala Client Access | 756
--query_option= Header line Sets default query options for an invocation of the
[impala.query_options], impala-shell command. To set multiple query
"option=
followed on subsequent options at once, use more than one instance of this
value" lines by option=value, one command-line option. The query option names are not
option per line. case-sensitive.
-Q "option=
value"
-s kerberos_service_name kerberos_service_name=name
Instructs impala-shell to authenticate to
or a particular impalad service principal. If a
kerberos_service_name is not specified, impala is
--
used by default. If this option is used in conjunction
kerberos_service_name=name
with a connection in which Kerberos is not supported,
errors are returned.
[impala]
verbose=true
default_db=tpc_benchmarking
write_delimited=true
output_delimiter=,
| Impala Client Access | 760
output_file=/home/tester1/benchmark_results.csv
show_profiles=true
keyval=msg1=hello,keyval=msg2=world
The following example shows a configuration file that connects to a specific remote Impala node, runs a single query
within a particular database, then exits. Any query options predefined under the [impala.query_options]
section in the configuration file take effect during the session.
You would typically use this kind of single-purpose configuration setting with the impala-shell command-line
option --config_file=path_to_config_file, to easily select between many predefined queries that could
be run against different databases, hosts, or even different clusters. To run a sequence of statements instead of a single
query, specify the configuration option query_file=path_to_query_file instead.
[impala]
impalad=impala-test-node1.example.com
default_db=site_stats
# Issue a predefined query and immediately exit.
query=select count(*) from web_traffic where event_date = trunc(now(),'dd')
[impala.query_options]
mem_limit=32g
$ impala-shell
2. Locate the hostname that is running the impalad daemon. If that impalad uses a non-default port (something
other than port 21000) for impala-shell connections, find out the port number also.
3. Use the connect command to connect to an Impala instance. Enter a command of the form:
Note: Replace impalad-host with the hostname you have configured to run Impala in your environment. The
changed prompt indicates a successful connection.
To start impala-shell in a specific database:
You can use all the same connection options as in previous examples. For simplicity, these examples assume that you
are logged into one of the Impala daemons.
1. Find the name of the database containing the relevant tables, views, and so on that you want to operate on.
2. Use the -d option to the impala-shell interpreter to connect and immediately switch to the specified
database, without the need for a USE statement or fully qualified names:
# A utility command that you might run while developing shell scripts
# to manipulate HDFS files.
$ impala-shell -i localhost -d database_of_interest -q 'show tables'
The below example shows a substitution variable passed in by the --var option, and then referenced by statements
issued interactively. Then the variable is reset with the SET command.
Command Explanation
alter Changes the underlying structure or settings of an Impala table, or a table shared
between Impala and Hive. See ALTER TABLE Statement on page 207 and ALTER
VIEW Statement on page 222 for details.
compute stats Gathers important performance-related information for a table, used by Impala to
optimize queries. See COMPUTE STATS Statement on page 223 for details.
connect Connects to the specified instance of impalad. The default port of 21000 is assumed
unless you provide another value. You can connect to any host in your cluster that is
running impalad. If you connect to an instance of impalad that was started with an
alternate port specified by the --fe_port flag, you must provide that alternate port.
See Connecting to Impala Daemon from impala-shell on page 760 for examples.
The SET statement has no effect until the impala-shell interpreter is connected to
an Impala server. Once you are connected, any query options you set remain in effect as
you issue a subsequent CONNECT command to connect to a different Impala host.
describe Shows the columns, column data types, and any column comments for a specified
table. DESCRIBE FORMATTED shows additional information such as the HDFS data
directory, partitions, and internal properties for the table. See DESCRIBE Statement
on page 258 for details about the basic DESCRIBE output and the DESCRIBE
FORMATTED variant. You can use DESC as shorthand for the DESCRIBE command.
drop Removes a schema object, and in some cases its associated data files. See DROP
TABLE Statement on page 278, DROP VIEW Statement on page 280, DROP
DATABASE Statement on page 270, and DROP FUNCTION Statement on page
272 for details.
explain Provides the execution plan for a query. EXPLAIN represents a query as a series of
steps. For example, these steps might be map/reduce stages, metastore operations,
or file system operations such as move or rename. See EXPLAIN Statement on page
281 and Using the EXPLAIN Plan for Performance Tuning on page 631 for
details.
history Maintains an enumerated cross-session command history. This history is stored in the
~/.impalahistory file.
insert Writes the results of a query to a specified table. This either overwrites table data or
appends data to the existing table content. See INSERT Statement on page 288 for
details.
| Impala Client Access | 764
Command Explanation
invalidate Updates impalad metadata for table existence and structure. Use this command after
metadata creating, dropping, or altering databases, tables, or partitions in Hive. See INVALIDATE
METADATA Statement on page 297 for details.
profile Displays low-level information about the most recent query. Used for performance
diagnosis and tuning. The report starts with the same information as produced by the
EXPLAIN statement and the SUMMARY command. See Using the Query Profile for
Performance Tuning on page 633 for details.
quit Exits the shell. Remember to include the final semicolon so that the shell recognizes the
end of the command.
refresh Refreshes impalad metadata for the locations of HDFS blocks corresponding to
Impala data files. Use this command after loading new data files into an Impala table
through Hive or through HDFS commands. See REFRESH Statement on page 302
for details.
rerun or @ Executes a previous impala-shell command again, from the list of commands
displayed by the history command. These could be SQL statements, or commands
specific to impala-shell such as quit or profile.
Specify an integer argument. A positive integer N represents the command labelled
N in the output of the HISTORY command. A negative integer -N represents the Nth
command from the end of the list, such as -1 for the most recent command. Commands
that are executed again do not produce new entries in the HISTORY output list.
select Specifies the data set on which to complete some action. All information returned
from select can be sent to some output such as the console or a file or can be used
to complete some other element of query. See SELECT Statement on page 306 for
details.
set Manages query options for an impala-shell session. The available options are the
ones listed in Query Options for the SET Statement on page 336. These options are
used for query tuning and troubleshooting. Issue SET with no arguments to see the
current query options, either based on the impalad defaults, as specified by you at
impalad startup, or based on earlier SET statements in the same session. To modify
option values, issue commands with the syntax set option=value. To restore an
option to its default, use the unset command.
The SET statement has no effect until the impala-shell interpreter is connected to
an Impala server. Once you are connected, any query options you set remain in effect as
you issue a subsequent CONNECT command to connect to a different Impala host.
In Impala 2.0 and later, SET is available as a SQL statement for any kind of application
as well as in impala-shell. See SET Statement on page 335 for details.
shell Executes the specified command in the operating system shell without exiting
impala-shell. You can use the ! character as shorthand for the shell command.
Note: Quote any instances of the -- or /* tokens to avoid them being interpreted as
the start of a comment. To embed comments within source or ! commands, use the
shell comment character # before the comment portion of the line.
| Impala Client Access | 765
Command Explanation
show Displays metastore data for schema objects created and accessed through Impala,
Hive, or both. show can be used to gather information about objects such as databases,
tables, and functions. See SHOW Statement on page 381 for details.
source or src Executes one or more statements residing in a specified file from the local filesystem.
Allows you to perform the same kinds of batch operations as with the -f option, but
interactively within the interpreter. The file can contain SQL statements and other
impala-shell commands, including additional SOURCE commands to perform a
flexible sequence of actions. Each command or statement, except the last one in the file,
must end with a semicolon. See Running Commands and SQL Statements in impala-
shell on page 762 for examples.
summary Summarizes the work performed in various stages of a query. It provides a higher-level
view of the information displayed by the EXPLAIN command. Added in Impala 1.4.0.
See Using the SUMMARY Report for Performance Tuning on page 632 for details
about the report format and how to interpret it.
In Impala 2.3 and higher, you can see a continuously updated report of the summary
information while a query is in progress. See LIVE_SUMMARY Query Option (Impala
2.3 or higher only) on page 352 for details.
unset Removes any user-specified value for a query option and returns the option to its
default value. See Query Options for the SET Statement on page 336 for the
available query options.
In Impala 2.5 and higher, it can also remove user-specified substitution variables using
the notation UNSET VAR:variable_name.
use Indicates the database against which to execute subsequent commands. Lets you avoid
using fully qualified names when referring to tables in databases other than default.
See USE Statement on page 406 for details. Not effective with the -q option,
because that option only allows a single statement in the argument.
• Verifying the communication port where the Impala daemons in your cluster are listening for incoming JDBC
requests.
• Installing the JDBC driver on every system that runs the JDBC-enabled application.
• Specifying a connection string for the JDBC application to access one of the servers running the impalad
daemon, with the appropriate security settings.
Make sure the port for the protocol you are using is available for communication with clients, for example, that it is
not blocked by firewall software.
If your JDBC client software connects to a different port, specify that alternative port number with the flag in the
above table when starting the impalad.
commons-logging-X.X.X.jar
hadoop-common.jar
hive-common-X.XX.X.jar
hive-jdbc-X.XX.X.jar
hive-metastore-X.XX.X.jar
hive-service-X.XX.X.jar
httpclient-X.X.X.jar
httpcore-X.X.X.jar
libfb303-X.X.X.jar
libthrift-X.X.X.jar
log4j-X.X.XX.jar
slf4j-api-X.X.X.jar
| Impala Client Access | 767
slf4j-logXjXX-X.X.X.jar
To enable JDBC support for Impala on the system where you run the JDBC application:
1. Download the JAR files listed above to each client machine.
Note: For Maven users, see this sample github page for an example of the dependencies you could add to a pom
file instead of downloading the individual JARs.
2. Store the JAR files in a location of your choosing, ideally a directory already referenced in your CLASSPATH
setting. For example:
• On Linux, you might use a location such as /opt/jars/.
• On Windows, you might use a subdirectory underneath C:\Program Files.
3. To successfully load the Impala JDBC driver, client programs must be able to locate the associated JAR files.
This often means setting the CLASSPATH for the client process to include the JARs. Consult the documentation
for your JDBC client for more details on how to install new JDBC drivers, but some examples of how to set
CLASSPATH variables include:
• On Linux, if you extracted the JARs to /opt/jars/, you might issue the following command to prepend the
JAR files path to an existing classpath:
export CLASSPATH=/opt/jars/*.jar:$CLASSPATH
• On Windows, use the System Properties control panel item to modify the Environment Variables for your
system. Modify the environment variables to include the path to which you extracted the files.
Note: If the existing CLASSPATH on your client machine refers to some older version of the Hive JARs,
ensure that the new JARs are the first ones listed. Either put the new JAR files earlier in the listings, or delete
the other references to Hive JAR files.
jdbc:hive2://myhost.example.com:21050/;auth=noSasl
To connect to an instance of Impala that requires Kerberos authentication, use a connection string of the form
jdbc:hive2://host:port/;principal=principal_name. The principal must be the same user
principal you used when starting Impala. For example, you might use:
jdbc:hive2://myhost.example.com:21050/;principal=impala/
[email protected]
| Impala Client Access | 768
To connect to an instance of Impala that requires LDAP authentication, use a connection string of the form
jdbc:hive2://host:port/db_name;user=ldap_userid;password=ldap_password. For
example, you might use:
jdbc:hive2://myhost.example.com:21050/test_db;user=fred;password=xyz123
To connect to an instance of Impala over HTTP, specify the HTTP port, 28000 by default, and
transportMode=http in the connection string. For example:
jdbc:hive2://myhost.example.com:28000/;transportMode=http
Note:
Prior to Impala 2.5, the Hive JDBC driver did not support connections that use both Kerberos authentication and SSL
encryption. If your cluster is running an older release that has this restriction, use an alternative JDBC driver that
supports both of these security features.
Notes about JDBC and ODBC Interaction with Impala SQL Features
Most Impala SQL features work equivalently through the impala-shell interpreter of the JDBC or ODBC APIs.
The following are some exceptions to keep in mind when switching between the interactive shell and applications
using the APIs:
• Complex type considerations:
• Queries involving the complex types (ARRAY, STRUCT, and MAP) require notation that might not be available
in all levels of JDBC and ODBC drivers. If you have trouble querying such a table due to the driver level or
inability to edit the queries used by the application, you can create a view that exposes a “flattened” version of
the complex columns and point the application at the view. See Complex Types (Impala 2.3 or higher only) on
page 134 for details.
• The complex types available in Impala 2.3 and higher are supported by the JDBC getColumns() API. Both
MAP and ARRAY are reported as the JDBC SQL Type ARRAY, because this is the closest matching Java SQL
type. This behavior is consistent with Hive. STRUCT types are reported as the JDBC SQL Type STRUCT.
To be consistent with Hive's behavior, the TYPE_NAME field is populated with the primitive type name
for scalar types, and with the full toSql() for complex types. The resulting type names are somewhat
inconsistent, because nested types are printed differently than top-level types. For example, the following list
shows how toSQL() for Impala types are translated to TYPE_NAME values:
Troubleshooting Impala
Troubleshooting for Impala requires being able to diagnose and debug problems with performance, network
connectivity, out-of-memory conditions, disk space usage, and crash or hang conditions in any of the Impala-related
daemons.
The KRPC implementation in Impala 2.12 / 3.0 greatly reduces thread counts and the chances of hitting a resource
limit.
If you still get an error similar to the above in Impala 3.0 and higher, try increasing the max_map_count OS virtual
memory parameter. max_map_count defines the maximum number of memory map areas that a process can use.
Configure each host running an impalad daemon with the command to increase max_map_count to 8 GB.
To make the above settings durable, refer to your OS documentation. For example, on RHEL 6.x:
1. Add the following line to /etc/sysctl.conf:
vm.max_map_count=8000000
2. Run the following command:
sysctl -p
| Troubleshooting Impala | 770
On modern hardware, a throughput rate of less than 100 MB/s typically indicates a performance issue with the storage
device. Correct the hardware problem before continuing with Impala tuning or benchmarking.
Joins fail to There may be insufficient memory. During Start by gathering statistics with the
complete. a join, data from the second, third, and so on COMPUTE STATS statement for each table
sets to be joined is loaded into memory. If involved in the join. Consider specifying the
Impala chooses an inefficient join order or [SHUFFLE] hint so that data from the joined
join mechanism, the query could exceed the tables is split up between nodes rather than
total memory available. broadcast to each node. If tuning at the SQL
level is not sufficient, add more memory to
your system or join smaller data sets.
| Troubleshooting Impala | 771
Queries return Impala metadata may be outdated after Where possible, use the appropriate Impala
incorrect changes are performed in Hive. statement (INSERT, LOAD DATA, CREATE
results. TABLE, ALTER TABLE, COMPUTE
STATS, and so on) rather than switching back
and forth between Impala and Hive. Impala
automatically broadcasts the results of DDL
and DML operations to all Impala nodes
in the cluster, but does not automatically
recognize when such changes are made
through Hive. After inserting data, adding a
partition, or other operation in Hive, refresh
the metadata for the table as described in
REFRESH Statement on page 302.
Queries are Some impalad instances may not have Ensure Impala is installed on all DataNodes.
slow to return started. Using a browser, connect to Start any impalad instances that are not
results. the host running the Impala state store. running.
Connect using an address of the form
http://hostname:port/metrics.
Note: Replace hostname and port with the
hostname and port of your Impala state store
host machine and web server port. The default
port is 25010.
The number of impalad instances listed
should match the expected number of
impalad instances installed in the cluster.
There should also be one impalad instance
installed on each DataNode.
Queries are Impala may not be configured to use native Ensure Impala is configured to use native
slow to return checksumming. Native checksumming uses checksumming as described in Post-
results. machine-specific instructions to compute Installation Configuration for Impala on page
checksums over HDFS data very quickly. 25.
Review Impala logs. If you find instances
of "INFO util.NativeCodeLoader:
Loaded the native-hadoop"
messages, native checksumming is not
enabled.
Queries are Impala may not be configured to use data Test Impala for data locality tracking and
slow to return locality tracking. make configuration changes as necessary.
results. Information on this process can be found in
Post-Installation Configuration for Impala on
page 25.
| Troubleshooting Impala | 772
Attempts This can be the result of permissions issues. In general, ensure the Impala user has
to complete For example, you could use the Hive shell as sufficient permissions. In the preceding
Impala tasks the hive user to create a table. After creating example, ensure the Impala user has sufficient
such as this table, you could attempt to complete permissions to the table that the Hive user
executing some action, such as an INSERT-SELECT created.
INSERT- on the table. Because the table was created
SELECT using one user and the INSERT-SELECT is
actions attempted by another, this action may fail due
fail. The to permissions issues.
Impala logs
include notes
that files
could not be
opened due
to permission
denied.
Impala fails A large number of databases, tables, Configure the statestore timeout value
to start up, partitions, and so on can require metadata and possibly other settings related to the
with the synchronization, particularly on startup, that frequency of statestore updates and metadata
impalad takes longer than the default timeout for the loading. See Increasing the Statestore
logs referring statestore service. Timeout on page 65 and Scalability
to errors Considerations for the Impala Statestore on
connecting to page 634.
the statestore
service and
attempts to
re-register.
Main Page
The main impalad Web UI page at / lists the following information about the impalad:
• The version of the impalad daemon
The Version section also contains other information, such as when Impala was built and what build flags were
used.
• Process start time
• Hardware information
• OS information
• Process information
• CGroup information
http://impala-server-hostname:port/admission?json
See Admission Control and Query Queuing on page 650 for the description of the properties in admission control.
Catalog Page
The Catalog page of the impalad debug Web UI is at /catalog under the main impalad Web UI.
This page displays a list of databases and associated tables recognized by this instance of impalad. You can use
this page to locate which database a table is in, check the exact spelling of a database or table name, look for identical
| Troubleshooting Impala | 775
table names in multiple databases. The primary debugging use case would be to check if an impalad instance has
knowledge of a particular table that someone expects to be in a particular database.
Hadoop Configuration
The Hadoop Configuration page of the impalad debug Web UI is at /hadoop-varz under the main impalad
Web UI.
This page displays the Hadoop common configurations that Impala is running with.
JMX
The JMX page of the impalad debug Web UI is at /jmx under the main impalad Web UI.
This page displays monitoring information about various JVM subsystems, such as memory pools, thread
management, runtime. etc.
Logs Page
The INFO logs page of the impalad debug Web UI is at /logs under the main impalad Web UI.
This page shows the last portion of the impalad.INFO log file, including the info, warning, and error logs for the
impalad. You can see the details of the most recent operations, whether the operations succeeded or encountered
errors. This page provides one central place for the log files and saves you from looking around the filesystem for the
log files, which could be in different locations on clusters that use cluster management software.
Memz Page
The Memory Usage page of the impalad debug Web UI is at /memz under the main impalad Web UI.
This page displays the summary and detailed information about memory usage by the impalad.
Metrics Page
The Metrics page of the impalad debug Web UI is at /metrics under the main impalad Web UI.
This page displays the current set of metrics, counters and flags representing various aspects of impalad internal
operations.
Queries Page
The Queries page of the impalad debug Web UI is at /queries under the main impalad Web UI.
This page lists:
• Currently running queries
• Queries that have completed their execution, but have not been closed yet
• Completed queries whose details still reside in memory
The queries are listed in reverse chronological order, with the most recent at the top. You can control the amount of
memory devoted to completed queries by specifying the -‑-‑query_log_size startup option for impalad.
This page provides:
• How many SQL statements are failing (State value of EXCEPTION)
• How large the result sets are (# rows fetched)
• How long each statement took (Start Time and End Time)
| Troubleshooting Impala | 776
Each query has an associated link that displays the detailed query profile, which you can examine to understand the
performance characteristics of that query. See Using the Query Profile for Performance Tuning on page 633 for
details.
The Queries page also includes the Query Locations section that lists the number of running queries with fragments
on this host.
Sessions Page
The Sessions page of the impalad debug Web UI is at /session under the main impalad Web UI.
This page displays information about the sessions currently connected to this impalad instance. For example,
sessions could include connections from the impala-shell command, JDBC or ODBC applications, or the Impala
Query UI in the Hue web interface.
Threadz Page
The Threads page of the impalad debug Web UI is at /threadz under the main impalad Web UI.
This page displays information about the threads used by this instance of impalad, and it shows which categories
they are grouped into. Making use of this information requires substantial knowledge about Impala internals.
Varz Page
The Varz page of the impalad debug Web UI is at /varz under the main impalad Web UI.
This page shows the configuration settings in effect when this instance of impalad communicates with other
Hadoop components such as HDFS and YARN. These settings are collected from a set of configuration files.
The bottom of this page also lists all the command-line settings in effect for this instance of impalad. See Modifying
Impala Startup Options on page 31 for information about modifying these values.
Main Page
The main statestored Web UI page at / lists the following information about the statestored:
• The version of the statestored daemon
• Process start time
• Hardware information
• OS information
• Process information
• CGroup information
Logs Page
The INFO logs page of the debug Web UI is at /logs under the main statestored Web UI.
This page shows the last portion of the statestored.INFO log file, including the info, warning, and error logs
for the statestored. You can refer here to see the details of the most recent operations, whether the operations
succeeded or encountered errors. This page provides one central place for the log files and saves you from looking
| Troubleshooting Impala | 777
around the filesystem for the log files, which could be in different locations on clusters that use cluster management
software.
Memz Page
The Memory Usage page of the debug Web UI is at /memz under the main statestored Web UI.
This page displays summary and detailed information about memory usage by the statestored. You can see the
memory limit in effect for the node, and how much of that memory Impala is currently using.
Metrics Page
The Metrics page of the debug Web UI is at /metrics under the main statestored Web UI.
This page displays the current set of metrics: counters and flags representing various aspects of statestored
internal operation.
Subscribers Page
The Subscribers page of the debug Web UI is at /subscribers under the main statestored Web UI.
This page displays information about the other Impala daemons that have registered with the statestored to
receive and send updates.
Threadz Page
The Threads page of the debug Web UI is at /threadz under the main statestored Web UI.
This page displays information about the threads used by this instance of statestored, and shows which
categories they are grouped into. Making use of this information requires substantial knowledge about Impala
internals.
Topics Page
The Topics page of the debug Web UI is at /topics under the main statestored Web UI.
This page displays information about the topics to which the other Impala daemons have registered to receive updates.
Varz Page
The Varz page of the debug Web UI is at /varz under the main statestored Web UI.
This page shows the configuration settings in effect when this instance of statestored communicates with other
Hadoop components such as HDFS and YARN. These settings are collected from a set of configuration files.
The bottom of this page also lists all the command-line settings in effect for this instance of statestored. See
Modifying Impala Startup Options on page 31 for information about modifying these values.
Main Page
The main catalogd Web UI page at / lists the following information about the catalogd:
• The version of the catalogd daemon
• Process start time
| Troubleshooting Impala | 778
• Hardware information
• OS information
• Process information
• CGroup information
Catalog Page
The Catalog page of the debug Web UI is at /catalog under the main catalogd Web UI.
This page displays a list of databases and associated tables recognized by this instance of catalogd. You can use
this page to locate which database a table is in, check the exact spelling of a database or table name, look for identical
table names in multiple databases. The catalog information is represented as the underlying Thrift data structures.
JMX
The JMX page of the catalogd debug Web UI is at /jmx under the main catalogd Web UI.
This page displays monitoring information about various JVM subsystems, such as memory pools, thread
management, runtime. etc.
Logs Page
The INFO logs page of the debug Web UI is at /logs under the main catalogd Web UI.
This page shows the last portion of the catalogd.INFO log file, including the info, warning, and error logs for
the catalogd daemon. You can refer here to see the details of the most recent operations, whether the operations
succeeded or encountered errors. This page provides one central place for the log files and saves you from looking
around the filesystem for the log files, which could be in different locations on clusters that use cluster management
software.
Memz Page
The Memory Usage page of the debug Web UI is at /memz under the main catalogd Web UI.
This page displays summary and detailed information about memory usage by the catalogd. You can see the
memory limit in effect for the node, and how much of that memory Impala is currently using.
Metrics Page
The Metrics page of the debug Web UI is at /metrics under the main catalogd Web UI.
This page displays the current set of metrics: counters and flags representing various aspects of catalogd internal
operation.
Threadz Page
The Threads page of the debug Web UI is at /threadz under the main catalogd Web UI.
This page displays information about the threads used by this instance of catalogd, and shows which categories
they are grouped into. Making use of this information requires substantial knowledge about Impala internals.
| Troubleshooting Impala | 779
Varz Page
The Varz page of the debug Web UI is at /varz under the main catalogd Web UI.
This page shows the configuration settings in effect when this instance of catalogd communicates with other
Hadoop components such as HDFS and YARN. These settings are collected from a set of configuration files.
The bottom of this page also lists all the command-line settings in effect for this instance of catalogd. See
Modifying Impala Startup Options on page 31 for information about modifying these values.
# ps ax | grep impalad
23114 ? Sl 0:18 /opt/local/parcels/<parcel_version>/lib/impala/
sbin/impalad --flagfile=/var/run/impala/process/114-impala-IMPALAD/impala-
conf/impalad_flags
31259 pts/0 S+ 0:00 grep impalad
#
# kill -11 23114
#
# ps ax | grep impalad
31374 ? Rl 0:04 /opt/local/parcels/<parcel_version>/lib/impala/
sbin/impalad --flagfile=/var/run/impala/process/114-impala-IMPALAD/impala-
conf/impalad_flags
31475 pts/0 S+ 0:00 grep impalad
We locate the log directory underneath /var/log. There is a .INFO, .WARNING, and .ERROR log file for the
23114 process ID. The minidump message is written to the .INFO file and the .ERROR file, but not the .WARNING
file. In this case, a large core file was also produced.
# cd /var/log/impalad
# ls -la | grep 23114
-rw------- 1 impala impala 3539079168 Jun 23 15:20 core.23114
-rw-r--r-- 1 impala impala 99057 Jun 23 15:20 hs_err_pid23114.log
-rw-r--r-- 1 impala impala 351 Jun 23 15:20
impalad.worker_node_123.impala.log.ERROR.20160623-140343.23114
-rw-r--r-- 1 impala impala 29101 Jun 23 15:20
impalad.worker_node_123.impala.log.INFO.20160623-140343.23114
-rw-r--r-- 1 impala impala 228 Jun 23 14:03
impalad.worker_node_123.impala.log.WARNING.20160623-140343.23114
The .INFO log includes the location of the minidump file, followed by a report of a core dump. With the breakpad
minidump feature enabled, now we might disable core dumps or keep fewer of them around.
# cat impalad.worker_node_123.impala.log.INFO.20160623-140343.23114
...
Wrote minidump to /var/log/impala-minidumps/impalad/0980da2d-
a905-01e1-25ff883a-04ee027a.dmp
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00000030c0e0b68a, pid=23114, tid=139869541455968
#
# JRE version: Java(TM) SE Runtime Environment (7.0_67-b01) (build 1.7.0_67-
b01)
| Ports Used by Impala | 781
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.65-b04 mixed mode linux-
amd64 compressed oops)
# Problematic frame:
# C [libpthread.so.0+0xb68a] pthread_cond_wait+0xca
#
# Core dump written. Default location: /var/log/impalad/core or core.23114
#
# An error report file with more information is saved as:
# /var/log/impalad/hs_err_pid23114.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
...
# cat impalad.worker_node_123.impala.log.ERROR.20160623-140343.23114
The resulting minidump file is much smaller than the corresponding core file, making it much easier to supply
diagnostic information to the appropriate support channel.
# pwd
/var/log/impalad
# cd ../impala-minidumps/impalad
# ls
0980da2d-a905-01e1-25ff883a-04ee027a.dmp
# du -kh *
2.4M 0980da2d-a905-01e1-25ff883a-04ee027a.dmp
Impala Daemon Impala Daemon Frontend 21000 External Used to transmit commands
Port and receive results by
impala-shell and
some ODBC drivers via the
Beeswax protocol.
| Ports Used by Impala | 782
Impala Daemon Impala Daemon Frontend 21050 External Used to transmit commands
Port and receive results by
client applications over
binary, such as Business
Intelligence tools, using
JDBC, the Beeswax query
editor in Hue, and some
ODBC drivers.
Impala Daemon Impala Daemon Backend 22000 Internal Internal use only. Impala
Port daemons use this port for
Thrift based communication
with each other.
Impala Daemon StateStoreSubscriber Service 23000 Internal Internal use only. Impala
Port daemons listen on this
port for updates from the
statestore daemon.
Catalog Daemon StateStoreSubscriber Service 23020 Internal Internal use only. The
Port catalog daemon listens on
this port for updates from
the statestore daemon.
Impala Daemon Impala Daemon HTTP 25000 External Impala web interface for
Server Port administrators to monitor
and troubleshoot.
Impala StateStore StateStore HTTP Server Port 25010 External StateStore web interface for
Daemon administrators to monitor
and troubleshoot.
Impala Catalog Catalog HTTP Server Port 25020 External Catalog service web
Daemon interface for administrators
to monitor and troubleshoot.
New in Impala 1.2 and
higher.
Impala StateStore StateStore Service Port 24000 Internal Internal use only. The
Daemon statestore daemon listens
on this port for registration/
unregistration requests.
Impala Catalog Catalog Service Port 26000 Internal Internal use only. The
Daemon catalog service uses this port
to communicate with the
Impala daemons. New in
Impala 1.2 and higher.
| Impala Reserved Words | 783
Impala Daemon HiveServer2 HTTP Port 28000 External Used to transmit commands
and receive results by client
applications over HTTP via
the HiveServer2 protocol.
Impala Llama Llama Thrift Admin Port 15002 Internal Internal use only. New in
ApplicationMaster Impala 1.3 and higher.
Impala Llama Llama Thrift Port 15000 Internal Internal use only. New in
ApplicationMaster Impala 1.3 and higher.
Impala Llama Llama HTTP Port 15001 External Llama service web interface
ApplicationMaster for administrators to monitor
and troubleshoot. New in
Impala 1.3 and higher.
• For those remaining SQL 2016 reserved words, if a word is likely to be in-use by users of older Impala versions
and if there is a low chance of Impala needing to reserve that word in the future, then the word is not reserved.
• Otherwise, the word is reserved in Impala.
abs X
acos X
add X X
aggregate X X
all X X X
allocate X X
alter X X X
analytic X X
and X X X
anti X X
any X X
api_version X
are X X
array X X X
array_agg X X
array_max_cardinality
X X
as X X X
asc X X
asensitive X X
asin X
asymmetric X X
at X X
atan X
atomic X X
authorization X X
avg X
avro X X
| Impala Reserved Words | 785
backup X
begin X X
begin_frame X X
begin_partitionX X
between X X X
bigint X X X
binary X X X
blob X X
block_size X
boolean X X X
both X X
break X
browse X
bulk X
by X X X
cache
cached X X
call X
called X X
cardinality X X
cascade X X
cascaded X X
case X X X
cast X X X
ceil X
ceiling X
change X X
char X X X
char_length X
character X X
character_length
X
check X X
checkpoint X
class X X
classifier X
| Impala Reserved Words | 786
clob X X
close X X
close_fn X
clustered X
coalesce X X
collate X X
collect X X
column X X X
columns X X
comment X X
commit X X
compression X X
compute X X
condition X X
conf
connect X X
constraint X X
contains X X
continue X
convert X X
copy X X
corr X X
corresponding X X
cos X
cosh X
count X
covar_pop X X
covar_samp X X
create X X X
cross X X X
cube X X
cume_dist X
current X X X
current_catalogX
current_date X X
| Impala Reserved Words | 787
current_default_transform_group
X X
current_path X X
current_role X X
current_row X X
current_schema X X
current_time X X
current_timestamp
X X
current_transform_group_for_type
X X
current_user X X
cursor X X
cycle X X
data X X
database X X
databases X X
date X X X
datetime X X
day X
dayofweek
dbcc X
deallocate X X
dec X X
decfloat X X
decimal X X X
declare X X
default X X X
define X X
delete X X X
delimited X X
dense_rank X
deny X
deref X X
desc X X
describe X X X
deterministic X X
disconnect X X
| Impala Reserved Words | 788
disk X
distinct X X X
distributed X
div X X
double X X X
drop X X X
dump X
dynamic X X
each X X
element X X
else X X X
empty X X
encoding X X
end X X X
end-exec X X
end_frame X X
end_partition X X
equals X X
errlvl X
escape X X
escaped X X
every X X
except X X
exchange
exec X X
execute X X
exists X X X
exit X
exp X
explain X X
extended X X
external X X X
extract X
false X X X
fetch X X
| Impala Reserved Words | 789
fields X X
file X
filefactor X
fileformat X X
files X X
filter X X
finalize_fn X
first X X
first_value X
float X X X
floor X
following X X
for X X X
foreign X X
format X X
formatted X X
frame_row X X
free X X
freetext X
from X X X
full X X X
function X X X
functions X X
fusion X X
get X X
global X X
goto X
grant X X X
group X X X
grouping X X
groups X X
hash X X
having X X X
hold X X
holdlock X
| Impala Reserved Words | 790
hour X
identity X X
if X X
ignore X X
ilike X X
import
in X X X
incremental X X
index X
indicator X X
init_fn X
initial X X
inner X X X
inout X X
inpath X X
insensitive X X
insert X X X
int X X X
integer X X X
intermediate X X
intersect X X
intersection X X
interval X X X
into X X X
invalidate X X
iregexp X X
is X X X
join X X X
json_array X X
json_arrayagg X X
json_exists X X
json_object X X
json_objectagg X X
json_query X X
json_table X X
| Impala Reserved Words | 791
json_table_primitive
X X
json_value X X
key X
kill X
kudu X X
lag X
language X
large X X
last X X
last_value X
lateral X X
lead X
leading X X
left X X X
less
like X X X
like_regex X X
limit X X
lineno X
lines X X
listagg X X
ln X
load X X
local X X
localtime X
localtimestamp X X
location X X
log X
log10 X X
lower X
macro
map X X
match X X
match_number X X
match_recognizeX X
| Impala Reserved Words | 792
matches X X
max X
member X
merge X X
merge_fn X
metadata X X
method X X
min X
minute X
mod X
modifies X X
module X
month X
more
multiset X X
national X X
natural X X
nchar X X
nclob X X
new X
no X X
nocheck X
nonclustered X
none X X
normalize X X
not X X X
nth_value X X
ntile X
null X X X
nullif X X
nulls X X
numeric X X
occurrences_regex
X X
octet_length X X
of X X
| Impala Reserved Words | 793
off X
offset X X X
offsets X
old X
omit X X
on X X X
one X X
only X X
open X X
option X
or X X X
order X X X
out X X
outer X X X
over X X X
overlaps X X
overlay X X
overwrite X X
parameter X
parquet X X
parquetfile X X
partialscan
partition X X X
partitioned X X
partitions X X
pattern X X
per X X
percent X X
percent_rank X
percentile_contX X
percentile_discX X
period X
pivot X
plan X
portion X X
| Impala Reserved Words | 794
position X X
position_regex X X
power X
precedes X X
preceding X X
precision X X
prepare X X
prepare_fn X
preserve
primary X X X
print X
proc X
procedure X X
produced X X
ptf X X
public X
purge X X
raiseerror X
range X X X
rank X
rcfile X X
read X
reads X X
readtext X
real X X X
reconfigure X
recover X X
recursive X X
reduce
ref X X
references X X
referencing X X
refresh X X
regexp X X
regr_avgx X X
| Impala Reserved Words | 795
regr_avgy X X
regr_count X X
regr_intercept X X
regr_r2 X X
regr_slope X X
regr_sxx X X
regr_sxy X X
regr_syy X X
release X X
rename X X
repeatable X X
replace X X
replication X X
restore X
restrict X X
result X
return X X
returns X X X
revert X
revoke X X X
right X X X
rlike X X
role X X
roles X X
rollback X X
rollup X X
row X X X
row_number X
rowcount X
rows X X X
rule X
running X X
save X
savepoint X X
schema X X
| Impala Reserved Words | 796
schemas X X
scope X X
scroll X X
search X X
second X
securityaudit X
seek X X
select X X X
semi X X
sensitive X X
sequencefile X X
serdeproperties X X
serialize_fn X
session_user X X
set X X X
setuser X
show X X X
shutdown X
similar X X
sin X
sinh X
skip X X
smallint X X X
some X X
sort X X
specific X X
specifictype X X
sql X
sqlexception X X
sqlstate X X
sqlwarning X X
sqrt X
start X
static X X
statistics X
| Impala Reserved Words | 797
stats X X
stddev_pop X
stddev_samp X
stored X X
straight_join X
string X X
struct X X
submultiset X X
subset X X
substring X
substring_regexX X
succeeds X X
sum X
symbol X X
symmetric X X
system X
system_time X X
system_user X X
table X X X
tables X X
tablesample X X X
tan X
tanh X
tblproperties X X
terminated X X
textfile X X
textsize X
then X X X
time X
timestamp X X X
timezone_hour X X
timezone_minuteX X
tinyint X X
to X X X
top X
| Impala Reserved Words | 798
trailing X X
tran X
transform
translate X
translate_regexX X
translation X X
treat X X
trigger X X
trim X
trim_array X X
true X X X
truncate X X X
try_convert X
uescape X X
unbounded X X
uncached X X
union X X X
unique X X
uniquejoin
unknown X X X
unnest X X
unpivot X
update X X X
update_fn X
updatetext X
upper X
upsert X X
use X X
user X X
using X X X
utc_tmestamp
value X
value_of X X
values X X X
var_pop X
| Impala Frequently Asked Questions | 799
var_samp X
varbinary X X
varchar X X X
varying X X
versioning X X
view X X
views
waitfor X
when X X X
whenever X X
where X X X
while X
width_bucket X X
window X X
with X X X
within X X
without X X
writetext X
year X
• Time-based and memory-based automatic invalidation of metadata to keep the size of metadata bounded and to
reduce the chances of catalogdcache running out of memory.
• Automatic invalidation of metadata
With automatic metadata management enabled, you no longer have to issue INVALIDATE / REFRESH in a
number of conditions.
In Impala 3.3, the following additional event in Hive Metastore can trigger automatic INVALIDATE / REFRESH
of Metadata:
• INSERT into tables and partitions from Impala or from Spark on the same or multiple cluster configuration
See Metadata Management on page 646 for the information on the above features.
Query Profile
The following information was added to the Query Profile output for better monitoring and troubleshooting of query
performance.
• Network I/O throughput
• System disk I/O throughput
See Impala Query Profile for generating and reading query profile.
For backward compatibility, you can use the DEFAULT_FILE_FORMAT query option to set the default file format
to the previous default, text, or other formats.
Ubuntu 18.04
This version of Impala is certified to run on Ubuntu 18.04.
Multi-cluster Support
• Remote File Handle Cache
Impala can now cache remote HDFS file handles when the cache_remote_file_handles impalad flag is
set to true. This feature does not apply to non-HDFS tables, such as Kudu or HBase tables, and does not apply
to the tables that store their data on cloud services, such as S3 or ADLS. See Scalabilty Considerations for file
handle caching in Impala.
Security Enhancement
• The REFRESH AUTHORIZATION statement was implemented for refreshing authorization data.
• A new function, replace(), which is faster than regexp_replace() for simple string substitutions. See
Impala String Functions on page 488 for details.
• Startup flags for the impalad daemon, is_executor and is_coordinator, let you divide the work on
a large, busy cluster between a small number of hosts acting as query coordinators, and a larger number of hosts
acting as query executors. By default, each host can act in both roles, potentially introducing bottlenecks during
heavily concurrent workloads. See How to Configure Impala with Dedicated Coordinators on page 642 for
details.
• The DELETE statement is a flexible way to remove data from a Kudu table. Previously, removing data from an
Impala table involved removing or rewriting the underlying data files, dropping entire partitions, or rewriting
the entire table. This Impala statement only works for Kudu tables.
• The UPDATE statement is a flexible way to modify data within a Kudu table. Previously, updating data in an
Impala table involved replacing the underlying data files, dropping entire partitions, or rewriting the entire
table. This Impala statement only works for Kudu tables.
• The UPSERT statement is a flexible way to ingest, modify, or both data within a Kudu table. Previously,
ingesting data that might contain duplicates involved an inefficient multi-stage operation, and there was
no built-in protection against duplicate data. The UPSERT statement, in combination with the primary key
designation for Kudu tables, lets you add or replace rows in a single operation, and automatically avoids
creating any duplicate data.
• The CREATE TABLE statement gains some new clauses that are specific to Kudu tables: PARTITION
BY, PARTITIONS, STORED AS KUDU, and column attributes PRIMARY KEY, NULL and NOT
NULL, ENCODING, COMPRESSION, DEFAULT, and BLOCK_SIZE. These clauses replace the explicit
TBLPROPERTIES settings that were required in the early experimental phases of integration between Impala
and Kudu.
• The ALTER TABLE statement can change certain attributes of Kudu tables. You can add, drop, or rename
columns. You can add or drop range partitions. You can change the TBLPROPERTIES value to rename
or point to a different underlying Kudu table, independently from the Impala table name in the metastore
database. You cannot change the data type of an existing column in a Kudu table.
• The SHOW PARTITIONS statement displays information about the distribution of data between partitions in
Kudu tables. A new variation, SHOW RANGE PARTITIONS, displays information about the Kudu-specific
partitions that apply across ranges of key values.
• Not all Impala data types are supported in Kudu tables. In particular, currently the Impala TIMESTAMP type
is not allowed in a Kudu table. Impala does not recognize the UNIXTIME_MICROS Kudu type when it is
present in a Kudu table. (These two representations of date/time data use different units and are not directly
compatible.) You cannot create columns of type TIMESTAMP, DECIMAL, VARCHAR, or CHAR within a Kudu
table. Within a query, you can cast values in a result set to these types. Certain types, such as BOOLEAN,
cannot be used as primary key columns.
• Currently, Kudu tables are not interchangeable between Impala and Hive the way other kinds of Impala tables
are. Although the metadata for Kudu tables is stored in the metastore database, currently Hive cannot access
Kudu tables.
• The INSERT statement works for Kudu tables. The organization of the Kudu data makes it more efficient than
with HDFS-backed tables to insert data in small batches, such as with the INSERT ... VALUES syntax.
• Some audit data is recorded for data governance purposes. All UPDATE, DELETE, and UPSERT statements
are characterized as INSERT operations in the audit log. Currently, lineage metadata is not generated for
UPDATE and DELETE operations on Kudu tables.
• Currently, Kudu tables have limited support for Sentry:
• Access to Kudu tables must be granted to roles as usual.
• Currently, access to a Kudu table through Sentry is “all or nothing”. You cannot enforce finer-grained
permissions such as at the column level, or permissions on certain operations such as INSERT.
• Only users with ALL privileges on SERVER can create external Kudu tables.
Because non-SQL APIs can access Kudu data without going through Sentry authorization, currently the Sentry
support is considered preliminary.
• Equality and IN predicates in Impala queries are pushed to Kudu and evaluated efficiently by the Kudu storage
layer.
• Security:
• Impala can take advantage of the S3 encrypted credential store, to avoid exposing the secret key when
accessing data stored on S3.
• [IMPALA-1654] Several kinds of DDL operations can now work on a range of partitions. The partitions can be
specified using operators such as <, >=, and != rather than just an equality predicate applying to a single partition.
This new feature extends the syntax of several clauses of the ALTER TABLE statement (DROP PARTITION,
SET [UN]CACHED, SET FILEFORMAT | SERDEPROPERTIES | TBLPROPERTIES), the SHOW
| Impala Release Notes | 806
FILES statement, and the COMPUTE INCREMENTAL STATS statement. It does not apply to statements that are
defined to only apply to a single partition, such as LOAD DATA, ALTER TABLE ... ADD PARTITION, SET
LOCATION, and INSERT with a static partitioning clause.
• The instr() function has optional second and third arguments, representing the character to position to begin
searching for the substring, and the Nth occurrence of the substring to find.
• Improved error handling for malformed Avro data. In particular, incorrect precision or scale for DECIMAL types is
now handled.
• Impala debug web UI:
• In addition to “inflight” and “finished” queries, the web UI now also includes a section for “queued” queries.
• The /sessions tab now clarifies how many of the displayed sections are active, and lets you sort by Expired
status to distinguish active sessions from expired ones.
• Improved stability when DDL operations such as CREATE DATABASE or DROP DATABASE are run in Hive at
the same time as an Impala INVALIDATE METADATA statement.
• The “out of memory” error report was made more user-friendly, with additional diagnostic information to help
identify the spot where the memory limit was exceeded.
• Improved disk space usage for Java-based UDFs. Temporary copies of the associated JAR files are removed
when no longer needed, so that they do not accumulate across restarts of the catalogd daemon and potentially
cause an out-of-space condition. These temporary files are also created in the directory specified by the
local_library_dir configuration setting, so that the storage for these temporary files can be independent
from any capacity limits on the /tmp filesystem.
• Impala now recognizes the auth_to_local setting, specified through the HDFS configuration setting
hadoop.security.auth_to_local. This feature is disabled by default; to enable it, specify --
load_auth_to_local_rules=true in the impalad configuration settings. See Mapping Kerberos
Principals to Short Names for Impala on page 87 for details.
• Timing improvements in the mechanism for the impalad daemon to acquire Kerberos tickets. This feature
spreads out the overhead on the KDC during Impala startup, especially for large clusters.
• For Kerberized clusters, the Catalog service now uses the Kerberos principal instead of the operating
sytem user that runs the catalogd daemon. This eliminates the requirement to configure a
hadoop.user.group.static.mapping.overrides setting to put the OS user into the Sentry
administrative group, on clusters where the principal and the OS user name for this user are different.
• Overall performance improvements for join queries, by using a prefetching mechanism while building the in-
memory hash table to evaluate join predicates. See PREFETCH_MODE Query Option (Impala 2.6 or higher only)
on page 372 for the query option to control this optimization.
• The impala-shell interpreter has a new command, SOURCE, that lets you run a set of SQL statements or
other impala-shell commands stored in a file. You can run additional SOURCE commands from inside a file,
to set up flexible sequences of statements for use cases such as schema setup, ETL, or reporting. See impala-shell
Command Reference on page 763 for details and Running Commands and SQL Statements in impala-shell on
page 762 for examples.
• The millisecond() built-in function lets you extract the fractional seconds part of a TIMESTAMP value. See
Impala Date and Time Functions on page 457 for details.
• If an Avro table is created without column definitions in the CREATE TABLE statement, and columns are later
added through ALTER TABLE, the resulting table is now queryable. Missing values from the newly added
columns now default to NULL. See Using the Avro File Format with Impala Tables on page 697 for general
details about working with Avro files.
• The mechanism for interpreting DECIMAL literals is improved, no longer going through an intermediate
conversion step to DOUBLE:
• Casting a DECIMAL value to TIMESTAMP DOUBLE produces a more precise value for the TIMESTAMP than
formerly.
• Certain function calls involving DECIMAL literals now succeed, when formerly they failed due to lack of a
function signature with a DOUBLE argument.
• Faster runtime performance for DECIMAL constant values, through improved native code generation for all
combinations of precision and scale.
See DECIMAL Data Type (Impala 3.0 or higher only) on page 103 for details about the DECIMAL type.
• Improved type accuracy for CASE return values. If all WHEN clauses of the CASE expression are of CHAR type,
the final result is also CHAR instead of being converted to STRING. See Impala Conditional Functions on page
481 for details about the CASE function.
• Uncorrelated queries using the NOT EXISTS operator are now supported. Formerly, the NOT EXISTS operator
was only available for correlated subqueries.
• Improved performance for reading Parquet files.
• Improved performance for top-N queries, that is, those including both ORDER BY and LIMIT clauses.
• Impala optionally skips an arbitrary number of header lines from text input files on HDFS based on the
skip.header.line.count value in the TBLPROPERTIES field of the table metadata. See Data Files for
Text Tables on page 675 for details.
• Trailing comments are now allowed in queries processed by the impala-shell options -q and -f.
• Impala can run COUNT queries for RCFile tables that include complex type columns. See Complex Types (Impala
2.3 or higher only) on page 134 for general information about working with complex types, and ARRAY
Complex Type (Impala 2.3 or higher only) on page 95, MAP Complex Type (Impala 2.3 or higher only) on
page 111, and STRUCT Complex Type (Impala 2.3 or higher only) on page 119 for syntax details of each
type.
| Impala Release Notes | 809
• Performance improvements related to code generation. Even in queries where code generation is not performed for
some phases of execution (such as reading data from Parquet tables), Impala can still use code generation in other
parts of the query, such as evaluating functions in the WHERE clause.
• Performance improvements for queries using aggregation functions on high-cardinality columns. Formerly,
Impala could do unnecessary extra work to produce intermediate results for operations such as DISTINCT
or GROUP BY on columns that were unique or had few duplicate values. Now, Impala decides at run time
whether it is more efficient to do an initial aggregation phase and pass along a smaller set of intermediate data,
or to pass raw intermediate data back to next phase of query processing to be aggregated there. This feature is
known as streaming pre-aggregation. In case of performance regression, this feature can be turned off using the
DISABLE_STREAMING_PREAGGREGATIONS query option.
• Spill-to-disk feature now always recommended. In earlier releases, the spill-to-disk feature could be
turned off using a pair of configuration settings, enable_partitioned_aggregation=false and
enable_partitioned_hash_join=false. The latest improvements in the spill-to-disk mechanism, and
related features that interact with it, make this feature robust enough that disabling it is now no longer needed or
supported. In particular, some new features in Impala 2.5 and higher do not work when the spill-to-disk feature is
disabled.
• Improvements to scripting capability for the impala-shell command, through user-specified substitution
variables that can appear in statements processed by impala-shell:
• The --var command-line option lets you pass key-value pairs to impala-shell. The shell can substitute
the values into queries before executing them, where the query text contains the notation ${var:varname}.
For example, you might prepare a SQL file containing a set of DDL statements and queries containing
variables for database and table names, and then pass the applicable names as part of the impala-shell -
f filename command.
• The SET and UNSET commands within the impala-shell interpreter now work with user-specified
substitution variables, as well as the built-in query options. The two kinds of variables are divided in the
SET output. As with variables defined by the --var command-line option, you refer to the user-specified
substitution variables in queries by using the notation ${var:varname} in the query text. Because the
substitution variables are processed by impala-shell instead of the impalad backend, you cannot define
your own substitution variables through the SET statement in a JDBC or ODBC application.
• Performance improvements for query startup. Impala better parallelizes certain work when coordinating plan
distribution between impalad instances, which improves startup time for queries involving tables with many
partitions on large clusters, or complicated queries with many plan fragments.
• Performance and scalability improvements for tables with many partitions. The memory requirements on the
coordinator node are reduced, making it substantially faster and less resource-intensive to do joins involving
several tables with thousands of partitions each.
• Whitelisting for access to internal APIs. For applications that need direct access to Impala APIs, without going
through the HiveServer2 or Beeswax interfaces, you can specify a list of Kerberos users who are allowed to call
those APIs. By default, the impala and hdfs users are the only ones authorized for this kind of access. Any
users not explicitly authorized through the internal_principals_whitelist configuration setting are
blocked from accessing the APIs. This setting applies to all the Impala-related daemons, although currently it is
primarily used for HDFS to control the behavior of the catalog server.
• Improvements to Impala integration and usability for Hue. (The code changes are actually on the Hue side.)
• The list of tables now refreshes dynamically.
• Usability improvements for case-insensitive queries. You can now use the operators ILIKE and IREGEXP to
perform case-insensitive wildcard matches or regular expression matches, rather than explicitly converting column
values with UPPER or LOWER.
• Performance and reliability improvements for DDL and insert operations on partitioned tables with a large
number of partitions. Impala only re-evaluates metadata for partitions that are affected by a DDL operation, not
all partitions in the table. While a DDL or insert statement is in progress, other Impala statements that attempt to
modify metadata for the same table wait until the first one finishes.
• Reliability improvements for the LOAD DATA statement. Previously, this statement would fail if the source HDFS
directory contained any subdirectories at all. Now, the statement ignores any hidden subdirectories, for example
_impala_insert_staging.
| Impala Release Notes | 811
• A new operator, IS [NOT] DISTINCT FROM, lets you compare values and always get a true or false
result, even if one or both of the values are NULL. The IS NOT DISTINCT FROM operator, or its equivalent
<=> notation, improves the efficiency of join queries that treat key values that are NULL in both tables as equal.
• Security enhancements for the impala-shell command. A new option, --ldap_password_cmd, lets
you specify a command to retrieve the LDAP password. The resulting password is then used to authenticate the
impala-shell command with the LDAP server.
• The CREATE TABLE AS SELECT statement now accepts a PARTITIONED BY clause, which lets you create
a partitioned table and insert data into it with a single statement.
• User-defined functions (UDFs and UDAFs) written in C++ now persist automatically when the catalogd
daemon is restarted. You no longer have to run the CREATE FUNCTION statements again after a restart.
• User-defined functions (UDFs) written in Java can now persist when the catalogd daemon is restarted, and
can be shared transparently between Impala and Hive. You must do a one-time operation to recreate these UDFs
using new CREATE FUNCTION syntax, without a signature for arguments or the return value. Afterwards, you
no longer have to run the CREATE FUNCTION statements again after a restart. Although Impala does not have
visibility into the UDFs that implement the Hive built-in functions, user-created Hive UDFs are now automatically
available for calling through Impala.
• Reliability enhancements for memory management. Some aggregation and join queries that formerly might
have failed with an out-of-memory error due to memory contention, now can succeed using the spill-to-disk
mechanism.
• The SHOW DATABASES statement now returns two columns rather than one. The second column includes
the associated comment string, if any, for each database. Adjust any application code that examines the list of
databases and assumes the result set contains only a single column.
• A new optimization speeds up aggregation operations that involve only the partition key columns of partitioned
tables. For example, a query such as SELECT COUNT(DISTINCT k), MIN(k), MAX(k) FROM t1 can
avoid reading any data files if T1 is a partitioned table and K is one of the partition key columns. Because this
technique can produce different results in cases where HDFS files in a partition are manually deleted or are empty,
you must enable the optimization by setting the query option OPTIMIZE_PARTITION_KEY_SCANS.
• The DESCRIBE statement can now display metadata about a database, using the syntax DESCRIBE DATABASE
db_name.
• The uuid() built-in function generates an alphanumeric value that you can use as a guaranteed unique identifier.
The uniqueness applies even across tables, for cases where an ascending numeric sequence is not suitable.
• Performance improvements for querying Parquet data files containing multiple row groups and multiple data
blocks:
• For files written by Hive, SparkSQL, and other Parquet MR writers and spanning multiple HDFS blocks,
Impala now scans the extra data blocks locally when possible, rather than using remote reads.
• Impala queries benefit from the improved alignment of row groups with HDFS blocks for Parquet files written
by Hive, MapReduce, and other components. (Impala itself never writes multiblock Parquet files, so the
alignment change does not apply to Parquet files produced by Impala.) These Parquet writers now add padding
to Parquet files that they write to align row groups with HDFS blocks. The parquet.writer.max-
padding setting specifies the maximum number of bytes, by default 8 megabytes, that can be added to the
file between row groups to fill the gap at the end of one block so that the next row group starts at the beginning
of the next block. If the gap is larger than this size, the writer attempts to fit another entire row group in
the remaining space. Include this setting in the hive-site configuration file to influence Parquet files
written by Hive, or the hdfs-site configuration file to influence Parquet files written by all non-Impala
components.
• Many new built-in scalar functions, for convenience and enhanced portability of SQL that uses common industry
extensions.
Math functions:
• ATAN2
• COSH
• COT
• DCEIL
• DEXP
• DFLOOR
• DLOG10
• DPOW
• DROUND
• DSQRT
• DTRUNC
• FACTORIAL, and corresponding ! operator
• FPOW
• RADIANS
• RANDOM
• SINH
• TANH
String functions:
• BTRIM
• CHR
• REGEXP_LIKE
• SPLIT_PART
Date and time functions:
• INT_MONTHS_BETWEEN
• MONTHS_BETWEEN
• TIMEOFDAY
• TIMESTAMP_CMP
Bit manipulation functions:
• BITAND
• BITNOT
• BITOR
| Impala Release Notes | 813
• BITXOR
• COUNTSET
• GETBIT
• ROTATELEFT
• ROTATERIGHT
• SETBIT
• SHIFTLEFT
• SHIFTRIGHT
Type conversion functions:
• TYPEOF
The effective_user() function.
• New built-in analytic functions: PERCENT_RANK, NTILE, CUME_DIST.
• The DROP DATABASE statement now works for a non-empty database. When you specify the optional CASCADE
clause, any tables in the database are dropped before the database itself is removed.
• The DROP TABLE and ALTER TABLE DROP PARTITION statements have a new optional keyword, PURGE.
This keyword causes Impala to immediately remove the relevant HDFS data files rather than sending them to the
HDFS trashcan. This feature can help to avoid out-of-space errors on storage devices, and to avoid files being
left behind in case of a problem with the HDFS trashcan, such as the trashcan not being configured or being in a
different HDFS encryption zone than the data files.
• The impala-shell command has a new feature for live progress reporting. This feature is enabled through
the --live_progress and --live_summary command-line options, or during a session through the
LIVE_SUMMARY and LIVE_PROGRESS query options.
• The impala-shell command also now displays a random “tip of the day” when it starts.
• The impala-shell option -f now recognizes a special filename - to accept input from stdin.
• Format strings for the unix_timestamp() function can now include numeric timezone offsets.
• Impala can now run a specified command to obtain the password to decrypt a private-key PEM file, rather than
having the private-key file be unencrypted on disk.
• Impala components now can use SSL for more of their internal communication. SSL is used for communication
between all three Impala-related daemons when the configuration option ssl_server_certificate
is enabled. SSL is used for communication with client applications when the configuration option
ssl_client_ca_certificate is enabled.
Currently, you can only use one of server-to-server TLS/SSL encryption or Kerberos authentication. This
limitation is tracked by the issue IMPALA-2598.
• Improved flexibility for intermediate data types in user-defined aggregate functions (UDAFs).
In Impala 2.3.2, the bug fix for IMPALA-2598 removes the restriction on using both Kerberos and SSL for internal
communication between Impala components.
the local time zone instead of UTC. To enable these features, set the impalad startup
flags -use_local_tz_for_unix_timestamp_conversions=true and -
convert_legacy_hive_parquet_utc_timestamps=true.
The -use_local_tz_for_unix_timestamp_conversions setting controls how the
unix_timestamp(), from_unixtime(), and now() functions handle time zones. By default
(when this setting is turned off), Impala considers all TIMESTAMP values to be in the UTC time zone when
converting to or from Unix time values. When this setting is enabled, Impala treats TIMESTAMP values passed
to or returned from these functions to be in the local time zone. When this setting is enabled, take particular
care that all hosts in the cluster have the same timezone settings, to avoid inconsistent results depending on
which host reads or writes TIMESTAMP data.
The -convert_legacy_hive_parquet_utc_timestamps setting causes Impala to convert
TIMESTAMP values to the local time zone when it reads them from Parquet files written by Hive. This setting
only applies to data using the Parquet file format, where Impala can use metadata in the files to reliably
determine that the files were written by Hive. If in the future Hive changes the way it writes TIMESTAMP data
in Parquet, Impala will automatically handle that new TIMESTAMP encoding.
See TIMESTAMP Data Type on page 125 for details about time zone handling and the configuration options
for Impala / Hive compatibility with Parquet format.
• In Impala 2.2.0 and higher, built-in functions that accept or return integers representing TIMESTAMP values
use the BIGINT type for parameters and return values, rather than INT. This change lets the date and time
functions avoid an overflow error that would otherwise occur on January 19th, 2038 (known as the “Year 2038
problem” or “Y2K38 problem”). This change affects the FROM_UNIXTIME() and UNIX_TIMESTAMP()
functions. You might need to change application code that interacts with these functions, change the types of
columns that store the return values, or add CAST() calls to SQL statements that call these functions.
See Impala Date and Time Functions on page 457 for the current function signatures.
• The SHOW FILES statement lets you view the names and sizes of the files that make up an entire table or a
specific partition. See SHOW FILES Statement on page 382 for details.
• Impala can now run queries against Parquet data containing columns with complex or nested types, as long as the
query only refers to columns with scalar types.
• Performance improvements for queries that include IN() operators and involve partitioned tables.
• The new -max_log_files configuration option specifies how many log files to keep at each severity level.
The default value is 10, meaning that Impala preserves the latest 10 log files for each severity level (INFO,
WARNING, and ERROR) for each Impala-related daemon (impalad, statestored, and catalogd). Impala
checks to see if any old logs need to be removed based on the interval specified in the logbufsecs setting,
every 5 seconds by default. See Rotating Impala Logs on page 750 for details.
• Redaction of sensitive data from Impala log files. This feature protects details such as credit card numbers or
tax IDs from administrators who see the text of SQL statements in the course of monitoring and troubleshooting
a Hadoop cluster. See Redacting Sensitive Information from Impala Log Files on page 752 for background
information for Impala users, and the documentation for your Apache Hadoop distribution for usage details.
• Lineage information is available for data created or queried by Impala. This feature lets you track who has
accessed data through Impala SQL statements, down to the level of specific columns, and how data has been
propagated between tables. See Viewing Lineage Information for Impala Data on page 93 for background
information for Impala users, the documentation for your Apache Hadoop distribution for usage details and how to
interpret the lineage information.
• Impala tables and partitions can now be located on the Amazon Simple Storage Service (S3) filesystem, for
convenience in cases where data is already located in S3 and you prefer to query it in-place. Queries might
have lower performance than when the data files reside on HDFS, because Impala uses some HDFS-specific
optimizations. Impala can query data in S3, but cannot write to S3. Therefore, statements such as INSERT and
LOAD DATA are not available when the destination table or partition is in S3. See Using Impala with the Amazon
S3 Filesystem on page 732 for details.
Important:
Impala query support for Amazon S3 is included in Impala 2.2, but is not supported or recommended for
production use in this version.
| Impala Release Notes | 815
• Improved support for HDFS encryption. The LOAD DATA statement now works when the source directory and
destination table are in different encryption zones. See the documentation for your Apache Hadoop distribution for
details about using HDFS encryption with Impala.
• Additional arithmetic function mod(). See Impala Mathematical Functions on page 418 for details.
• Flexibility to interpret TIMESTAMP values using the UTC time zone (the traditional Impala behavior) or using the
local time zone (for compatibility with TIMESTAMP values produced by Hive).
• Enhanced support for ETL using tools such as Flume. Impala ignores temporary files typically produced by these
tools (filenames with suffixes .copying and .tmp).
• The CPU requirement for Impala, which had become more restrictive in Impala 2.0.x and 2.1.x, has now been
relaxed.
The prerequisite for CPU architecture has been relaxed in Impala 2.2.0 and higher. From this release onward,
Impala works on CPUs that have the SSSE3 instruction set. The SSE4 instruction set is no longer required. This
relaxed requirement simplifies the upgrade planning from Impala 1.x releases, which also worked on SSSE3-
enabled processors.
• Enhanced support for CHAR and VARCHAR types in the COMPUTE STATS statement.
• The amount of memory required during setup for “spill to disk” operations is greatly reduced. This enhancement
reduces the chance of a memory-intensive join or aggregation query failing with an out-of-memory error.
• Several new conditional functions provide enhanced compatibility when porting code that uses industry
extensions. The new functions are: isfalse(), isnotfalse(), isnottrue(), istrue(),
nonnullvalue(), and nullvalue(). See Impala Conditional Functions on page 481 for details.
• The Impala debug web UI now can display a visual representation of the query plan. On the /queries tab, select
Details for a particular query. The Details page includes a Plan tab with a plan diagram that you can zoom in or
out (using scroll gestures through mouse wheel or trackpad).
• Subqueries are now allowed in the WHERE clause, for example with the IN operator.
• The EXISTS and NOT EXISTS operators are available. They are always used in conjunction with
subqueries.
• The IN and NOT IN queries can now operate on the result set from a subquery, not just a hardcoded list of
values.
• Uncorrelated subqueries let you compare against one or more values for equality, IN, and
EXISTS comparisons. For example, you might use WHERE clauses such as WHERE column =
(SELECT MAX(some_other_column FROM table) or WHERE column IN (SELECT
some_other_column FROM table WHERE conditions).
• Correlated subqueries let you cross-reference values from the outer query block and the subquery.
• Scalar subqueries let you substitute the result of single-value aggregate functions such as MAX(), MIN(),
COUNT(), or AVG(), where you would normally use a numeric value in a WHERE clause.
For details about subqueries, see Subqueries in Impala SELECT Statements on page 325 For information about
new and improved operators, see EXISTS Operator on page 175 and IN Operator on page 179.
• Analytic functions such as RANK(), LAG(), LEAD(), and FIRST_VALUE() let you analyze sequences of
rows with flexible ordering and grouping. Existing aggregate functions such as MAX(), SUM(), and COUNT()
can also be used in an analytic context. See Impala Analytic Functions on page 547 for details. See Impala
Aggregate Functions on page 516 for enhancements to existing aggregate functions.
• New data types provide greater compatibility with source code from traditional database systems:
• VARCHAR is like the STRING data type, but with a maximum length. See VARCHAR Data Type (Impala 2.0
or higher only) on page 132 for details.
• CHAR is like the STRING data type, but with a precise length. Short values are padded with spaces on the
right. See CHAR Data Type (Impala 2.0 or higher only) on page 101 for details.
• Security enhancements:
• Formerly, Impala was restricted to using either Kerberos or LDAP / Active Directory authentication within a
cluster. Now, Impala can freely accept either kind of authentication request, allowing you to set up some hosts
with Kerberos authentication and others with LDAP or Active Directory. See Using Multiple Authentication
Methods with Impala on page 89 for details.
• GRANT statement. See GRANT Statement (Impala 2.0 or higher only) on page 284 for details.
• REVOKE statement. See REVOKE Statement (Impala 2.0 or higher only) on page 305 for details.
• CREATE ROLE statement. See CREATE ROLE Statement (Impala 2.0 or higher only) on page 239 for
details.
• DROP ROLE statement. See DROP ROLE Statement (Impala 2.0 or higher only) on page 273 for details.
• SHOW ROLES and SHOW ROLE GRANT statements. See SHOW Statement on page 381 for details.
• To complement the HDFS encryption feature, a new Impala configuration option, --
disk_spill_encryption secures sensitive data from being observed or tampered with when temporarily
stored on disk.
The new security-related SQL statements work along with the Sentry authorization framework. See Enabling
Sentry Authorization for Impala for details.
• Impala can now read compressed text files compressed by gzip, bzip, or Snappy. These files do not require any
special table settings to work in an Impala text table. Impala recognizes the compression type automatically based
on file extensions of .gz, .bz2, and .snappy respectively. These types of compressed text files are intended
for convenience with existing ETL pipelines. Their non-splittable nature means they are not optimal for high-
performance parallel queries. See Using gzip, bzip2, or Snappy-Compressed Text Files on page 679 for details.
• Query hints can now use comment notation, /* +hint_name */ or -- +hint_name, at the same places
in the query where the hints enclosed by [ ] are recognized. This enhancement makes it easier to reuse Impala
queries on other database systems. See Optimizer Hints on page 407 for details.
• A new query option, QUERY_TIMEOUT_S, lets you specify a timeout period in seconds for individual queries.
The working of the --idle_query_timeout configuration option is extended. If no QUERY_OPTION_S
query option is in effect, --idle_query_timeout works the same as before, setting the timeout interval.
| Impala Release Notes | 817
When the QUERY_OPTION_S query option is specified, its maximum value is capped by the value of the --
idle_query_timeout option.
That is, the system administrator sets the default and maximum timeout through the --idle_query_timeout
startup option, and then individual users or applications can set a lower timeout value if desired through the
QUERY_TIMEOUT_S query option. See Setting Timeout Periods for Daemons, Queries, and Sessions on page
65 and QUERY_TIMEOUT_S Query Option (Impala 2.0 or higher only) on page 373 for details.
• New functions VAR_SAMP() and VAR_POP() are aliases for the existing VARIANCE_SAMP() and
VARIANCE_POP() functions.
• A new date and time function, DATE_PART(), provides similar functionality to EXTRACT(). You can also
call the EXTRACT() function using the SQL-99 syntax, EXTRACT(unit FROM timestamp). These
enhancements simplify the porting process for date-related code from other systems. See Impala Date and Time
Functions on page 457 for details.
• New approximation features provide a fast way to get results when absolute precision is not required:
• The APPX_COUNT_DISTINCT query option lets Impala rewrite COUNT(DISTINCT) calls to use NDV()
instead, which speeds up the operation and allows multiple COUNT(DISTINCT) operations in a single query.
See APPX_COUNT_DISTINCT Query Option (Impala 2.0 or higher only) on page 337 for details.
The APPX_MEDIAN() aggregate function produces an estimate for the median value of a column by using
sampling. See APPX_MEDIAN Function on page 517 for details.
• Impala now supports a DECODE() function. This function works as a shorthand for a CASE() expression, and
improves compatibility with SQL code containing vendor extensions. See Impala Conditional Functions on page
481 for details.
• The STDDEV(), STDDEV_POP(), STDDEV_SAMP(), VARIANCE(), VARIANCE_POP(),
VARIANCE_SAMP(), and NDV() aggregate functions now all return DOUBLE results rather than STRING.
Formerly, you were required to CAST() the result to a numeric type before using it in arithmetic operations.
• The default settings for Parquet block size, and the associated PARQUET_FILE_SIZE query option, are
changed. Now, Impala writes Parquet files with a size of 256 MB and an HDFS block size of 256 MB. Previously,
Impala attempted to write Parquet files with a size of 1 GB and an HDFS block size of 1 GB. In practice,
Impala used a conservative estimate of the disk space needed for each Parquet block, leading to files that were
typically 512 MB anyway. Thus, this change will make the file size more accurate if you specify a value for the
PARQUET_FILE_SIZE query option. It also reduces the amount of memory reserved during INSERT into
Parquet tables, potentially avoiding out-of-memory errors and improving scalability when inserting data into
Parquet tables.
• Anti-joins are now supported, expressed using the LEFT ANTI JOIN and RIGHT ANTI JOIN clauses. These
clauses returns results from one table that have no match in the other table. You might use this type of join in the
same sorts of use cases as the NOT EXISTS and NOT IN operators. See Joins in Impala SELECT Statements on
page 308 for details.
• The SET command in impala-shell has been promoted to a real SQL statement. You can now set query
options such as PARQUET_FILE_SIZE, MEM_LIMIT, and SYNC_DDL within JDBC, ODBC, or any other kind
of application that submits SQL without going through the impala-shell interpreter. See SET Statement on
page 335 for details.
• The impala-shell interpreter now reads settings from an optional configuration file, named
$HOME/.impalarc by default. See impala-shell Configuration File on page 759 for details.
• The library used for regular expression parsing has changed from Boost to Google RE2. This implementation
change adds support for non-greedy matches using the .*? notation. This and other changes in the way
regular expressions are interpreted means you might need to re-test queries that use functions such as
regexp_extract() or regexp_replace(), or operators such as REGEXP or RLIKE. See Incompatible
Changes and Limitations in Apache Impala on page 826 for those details.
• The DECIMAL data type lets you store fixed-precision values, for working with currency or other fractional values
where it is important to represent values exactly and avoid rounding errors. This feature includes enhancements to
built-in functions, numeric literals, and arithmetic expressions.
• Where the underlying HDFS support exists, Impala can take advantage of the HDFS caching feature to “pin”
entire tables or individual partitions in memory, to speed up queries on frequently accessed data and reduce the
CPU overhead of memory-to-memory copying. When HDFS files are cached in memory, Impala can read the
cached data without any disk reads, and without making an additional copy of the data in memory. Other Hadoop
components that read the same data files also experience a performance benefit.
• Impala can now use Sentry-based authorization based either on the original policy file, or on rules defined by
GRANT and REVOKE statements issued through Hive. See Enabling Sentry Authorization for Impala for details.
• For interoperability with Parquet files created through other Hadoop components, such as Pig or MapReduce jobs,
you can create an Impala table that automatically sets up the column definitions based on the layout of an existing
Parquet data file.
• ORDER BY queries no longer require a LIMIT clause. If the size of the result set to be sorted exceeds the
memory available to Impala, Impala uses a temporary work space on disk to perform the sort operation.
• LDAP connections can be secured through either SSL or TLS.
• The following new built-in scalar and aggregate functions are available:
• A new built-in function, EXTRACT(), returns one date or time field from a TIMESTAMP value.
• A new built-in function, TRUNC(), truncates date/time values to a particular granularity, such as year, month,
day, hour, and so on.
• ADD_MONTHS() built-in function, an alias for the existing MONTHS_ADD() function.
• A new built-in function, ROUND(), rounds DECIMAL values to a specified number of fractional digits.
• Several built-in aggregate functions for computing properties for statistical distributions: STDDEV(),
STDDEV_SAMP(), STDDEV_POP(), VARIANCE(), VARIANCE_SAMP(), and VARIANCE_POP().
• Several new built-in functions, such as MAX_INT(), MIN_SMALLINT(), and so on, let you conveniently
check whether data values are in an expected range. You might be able to switch a column to a smaller type,
saving memory during processing.
• New built-in functions, IS_INF() and IS_NAN(), check for the special values infinity and “not a number”.
These values could be specified as inf or nan in text data files, or be produced by certain arithmetic
expressions.
• The SHOW PARTITIONS statement displays information about the structure of a partitioned table.
• New configuration options for the impalad daemon let you specify initial memory usage for all queries. The
initial resource requests handled by Llama and YARN can be expanded later if needed, avoiding unnecessary
over-allocation and reducing the chance of out-of-memory conditions.
• The Impala CREATE TABLE statement now has a STORED AS AVRO clause, allowing you to create Avro
tables through Impala.
• New impalad configuration options let you fine-tune the calculations Impala makes to estimate resource
requirements for each query. These options can help avoid problems due to overconsumption due to too-low
estimates, or underutilization due to too-high estimates.
• A new SUMMARY command in the impala-shell interpreter provides a high-level summary of the work
performed at each stage of the explain plan. The summary is also included in output from the PROFILE
command.
• Performance improvements for the COMPUTE STATS statement:
• The NDV function is speeded up through native code generation.
• Because the NULL count is not currently used by the Impala query planner, in Impala 1.4.0 and higher,
COMPUTE STATS does not count the NULL values for each column. (The #Nulls field of the stats table is
left as -1, signifying that the value is unknown.)
• Performance improvements for partition pruning. This feature reduces the time spent in query planning,
for partitioned tables with thousands of partitions. Previously, Impala typically queried tables with up to
approximately 3000 partitions. With the performance improvement in partition pruning, now Impala can
comfortably handle tables with tens of thousands of partitions.
• The documentation provides additional guidance for planning tasks.
| Impala Release Notes | 819
• The impala-shell interpreter now supports UTF-8 characters for input and output. You can control whether
impala-shell ignores invalid Unicode code points through the --strict_unicode option. (Although this
option is removed in Impala 2.0.)
• -llama_port
• -llama_callback_port
• -cgroup_hierarchy_path
For details of these startup options, see Modifying Impala Startup Options on page 31.
This feature also involves several new or changed query options that you can set through the impala-shell
interpreter and apply within a specific session:
• MEM_LIMIT: the function of this existing option changes when Impala resource management is enabled.
• REQUEST_POOL: a new option. (Renamed to RESOURCE_POOL in Impala 1.3.0.)
• V_CPU_CORES: a new option.
• RESERVATION_REQUEST_TIMEOUT: a new option.
For details of these query options, see #unique_762.
• OFFSET clause, for use with the ORDER BY and LIMIT clauses to produce “paged” result sets such as items
1-10, then 11-20, and so on.
• NULLS FIRST and NULLS LAST clauses to ensure consistent placement of NULL values in ORDER BY
queries.
• New built-in functions: least(), greatest(), initcap().
• New aggregate function: ndv(), a fast alternative to COUNT(DISTINCT col) returning an approximate
result.
• The LIMIT clause can now accept a numeric expression as an argument, rather than only a literal constant.
• The SHOW CREATE TABLE statement displays the end result of all the CREATE TABLE and ALTER TABLE
statements for a particular table. You can use the output to produce a simplified setup script for a schema.
• The --idle_query_timeout and --idle_session_timeout options for impalad control the
time intervals after which idle queries are cancelled, and idle sessions expire. See Setting Timeout Periods for
Daemons, Queries, and Sessions on page 65 for details.
• User-defined functions (UDFs). This feature lets you transform data in very flexible ways, which is important
when using Impala as part of an ETL or ELT pipeline. Prior to Impala 1.2, using UDFs required switching
into Hive. Impala 1.2 can run scalar UDFs and user-defined aggregate functions (UDAs). Impala can run high-
performance functions written in C++, or you can reuse existing Hive functions written in Java.
You create UDFs through the CREATE FUNCTION statement and drop them through the DROP FUNCTION
statement. See User-Defined Functions (UDFs) on page 566 for instructions about coding, building, and
deploying UDFs, and CREATE FUNCTION Statement on page 233 and DROP FUNCTION Statement on page
272 for related SQL syntax.
• A new service automatically propagates changes to table data and metadata made by one Impala node, sending
the new or updated metadata to all the other Impala nodes. The automatic synchronization mechanism eliminates
the need to use the INVALIDATE METADATA and REFRESH statements after issuing Impala statements such as
CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, and LOAD DATA.
For even more precise synchronization, you can enable the SYNC_DDL query option before issuing a DDL,
INSERT, or LOAD DATA statement. This option causes the statement to wait, returning only after the catalog
service has broadcast the applicable changes to all Impala nodes in the cluster.
Note:
Because the catalog service only monitors operations performed through Impala, INVALIDATE METADATA and
REFRESH are still needed on the Impala side after creating new tables or loading data through the Hive shell or by
manipulating data files directly in HDFS. Because the catalog service broadcasts the result of the REFRESH and
INVALIDATE METADATA statements to all Impala nodes, when you do need to use those statements, you can do
so a single time rather than on every Impala node.
This service is implemented by the catalogd daemon. See The Impala Catalog Service on page 17 for
details.
• The CREATE TABLE and ALTER TABLE statements have new clauses TBLPROPERTIES and WITH
SERDEPROPERTIES. The TBLPROPERTIES clause lets you associate arbitrary items of metadata with a
particular table as key-value pairs. The WITH SERDEPROPERTIES clause lets you specify the serializer/
deserializer (SerDes) classes that read and write data for a table; although Impala does not make use of these
properties, sometimes particular values are needed for Hive compatibility. See CREATE TABLE Statement on
page 240 and ALTER TABLE Statement on page 207 for details.
• Delegation support lets you authorize certain OS users associated with applications (for example, hue), to submit
requests using the credentials of other users. See Configuring Impala Delegation for Clients on page 90 for
details.
• Enhancements to EXPLAIN output. In particular, when you enable the new EXPLAIN_LEVEL query option, the
EXPLAIN and PROFILE statements produce more verbose output showing estimated resource requirements and
whether table and column statistics are available for the applicable tables and columns. See EXPLAIN Statement
on page 281 for details.
• SHOW CREATE TABLE summarizes the effects of the original CREATE TABLE statement and any subsequent
ALTER TABLE statements, giving you a CREATE TABLE statement that will re-create the current structure and
layout for a table.
| Impala Release Notes | 823
• The LIMIT clause for queries now accepts an arithmetic expression, in addition to numeric literals.
• Delegation support lets you authorize certain OS users associated with applications (for example, hue), to submit
requests using the credentials of other users. See Configuring Impala Delegation for Clients on page 90 for
details.
• Enhancements to EXPLAIN output. In particular, when you enable the new EXPLAIN_LEVEL query option, the
EXPLAIN and PROFILE statements produce more verbose output showing estimated resource requirements and
whether table and column statistics are available for the applicable tables and columns. See EXPLAIN Statement
on page 281 for details.
corresponding columns in the source. VALUES clause. This feature is known as “column permutation”. See
INSERT Statement on page 288.
• The new LOAD DATA statement lets you load data into a table directly from an HDFS data file. This technique
lets you minimize the number of steps in your ETL process, and provides more flexibility. For example, you can
bring data into an Impala table in one step. Formerly, you might have created an external table where the data
files are not entirely under your control, or copied the data files to Impala data directories manually, or loaded
the original data into one table and then used the INSERT statement to copy it to a new table with a different file
format, partitioning scheme, and so on. See LOAD DATA Statement on page 299.
• Improvements to Impala-HBase integration:
• New query options for HBase performance: HBASE_CACHE_BLOCKS and HBASE_CACHING.
• Support for binary data types in HBase tables. See Supported Data Types for HBase Columns on page 723
for details.
• You can issue REFRESH as a SQL statement through any of the programming interfaces that Impala supports.
REFRESH formerly had to be issued as a command through the impala-shell interpreter, and was not
available through a JDBC or ODBC API call. As part of this change, the functionality of the REFRESH statement
is divided between two statements. In Impala 1.1, REFRESH requires a table name argument and immediately
reloads the metadata; the new INVALIDATE METADATA statement works the same as the Impala 1.0 REFRESH
did: the table name argument is optional, and the metadata for one or all tables is marked as stale, but not actually
reloaded until the table is queried. When you create a new table in the Hive shell or through a different Impala
node, you must enter INVALIDATE METADATA with no table parameter before you can see the new table in
impala-shell. See REFRESH Statement on page 302 and INVALIDATE METADATA Statement on page
297.
• Support for the memory limits. For more information, see the example on modifying memory limits in Modifying
Impala Startup Options on page 31.
• Bigger and faster joins through the addition of partitioned joins to the already supported broadcast joins.
• Fully distributed aggregations.
• Fully distributed top-n computation.
• Support for creating and altering tables.
• Support for GROUP BY with floats and doubles.
-default_query_options='key=value;key=value'
Even added SQL statements or clauses can produce incompatibilities, if you have databases, tables, or columns whose
names conflict with the new keywords.
• The syntax related to Kudu tables includes a number of new reserved words, such as COMPRESSION, DEFAULT,
and ENCODING, that might conflict with names of existing tables, columns, or other identifiers from older Impala
versions. See Impala Reserved Words on page 783 for the full list of reserved words.
• The DDL syntax for Kudu tables, particularly in the CREATE TABLE statement, is different from the special
impala_next fork that was previously used for accessing Kudu tables from Impala:
• The DISTRIBUTE BY clause is now PARTITIONED BY.
• The INTO N BUCKETS clause is now PARTITIONS N.
• The SPLIT ROWS clause is replaced by different syntax for specifying the ranges covered by each partition.
• The DESCRIBE output for Kudu tables includes several extra columns.
• Non-primary-key columns can contain NULL values by default. The SHOW CREATE TABLE output for these
columns displays the NULL attribute. There was a period during early experimental versions of Impala + Kudu
where non-primary-key columns had the NOT NULL attribute by default.
• The IGNORE keyword that was present in early experimental versions of Impala + Kudu is no longer present.
The behavior of the IGNORE keyword is now the default: DML statements continue with warnings, instead of
failing with errors, if they encounter conditions such as “primary key already exists” for an INSERT statement or
“primary key already deleted” for a DELETE statement.
• The replication factor for Kudu tables must be an odd number.
• A UDF compiled into an LLVM IR bitcode module (.bc) might encounter a runtime error when native code
generation is turned off by setting the query option DISABLE_CODEGEN=1. This issue also applies when
running a built-in or native UDF with more than 20 arguments. See IMPALA-4432 for details. As a workaround,
either turn native code generation back on with the query option DISABLE_CODEGEN=0, or use the regular UDF
compilation path that does not produce an IR module.
• Improved type accuracy for CASE return values. If all WHEN clauses of the CASE expression are of CHAR type,
the final result is also CHAR instead of being converted to STRING.
• The initial release of Impala 2.5 sometimes has a higher peak memory usage than in previous releases while
reading Parquet files. The following query options might help to reduce memory consumption in the Parquet
scanner:
• Reduce the number of scanner threads, for example: set num_scanner_threads=30
• Reduce the batch size, for example: set batch_size=512
• Increase the memory limit, for example: set mem_limit=64g
You can track the status of the fix for this issue at IMPALA-3662.
• The S3_SKIP_INSERT_STAGING query option, which is enabled by default, increases the speed of INSERT
operations for S3 tables. The speedup applies to regular INSERT, but not INSERT OVERWRITE. The
tradeoff is the possibility of inconsistent output files left behind if a node fails during INSERT execution. See
S3_SKIP_INSERT_STAGING Query Option (Impala 2.6 or higher only) on page 376 for details.
Certain features are turned off by default, to avoid regressions or unexpected behavior following an upgrade. Consider
turning on these features after suitable testing:
• Impala now recognizes the auth_to_local setting, specified through the HDFS configuration setting
hadoop.security.auth_to_local. This feature is disabled by default; to enable it, specify --
load_auth_to_local_rules=true in the impalad configuration settings.
• A new query option, PARQUET_ANNOTATE_STRINGS_UTF8, makes Impala include the UTF-8 annotation
metadata for STRING, CHAR, and VARCHAR columns in Parquet files created by INSERT or CREATE TABLE
AS SELECT statements.
• A new query option, PARQUET_FALLBACK_SCHEMA_RESOLUTION, lets Impala locate columns within
Parquet files based on column name rather than ordinal position. This enhancement improves interoperability with
applications that write Parquet files with a different order or subset of columns than are used in the Impala table.
+--------------+-------------------------------------------------
+-------------+---------------+
| Impala Release Notes | 830
Changes to Prerequisites
The prerequisite for CPU architecture has been relaxed in Impala 2.2.0 and higher. From this release onward,
Impala works on CPUs that have the SSSE3 instruction set. The SSE4 instruction set is no longer required. This
relaxed requirement simplifies the upgrade planning from Impala 1.x releases, which also worked on SSSE3-enabled
processors.
Changes to Prerequisites
Currently, Impala 2.1.x does not function on CPUs without the SSE4.1 instruction set. This minimum CPU
requirement is higher than in previous versions, which relied on the older SSSE3 instruction set. Check the CPU level
of the hosts in your cluster before upgrading to Impala 2.1.
Changes to Prerequisites
Currently, Impala 2.0.x does not function on CPUs without the SSE4.1 instruction set. This minimum CPU
requirement is higher than in previous versions, which relied on the older SSSE3 instruction set. Check the CPU level
of the hosts in your cluster before upgrading to Impala 2.0.
The --strict_unicode option of impala-shell was removed. To avoid problems with Unicode values in
impala-shell, define the following locale setting before running impala-shell:
export LC_CTYPE=en_US.UTF-8
• PARTITIONS
• PRODUCED
• UNCACHED
The following were formerly reserved keywords, but are no longer reserved:
• COUNT
• GROUP_CONCAT
• NDV
• SUM
• The fix for issue IMPALA-973 changes the behavior of the INVALIDATE METADATA statement regarding
nonexistent tables. In Impala 1.4.0 and higher, the statement returns an error if the specified table is not in the
metastore database at all. It completes successfully if the specified table is in the metastore database but not yet
recognized by Impala, for example if the table was created through Hive. Formerly, you could issue this statement
for a completely nonexistent table, with no error.
If you are running a level of Impala that is older than 1.1.1, do the metadata update through Hive:
Impala 1.1.1 and higher can reuse Parquet data files created by Hive, without any action required.
As usual, make sure to upgrade the Impala LZO package to the latest level at the same time as you upgrade the Impala
server.
For issues fixed in various Impala releases, see Fixed Issues in Apache Impala on page 842.
ImpalaODBC: Can not get the value in the SQLGetData(m-x th column) after the SQLBindCol(m th
column)
If the ODBC SQLGetData is called on a series of columns, the function calls must follow the same order as the
columns. For example, if data is fetched from column 2 then column 1, the SQLGetData call for column 1 returns
NULL.
Apache Issue: IMPALA-1792
Workaround: Fetch columns in the same order they are defined in the table.
Impala does not allow the use of insecure clusters with public IPs
Starting in Impala 2.12, Impala, by default, will only allow unencrypted or unauthenticated connections from
trusted subnets: 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16.
Unencrypted or unauthenticated connections from publicly routable IPs will be rejected.
The trusted subnets can be configured using the --trusted_subnets flag. Set it to '0.0.0.0/0' to allow
unauthenticated connections from all remote IP addresses. However, if network access is not otherwise restricted by a
firewall, malicious users may be able to gain unauthorized access.
Workaround: To prevent such errors, configure each host running an impalad daemon with the following settings:
Breakpad minidumps can be very large when the thread count is high
The size of the breakpad minidump files grows linearly with the number of threads. By default, each thread adds
8 KB to the minidump size. Minidump files could consume significant disk space when the daemons have a high
number of threads.
Workaround: Add --minidump_size_limit_hint_kb=size to set a soft upper limit on the size of each
minidump file. If the minidump file would exceed that limit, Impala reduces the amount of information for each
thread from 8 KB to 2 KB. (Full thread information is captured for the first 20 threads, then 2 KB per thread after
that.) The minidump file can still grow larger than the "hinted" size. For example, if you have 10,000 threads, the
minidump file can be more than 20 MB.
Apache Issue: IMPALA-3509
Process mem limit does not account for the JVM's memory usage
Some memory allocated by the JVM used internally by Impala is not counted against the memory limit for the
impalad daemon.
Workaround: To monitor overall memory usage, use the top command, or add the memory figures in the Impala
web UI /memz tab to JVM memory usage shown on the /metrics tab.
Apache Issue: IMPALA-691
% escaping does not work correctly when occurs at the end in a LIKE clause
If the final character in the RHS argument of a LIKE operator is an escaped \% character, it does not match a % final
character of the LHS argument.
Apache Issue: IMPALA-2422
Crash: impala::Coordinator::ValidateCollectionSlots
A query could encounter a serious error if includes multiple nested levels of INNER JOIN clauses involving
subqueries.
Apache Issue: IMPALA-2603
(The part in parentheses is case sensitive.) Make sure to pick the right choice between internal and external
when recreating the table. See Overview of Impala Tables on page 198 for the differences between internal
and external tables.
Severity: High
A failed CTAS does not drop the table if the insert fails
If a CREATE TABLE AS SELECT operation successfully creates the target table but an error occurs while querying
the source table or copying the data, the new table is left behind rather than being dropped.
Apache Issue: IMPALA-2005
Workaround: Drop the new table manually after a failed CREATE TABLE AS SELECT.
RuntimeState::error_log_ crashes
A crash could occur, with stack trace pointing to impala::RuntimeState::ErrorLog.
Bug: IMPALA-3385
Severity: High
Thrift buffer overflows when serialize more than 3355443200 bytes in impala
A very large memory allocation within the catalogd daemon could exceed an internal Thrift limit, causing a crash.
Bug: IMPALA-3494
Severity: High
Altering table partition's storage format is not working and crashing the daemon
If a partitioned table used a file format other than Avro, and the file format of an individual partition was changed to
Avro, subsequent queries could encounter a crash.
Bug: IMPALA-3314
Severity: High
Race condition may cause scanners to spin with runtime filters on Avro or Sequence files
A timing problem during runtime filter processing could cause queries against Avro or SequenceFile tables to hang.
Bug: IMPALA-3798
Severity: High
Stress test hit assert in LLVM: external function could not be resolved
Bug: IMPALA-2683
The stress test was running a build with the TPC-H, TPC-DS, and TPC-H nested queries with scale factor 3.
Impalad is crashing if udf jar is not available in hdfs location for first time
Bug: IMPALA-2365
If a UDF JAR was not available in the HDFS location specified in the CREATE FUNCTION statement, the
impalad daemon could crash.
Wrong plan of NOT IN aggregate subquery when a constant is used in subquery predicate
Bug: IMPALA-2093
IN subqueries might return wrong results if the left-hand side of the IN is a constant. For example:
select case when 1=1 then 'didit' end as c1 from (select 1 as one) a having
1!=1;
Aggregation spill loop gives up too early leading to mem limit exceeded errors
Bug: IMPALA-2986
An aggregation query could fail with an out-of-memory error, despite sufficient memory being reported as available.
SET disable_codegen=true;
Impala returns wrong result for function conv(). Function conv(bigint, from_base, to_base) returns
an correct result, while conv(string, from_base, to_base) returns the correct value. For example:
Workaround: Cast the value to string and use conv(string, from_base, to_base) for conversion.
Free local allocations once for every row batch when building hash tables
A join query could encounter a serious error due to an internal failure to allocate memory, which resulted in
dereferencing a NULL pointer.
Bug: IMPALA-2612
Throw AnalysisError if table properties are too large (for the Hive metastore)
CREATE TABLE or ALTER TABLE statements could fail with metastore database errors due to length limits on the
SERDEPROPERTIES and TBLPROPERTIES clauses. (The limit on key size is 256, while the limit on value size
is 4000.) The fix makes Impala handle these error conditions more cleanly, by detecting too-long values rather than
passing them to the metastore database.
Bug: IMPALA-2226
| Impala Release Notes | 850
Handle error when distinct and aggregates are used with a having clause
A query that included a DISTINCT operator and a HAVING clause, but no aggregate functions or GROUP BY, would
fail with an uninformative error message.
Bug: IMPALA-2113
Handle error when star based select item and aggregate are incorrectly used
A query that included * in the SELECT list, in addition to an aggregate function call, would fail with an
uninformative message if the query had no GROUP BY clause.
Bug: IMPALA-2225
Bug: IMPALA-2535
Bug: IMPALA-2591
HBase scan node uses 2-4x memory after upgrade to Impala 2.2.8
Queries involving HBase tables used substantially more memory than in earlier Impala versions. The problem
occurred starting in Impala 2.2.8, as a result of the changes for IMPALA-2284. The fix for this issue involves
removing a separate memory work area for HBase queries and reusing other memory that was already allocated.
Bug: IMPALA-2731
Fix spilling sorts with var-len slots that are NULL or empty.
A query that activated the spill-to-disk mechanism could fail if it contained a sort expression involving certain
combinations of fixed-length or variable-length types.
Bug: IMPALA-2357
| Impala Release Notes | 853
Impala throws IllegalStateException when inserting data into a partition while select subquery
group by partition columns
An INSERT ... SELECT operation into a partitioned table could fail if the SELECT query included a GROUP BY
clause referring to the partition key columns.
Bug: IMPALA-2533
Impala is unable to read hive tables created with the "STORED AS AVRO" clause
Impala could not read Avro tables created in Hive with the STORED AS AVRO clause.
Bug: IMPALA-1136, IMPALA-2161
make Parquet scanner fail query if the file size metadata is stale
If a Parquet file in HDFS was overwritten by a smaller file, Impala could encounter a serious error. Issuing a
INVALIDATE METADATA statement before a subsequent query would avoid the error. The fix allows Impala to
handle such inconsistencies in Parquet file length cleanly regardless of whether the table metadata is up-to-date.
Bug: IMPALA-2213
The catalog does not close the connection to HMS during table invalidation
A communication error could occur between Impala and the Hive metastore database, causing Impala operations that
update table metadata to fail.
Bug: IMPALA-2348
Set the output smap of an EmptySetNode produced from an empty inline view.
A query could encounter a serious error if it included an inline view whose subquery had no FROM clause.
Bug: IMPALA-2216
Set an InsertStmt's result exprs from the source statement's result exprs.
A CREATE TABLE AS SELECT or INSERT ... SELECT statement could produce different results than a
SELECT statement, for queries including a FULL JOIN clause and including literal values in the select list.
Bug: IMPALA-2203
Row count not set for empty partition when spec is used with compute incremental stats
A COMPUTE INCREMENTAL STATS statement could leave the row count for an emptyp partition as -1, rather than
initializing the row count to 0. The missing statistic value could result in reduced query performance.
Bug: IMPALA-2199
Substitute an InsertStmt's partition key exprs with the root node's smap.
An INSERT statement could encounter a serious error if the SELECT portion called an analytic function.
Bug: IMPALA-1737
Impala DML/DDL operations corrupt table metadata leading to Hive query failures
When the Impala COMPUTE STATS statement was run on a partitioned Parquet table that was created in Hive, the
table subsequently became inaccessible in Hive. The table was still accessible to Impala. Regaining access in Hive
required a workaround of creating a new table. The error displayed in Hive was:
Bug: IMPALA-2048
Altering a column's type causes column stats to stop sticking for that column
When the type of a column was changed in either Hive or Impala through ALTER TABLE CHANGE COLUMN,
the metastore database did not correctly propagate that change to the table that contains the column statistics. The
statistics (particularly the NDV) for that column were permanently reset and could not be changed by Impala's
COMPUTE STATS command. The underlying cause is a Hive bug (HIVE-9866).
Bug: IMPALA-1607
Resolution: Resolved by incorporating the fix for HIVE-9866.
Workaround: On systems without the corresponding Hive fix, change the column back to its original type. The stats
reappear and you can recompute or drop them.
DROP TABLE fails after COMPUTE STATS and ALTER TABLE RENAME to a different database.
When a table was moved from one database to another, the column statistics were not pointed to the new database.i
This could result in lower performance for queries due to unavailable statistics, and also an inability to drop the table.
Bug: IMPALA-1711
Impala incorrectly handles text data missing a newline on the last line
Some queries did not recognize the final line of a text data file if the line did not end with a newline character.
This could lead to inconsistent results, such as a different number of rows for SELECT COUNT(*) as opposed to
SELECT *.
Bug: IMPALA-1476
Impala's ACLs check do not consider all group ACLs, only checked first one.
If the HDFS user ID associated with the impalad process had read or write access in HDFS based on group
membership, Impala statements could still fail with HDFS permission errors if that group was not the first listed
group for that user ID.
Bug: IMPALA-1805
Cannot write Parquet files when values are larger than 64KB
Impala could sometimes fail to INSERT into a Parquet table if a column value such as a STRING was larger than 64
KB.
Bug: IMPALA-1705
Constant filter expressions are not checked for errors and state cleanup on exception / DCHECK on
destroying an ExprContext
An invalid constant expression in a WHERE clause (for example, an invalid regular expression pattern) could fail to
clean up internal state after raising a query error. Therefore, certain combinations of invalid expressions in a query
could cause a crash, or cause a query to continue when it should halt with an error.
Bug: IMPALA-1756, IMPALA-2514
Bug: IMPALA-1919
Crash: impala::TupleIsNullPredicate::Prepare
When expressions that tested for NULL were used in combination with analytic functions, an error could occur. (The
original crash issue was fixed by an earlier patch.)
Bug: IMPALA-1519
Impala's ACLs check do not consider all group ACLs, only checked first one.
If the HDFS user ID associated with the impalad process had read or write access in HDFS based on group
membership, Impala statements could still fail with HDFS permission errors if that group was not the first listed
group for that user ID.
Bug: IMPALA-1805
Impala incorrectly handles double numbers with more than 19 significant decimal digits
When a floating-point value was read from a text file and interpreted as a FLOAT or DOUBLE value, it could be
incorrectly interpreted if it included more than 19 significant digits.
Bug: IMPALA-1622
Fetch column stats in bulk using new (Hive .13) HMS APIs
The performance of the COMPUTE STATS statement and queries was improved, particularly for wide tables.
Bug: IMPALA-1120
IoMgr infinite loop opening/closing file when shorter than cached metadata size
If a table data file was replaced by a shorter file outside of Impala, such as with INSERT OVERWRITE in Hive
producing an empty output file, subsequent Impala queries could hang.
Bug: IMPALA-1794
Bug: IMPALA-1471
Fetch column stats in bulk using new (Hive .13) HMS APIs
The performance of the COMPUTE STATS statement and queries was improved, particularly for wide tables.
Bug: IMPALA-1120
Queries fail with metastore exception after upgrade and compute stats
After running the COMPUTE STATS statement on an Impala table, subsequent queries on that table could fail with
the exception message Failed to load metadata for table: default.stats_test.
Bug: https://issues.apache.org/jira/browse/IMPALA-1416 IMPALA-1416
Workaround: Upgrading to a level of that includes the fix for HIVE-8627, prevents the problem from affecting
future COMPUTE STATS statements. On affected levels of , or for Impala tables that have become inaccessible,
the workaround is to disable the hive.metastore.try.direct.sql setting in the Hive metastore hive-
site.xml file and issue the INVALIDATE METADATA statement for the affected table. You do not need to rerun
the COMPUTE STATS statement for the table.
Incorrect plan after reordering predicates (inner join following outer join)
Potential wrong results for some types of queries.
Bug: IMPALA-111"8
Combining fragments with compatible data partitions can lead to incorrect results due to type
incompatibilities (missing casts).
Potential wrong results for some types of queries.
Bug: IMPALA-112"3
Bug: IMPALA-110"5
Allow creating Avro tables without column definitions. Allow COMPUTE STATS to always work on
Impala-created Avro tables.
Hive-created Avro tables with columns specified by a JSON file or literal could produce errors when queried in
Impala, and could not be used with the COMPUTE STATS statement. Now you can create such tables in Impala to
avoid such errors.
Bug: IMPALA-110"4
Impala does not employ ACLs when checking path permissions for LOAD and INSERT
Certain INSERT and LOAD DATA statements could fail unnecessarily, if the target directories in HDFS had
restrictive HDFS permissions, but those permissions were overridden by HDFS extended ACLs.
Bug: IMPALA-127"9
boost::exception_detail::clone_impl
<boost::exception_detail::error_info_injector<boost::lock_error> >
Severity: High
Impalad catalog updates can fail with error: "IllegalArgumentException: fromKey out of range"
The error in the title could occur following a DDL statement. This issue was discovered during internal testing and
has not been reported in customer environments.
Bug: IMPALA-1093
"Total" time counter does not capture all the network transmit time
The time for some network operations was not counted in the report of total time for a query, making it difficult to
diagnose network-related performance issues.
Bug: IMPALA-1131
Impala will crash when reading certain Avro files containing bytes data
Certain Avro fields for byte data could cause Impala to be unable to read an Avro data file, even if the field was not
part of the Impala table definition. With this fix, Impala can now read these Avro data files, although Impala queries
cannot refer to the “bytes” fields.
Bug: IMPALA-1149
For the full list of fixed issues in Impala 1.4.0, see this report in the Impala JIRA tracker.
The issue was due to the use of HDFS caching with data files accessed by Impala. Support for HDFS caching in
Impala was introduced in Impala 1.4.0. The fix for this issue was backported to Impala 1.3.x, and is the only change
in Impala 1.3.2.
Bug: IMPALA-1019
Resolution: This issue is fixed in Impala 1.3.2. The addition of HDFS caching support in Impala 1.4 means that this
issue does not apply to any new level of Impala.
The extended view definition SQL text in Views created by Impala should always have fully-
qualified table names
When a view was accessed while inside a different database, references to tables were not resolved unless the names
were fully qualified when the view was created.
Bug: IMPALA-962
When I run CREATE TABLE new_table LIKE avro_table, the schema does not get mapped properly
from an avro schema to a hive schema
After a CREATE TABLE LIKE statement using an Avro table as the source, the new table could have incorrect
metadata and be inaccessible, depending on how the original Avro table was created.
Bug: IMPALA-185
The issue was due to the use of HDFS caching with data files accessed by Impala. Support for HDFS caching in
Impala was introduced in Impala 1.4.0. The fix for this issue was backported to Impala 1.3.x, and is the only change
in Impala 1.3.2.
Bug: IMPALA-1019
Resolution: This issue is fixed in Impala 1.3.2. The addition of HDFS caching support in Impala 1.4 means that this
issue does not apply to any new level of Impala.
Impalad crashes when left joining inline view that has aggregate using distinct
Impala could encounter a severe error in a query combining a left outer join with an inline view containing a
COUNT(DISTINCT) operation.
Bug: IMPALA-904
Incorrect result with group by query with null value in group by data
If the result of a GROUP BY operation is NULL, the resulting row might be omitted from the result set. This issue
depends on the data values and data types in the table.
Bug: IMPALA-901
| Impala Release Notes | 870
Text data with carriage returns generates wrong results for count(*)
A COUNT(*) operation could return the wrong result for text tables using nul characters (ASCII value 0) as
delimiters.
Bug: IMPALA-13
Workaround: Impala adds support for ASCII 0 characters as delimiters through the clause FIELDS TERMINATED
BY '\0'.
IO Mgr should take instance memory limit into account when creating io buffers
Impala could allocate more memory than necessary during certain operations.
Bug: IMPALA-488
Workaround: Before issuing a COMPUTE STATS statement for a Parquet table, reduce the number of threads
used in that operation by issuing SET NUM_SCANNER_THREADS=2 in impala-shell. Then issue UNSET
NUM_SCANNER_THREADS before continuing with queries.
Impala should provide an option for new sub directories to automatically inherit the permissions of
the parent directory
When new subdirectories are created underneath a partitioned table by an INSERT statement, previously the new
subdirectories always used the default HDFS permissions for the impala user, which might not be suitable for
directories intended to be read and written by other components also.
Bug: IMPALA-827
Resolution: In Impala 1.3.1 and higher, you can specify the --insert_inherit_permissions configuration
when starting the impalad daemon.
Illegal state exception (or crash) in query with UNION in inline view
Impala could encounter a severe error in a query where the FROM list contains an inline view that includes a UNION.
The exact type of the error varies.
Bug: IMPALA-888
| Impala Release Notes | 871
Using distinct inside aggregate function may cause incorrect result when using having clause
A query could return incorrect results if it combined an aggregate function call, a DISTINCT operator, and a
HAVING clause, without a GROUP BY clause.
Bug: IMPALA-845
Wrong expression may be used in aggregate query if there are multiple similar expressions
If a GROUP BY query referenced the same columns multiple times using different operators, result rows could
contain multiple copies of the same expression.
Bug: IMPALA-817
Incorrect results when changing the order of aggregates in the select list with codegen enabled
Referencing the same columns in both a COUNT() and a SUM() call in the same query, or some other combinations
of aggregate function calls, could incorrectly return a result of 0 from one of the aggregate functions. This issue
affected references to TINYINT and SMALLINT columns, but not INT or BIGINT columns.
Bug: IMPALA-765
Workaround: Setting the query option DISABLE_CODEGEN=TRUE prevented the incorrect results. Switching the
order of the function calls could also prevent the issue from occurring.
| Impala Release Notes | 872
Union queries give Wrong result in a UNION followed by SIGSEGV in another union
A UNION query could produce a wrong result, followed by a serious error for a subsequent UNION query.
Bug: IMPALA-723
Compute stats need to use quotes with identifiers that are Impala keywords
Using a column or table name that conflicted with Impala keywords could prevent running the COMPUTE STATS
statement for the table.
Bug: IMPALA-777
Fail early (in analysis) when COMPUTE STATS is run against Avro table with no columns
If the columns for an Avro table were all defined in the TBLPROPERTIES or SERDEPROPERTIES clauses, the
COMPUTE STATS statement would fail after completely analyzing the table, potentially causing a long delay.
Although the COMPUTE STATS statement still does not work for such tables, now the problem is detected and
reported immediately.
Bug: IMPALA-867
Workaround: Re-create the Avro table with columns defined in SQL style, using the output of SHOW CREATE
TABLE. (See the JIRA page for detailed steps.)
The Catalog Server exits with an OOM error after a certain number of CREATE statements
A large number of concurrent CREATE TABLE statements can cause the catalogd process to consume excessive
memory, and potentially be killed due to an out-of-memory condition.
Bug: IMPALA-818
Workaround: Restart the catalogd service and re-try the DDL operations that failed.
Statestore seems to send concurrent heartbeats to the same subscriber leading to repeated
"Subscriber 'hostname' is registering with statestore, ignoring update" messages
Impala nodes could produce repeated error messages after recovering from a communication error with the statestore
service.
Bug: IMPALA-809
Impala cannot load tables with more than Short.MAX_VALUE number of partitions
If a table had more than 32,767 partitions, Impala would not recognize the partitions above the 32K limit and query
results could be incomplete.
Bug: IMPALA-749
Parquet in CDH4.5 writes data files that are sometimes unreadable by Impala
Some Parquet files could be generated by other components that Impala could not read.
Bug: IMPALA-694
Resolution: The underlying issue is being addressed by a fix in the Parquet libraries. Impala 1.2.2 works around the
problem and reads the existing data files.
Scanners use too much memory when reading past scan range
While querying a table with long column values, Impala could over-allocate memory leading to an out-of-memory
error. This problem was observed most frequently with tables using uncompressed RCFile or text data files.
Bug: IMPALA-525
Resolution: Fixed in 1.2.1
Excessive memory consumption when query tables with 1k columns (Parquet file)
Impala could encounter an out-of-memory condition setting up work areas for Parquet tables with many columns. The
fix reduces the size of the allocated memory when not actually needed to hold table data.
Bug: IMPALA-652
Resolution: Fixed in 1.2.1
"block size is too big" error with Snappy-compressed RCFile containing null
Queries could fail with a “block size is too big” error, due to NULL values in RCFile tables using Snappy
compression.
Bug: IMPALA-482
| Impala Release Notes | 876
Cannot query RC file for table that has more columns than the data file
Queries could fail if an Impala RCFile table was defined with more columns than in the corresponding RCFile data
files.
Bug: IMPALA-510
Update the serde name we write into the metastore for Parquet tables
The SerDes class string written into Parquet data files created by Impala was updated for compatibility with Parquet
support in Hive. See Incompatible Changes Introduced in Impala 1.1.1 on page 836 for the steps to update older
Parquet data files for Hive compatibility.
Bug: IMPALA-485
Impala continues to allocate more memory even though it has exceed its mem-limit
Queries could allocate substantially more memory than specified in the impalad -mem_limit startup option. The
fix causes more frequent checking of the limit during query execution.
Bug: IMPALA-520
10-20% perf regression for most queries across all table formats
This issue is due to a performance tradeoff between systems running many queries concurrently, and systems running
a single query. Systems running only a single query could experience lower performance than in early beta releases.
Systems running many queries simultaneously should experience higher performance than in the beta releases.
planner fails with "Join requires at least one equality predicate between the two tables" when
"from" table order does not match "where" join order
A query could fail if it involved 3 or more tables and the last join table was specified as a subquery.
Bug: IMPALA-85
Bug: IMPALA-257
Comments in impala-shell in interactive mode are not handled properly causing syntax errors or
wrong results
The impala-shell interpreter did not accept comment entered at the command line, making it problematic to copy
and paste from scripts or other code examples.
Bug: IMPALA-192
Cancelled queries sometimes aren't removed from the inflight query list
The Impala web UI would sometimes display a query as if it were still running, after the query was cancelled.
Bug: IMPALA-364
Impala's 1.0.1 Shell Broke Python 2.4 Compatibility (AttributeError: 'module' object has no attribute
'field_size_limit)
The impala-shell command in Impala 1.0.1 does not work with Python 2.4, which is the default on Red Hat 5.
For the impala-shell command in Impala 1.0, the -o option (pipe output to a file) does not work with Python
2.4.
Bug: IMPALA-396
Impala parquet scanner cannot read all data files generated by other frameworks
Impala might issue an erroneous error message when processing a Parquet data file produced by a non-Impala
Hadoop component.
Bug: IMPALA-333
Resolution: Fixed
Impala is unable to query RCFile tables which describe fewer columns than the file's header.
If an RCFile table definition had fewer columns than the fields actually in the data files, queries would fail.
Bug: IMPALA-293
Resolution: Fixed
Impala returns 0 for bad time values in UNIX_TIMESTAMP, Hive returns NULL
Impala returns 0 for bad time values in UNIX_TIMESTAMP, Hive returns NULL.
Impala:
Hive:
Bug: IMPALA-16
Anticipated Resolution: Fixed
Undeterministically receive "ERROR: unknown row bach destination..." and "ERROR: Invalid query
handle" from impala shell when running union query
A query containing both UNION and LIMIT clauses could intermittently cause the impalad process to halt with a
segmentation fault.
Bug: IMPALA-183
Resolution: Fixed
| Impala Release Notes | 879
Add some library version validation logic to impalad when loading impala-lzo shared library
No validation was done to check that the impala-lzo shared library was compatible with the version of Impala,
possibly leading to a crash when using LZO-compressed text files.
Bug: IMPALA-234
Resolution: Fixed
Workaround: Always upgrade the impala-lzo library at the same time as you upgrade Impala itself.
Problems inserting into tables with TIMESTAMP partition columns leading table metadata loading
failures and failed dchecks
INSERT statements for tables partitioned on columns involving datetime types could appear to succeed, but cause
errors for subsequent queries on those tables. The problem was especially serious if an improperly formatted
timestamp value was specified for the partition key.
Bug: IMPALA-238
Resolution: Fixed
Ctrl-C sometimes interrupts shell in system call, rather than cancelling query
Pressing Ctrl-C in the impala-shell interpreter could sometimes display an error and return control to the shell,
making it impossible to cancel the query.
Bug: IMPALA-243
Resolution: Fixed
Excessive mem usage for certain queries which are very selective
Some queries that returned very few rows experienced unnecessary memory usage.
Bug: IMPALA-288
Resolution: Fixed
Right outer Join includes NULLs as well and hence wrong result count
The result set from a right outer join query could include erroneous rows containing NULL values.
| Impala Release Notes | 881
Bug: IMPALA-90
Resolution: Fixed
DDL statements (CREATE/ALTER/DROP TABLE) are not supported in the Impala Beta Release
Resolution: Fixed in 0.7
Impala does not currently allow limiting the memory consumption of a single query
It is currently not possible to limit the memory consumption of a single query. All tables on the right hand side of
JOIN statements need to be able to fit in memory. If they do not, Impala may crash due to out of memory errors.
Resolution: Fixed in 0.7
Aggregate of a subquery result set returns wrong results if the subquery contains a 'limit' and data
is distributed across multiple nodes
Aggregate of a subquery result set returns wrong results if the subquery contains a 'limit' clause and data is distributed
across multiple nodes. From the query plan, it looks like we are just summing the results from each worker node.
Bug: IMPALA-20
Resolution: Fixed in 0.7
Partition pruning for arbitrary predicates that are fully bound by a particular partition column
We currently cannot utilize a predicate like "country_code in ('DE', 'FR', 'US')" to do partitioning pruning, because
that requires an equality predicate or a binary comparison.
We should create a superclass of planner.ValueRange, ValueSet, that can be constructed with an arbitrary predicate,
and whose isInRange(analyzer, valueExpr) constructs a literal predicate by substitution of the valueExpr into the
predicate.
Bug: IMPALA-144
Resolution: Fixed in 0.7
| Impala Release Notes | 882
Queries may fail on secure environment due to impalad Kerberos ticket expiration
Queries may fail on secure environment due to impalad Kerberos tickets expiring. This can happen if the Impala
-kerberos_reinit_interval flag is set to a value ten minutes or less. This may lead to an impalad
requesting a ticket with a lifetime that is less than the time to the next ticket renewal.
Bug: IMPALA-64
Resolution: Fixed in 0.6
Concurrent queries may fail when Impala uses Thrift to communicate with the Hive Metastore
Concurrent queries may fail when Impala is using Thrift to communicate with part of the Hive Metastore such as the
Hive Metastore Service. In such a case, the error get_fields failed: out of sequence response"
may occur because Impala shared a single Hive Metastore Client connection across threads. With Impala 0.6, a
separate connection is used for each metadata request.
Bug: IMPALA-48
Resolution: Fixed in 0.6
Backend client connections not getting cached causes an observable latency in secure clusters
Backend impalads do not cache connections to the coordinator. On a secure cluster, this introduces a latency
proportional to the number of backend clients involved in query execution, as the cost of establishing a secure
connection is much higher than in the non-secure case.
Bug: IMPALA-38
Resolution: Fixed in 0.5
Concurrent queries may fail with error: "Table object has not been been initialised : `PARTITIONS`"
Concurrent queries may fail with error: "Table object has not been been initialised :
`PARTITIONS`". This was due to a lack of locking in the Impala table/database metadata cache.
Bug: IMPALA-30
Resolution: Fixed in 0.5
UNIX_TIMESTAMP format behaviour deviates from Hive when format matches a prefix of the time
value
The Impala UNIX_TIMESTAMP(val, format) operation compares the length of format and val and returns NULL if
they do not match. Hive instead effectively truncates val to the length of the format parameter.
Bug: IMPALA-15
Resolution: Fixed in 0.5
Order by on a string column produces incorrect results if there are empty strings
Resolution: Fixed in 0.4
| Impala Release Notes | 884
Impala cannot read from HBase tables that are not created as external tables in the hive metastore.
Attempting to select from these tables fails.
Resolution: Fixed in 0.3
Certain queries that contain OUTER JOINs may return incorrect results
Queries that contain OUTER JOINs may not return the correct results if there are predicates referencing any of the
joined tables in the WHERE clause.
Resolution: Fixed in 0.3.
An insert with a limit that runs as more than one query fragment inserts more rows than the limit.
For example:
Impala server raises a null pointer exception when running an HBase query.
When querying an HBase table whose row-key is string type, the Impala server may raise a null pointer exception.
| Impala Release Notes | 885