mistercrunch commented on issue #26646:
URL: https://github.com/apache/superset/issues/26646#issuecomment-2103692237

   Any solution that requires parsing SQL is bound to intricately fail at time. 
Given that another idea would be to have the user be explicit about what goes 
above the SELECT statement. You could imagine having two TEXTAREAs in the 
dataset editor, say one label `pre-query` and one labeled `sub-query` (we'd 
probably need a better explanation for this, but that's one option).
   
   Another option would be to use hints and have the parse look for hints
   
   ```sql
   --__PRE_QUERY_BLOCK__
   DECLARE stuff
   WITH crazy_sub_query AS (
     SELECT * from bananas
   )
   --__END_BLOCK__
   SELECT * FROM crazy_sub_query
   ```
   
   Then even today you could have the `config.SQL_QUERY_MUTATOR` do something 
like (this was GPT-generated, didn't take time to validate)
   ```python
   import re
   
   def extract_and_move_block(sql):
       # Define the block markers
       start_marker = "--__PRE_QUERY_BLOCK__"
       end_marker = "--__END_BLOCK__"
       
       # Find the start and end indices of the block
       start_index = sql.find(start_marker)
       end_index = sql.find(end_marker)
       
       # If both markers are found
       if start_index != -1 and end_index != -1:
           # Extract the block content
           block_content = sql[start_index + 
len(start_marker):end_index].strip()
           
           # Remove the block from the original SQL
           sql_without_block = sql[:start_index] + sql[end_index + 
len(end_marker):]
           
           # Move the block to the top of the SQL
           new_sql = start_marker + "\n" + block_content + "\n" + end_marker + 
"\n" + sql_without_block
           
           return new_sql
       else:
           return sql
   
   # Example usage
   sql_input = """
   --__PRE_QUERY_BLOCK__
   DECLARE stuff
   WITH crazy_sub_query AS (
     SELECT * from bananas
   )
   --__END_BLOCK__
   SELECT * FROM crazy_sub_query
   """
   
   new_sql = extract_and_move_block(sql_input)
   print(new_sql)
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to