Skip to main content

RTGL: A framework providing a Relational Task Generation Language for Relational Deep Learning

Project description

RTGL

RTGL (Relational Task Generation Language) is a Python framework for writing compact, expressive predictive queries over relational data, especially for Relational Deep Learning.

It lets you write shorter, more expressive queries by abstracting temporal joins and complex aggregations.

🧠 Features

  • 🎯 ANTLR-based Parser

    • Lexer and parser for RTGL syntax
  • 🌳 Structured parse-tree visitor

    • Converts parsed queries into normalized dictionaries with source positions.
  • 🔍 Semantic validation

    • Schema-aware query validation with error reporting.
  • 🔀 Two converters

    • 📌 SConverter for static prediction queries.
    • TConverter for temporal prediction queries with timestamp windows.
  • ⚙️ Dual output mode

    • execute=False returns generated SQL.
    • execute=True executes SQL and returns a Table object.

⚙️ Installation

Install RTGL via pip:

pip install rtgl

🚀 Quickstart

1. Build your database as RelBench Database object or use simplified RTGL version

# path to classes
from rtgl.base import Database, Table

2. Static query with SConverter

from rtgl.converter import SConverter

converter = SConverter(db)

rtgl_query = """
    PREDICT COUNT_DISTINCT(votes.* 
        WHERE votes.votetypeid == 2)
    FOR EACH posts.* WHERE posts.PostTypeId == 1
                       AND posts.OwnerUserId IS NOT NULL
                       AND posts.OwnerUserId != -1;
"""

# SQL only
sql_query = converter.convert(rtgl_query, execute=False)

# execute and get Table(fk, label)
table = converter.convert(rtgl_query, execute=True)

3. Temporal query with TConverter

import pandas as pd
from rtgl.converter import TConverter

timestamps = pd.Series(...) # define timestamps for which prediction must be made
converter = TConverter(db, timestamps)

# also, it is possible to update prediction timestamps later without recreating converter
converter.set_timestamps(new_timestamps)

rtgl_query = """
    PREDICT COUNT_DISTINCT(votes.* 
        WHERE votes.votetypeid == 2, 0, 91, DAYS)
    FOR EACH posts.* WHERE posts.PostTypeId == 1
                       AND posts.OwnerUserId IS NOT NULL
                       AND posts.OwnerUserId != -1;
"""

# SQL only
sql_query = converter.convert(rtgl_query, execute=False)

# execute and get Table(fk, timestamp, label)
table = converter.convert(rtgl_query, execute=True)

4. Examples

For more comprehensive examples and use cases, check out the relbench_exp.ipynb notebook.
You can also check the rtgl-tasks repository for more tasks.

📐 Query Language

📌 Static query design

PREDICT <aggregation | expression | table.column> [RANK TOP K | CLASSIFY]
FOR EACH <entity_table>.<primary_key>
[WHERE <static_condition | static_nested_expression>];

⏰ Temporal query shape

PREDICT <aggregation | temporal_expression> [RANK TOP K | CLASSIFY]
FOR EACH <entity_table>.<primary_key> [WHERE <static_condition | static_nested_expression>]
[ASSUMING <temporal_condition | temporal_nested_expression>]
[WHERE <temporal_condition | temporal_nested_expression>];

🧮 Aggregations

Function Meaning Condition-Compatible
AVG average
MAX maximum
MIN minimum
SUM sum
COUNT non-null count
COUNT_DISTINCT distinct count
FIRST earliest value by time
LAST latest value by time
LIST_DISTINCT list of distinct values

🧭 Temporal window rules

  • Window format: <start>, <end>, <measure_unit>.
  • Supported units: YEARS, MONTHS, WEEKS, DAYS, HOURS, MINUTES, SECONDS.
  • Window semantics are half-open: (start, end].
  • PREDICT/WHERE: start and end must be non-negative.
  • ASSUMING: start and end must be non-positive.
  • start must be strictly less than end.

🏗️ Architecture

RTGL Query String
    ↓
[Lexer] -> Tokens
    ↓
[Parser] -> Parse Tree
    ↓
[Visitor] -> Structured Dictionary
    ↓
[Validator] -> Semantic Checks
    ↓
[Converter] -> SQL Query
    ↓ (optional execute=True)
[DuckDB] -> Result Table

🔧 Development

Install uv

  • macOS & Linux
wget -qO- https://astral.sh/uv/install.sh | sh
  • Windows
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Install dependencies

uv sync --all-extras

Regenerate parser files

If you modify lexer or parser grammar files (*.g4), regenerate ANTLR outputs from the repo root:

./regenerate_parser.sh

Run tests

pytest

Run linter

ruff check .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rtgl-0.0.3.tar.gz (80.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rtgl-0.0.3-py3-none-any.whl (90.8 kB view details)

Uploaded Python 3

File details

Details for the file rtgl-0.0.3.tar.gz.

File metadata

  • Download URL: rtgl-0.0.3.tar.gz
  • Upload date:
  • Size: 80.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rtgl-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fef119270ea891d3b4ff23da5478709c7139cb1374734176a89858ab6210ec9b
MD5 f396d6e1aa73453251990b209e24183e
BLAKE2b-256 d1c32b61e47778951555179afc44a95836215ac208cfebfabf72f46af68d706b

See more details on using hashes here.

File details

Details for the file rtgl-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: rtgl-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 90.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rtgl-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bbfc6952ccda6d2f8957776507a1a38af3f0e9edea940d22f3f6e5ec86b0ebaa
MD5 6e917a423776c32ee230e5d17100e020
BLAKE2b-256 948f0464d97d5ce10a5e09239a1e0e8b97073baae487c21dafabb422c015092f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page