- 
                Notifications
    You must be signed in to change notification settings 
- Fork 54
Implement values #174
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
          
     Merged
      
      
            warmwaffles
  merged 12 commits into
  elixir-sqlite:main
from
spicychickensauce:implement-values
  
      
      
   
  Sep 15, 2025 
      
    
  
     Merged
                    Implement values #174
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            12 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ff6b2e1
              
                Enable tests for values
              
              
                 bbd1dbb
              
                Implement `values` via CTE
              
              
                 cafc720
              
                Add snapshot test of join with values query
              
              
                 40733b8
              
                Test `values` when combined with `with_cte`
              
              
                 2b01d45
              
                Add test for values using naive_datetime
              
              
                 e851684
              
                Use random name for temp table
              
              
                 2054a79
              
                Exclude out of scope values_list test
              
              
                 3183b70
              
                Ensure test works across multiple OTP versions
              
              
                 b86ecbb
              
                Do not run :values_list tests on elixir 1.15
              
              
                 99cec9a
              
                Cast to appropriate type in values expression
              
              
                 c5cc3c5
              
                Simplify generated code
              
              
                 7cb8351
              
                Account for different key order in OTP 25/26
              
              
                 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 | 
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| defmodule Ecto.Integration.ValuesTest do | ||
| use Ecto.Integration.Case, async: true | ||
|  | ||
| import Ecto.Query, only: [from: 2, with_cte: 3] | ||
|  | ||
| alias Ecto.Integration.Comment | ||
| alias Ecto.Integration.Post | ||
| alias Ecto.Integration.TestRepo | ||
|  | ||
| test "values works with datetime" do | ||
| TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:01:00]}) | ||
| TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:02:00]}) | ||
| TestRepo.insert!(%Post{inserted_at: ~N[2000-01-01 00:03:00]}) | ||
|  | ||
| params = [ | ||
| %{id: 1, date: ~N[2000-01-01 00:00:00]}, | ||
| %{id: 2, date: ~N[2000-01-01 00:01:00]}, | ||
| %{id: 3, date: ~N[2000-01-01 00:02:00]}, | ||
| %{id: 4, date: ~N[2000-01-01 00:03:00]} | ||
| ] | ||
|  | ||
| types = %{id: :integer, date: :naive_datetime} | ||
|  | ||
| results = | ||
| from(params in values(params, types), | ||
| left_join: p in Post, | ||
| on: p.inserted_at <= params.date, | ||
| group_by: params.id, | ||
| select: %{id: params.id, count: count(p.id)}, | ||
| order_by: count(p.id) | ||
| ) | ||
| |> TestRepo.all() | ||
|  | ||
| assert results == [ | ||
| %{count: 0, id: 1}, | ||
| %{count: 1, id: 2}, | ||
| %{count: 2, id: 3}, | ||
| %{count: 3, id: 4} | ||
| ] | ||
| end | ||
|  | ||
| test "join to values works" do | ||
| TestRepo.insert!(%Post{id: 1}) | ||
| TestRepo.insert!(%Comment{post_id: 1, text: "short"}) | ||
| TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) | ||
|  | ||
| params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] | ||
| types = %{id: :integer, post_id: :integer, n: :integer} | ||
|  | ||
| results = | ||
| from(p in Post, | ||
| right_join: params in values(params, types), | ||
| on: params.post_id == p.id, | ||
| left_join: c in Comment, | ||
| on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, | ||
| group_by: params.id, | ||
| select: {params.id, count(c.id)} | ||
| ) | ||
| |> TestRepo.all() | ||
|  | ||
| assert [{1, 2}, {2, 1}] = results | ||
| end | ||
|  | ||
| test "values can be used together with CTE" do | ||
| TestRepo.insert!(%Post{id: 1, visits: 42}) | ||
| TestRepo.insert!(%Comment{post_id: 1, text: "short"}) | ||
| TestRepo.insert!(%Comment{post_id: 1, text: "much longer text"}) | ||
|  | ||
| params = [%{id: 1, post_id: 1, n: 0}, %{id: 2, post_id: 1, n: 10}] | ||
| types = %{id: :integer, post_id: :integer, n: :integer} | ||
|  | ||
| cte_query = from(p in Post, select: %{id: p.id, visits: coalesce(p.visits, 0)}) | ||
|  | ||
| q = Post |> with_cte("xxx", as: ^cte_query) | ||
|  | ||
| results = | ||
| from(p in q, | ||
| right_join: params in values(params, types), | ||
| on: params.post_id == p.id, | ||
| left_join: c in Comment, | ||
| on: c.post_id == p.id and fragment("LENGTH(?)", c.text) > params.n, | ||
| left_join: cte in "xxx", | ||
| on: cte.id == p.id, | ||
| group_by: params.id, | ||
| select: {params.id, count(c.id), cte.visits} | ||
| ) | ||
| |> TestRepo.all() | ||
|  | ||
| assert [{1, 2, 42}, {2, 1, 42}] = results | ||
| end | ||
| end | 
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
I've added the type casting as in the postgres version.
It's probably only needed in a couple of cases, so it might do some unnecessary casting, but I don't think this will hurt.
Without it we were completely ignoring the types that the user supplied, so I think it makes more sense to just always cast.