mechatroner / RBQL

🦜RBQL - Rainbow Query Language: SQL-like query engine for (not only) CSV file processing. Supports SQL queries with Python and JavaScript expressions.
https://rbql.org
MIT License
281 stars 13 forks source link

rbql.query_table with headers #42

Closed WhiteG00se closed 1 year ago

WhiteG00se commented 1 year ago

Hello there,

so when using the VS Code extension to query a csv file, it works flawless if the .csv has headers. When trying to query a 2D array in node.js, it's not going so well.

As far as I understood the docu on npm, I just need to add "WITH (header)" to the end of my query. Or does that only work with rbql.query_csv? Or do I perhaps need to pass it as an argument somehow? Is there maybe an alternative where I can query an array of objects instead of a 2D array (then the "headers" would just be the keys and not cause issues)?

Here's a code snippet where I use it (applyMappings turns the header (e.g. "hours") into columns (e.g. "a6")):

const query = "SELECT project, psp_title, psp_id, SUM(hours) WHERE date GROUP BY psp_id WITH (header)"
const mappedQuery = this.applyMappings(query)
let output_table: unknown[] = []
let warnings: string[] = []
await rbql.query_table(mappedQuery, input_table, output_table, warnings)

This keeps giving me "Error: At record 1, Details: Unable to convert value "hours" to a number." Well yes, the column with the hours has a string in the first row, because that's the header, and all the other values are numbers.

Is there an error on my end, or does it just not have this functionality?

BR Tobi

mechatroner commented 1 year ago

Hello, query_table function has input_column_names parameter which you can use to pass the column names: https://www.npmjs.com/package/rbql#rbqlquery_table

WhiteG00se commented 1 year ago

Thank you for the suggestion, this removed my need for regex to map the query.

So to my understanding it's not possible to have a 2D array that stores table header + table body. So instead I made my table an object that stores string array for the headers and the 2D array for the body. I suppose that's cleaner anyway. But it would have been less code had I been able to keep the header in the 2D array.