chainalysis.orm package

Module contents

class chainalysis.orm.Orm(api_key: str)[source]

Bases: object

The Orm class is used to query Database tables using Object Relational Mapping (ORM), which allows querying tables using Python methods rather than raw SQL.

TransactionalSelect(chain_table_name: str) TransactionalSelect[source]

Method to create a TransactionalSelect object.

Parameters:

chain_table_name (str) – The name of the chain table.

Returns:

TransactionalSelect object.

Return type:

TransactionalSelect

AnalyticalSelect(chain_table_name: str) AnalyticalSelect[source]

Method to create an AnalyticalSelect object.

Parameters:

chain_table_name (str) – The name of the chain table.

Returns:

AnalyticalSelect object.

Return type:

AnalyticalSelect

class chainalysis.orm.Select(api_key: str, chain_table_name: str)[source]

Bases: Select, ABC

Select is an abstract base class that represents an interface for an Object Relational Mapper (ORM) which queries a chain-specific table within Data Solutions.

Select provides the structure for constructing and executing SQL queries using functions. Child classes must implement methods to retrieve tables and execute queries, which will return the appropriate result type.

property c: ColumnCollection

Get the columns of the table.

Returns:

A collection of column objects associated with the table.

Return type:

ColumnCollection

property table: Table

The chosen table to query.

Returns:

A Table object.

Return type:

Table

sql() str[source]

Compile the query into a raw SQL string.

Returns:

The compiled SQL query as a string.

Return type:

str

abstract execute() TransactionalQuery | AnalyticalQuery[source]

Execute the query and create a Transactional or Analytical object.

Returns:

Transactional or Analytical object.

Return type:

Union[Transactional, Analytical]

with_columns(*columns: Column) Self[source]

Select specific columns from the table for querying.

Note: All columns are selected by default. Use this method to select specific columns.

Parameters:

columns (Column) – The columns to be selected.

Returns:

A Select instance with the selected columns.

Return type:

Select

E.g.:

select_query = query_1.with_columns(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2
FROM "chain.table1"
where(*whereclauses: ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement) Self[source]

Create a new Select with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.

Parameters:

whereclauses (ColumnExpressionArgument[bool]) – The WHERE clauses to apply.

Returns:

A new Select with the WHERE clause applied.

Return type:

Select

E.g.:

where_query = query_1.where(query_1.c.column1 == 1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
WHERE "chain.table1".column1 = 1
select_from(table: Table) Self[source]

Return a new Select with the given FROM expression(s) merged into its list of FROM objects.

Parameters:

table (Table) – The table to select from.

Returns:

A new Select with the FROM clause applied.

Return type:

Select

E.g.:

from chainalysis import func

select_from_query = query_1.with_columns((func.count('*')).select_from(query_1.table))

The resulting SQL query will be:

SELECT COUNT(*) FROM "chain.table1"
limit(limit: int) Self[source]

Create a new Select with the given LIMIT clause applied.

Parameters:

limit (int) – The number of rows to limit the result set to.

Returns:

A new Select with the LIMIT clause applied.

Return type:

Select

E.g.:

limit_query = query_1.limit(10)

The resulting SQL query will be:

SELECT * FROM "chain.table1"
LIMIT 10
group_by(*columns: Column) Self[source]

Create a new Select with the given expression(s) added to its GROUP BY clause.

Parameters:

columns (Column) – The columns to group by.

Returns:

A new Select with the GROUP BY clause applied.

Return type:

Select

E.g.:

group_by_query = query_1.group_by(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
GROUP BY "chain.table1".column1, "chain.table1".column2
having(*having: ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement) Self[source]

Create a new Select with the given expression added to its HAVING clause joined to the existing clause via AND, if any.

Parameters:

having (ColumnExpressionArgument[bool]) – The HAVING clauses to apply.

Returns:

A new Select with the HAVING clause applied.

Return type:

Select

E.g.:

having_query = query_1.group_by(query_1.c.column1).having(query_1.c.column1 == 1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
GROUP BY "chain.table1".column1
HAVING "chain.table1".column1 = 1
order_by(*clauses: Column) Self[source]

Create a new Select with the given list of ORDER BY criteria applied.

Parameters:

clauses (Column) – The ORDER BY criteria to apply.

Returns:

A new Select with the ORDER BY criteria applied.

Return type:

Select

E.g.:

order_by_query = query_1.order_by(query_1.c.column1, query_1.c.column2)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
ORDER BY "chain.table1".column1, "chain.table1".column2
join(target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None, *, isouter: bool = False, full: bool = False) Self[source]

Create a SQL JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument, optional) – The ON clause to apply. If omitted, an ON clause is generated automatically based on the ForeignKey linkages between the two tables, if one can be unambiguously determined, otherwise an error is raised.

  • isouter (bool, optional, default False) – Whether to apply an outer join.

  • full (bool, optional, default False) – Whether to apply a full join.

Returns:

A new Select with the join operation applied.

Return type:

Select

Raises:

ValueError – If the ON clause is not provided and an unambiguous foreign key linkage cannot be determined.

E.g.:

join_query = query_1.join(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
leftjoin(target, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None) Self[source]

Create a LEFT JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

Returns:

A new Select with the left join operation applied.

Return type:

Select

E.g.:

leftjoin_query = query_1.leftjoin(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" LEFT OUTER JOIN "chain.table2"

Note: This method is an alias for .outerjoin() with isouter=False. LEFT OUTER JOIN and LEFT JOIN are equivalent.

outerjoin(target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement, *, full: bool = False) Self[source]

Create a SQL LEFT OUTER JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

  • full (bool, optional, default False) – Whether to apply a full join.

Returns:

A new Select with the outer join operation applied.

Return type:

Select

E.g.:

outerjoin_query = query_1.outerjoin(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" LEFT OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
full_outerjoin(target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement) Self[source]

Create a SQL FULL OUTER JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

Returns:

A new Select with the full outer join operation applied.

Return type:

Select

E.g.:

full_outerjoin_query = query_1.full_outerjoin(query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT "chain.table1".column1, "chain.table1".column2, "chain.table1".column3
FROM "chain.table1" FULL OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
outerjoin_from(from_: Column, target: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement, *, full: bool = False) Self[source]

Create a SQL LEFT OUTER JOIN against this Select object’s criterion and apply generatively, returning the newly resulting Select.

Parameters:
  • from (Table) – The table to join from.

  • target (Table) – The target table to join.

  • onclause (ColumnExpressionArgument) – The ON clause to apply.

  • full (bool, optional, default False) – Whether to apply a full join.

Returns:

A new Select with the outer join operation applied.

Return type:

Select

E.g.:

outerjoin_from_query = query_1.outerjoin_from(query_1.table, query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1" LEFT OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
distinct() Self[source]

Create a new Select which will apply DISTINCT operation applied.

Returns:

A new Select with the DISTINCT operation applied.

Return type:

Select

E.g.:

distinct_query = query_1.distinct()

The resulting SQL query will be:

SELECT DISTINCT *
FROM "chain.table1"
exists() Self[source]

Create a new subquery which will apply an EXISTS clause to the SELECT statement overall.

Returns:

A new subquery with the EXISTS operation applied.

Return type:

Select

E.g.:

query_1 = query_1.where(query_1.c.column1 == 1)
exists_query = query_1.exists()

The resulting SQL query will be:

EXISTS (SELECT * FROM "chain.table1" WHERE "chain.table1".column1 = 1)
alias(name: str | None = None) Self[source]

Create a new Select object with the given name as an alias.

Parameters:

name (str, optional, default None) – The name of the alias.

Returns:

A new Select with the alias applied.

Return type:

Select

E.g.:

alias_query = query_1.alias(name="alias")

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" AS alias
union(*other: Select) Self[source]

Create an SQL UNION alias of this Select against the given selectables provided as positional arguments.

A union is the set of all rows returned by either the left or right selectables. If a row is present in both selectables, it will appear in the result set only once.

Parameters:

other (Select) – One or more Selects with which to union.

Returns:

A new Select with the union operation applied.

Return type:

Select

E.g.:

union_query = query_1.union(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
union_all(*other: Select) Self[source]

Create an SQL UNION ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to union all.

Returns:

A new Select with the union all operation applied.

Return type:

Select

E.g.:

union_all_query = query_1.union_all(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table2".column1 AS column1
FROM "chain.table2" UNION ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2" UNION ALL SELECT "chain.table1".column1 AS column1
FROM "chain.table1") AS anon_1
except_(*other: Select) Self[source]

Create an SQL EXCEPT alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Select with which to except.

Returns:

A new Select with the except operation applied.

Return type:

Select

E.g.:

except_query = query_1.except_(query_2)

The resulting SQL query will be:

SELECT *
FROM (SELECT anon_1.column1 AS column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" EXCEPT SELECT "chain.table".column1 AS column1
FROM "chain.table" EXCEPT SELECT "chain.table".column2 AS column2
FROM "chain.table") AS anon_1) AS anon_1
except_all(*other: Select) Self[source]

Create an SQL EXCEPT ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to except all.

Returns:

A new Select with the except all operation applied.

Return type:

Select

E.g.:

except_all_query = query_1.except_all(query_2)

The resulting SQL query will be:

SELECT *
FROM (SELECT "chain.name".column1 AS column1
FROM "chain.name" EXCEPT ALL SELECT "chain.name".column2 AS column2
FROM "chain.name") AS anon_1
intersect(*other: Select) Self[source]

Create an SQL INTERSECT alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to intersect.

Returns:

A new Select with the intersect operation applied.

Return type:

Select

E.g.:

intersect_query = query_1.intersect(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
intersect_all(*other: Select) Self[source]

Create an SQL INTERSECT ALL alias of this Select against the given selectables provided as positional arguments.

Parameters:

other (Select) – One or more Selects with which to intersect all.

Returns:

A new Select with the intersect all operation applied.

Return type:

Select

E.g.:

intersect_all_query = query_1.intersect_all(query_2)

The resulting SQL query will be:

SELECT anon_1.column1
FROM (SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT ALL SELECT "chain.table".column1 AS column1
FROM "chain.table" INTERSECT ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2") AS anon_1
lateral(name: str | None = None) Self[source]

Create an SQL LATERAL alias of this Select against the given selectables provided as positional arguments.

Parameters:

name (str, optional, default None) – The name of the lateral selection.

Returns:

A new Select with the lateral operation applied.

Return type:

Select

E.g.:

lateral_query = query_1.where(query_1.c.column1 > 10).lateral(name="alias")

The resulting SQL query will be:

SELECT *
FROM LATERAL (
    SELECT *
    FROM "chain.table1"
    WHERE "chain.table1".column1 > 10
) AS alias
class chainalysis.orm.AnalyticalSelect(api_key: str, chain_table_name: str)[source]

Bases: Select

The AnalyticalSelect class implements the Select base class for querying a chain-specific table within the Data Solutions system.

It provides the structure for constructing and executing SQL queries using ORM methods.

execute(polling_interval_sec: int = 5, autopaginate: bool = True) AnalyticalQuery[source]

Execute the query and return an Analytical object.

Parameters:
  • polling_interval_sec (int, optional) – The interval in seconds between status checks. The minimum value is 5 seconds.

  • autopaginate (bool, optional) – Whether to automatically retrieve full results instead of individual pages.

Returns:

Analytical object.

Return type:

Analytical

class chainalysis.orm.TransactionalSelect(api_key: str, chain_table_name: str)[source]

Bases: Select

The TransactionalSelect class implements the Select base class for querying a chain-specific table within the Data Solutions system.

It provides the structure for constructing and executing SQL queries using ORM methods.

execute(options: dict = {}) TransactionalQuery[source]

Execute the query and return a TransactionalQuery object.

Parameters:

options (dict) – The options for the query.

Returns:

TransactionalQuery object.

Return type:

TransactionalQuery

chainalysis.orm.and_(*clauses: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a conjunction of expressions joined by an AND.

Parameters:

clauses (ColumnExpressionArgument) – The expressions to join.

Returns:

A new conjunction of expressions.

Return type:

ColumnExpressionArgument

E.g.:

from chainalysis.orm import and_

and_query = query_1.where(
    and_(
        query_1.c.column1 == 1,
        query_1.c.column2 == 2
    )
)

The resulting SQL query will be:

SELECT * FROM "chain.table1"
WHERE "chain.table1".column1 = 1 AND "chain.table1".column2 = 2

Python’s & operator can also be used to create an AND clause:

and_query = (query_1.c.column1 == 1) & (query_1.c.column2 == 2)
chainalysis.orm.between(column: Column, lower_bound: Any, upper_bound: Any, symmetric: bool = False) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a BETWEEN predicate clause.

Parameters:
  • column (Column) – The column to apply the BETWEEN clause to.

  • lower_bound (Any) – The lower bound of the BETWEEN clause.

  • upper_bound (Any) – The upper bound of the BETWEEN clause.

  • symmetric (bool, optional, default False) – Whether to apply a symmetric BETWEEN clause.

Returns:

A new BETWEEN clause.

Return type:

ColumnExpressionArgument

E.g.:

from chainalysis.orm import between

between_query = query_1.where(between(query_1.c.column1, 1, 10))

The resulting SQL query will be:

SELECT * FROM "chain.table1"
WHERE "chain.table1".column1 BETWEEN 1 AND 10
chainalysis.orm.case(*whens: Any, value: Any = None, else_: Any = None) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a CASE expression.

Parameters:
  • whens (Any) – The WHEN clauses to apply.

  • value (Any, optional, default None) – The value to compare.

  • else (Any, optional, default None) – The ELSE clause to apply.

Returns:

A new CASE expression.

Return type:

ColumnExpressionArgument

E.g. using whens:

from chainalysis.orm import case

case_query = query_1.with_columns(
    case(
        (query_1.c.column1 == 1, "one"),
        (query_1.c.column2 == 2, "two"),
        else_="three"
    )
)

The resulting SQL query will be:

SELECT
    CASE
        WHEN "chain.table1".column1 = 1 THEN 'one'
        WHEN "chain.table1".column2 = 2 THEN 'two'
        ELSE 'three'
    END

E.g. using value:

from chainalysis.orm import case

case_query = query_1.with_columns(
    case(
        {
            query_1.c.column1: "one",
            query_1.c.column2: "two"
        },
        value=query_1.c.column1,
        else_="three"
    )
)

The resulting SQL query will be:

SELECT
    CASE "chain.table1".column1
        WHEN "chain.table1".column1 THEN 'one'
        WHEN "chain.table1".column2 THEN 'two'
        ELSE 'three'
    END
chainalysis.orm.distinct(column: Column) Subquery[source]

Create an column-expression-level DISTINCT clause.

Parameters:

column (Column) – The column to apply DISTINCT to.

Returns:

A new Select with the DISTINCT operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import distinct

distinct_query = distinct(query_1)

The resulting SQL query will be:

SELECT DISTINCT *
FROM "chain.table1"
chainalysis.orm.except_(*selects: Select) Subquery[source]

Create an SQL EXCEPT of all the given Selects provided as positional arguments.

Parameters:

selects (Select) – A list of Selects to except.

Returns:

A new Select with the except operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import except_

except_query = except_(query_1, query_2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" EXCEPT SELECT "chain.table2".column1 AS column1
FROM "chain.table2",
chainalysis.orm.except_all(*selects: Select) Subquery[source]

Create an SQL EXCEPT ALL of all the given Selects provided as positional arguments.

Parameters:

selects (Select) – A list of Selects to except all.

Returns:

A new Select with the except all operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import except_all

except_all_query = except_all(query_1, query_2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" EXCEPT ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2",
chainalysis.orm.exists(select: Select) Subquery[source]

Create an EXISTS clause.

Returns:

A new Select with the EXISTS operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import exists

stmt = query.with_columns().where(query.c.column1 == 1)
exists_query = exists(stmt)

The resulting SQL query will be:

EXISTS (SELECT * FROM "chain.table1" WHERE "chain.table1".column1 = 1)
chainalysis.orm.intersect(*selects: Select) Subquery[source]

Create an SQL INTERSECT of all the given Selects provided as positional arguments.

Parameters:

selects (Select) – A list of Selects to intersect.

Returns:

A new Select with the intersect operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import intersect

intersect_query = intersect(table1, table2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" INTERSECT SELECT "chain.table2".column1 AS column1
FROM "chain.table2",
chainalysis.orm.intersect_all(*selects: Select) Subquery[source]

Create an SQL INTERSECT ALL of all the given Selects provided as positional arguments.

Parameters:

selects (Select) – A list of Selects to intersect all.

Returns:

A new Selects with the intersect all operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import intersect_all

intersect_all_query = intersect_all(table1, table2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" INTERSECT ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2",
chainalysis.orm.join(left: Table, right: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None, isouter=False, full=False) Subquery[source]

Create a new Select object, joining two tables.

Parameters:
  • left (Table) – The left table to join.

  • right (Table) – The right table to join.

  • onclause (ColumnExpressionArgument, optional) – The ON clause to apply.

  • isouter (bool, optional, default False) – Whether to apply an outer join.

  • full (bool, optional, default False) – Whether to apply a full join.

Returns:

A new Select with the join operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import join

join_query = join(query_1.table, query_2.table_2, table1.c.column1 == table2.c.column1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
JOIN "chain.table2" ON "chain.table1".column1 = "chain.table2".column
chainalysis.orm.leftjoin(left: Table, right: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None) Subquery[source]

Create a new Select object, joining two tables with a LEFT JOIN.

Parameters:
  • left (Table) – The left table to join.

  • right (Table) – The right table to join.

  • onclause (ColumnExpressionArgument, optional) – The ON clause to apply.

Returns:

A new Select with the left join operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import leftjoin

leftjoin_query = leftjoin(query_1.table, query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
LEFT OUTER JOIN "chain.table2" ON "chain.table1".column1 = "chain.table2".column1

Note: This method is an alias for .outerjoin() with isouter=False. LEFT OUTER JOIN and LEFT JOIN are equivalent.

chainalysis.orm.outerjoin(left: Table, right: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None, full: bool = False) Subquery[source]

Create a new Select object, joining two tables with a LEFT OUTER JOIN.

Parameters:
  • left (Table) – The left table to join.

  • right (Table) – The right table to join.

  • onclause (ColumnExpressionArgument, optional, default None) – The ON clause to apply.

  • full (bool, optional, default False) – Whether to apply a full join.

Returns:

A new Select with the outer join operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import outerjoin

outerjoin_query = outerjoin(
    left=query_1.table,
    right=query_2.table,
    onclause=query_1.c.column1 == query_2.c.column1
)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
LEFT OUTER JOIN "chain.table2"
ON "chain.table1".column1 = "chain.table2".column1
chainalysis.orm.full_outerjoin(left: Table, right: Table, onclause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement = None) Subquery[source]

Creae a new Select object, joining two tables with a FULL OUTER JOIN.

Parameters:
  • left (Table) – The left table to join.

  • right (Table) – The right table to join.

  • onclause (ColumnExpressionArgument, optional) – The ON clause to apply.

Returns:

A new Select with the full outer join operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import full_outerjoin

full_outerjoin_query = full_outerjoin(query_1.table, query_2.table, query_1.c.column1 == query_2.c.column1)

The resulting SQL query will be:

SELECT *
FROM "chain.table1"
FULL OUTER JOIN "chain.table2" ON "chain.table1".column1 = "chain.table2".column1
chainalysis.orm.lateral(select: Select, name: str | None = None) Subquery[source]

Create an SQL LATERAL of this Select

Parameters:
  • select (Select) – A Select to lateral.

  • name (str, optional, default None) – The name of the lateral Select.

Returns:

A new Select with the lateral operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import lateral

lateral_query = lateral(query_1.where(query_1.c.column1 > 10))

The resulting SQL query will be:

SELECT *
FROM LATERAL (
    SELECT *
    FROM "chain.table1"
    WHERE "chain.table1".column1 > 10
) AS alias
chainalysis.orm.literal(value: Any) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a literal clause, bound to a bind parameter.

Parameters:

value (Any) – The value to bind.

Returns:

A new literal clause.

Return type:

ColumnExpressionArgument

E.g.:

from chainalysis.orm import literal

literal_query = query_1.with_columns(
    query_1.c.column1,
    literal(1).label("one")
)

The resulting SQL query will be:

SELECT "chain.table".column1, 1 AS one
FROM "chain.table"
chainalysis.orm.not_(clause: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a negation of a clause.

Parameters:

clause (ColumnExpressionArgument) – The clause to negate.

Returns:

A new negated clause.

Return type:

ColumnExpressionArgument

E.g.:

from chainalysis.orm import not_

not_query = query_1.where(not_(query_1.c.column1 == 1))

The resulting SQL query will be:

SELECT * FROM "chain.table1"
WHERE NOT "chain.table1".column1 = 1

Python’s ~ operator can also be used to negate a clause:

not_query = ~(query_1.c.column1 == 1)
chainalysis.orm.or_(*clauses: ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement) ColumnElement[_T] | _HasClauseElement[_T] | SQLCoreOperations[_T] | ExpressionElementRole[_T] | TypedColumnsClauseRole[_T] | Callable[[], ColumnElement[_T]] | LambdaElement[source]

Create a conjunction of expressions joined by an OR.

Parameters:

clauses (ColumnExpressionArgument) – The expressions to join.

Returns:

A new conjunction of expressions.

Return type:

ColumnExpressionArgument

E.g.:

from chainalysis.orm import or_

or_query = query_1.where(
    or_(
        query_1.c.column1 == 1,
        query_1.c.column2 == 2
    )
)

The resulting SQL query will be:

SELECT * FROM "chain.table1"
WHERE "chain.table1".column1 = 1 OR "chain.table1".column2 = 2

Python’s | operator can also be used to create an OR clause:

or_query = (query_1.c.column1 == 1) | (query_1.c.column2 == 2)
chainalysis.orm.union(*selects: Select) Subquery[source]

Create an SQL UNION of all the given Selects provided as positional arguments.

Parameters:

selects (Select) – A list of Selects to union.

Returns:

A new Select with the union operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import union

union_query = union(table1, table2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION SELECT "chain.table2".column1 AS column1
FROM "chain.table2",
chainalysis.orm.union_all(*selects: Select) Subquery[source]

Create an UNION ALL of multiple Selects.

Parameters:

selects (Select) – A list of Selects to union all.

Returns:

A new Select with the union all operation applied.

Return type:

Subquery

E.g.:

from chainalysis.orm import union_all

union_all_query = union_all(query_1, query_2)

The resulting SQL query will be:

SELECT "chain.table1".column1 AS column1
FROM "chain.table1" UNION ALL SELECT "chain.table2".column1 AS column1
FROM "chain.table2",