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

fix: resolve edge case with varying shapes in multiple item inserts causing undefined in parameters. #1311

Conversation

naorpeled
Copy link
Contributor

closes #1310

Copy link

vercel bot commented Jan 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 18, 2025 9:30pm

Copy link

pkg-pr-new bot commented Jan 5, 2025

Open in Stackblitzkysely_koa_example

npm i https://pkg.pr.new/kysely-org/kysely@1311

commit: 0c2df8b

@koskimas
Copy link
Member

koskimas commented Jan 6, 2025

Looking good otheriwse but: when inserting a single row, the value should actually be missing and not DEFAULT. The change now caused DEFAULT being used even when you insert a single row with an undefined value (see all the failing tests).

Most databases support DEFAULT but not all. For example on sqlite we need to use null instead. Mapping undefined to null is not a good idea when it can be avoided.

@naorpeled
Copy link
Contributor Author

Looking good otheriwse but: when inserting a single row, the value should actually be missing and not DEFAULT. The change now caused DEFAULT being used even when you insert a single row with an undefined value (see all the failing tests).

Most databases support DEFAULT but not all. For example on sqlite we need to use null instead. Mapping undefined to null is not a good idea when it can be avoided.

Makes sense,
how would you separate those two cases in the cleanest way?

@koskimas
Copy link
Member

koskimas commented Jan 7, 2025

how would you separate those two cases in the cleanest way?

Don't know. Maybe just a special case when rows.length === 1? Add a comment why the special case is there in case you end up with this solution.

@naorpeled naorpeled force-pushed the fix/inserts/resolve-undefined-values-not-replacing-with-default branch from 6b7e19f to c22f2dd Compare January 9, 2025 20:04
@naorpeled naorpeled force-pushed the fix/inserts/resolve-undefined-values-not-replacing-with-default branch from c22f2dd to bb1ed4b Compare January 9, 2025 20:05
@naorpeled naorpeled marked this pull request as ready for review January 9, 2025 20:08
@naorpeled
Copy link
Contributor Author

@koskimas let me know what you think 🙏

Copy link
Member

@koskimas koskimas left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@igalklebanov igalklebanov added the bug Something isn't working label Jan 18, 2025
@igalklebanov igalklebanov force-pushed the fix/inserts/resolve-undefined-values-not-replacing-with-default branch from bb1ed4b to a6cccc7 Compare January 18, 2025 20:02
@igalklebanov igalklebanov changed the title fix(inserts): resolve issues with undefined values fix: resolve edge case with varying shapes in multiple item inserts causing undefined in parameters. Jan 18, 2025
Co-authored-by: Naor Peled <[email protected]>
@igalklebanov
Copy link
Member

igalklebanov commented Jan 18, 2025

Hey @naorpeled @koskimas 👋

The approved changes resolved the issue but were producing more sql than needed.

Here's an added test case that reproduces #1310 output with those changes:
image
There's an extra column ("children") that's always undefined in the items array - so we should not add it to the query.

I have reverted those changes and found the issue in:

  const hasMissingColumns = rowColumns.length < columns.size

This check was false because columns.size was equal to rowColumns.length, but the row had a missing column actually, replaced by an unindexed column with an undefined value.

When it is false and there are no complex expressions, it just creates a primitive list - which included undefined values!

We need to count how many indexed columns the current row has and compare that to the indexed columns count.

@igalklebanov igalklebanov merged commit 660dfb4 into kysely-org:master Jan 18, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Batch inserts use "undefined" instead of falling back on default value
3 participants