Defining Queries

HAWQ is based on the PostgreSQL implementation of the SQL standard. SQL commands are typically entered using the standard PostgreSQL interactive terminal psql, but other programs that have similar functionality can be used as well.

SQL Lexicon

SQL is a standard language for accessing databases. The language consists of elements that enable data storage, retrieval, analysis, viewing, and so on. You use SQL commands to construct queries and commands that the HAWQ engine understands.

SQL queries consist of a sequence of commands. Commands consist of a sequence of valid tokens in correct syntax order, terminated by a semicolon (;).

H uses PostgreSQL’s structure and syntax, with some exceptions. For more information about SQL rules and concepts in PostgreSQL, see “SQL Syntax” in the PostgreSQL documentation.

SQL Value Expressions

SQL value expressions consist of one or more values, symbols, operators, SQL functions, and data. The expressions compare data or perform calculations and return a value as the result. Calculations include logical, arithmetic, and set operations.

The following are value expressions:

  • Aggregate expressions
  • Array constructors
  • Column references
  • Constant or literal values
  • Correlated subqueries
  • Field selection expressions
  • Function calls
  • New column values in an INSERT
  • Operator invocation column references
  • Positional parameter references, in the body of a function definition or prepared statement
  • Row constructors
  • Scalar subqueries
  • Search conditions in a WHERE clause
  • Target lists of a SELECT command
  • Type casts
  • Value expressions in parentheses, useful to group sub-expressions and override precedence
  • Window expressions

SQL constructs such as functions and operators are expressions but do not follow any general syntax rules. For more information about these constructs, see Using Functions and Operators.

Column References

A column reference has the form:

  1. correlation.columnname

Here, correlation is the name of a table (possibly qualified with a schema name) or an alias for a table defined with a FROM clause or one of the keywords NEW or OLD. NEW and OLD can appear only in rewrite rules, but you can use other correlation names in any SQL statement. If the column name is unique across all tables in the query, you can omit the “correlation.” part of the column reference.

Positional Parameters

Positional parameters are arguments to SQL statements or functions that you reference by their positions in a series of arguments. For example, $1 refers to the first argument, $2 to the second argument, and so on. The values of positional parameters are set from arguments external to the SQL statement or supplied when SQL functions are invoked. Some client libraries support specifying data values separately from the SQL command, in which case parameters refer to the out-of-line data values. A parameter reference has the form:

  1. $number

For example:

  1. CREATE FUNCTION dept(text) RETURNS dept
  2. AS $$ SELECT * FROM dept WHERE name = $1 $$
  3. LANGUAGE SQL;

Here, the $1 references the value of the first function argument whenever the function is invoked.

Subscripts

If an expression yields a value of an array type, you can extract a specific element of the array value as follows:

  1. expression[subscript]

You can extract multiple adjacent elements, called an array slice, as follows (including the brackets):

  1. expression[lower_subscript:upper_subscript]

Each subscript is an expression and yields an integer value.

Array expressions usually must be in parentheses, but you can omit the parentheses when the expression to be subscripted is a column reference or positional parameter. You can concatenate multiple subscripts when the original array is multidimensional. For example (including the parentheses):

  1. mytable.arraycolumn[4]
  1. mytable.two_d_column[17][34]
  1. $1[10:42]
  1. (arrayfunction(a,b))[42]

Field Selections

If an expression yields a value of a composite type (row type), you can extract a specific field of the row as follows:

  1. expression.fieldname

The row expression usually must be in parentheses, but you can omit these parentheses when the expression to be selected from is a table reference or positional parameter. For example:

  1. mytable.mycolumn
  1. $1.somecolumn
  1. (rowfunction(a,b)).col3

A qualified column reference is a special case of field selection syntax.

Operator Invocations

Operator invocations have the following possible syntaxes:

  1. expression operator expression(binary infix operator)
  1. operator expression(unary prefix operator)
  1. expression operator(unary postfix operator)

Where operator is an operator token, one of the key words AND, OR, or NOT, or qualified operator name in the form:

  1. OPERATOR(schema.operatorname)

Available operators and whether they are unary or binary depends on the operators that the system or user defines. For more information about built-in operators, see Built-in Functions and Operators.

Function Calls

The syntax for a function call is the name of a function (possibly qualified with a schema name), followed by its argument list enclosed in parentheses:

  1. function ([expression [, expression ... ]])

For example, the following function call computes the square root of 2:

  1. sqrt(2)

Aggregate Expressions

An aggregate expression applies an aggregate function across the rows that a query selects. An aggregate function performs a calculation on a set of values and returns a single value, such as the sum or average of the set of values. The syntax of an aggregate expression is one of the following:

  • aggregate_name ([ , ... ] ) [FILTER (WHERE condition)] — operates across all input rows for which the expected result value is non-null. ALL is the default.
  • aggregate_name(ALLexpression[ , ... ] ) [FILTER (WHERE condition)] — operates identically to the first form because ALL is the default
  • aggregate_name(DISTINCT expression[ , ... ] ) [FILTER (WHERE condition)] — operates across all distinct non-null values of input rows
  • aggregate_name(*) [FILTER (WHERE condition)] — operates on all rows with values both null and non-null. Generally, this form is most useful for the count(*) aggregate function.

Where aggregate_name is a previously defined aggregate (possibly schema-qualified) and expression is any value expression that does not contain an aggregate expression.

For example, count(*) yields the total number of input rows, count(f1) yields the number of input rows in which f1 is non-null, and count(distinct f1) yields the number of distinct non-null values of f1.

You can specify a condition with the FILTER clause to limit the input rows to the aggregate function. For example:

  1. SELECT count(*) FILTER (WHERE gender='F') FROM employee;

The WHERE condition of the FILTER clause cannot contain a set-returning function, subquery, window function, or outer reference. If you use a user-defined aggregate function, declare the state transition function as STRICT (see CREATE AGGREGATE).

For predefined aggregate functions, see Built-in Functions and Operators. You can also add custom aggregate functions.

HAWQ provides the MEDIAN aggregate function, which returns the fiftieth percentile of the PERCENTILE_CONT result and special aggregate expressions for inverse distribution functions as follows:

  1. PERCENTILE_CONT(_percentage_) WITHIN GROUP (ORDER BY _expression_)
  1. PERCENTILE_DISC(_percentage_) WITHIN GROUP (ORDER BY _expression_)

Currently you can use only these two expressions with the keyword WITHIN GROUP.

Limitations of Aggregate Expressions

The following are current limitations of the aggregate expressions:

  • HAWQ does not support the following keywords: ALL, DISTINCT, FILTER and OVER. See Advanced Aggregate Functions for more details.
  • An aggregate expression can appear only in the result list or HAVING clause of a SELECT command. It is forbidden in other clauses, such as WHERE, because those clauses are logically evaluated before the results of aggregates form. This restriction applies to the query level to which the aggregate belongs.
  • When an aggregate expression appears in a subquery, the aggregate is normally evaluated over the rows of the subquery. If the aggregate’s arguments contain only outer-level variables, the aggregate belongs to the nearest such outer level and evaluates over the rows of that query. The aggregate expression as a whole is then an outer reference for the subquery in which it appears, and the aggregate expression acts as a constant over any one evaluation of that subquery. See Scalar Subqueries and Built-in functions and operators.
  • HAWQ does not support DISTINCT with multiple input expressions.

Window Expressions

Window expressions allow application developers to more easily compose complex online analytical processing (OLAP) queries using standard SQL commands. For example, with window expressions, users can calculate moving averages or sums over various intervals, reset aggregations and ranks as selected column values change, and express complex ratios in simple terms.

A window expression represents the application of a window function applied to a window frame, which is defined in a special OVER() clause. A window partition is a set of rows that are grouped together to apply a window function. Unlike aggregate functions, which return a result value for each group of rows, window functions return a result value for every row, but that value is calculated with respect to the rows in a particular window partition. If no partition is specified, the window function is computed over the complete intermediate result set.

The syntax of a window expression is:

  1. window_function ( [expression [, ...]] ) OVER ( window_specification )

Where window_function is one of the functions listed in Window functions, expression is any value expression that does not contain a window expression, and window_specification is:

  1. [window_name]
  2. [PARTITION BY expression [, ...]]
  3. [[ORDER BY expression [ASC | DESC | USING operator] [, ...]
  4. [{RANGE | ROWS}
  5. { UNBOUNDED PRECEDING
  6. | expression PRECEDING
  7. | CURRENT ROW
  8. | BETWEEN window_frame_bound AND window_frame_bound }]]

and where window_frame_bound can be one of:

  1. UNBOUNDED PRECEDING
  2. expression PRECEDING
  3. CURRENT ROW
  4. expression FOLLOWING
  5. UNBOUNDED FOLLOWING

A window expression can appear only in the select list of a SELECT command. For example:

  1. SELECT count(*) OVER(PARTITION BY customer_id), * FROM sales;

The OVER clause differentiates window functions from other aggregate or reporting functions. The OVER clause defines the window_specification to which the window function is applied. A window specification has the following characteristics:

  • The PARTITION BY clause defines the window partitions to which the window function is applied. If omitted, the entire result set is treated as one partition.
  • The ORDER BY clause defines the expression(s) for sorting rows within a window partition. The ORDER BY clause of a window specification is separate and distinct from the ORDER BY clause of a regular query expression. The ORDER BY clause is required for the window functions that calculate rankings, as it identifies the measure(s) for the ranking values. For OLAP aggregations, the ORDER BY clause is required to use window frames (the ROWS | RANGE clause).

Note: Columns of data types without a coherent ordering, such as time, are not good candidates for use in the ORDER BY clause of a window specification. Time, with or without a specified time zone, lacks a coherent ordering because addition and subtraction do not have the expected effects. For example, the following is not generally true: x::time < x::time + '2 hour'::interval

  • The ROWS/RANGE clause defines a window frame for aggregate (non-ranking) window functions. A window frame defines a set of rows within a window partition. When a window frame is defined, the window function computes on the contents of this moving frame rather than the fixed contents of the entire window partition. Window frames are row-based (ROWS) or value-based (RANGE).

Type Casts

A type cast specifies a conversion from one data type to another. HAWQ accepts two equivalent syntaxes for type casts:

  1. CAST ( expression AS type )
  2. expression::type

The CAST syntax conforms to SQL; the syntax with :: is historical PostgreSQL usage.

A cast applied to a value expression of a known type is a run-time type conversion. The cast succeeds only if a suitable type conversion function is defined. This differs from the use of casts with constants. A cast applied to a string literal represents the initial assignment of a type to a literal constant value, so it succeeds for any type if the contents of the string literal are acceptable input syntax for the data type.

You can usually omit an explicit type cast if there is no ambiguity about the type a value expression must produce; for example, when it is assigned to a table column, the system automatically applies a type cast. The system applies automatic casting only to casts marked “OK to apply implicitly” in system catalogs. Other casts must be invoked with explicit casting syntax to prevent unexpected conversions from being applied without the user’s knowledge.

Scalar Subqueries

A scalar subquery is a SELECT query in parentheses that returns exactly one row with one column. Do not use a SELECT query that returns multiple rows or columns as a scalar subquery. The query runs and uses the returned value in the surrounding value expression. A correlated scalar subquery contains references to the outer query block.

Correlated Subqueries

A correlated subquery (CSQ) is a SELECT query with a WHERE clause or target list that contains references to the parent outer clause. CSQs efficiently express results in terms of results of another query. HAWQ supports correlated subqueries that provide compatibility with many existing applications. A CSQ is a scalar or table subquery, depending on whether it returns one or multiple rows. HAWQ does not support correlated subqueries with skip-level correlations.

Correlated Subquery Examples

Example 1 – Scalar correlated subquery

  1. SELECT * FROM t1 WHERE t1.x
  2. > (SELECT MAX(t2.x) FROM t2 WHERE t2.y = t1.y);

Example 2 – Correlated EXISTS subquery

  1. SELECT * FROM t1 WHERE
  2. EXISTS (SELECT 1 FROM t2 WHERE t2.x = t1.x);

HAWQ uses one of the following methods to run CSQs:

  • Unnest the CSQ into join operations – This method is most efficient, and it is how HAWQ runs most CSQs, including queries from the TPC-H benchmark.
  • Run the CSQ on every row of the outer query – This method is relatively inefficient, and it is how HAWQ runs queries that contain CSQs in the SELECT list or are connected by OR conditions.

The following examples illustrate how to rewrite some of these types of queries to improve performance.

Example 3 - CSQ in the Select List

Original Query

  1. SELECT T1.a,
  2. (SELECT COUNT(DISTINCT T2.z) FROM t2 WHERE t1.x = t2.y) dt2
  3. FROM t1;

Rewrite this query to perform an inner join with t1 first and then perform a left join with t1 again. The rewrite applies for only an equijoin in the correlated condition.

Rewritten Query

  1. SELECT t1.a, dt2 FROM t1
  2. LEFT JOIN
  3. (SELECT t2.y AS csq_y, COUNT(DISTINCT t2.z) AS dt2
  4. FROM t1, t2 WHERE t1.x = t2.y
  5. GROUP BY t1.x)
  6. ON (t1.x = csq_y);

Example 4 - CSQs connected by OR Clauses

Original Query

  1. SELECT * FROM t1
  2. WHERE
  3. x > (SELECT COUNT(*) FROM t2 WHERE t1.x = t2.x)
  4. OR x < (SELECT COUNT(*) FROM t3 WHERE t1.y = t3.y)

Rewrite this query to separate it into two parts with a union on the OR conditions.

Rewritten Query

  1. SELECT * FROM t1
  2. WHERE x > (SELECT count(*) FROM t2 WHERE t1.x = t2.x)
  3. UNION
  4. SELECT * FROM t1
  5. WHERE x < (SELECT count(*) FROM t3 WHERE t1.y = t3.y)

To view the query plan, use EXPLAIN SELECT or EXPLAIN ANALYZE SELECT. Subplan nodes in the query plan indicate that the query will run on every row of the outer query, and the query is a candidate for rewriting. For more information about these statements, see Query Profiling.

Advanced Table Functions

HAWQ supports table functions with TABLE value expressions. You can sort input rows for advanced table functions with an ORDER BY clause. You can redistribute them with a SCATTER BY clause to specify one or more columns or an expression for which rows with the specified characteristics are available to the same process. This usage is similar to using a DISTRIBUTED BY clause when creating a table, but the redistribution occurs when the query runs.

Note: Based on the distribution of data, HAWQ automatically parallelizes table functions with TABLE value parameters over the nodes of the cluster.

Array Constructors

An array constructor is an expression that builds an array value from values for its member elements. A simple array constructor consists of the key word ARRAY, a left square bracket [, one or more expressions separated by commas for the array element values, and a right square bracket ]. For example,

  1. SELECT ARRAY[1,2,3+4];
  1. array
  2. ---------
  3. {1,2,7}

The array element type is the common type of its member expressions, determined using the same rules as for UNION or CASE constructs.

You can build multidimensional array values by nesting array constructors. In the inner constructors, you can omit the keyword ARRAY. For example, the following two SELECT statements produce the same result:

  1. SELECT ARRAY[ARRAY[1,2], ARRAY[3,4]];
  2. SELECT ARRAY[[1,2],[3,4]];
  1. array
  2. ---------------
  3. {{1,2},{3,4}}

Since multidimensional arrays must be rectangular, inner constructors at the same level must produce sub-arrays of identical dimensions.

Multidimensional array constructor elements are not limited to a sub-ARRAY construct; they are anything that produces an array of the proper kind. For example:

  1. CREATE TABLE arr(f1 int[], f2 int[]);
  2. INSERT INTO arr VALUES (ARRAY[[1,2],[3,4]],
  3. ARRAY[[5,6],[7,8]]);
  4. SELECT ARRAY[f1, f2, '{{9,10},{11,12}}'::int[]] FROM arr;
  1. array
  2. ------------------------------------------------
  3. {{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}}

You can construct an array from the results of a subquery. Write the array constructor with the keyword ARRAY followed by a subquery in parentheses. For example:

  1. SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
  1. ?column?
  2. -----------------------------------------------------------
  3. {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}

The subquery must return a single column. The resulting one-dimensional array has an element for each row in the subquery result, with an element type matching that of the subquery’s output column. The subscripts of an array value built with ARRAY always begin with 1.

Row Constructors

A row constructor is an expression that builds a row value (also called a composite value) from values for its member fields. For example,

  1. SELECT ROW(1,2.5,'this is a test');

Row constructors have the syntax rowvalue.*, which expands to a list of the elements of the row value, as when you use the syntax .* at the top level of a SELECT list. For example, if table t has columns f1 and f2, the following queries are the same:

  1. SELECT ROW(t.*, 42) FROM t;
  2. SELECT ROW(t.f1, t.f2, 42) FROM t;

By default, the value created by a ROW expression has an anonymous record type. If necessary, it can be cast to a named composite type — either the row type of a table, or a composite type created with CREATE TYPE AS. To avoid ambiguity, you can explicitly cast the value if necessary. For example:

  1. CREATE TABLE mytable(f1 int, f2 float, f3 text);
  2. CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1'
  3. LANGUAGE SQL;

In the following query, you do not need to cast the value because there is only one getf1() function and therefore no ambiguity:

  1. SELECT getf1(ROW(1,2.5,'this is a test'));
  1. getf1
  2. -------
  3. 1
  1. CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric);
  2. CREATE FUNCTION getf1(myrowtype) RETURNS int AS 'SELECT
  3. $1.f1' LANGUAGE SQL;

Now we need a cast to indicate which function to call:

  1. SELECT getf1(ROW(1,2.5,'this is a test'));
  1. ERROR: function getf1(record) is not unique
  1. SELECT getf1(ROW(1,2.5,'this is a test')::mytable);
  1. getf1
  2. -------
  3. 1
  1. SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype));
  1. getf1
  2. -------
  3. 11

You can use row constructors to build composite values to be stored in a composite-type table column or to be passed to a function that accepts a composite parameter.

Expression Evaluation Rules

The order of evaluation of subexpressions is undefined. The inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order.

If you can determine the result of an expression by evaluating only some parts of the expression, then other subexpressions might not be evaluated at all. For example, in the following expression:

  1. SELECT true OR somefunc();

somefunc() would probably not be called at all. The same is true in the following expression:

  1. SELECT somefunc() OR true;

This is not the same as the left-to-right evaluation order that Boolean operators enforce in some programming languages.

Do not use functions with side effects as part of complex expressions, especially in WHERE and HAVING clauses, because those clauses are extensively reprocessed when developing an execution plan. Boolean expressions (AND/OR/NOT combinations) in those clauses can be reorganized in any manner that Boolean algebra laws allow.

Use a CASE construct to force evaluation order. The following example is an untrustworthy way to avoid division by zero in a WHERE clause:

  1. SELECT ... WHERE x <> 0 AND y/x > 1.5;

The following example shows a trustworthy evaluation order:

  1. SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false
  2. END;

This CASE construct usage defeats optimization attempts; use it only when necessary.