executeScript method
Future<TtqlExecutionResult>
executeScript(
- String scriptSource, {
- TtqlEnvironmentContext? environmentContext,
- int maxRowsPerStep = 500,
Executes a raw TTQL script and yields a normalized analytical result set.
Implementation
Future<TtqlExecutionResult> executeScript(
String scriptSource, {
TtqlEnvironmentContext? environmentContext,
int maxRowsPerStep = 500,
}) async {
final stopwatch = Stopwatch()..start();
final envContext = environmentContext ?? TtqlEnvironmentBuilder(connectionProvider: connectionProvider).buildContext();
final lexer = TtqlLexer(scriptSource);
final tokens = lexer.tokenize();
final parser = TtqlParser(tokens, environmentContext: envContext);
final ast = parser.parse();
final planner = TtqlDagPlanner(environmentContext: envContext);
final TtqlQueryPlan plan = planner.plan(ast);
final stagingDb = TtqlEphemeralStagingDatabase();
final List<TtqlPlannedStep> allSteps = [];
try {
final sortedStages = plan.sortedStageIndices;
for (final stageIndex in sortedStages) {
final List<TtqlPlannedStep> stageSteps = plan.stages[stageIndex]!;
allSteps.addAll(stageSteps);
await Future.wait(
stageSteps.map((step) => _executeStageStep(step, stagingDb, maxRowsPerStep)),
);
}
final rawJoinedRows = stagingDb.executeFinalSyntheticJoin(
allSteps,
orderBys: ast.orderBys,
orderBy: ast.orderBy,
limit: ast.limit,
offset: ast.offset,
groupByColumns: ast.groupByColumns,
havingPredicate: ast.havingPredicate,
projections: ast.projections,
);
final List<Map<String, dynamic>> normalizedRows;
if (rawJoinedRows.length > 200) {
normalizedRows = await Isolate.run(() => _normalizeResultRows(rawJoinedRows));
} else {
normalizedRows = _normalizeResultRows(rawJoinedRows);
}
final Set<String> detectedCols = {};
for (final row in normalizedRows) {
detectedCols.addAll(row.keys);
}
stopwatch.stop();
return TtqlExecutionResult(
columns: detectedCols.toList(),
rows: normalizedRows,
totalRowCount: normalizedRows.length,
executionDurationMs: stopwatch.elapsedMilliseconds,
stagesExecuted: sortedStages.length,
stagingTableCounts: stagingDb.stagedRowCounts,
);
} finally {
stagingDb.dispose();
}
}