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 SELECT queries support:
- SQL data types: BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, TIMESTAMP, STRING.
- An optional WITH clause before the SELECT keyword, to define a subquery whose name or column names can be referenced from later in the main query. This clause lets you abstract repeated clauses, such as aggregation functions, that are referenced multiple times in the same query.
- DISTINCT clause per query. See DISTINCT Operator for details.
- Subqueries in a FROM clause.
- WHERE, GROUP BY, HAVING clauses.
-
ORDER
BY. Impala requires that queries using this keyword also include a
LIMIT
clause.
Note
: ORDER BY queries require limits on results. These limits can be set when you start Impala or they can be set in the Impala shell. Setting query options though ODBC or JDBC is not supported at this time, so in those cases, if you are using either of those connectors, set the limit value when starting Impala. For example, to set this value in the shell, use a command similar to:
[impalad-host:21000] > set default_order_by_limit=50000
To set the limit when starting Impala, include the -default_query_option startup parameter for the impalad daemon. For example, to start Impala with a result limit for ORDER BY queries, use a command similar to:
$ GLOG_v=1 nohup impalad -state_store_host=state_store_hostname -hostname=impalad_hostname -default_query_options default_order_by_limit=50000
-
Impala supports a wide variety of JOIN clauses. Left, right, semi, full, and outer joins are supported in all Impala versions. The CROSS JOIN operator is available in Impala 1.2.2 and higher. During performance tuning, you can override the reordering of join clauses that Impala does internally by including the keyword STRAIGHT_JOIN immediately after the SELECT keyword
See Joins for details and examples of join queries.
- UNION ALL.
- LIMIT.
- External tables.
- Relational operators such as greater than, less than, or equal to.
- Arithmetic operators such as addition or subtraction.
- Logical/Boolean operators AND, OR, and NOT. Impala does not support the corresponding symbols &&, ||, and !.
- Common SQL built-in functions such as COUNT, SUM, CAST, LIKE, IN, BETWEEN, and COALESCE. Impala specifically supports built-ins described in Built-in Functions.
Joins
A join query is one that combines data from two or more tables, and returns a result set containing items from some or all of those tables.
Syntax:
Impala supports a wide variety of JOIN clauses. Left, right, semi, full, and outer joins are supported in all Impala versions. The CROSS JOIN operator is available in Impala 1.2.2 and higher. During performance tuning, you can override the reordering of join clauses that Impala does internally by including the keyword STRAIGHT_JOIN immediately after the SELECT keyword
SELECT select_list FROM table_or_subquery1 [INNER] JOIN table_or_subquery2 | table_or_subquery1 {LEFT [OUTER] | RIGHT [OUTER] | FULL [OUTER]} JOIN table_or_subquery2 | table_or_subquery1 LEFT SEMI JOIN table_or_subquery2 [ ON col1 = col2 [AND col3 = col4 ...] | USING (col1 [, col2 ...]) ] [other_join_clause ...] [ WHERE where_clauses ] SELECT select_list FROM table_or_subquery1, table_or_subquery2 [, table_or_subquery3 ...] [other_join_clause ...] WHERE col1 = col2 [AND col3 = col4 ...] SELECT select_list FROM table_or_subquery1 CROSS JOIN table_or_subquery2 [other_join_clause ...] [ WHERE where_clauses ]
SQL-92 and SQL-89 Joins:
Queries with the explicit JOIN keywords are known as SQL-92 style joins, referring to the level of the SQL standard where they were introduced. The corresponding ON or USING clauses clearly show which columns are used as the join keys in each case:
SELECT t1.c1, t2.c2 FROM t1 JOIN t2 ON t1.id = t2.id and t1.type_flag = t2.type_flag WHERE t1.c1 > 100; SELECT t1.c1, t2.c2 FROM t1 JOIN t2 USING (id, type_flag) WHERE t1.c1 > 100;
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.
SELECT t1.c1, t2.c2 FROM t1, t2 WHERE t1.id = t2.id AND t1.type_flag = t2.type_flag AND t1.c1 > 100;
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:
-- Combine fields from both parent and child rows. SELECT lhs.id, rhs.parent, lhs.c1, rhs.c2 FROM tree_data lhs, tree_data rhs WHERE lhs.id = rhs.parent;
Cartesian joins:
SELECT ... FROM t1 JOIN t2; SELECT ... FROM t1, t2;If you intend to join the tables based on common values, add ON or WHERE clauses to compare columns across the tables. If you truly intend to do a Cartesian join, use the CROSS JOIN keyword as the join operator. The CROSS JOIN form does not use any ON clause, because it produces a result set with all combinations of rows from the left-hand and right-hand tables. The result set can still be filtered by subsequent WHERE clauses. For example:
SELECT ... FROM t1 CROSS JOIN t2; SELECT ... FROM t1 CROSS JOIN t2 WHERE tests_on_non_join_columns;
Inner and outer joins:
An inner join is the most common and familiar type: rows in the result set contain the requested columns from the appropriate tables, for all combinations of rows where the join columns of the tables have identical values. If a 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:
-- The following 3 forms are all equivalent. SELECT t1.id, c1, c2 FROM t1, t2 WHERE t1.id = t2.id; SELECT t1.id, c1, c2 FROM t1 JOIN t2 ON t1.id = t2.id; SELECT t1.id, c1, c2 FROM t1 INNER JOIN t2 ON t1.id = t2.id;
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:
SELECT * FROM t1 LEFT OUTER JOIN t2 ON t1.id = t2.id; SELECT * FROM t1 RIGHT OUTER JOIN t2 ON t1.id = t2.id; SELECT * FROM t1 FULL OUTER JOIN t2 ON t1.id = t2.id;
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:
SELECT ... FROM t1 CROSS JOIN t2 WHERE t1.total > t2.maximum_price;
Semi-joins:
Semi-joins are a relatively rarely used variation. With the left semi-join (the only kind of semi-join available with Impala), 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.
SELECT t1.c1, t1.c2, t1.c2 FROM t1 LEFT SEMI JOIN t2 ON t1.id = t2.id;
Natural joins (not supported):
Impala does not support the NATURAL JOIN operator, again to avoid inconsistent or huge result sets. Natural joins do away with the ON and USING clauses, and instead automatically join on all columns with the same names in the left-hand and right-hand tables. This kind of query is not recommended for rapidly evolving data structures such as are typically used in Hadoop. Thus, Impala does not support the NATURAL JOIN syntax, which can produce different query results as columns are added to or removed from tables.
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:
-- 'NATURAL' is interpreted as an alias for 't1' and Impala attempts an inner join, -- resulting in an error because inner joins require explicit comparisons between columns. SELECT t1.c1, t2.c2 FROM t1 NATURAL JOIN t2; ERROR: NotImplementedException: Join with 't2' requires at least one conjunctive equality predicate. To perform a Cartesian product between two tables, use a CROSS JOIN. -- 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);
Anti-joins (not supported):
Impala does not support WHERE clauses such as IN (subquery), NOT IN (subquery), EXISTS (subquery), and NOT EXISTS (subquery). Therefore from a practical standpoint, you cannot express an anti-join condition, where values from one table are returned only if no matching values are present in another table.
Usage notes:
You typically use join queries in situations like these:
-
When related data arrives from different sources, with each data set physically residing in a separate table.
For example, you might have address data from business records that you cross-check against phone listings or
census data.
Note
: Impala can join tables of different file formats, including Impala-managed tables and HBase tables. For example, you might keep small dimension tables in HBase, for convenience of single-row lookups and updates, and for the larger fact tables use Parquet or other binary file format optimized for scan operations. Then, you can issue a join query to cross-reference the fact tables with the dimension tables. -
When data is normalized, a technique for reducing data duplication by dividing it across multiple tables. This
kind of organization is often found in data that comes from traditional relational database systems. For
example, instead of repeating some long string such as a customer name in multiple tables, each table might
contain a numeric customer ID. Queries that need to display the customer name could
" join" the table that specifies which customer ID corresponds to which name. - When certain columns are rarely needed for queries, so they are moved into separate tables to reduce overhead for common queries. For example, a biography field might be rarely needed in queries on employee data. Putting that field in a separate table reduces the amount of I/O for common queries on employee addresses or phone numbers. Queries that do need the biography column can retrieve it by performing a join with that separate table.
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:
select t1.c1 as first_id, t2.c2 as second_id from t1 join t2 on first_id = second_id; select fact.custno, dimension.custno from customer_data as fact join customer_address as dimension using (custno)
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. Use SHOW TABLE STATS table_name and SHOW COLUMN STATS table_name to check.
- 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. In Impala 1.2.2 and higher, use the Impala COMPUTE STATS statement to collect statistics at both the table and column levels, and keep the statistics up to date after any substantial INSERT or LOAD DATA operation.
- 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.
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.
[localhost:21000] > select c_last_name, ca_city from customer join customer_address where c_customer_sk = ca_address_sk; +-------------+-----------------+ | c_last_name | ca_city | +-------------+-----------------+ | Lewis | Fairfield | | Moses | Fairview | | Hamilton | Pleasant Valley | | White | Oak Ridge | | Moran | Glendale | ... | Richards | Lakewood | | Day | Lebanon | | Painter | Oak Hill | | Bentley | Greenfield | | Jones | Stringtown | +-------------+------------------+ Returned 50000 row(s) in 9.82s
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.
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:
[localhost:21000] > select c_last_name, ca_city from customer, customer_address where c_customer_sk = ca_address_sk limit 10; +-------------+-----------------+ | c_last_name | ca_city | +-------------+-----------------+ | Lewis | Fairfield | | Moses | Fairview | | Hamilton | Pleasant Valley | | White | Oak Ridge | | Moran | Glendale | | Sharp | Lakeview | | Wiles | Farmington | | Shipman | Union | | Gilbert | New Hope | | Brunson | Martinsville | +-------------+-----------------+ Returned 10 row(s) in 0.63s
Or you might use additional comparison operators or aggregation functions to condense a large result set into a smaller set of values:
[localhost:21000] > -- Find the names of customers who live in one particular town. [localhost:21000] > select distinct c_last_name from customer, customer_address where c_customer_sk = ca_address_sk and ca_city = "Green Acres"; +---------------+ | c_last_name | +---------------+ | Hensley | | Pearson | | Mayer | | Montgomery | | Ricks | ... | Barrett | | Price | | Hill | | Hansen | | Meeks | +---------------+ Returned 332 row(s) in 0.97s [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
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 Hints.
See these tutorials for examples of different kinds of joins:
ORDER BY Clause
The familiar ORDER BY clause of a SELECT statement sorts the result set based on the values from one or more columns. For distributed queries, this is a relatively expensive operation, because the entire result set must be produced and transferred to one node before the sorting can happen. This 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.
The full syntax for the ORDER BY clause is:
ORDER BY col1 [, col2 ...] [ASC | DESC] [NULLS FIRST | NULLS LAST]
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.
See NULL 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.)
Impala requires any query including an ORDER BY clause to also use a LIMIT clause. Because sorting a huge result set can require so much memory, and top-N queries are so common for Impala use cases, this combination of clauses prevents accidental excessive memory consumption on the coordinator node for the query. You can specify the LIMIT clause as part of the query, or set a default limit for all queries in a session with the command SET DEFAULT_ORDER_BY_LIMIT=... in impala-shell, or set the limit instance-wide with the -default_query_options default_order_by_limit=... option when starting impalad.
See SELECT Statement for further examples of queries with the ORDER BY clause. For information about the query options you can set to fine-tune the behavior of the ORDER BY clause and avoid changing your SQL to add an explicit LIMIT clause, see DEFAULT_ORDER_BY_LIMIT and ABORT_ON_DEFAULT_LIMIT_EXCEEDED.
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.
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 | +-------+-----------------+--------------------------+
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
select ss_wholesale_cost, avg(ss_quantity * ss_sales_price) as avg_revenue_per_sale from sales group by ss_wholesale_cost order by avg_revenue_per_sale desc limit 5; +-------------------+----------------------+ | ss_wholesale_cost | avg_revenue_per_sale | +-------------------+----------------------+ | 96.94000244140625 | 4454.351539300434 | | 95.93000030517578 | 4423.119941283189 | | 98.37999725341797 | 4332.516490316291 | | 97.97000122070312 | 4330.480601655014 | | 98.52999877929688 | 4291.316953108634 | +-------------------+----------------------+
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.
HAVING Clause
LIMIT Clause
The LIMIT clause in a SELECT query sets a maximum number of rows for the result set. It 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, for a query with no ORDER BY clause.
- 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.
Usage notes:
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.
Impala requires any query including an ORDER BY clause to also use a LIMIT clause. Because sorting a huge result set can require so much memory, and top-N queries are so common for Impala use cases, this combination of clauses prevents accidental excessive memory consumption on the coordinator node for the query. You can specify the LIMIT clause as part of the query, or set a default limit for all queries in a session with the command SET DEFAULT_ORDER_BY_LIMIT=... in impala-shell, or set the limit instance-wide with the -default_query_options default_order_by_limit=... option when starting impalad.
See ORDER BY Clause for details, and the query options you can use to avoid adding an explicit LIMIT clause to each ORDER BY query.
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
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.
[localhost:21000] > create database limits; [localhost:21000] > use limits; [localhost:21000] > create table numbers (x int); [localhost:21000] > insert into numbers values (1), (3), (4), (5), (2); Inserted 5 rows in 1.34s [localhost:21000] > select x from numbers limit 100; +---+ | 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 queries, you use the ORDER BY and LIMIT clauses together. If you have set the DEFAULT_ORDER_BY_LIMIT query option so that you do not have to explicitly add a LIMIT clause to each ORDER BY query, you can also set the ABORT_ON_DEFAULT_LIMIT_EXCEEDED query option to avoid truncating the result set by accident.
[localhost:21000] > select x from numbers order by x; ERROR: NotImplementedException: ORDER BY without LIMIT currently not supported [localhost:21000] > set default_order_by_limit=1000; DEFAULT_ORDER_BY_LIMIT set to 1000 [localhost:21000] > select x from numbers order by x; +---+ | x | +---+ | 1 | | 2 | | 3 | | 4 | | 5 | +---+ Returned 5 row(s) in 0.35s [localhost:21000] > set abort_on_default_limit_exceeded=true; ABORT_ON_DEFAULT_LIMIT_EXCEEDED set to true [localhost:21000] > set default_order_by_limit=3; DEFAULT_ORDER_BY_LIMIT set to 3 [localhost:21000] > select x from numbers order by x; ERROR: DEFAULT_ORDER_BY_LIMIT has been exceeded. Cancelling query ...
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).
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
Examples:
The following example shows how you could run a
[localhost:21000] > create table numbers (x int); [localhost:21000] > insert into numbers select x from very_long_sequence; Inserted 1000000 rows in 1.34s [localhost:21000] > select x from numbers order by x limit 5 offset 0; +----+ | x | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | +----+ Returned 5 row(s) in 0.26s [localhost:21000] > select x from numbers order by x limit 5 offset 5; +----+ | x | +----+ | 6 | | 7 | | 8 | | 9 | | 10 | +----+ Returned 5 row(s) in 0.23s
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:
query_1 UNION [DISTINCT | ALL] query_2
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, the LIMIT clause is required as usual. If you set the DEFAULT_ORDER_BY_LIMIT query option, 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, we set up some sample data, including duplicate 1 values.
[localhost:21000] > create table few_ints (x int); [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.
[localhost:21000] > select x from few_ints order by x; +---+ | x | +---+ | 1 | | 1 | | 2 | | 3 | +---+ Returned 4 row(s) in 0.41s [localhost:21000] > select x from few_ints union all select x from few_ints; +---+ | x | +---+ | 1 | | 1 | | 2 | | 3 | | 1 | | 1 | | 2 | | 3 | +---+ Returned 8 row(s) in 0.42s [localhost:21000] > select * from (select x from few_ints union all select x from few_ints) as t1 order by x; +---+ | x | +---+ | 1 | | 1 | | 1 | | 1 | | 2 | | 2 | | 3 | | 3 | +---+ Returned 8 row(s) in 0.53s [localhost:21000] > select x from few_ints union all select 10; +----+ | x | +----+ | 10 | | 1 | | 1 | | 2 | | 3 | +----+ Returned 5 row(s) in 0.38s
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
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
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 2 subqueries that can be referenced from the body of a longer query. with t1 as (select 1), t2 as (select 2) insert into tab select * from t1 union all select * from t2; -- 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;
Hints
The Impala SQL dialect 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. The hints are represented as keywords surrounded by [] square brackets; include the brackets in the text of the SQL statement.
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.)
To see which join strategy is used for a particular query, examine the EXPLAIN output for that query.
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 for details.
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:
select customer.address, state_lookup.state_name from customer join [broadcast] state_lookup on customer.state_id = state_lookup.state_id;
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:
select weather.wind_velocity, geospatial.altitude from weather join [shuffle] geospatial on weather.lat = geospatial.lat and weather.long = geospatial.long;
For joins involving three or more tables, the hint applies to the tables on either side of that specific JOIN keyword. The joins are processed 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:
select t1.name, t2.id, t3.price from t1 join [shuffle] t2 join [broadcast] t3 on t1.id = t2.id and t2.id = t3.id;
For more background information and performance considerations for join queries, see Joins.
- These hints are available in Impala 1.2.2 and higher.
- You would only use these hints if an INSERT into a partitioned Parquet table was failing due to capacity limits, or if such an INSERT was succeeding but with less-than-optimal performance.
- To use these hints, put the hint keyword [SHUFFLE] or [NOSHUFFLE] (including the square brackets) after the PARTITION clause, immediately before the SELECT keyword.
- [SHUFFLE] selects an execution plan that minimizes the number of files being written simultaneously to HDFS, and the number of 1 GB memory buffers holding data for individual partitions. Thus it reduces overall resource usage for the INSERT operation, allowing some INSERT 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 constructed on the same node.
- [NOSHUFFLE] selects an execution plan that might be faster overall, but might also produce a larger number of small data files or exceed capacity limits, causing the INSERT operation to fail. Use [SHUFFLE] in cases where an INSERT statement fails or runs inefficiently due to all nodes attempting to construct data for all partitions.
- Impala automatically uses the [SHUFFLE] method if any partition key column in the source table, mentioned in the INSERT ... SELECT query, does not have column statistics. In this case, only the [NOSHUFFLE] hint would have any effect.
- If column statistics are available for all partition key columns in the source table mentioned in the INSERT ... SELECT 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 INSERT operation. In this case, you might need the [SHUFFLE] or the [NOSHUFFLE] hint to override the execution plan selected by Impala.
DISTINCT Operator
The DISTINCT operator in a SELECT statement filters the result set to remove duplicates:
-- Returns the unique values from one column. -- NULL is included in the set of values if any rows have a NULL in this column. select distinct c_birth_country from customer; -- Returns the unique combinations of values from multiple columns. select distinct c_salutation, c_last_name from customer;
You can use DISTINCT in combination with an aggregation function, typically COUNT(), to find how many different values a column contains:
-- Counts the unique values from one column. -- NULL is not included as a distinct value in the count. select count(distinct c_birth_country) from customer; -- Counts the unique combinations of values from multiple columns. select count(distinct c_salutation, c_last_name) from customer;
One construct that Impala SQL does not support is using DISTINCT in more than one aggregation function in the same query. For example, you could not have a single query with both COUNT(DISTINCT c_first_name) and COUNT(DISTINCT c_last_name) in the SELECT list.
Impala only allows a single COUNT(DISTINCT columns) expression in each query. To produce the same result as multiple COUNT(DISTINCT) expressions, you can use the following technique for queries involving a single table:
select v1.c1 result1, v2.c1 result2 from (select count(distinct col1) as c1 from t1) v1 cross join (select count(distinct col2) as c1 from t1) v2;
If you do not need precise accuracy, you can produce an estimate of the distinct values for a column by specifying COUNT(NDV(column)); a query can contain multiple instances of COUNT(NDV(column)).
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.
<< REFRESH Statement | SHOW Statement >> | |