pytrilogy 0.0.1.102__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pytrilogy might be problematic. Click here for more details.
- pytrilogy-0.0.1.102/LICENSE.md +19 -0
- pytrilogy-0.0.1.102/PKG-INFO +277 -0
- pytrilogy-0.0.1.102/README.md +248 -0
- pytrilogy-0.0.1.102/pyproject.toml +10 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/PKG-INFO +277 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/SOURCES.txt +97 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/dependency_links.txt +1 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/entry_points.txt +2 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/requires.txt +16 -0
- pytrilogy-0.0.1.102/pytrilogy.egg-info/top_level.txt +1 -0
- pytrilogy-0.0.1.102/setup.cfg +4 -0
- pytrilogy-0.0.1.102/setup.py +62 -0
- pytrilogy-0.0.1.102/tests/test_declarations.py +16 -0
- pytrilogy-0.0.1.102/tests/test_derived_concepts.py +8 -0
- pytrilogy-0.0.1.102/tests/test_discovery_nodes.py +67 -0
- pytrilogy-0.0.1.102/tests/test_environment.py +22 -0
- pytrilogy-0.0.1.102/tests/test_functions.py +282 -0
- pytrilogy-0.0.1.102/tests/test_imports.py +23 -0
- pytrilogy-0.0.1.102/tests/test_metadata.py +11 -0
- pytrilogy-0.0.1.102/tests/test_models.py +179 -0
- pytrilogy-0.0.1.102/tests/test_multi_join_assignments.py +69 -0
- pytrilogy-0.0.1.102/tests/test_parsing.py +184 -0
- pytrilogy-0.0.1.102/tests/test_partial_handling.py +161 -0
- pytrilogy-0.0.1.102/tests/test_query_processing.py +173 -0
- pytrilogy-0.0.1.102/tests/test_select.py +128 -0
- pytrilogy-0.0.1.102/tests/test_statements.py +43 -0
- pytrilogy-0.0.1.102/tests/test_undefined_concept.py +37 -0
- pytrilogy-0.0.1.102/tests/test_where_clause.py +207 -0
- pytrilogy-0.0.1.102/trilogy/__init__.py +8 -0
- pytrilogy-0.0.1.102/trilogy/compiler.py +0 -0
- pytrilogy-0.0.1.102/trilogy/constants.py +30 -0
- pytrilogy-0.0.1.102/trilogy/core/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/core/constants.py +3 -0
- pytrilogy-0.0.1.102/trilogy/core/enums.py +270 -0
- pytrilogy-0.0.1.102/trilogy/core/env_processor.py +33 -0
- pytrilogy-0.0.1.102/trilogy/core/environment_helpers.py +156 -0
- pytrilogy-0.0.1.102/trilogy/core/ergonomics.py +187 -0
- pytrilogy-0.0.1.102/trilogy/core/exceptions.py +23 -0
- pytrilogy-0.0.1.102/trilogy/core/functions.py +320 -0
- pytrilogy-0.0.1.102/trilogy/core/graph_models.py +55 -0
- pytrilogy-0.0.1.102/trilogy/core/internal.py +37 -0
- pytrilogy-0.0.1.102/trilogy/core/models.py +3145 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/concept_strategies_v3.py +603 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/graph_utils.py +44 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/__init__.py +25 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/basic_node.py +71 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/common.py +239 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/concept_merge.py +152 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/filter_node.py +83 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/group_node.py +92 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/group_to_node.py +99 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/merge_node.py +148 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/multiselect_node.py +189 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/rowset_node.py +130 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/select_node.py +328 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/unnest_node.py +37 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/node_generators/window_node.py +85 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/__init__.py +76 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/base_node.py +251 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/filter_node.py +49 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/group_node.py +110 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/merge_node.py +326 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/select_node_v2.py +198 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/unnest_node.py +54 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/nodes/window_node.py +34 -0
- pytrilogy-0.0.1.102/trilogy/core/processing/utility.py +278 -0
- pytrilogy-0.0.1.102/trilogy/core/query_processor.py +331 -0
- pytrilogy-0.0.1.102/trilogy/dialect/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/dialect/base.py +679 -0
- pytrilogy-0.0.1.102/trilogy/dialect/bigquery.py +80 -0
- pytrilogy-0.0.1.102/trilogy/dialect/common.py +43 -0
- pytrilogy-0.0.1.102/trilogy/dialect/config.py +55 -0
- pytrilogy-0.0.1.102/trilogy/dialect/duckdb.py +83 -0
- pytrilogy-0.0.1.102/trilogy/dialect/enums.py +95 -0
- pytrilogy-0.0.1.102/trilogy/dialect/postgres.py +86 -0
- pytrilogy-0.0.1.102/trilogy/dialect/presto.py +82 -0
- pytrilogy-0.0.1.102/trilogy/dialect/snowflake.py +82 -0
- pytrilogy-0.0.1.102/trilogy/dialect/sql_server.py +89 -0
- pytrilogy-0.0.1.102/trilogy/docs/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/engine.py +48 -0
- pytrilogy-0.0.1.102/trilogy/executor.py +242 -0
- pytrilogy-0.0.1.102/trilogy/hooks/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/hooks/base_hook.py +37 -0
- pytrilogy-0.0.1.102/trilogy/hooks/graph_hook.py +24 -0
- pytrilogy-0.0.1.102/trilogy/hooks/query_debugger.py +133 -0
- pytrilogy-0.0.1.102/trilogy/metadata/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/parser.py +10 -0
- pytrilogy-0.0.1.102/trilogy/parsing/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/parsing/common.py +176 -0
- pytrilogy-0.0.1.102/trilogy/parsing/config.py +5 -0
- pytrilogy-0.0.1.102/trilogy/parsing/exceptions.py +2 -0
- pytrilogy-0.0.1.102/trilogy/parsing/helpers.py +1 -0
- pytrilogy-0.0.1.102/trilogy/parsing/parse_engine.py +1951 -0
- pytrilogy-0.0.1.102/trilogy/parsing/render.py +483 -0
- pytrilogy-0.0.1.102/trilogy/py.typed +0 -0
- pytrilogy-0.0.1.102/trilogy/scripts/__init__.py +0 -0
- pytrilogy-0.0.1.102/trilogy/scripts/trilogy.py +127 -0
- pytrilogy-0.0.1.102/trilogy/utility.py +31 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2023
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pytrilogy
|
|
3
|
+
Version: 0.0.1.102
|
|
4
|
+
Summary: Declarative, typed query language that compiles to SQL.
|
|
5
|
+
Home-page:
|
|
6
|
+
Author:
|
|
7
|
+
Author-email: preql-community@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE.md
|
|
16
|
+
Requires-Dist: lark
|
|
17
|
+
Requires-Dist: jinja2
|
|
18
|
+
Requires-Dist: sqlalchemy<2.0.0
|
|
19
|
+
Requires-Dist: networkx
|
|
20
|
+
Requires-Dist: pyodbc
|
|
21
|
+
Requires-Dist: pydantic
|
|
22
|
+
Requires-Dist: duckdb-engine
|
|
23
|
+
Provides-Extra: postgres
|
|
24
|
+
Requires-Dist: psycopg2-binary; extra == "postgres"
|
|
25
|
+
Provides-Extra: bigquery
|
|
26
|
+
Requires-Dist: sqlalchemy-bigquery; extra == "bigquery"
|
|
27
|
+
Provides-Extra: snowflake
|
|
28
|
+
Requires-Dist: snowflake-sqlalchemy; extra == "snowflake"
|
|
29
|
+
|
|
30
|
+
##Trilogy
|
|
31
|
+
[](https://trilogydata.dev/)
|
|
32
|
+
[](https://discord.gg/Z4QSSuqGEd)
|
|
33
|
+
|
|
34
|
+
pytrilogy is an experimental implementation of the Trilogy language, a higher-level SQL that replaces tables/joins with a lightweight semantic binding layer.
|
|
35
|
+
|
|
36
|
+
Trilogy looks like SQL, but simpler. It's a modern SQL refresh targeted at SQL lovers who want reusability and simplicity with the power and iteratability of SQL. It compiles to SQL - making it easy to debug or integrate into existing workflows - and can be run against any supported SQL backend.
|
|
37
|
+
|
|
38
|
+
> [!TIP]
|
|
39
|
+
> To get an overview of the language and run interactive examples, head to the [documentation](https://trilogydata.dev/).
|
|
40
|
+
|
|
41
|
+
Installation: `pip install pytrilogy`
|
|
42
|
+
|
|
43
|
+
`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.
|
|
44
|
+
|
|
45
|
+
You can read more about the project [here](https://trilogydata.dev/) and try out an interactive demo on the page an interactive demo [here](https://trilogydata.dev/demo).
|
|
46
|
+
|
|
47
|
+
Trilogy:
|
|
48
|
+
```sql
|
|
49
|
+
SELECT
|
|
50
|
+
name,
|
|
51
|
+
count(name) as name_count
|
|
52
|
+
WHERE
|
|
53
|
+
name='Elvis'
|
|
54
|
+
ORDER BY
|
|
55
|
+
name_count desc
|
|
56
|
+
LIMIT 10;
|
|
57
|
+
```
|
|
58
|
+
## Goals
|
|
59
|
+
vs SQL, the goals are:
|
|
60
|
+
|
|
61
|
+
Preserve:
|
|
62
|
+
- Correctness
|
|
63
|
+
- Accessibility
|
|
64
|
+
|
|
65
|
+
Enhance:
|
|
66
|
+
- Simplicity
|
|
67
|
+
- Understandability
|
|
68
|
+
- Refactoring/mantainability
|
|
69
|
+
- Reusability
|
|
70
|
+
|
|
71
|
+
Maintain:
|
|
72
|
+
- Acceptable performance
|
|
73
|
+
|
|
74
|
+
## Hello World
|
|
75
|
+
|
|
76
|
+
Save the following code in a file named `hello.preql`
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
key sentence_id int;
|
|
80
|
+
property sentence_id.word_one string; # comments after a definition
|
|
81
|
+
property sentence_id.word_two string; # are syntactic sugar for adding
|
|
82
|
+
property sentence_id.word_three string; # a description to it
|
|
83
|
+
|
|
84
|
+
# comments in other places are just comments
|
|
85
|
+
|
|
86
|
+
# define our datasources as queries in duckdb
|
|
87
|
+
datasource word_one(
|
|
88
|
+
sentence: sentence_id,
|
|
89
|
+
word:word_one
|
|
90
|
+
)
|
|
91
|
+
grain(sentence_id)
|
|
92
|
+
query '''
|
|
93
|
+
select 1 as sentence, 'Hello' as word
|
|
94
|
+
union all
|
|
95
|
+
select 2, 'Bonjour'
|
|
96
|
+
''';
|
|
97
|
+
|
|
98
|
+
datasource word_two(
|
|
99
|
+
sentence: sentence_id,
|
|
100
|
+
word:word_two
|
|
101
|
+
)
|
|
102
|
+
grain(sentence_id)
|
|
103
|
+
query '''
|
|
104
|
+
select 1 as sentence, 'World' as word
|
|
105
|
+
union all
|
|
106
|
+
select 2 as sentence, 'World'
|
|
107
|
+
''';
|
|
108
|
+
|
|
109
|
+
datasource word_three(
|
|
110
|
+
sentence: sentence_id,
|
|
111
|
+
word:word_three
|
|
112
|
+
)
|
|
113
|
+
grain(sentence_id)
|
|
114
|
+
query '''
|
|
115
|
+
select 1 as sentence, '!' as word
|
|
116
|
+
union all
|
|
117
|
+
select 2 as sentence, '!'
|
|
118
|
+
''';
|
|
119
|
+
|
|
120
|
+
# an actual select statement
|
|
121
|
+
# joins are automatically resolved between the 3 sources
|
|
122
|
+
with sentences as
|
|
123
|
+
select sentence_id, word_one || ' ' || word_two || word_three as text;
|
|
124
|
+
|
|
125
|
+
SELECT
|
|
126
|
+
--sentences.sentence_id,
|
|
127
|
+
sentences.text
|
|
128
|
+
WHERE
|
|
129
|
+
sentences.sentence_id = 1
|
|
130
|
+
;
|
|
131
|
+
|
|
132
|
+
SELECT
|
|
133
|
+
--sentences.sentence_id,
|
|
134
|
+
sentences.text
|
|
135
|
+
WHERE
|
|
136
|
+
sentences.sentence_id = 2
|
|
137
|
+
;
|
|
138
|
+
# semicolon termination for all statements
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Run the following from the directory the file is in.
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
trilogy run hello.trilogy duckdb
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+

|
|
149
|
+
|
|
150
|
+
## Backends
|
|
151
|
+
|
|
152
|
+
The current Trilogy implementation supports these backends:
|
|
153
|
+
|
|
154
|
+
- Bigquery
|
|
155
|
+
- SQL Server
|
|
156
|
+
- DuckDB
|
|
157
|
+
- Snowflake
|
|
158
|
+
|
|
159
|
+
## Basic Example - Python
|
|
160
|
+
|
|
161
|
+
Trilogy can be run directly in python.
|
|
162
|
+
|
|
163
|
+
A bigquery example, similar to bigquery [the quickstart](https://cloud.google.com/bigquery/docs/quickstarts/query-public-dataset-console)
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
from trilogy import Dialects, Environment
|
|
169
|
+
|
|
170
|
+
environment = Environment()
|
|
171
|
+
|
|
172
|
+
environment.parse('''
|
|
173
|
+
|
|
174
|
+
key name string;
|
|
175
|
+
key gender string;
|
|
176
|
+
key state string;
|
|
177
|
+
key year int;
|
|
178
|
+
key yearly_name_count int; int;
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
datasource usa_names(
|
|
182
|
+
name:name,
|
|
183
|
+
number:yearly_name_count,
|
|
184
|
+
year:year,
|
|
185
|
+
gender:gender,
|
|
186
|
+
state:state
|
|
187
|
+
)
|
|
188
|
+
address bigquery-public-data.usa_names.usa_1910_2013;
|
|
189
|
+
|
|
190
|
+
'''
|
|
191
|
+
)
|
|
192
|
+
executor = Dialects.BIGQUERY.default_executor(environment=environment)
|
|
193
|
+
|
|
194
|
+
results = executor.execute_text(
|
|
195
|
+
'''SELECT
|
|
196
|
+
name,
|
|
197
|
+
sum(yearly_name_count) -> name_count
|
|
198
|
+
WHERE
|
|
199
|
+
name = 'Elvis'
|
|
200
|
+
ORDER BY
|
|
201
|
+
name_count desc
|
|
202
|
+
LIMIT 10;
|
|
203
|
+
'''
|
|
204
|
+
|
|
205
|
+
)
|
|
206
|
+
# multiple queries can result from one text batch
|
|
207
|
+
for row in results:
|
|
208
|
+
# get results for first query
|
|
209
|
+
answers = row.fetchall()
|
|
210
|
+
for x in answers:
|
|
211
|
+
print(x)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
## Basic Example - CLI
|
|
216
|
+
|
|
217
|
+
Trilogy can be run through a CLI tool, appropriately named 'trilogy'.
|
|
218
|
+
|
|
219
|
+
After installing trilogy, you can run the trilogy CLI with two required positional arguments; the first the path to a file or a direct command,
|
|
220
|
+
and second the dialect to run.
|
|
221
|
+
|
|
222
|
+
`trilogy run <cmd or path to trilogy file> <dialect>`
|
|
223
|
+
|
|
224
|
+
To pass arguments to a backend, append additional --<option> flags after specifying the dialect.
|
|
225
|
+
|
|
226
|
+
Example:
|
|
227
|
+
`trilogy run key in int; datasource test_source ( i:in) grain(in) address test; select in;" duckdb --path <path/to/duckdb>`
|
|
228
|
+
|
|
229
|
+
### Bigquery Args
|
|
230
|
+
N/A, only supports default auth. In python you can pass in a custom client.
|
|
231
|
+
<TODO> support arbitrary cred paths.
|
|
232
|
+
|
|
233
|
+
### DuckDB Args
|
|
234
|
+
- path <optional>
|
|
235
|
+
|
|
236
|
+
### Postgres Args
|
|
237
|
+
- host
|
|
238
|
+
- port
|
|
239
|
+
- username
|
|
240
|
+
- password
|
|
241
|
+
- database
|
|
242
|
+
|
|
243
|
+
### Snowflake Args
|
|
244
|
+
- account
|
|
245
|
+
- username
|
|
246
|
+
- password
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
> [!TIP]
|
|
250
|
+
> The CLI can also be used for formatting. PreQL has a default formatting style that should always be adhered to. `trilogy fmt <path to trilogy file>`
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
## More Examples
|
|
254
|
+
|
|
255
|
+
[Interactive demo](https://trilogydata.dev/demo).
|
|
256
|
+
|
|
257
|
+
Additional examples can be found in the [public model repository](https://github.com/trilogydata/trilogy-public-models).
|
|
258
|
+
|
|
259
|
+
This is a good place to look for modeling examples.
|
|
260
|
+
|
|
261
|
+
## Developing
|
|
262
|
+
|
|
263
|
+
Clone repository and install requirements.txt and requirements-test.txt.
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
Please open an issue first to discuss what you would like to change, and then create a PR against that issue.
|
|
268
|
+
|
|
269
|
+
## Similar in space
|
|
270
|
+
|
|
271
|
+
"Better SQL" has been a popular space. We believe Trilogy takes a different approach then the following,
|
|
272
|
+
but all are worth checking out. Please open PRs/comment for anything missed!
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
- [malloy](https://github.com/malloydata/malloy)
|
|
276
|
+
- [preql](https://github.com/erezsh/Preql)
|
|
277
|
+
- [PREQL](https://github.com/PRQL/prql)
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
##Trilogy
|
|
2
|
+
[](https://trilogydata.dev/)
|
|
3
|
+
[](https://discord.gg/Z4QSSuqGEd)
|
|
4
|
+
|
|
5
|
+
pytrilogy is an experimental implementation of the Trilogy language, a higher-level SQL that replaces tables/joins with a lightweight semantic binding layer.
|
|
6
|
+
|
|
7
|
+
Trilogy looks like SQL, but simpler. It's a modern SQL refresh targeted at SQL lovers who want reusability and simplicity with the power and iteratability of SQL. It compiles to SQL - making it easy to debug or integrate into existing workflows - and can be run against any supported SQL backend.
|
|
8
|
+
|
|
9
|
+
> [!TIP]
|
|
10
|
+
> To get an overview of the language and run interactive examples, head to the [documentation](https://trilogydata.dev/).
|
|
11
|
+
|
|
12
|
+
Installation: `pip install pytrilogy`
|
|
13
|
+
|
|
14
|
+
`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.
|
|
15
|
+
|
|
16
|
+
You can read more about the project [here](https://trilogydata.dev/) and try out an interactive demo on the page an interactive demo [here](https://trilogydata.dev/demo).
|
|
17
|
+
|
|
18
|
+
Trilogy:
|
|
19
|
+
```sql
|
|
20
|
+
SELECT
|
|
21
|
+
name,
|
|
22
|
+
count(name) as name_count
|
|
23
|
+
WHERE
|
|
24
|
+
name='Elvis'
|
|
25
|
+
ORDER BY
|
|
26
|
+
name_count desc
|
|
27
|
+
LIMIT 10;
|
|
28
|
+
```
|
|
29
|
+
## Goals
|
|
30
|
+
vs SQL, the goals are:
|
|
31
|
+
|
|
32
|
+
Preserve:
|
|
33
|
+
- Correctness
|
|
34
|
+
- Accessibility
|
|
35
|
+
|
|
36
|
+
Enhance:
|
|
37
|
+
- Simplicity
|
|
38
|
+
- Understandability
|
|
39
|
+
- Refactoring/mantainability
|
|
40
|
+
- Reusability
|
|
41
|
+
|
|
42
|
+
Maintain:
|
|
43
|
+
- Acceptable performance
|
|
44
|
+
|
|
45
|
+
## Hello World
|
|
46
|
+
|
|
47
|
+
Save the following code in a file named `hello.preql`
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
key sentence_id int;
|
|
51
|
+
property sentence_id.word_one string; # comments after a definition
|
|
52
|
+
property sentence_id.word_two string; # are syntactic sugar for adding
|
|
53
|
+
property sentence_id.word_three string; # a description to it
|
|
54
|
+
|
|
55
|
+
# comments in other places are just comments
|
|
56
|
+
|
|
57
|
+
# define our datasources as queries in duckdb
|
|
58
|
+
datasource word_one(
|
|
59
|
+
sentence: sentence_id,
|
|
60
|
+
word:word_one
|
|
61
|
+
)
|
|
62
|
+
grain(sentence_id)
|
|
63
|
+
query '''
|
|
64
|
+
select 1 as sentence, 'Hello' as word
|
|
65
|
+
union all
|
|
66
|
+
select 2, 'Bonjour'
|
|
67
|
+
''';
|
|
68
|
+
|
|
69
|
+
datasource word_two(
|
|
70
|
+
sentence: sentence_id,
|
|
71
|
+
word:word_two
|
|
72
|
+
)
|
|
73
|
+
grain(sentence_id)
|
|
74
|
+
query '''
|
|
75
|
+
select 1 as sentence, 'World' as word
|
|
76
|
+
union all
|
|
77
|
+
select 2 as sentence, 'World'
|
|
78
|
+
''';
|
|
79
|
+
|
|
80
|
+
datasource word_three(
|
|
81
|
+
sentence: sentence_id,
|
|
82
|
+
word:word_three
|
|
83
|
+
)
|
|
84
|
+
grain(sentence_id)
|
|
85
|
+
query '''
|
|
86
|
+
select 1 as sentence, '!' as word
|
|
87
|
+
union all
|
|
88
|
+
select 2 as sentence, '!'
|
|
89
|
+
''';
|
|
90
|
+
|
|
91
|
+
# an actual select statement
|
|
92
|
+
# joins are automatically resolved between the 3 sources
|
|
93
|
+
with sentences as
|
|
94
|
+
select sentence_id, word_one || ' ' || word_two || word_three as text;
|
|
95
|
+
|
|
96
|
+
SELECT
|
|
97
|
+
--sentences.sentence_id,
|
|
98
|
+
sentences.text
|
|
99
|
+
WHERE
|
|
100
|
+
sentences.sentence_id = 1
|
|
101
|
+
;
|
|
102
|
+
|
|
103
|
+
SELECT
|
|
104
|
+
--sentences.sentence_id,
|
|
105
|
+
sentences.text
|
|
106
|
+
WHERE
|
|
107
|
+
sentences.sentence_id = 2
|
|
108
|
+
;
|
|
109
|
+
# semicolon termination for all statements
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Run the following from the directory the file is in.
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
trilogy run hello.trilogy duckdb
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+

|
|
120
|
+
|
|
121
|
+
## Backends
|
|
122
|
+
|
|
123
|
+
The current Trilogy implementation supports these backends:
|
|
124
|
+
|
|
125
|
+
- Bigquery
|
|
126
|
+
- SQL Server
|
|
127
|
+
- DuckDB
|
|
128
|
+
- Snowflake
|
|
129
|
+
|
|
130
|
+
## Basic Example - Python
|
|
131
|
+
|
|
132
|
+
Trilogy can be run directly in python.
|
|
133
|
+
|
|
134
|
+
A bigquery example, similar to bigquery [the quickstart](https://cloud.google.com/bigquery/docs/quickstarts/query-public-dataset-console)
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
from trilogy import Dialects, Environment
|
|
140
|
+
|
|
141
|
+
environment = Environment()
|
|
142
|
+
|
|
143
|
+
environment.parse('''
|
|
144
|
+
|
|
145
|
+
key name string;
|
|
146
|
+
key gender string;
|
|
147
|
+
key state string;
|
|
148
|
+
key year int;
|
|
149
|
+
key yearly_name_count int; int;
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
datasource usa_names(
|
|
153
|
+
name:name,
|
|
154
|
+
number:yearly_name_count,
|
|
155
|
+
year:year,
|
|
156
|
+
gender:gender,
|
|
157
|
+
state:state
|
|
158
|
+
)
|
|
159
|
+
address bigquery-public-data.usa_names.usa_1910_2013;
|
|
160
|
+
|
|
161
|
+
'''
|
|
162
|
+
)
|
|
163
|
+
executor = Dialects.BIGQUERY.default_executor(environment=environment)
|
|
164
|
+
|
|
165
|
+
results = executor.execute_text(
|
|
166
|
+
'''SELECT
|
|
167
|
+
name,
|
|
168
|
+
sum(yearly_name_count) -> name_count
|
|
169
|
+
WHERE
|
|
170
|
+
name = 'Elvis'
|
|
171
|
+
ORDER BY
|
|
172
|
+
name_count desc
|
|
173
|
+
LIMIT 10;
|
|
174
|
+
'''
|
|
175
|
+
|
|
176
|
+
)
|
|
177
|
+
# multiple queries can result from one text batch
|
|
178
|
+
for row in results:
|
|
179
|
+
# get results for first query
|
|
180
|
+
answers = row.fetchall()
|
|
181
|
+
for x in answers:
|
|
182
|
+
print(x)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
## Basic Example - CLI
|
|
187
|
+
|
|
188
|
+
Trilogy can be run through a CLI tool, appropriately named 'trilogy'.
|
|
189
|
+
|
|
190
|
+
After installing trilogy, you can run the trilogy CLI with two required positional arguments; the first the path to a file or a direct command,
|
|
191
|
+
and second the dialect to run.
|
|
192
|
+
|
|
193
|
+
`trilogy run <cmd or path to trilogy file> <dialect>`
|
|
194
|
+
|
|
195
|
+
To pass arguments to a backend, append additional --<option> flags after specifying the dialect.
|
|
196
|
+
|
|
197
|
+
Example:
|
|
198
|
+
`trilogy run key in int; datasource test_source ( i:in) grain(in) address test; select in;" duckdb --path <path/to/duckdb>`
|
|
199
|
+
|
|
200
|
+
### Bigquery Args
|
|
201
|
+
N/A, only supports default auth. In python you can pass in a custom client.
|
|
202
|
+
<TODO> support arbitrary cred paths.
|
|
203
|
+
|
|
204
|
+
### DuckDB Args
|
|
205
|
+
- path <optional>
|
|
206
|
+
|
|
207
|
+
### Postgres Args
|
|
208
|
+
- host
|
|
209
|
+
- port
|
|
210
|
+
- username
|
|
211
|
+
- password
|
|
212
|
+
- database
|
|
213
|
+
|
|
214
|
+
### Snowflake Args
|
|
215
|
+
- account
|
|
216
|
+
- username
|
|
217
|
+
- password
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
> [!TIP]
|
|
221
|
+
> The CLI can also be used for formatting. PreQL has a default formatting style that should always be adhered to. `trilogy fmt <path to trilogy file>`
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
## More Examples
|
|
225
|
+
|
|
226
|
+
[Interactive demo](https://trilogydata.dev/demo).
|
|
227
|
+
|
|
228
|
+
Additional examples can be found in the [public model repository](https://github.com/trilogydata/trilogy-public-models).
|
|
229
|
+
|
|
230
|
+
This is a good place to look for modeling examples.
|
|
231
|
+
|
|
232
|
+
## Developing
|
|
233
|
+
|
|
234
|
+
Clone repository and install requirements.txt and requirements-test.txt.
|
|
235
|
+
|
|
236
|
+
## Contributing
|
|
237
|
+
|
|
238
|
+
Please open an issue first to discuss what you would like to change, and then create a PR against that issue.
|
|
239
|
+
|
|
240
|
+
## Similar in space
|
|
241
|
+
|
|
242
|
+
"Better SQL" has been a popular space. We believe Trilogy takes a different approach then the following,
|
|
243
|
+
but all are worth checking out. Please open PRs/comment for anything missed!
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
- [malloy](https://github.com/malloydata/malloy)
|
|
247
|
+
- [preql](https://github.com/erezsh/Preql)
|
|
248
|
+
- [PREQL](https://github.com/PRQL/prql)
|