-
Notifications
You must be signed in to change notification settings - Fork 690
Implement column-type-aware CSV import: defer type conversion to INSERT based on table definitions #2403
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
Draft
Copilot
wants to merge
8
commits into
develop
Choose a base branch
from
copilot/fix-wrong-datatype-from-csv
base: develop
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.
Draft
Implement column-type-aware CSV import: defer type conversion to INSERT based on table definitions #2403
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5411079
Initial plan
Copilot 8ae503a
Add csvStringToNumber option to control CSV type conversion
Copilot 1757841
Refactor tests to use assert.deepEqual for complete object validation
Copilot 05448ca
Co-authored-by: mathiasrw <[email protected]>
Copilot 588303a
Implement column-type-aware conversion: keep CSV as strings, convert …
Copilot 06abb2d
Refactor: simplify type converter with regex patterns, streamline SEL…
Copilot 95707b5
Remove individual property assertions, keep only assert.deepEqual for…
Copilot 689a7ca
Refactor type converter: simplify int regex, remove trim, use regex f…
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1347,5 +1347,62 @@ var getXLSX = function () { | |
| return XLSX; | ||
| }; | ||
|
|
||
| /** | ||
| * Type converter regex patterns | ||
| */ | ||
| var reTypeConverter = { | ||
| str: /string|char$|text/i, | ||
| int: /int$/i, | ||
| num: /float|double|real|^num|decimal|money/i, | ||
| bool: /^bool/i, | ||
| date: /^date|^time/i, | ||
| }; | ||
|
|
||
| /** | ||
| * Convert a value to the appropriate type based on column definition | ||
| * @param {*} value - The value to convert | ||
| * @param {string} dbtypeid - The database type (INT, FLOAT, STRING, etc.) | ||
| * @return {*} The converted value | ||
| */ | ||
| utils.convertValueToType = function (value, dbtypeid) { | ||
|
Member
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. please rename to "typeConverter" so its aligned with reTypeConverter |
||
| // If value is null or undefined, return as is | ||
| if (value === null || value === undefined) { | ||
| return value; | ||
| } | ||
|
|
||
| // If no type specified, try to auto-convert if it looks like a number | ||
| if (!dbtypeid) { | ||
| if (alasql.options.csvStringToNumber && typeof value === 'string' && value.length > 0) { | ||
| if (value == +value) { | ||
| return +value; | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| // Check type using regex patterns | ||
| if (reTypeConverter.str.test(dbtypeid)) { | ||
| return String(value); | ||
| } | ||
| if (reTypeConverter.int.test(dbtypeid)) { | ||
| return parseInt(value, 10); | ||
| } | ||
| if (reTypeConverter.num.test(dbtypeid)) { | ||
| return parseFloat(value); | ||
| } | ||
| if (reTypeConverter.bool.test(dbtypeid)) { | ||
| if (typeof value === 'string') { | ||
| return /^(true|1|yes)$/i.test(value); | ||
| } | ||
| return Boolean(value); | ||
| } | ||
| if (reTypeConverter.date.test(dbtypeid)) { | ||
| return value instanceof Date ? value : new Date(value); | ||
| } | ||
|
|
||
| // Unknown type, return as is | ||
| return value; | ||
| }; | ||
|
|
||
| // set AlaSQl path | ||
| alasql.path = alasql.utils.findAlaSQLPath(); | ||
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
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
|
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
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
|
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,178 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| var fs = require('fs'); | ||
| } | ||
|
|
||
| describe('Test CSV string type preservation and column type conversion', function () { | ||
| const test = 'csvstringtype'; | ||
|
|
||
| before(function () { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(function () { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) CSV parser always keeps values as strings', function () { | ||
| var csvData = '"117.20";"500"\n"88.33";"600"'; | ||
| var res = alasql('SELECT * FROM CSV(?, {separator:";", headers:false})', [csvData]); | ||
| assert.deepEqual(res, [ | ||
| {0: '117.20', 1: '500'}, | ||
| {0: '88.33', 1: '600'}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('B) STRING type - preserves string values', function () { | ||
| alasql('CREATE TABLE test_string (id STRING, name STRING)'); | ||
| var csvData = '"id";"name"\n"117.20";"test"\n"88.33";"item"'; | ||
| alasql('SELECT * INTO test_string FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_string'); | ||
| assert.deepEqual(res, [ | ||
| {id: '117.20', name: 'test'}, | ||
| {id: '88.33', name: 'item'}, | ||
| ]); | ||
| alasql('DROP TABLE test_string'); | ||
| }); | ||
|
|
||
| it('C) STRING type - converts numbers to strings', function () { | ||
| alasql('CREATE TABLE test_str_num (code STRING, qty STRING)'); | ||
| var csvData = '"code";"qty"\n"123";"456"'; | ||
| alasql('SELECT * INTO test_str_num FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_str_num'); | ||
| assert.deepEqual(res, [{code: '123', qty: '456'}]); | ||
| alasql('DROP TABLE test_str_num'); | ||
| }); | ||
|
|
||
| it('D) INT type - converts strings to integers', function () { | ||
| alasql('CREATE TABLE test_int (id INT, qty INT)'); | ||
| var csvData = '"id";"qty"\n"123";"456"'; | ||
| alasql('SELECT * INTO test_int FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_int'); | ||
| assert.deepEqual(res, [{id: 123, qty: 456}]); | ||
| alasql('DROP TABLE test_int'); | ||
| }); | ||
|
|
||
| it('E) INT type - truncates decimal values', function () { | ||
| alasql('CREATE TABLE test_int_dec (amount INT)'); | ||
| var csvData = '"amount"\n"99.99"\n"123.45"'; | ||
| alasql('SELECT * INTO test_int_dec FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_int_dec'); | ||
| assert.deepEqual(res, [{amount: 99}, {amount: 123}]); | ||
| alasql('DROP TABLE test_int_dec'); | ||
| }); | ||
|
|
||
| it('F) FLOAT type - preserves decimal precision', function () { | ||
| alasql('CREATE TABLE test_float (price FLOAT, cost FLOAT)'); | ||
| var csvData = '"price";"cost"\n"99.99";"123.45"'; | ||
| alasql('SELECT * INTO test_float FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_float'); | ||
| assert.deepEqual(res, [{price: 99.99, cost: 123.45}]); | ||
| alasql('DROP TABLE test_float'); | ||
| }); | ||
|
|
||
| it('G) FLOAT type - converts string decimals to numbers', function () { | ||
| alasql('CREATE TABLE test_float_str (amount FLOAT)'); | ||
| var csvData = '"amount"\n"117.20"\n"88.33"'; | ||
| alasql('SELECT * INTO test_float_str FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_float_str'); | ||
| assert.deepEqual(res, [{amount: 117.2}, {amount: 88.33}]); | ||
| alasql('DROP TABLE test_float_str'); | ||
| }); | ||
|
|
||
| it('H) BOOLEAN type - converts string true/false', function () { | ||
| alasql('CREATE TABLE test_bool (active BOOLEAN, enabled BOOLEAN)'); | ||
| var csvData = '"active";"enabled"\n"true";"false"\n"1";"0"'; | ||
| alasql('SELECT * INTO test_bool FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_bool'); | ||
| assert.deepEqual(res, [ | ||
| {active: true, enabled: false}, | ||
| {active: true, enabled: false}, | ||
| ]); | ||
| alasql('DROP TABLE test_bool'); | ||
| }); | ||
|
|
||
| it('I) BOOLEAN type - handles yes/no strings', function () { | ||
| alasql('CREATE TABLE test_bool_yn (flag BOOLEAN)'); | ||
| var csvData = '"flag"\n"yes"\n"no"'; | ||
| alasql('SELECT * INTO test_bool_yn FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_bool_yn'); | ||
| assert.deepEqual(res, [{flag: true}, {flag: false}]); | ||
| alasql('DROP TABLE test_bool_yn'); | ||
| }); | ||
|
|
||
| it('J) DATE type - converts string dates', function () { | ||
| alasql('CREATE TABLE test_date (created DATE)'); | ||
| var csvData = '"created"\n"2023-01-15"'; | ||
| alasql('SELECT * INTO test_date FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_date'); | ||
| assert.deepEqual(res, [{created: new Date('2023-01-15')}]); | ||
| alasql('DROP TABLE test_date'); | ||
| }); | ||
|
|
||
| it('K) Mixed types - all conversions work together', function () { | ||
| alasql('CREATE TABLE test_mixed (id STRING, qty INT, price FLOAT, active BOOLEAN)'); | ||
| var csvData = '"id";"qty";"price";"active"\n"117.20";"10";"99.99";"true"'; | ||
| alasql('SELECT * INTO test_mixed FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_mixed'); | ||
| assert.deepEqual(res, [{id: '117.20', qty: 10, price: 99.99, active: true}]); | ||
| alasql('DROP TABLE test_mixed'); | ||
| }); | ||
|
|
||
| it('L) No column definitions with csvStringToNumber=true - auto-converts', function () { | ||
| alasql.options.csvStringToNumber = true; | ||
| alasql('CREATE TABLE test_nodef'); | ||
| var csvData = '"id";"amount"\n"117.20";"500"'; | ||
| alasql('SELECT * INTO test_nodef FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_nodef'); | ||
| assert.deepEqual(res, [{id: 117.2, amount: 500}]); | ||
| alasql('DROP TABLE test_nodef'); | ||
| }); | ||
|
|
||
| it('M) No column definitions with csvStringToNumber=false - preserves strings', function () { | ||
| alasql.options.csvStringToNumber = false; | ||
| alasql('CREATE TABLE test_nodef2'); | ||
| var csvData = '"id";"amount"\n"117.20";"500"'; | ||
| alasql('SELECT * INTO test_nodef2 FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_nodef2'); | ||
| assert.deepEqual(res, [{id: '117.20', amount: '500'}]); | ||
| alasql('DROP TABLE test_nodef2'); | ||
| alasql.options.csvStringToNumber = true; // Restore default | ||
| }); | ||
|
|
||
| it('N) VARCHAR and CHAR types work like STRING', function () { | ||
| alasql('CREATE TABLE test_varchar (name VARCHAR, code CHAR)'); | ||
| var csvData = '"name";"code"\n"123.45";"ABC"'; | ||
| alasql('SELECT * INTO test_varchar FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_varchar'); | ||
| assert.deepEqual(res, [{name: '123.45', code: 'ABC'}]); | ||
| alasql('DROP TABLE test_varchar'); | ||
| }); | ||
|
|
||
| it('O) NULL and undefined values are preserved', function () { | ||
| alasql('CREATE TABLE test_nulls (id STRING, qty INT)'); | ||
| var csvData = '"id";"qty"\n"A001";""'; | ||
| alasql('SELECT * INTO test_nulls FROM CSV(?, {separator:";"})', [csvData]); | ||
| var res = alasql('SELECT * FROM test_nulls'); | ||
| // Empty string in CSV becomes empty string, then parseInt returns NaN | ||
| assert.deepEqual(res, [{id: 'A001', qty: NaN}]); | ||
| alasql('DROP TABLE test_nulls'); | ||
| }); | ||
|
|
||
| it('P) TSV also respects column types', function () { | ||
| alasql('CREATE TABLE test_tsv (id STRING, amount INT)'); | ||
| var tsvData = 'id\tamount\n117.20\t500'; | ||
| alasql('SELECT * INTO test_tsv FROM TSV(?)', [tsvData]); | ||
| var res = alasql('SELECT * FROM test_tsv'); | ||
| assert.deepEqual(res, [{id: '117.20', amount: 500}]); | ||
| alasql('DROP TABLE test_tsv'); | ||
| }); | ||
|
|
||
| it('Q) Direct SELECT from CSV without INSERT returns strings', function () { | ||
| var csvData = '"id";"name"\n"117.20";"test"'; | ||
| var res = alasql('SELECT * FROM CSV(?, {separator:";"})', [csvData]); | ||
| assert.deepEqual(res, [{id: '117.20', name: 'test'}]); | ||
| }); | ||
| }); |
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.
What about
INTEGER? It needs.^intaswell