Dynamic Data Functions
Complex excel like formulas for recursive and external data
Dynamic Data Functions are currently in development. This documentation is for feedback and development purposes.
Overview
There are certain types of calculations that are difficult to perform in SQL, particularly recursive calculations that depend upon the result of prior calculations, or that depend on external data. Dynamic Data Functions (DDF) allow you to perform these operations within ChartSQL for advanced use cases.
DDFs are like advanced excel formulas. Your ChartSQL queries can return functions which are executed before the data is rendered.
DDFs are particularly useful for complex, iterative, and conditionally dependent calculations often found in financial modeling, simulations, and analytics
Quick Start
You enable Dynamic Data Functions by adding the directive @ddf: true
to your script. This tells ChartSQL Studio to check each cell in your result for DDFs to be evaluated.
A DDF is any cell that returns a string like =function(){ //code }
DDFs vs Common Table Expressions (CTEs) and Recursive CTEs
SQL Common Table Expressions (CTE) and Recursive CTEs are the native way to perform complex hierarchical and self-referential calculations, however DDFs can do more than CTEs:
Dynamic Execution: DDFs can execute additional runtime logic, manipulate data dynamically, and even fetch or integrate live data during execution.
Inline Functionality: With DDFs, additional logic (like calling external APIs, conditional recursion) can be executed inline without needing additional stored procedures or scripts.
Direct Data Manipulation: DDFs allow on-the-fly data manipulation (like adding rows based on runtime conditions), which is not possible directly within SQL or its CTEs.
Evaluation Order
DDF cells are always evaluated from top to bottom and left to right.
Limitations
Post Processing Only
Dynamic Data Functions are processed by ChartSQL after the data is returned, and so you cannot wrap the result in an outer query or process it further with SQL.
ChartSQL Studio Desktop Only
Dynamic Data Functions have full access to the ChartSQL Studio Server process, and so for security purposes, are not available on ChartSQL Studio Cloud, and cannot be published to DashSQL.
Execution Context
For each DDF invocation, your DDF can access variables about the invocation. You can use these variables to access data and perform dynamic calculations.
In the quick start example, the DDF makes use of the row
context variable, which returns a structure (hashmap) of the currently executing row. It uses this data to concatenate the first name and last name into the full name.
Context Variables Reference
Examples
You will typically use DDFs for the following situations:
Recursive calculations. Complex recursive calculations are straightforward and computationally efficient as DDFs. DDFs naturally accommodate recursion without complex SQL constructs.
Integration with real-time data from external APIs. DDFs allow ChartSQL to embed external functionality directly into queries, enhancing flexibility and reducing the latency you might face in a traditional ETL process that periodically updates a data warehouse.
Fibonacci Series
Consider this complex SQL query to generate a Fibonacci Series (for each row, add the previous two rows). This is a complicated query, hard to read and debug. These kinds of recursive queries are difficult to do in SQL when the business logic gets more complex.
However with DDFs, this is more understandably accomplished:
Compound Interest Rate
The equivalent Recursive CTE (MySQL 8.0)
Last updated