Skip to content
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

Wrong Arel expressions for semi-joins #42

Open
leoarnold opened this issue Jan 2, 2023 · 1 comment
Open

Wrong Arel expressions for semi-joins #42

leoarnold opened this issue Jan 2, 2023 · 1 comment

Comments

@leoarnold
Copy link

Given the SQL statement

SELECT * FROM ingredients WHERE EXISTS (
  SELECT "1" AS one FROM dishes
  WHERE dishes.taste = 'umami' AND dishes.ingredient_id = ingredients.id
);

scuttle suggests

Ingredient.select(Arel.star).where(
  Dish.select(:1.as('one')).where(
    Dish.arel_table[:taste].eq('umami').and(
      Dish.arel_table[:ingredient_id].eq(Ingredient.arel_table[:id])
    )
  ).exists
)

which is incorrect, as .exists is defined only on a SelectManager, so we need #project:

Ingredient.select(Arel.star).where(
  Dish.arel_table.project(:1.as('one')).where( # <---- 
    Dish.arel_table[:taste].eq('umami').and(
      Dish.arel_table[:ingredient_id].eq(Ingredient.arel_table[:id])
    )
  ).exists
)

Also, for the negation

SELECT * FROM ingredients WHERE NOT ( EXISTS (
  SELECT "1" AS one FROM dishes
  WHERE dishes.taste = 'umami' AND dishes.ingredient_id = ingredients.id
));

scuttle seems to ignore the the negation:

Ingredient.select(Arel.star).where(
  Dish.select(:1.as('one')).where(
    Dish.arel_table[:taste].eq('umami').and(
      Dish.arel_table[:ingredient_id].eq(Ingredient.arel_table[:id])
    )
  ).exists
)

We would need:

Ingredient.select(Arel.star).where.not( # <----
  Dish.arel_table.project(:1.as('one')).where( # <---- 
    Dish.arel_table[:taste].eq('umami').and(
      Dish.arel_table[:ingredient_id].eq(Ingredient.arel_table[:id])
    )
  ).exists
)
@camertron
Copy link
Owner

Thanks for the bug report @leoarnold. Do you know if project is always allowed in place of select?

Also, would this be something you'd be willing to dive in and fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@camertron @leoarnold and others