Back to Cube

SQL API reference

docs-mintlify/reference/core-data-apis/sql-api/reference.mdx

1.6.4825.4 KB
Original Source

SQL API supports the following commands as well as functions and operators.

<Info>

If you'd like to propose a function or an operator to be supported in the SQL API, check the existing issues on GitHub. If there are no relevant issues, please file a new one.

</Info>

SQL commands

SELECT

Synopsis:

sql
SELECT select_expr [, ...]
  FROM from_item
    CROSS JOIN join_item
    ON join_criteria]*
  [ WHERE where_condition ]
  [ GROUP BY grouping_expression ]
  [ HAVING having_expression ]
  [ LIMIT number ] [ OFFSET number ];

SELECT retrieves rows from a cube.

The FROM clause specifies one or more source cube tables for the SELECT. Qualification conditions can be added (via WHERE) to restrict the returned rows to a small subset of the original dataset.

Example:

sql
SELECT COUNT(*), orders.status, users.city
FROM orders
  CROSS JOIN users
WHERE city IN ('San Francisco', 'Los Angeles')
GROUP BY orders.status, users.city
HAVING status = 'shipped'
LIMIT 1 OFFSET 1;

EXPLAIN

Synopsis:

sql
EXPLAIN [ ANALYZE ] statement

The EXPLAIN command displays the query execution plan that the Cube planner will generate for the supplied statement.

The ANALYZE will execute statement and display actual runtime statistics, including the total elapsed time expended within each plan node and the total number of rows it actually returned.

Example:

sql
EXPLAIN WITH cte AS (
  SELECT o.count as count, p.name as product_name, p.description as product_description
  FROM orders o
    CROSS JOIN products p
)
SELECT COUNT(*) FROM cte;
   plan_type   |                                plan
---------------+---------------------------------------------------------------------
 logical_plan  | Projection: #COUNT(UInt8(1))                                       +
               |   Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]                +
               |     CubeScan: request={                                            +
               |   "measures": [                                                    +
               |     "orders.count"                                                 +
               |   ],                                                               +
               |   "dimensions": [                                                  +
               |     "products.name",                                               +
               |     "products.description"                                         +
               |   ],                                                               +
               |   "segments": []                                                   +
               | }
 physical_plan | ProjectionExec: expr=[COUNT(UInt8(1))@0 as COUNT(UInt8(1))]        +
               |   HashAggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))]    +
               |     HashAggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))]+
               |       CubeScanExecutionPlan                                        +
               |
(2 rows)

With ANALYZE:

sql
EXPLAIN ANALYZE WITH cte AS (
  SELECT o.count as count, p.name as product_name, p.description as product_description
  FROM orders o
    CROSS JOIN products p
)
SELECT COUNT(*) FROM cte;
     plan_type     |                                                                                plan
-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Plan with Metrics | ProjectionExec: expr=[COUNT(UInt8(1))@0 as COUNT(UInt8(1))], metrics=[output_rows=1, elapsed_compute=541ns, spill_count=0, spilled_bytes=0, mem_used=0]           +
                   |   HashAggregateExec: mode=Final, gby=[], aggr=[COUNT(UInt8(1))], metrics=[output_rows=1, elapsed_compute=6.583µs, spill_count=0, spilled_bytes=0, mem_used=0]     +
                   |     HashAggregateExec: mode=Partial, gby=[], aggr=[COUNT(UInt8(1))], metrics=[output_rows=1, elapsed_compute=13.958µs, spill_count=0, spilled_bytes=0, mem_used=0]+
                   |       CubeScanExecutionPlan, metrics=[]                                                                                                                           +
                   |
(1 row)

SHOW

Synopsis:

sql
SHOW name
SHOW ALL

Returns the value of a runtime parameter using name, or all runtime parameters if ALL is specified.

Example:

sql
SHOW timezone;
 setting
---------
 GMT
(1 row)

SHOW ALL;
            name             |    setting     | description
-----------------------------+----------------+-------------
 max_index_keys              | 32             |
 max_allowed_packet          | 67108864       |
 timezone                    | GMT            |
 client_min_messages         | NOTICE         |
 standard_conforming_strings | on             |
 extra_float_digits          | 1              |
 transaction_isolation       | read committed |
 application_name            | NULL           |
 lc_collate                  | en_US.utf8     |
(9 rows)

SET

Synopsis:

sql
SET name TO value
SET name = value

The SET command changes a session variable to a new value.

Use SET with the cube_cache session variable for cache control.

SQL functions and operators

SQL API currently implements a subset of functions and operators supported by PostgreSQL. Additionally, it supports a few custom functions.

Comparison operators

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
<Returns TRUE if the first value is less than the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
>Returns TRUE if the first value is greater than the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
<=Returns TRUE if the first value is less than or equal to the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
>=Returns TRUE if the first value is greater than or equal to the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
=Returns TRUE if the first value is equal to the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
<> or !=Returns TRUE if the first value is not equal to the second✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Comparison predicates

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
BETWEENReturns TRUE if the first value is between the second and the third❌ No<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
IS NULLTest whether value is NULL✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
IS NOT NULLTest whether value is not NULL✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Mathematical functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
ABSAbsolute value✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
CEILNearest integer greater than or equal to argument✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
DEGREESConverts radians to degrees✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
EXPExponential (e raised to the given power)✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
FLOORNearest integer less than or equal to argument✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LNNatural logarithm✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LOGBase 10 logarithm✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LOG10Base 10 logarithm (same as LOG)✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
PIApproximate value of π✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
POWERa raised to the power of b✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
RADIANSConverts degrees to radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
ROUNDRounds v to s decimal places✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
SIGNSign of the argument (-1, 0, or +1)✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
SQRTSquare root✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
TRUNCTruncates to integer (towards zero)✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>

Trigonometric functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
ACOSInverse cosine, result in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
ASINInverse sine, result in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
ATANInverse tangent, result in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
ATAN2Inverse tangent of y/x, result in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COSCosine, argument in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COTCotangent, argument in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
SINSine, argument in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
TANTangent, argument in radians✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

String functions and operators

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
||Concatenates two strings✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
BTRIMRemoves the longest string containing only characters in characters from the start and end of string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
BIT_LENGTHReturns number of bits in the string (8 times the OCTET_LENGTH)✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
CHAR_LENGTH or CHARACTER_LENGTHReturns number of characters in the string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
LOWERConverts the string to all lower case✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
LTRIMRemoves the longest string containing only characters in characters from the start of string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
OCTET_LENGTHReturns number of bytes in the string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
POSITIONReturns first starting index of the specified substring within string, or zero if it's not present✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
RTRIMRemoves the longest string containing only characters in characters from the end of string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
SUBSTRINGExtracts the substring of string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
TRIMRemoves the longest string containing only characters in characters from the start, end, or both ends of string✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>
UPPERConverts the string to all upper case✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>

Other string functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
ASCIIReturns the numeric code of the first character of the argument✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
CONCATConcatenates the text representations of all the arguments✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LEFTReturns first n characters in the string, or when n is negative, returns all but last ABS(n) characters✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
REPEATRepeats string the specified number of times✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
REPLACEReplaces all occurrences in string of substring from with substring to✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
RIGHTReturns last n characters in the string, or when n is negative, returns all but first ABS(n) characters✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
STARTS_WITHReturns TRUE if string starts with prefix✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>❌ Inner (projections)</nobr>

Pattern matching

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
LIKEReturns TRUE if the string matches the supplied pattern✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
REGEXP_SUBSTRReturns the substring that matches a POSIX regular expression pattern✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Data type formatting functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
TO_CHARConverts a timestamp to string according to the given format✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Date/time functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
DATE_ADDAdd an interval to a timestamp with time zone✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
DATE_TRUNCTruncate a timestamp to specified precision✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
DATEDIFFFrom Redshift. Returns the difference between the date parts of two date or time expressions✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
EXTRACTRetrieves subfields such as year or hour from date/time values✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LOCALTIMESTAMPReturns the current date and time without time zone✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
NOWReturns the current date and time with time zone✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Conditional expressions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
Function, expressionDescriptionPushdown<nobr>Post-processing</nobr>
CASEGeneric conditional expression✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COALESCEReturns the first of its arguments that is not NULL✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
NULLIFReturns NULL if both arguments are equal, otherwise returns the first argument✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
GREATESTSelect the largest value from a list of expressions✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
LEASTSelect the smallest value from a list of expressions✅ Yes<nobr>✅ Outer</nobr>
<nobr>❌ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

General-purpose aggregate functions

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
AVGComputes the average (arithmetic mean) of all the non-NULL input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COUNTComputes the number of input rows in which the input value is not NULL✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COUNT(DISTINCT)Computes the number of input rows containing unique input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
MAXComputes the maximum of the non-NULL input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
MINComputes the minimum of the non-NULL input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
SUMComputes the sum of the non-NULL input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
MEASUREWorks with measures of any type✅ Yes<nobr>❌ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

In projections in inner parts of post-processing queries:

  • AVG, COUNT, MAX, MIN, and SUM can only be used with measures of compatible types.
  • If COUNT(*) is specified, Cube will query the first measure of type count of the relevant cube.

Aggregate functions for statistics

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
COVAR_POPComputes the population covariance✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
COVAR_SAMPComputes the sample covariance✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
STDDEV_POPComputes the population standard deviation of the input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
STDDEV_SAMPComputes the sample standard deviation of the input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
VAR_POPComputes the population variance of the input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
VAR_SAMPComputes the sample variance of the input values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Row and array comparisons

<Info>

Learn more in the relevant section of the PostgreSQL documentation.

</Info>
FunctionDescriptionPushdown<nobr>Post-processing</nobr>
INReturns TRUE if a left-side value matches any of right-side values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>
NOT INReturns TRUE if a left-side value matches none of right-side values✅ Yes<nobr>✅ Outer</nobr>
<nobr>✅ Inner (selections)</nobr>
<nobr>✅ Inner (projections)</nobr>

Custom functions

FunctionDescription
XIRRCalculates the internal rate of return for a series of cash flows
<Note>

See the XIRR recipe for more details.

</Note>