-
Notifications
You must be signed in to change notification settings - Fork 690
Fix INSERT INTO SELECT FROM parameter overwriting data with defaults #2210
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
efe76d6
Initial plan
Copilot e39269b
Add test to reproduce INSERT SELECT FROM ? issue with empty values
Copilot 718c191
Fix INSERT SELECT FROM ? inserting empty values by reversing extend o…
Copilot a1ccb86
Format code and remove test artifact file
Copilot 6b6ddc0
Use assert.deepEqual for complete object comparison in tests
Copilot 36db816
Remove unnecessary done() callbacks from synchronous tests
Copilot 1dc5642
Merge branch 'develop' into copilot/fix-insert-empty-records
mathiasrw 121c5c9
Merge branch 'develop' into copilot/fix-insert-empty-records
mathiasrw 59c237c
Remove console.log statements from test output
Copilot c2554ec
Merge branch 'develop' into copilot/fix-insert-empty-records
mathiasrw cff9610
Merge branch 'develop' into copilot/fix-insert-empty-records
mathiasrw 8b331c0
Rename test file from test_insert_from_param.js to test1484.js
Copilot 3059cfa
Merge branch 'develop' into copilot/fix-insert-empty-records
mathiasrw 032ae22
Fix default value application to only set missing properties
Copilot 553f5ec
Add comprehensive edge case tests for INSERT with defaults
Copilot 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,189 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 1484 - INSERT FROM parameter with defaults', function () { | ||
| it('1. INSERT INTO table SELECT * FROM ? with data array', () => { | ||
| // Create a new database like in the issue | ||
| var inmemDB = new alasql.Database('test_insert_db'); | ||
|
|
||
| // Create table | ||
| var query = | ||
| "CREATE TABLE IF NOT EXISTS [BCPartners] (RecordNum INTEGER PRIMARY KEY AUTOINCREMENT, [TABLE_NAME] TEXT DEFAULT '', [BC] TEXT DEFAULT '')"; | ||
| inmemDB.exec(query); | ||
|
|
||
| // Prepare test data | ||
| var data = [ | ||
| {TABLE_NAME: 'Table1', BC: 'BC1'}, | ||
| {TABLE_NAME: 'Table2', BC: 'BC2'}, | ||
| {TABLE_NAME: 'Table3', BC: 'BC3'}, | ||
| ]; | ||
|
|
||
| // Insert data | ||
| inmemDB.exec('INSERT INTO [BCPartners] SELECT * FROM ?', [data]); | ||
|
|
||
| // Verify data was inserted correctly | ||
| var result = inmemDB.exec('SELECT * FROM [BCPartners]'); | ||
|
|
||
| var expected = [ | ||
| {RecordNum: 1, TABLE_NAME: 'Table1', BC: 'BC1'}, | ||
| {RecordNum: 2, TABLE_NAME: 'Table2', BC: 'BC2'}, | ||
| {RecordNum: 3, TABLE_NAME: 'Table3', BC: 'BC3'}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| // Clean up | ||
| alasql('DROP DATABASE test_insert_db'); | ||
| }); | ||
|
|
||
| it('2. INSERT INTO table SELECT * FROM ? using default database', () => { | ||
| // Test with default database (alasql) | ||
| alasql('DROP TABLE IF EXISTS BCPartners'); | ||
| alasql( | ||
| "CREATE TABLE BCPartners (RecordNum INTEGER PRIMARY KEY AUTOINCREMENT, TABLE_NAME TEXT DEFAULT '', BC TEXT DEFAULT '')" | ||
| ); | ||
|
|
||
| // Prepare test data | ||
| var data = [ | ||
| {TABLE_NAME: 'Table1', BC: 'BC1'}, | ||
| {TABLE_NAME: 'Table2', BC: 'BC2'}, | ||
| ]; | ||
|
|
||
| // Insert data | ||
| alasql('INSERT INTO BCPartners SELECT * FROM ?', [data]); | ||
|
|
||
| // Verify data was inserted correctly | ||
| var result = alasql('SELECT * FROM BCPartners'); | ||
|
|
||
| var expected = [ | ||
| {RecordNum: 1, TABLE_NAME: 'Table1', BC: 'BC1'}, | ||
| {RecordNum: 2, TABLE_NAME: 'Table2', BC: 'BC2'}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| // Clean up | ||
| alasql('DROP TABLE BCPartners'); | ||
| }); | ||
|
|
||
| it('3. INSERT with partial columns - defaults should apply for missing columns', () => { | ||
| alasql('DROP TABLE IF EXISTS TestDefaults'); | ||
| alasql( | ||
| 'CREATE TABLE TestDefaults (id INT, name TEXT DEFAULT "NoName", status TEXT DEFAULT "Active")' | ||
| ); | ||
|
|
||
| // Insert data with only id and name | ||
| var data = [ | ||
| {id: 1, name: 'Item1'}, | ||
| {id: 2, name: 'Item2'}, | ||
| ]; | ||
| alasql('INSERT INTO TestDefaults SELECT * FROM ?', [data]); | ||
|
|
||
| var result = alasql('SELECT * FROM TestDefaults'); | ||
|
|
||
| // status should have default value | ||
| var expected = [ | ||
| {id: 1, name: 'Item1', status: 'Active'}, | ||
| {id: 2, name: 'Item2', status: 'Active'}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| alasql('DROP TABLE TestDefaults'); | ||
| }); | ||
|
|
||
| it('4. INSERT with empty source columns should preserve empty values not defaults', () => { | ||
| alasql('DROP TABLE IF EXISTS TestEmpty'); | ||
| alasql('CREATE TABLE TestEmpty (id INT, val TEXT DEFAULT "DefaultValue")'); | ||
|
|
||
| // Insert data with empty string (not undefined/missing) | ||
| var data = [ | ||
| {id: 1, val: ''}, | ||
| {id: 2, val: ''}, | ||
| ]; | ||
| alasql('INSERT INTO TestEmpty SELECT * FROM ?', [data]); | ||
|
|
||
| var result = alasql('SELECT * FROM TestEmpty'); | ||
|
|
||
| // Empty strings should be preserved, not replaced with defaults | ||
| var expected = [ | ||
| {id: 1, val: ''}, | ||
| {id: 2, val: ''}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| alasql('DROP TABLE TestEmpty'); | ||
| }); | ||
|
|
||
| it('5. INSERT from subquery with defaults', () => { | ||
| alasql('DROP TABLE IF EXISTS Source'); | ||
| alasql('DROP TABLE IF EXISTS TargetTable'); | ||
| alasql('CREATE TABLE Source (id INT, name TEXT)'); | ||
| alasql('CREATE TABLE TargetTable (id INT, name TEXT, timestamp TEXT DEFAULT "2025")'); | ||
|
|
||
| // Insert source data | ||
| alasql('INSERT INTO Source VALUES (1, "A"), (2, "B")'); | ||
|
|
||
| // Insert from source to target - timestamp should get default | ||
| alasql('INSERT INTO TargetTable SELECT * FROM Source'); | ||
|
|
||
| var result = alasql('SELECT * FROM TargetTable'); | ||
|
|
||
| var expected = [ | ||
| {id: 1, name: 'A', timestamp: '2025'}, | ||
| {id: 2, name: 'B', timestamp: '2025'}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| alasql('DROP TABLE Source'); | ||
| alasql('DROP TABLE TargetTable'); | ||
| }); | ||
|
|
||
| it('6. INSERT with mixed present and missing columns', () => { | ||
| alasql('DROP TABLE IF EXISTS MixedTest'); | ||
| alasql( | ||
| 'CREATE TABLE MixedTest (a INT, b TEXT DEFAULT "B_default", c TEXT DEFAULT "C_default", d TEXT DEFAULT "D_default")' | ||
| ); | ||
|
|
||
| // Some records have different columns | ||
| var data = [ | ||
| {a: 1, b: 'B1', c: 'C1'}, // missing d | ||
| {a: 2, c: 'C2', d: 'D2'}, // missing b | ||
| {a: 3, b: 'B3', d: 'D3'}, // missing c | ||
| ]; | ||
| alasql('INSERT INTO MixedTest SELECT * FROM ?', [data]); | ||
|
|
||
| var result = alasql('SELECT * FROM MixedTest'); | ||
|
|
||
| var expected = [ | ||
| {a: 1, b: 'B1', c: 'C1', d: 'D_default'}, | ||
| {a: 2, b: 'B_default', c: 'C2', d: 'D2'}, | ||
| {a: 3, b: 'B3', c: 'C_default', d: 'D3'}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| alasql('DROP TABLE MixedTest'); | ||
| }); | ||
|
|
||
| it('7. INSERT with undefined values should preserve undefined not apply defaults', () => { | ||
| alasql('DROP TABLE IF EXISTS UndefinedTest'); | ||
| alasql('CREATE TABLE UndefinedTest (id INT, val TEXT DEFAULT "DefaultVal")'); | ||
|
|
||
| // Explicitly set val to undefined | ||
| var data = [ | ||
| {id: 1, val: undefined}, | ||
| {id: 2, val: null}, | ||
| ]; | ||
| alasql('INSERT INTO UndefinedTest SELECT * FROM ?', [data]); | ||
|
|
||
| var result = alasql('SELECT * FROM UndefinedTest'); | ||
|
|
||
| // undefined and null should be preserved | ||
| var expected = [ | ||
| {id: 1, val: undefined}, | ||
| {id: 2, val: null}, | ||
| ]; | ||
| assert.deepEqual(result, expected); | ||
|
|
||
| alasql('DROP TABLE UndefinedTest'); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.