- 
                Notifications
    You must be signed in to change notification settings 
- Fork 147
Skip lightweight delete when there is no data to delete #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Magicbeanbuyer
  wants to merge
  2
  commits into
  ClickHouse:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
DeepLcom:lw-delete-no-op
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -8,7 +8,7 @@ | |
| seeds_base_csv, | ||
| ) | ||
| from dbt.tests.adapter.basic.test_incremental import BaseIncremental, BaseIncrementalNotSchemaChange | ||
| from dbt.tests.util import run_dbt | ||
| from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
|  | ||
| from tests.integration.adapter.incremental.test_base_incremental import uniq_schema | ||
|  | ||
|  | @@ -56,15 +56,6 @@ def test_simple_incremental(self, project): | |
| run_dbt(["run", "--select", "unique_source_one"]) | ||
| run_dbt(["run", "--select", "unique_incremental_one"]) | ||
|  | ||
|  | ||
| lw_delete_schema = """ | ||
| version: 2 | ||
|  | ||
| models: | ||
| - name: "lw_delete_inc" | ||
| description: "Incremental table" | ||
| """ | ||
|  | ||
| lw_delete_inc = """ | ||
| {{ config( | ||
| materialized='distributed_incremental', | ||
|  | @@ -81,23 +72,93 @@ def test_simple_incremental(self, project): | |
| {% endif %} | ||
| """ | ||
|  | ||
| lw_delete_no_op = """ | ||
| {{ config( | ||
| materialized='distributed_incremental', | ||
| order_by=['key'], | ||
| unique_key='key', | ||
| incremental_strategy='delete+insert' | ||
| ) | ||
| }} | ||
| {% if is_incremental() %} | ||
| SELECT toUInt64(number) as key FROM numbers(50, 10) | ||
| {% else %} | ||
| SELECT toUInt64(number) as key FROM numbers(10) | ||
| {% endif %} | ||
| """ | ||
|  | ||
| LW_DELETE_UNIQUE_KEY_COMPILATION = """ | ||
| {{ config( | ||
| materialized='distributed_incremental', | ||
| order_by=['key'], | ||
| unique_key='key', | ||
| incremental_strategy='delete+insert' | ||
| ) | ||
| }} | ||
| SELECT 1 as key | ||
| UNION ALL | ||
| SELECT 1 as key | ||
| """ | ||
|  | ||
| LW_DELETE_COMPOSITE_UNIQUE_KEY_COMPILATION = """ | ||
| {{ config( | ||
| materialized='distributed_incremental', | ||
| order_by=['key'], | ||
| unique_key=['key', 'date'], | ||
| incremental_strategy='delete+insert' | ||
| ) | ||
| }} | ||
| SELECT 1 as key, toDate('2024-10-21') as date | ||
| UNION ALL | ||
| SELECT 1 as key, toDate('2024-10-21') as date | ||
| """ | ||
|  | ||
|  | ||
| @pytest.mark.skipif(os.environ.get('DBT_CH_TEST_CLUSTER', '').strip() == '', reason='Not on a cluster') | ||
| class TestLWDeleteDistributedIncremental: | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return {"lw_delete_inc.sql": lw_delete_inc} | ||
| return { | ||
| "lw_delete_inc.sql": lw_delete_inc, | ||
| 'lw_delete_no_op.sql': lw_delete_no_op, | ||
| 'lw_delete_unique_key_compilation.sql': LW_DELETE_UNIQUE_KEY_COMPILATION, | ||
| 'lw_delete_composite_unique_key_compilation.sql': LW_DELETE_COMPOSITE_UNIQUE_KEY_COMPILATION, | ||
| } | ||
|  | ||
| @pytest.mark.skipif( | ||
| os.environ.get('DBT_CH_TEST_CLUSTER', '').strip() == '', reason='Not on a cluster' | ||
| ) | ||
| def test_lw_delete(self, project): | ||
| run_dbt() | ||
| @pytest.mark.parametrize("model", ["lw_delete_inc"]) | ||
| def test_lw_delete(self, project, model): | ||
| run_dbt(["run", "--select", model]) | ||
| result = project.run_sql("select count(*) as num_rows from lw_delete_inc", fetch="one") | ||
| assert result[0] == 100 | ||
| run_dbt() | ||
| _, log = run_dbt_and_capture(["run", "--select", model]) | ||
| result = project.run_sql("select count(*) as num_rows from lw_delete_inc", fetch="one") | ||
| assert '20 rows to be deleted.' in log | ||
| assert result[0] == 180 | ||
|  | ||
| @pytest.mark.parametrize("model", ["lw_delete_no_op"]) | ||
| def test_lw_delete_no_op(self, project, model): | ||
| run_dbt(["run", "--select", model]) | ||
| _, log = run_dbt_and_capture(["run", "--select", model]) | ||
| # assert that no delete query is issued against table lw_delete_no_op | ||
| assert 'rows to be deleted.' not in log | ||
| assert 'No data to be deleted, skip lightweight delete.' in log | ||
|  | ||
| @pytest.mark.parametrize( | ||
| "model,delete_filter_log", | ||
| [ | ||
| ("lw_delete_unique_key_compilation", "Delete filter: (1)"), | ||
| ("lw_delete_composite_unique_key_compilation", "Delete filter: (1,'2024-10-21')"), | ||
| ], | ||
| ) | ||
| def test_lw_delete_unique_key(self, project, model, delete_filter_log): | ||
| """Assure that the delete_filter in `DELETE FROM <table> WHERE <unique_key> IN (<delete_filter>)` is templated | ||
| by a string of unique key value(s) when there is only one value (combination) for the unique key(s).""" | ||
| run_dbt(["run", "--select", model]) | ||
| _, log = run_dbt_and_capture(["run", "--select", model, "--log-level", "debug"]) | ||
| result = project.run_sql(f"select count(*) as num_rows from {model}", fetch="one") | ||
| assert delete_filter_log in log | ||
| assert result[0] == 1 | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test failed.  | ||
|  | ||
|  | ||
| compound_key_schema = """ | ||
| version: 2 | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure I fully understood, we fetch the actual keys here, because when you put it as a sup query, you get the issue mentioned in ClickHouse/ClickHouse#69559 ?