pytrilogy-0.3.179
0.3.179
0.3.178
0.3.177
0.3.176
0.3.175
0.3.174
0.3.173
0.3.172
0.3.171
0.3.170
0.3.169
0.3.168
0.3.167
0.3.166
0.3.165
0.3.164
0.3.163
0.3.162
0.3.161
0.3.160
0.3.159
0.3.158
0.3.157
0.3.156
0.3.155
0.3.154
0.3.153
0.3.152
0.3.151
0.3.150
0.3.149
0.3.148
0.3.147
0.3.145
0.3.144
0.3.143
0.3.142
0.3.141
0.3.140
0.3.139
0.3.138
0.3.137
0.3.136
0.0.3.99
0.0.3.98
0.0.3.97
0.0.3.96
0.0.3.95
0.0.3.94
0.0.3.93
0.0.3.92
0.0.3.91
0.0.3.90
0.0.3.9
0.0.3.89
0.0.3.88
0.0.3.86
0.0.3.85
0.0.3.84
0.0.3.83
0.0.3.82
0.0.3.81
0.0.3.80
0.0.3.8
0.0.3.79
0.0.3.78
0.0.3.77
0.0.3.76
0.0.3.75
0.0.3.74
0.0.3.73
0.0.3.72
0.0.3.71
0.0.3.70
0.0.3.7
0.0.3.69
0.0.3.68
0.0.3.67
0.0.3.66
0.0.3.65
0.0.3.64
0.0.3.63
0.0.3.61
0.0.3.60
0.0.3.6
0.0.3.57
0.0.3.56
0.0.3.55
0.0.3.54
0.0.3.53
0.0.3.52
0.0.3.51
0.0.3.5
0.0.3.48
0.0.3.47
0.0.3.46
0.0.3.45
0.0.3.44
0.0.3.43
0.0.3.42
0.0.3.41
0.0.3.40
0.0.3.4
0.0.3.39
0.0.3.38
0.0.3.37
0.0.3.36
0.0.3.35
0.0.3.34
0.0.3.33
0.0.3.32
0.0.3.31
0.0.3.30
0.0.3.3
0.0.3.29
0.0.3.28
0.0.3.27
0.0.3.26
0.0.3.25
0.0.3.24
0.0.3.23
0.0.3.22
0.0.3.21
0.0.3.20
0.0.3.2
0.0.3.19
0.0.3.18
0.0.3.17
0.0.3.16
0.0.3.15
0.0.3.14
0.0.3.136
0.0.3.135
0.0.3.134
0.0.3.133
0.0.3.132
0.0.3.131
0.0.3.130
0.0.3.13
0.0.3.129
0.0.3.128
0.0.3.127
0.0.3.126
0.0.3.125
0.0.3.124
0.0.3.123
0.0.3.122
0.0.3.121
0.0.3.120
0.0.3.12
0.0.3.119
0.0.3.117
0.0.3.116
0.0.3.115
0.0.3.113
0.0.3.112
0.0.3.111
0.0.3.110
0.0.3.11
0.0.3.109
0.0.3.108
0.0.3.107
0.0.3.106
0.0.3.105
0.0.3.104
0.0.3.103
0.0.3.102
0.0.3.101
0.0.3.100
0.0.3.10
0.0.3.1
0.0.3.0
0.0.2.9
0.0.2.8
0.0.2.7
0.0.2.6
0.0.2.58
0.0.2.57
0.0.2.56
0.0.2.55
0.0.2.54
0.0.2.53
0.0.2.52
0.0.2.51
0.0.2.50
0.0.2.5
0.0.2.49
0.0.2.48
0.0.2.47
0.0.2.46
0.0.2.45
0.0.2.44
0.0.2.43
0.0.2.42
0.0.2.41
0.0.2.40
0.0.2.4
0.0.2.39
0.0.2.38
0.0.2.37
0.0.2.36
0.0.2.35
0.0.2.34
0.0.2.33
0.0.2.32
0.0.2.31
0.0.2.30
0.0.2.3
0.0.2.29
0.0.2.28
0.0.2.27
0.0.2.26
0.0.2.25
0.0.2.23
0.0.2.22
0.0.2.21
0.0.2.20
0.0.2.2
0.0.2.19
0.0.2.18
0.0.2.17
0.0.2.15
0.0.2.14
0.0.2.13
0.0.2.12
0.0.2.11
0.0.2.10
0.0.2.1
0.0.1.118
0.0.1.117
0.0.1.116
0.0.1.115
0.0.1.114
0.0.1.113
0.0.1.112
0.0.1.111
0.0.1.110
0.0.1.109
0.0.1.108
0.0.1.107
0.0.1.106
0.0.1.105
0.0.1.104
0.0.1.103
0.0.1.102
0.3.179
0.3.178
0.3.177
0.3.176
0.3.175
0.3.174
0.3.173
0.3.172
0.3.171
0.3.170
0.3.169
0.3.168
0.3.167
0.3.166
0.3.165
0.3.164
0.3.163
0.3.162
0.3.161
0.3.160
0.3.159
0.3.158
0.3.157
0.3.156
0.3.155
0.3.154
0.3.153
0.3.152
0.3.151
0.3.150
0.3.149
0.3.148
0.3.147
0.3.145
0.3.144
0.3.143
0.3.142
0.3.141
0.3.140
0.3.139
0.3.138
0.3.137
0.3.136
0.0.3.99
0.0.3.98
0.0.3.97
0.0.3.96
0.0.3.95
0.0.3.94
0.0.3.93
0.0.3.92
0.0.3.91
0.0.3.90
0.0.3.9
0.0.3.89
0.0.3.88
0.0.3.86
0.0.3.85
0.0.3.84
0.0.3.83
0.0.3.82
0.0.3.81
0.0.3.80
0.0.3.8
0.0.3.79
0.0.3.78
0.0.3.77
0.0.3.76
0.0.3.75
0.0.3.74
0.0.3.73
0.0.3.72
0.0.3.71
0.0.3.70
0.0.3.7
0.0.3.69
0.0.3.68
0.0.3.67
0.0.3.66
0.0.3.65
0.0.3.64
0.0.3.63
0.0.3.61
0.0.3.60
0.0.3.6
0.0.3.57
0.0.3.56
0.0.3.55
0.0.3.54
0.0.3.53
0.0.3.52
0.0.3.51
0.0.3.5
0.0.3.48
0.0.3.47
0.0.3.46
0.0.3.45
0.0.3.44
0.0.3.43
0.0.3.42
0.0.3.41
0.0.3.40
0.0.3.4
0.0.3.39
0.0.3.38
0.0.3.37
0.0.3.36
0.0.3.35
0.0.3.34
0.0.3.33
0.0.3.32
0.0.3.31
0.0.3.30
0.0.3.3
0.0.3.29
0.0.3.28
0.0.3.27
0.0.3.26
0.0.3.25
0.0.3.24
0.0.3.23
0.0.3.22
0.0.3.21
0.0.3.20
0.0.3.2
0.0.3.19
0.0.3.18
0.0.3.17
0.0.3.16
0.0.3.15
0.0.3.14
0.0.3.136
0.0.3.135
0.0.3.134
0.0.3.133
0.0.3.132
0.0.3.131
0.0.3.130
0.0.3.13
0.0.3.129
0.0.3.128
0.0.3.127
0.0.3.126
0.0.3.125
0.0.3.124
0.0.3.123
0.0.3.122
0.0.3.121
0.0.3.120
0.0.3.12
0.0.3.119
0.0.3.117
0.0.3.116
0.0.3.115
0.0.3.113
0.0.3.112
0.0.3.111
0.0.3.110
0.0.3.11
0.0.3.109
0.0.3.108
0.0.3.107
0.0.3.106
0.0.3.105
0.0.3.104
0.0.3.103
0.0.3.102
0.0.3.101
0.0.3.100
0.0.3.10
0.0.3.1
0.0.3.0
0.0.2.9
0.0.2.8
0.0.2.7
0.0.2.6
0.0.2.58
0.0.2.57
0.0.2.56
0.0.2.55
0.0.2.54
0.0.2.53
0.0.2.52
0.0.2.51
0.0.2.50
0.0.2.5
0.0.2.49
0.0.2.48
0.0.2.47
0.0.2.46
0.0.2.45
0.0.2.44
0.0.2.43
0.0.2.42
0.0.2.41
0.0.2.40
0.0.2.4
0.0.2.39
0.0.2.38
0.0.2.37
0.0.2.36
0.0.2.35
0.0.2.34
0.0.2.33
0.0.2.32
0.0.2.31
0.0.2.30
0.0.2.3
0.0.2.29
0.0.2.28
0.0.2.27
0.0.2.26
0.0.2.25
0.0.2.23
0.0.2.22
0.0.2.21
0.0.2.20
0.0.2.2
0.0.2.19
0.0.2.18
0.0.2.17
0.0.2.15
0.0.2.14
0.0.2.13
0.0.2.12
0.0.2.11
0.0.2.10
0.0.2.1
0.0.1.118
0.0.1.117
0.0.1.116
0.0.1.115
0.0.1.114
0.0.1.113
0.0.1.112
0.0.1.111
0.0.1.110
0.0.1.109
0.0.1.108
0.0.1.107
0.0.1.106
0.0.1.105
0.0.1.104
0.0.1.103
0.0.1.102
توضیحات
Declarative, typed query language that compiles to SQL.
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | pytrilogy-0.3.179 |
| نام | pytrilogy |
| نسخه کتابخانه | 0.3.179 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | - |
| ایمیل نویسنده | - |
| آدرس صفحه اصلی | - |
| آدرس اینترنتی | https://pypi.org/project/pytrilogy/ |
| مجوز | - |
# Trilogy
**SQL with superpowers for analytics**
[](https://trilogydata.dev/)
[](https://discord.gg/Z4QSSuqGEd)
[](https://badge.fury.io/py/pytrilogy)
The Trilogy language is an experiment in better SQL for analytics - a streamlined version that replaces tables/joins with a lightweight semantic binding layer and provides easy reuse and composability. It compiles to SQL - making it easy to debug or integrate into existing workflows - and can be run against any supported SQL backend.
It shines when used with AI agents, but is built for people first.
[pytrilogy](https://github.com/trilogy-data/pytrilogy) is the reference implementation, written in Python.
## What Trilogy Gives You
- **Speed** - write less, faster. Concise but powerful syntax
- **Efficiency** - easily reuse and compose functions and models, modeled after python
- **Easy refactoring** - change and update tables without breaking queries, and easy testing snd static analysis
- **Testability** - built-in testing patterns with query fixtures
- **Straightforward** - for humans and LLMs alike
Trilogy is especially powerful for data consumption, providing a rich metadata layer that makes creating, interpreting, and visualizing queries easy and expressive.
We recommend starting with the studio to explore Trilogy. For integration, `pytrilogy` can be run locally to parse and execute trilogy model [.preql] files using the `trilogy` CLI tool, or can be run in python by importing the `trilogy` package.
## Quick Start
> [!TIP]
> **Try it now:** [Open-source studio](https://trilogydata.dev/trilogy-studio-core/) | [Interactive demo](https://trilogydata.dev/demo/) | [Documentation](https://trilogydata.dev/)
**Install**
```bash
pip install pytrilogy
```
**Save in hello.preql**
```sql
const prime <- unnest([2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
def cube_plus_one(x) -> (x * x * x + 1);
WHERE
prime_cubed_plus_one % 7 = 0
SELECT
prime,
@cube_plus_one(prime) as prime_cubed_plus_one
ORDER BY
prime asc
LIMIT 10;
```
**Run it in DuckDB**
```bash
trilogy run hello.preql duckdb
```
## Trilogy is Easy to Write
For humans *and* AI. Enjoy flexible, one-shot query generation without any DB access or security risks.
(full code in the python API section.)
```python
query = text_to_query(
executor.environment,
"number of flights by month in 2005",
Provider.OPENAI,
"gpt-5-chat-latest",
api_key,
)
# get a ready to run query
print(query)
# typical output
'''where local.dep_time.year = 2020
select
local.dep_time.month,
count(local.id2) as number_of_flights
order by
local.dep_time.month asc;'''
```
## Goals
Versus SQL, Trilogy aims to:
**Keep:**
- Correctness
- Accessibility
**Improve:**
- Simplicity
- Refactoring/maintainability
- Reusability/composability
- Expressivness
**Maintain:**
- Acceptable performance
## Backend Support
| Backend | Status | Notes |
|---------|--------|-------|
| **BigQuery** | Core | Full support |
| **DuckDB** | Core | Full support |
| **Snowflake** | Core | Full support |
| **SQL Server** | Experimental | Limited testing |
| **Presto** | Experimental | Limited testing |
## Examples
### Hello World
Save the following code in a file named `hello.preql`
```python
# semantic model is abstract from data
type word string; # types can be used to provide expressive metadata tags that propagate through dataflow
key sentence_id int;
property sentence_id.word_one string::word; # comments after a definition
property sentence_id.word_two string::word; # are syntactic sugar for adding
property sentence_id.word_three string::word; # a description to it
# comments in other places are just comments
# define our datasource to bind the model to data
# for most work, you can import something already defined
# testing using query fixtures is a common pattern
datasource word_one(
sentence: sentence_id,
word:word_one
)
grain(sentence_id)
query '''
select 1 as sentence, 'Hello' as word
union all
select 2, 'Bonjour'
''';
datasource word_two(
sentence: sentence_id,
word:word_two
)
grain(sentence_id)
query '''
select 1 as sentence, 'World' as word
union all
select 2 as sentence, 'World'
''';
datasource word_three(
sentence: sentence_id,
word:word_three
)
grain(sentence_id)
query '''
select 1 as sentence, '!' as word
union all
select 2 as sentence, '!'
''';
def concat_with_space(x,y) -> x || ' ' || y;
# an actual select statement
# joins are automatically resolved between the 3 sources
with sentences as
select sentence_id, @concat_with_space(word_one, word_two) || word_three as text;
WHERE
sentences.sentence_id in (1,2)
SELECT
sentences.text
;
```
**Run it:**
```bash
trilogy run hello.preql duckdb
```

### Python SDK Usage
Trilogy can be run directly in python through the core SDK. Trilogy code can be defined and parsed inline or parsed out of files.
A BigQuery example, similar to the [BigQuery quickstart](https://cloud.google.com/bigquery/docs/quickstarts/query-public-dataset-console):
```python
from trilogy import Dialects, Environment
environment = Environment()
environment.parse('''
key name string;
key gender string;
key state string;
key year int;
key yearly_name_count int; int;
datasource usa_names(
name:name,
number:yearly_name_count,
year:year,
gender:gender,
state:state
)
address `bigquery-public-data.usa_names.usa_1910_2013`;
''')
executor = Dialects.BIGQUERY.default_executor(environment=environment)
results = executor.execute_text('''
WHERE
name = 'Elvis'
SELECT
name,
sum(yearly_name_count) -> name_count
ORDER BY
name_count desc
LIMIT 10;
''')
# multiple queries can result from one text batch
for row in results:
# get results for first query
answers = row.fetchall()
for x in answers:
print(x)
```
### LLM Usage
Connect to your favorite provider and generate queries with confidence and high accuracy.
```python
from trilogy import Environment, Dialects
from trilogy.ai import Provider, text_to_query
import os
executor = Dialects.DUCK_DB.default_executor(
environment=Environment(working_path=Path(__file__).parent)
)
api_key = os.environ.get(OPENAI_API_KEY)
if not api_key:
raise ValueError("OPENAI_API_KEY required for gpt generation")
# load a model
executor.parse_file("flight.preql")
# create tables in the DB if needed
executor.execute_file("setup.sql")
# generate a query
query = text_to_query(
executor.environment,
"number of flights by month in 2005",
Provider.OPENAI,
"gpt-5-chat-latest",
api_key,
)
# print the generated trilogy query
print(query)
# run it
results = executor.execute_text(query)[-1].fetchall()
assert len(results) == 12
for row in results:
# all monthly flights are between 5000 and 7000
assert row[1] > 5000 and row[1] < 7000, row
```
### CLI Usage
Trilogy can be run through a CLI tool, also named 'trilogy'.
**Basic syntax:**
```bash
trilogy run <cmd or path to trilogy file> <dialect>
```
**With backend options:**
```bash
trilogy run "key x int; datasource test_source(i:x) grain(x) address test; select x;" duckdb --path <path/to/database>
```
**Format code:**
```bash
trilogy fmt <path to trilogy file>
```
#### Backend Configuration
**BigQuery:**
- Uses applicationdefault authentication (TODO: support arbitrary credential paths)
- In Python, you can pass a custom client
**DuckDB:**
- `--path` - Optional database file path
**Postgres:**
- `--host` - Database host
- `--port` - Database port
- `--username` - Username
- `--password` - Password
- `--database` - Database name
**Snowflake:**
- `--account` - Snowflake account
- `--username` - Username
- `--password` - Password
## Config Files
The CLI can pick up default configuration from a config file in the toml format.
Detection will be recursive form parent directories of the current working directory,
including the current working directory.
This can be used to set
- default engine and arguments
- parallelism for execute for the CLI
- any startup commands to run whenever creating an executor.
```toml
# Trilogy Configuration File
# Learn more at: https://github.com/trilogy-data/pytrilogy
[engine]
# Default dialect for execution
dialect = "duck_db"
# Parallelism level for directory execution
# parallelism = 2
# Startup scripts to run before execution
[setup]
# startup_trilogy = []
sql = ['setup/setup_dev.sql']
```
## More Resources
- [Interactive demo](https://trilogydata.dev/demo/)
- [Public model repository](https://github.com/trilogydata/trilogy-public-models) - Great place for modeling examples
- [Full documentation](https://trilogydata.dev/)
## Python API Integration
### Root Imports
Are stable and should be sufficient for executing code from Trilogy as text.
```python
from pytrilogy import Executor, Dialect
```
### Authoring Imports
Are also stable, and should be used for cases which programatically generate Trilogy statements without text inputs
or need to process/transform parsed code in more complicated ways.
```python
from pytrilogy.authoring import Concept, Function, ...
```
### Other Imports
Are likely to be unstable. Open an issue if you need to take dependencies on other modules outside those two paths.
## MCP/Server
Trilogy is straightforward to run as a server/MCP server; the former to generate SQL on demand and integrate into other tools, and MCP
for full interactive query loops.
This makes it easy to integrate Trilogy into existing tools or workflows.
You can see examples of both use cases in the trilogy-studio codebase [here](https://github.com/trilogy-data/trilogy-studio-core)
and install and run an MCP server directly with that codebase.
If you're interested in a more fleshed out standalone server or MCP server, please open an issue and we'll prioritize it!
## Trilogy Syntax Reference
Not exhaustive - see [documentation](https://trilogydata.dev/) for more details.
### Import
```sql
import [path] as [alias];
```
### Concepts
**Types:**
`string | int | float | bool | date | datetime | time | numeric(scale, precision) | timestamp | interval | array<[type]> | map<[type], [type]> | struct<name:[type], name:[type]>`
**Key:**
```sql
key [name] [type];
```
**Property:**
```sql
property [key].[name] [type];
property x.y int;
# or multi-key
property <[key],[key]>.[name] [type];
property <x,y>.z int;
```
**Transformation:**
```sql
auto [name] <- [expression];
auto x <- y + 1;
```
### Datasource
```sql
datasource <name>(
<column_and_concept_with_same_name>,
# or a mapping from column to concept
<column>:<concept>,
<column>:<concept>,
)
grain(<concept>, <concept>)
address <table>;
datasource orders(
order_id,
order_date,
total_rev: point_of_sale_rev,
customomer_id: customer.id
)
grain orders
address orders;
```
### Queries
**Basic SELECT:**
```sql
WHERE
<concept> = <value>
SELECT
<concept>,
<concept>+1 -> <alias>,
...
HAVING
<alias> = <value2>
ORDER BY
<concept> asc|desc
;
```
**CTEs/Rowsets:**
```sql
with <alias> as
WHERE
<concept> = <value>
select
<concept>,
<concept>+1 -> <alias>,
...
select <alias>.<concept>;
```
### Data Operations
**Persist to table:**
```sql
persist <alias> as <table_name> from
<select>;
```
**Export to file:**
```sql
COPY INTO <TARGET_TYPE> '<target_path>' FROM SELECT
<concept>, ...
ORDER BY
<concept>, ...
;
```
**Show generated SQL:**
```sql
show <select>;
```
**Validate Model**
```sql
validate all
validate concepts abc,def...
validate datasources abc,def...
```
## Contributing
Clone repository and install requirements.txt and requirements-test.txt.
Please open an issue first to discuss what you would like to change, and then create a PR against that issue.
## Similar Projects
Trilogy combines two aspects: a semantic layer and a query language. Examples of both are linked below:
**Semantic layers** - tools for defining a metadata layer above SQL/warehouse to enable higher level abstractions:
- [MetricFlow](https://github.com/dbt-labs/metricflow)
- [Cube](https://github.com/cube-js/cube)
- [Zillion](https://github.com/totalhack/zillion)
**Better SQL** has been a popular space. We believe Trilogy takes a different approach than the following, but all are worth checking out. Please open PRs/comment for anything missed!
- [Malloy](https://github.com/malloydata/malloy)
- [Preql](https://github.com/erezsh/Preql)
- [PRQL](https://github.com/PRQL/prql)
نیازمندی
| مقدار | نام |
|---|---|
| xtr | httpx; |
| xtr | altair; |
| xtr | sqlalchemy-bigquery; |
| xtr | rich; |
| xtr | plotext; |
| xtr | pyodbc; |
| xtr | psycopg2-binary; |
| xtr | fastapi; |
| xtr | uvicorn; |
| xtr | snowflake-sqlalchemy; |
| - | lark |
| - | jinja2 |
| - | sqlalchemy>=2.0.0 |
| - | networkx |
| - | pydantic |
| - | duckdb>1.4.0 |
| - | pyarrow |
| - | duckdb-engine |
| - | click |
| - | pytz |
نحوه نصب
نصب پکیج whl pytrilogy-0.3.179:
pip install pytrilogy-0.3.179.whl
نصب پکیج tar.gz pytrilogy-0.3.179:
pip install pytrilogy-0.3.179.tar.gz