rtgl 0.0.3__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.
Files changed (40) hide show
  1. rtgl-0.0.3/LICENSE +21 -0
  2. rtgl-0.0.3/PKG-INFO +227 -0
  3. rtgl-0.0.3/README.md +197 -0
  4. rtgl-0.0.3/pyproject.toml +77 -0
  5. rtgl-0.0.3/rtgl/__init__.py +9 -0
  6. rtgl-0.0.3/rtgl/base/__init__.py +6 -0
  7. rtgl-0.0.3/rtgl/base/database.py +57 -0
  8. rtgl-0.0.3/rtgl/base/table.py +65 -0
  9. rtgl-0.0.3/rtgl/converter/__init__.py +7 -0
  10. rtgl-0.0.3/rtgl/converter/converter.py +484 -0
  11. rtgl-0.0.3/rtgl/converter/static_converter.py +250 -0
  12. rtgl-0.0.3/rtgl/converter/temporal_converter.py +565 -0
  13. rtgl-0.0.3/rtgl/converter/utils.py +117 -0
  14. rtgl-0.0.3/rtgl/parser/.antlr/LexerRTGL.interp +156 -0
  15. rtgl-0.0.3/rtgl/parser/.antlr/LexerRTGL.java +670 -0
  16. rtgl-0.0.3/rtgl/parser/.antlr/LexerRTGL.tokens +50 -0
  17. rtgl-0.0.3/rtgl/parser/.antlr/ParserRTGL.interp +121 -0
  18. rtgl-0.0.3/rtgl/parser/.antlr/ParserRTGL.java +1743 -0
  19. rtgl-0.0.3/rtgl/parser/.antlr/ParserRTGL.tokens +50 -0
  20. rtgl-0.0.3/rtgl/parser/.antlr/ParserRTGLBaseListener.java +303 -0
  21. rtgl-0.0.3/rtgl/parser/.antlr/ParserRTGLListener.java +229 -0
  22. rtgl-0.0.3/rtgl/parser/LexerRTGL.g4 +252 -0
  23. rtgl-0.0.3/rtgl/parser/ParserRTGL.g4 +134 -0
  24. rtgl-0.0.3/rtgl/parser/__init__.py +7 -0
  25. rtgl-0.0.3/rtgl/parser/gen/LexerRTGL.interp +156 -0
  26. rtgl-0.0.3/rtgl/parser/gen/LexerRTGL.py +415 -0
  27. rtgl-0.0.3/rtgl/parser/gen/LexerRTGL.tokens +50 -0
  28. rtgl-0.0.3/rtgl/parser/gen/ParserRTGL.interp +121 -0
  29. rtgl-0.0.3/rtgl/parser/gen/ParserRTGL.py +1911 -0
  30. rtgl-0.0.3/rtgl/parser/gen/ParserRTGL.tokens +50 -0
  31. rtgl-0.0.3/rtgl/parser/gen/ParserRTGLListener.py +210 -0
  32. rtgl-0.0.3/rtgl/parser/gen/ParserRTGLVisitor.py +123 -0
  33. rtgl-0.0.3/rtgl/validator/__init__.py +8 -0
  34. rtgl-0.0.3/rtgl/validator/error.py +124 -0
  35. rtgl-0.0.3/rtgl/validator/static_validator.py +132 -0
  36. rtgl-0.0.3/rtgl/validator/temporal_validator.py +229 -0
  37. rtgl-0.0.3/rtgl/validator/validator.py +458 -0
  38. rtgl-0.0.3/rtgl/visitor/__init__.py +6 -0
  39. rtgl-0.0.3/rtgl/visitor/parsed_value.py +32 -0
  40. rtgl-0.0.3/rtgl/visitor/visitor.py +531 -0
rtgl-0.0.3/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Oleksii Kolesnichenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
rtgl-0.0.3/PKG-INFO ADDED
@@ -0,0 +1,227 @@
1
+ Metadata-Version: 2.4
2
+ Name: rtgl
3
+ Version: 0.0.3
4
+ Summary: RTGL: A framework providing a Relational Task Generation Language for Relational Deep Learning
5
+ Keywords: relational-task-generation-language,predictive-query-language,sql,relational-deep-learning,deep-learning,relational-learning,machine-learning,temporal-data,task-generation
6
+ Author-email: Oleksii Kolesnichenko <oleksii.kolesnichenko@gmail.com>
7
+ Requires-Python: >=3.12
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: pandas>=2.2.0
11
+ Requires-Dist: antlr4-python3-runtime>=4.13.2
12
+ Requires-Dist: duckdb>=1.0.0
13
+ Requires-Dist: rtgl[test] ; extra == "dev"
14
+ Requires-Dist: rtgl[notebook] ; extra == "dev"
15
+ Requires-Dist: antlr4-tools>=0.2.1 ; extra == "dev"
16
+ Requires-Dist: relbench>=1.1.0 ; extra == "dev"
17
+ Requires-Dist: tqdm>=4.66.0 ; extra == "dev"
18
+ Requires-Dist: ruff>=0.14.10 ; extra == "dev"
19
+ Requires-Dist: ipykernel>=7.1.0 ; extra == "notebook"
20
+ Requires-Dist: jupyter>=1.0.0 ; extra == "notebook"
21
+ Requires-Dist: ipywidgets>=8.1.0 ; extra == "notebook"
22
+ Requires-Dist: pytest>=8.0.0 ; extra == "test"
23
+ Requires-Dist: pytest-cov>=4.1.0 ; extra == "test"
24
+ Project-URL: Issues, https://github.com/kolesole/RTGL/issues
25
+ Project-URL: Repository, https://github.com/kolesole/RTGL
26
+ Provides-Extra: dev
27
+ Provides-Extra: notebook
28
+ Provides-Extra: test
29
+
30
+ # RTGL
31
+
32
+ **RTGL** (Relational Task Generation Language) is a Python framework for writing compact, expressive predictive queries over relational data, especially for Relational Deep Learning.
33
+
34
+ It lets you write shorter, more expressive queries by abstracting temporal joins and complex aggregations.
35
+
36
+ ## 🧠 Features
37
+
38
+ - 🎯 **ANTLR-based Parser**
39
+ - Lexer and parser for RTGL syntax
40
+
41
+ - 🌳 **Structured parse-tree visitor**
42
+ - Converts parsed queries into normalized dictionaries with source positions.
43
+
44
+ - 🔍 **Semantic validation**
45
+ - Schema-aware query validation with error reporting.
46
+
47
+ - 🔀 **Two converters**
48
+ - 📌 `SConverter` for static prediction queries.
49
+ - ⏰ `TConverter` for temporal prediction queries with timestamp windows.
50
+
51
+ - ⚙️ **Dual output mode**
52
+ - `execute=False` returns generated SQL.
53
+ - `execute=True` executes SQL and returns a `Table` object.
54
+
55
+ ## ⚙️ Installation
56
+
57
+ Install RTGL via pip:
58
+
59
+ ```bash
60
+ pip install rtgl
61
+ ```
62
+
63
+ ## 🚀 Quickstart
64
+
65
+ ### 1. Build your database as [RelBench](https://github.com/snap-stanford/relbench) `Database` object or use simplified RTGL version
66
+
67
+ ```python
68
+ # path to classes
69
+ from rtgl.base import Database, Table
70
+ ```
71
+
72
+ ### 2. Static query with `SConverter`
73
+
74
+ ```python
75
+ from rtgl.converter import SConverter
76
+
77
+ converter = SConverter(db)
78
+
79
+ rtgl_query = """
80
+ PREDICT COUNT_DISTINCT(votes.*
81
+ WHERE votes.votetypeid == 2)
82
+ FOR EACH posts.* WHERE posts.PostTypeId == 1
83
+ AND posts.OwnerUserId IS NOT NULL
84
+ AND posts.OwnerUserId != -1;
85
+ """
86
+
87
+ # SQL only
88
+ sql_query = converter.convert(rtgl_query, execute=False)
89
+
90
+ # execute and get Table(fk, label)
91
+ table = converter.convert(rtgl_query, execute=True)
92
+ ```
93
+
94
+ ### 3. Temporal query with `TConverter`
95
+
96
+ ```python
97
+ import pandas as pd
98
+ from rtgl.converter import TConverter
99
+
100
+ timestamps = pd.Series(...) # define timestamps for which prediction must be made
101
+ converter = TConverter(db, timestamps)
102
+
103
+ # also, it is possible to update prediction timestamps later without recreating converter
104
+ converter.set_timestamps(new_timestamps)
105
+
106
+ rtgl_query = """
107
+ PREDICT COUNT_DISTINCT(votes.*
108
+ WHERE votes.votetypeid == 2, 0, 91, DAYS)
109
+ FOR EACH posts.* WHERE posts.PostTypeId == 1
110
+ AND posts.OwnerUserId IS NOT NULL
111
+ AND posts.OwnerUserId != -1;
112
+ """
113
+
114
+ # SQL only
115
+ sql_query = converter.convert(rtgl_query, execute=False)
116
+
117
+ # execute and get Table(fk, timestamp, label)
118
+ table = converter.convert(rtgl_query, execute=True)
119
+ ```
120
+
121
+ ### 4. Examples
122
+
123
+ For more comprehensive examples and use cases, check out the [`relbench_exp.ipynb`](./experiments/relbench_exp.ipynb) notebook.
124
+ You can also check the [`rtgl-tasks`](https://github.com/kolesole/rtgl-tasks) repository for more tasks.
125
+
126
+ ## 📐 Query Language
127
+
128
+ ### 📌 Static query design
129
+
130
+ ```sql
131
+ PREDICT <aggregation | expression | table.column> [RANK TOP K | CLASSIFY]
132
+ FOR EACH <entity_table>.<primary_key>
133
+ [WHERE <static_condition | static_nested_expression>];
134
+ ```
135
+
136
+ ### ⏰ Temporal query shape
137
+
138
+ ```sql
139
+ PREDICT <aggregation | temporal_expression> [RANK TOP K | CLASSIFY]
140
+ FOR EACH <entity_table>.<primary_key> [WHERE <static_condition | static_nested_expression>]
141
+ [ASSUMING <temporal_condition | temporal_nested_expression>]
142
+ [WHERE <temporal_condition | temporal_nested_expression>];
143
+ ```
144
+
145
+ ### 🧮 Aggregations
146
+
147
+ | Function | Meaning | Condition-Compatible |
148
+ | :--- | :--- | :--- |
149
+ | `AVG` | average | ✅ |
150
+ | `MAX` | maximum | ✅ |
151
+ | `MIN` | minimum | ✅ |
152
+ | `SUM` | sum | ✅ |
153
+ | `COUNT` | non-null count | ✅ |
154
+ | `COUNT_DISTINCT` | distinct count | ✅ |
155
+ | `FIRST` | earliest value by time | ✅ |
156
+ | `LAST` | latest value by time | ✅ |
157
+ | `LIST_DISTINCT` | list of distinct values | ❌ |
158
+
159
+ ### 🧭 Temporal window rules
160
+
161
+ - Window format: `<start>, <end>, <measure_unit>`.
162
+ - Supported units: `YEARS`, `MONTHS`, `WEEKS`, `DAYS`, `HOURS`, `MINUTES`, `SECONDS`.
163
+ - Window semantics are half-open: `(start, end]`.
164
+ - `PREDICT`/`WHERE`: `start` and `end` must be non-negative.
165
+ - `ASSUMING`: `start` and `end` must be non-positive.
166
+ - `start` must be strictly less than `end`.
167
+
168
+ ## 🏗️ Architecture
169
+
170
+ ```text
171
+ RTGL Query String
172
+
173
+ [Lexer] -> Tokens
174
+
175
+ [Parser] -> Parse Tree
176
+
177
+ [Visitor] -> Structured Dictionary
178
+
179
+ [Validator] -> Semantic Checks
180
+
181
+ [Converter] -> SQL Query
182
+ ↓ (optional execute=True)
183
+ [DuckDB] -> Result Table
184
+ ```
185
+
186
+ ## 🔧 Development
187
+
188
+ ### Install uv
189
+
190
+ - macOS & Linux
191
+
192
+ ```bash
193
+ wget -qO- https://astral.sh/uv/install.sh | sh
194
+ ```
195
+
196
+ - Windows
197
+
198
+ ```bash
199
+ powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
200
+ ```
201
+
202
+ ### Install dependencies
203
+
204
+ ```bash
205
+ uv sync --all-extras
206
+ ```
207
+
208
+ ### Regenerate parser files
209
+
210
+ If you modify lexer or parser grammar files (`*.g4`), regenerate ANTLR outputs from the repo root:
211
+
212
+ ```bash
213
+ ./regenerate_parser.sh
214
+ ```
215
+
216
+ ### Run tests
217
+
218
+ ```bash
219
+ pytest
220
+ ```
221
+
222
+ ### Run linter
223
+
224
+ ```bash
225
+ ruff check .
226
+ ```
227
+
rtgl-0.0.3/README.md ADDED
@@ -0,0 +1,197 @@
1
+ # RTGL
2
+
3
+ **RTGL** (Relational Task Generation Language) is a Python framework for writing compact, expressive predictive queries over relational data, especially for Relational Deep Learning.
4
+
5
+ It lets you write shorter, more expressive queries by abstracting temporal joins and complex aggregations.
6
+
7
+ ## 🧠 Features
8
+
9
+ - 🎯 **ANTLR-based Parser**
10
+ - Lexer and parser for RTGL syntax
11
+
12
+ - 🌳 **Structured parse-tree visitor**
13
+ - Converts parsed queries into normalized dictionaries with source positions.
14
+
15
+ - 🔍 **Semantic validation**
16
+ - Schema-aware query validation with error reporting.
17
+
18
+ - 🔀 **Two converters**
19
+ - 📌 `SConverter` for static prediction queries.
20
+ - ⏰ `TConverter` for temporal prediction queries with timestamp windows.
21
+
22
+ - ⚙️ **Dual output mode**
23
+ - `execute=False` returns generated SQL.
24
+ - `execute=True` executes SQL and returns a `Table` object.
25
+
26
+ ## ⚙️ Installation
27
+
28
+ Install RTGL via pip:
29
+
30
+ ```bash
31
+ pip install rtgl
32
+ ```
33
+
34
+ ## 🚀 Quickstart
35
+
36
+ ### 1. Build your database as [RelBench](https://github.com/snap-stanford/relbench) `Database` object or use simplified RTGL version
37
+
38
+ ```python
39
+ # path to classes
40
+ from rtgl.base import Database, Table
41
+ ```
42
+
43
+ ### 2. Static query with `SConverter`
44
+
45
+ ```python
46
+ from rtgl.converter import SConverter
47
+
48
+ converter = SConverter(db)
49
+
50
+ rtgl_query = """
51
+ PREDICT COUNT_DISTINCT(votes.*
52
+ WHERE votes.votetypeid == 2)
53
+ FOR EACH posts.* WHERE posts.PostTypeId == 1
54
+ AND posts.OwnerUserId IS NOT NULL
55
+ AND posts.OwnerUserId != -1;
56
+ """
57
+
58
+ # SQL only
59
+ sql_query = converter.convert(rtgl_query, execute=False)
60
+
61
+ # execute and get Table(fk, label)
62
+ table = converter.convert(rtgl_query, execute=True)
63
+ ```
64
+
65
+ ### 3. Temporal query with `TConverter`
66
+
67
+ ```python
68
+ import pandas as pd
69
+ from rtgl.converter import TConverter
70
+
71
+ timestamps = pd.Series(...) # define timestamps for which prediction must be made
72
+ converter = TConverter(db, timestamps)
73
+
74
+ # also, it is possible to update prediction timestamps later without recreating converter
75
+ converter.set_timestamps(new_timestamps)
76
+
77
+ rtgl_query = """
78
+ PREDICT COUNT_DISTINCT(votes.*
79
+ WHERE votes.votetypeid == 2, 0, 91, DAYS)
80
+ FOR EACH posts.* WHERE posts.PostTypeId == 1
81
+ AND posts.OwnerUserId IS NOT NULL
82
+ AND posts.OwnerUserId != -1;
83
+ """
84
+
85
+ # SQL only
86
+ sql_query = converter.convert(rtgl_query, execute=False)
87
+
88
+ # execute and get Table(fk, timestamp, label)
89
+ table = converter.convert(rtgl_query, execute=True)
90
+ ```
91
+
92
+ ### 4. Examples
93
+
94
+ For more comprehensive examples and use cases, check out the [`relbench_exp.ipynb`](./experiments/relbench_exp.ipynb) notebook.
95
+ You can also check the [`rtgl-tasks`](https://github.com/kolesole/rtgl-tasks) repository for more tasks.
96
+
97
+ ## 📐 Query Language
98
+
99
+ ### 📌 Static query design
100
+
101
+ ```sql
102
+ PREDICT <aggregation | expression | table.column> [RANK TOP K | CLASSIFY]
103
+ FOR EACH <entity_table>.<primary_key>
104
+ [WHERE <static_condition | static_nested_expression>];
105
+ ```
106
+
107
+ ### ⏰ Temporal query shape
108
+
109
+ ```sql
110
+ PREDICT <aggregation | temporal_expression> [RANK TOP K | CLASSIFY]
111
+ FOR EACH <entity_table>.<primary_key> [WHERE <static_condition | static_nested_expression>]
112
+ [ASSUMING <temporal_condition | temporal_nested_expression>]
113
+ [WHERE <temporal_condition | temporal_nested_expression>];
114
+ ```
115
+
116
+ ### 🧮 Aggregations
117
+
118
+ | Function | Meaning | Condition-Compatible |
119
+ | :--- | :--- | :--- |
120
+ | `AVG` | average | ✅ |
121
+ | `MAX` | maximum | ✅ |
122
+ | `MIN` | minimum | ✅ |
123
+ | `SUM` | sum | ✅ |
124
+ | `COUNT` | non-null count | ✅ |
125
+ | `COUNT_DISTINCT` | distinct count | ✅ |
126
+ | `FIRST` | earliest value by time | ✅ |
127
+ | `LAST` | latest value by time | ✅ |
128
+ | `LIST_DISTINCT` | list of distinct values | ❌ |
129
+
130
+ ### 🧭 Temporal window rules
131
+
132
+ - Window format: `<start>, <end>, <measure_unit>`.
133
+ - Supported units: `YEARS`, `MONTHS`, `WEEKS`, `DAYS`, `HOURS`, `MINUTES`, `SECONDS`.
134
+ - Window semantics are half-open: `(start, end]`.
135
+ - `PREDICT`/`WHERE`: `start` and `end` must be non-negative.
136
+ - `ASSUMING`: `start` and `end` must be non-positive.
137
+ - `start` must be strictly less than `end`.
138
+
139
+ ## 🏗️ Architecture
140
+
141
+ ```text
142
+ RTGL Query String
143
+
144
+ [Lexer] -> Tokens
145
+
146
+ [Parser] -> Parse Tree
147
+
148
+ [Visitor] -> Structured Dictionary
149
+
150
+ [Validator] -> Semantic Checks
151
+
152
+ [Converter] -> SQL Query
153
+ ↓ (optional execute=True)
154
+ [DuckDB] -> Result Table
155
+ ```
156
+
157
+ ## 🔧 Development
158
+
159
+ ### Install uv
160
+
161
+ - macOS & Linux
162
+
163
+ ```bash
164
+ wget -qO- https://astral.sh/uv/install.sh | sh
165
+ ```
166
+
167
+ - Windows
168
+
169
+ ```bash
170
+ powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
171
+ ```
172
+
173
+ ### Install dependencies
174
+
175
+ ```bash
176
+ uv sync --all-extras
177
+ ```
178
+
179
+ ### Regenerate parser files
180
+
181
+ If you modify lexer or parser grammar files (`*.g4`), regenerate ANTLR outputs from the repo root:
182
+
183
+ ```bash
184
+ ./regenerate_parser.sh
185
+ ```
186
+
187
+ ### Run tests
188
+
189
+ ```bash
190
+ pytest
191
+ ```
192
+
193
+ ### Run linter
194
+
195
+ ```bash
196
+ ruff check .
197
+ ```
@@ -0,0 +1,77 @@
1
+ [build-system]
2
+ requires = ["flit_core>=3.4"]
3
+ build-backend = "flit_core.buildapi"
4
+
5
+ [project]
6
+ name = "rtgl"
7
+ version = "0.0.3"
8
+ description = "RTGL: A framework providing a Relational Task Generation Language for Relational Deep Learning"
9
+ authors = [{ name = "Oleksii Kolesnichenko", email = "oleksii.kolesnichenko@gmail.com" }]
10
+ readme = "README.md"
11
+ requires-python = ">=3.12"
12
+ keywords = [
13
+ "relational-task-generation-language",
14
+ "predictive-query-language",
15
+ "sql",
16
+ "relational-deep-learning",
17
+ "deep-learning",
18
+ "relational-learning",
19
+ "machine-learning",
20
+ "temporal-data",
21
+ "task-generation"
22
+ ]
23
+ dependencies = [
24
+ "pandas>=2.2.0",
25
+ "antlr4-python3-runtime>=4.13.2",
26
+ "duckdb>=1.0.0",
27
+ ]
28
+
29
+ [project.optional-dependencies]
30
+ test = [
31
+ "pytest>=8.0.0",
32
+ "pytest-cov>=4.1.0"
33
+ ]
34
+ notebook = [
35
+ "ipykernel>=7.1.0",
36
+ "jupyter>=1.0.0",
37
+ "ipywidgets>=8.1.0"
38
+ ]
39
+ dev = [
40
+ "rtgl[test]",
41
+ "rtgl[notebook]",
42
+ "antlr4-tools>=0.2.1",
43
+ "relbench>=1.1.0",
44
+ "tqdm>=4.66.0",
45
+ "ruff>=0.14.10"
46
+ ]
47
+
48
+ [project.urls]
49
+ Repository = "https://github.com/kolesole/RTGL"
50
+ Issues = "https://github.com/kolesole/RTGL/issues"
51
+
52
+ [tool.ruff]
53
+ line-length = 121
54
+ target-version = "py312"
55
+ extend-exclude = ["experiments", "showcase", "tasks", "tests", "rtgl/parser/gen/**",]
56
+
57
+ [tool.ruff.lint]
58
+ select = [
59
+ "B", # flake8-bugbear
60
+ "D", # pydocstyle
61
+ "E", # pycodestyle errors
62
+ "F", # pyflakes
63
+ "I", # isort
64
+ "N", # pep8-naming
65
+ "SIM", # flake8-simplify
66
+ "UP", # pyupgrade
67
+ "W", # pycodestyle warnings
68
+ ]
69
+
70
+ [tool.ruff.lint.pydocstyle]
71
+ convention = "google"
72
+
73
+ [tool.ruff.lint.per-file-ignores]
74
+ "__init__.py" = ["F401"]
75
+
76
+ [tool.pytest.ini_options]
77
+ testpaths = ["tests"]
@@ -0,0 +1,9 @@
1
+ """RTGL: A Framework for Relational Task Generation Language."""
2
+
3
+ from rtgl import base
4
+ from rtgl import converter
5
+ from rtgl import parser
6
+ from rtgl import validator
7
+ from rtgl import visitor
8
+
9
+ __all__ = ["base", "converter", "parser", "validator", "visitor"]
@@ -0,0 +1,6 @@
1
+ """Database representation module for RTGL."""
2
+
3
+ from rtgl.base.database import Database
4
+ from rtgl.base.table import Table
5
+
6
+ __all__ = ["Database", "Table"]
@@ -0,0 +1,57 @@
1
+ """Database class to hold multiple tables."""
2
+
3
+ from functools import cached_property
4
+
5
+ import pandas as pd
6
+
7
+ from rtgl.base.table import Table
8
+
9
+
10
+ class Database:
11
+ r"""Represents a database containing multiple related tables.
12
+
13
+ The *`Database`* class stores a collection of *`Table`* objects and provides
14
+ a representation method for displaying all tables in the database.
15
+ """
16
+
17
+ def __init__(self, table_dict: dict[str, Table]) -> None:
18
+ r"""Initializes *`Database`* with a dictionary of tables.
19
+
20
+ Args:
21
+ table_dict (dict[str, Table]): Dictionary where keys are table
22
+ names and values are Table objects.
23
+
24
+ Returns:
25
+ out (None):
26
+ """
27
+ self.table_dict = table_dict
28
+
29
+ def __repr__(self) -> str:
30
+ r"""Returns a string representation of the database.
31
+
32
+ Returns:
33
+ out (str): Formatted string showing all tables in the database.
34
+ """
35
+ return "================= Database ================\n" + "".join(
36
+ f"Table Name: {name}\n{table}\n" for name, table in self.table_dict.items()
37
+ )
38
+
39
+ @cached_property
40
+ def min_timestamp(self) -> pd.Timestamp | None:
41
+ r"""Returns the minimum timestamp across all tables in the database, if any time columns exist.
42
+
43
+ Returns:
44
+ min_timestamp (pd.Timestamp | None): Minimum timestamp across all tables, or None if no time columns.
45
+ """
46
+ min_timestamps = [table.min_timestamp for table in self.table_dict.values() if table.time_col]
47
+ return min(min_timestamps) if min_timestamps else None
48
+
49
+ @cached_property
50
+ def max_timestamp(self) -> pd.Timestamp | None:
51
+ r"""Returns the maximum timestamp across all tables in the database, if any time columns exist.
52
+
53
+ Returns:
54
+ max_timestamp (pd.Timestamp | None): Maximum timestamp across all tables, or None if no time columns.
55
+ """
56
+ max_timestamps = [table.max_timestamp for table in self.table_dict.values() if table.time_col]
57
+ return max(max_timestamps) if max_timestamps else None
@@ -0,0 +1,65 @@
1
+ """Table class representing a database table with metadata."""
2
+
3
+ from functools import cached_property
4
+
5
+ import pandas as pd
6
+
7
+
8
+ class Table:
9
+ r"""Represents a database table with its data and relational metadata.
10
+
11
+ The *`Table`* class encapsulates a pandas DataFrame along with metadata about
12
+ primary keys, foreign keys, and temporal columns.
13
+ """
14
+
15
+ def __init__(
16
+ self, df: pd.DataFrame, fkey_col_to_pkey_table: dict[str, str] = None, pkey_col: str = None, time_col: str = None
17
+ ) -> None:
18
+ r"""Initializes *`Table`* with data and metadata.
19
+
20
+ Args:
21
+ df (pd.DataFrame): The table data.
22
+ fkey_col_to_pkey_table (dict, optional): Dictionary mapping foreign key column names to parent table names.
23
+ Default = None.
24
+ pkey_col (str, optional): Primary key column name.
25
+ Default = None.
26
+ time_col (str, optional): Timestamp column name for temporal tables.
27
+ Default = None.
28
+ """
29
+ self.df = df
30
+ self.fkey_col_to_pkey_table = fkey_col_to_pkey_table
31
+ self.pkey_col = pkey_col
32
+ self.time_col = time_col
33
+
34
+ def __repr__(self) -> str:
35
+ r"""Returns a string representation of the table.
36
+
37
+ Returns:
38
+ out (str): Formatted string showing *`DataFrame`* and all metadata.
39
+ """
40
+ return (
41
+ "------------------ Table ------------------\n"
42
+ f"DataFrame:\n{self.df}\n"
43
+ f"Foreign Key Columns to Primary Key Tables: {self.fkey_col_to_pkey_table}\n"
44
+ f"Primary Key Column: {self.pkey_col}\n"
45
+ f"Time Column: {self.time_col}\n"
46
+ "-------------------------------------------"
47
+ )
48
+
49
+ @cached_property
50
+ def min_timestamp(self) -> pd.Timestamp | None:
51
+ r"""Returns the minimum timestamp in the time column, if it exists.
52
+
53
+ Returns:
54
+ min_timestamp (pd.Timestamp | None): Minimum timestamp in the time column, or None if no time column.
55
+ """
56
+ return self.df[self.time_col].min() if self.time_col else None
57
+
58
+ @cached_property
59
+ def max_timestamp(self) -> pd.Timestamp | None:
60
+ r"""Returns the maximum timestamp in the time column, if it exists.
61
+
62
+ Returns:
63
+ max_timestamp (pd.Timestamp) | None: Maximum timestamp in the time column, or None if no time column.
64
+ """
65
+ return self.df[self.time_col].max() if self.time_col else None
@@ -0,0 +1,7 @@
1
+ """Converter modules for RTGL to SQL translation."""
2
+
3
+ from rtgl.converter.converter import Converter
4
+ from rtgl.converter.static_converter import SConverter
5
+ from rtgl.converter.temporal_converter import TConverter
6
+
7
+ __all__ = ["Converter", "SConverter", "TConverter"]