Select
Query Magpie tables. Run a query in a SQL paragraph, or in a Magpie paragraph as sql(SELECT …), where it is evaluated only when something reads its result, such as show. A query is also the argument of Show SQL, Profile SQL, Export Result, Assert SQL Count, and Create Table From SQL, and the body of Create Table As Select and Create View.
Magpie parses each query before Spark runs it, so it accepts the subset of Spark SQL summarized on this page. This page describes the shape of a query, not every expression and function.
Syntax
[WITH <name> AS (<query>) [, ...]]
<query term> [{UNION | EXCEPT | INTERSECT} [ALL | DISTINCT] <query term> ...]
[ORDER BY <expression> [ASC | DESC] [NULLS {FIRST | LAST}] [, ...]]
[LIMIT <n>]
Each query term is a SELECT, a VALUES list, TABLE <table>, or a query in parentheses:
SELECT [ALL | DISTINCT] <column> [, ...]
[FROM <from item> [[<join type>] JOIN <from item> [ON <condition>] ...]]
[PIVOT (...)]
[UNPIVOT [INCLUDE NULLS | EXCLUDE NULLS] (...)]
[WHERE <condition>]
[GROUP BY <grouping> [, ...] [WITH ROLLUP | WITH CUBE]]
[HAVING <condition>]
[WINDOW <name> AS (<window specification>) [, ...]]
Parameters
query term
INTERSECT binds tighter than UNION and EXCEPT, which apply left to right. Use parentheses to group terms differently.
column
Expression — A column, an expression with an optional AS alias, or *. Expressions can use Spark SQL's built-in functions, including aggregate functions with FILTER (WHERE …), window functions with OVER, CASE, CAST, intervals, and lambda functions.
from item
String — One of:
- A table, as
table,schema.table, orrepository.schema.table, with an optional alias. A table in another repository can be queried by naming the repository. result, the last result in the session.- A subquery in parentheses, with an alias.
LATERAL (<query>), with an alias.- A
VALUESlist, with an alias.
join type
INNER (the default), LEFT [OUTER], RIGHT [OUTER], FULL [OUTER], LEFT SEMI, LEFT ANTI, or CROSS. Tables are joined only with JOIN; a comma-separated list of tables after FROM isn't accepted.
grouping
Expression — A column or expression, ROLLUP (…), CUBE (…), or GROUPING SETS ((…), …).
window specification
[PARTITION BY <expression>, ...] [ORDER BY <sort expression>, ... [{ROWS | RANGE} <frame>]]. Use it in a WINDOW clause or after OVER.
n
Integer — The maximum number of rows to return.
Not supported
These Spark SQL clauses fail with a parse error: OFFSET, QUALIFY, TABLESAMPLE, LATERAL VIEW, and CLUSTER BY, DISTRIBUTE BY, and SORT BY as query clauses.
Query hints (/*+ … */) are read as comments and ignored.
Examples
WITH recent AS (
SELECT customer_id, amount
FROM sales.orders
WHERE order_date >= date_sub(current_date(), 30)
)
SELECT c.region, count(*) AS orders, sum(r.amount) AS revenue
FROM recent r
JOIN sales.customers c ON c.customer_id = r.customer_id
GROUP BY c.region
ORDER BY revenue DESC
LIMIT 10
In a Magpie paragraph, wrap it in sql(...) and read the result with show:
sql(select region, count(*) as customers from sales.customers group by region);
show 10 from result;
Security
Requires Read permission on every table the query reads.