Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Summary

Applied maintainer feedback to further optimize the type converter utility.

Changes Made

Type Converter Optimizations (src/15utility.js)

  1. Simplified int regex (Line 1355)

    • Before: /^int|int$|^smallint$|^bigint$|^tinyint$/i
    • After: /int$/i
    • All int types (smallint, bigint, tinyint) are already covered by int$ pattern
  2. Removed trim() call (Line 1376)

    • Before: var trimmed = value.trim(); if (trimmed == +trimmed)
    • After: if (value == +value)
    • Direct comparison without unnecessary trim operation
  3. Boolean conversion using regex (Lines 1396-1397)

    • Before: var lower = value.toLowerCase(); return lower === 'true' || lower === '1' || lower === 'yes';
    • After: return /^(true|1|yes)$/i.test(value);
    • More efficient single regex test instead of toLowerCase + multiple comparisons

Testing Results

  • ✅ All 17 CSV string type tests pass
  • ✅ All 47 CSV-related tests pass
  • ✅ No regressions detected
  • ✅ Code is cleaner and more efficient
Original prompt

This section details on the original issue you should resolve

<issue_title>Wrong datatype after reading FROM CSV </issue_title>
<issue_description>I am very enthusiastic about alasql and it was already a great help to me. Thanks a lot for the great work.
But...

Datatype switches from string to number after FROM CSV import.

CSV sample:
"117.20";"some name"
"88.33";"other name"

SQL:
CREATE TABLE test (id string,name string);
SELECT * INTO test FROM CSV('some csv file');
SELECT * FROM test;

results in:

id name
117.2 some name
88.33 other name

The column 'ID' datatype changed to number and the value is handled as float and trailing Null is cut off.
Bad if this is an identifier.

PS: it seems to work with FROM XLSX, when column is formated as TEXT in Excel
</issue_description>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Hi @casavipiestrick - thank you for the kind words.

Good point. Nice little bug there :)</comment_new>
<comment_new>@mathiasrw
The conversion of numberstrings into numbers has been part of Alasql too long to change. I am happy to let a config flag change how it works.

@mathiasrw Any PR is welcome to fix this - but the fix must be provide in a way so you need to set something like alasql.config.voidstr2intCSV (preferably a better name) to have that new functionality.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong datatype issue after reading from CSV Add csvStringToNumber option to control automatic string-to-number conversion in CSV imports Dec 22, 2025
Copilot AI requested a review from mathiasrw December 22, 2025 02:55
@mathiasrw
Copy link
Member

I recommend we keep csv data as string until we are inserting them.

If there are col definitions, then we adhire to that (also with things like dates, int, string, float, varchar)

If there are no definitions we behave like now when we do auto convert if we detect numbers (for example into tables provideda s reference data)

@mathiasrw
Copy link
Member

@copilot

…at INSERT based on table definitions

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 22, 2025

I recommend we keep csv data as string until we are inserting them.

If there are col definitions, then we adhire to that (also with things like dates, int, string, float, varchar)
...

Implemented as requested. CSV data now stays as strings until INSERT. Type conversion happens based on column definitions (STRING, INT, FLOAT, etc.) when available. Without definitions, the csvStringToNumber option controls auto-conversion for backward compatibility. Commit: 588303a

Copilot AI changed the title Add csvStringToNumber option to control automatic string-to-number conversion in CSV imports Implement column-type-aware CSV import: defer type conversion to INSERT based on table definitions Dec 22, 2025
…ECT INTO, add comprehensive edge case tests

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
… complete object validation

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI requested a review from mathiasrw December 22, 2025 15:45
src/15utility.js Outdated
*/
var reTypeConverter = {
str: /string|char$|text/i,
int: /^int|int$|^smallint$|^bigint$|^tinyint$/i,
Copy link
Member

Choose a reason for hiding this comment

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

Small int, big int, tinyint are all coverd by int$

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified int regex to /int$/i which covers all int types. Commit: 689a7ca

src/15utility.js Outdated
// 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) {
var trimmed = value.trim();
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid trim here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed trim() call, now using direct comparison value == +value. Commit: 689a7ca

src/15utility.js Outdated
Comment on lines 1396 to 1397
var lower = value.toLowerCase();
return lower === 'true' || lower === '1' || lower === 'yes';
Copy link
Member

Choose a reason for hiding this comment

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

Can we please use a regex here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced toLowerCase + string comparisons with single regex: /^(true|1|yes)$/i.test(value). Commit: 689a7ca

…or boolean matching

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
*/
var reTypeConverter = {
str: /string|char$|text/i,
int: /int$/i,
Copy link
Member

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. ^int aswell

* @param {string} dbtypeid - The database type (INT, FLOAT, STRING, etc.)
* @return {*} The converted value
*/
utils.convertValueToType = function (value, dbtypeid) {
Copy link
Member

Choose a reason for hiding this comment

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

please rename to "typeConverter" so its aligned with reTypeConverter

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

Successfully merging this pull request may close these issues.

Wrong datatype after reading FROM CSV

2 participants