Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

SQL Modules and Variable Expansion

Graham Wheeler edited this page Jun 3, 2016 · 6 revisions

The %sql cell magic serves two purposes:

  • executing a SQL query statement immediately
  • defining a SQL module for later execution

Variable substitution is done immediately in the first case using the IPython notebook execution environment.

In the second case what happens is a Python module is created and immediately imported, using the name supplied in the -m/--module argument. This module will have several variables defined in its namespace:

  • _sql_module_arg_parser is a Python argparse argument parser that is created from any Python variable definitions at the start of the cell;
  • _sql_module_main is the main query statement (datalab.data.SqlStatement object) to be executed for the cell;
  • _sql_module_last is the last query statement defined in the cell; this is frequently the same as _sql_module_main
  • for each SQL query that starts with a DEFINE QUERY \<name>\ prefix, `<name>' will be bound to that query statement