Skip to content

Conversation

@sundy-li
Copy link
Member

@sundy-li sundy-li commented Oct 15, 2025

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

This PR implements a better script engine with enhanced support for dynamic schema handling and improved AST parsing for stored procedures and script execution.

Key improvements:

  • Dynamic Schema Support: Added dynamic schema handling for EXECUTE IMMEDIATE statements that can return different schemas based on runtime execution
  • Enhanced AST Parsing: Refactored procedure-related AST structures to use Identifier instead of String for better type safety and script variable support
  • Script Engine Improvements: Enhanced script compilation and execution with better variable resolution and return value handling
  • CALL Statement Enhancements: Improved CALL statements to support identifier variables for dynamic procedure/task names
  • Cursor syntax in procedure:

declare cursor from stmt

 LET c1 CURSOR FOR SELECT number + 10 FROM numbers(10);

declare cursor from resultset, open fetch and close

 LET S RESULTSET := (select number + 10 a, number + 20 b FROM numbers(10));
  LET c1 CURSOR FOR S;

 OPEN c1;
  FETCH c1 INTO var_for_column_value;

close c1;

iterator the cursor:

LET S RESULTSET := (select number + 10 a, number + 20 b FROM numbers(10));
  LET c1 CURSOR FOR S;
  LET sum := 0;
  for c in c1 do
    sum := sum + c.a + c.b;
  end for;

Major changes:

  • Modified ExecuteImmediateStmt to accept expressions instead of plain strings
  • Added ScriptBlockOrStmt enum to handle both script blocks and individual statements
  • Updated task and procedure execution to use Identifier types for names
  • Enhanced HTTP query execution to handle dynamic schemas
  • Added comprehensive test coverage for script variable usage in procedure calls

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@github-actions github-actions bot added the pr-chore this PR only has small changes that no need to record, like coding styles. label Oct 15, 2025
@sundy-li sundy-li changed the title chore(query): improve SQL procedure feat(query): better script engine with dynamic schema support Oct 15, 2025
@github-actions github-actions bot added the pr-feature this PR introduces a new feature to the codebase label Oct 15, 2025
sender.abort();
}
Some(Ok(block)) => {
if is_dynamic_schema {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youngsofun for dynamic schema, please review this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sundy-li sundy-li marked this pull request as ready for review October 15, 2025 15:23
@sundy-li sundy-li requested a review from TCeason October 16, 2025 01:33
@sundy-li sundy-li merged commit f8a4d9b into databendlabs:main Oct 20, 2025
167 of 170 checks passed
@sundy-li sundy-li deleted the script branch October 20, 2025 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-chore this PR only has small changes that no need to record, like coding styles. pr-feature this PR introduces a new feature to the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants