bobzhang/sqlglot/executor does not have a README file

    Row

    type Row = Array[Value]

    A row of a table (a Python tuple).

    ExecuteError

    pub(all) suberror ExecuteError {
    ExecuteError(String, cause~ : Error?)
    }

    Raised when the execution of a plan fails (Python sqlglot.errors.ExecuteError); cause is the underlying error (Python __cause__).

    PyException

    pub(all) suberror PyException {
    PyException(String, String)
    }

    A Python exception raised while evaluating generated code, e.g. PyException("ZeroDivisionError", "division by zero").

    PyException::kind

    fn PyException::kind(self : PyException) -> String

    PyException::message

    fn PyException::message(self : PyException) -> String

    Python str(exception).

    Code

    pub enum Code {
    Const(Value)
    Name(String)
    Attr(Code, String)
    Subscript(Code, Code)
    Call(Code, Array[Code])
    Lambda(Array[String], Code)
    IfExp(Code, Code, Code)
    BoolOp(Bool, Array[Code])
    Not(Code)
    Unary(String, Code)
    Binary(String, Code, Code)
    Compare(Code, Array[(String, Code)])
    ListDisplay(Array[Code])
    TupleDisplay(Array[Code])
    DictDisplay(Array[(Code, Code)])
    }

    A compiled Python expression (the result of compile(source, ..., "eval")).

    Context

    pub struct Context {
    tables : Array[(String?, Table)]
    table_ : Table?
    range_readers : Map[String?, Reader]
    row_readers : Map[String?, Reader]
    env : Map[String, Value]
    scope : Value
    }

    Execution context for sql expressions: the tables of the current scope. Column references evaluate to scalars after set_row and to vectors after set_range.

    Context::add_columns

    fn Context::add_columns(self : Context, columns : Array[String]) -> Unit

    Context::columns

    fn Context::columns(self : Context) -> Array[String] raise PyException

    Context::contains

    fn Context::contains(self : Context, name : String?) -> Bool

    Context::each

    fn Context::each(self : Context, f : (RowReader) -> Unit raise) -> Unit raise

    Python iter(context): sets each row in turn and calls f with the reader of the last table.

    Context::eval

    fn Context::eval(self : Context, code : Code) -> Value raise

    Python eval(code, self.env).

    Context::eval_tuple

    fn Context::eval_tuple(self : Context, codes : Array[Code]) -> Array[Value] raise

    Context::get_table

    fn Context::get_table(self : Context, name : String?) -> Table raise PyException

    Context::new

    fn Context::new(tables : Array[(String?, Table)], env? : Map[String, Value], outer? : Map[String?, Reader]) -> Context

    Builds a context. Entries of tables with the same name replace earlier ones (as in a Python dict); outer are the readers of an enclosing query.

    Context::set_index

    fn Context::set_index(self : Context, index : Int) -> Unit raise PyException

    Context::set_range

    fn Context::set_range(self : Context, start : Int, end : Int) -> Unit

    Context::set_row

    fn Context::set_row(self : Context, row : Array[Value]) -> Unit

    Context::sort

    fn Context::sort(self : Context, key : Array[Code]) -> Unit raise

    Context::table

    fn Context::table(self : Context) -> Table raise PyException

    Context::table_iter

    fn Context::table_iter(self : Context, name : String?) -> TableIter raise PyException

    Python context.table_iter(table): an iterator over the rows of a table.

    PyDate

    pub(all) struct PyDate {
    year : Int
    month : Int
    day : Int
    } derive(Eq)

    PyDate::isoformat

    fn PyDate::isoformat(self : PyDate) -> String

    PyDate::repr

    fn PyDate::repr(self : PyDate) -> String

    PyDate::toordinal

    fn PyDate::toordinal(self : PyDate) -> Int64

    PyDate::weekday

    fn PyDate::weekday(self : PyDate) -> Int

    Python date.weekday() (Monday is 0).

    PyDateTime

    pub(all) struct PyDateTime {
    date : PyDate
    time : PyTime
    tz : Int?
    }

    A datetime.datetime; tz is the UTC offset in seconds of an aware datetime (only datetime.timezone.utc, i.e. 0, is produced by the executor).

    PyDateTime::isoformat

    fn PyDateTime::isoformat(self : PyDateTime, sep? : String) -> String

    PyDateTime::repr

    fn PyDateTime::repr(self : PyDateTime) -> String

    PyFunc

    pub(all) struct PyFunc {
    name : String
    call : (Array[Value]) -> Value raise
    }

    A Python callable (an ENV function, a lambda or a builtin).

    PyTime

    pub(all) struct PyTime {
    hour : Int
    minute : Int
    second : Int
    microsecond : Int
    } derive(Eq)

    PyTime::isoformat

    fn PyTime::isoformat(self : PyTime) -> String

    PyTime::repr

    fn PyTime::repr(self : PyTime) -> String

    PyTimeDelta

    pub(all) struct PyTimeDelta {
    days : Int64
    seconds : Int
    microseconds : Int
    } derive(Eq)

    A normalized datetime.timedelta.

    PyTimeDelta::from_us

    fn PyTimeDelta::from_us(us : Int64) -> PyTimeDelta

    Builds a normalized timedelta from a total number of microseconds.

    PyTimeDelta::py_str

    fn PyTimeDelta::py_str(self : PyTimeDelta) -> String

    Python str(timedelta).

    PyTimeDelta::repr

    fn PyTimeDelta::repr(self : PyTimeDelta) -> String

    PyTimeDelta::total_us

    fn PyTimeDelta::total_us(self : PyTimeDelta) -> Int64

    PythonExecutor

    pub struct PythonExecutor {
    env : Map[String, Value]
    tables : Tables
    // private fields
    }

    Executes plans with Python semantics (Python PythonExecutor).

    PythonExecutor::context

    fn PythonExecutor::context(self : PythonExecutor, tables : Array[(String?, Table)]) -> Context

    Python PythonExecutor.context(tables).

    PythonExecutor::execute

    fn PythonExecutor::execute(self : PythonExecutor, plan :
    Plan
    , outer_scope? : Map[String?, Reader]) -> Table raise

    Executes a plan; outer_scope are the readers of an enclosing query.

    PythonExecutor::generate

    fn PythonExecutor::generate(self : PythonExecutor, expression :
    Expr
    ?) -> Code? raise

    Converts a SQL expression into Python code and compiles it.

    PythonExecutor::new

    Creates an executor. env adds or overrides functions available to the generated code.

    PythonExecutor::subquery_cache_sizes

    fn PythonExecutor::subquery_cache_sizes(self : PythonExecutor) -> Array[Int]

    The number of distinct subquery plans and the size of each one's result cache.

    RangeReader

    pub struct RangeReader {
    table : Table?
    start : Int
    stop : Int
    }

    Reads the values of a column over a range of rows.

    RangeReader::get

    fn RangeReader::get(self : RangeReader, column : String) -> Value raise PyException

    RangeReader::length

    fn RangeReader::length(self : RangeReader) -> Int

    Reader

    pub enum Reader {
    Row(RowReader)
    Range(RangeReader)
    }

    A reader of the current row(s) of a table.

    Reader::get

    fn Reader::get(self : Reader, column : String) -> Value raise PyException

    RowReader

    pub struct RowReader {
    columns : Map[String, Int]
    row : Array[Value]
    }

    Reads the columns of the current row.

    RowReader::get

    fn RowReader::get(self : RowReader, column : String) -> Value raise PyException

    RowReader::new

    fn RowReader::new(columns? : Array[String], column_range? : (Int, Int)) -> RowReader

    Table

    pub struct Table {
    columns : Array[String]
    column_range : (Int, Int)?
    reader : RowReader
    rows : Array[Array[Value]]
    range_reader : RangeReader
    }

    A simple columnar data structure: column names and rows of values.
    impl Show for Table

    Table::add_columns

    fn Table::add_columns(self : Table, columns : Array[String]) -> Unit

    Table::append

    fn Table::append(self : Table, row : Array[Value]) -> Unit raise PyException

    Table::get_row

    fn Table::get_row(self : Table, index : Int) -> RowReader raise PyException

    Python table[index]: points the table's reader at a row and returns it.

    Table::length

    fn Table::length(self : Table) -> Int

    Table::new

    fn Table::new(columns? : Array[String], rows? : Array[Array[Value]], column_range? : (Int, Int)) -> Table

    Creates a table. rows is shared (not copied), as in Python, unless empty.

    Table::pop

    fn Table::pop(self : Table) -> Unit

    Table::to_pylist

    fn Table::to_pylist(self : Table) -> Array[Array[(String, Value)]]

    Python Table.to_pylist(): the rows as (column, value) records.

    Table::to_string

    fn Table::to_string(self : Table) -> String

    Python repr(table).

    Table::width

    fn Table::width(self : Table) -> Int

    TableData

    pub(all) enum TableData {
    Records(Array[Array[(String, Value)]])
    Tbl(Table)
    Nested(Map[String, TableData])
    }

    The input data of execute: rows given as records, a Table, or a nested mapping of {db: {table: ...}}.

    TableIter

    pub struct TableIter {
    table : Table
    index : Int
    }

    Python TableIter.

    TableIter::next

    fn TableIter::next(self : TableIter) -> RowReader? raise PyException

    Tables

    pub struct Tables {
    mapping : Map[String, TableData]
    supported_table_args : Array[String]
    }

    A nested mapping of table names to tables (Python Tables(AbstractMappingSchema)).

    Tables::find

    fn Tables::find(self : Tables, table :
    Expr
    , raise_on_missing? : Bool) -> Table? raise
    SqlglotError

    Python AbstractMappingSchema.find(table): the table matching an exp.Table.

    Tables::flatten

    fn Tables::flatten(self : Tables) -> Array[Array[String]]

    The table paths of the mapping (Python flatten_schema(mapping, depth)).

    Tables::get

    fn Tables::get(self : Tables, keys : Array[String]) -> Table?

    Looks up a table by its path (outermost first).

    Tables::new

    Value

    pub(all) enum Value {
    Null
    Bool(Bool)
    Int(Int64)
    Float(Double)
    Str(String)
    List(Array[Value])
    Tuple(Array[Value])
    Dict(Array[(Value, Value)])
    Set(Array[Value])
    Date(PyDate)
    Time(PyTime)
    DateTime(PyDateTime)
    TimeDelta(PyTimeDelta)
    Iter(Array[Value])
    Func(PyFunc)
    Module(String)
    DTypeV(
    DType
    )
    Readers(Map[String?, Reader])
    ReaderV(Reader)
    RevKey(Value)
    }

    A runtime value (a Python object).
    impl Eq for Value
    impl Show for Value

    Value::is_null

    fn Value::is_null(self : Value) -> Bool

    Value::py_str

    fn Value::py_str(self : Value) -> String

    Python str(value).

    Value::repr

    fn Value::repr(self : Value) -> String

    Python repr(value).

    Value::truthy

    fn Value::truthy(self : Value) -> Bool

    Python truthiness (bool(value)).

    Value::type_name

    fn Value::type_name(self : Value) -> String

    Python type(value).__name__ (as used in error messages).

    compile_python

    fn compile_python(source : String) -> Code raise PyException

    Python compile(source, source, "eval"): parses a Python expression.

    date_fromisoformat

    fn date_fromisoformat(v : Value) -> PyDate raise PyException

    Python datetime.date.fromisoformat(s).

    datetime_fromisoformat

    fn datetime_fromisoformat(v : Value) -> PyDateTime raise PyException

    Python datetime.datetime.fromisoformat(s).

    default_env

    let default_env : Map[String, Value]

    The functions available to the generated code (Python env.ENV). Entries can be replaced (e.g. to pin CURRENT_TIMESTAMP in tests), like patching the Python dict.

    ensure_tables

    Python ensure_tables(d, dialect): normalizes table and column names and converts records to Tables.

    error_message

    fn error_message(e : Error) -> String

    Python str(error).

    eval_code

    fn eval_code(code : Code, globals : Map[String, Value], scope : Value) -> Value raise

    Python eval(code, globals); scope is the value of the scope global and globals the other globals (falling back to the builtins).

    execute

    fn[T :
    IntoPy
    ] execute(sql : T, schema? : Map[String,
    SchemaNode
    ], mapping_schema? :
    MappingSchema
    , read? : String, dialect? : String, tables? : Map[String, TableData]) -> Table raise

    Run a sql query against data.

    • sql: a SQL statement (a string or an expression).
    • schema: the database schema, in one of the forms {table: {col: type}}, {db: {table: {col: type}}} or {catalog: {db: {table: {col: type}}}}. When it is omitted or empty, it is inferred from the first row of each table.
    • mapping_schema: the schema as a MappingSchema (takes precedence over schema).
    • dialect: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto"); read is an alias of it.
    • tables: the tables to register.

    Returns a simple columnar data structure.

    float_repr

    fn float_repr(d : Double) -> String

    Python repr(float): the shortest round-tripping digits, in scientific notation when the decimal exponent is = 16.

    hash_key

    fn hash_key(v : Value) -> String raise PyException

    A key that identifies a hashable value up to Python equality (e.g. 1, 1.0 and True share a key). Raises TypeError for unhashable values.

    py_abs

    fn py_abs(a : Value) -> Value raise PyException

    Python abs(a).

    py_add

    fn py_add(a : Value, b : Value) -> Value raise PyException

    Python a + b.

    py_bitop

    fn py_bitop(op : String, a : Value, b : Value) -> Value raise PyException

    Python bitwise operators &, |, ^, <<, >>.

    py_eq

    fn py_eq(a : Value, b : Value) -> Bool

    Python == (never raises).

    py_float

    fn py_float(v : Value) -> Value raise PyException

    Python float(x).

    py_floordiv

    fn py_floordiv(a : Value, b : Value) -> Value raise PyException

    Python a // b.

    py_fsum

    fn py_fsum(values : Array[Value]) -> Double raise PyException

    Python math.fsum(values) (Shewchuk's exactly rounded summation, as in CPython).

    py_ge

    fn py_ge(a : Value, b : Value) -> Bool raise PyException

    Python a >= b.

    py_getitem

    fn py_getitem(container : Value, key : Value) -> Value raise PyException

    Python x[key] for dicts, lists, tuples and strings.

    py_gt

    fn py_gt(a : Value, b : Value) -> Bool raise PyException

    Python a > b.

    py_int

    fn py_int(v : Value) -> Value raise PyException

    Python int(x).

    py_invert

    fn py_invert(a : Value) -> Value raise PyException

    Python ~a.

    py_iter

    fn py_iter(v : Value) -> Array[Value] raise PyException

    Python iter(value), materialized.

    py_le

    fn py_le(a : Value, b : Value) -> Bool raise PyException

    Python a <= b.

    py_len

    fn py_len(v : Value) -> Int raise PyException

    Python len(value).

    py_lt

    fn py_lt(a : Value, b : Value) -> Bool raise PyException

    Python a < b.

    py_max

    fn py_max(values : Array[Value]) -> Value raise PyException

    Python max(values) (the first maximal item wins).

    py_min

    fn py_min(values : Array[Value]) -> Value raise PyException

    Python min(values) (the first minimal item wins).

    py_mod

    fn py_mod(a : Value, b : Value) -> Value raise PyException

    Python a % b.

    py_mul

    fn py_mul(a : Value, b : Value) -> Value raise PyException

    Python a * b.

    py_neg

    fn py_neg(a : Value) -> Value raise PyException

    Python -a.

    py_pos

    fn py_pos(a : Value) -> Value raise PyException

    Python +a.

    py_pow

    fn py_pow(a : Value, b : Value) -> Value raise PyException

    Python pow(a, b) / a ** b.

    py_round

    fn py_round(x : Value, ndigits : Value) -> Value raise PyException

    Python round(x, ndigits); ndigits=None rounds to an integer.

    py_set

    fn py_set(values : Array[Value]) -> Array[Value] raise PyException

    Python set(values) in CPython's iteration order (insertion order when the hashes are not deterministic).

    py_set_union

    fn py_set_union(a : Array[Value], b : Array[Value]) -> Array[Value] raise PyException

    Python list(set(a).union(set(b))).

    py_slice

    fn py_slice(v : Value, start : Value, stop : Value, step? : Int) -> Value raise PyException

    Python slicing x[start:stop:step] of a string, list or tuple.

    py_sub

    fn py_sub(a : Value, b : Value) -> Value raise PyException

    Python a - b.

    py_sum

    fn py_sum(values : Array[Value]) -> Value raise PyException

    Python 3.12+ sum(values) (start 0): exact integer accumulation, then Neumaier compensated summation once a float is seen.

    py_truediv

    fn py_truediv(a : Value, b : Value) -> Value raise PyException

    Python a / b.

    python_dialect

    The Python dialect used by the executor to generate Python code.

    python_generate

    fn python_generate(expression :
    Expr
    , identify? : Bool) -> String raise
    SqlglotError

    Python Python().generate(expression): renders an expression as Python code.

    strftime

    fn strftime(dt : PyDateTime, fmt : String) -> String

    Python datetime.strftime(fmt) (C locale).

    strptime

    fn strptime(s : String, fmt : String) -> PyDateTime raise PyException

    Python datetime.datetime.strptime(s, fmt) for the common directives.

    time_fromisoformat

    fn time_fromisoformat(v : Value) -> PyTime raise PyException

    Python datetime.time.fromisoformat(s).