AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.01k stars 653 forks source link

JOIN throws error #1043

Open wittenator opened 6 years ago

wittenator commented 6 years ago

Hi,

I am currently working on an application which should crossreference a bunch of CSV files. For that I thought of using Alasql. I browsed the repo a bit and found the discussion concerning the broken Sub-SELECT. Therefore I implemented my loading logic like that:

async load() {
        try {
            const projects = await alasql.promise(`
            WITH inst AS (
                SELECT * FROM CSV("${path.join(__dirname, this.paths.in["institutions"])}")
            )
            SELECT proj.institution_id AS instid FROM CSV("${path.join(__dirname, this.paths.in["projects"])}") AS proj
            JOIN inst 
            ON proj.institution_id = inst.institution_id`)
                    console.log(projects);  
        }
        catch(reason) {
            console.log(reason)
        }
    }

But for some reason I get the following error:

/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:7276
                var ilen = source.data.length;
                                       ^

TypeError: Cannot read property 'length' of undefined
    at preIndex (/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:7276:28)
    at queryfn3 (/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:6930:2)
    at queryfn2 (/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:6921:9)
    at parseText (/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:17563:10)
    at /home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:3485:7
    at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)

I tried the SELECTs without a JOIN separately and they are working. Afterwards I had a look at the Alasql code(especially since the alasql fs functions seem to have a problem), but couldn't find an exact reason for that error. Did anyone face similar troubles?

mathiasrw commented 6 years ago

Looks like a bug. Have you tried something like

a = alasql('SELECT * FROM CSV(a)')
b = alasql('SELECT * FROM CSV(b)')
result = alasql(`
            SELECT proj.institution_id AS instid FROM ? AS proj
            JOIN ? as inst 
            ON proj.institution_id = inst.institution_id
        `, [a,b])

?

wittenator commented 6 years ago

I played a bit around and apparently the version with the separated subqueries works. At the same time, if I change alasql.promise('...') to alasql(['...']) (which should be equivalent, right?) I get the following error:

ReferenceError: HTMLElement is not defined
    at alasql (/home/tim/HCC/IKON-backend/node_modules/alasql/dist/alasql.fs.js:130:54)
mathiasrw commented 6 years ago

Ohh - that is very bad! Any chance you could share the full code generating the problem?

wittenator commented 6 years ago

Sure, luckily my project is open-source. I got the problem with both Node.js 8 and 10. https://github.com/FUB-HCC/IKON-backend/blob/master/src/apiserver/Dataloader/dataloader.js

Sadly the project uses an external API, so I can't commit everything to the repo. If you want to test it, you'll need an Google Maps API key for geocoding. (I'll send you the config in this case.) If I can help somehow, just tell me!