Logica fork with extended SQL dialect support including T-SQL (SQL Server) and ClickHouse.
| Dialect | Engine Name | Description |
|---|---|---|
| BigQuery | bigquery |
Google BigQuery (original Logica target) |
| SQLite | sqlite |
SQLite database |
| PostgreSQL | psql |
PostgreSQL database |
| Trino | trino |
Trino distributed SQL |
| Presto | presto |
Presto distributed SQL |
| Databricks | databricks |
Databricks SQL |
| DuckDB | duckdb |
DuckDB analytical database |
| T-SQL | mssql |
Microsoft SQL Server |
| ClickHouse | clickhouse |
ClickHouse OLAP database |
Specify the engine in your Logica program:
@Engine("mssql"); # For SQL Server
Parent(parent: "Alice", child: "Bob");
Parent(parent: "Bob", child: "Carol");
Grandparent(grandparent:, grandchild:) :-
Parent(parent: grandparent, child: x),
Parent(parent: x, child: grandchild);
python logica.py program.l print GrandparentSet the appropriate connection environment variable:
LOGICA_MSSQL_CONNECTION- SQL Server (ODBC format or JSON)LOGICA_CLICKHOUSE_CONNECTION- ClickHouse connection string
tlogica supports native recursive CTEs for T-SQL, similar to the C# implementation (logicasharp). When you define a recursive predicate without the @Recursive annotation, tlogica automatically compiles it to a true SQL recursive CTE:
@Engine("mssql");
Parent("Alice", "Bob");
Parent("Bob", "Carol");
Parent("Carol", "David");
# Native recursive CTE - no @Recursive annotation needed
Ancestor(a, d) :- Parent(a, d);
Ancestor(a, d) :- Parent(a, c), Ancestor(c, d);
This generates efficient T-SQL with WITH ... AS (anchor UNION ALL recursive) syntax:
WITH t_0_Parent AS (...),
Ancestor AS (
SELECT ... FROM t_0_Parent AS Parent -- anchor
UNION ALL
SELECT ... FROM t_0_Parent, Ancestor WHERE ... -- recursive
)
SELECT col0, col1 FROM Ancestor;When to use each approach:
- Native CTE (no annotation): Best for most recursive queries, lets SQL Server optimize the recursion
@Recursive(Predicate, depth): Use when you need explicit depth control or the native CTE causes issues
compiler/dialect_libraries/mssql_library.py- T-SQL specific library functions including ArgMin, ArgMax, Array, Fingerprint, etc.common/mssql_logica.py- Execution module supporting connection via pyodbc with:- Environment variable connection (LOGICA_MSSQL_CONNECTION)
- Support for both ODBC connection strings and JSON config
- Windows authentication (Trusted Connection) support
compiler/recursive_cte.py- Native recursive CTE compilation support with:- Automatic detection of recursive predicates
- Separation of base cases and recursive cases
- Integration with existing WITH clause system
compiler/dialects.py- Added MSSQL dialect class with:- 40+ built-in functions mapped to T-SQL equivalents
- CONCAT() for string concatenation
- OPENJSON() for array unnesting
- JSON_VALUE() for record subscripting
- STRING_AGG() for aggregations
- Proper type casting (NVARCHAR(MAX), BIGINT, FLOAT)
- Native recursive CTE support (
SupportsNativeRecursiveCte()) - Registered as 'mssql' in the DIALECTS dictionary
compiler/expr_translate.py- Added MSSQL to the list of dialects using single-quoted stringscompiler/universe.py- Added native CTE compilation logic and self-reference handlinglogica.py- Added MSSQL execution handler
The integration_tests/mssql_localdb_tests.py file contains integration tests that run against SQL Server LocalDB on Windows. These tests verify T-SQL compilation and execution for:
- Simple facts and rules
- Named fields
- Filters and joins
- Self-joins
- Recursive predicates (both native CTE and depth-based)
- Aggregation (COUNT, SUM)
- Arithmetic operations
- Negation
- UNION of multiple rules
Running the tests:
# Using the batch file (Windows)
run_localdb_tests.bat
# Or using pytest directly
pip install pyodbc pytest
python -m pytest integration_tests/mssql_localdb_tests.py -vRequirements:
- SQL Server LocalDB installed (comes with Visual Studio or SQL Server Express)
- Python pyodbc package
- ODBC Driver 17 or 18 for SQL Server
ClickHouse dialect provides OLAP-optimized SQL generation.
groupArray()for array aggregationarrayJoin()for array unnestingtuple()for record types- ClickHouse-specific type casting
- Backtick identifier quoting