CSVQL is a custom query language designed to allow users to write SQL-like queries to interact with CSV files. This syntax documentation provides detailed descriptions of the commands and expressions that can be used in CSVQL.
The IMPORT statement is used to load a CSV file into the query context. This is the first command that must be executed in any CSVQL script.
IMPORT [variable_name] FROM '[file_path]'variable_name: The name of the variable that will reference the loaded CSV file.file_path: The path to the CSV file (relative or absolute).
IMPORT file FROM 'files/machine-readable-business-employment-data-mar-2024-quarter.csv'In this example, the file machine-readable-business-employment-data-mar-2024-quarter.csv is loaded and referenced as file.
The SELECT statement is used to retrieve data from the CSV file. You can select all columns or specific columns from the file. The SELECT statement can optionally include a WHERE clause to filter rows.
SELECT [columns] FROM [variable_name] [WHERE condition]columns: The columns you want to select. Use*to select all columns.variable_name: The name of the variable that references the imported CSV file.WHERE condition(Optional): A condition to filter rows based on column values.
SELECT * FROM fileThis query selects all columns from the file.
SELECT Data_value FROM fileThis query selects the Data_value column from the file.
The WHERE clause is used to filter the rows returned by the SELECT statement. The clause specifies a condition that each row must meet to be included in the result.
WHERE [column] [operator] [value]column: The name of the column to filter on.operator: The comparison operator (e.g.,=,!=,<,>,<=,>=).value: The value to compare the column against. This can be a string, number, or boolean value.
=: Equal to.!=: Not equal to.<: Less than.>: Greater than.<=: Less than or equal to.>=: Greater than or equal to.AND: Logical AND (converted to&&in JavaScript).OR: Logical OR (converted to||in JavaScript).
SELECT * FROM file WHERE Data_value = '80078' AND Period = '2011.06'This query selects all columns from file where the Data_value column equals '80078' and the Period column equals '2011.06'.
Currently, the syntax supports basic logical operators like AND and OR in the WHERE clause to combine multiple conditions.
WHERE [condition1] AND/OR [condition2]SELECT * FROM file WHERE Data_value = '80078' AND Year = '2024'This query selects all columns from file where the Data_value is '80078' and the Year is '2024'.
You can use parentheses in the WHERE clause to group conditions and control the order of evaluation.
WHERE ([condition1] AND [condition2]) OR [condition3]SELECT * FROM file WHERE (Data_value = '80078' AND Year = '2024') OR Region = 'North'This query selects all columns from file where the Data_value is '80078' and the Year is '2024', or the Region is 'North'.
Here's a complete example of a CSVQL script:
IMPORT file FROM 'files/machine-readable-business-employment-data-mar-2024-quarter.csv'
SELECT * FROM file WHERE Data_value = '80078' AND Year = '2024'- IMPORT: Loads the CSV file into the
filevariable. - SELECT: Retrieves all columns from the
file. - WHERE: Filters the rows to only include those where
Data_valueequals'80078'andYearequals'2024'.
- Syntax Error: Ensure that the syntax follows the correct format. For example, missing quotation marks around the file path or incorrect use of operators can cause syntax errors.
- Unsupported Operators: Ensure that you are using supported comparison operators in the
WHEREclause. - Unknown Variable: Make sure that the variable name in the
SELECTstatement matches the name defined in theIMPORTstatement.
Error: Unsupported condition node type: WHERE_CLAUSEThis error indicates that the parser encountered an unsupported or incorrectly structured condition in the WHERE clause. Check the syntax and ensure that the condition is valid.
If you want to extend CSVQL to support more features, such as additional SQL-like commands (JOIN, ORDER BY, etc.), you can modify the parser to include new AST node types and extend the code generation logic accordingly.