tdoehmen commited on
Commit
fa73083
·
verified ·
1 Parent(s): 6476259

Update duckdb-nsql/eval/prompt_formatters.py

Browse files
Files changed (1) hide show
  1. duckdb-nsql/eval/prompt_formatters.py +145 -0
duckdb-nsql/eval/prompt_formatters.py CHANGED
@@ -385,6 +385,151 @@ Here is the question or an instruction the user provided:
385
 
386
  Write a DuckDB SQL query for the given question!
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  Answer:
389
  ```
390
  """
 
385
 
386
  Write a DuckDB SQL query for the given question!
387
 
388
+ Answer:
389
+ ```
390
+ """
391
+
392
+ @classmethod
393
+ def format_retrieved_context(
394
+ cls,
395
+ context: list[str],
396
+ ) -> str:
397
+ """Format retrieved context."""
398
+ context_str = "\n--------\n".join(context)
399
+ return f"\n### Documentation:\n{context_str}\n"
400
+
401
+ @classmethod
402
+ def format_prompt(
403
+ cls,
404
+ instruction: str,
405
+ table_text: str,
406
+ context_text: str,
407
+ ) -> str | list[str]:
408
+ """Get prompt format."""
409
+ instruction = cls.PROMPT_TEMPLATE.format(
410
+ schema=table_text,
411
+ question=instruction
412
+ )
413
+ return instruction
414
+
415
+ class DuckDBInstFormatterGraniteBench(RajkumarFormatter):
416
+ """DuckDB Inst class."""
417
+
418
+ PROMPT_TEMPLATE = """System:
419
+ Your task is to generate valid DuckDB SQL to answer the question that the user asks. You should only respond with a valid DuckDB SQL query.
420
+
421
+ Here are some DuckDB SQL syntax specifics you should be aware of:
422
+
423
+ - DuckDB use double quotes (") for identifiers that contain spaces or special characters, or to force case-sensitivity and single quotes (') to define string literals
424
+ - DuckDB can query CSV, Parquet, and JSON directly without loading them first, e.g. `SELECT * FROM 'data.csv';`
425
+ - DuckDB supports CREATE TABLE AS (CTAS): `CREATE TABLE new_table AS SELECT * FROM old_table;`
426
+ - DuckDB queries can start with FROM, and optionally omit SELECT *, e.g. `FROM my_table WHERE condition;` is equivalent to `SELECT * FROM my_table WHERE condition;`
427
+ - DuckDB allows you to use SELECT without a FROM clause to generate a single row of results or to work with expressions directly, e.g. `SELECT 1 + 1 AS result;`
428
+ - DuckDB supports attaching multiple databases, unsing the ATTACH statement: `ATTACH 'my_database.duckdb' AS mydb;`. Tables within attached databases can be accessed using the dot notation (.), e.g. `SELECT * FROM mydb.table_name syntax`. The default databases doesn't require the do notation to access tables. The default database can be changed with the USE statement, e.g. `USE my_db;`.
429
+ - DuckDB is generally more lenient with implicit type conversions (e.g. `SELECT '42' + 1;` - Implicit cast, result is 43), but you can always be explicit using `::`, e.g. `SELECT '42'::INTEGER + 1;`
430
+ - DuckDB can extract parts of strings and lists using [start:end] or [start:end:step] syntax. Indexes start at 1. String slicing: `SELECT 'DuckDB'[1:4];`. Array/List slicing: `SELECT [1, 2, 3, 4][1:3];`
431
+ - DuckDB has a powerful way to select or transform multiple columns using patterns or functions. You can select columns matching a pattern: `SELECT COLUMNS('sales_.*') FROM sales_data;` or transform multiple columns with a function: `SELECT AVG(COLUMNS('sales_.*')) FROM sales_data;`
432
+ - DuckDB an easy way to include/exclude or modify columns when selecting all: e.g. Exclude: `SELECT * EXCLUDE (sensitive_data) FROM users;` Replace: `SELECT * REPLACE (UPPER(name) AS name) FROM users;`
433
+ - DuckDB has a shorthand for grouping/ordering by all non-aggregated/all columns. e.g `SELECT category, SUM(sales) FROM sales_data GROUP BY ALL;` and `SELECT * FROM my_table ORDER BY ALL;`
434
+ - DuckDB can combine tables by matching column names, not just their positions using UNION BY NAME. E.g. `SELECT * FROM table1 UNION BY NAME SELECT * FROM table2;`
435
+ - DuckDB has an inutitive syntax to create List/Struct/Map and Array types. Create complex types using intuitive syntax. List: `SELECT [1, 2, 3] AS my_list;`, Struct: `{{'a': 1, 'b': 'text'}} AS my_struct;`, Map: `MAP([1,2],['one','two']) as my_map;`. All types can also be nested into each other. Array types are fixed size, while list types have variable size. Compared to Structs, MAPs do not need to have the same keys present for each row, but keys can only be of type Integer or Varchar. Example: `CREATE TABLE example (my_list INTEGER[], my_struct STRUCT(a INTEGER, b TEXT), my_map MAP(INTEGER, VARCHAR), my_array INTEGER[3], my_nested_struct STRUCT(a INTEGER, b Integer[3]));`
436
+ - DuckDB has an inutive syntax to access struct fields using dot notation (.) or brackets ([]) with the field name. Maps fields can be accessed by brackets ([]).
437
+ - DuckDB's way of converting between text and timestamps, and extract date parts. Current date as 'YYYY-MM-DD': `SELECT strftime(NOW(), '%Y-%m-%d');` String to timestamp: `SELECT strptime('2023-07-23', '%Y-%m-%d')::TIMESTAMP;`, Extract Year from date: `SELECT EXTRACT(YEAR FROM DATE '2023-07-23');`
438
+ - Column Aliases in WHERE/GROUP BY/HAVING: You can use column aliases defined in the SELECT clause within the WHERE, GROUP BY, and HAVING clauses. E.g.: `SELECT a + b AS total FROM my_table WHERE total > 10 GROUP BY total HAVING total < 20;`
439
+ - DuckDB allows generating lists using expressions similar to Python list comprehensions. E.g. `SELECT [x*2 FOR x IN [1, 2, 3]];` Returns [2, 4, 6].
440
+ - DuckDB allows chaining multiple function calls together using the dot (.) operator. E.g.: `SELECT 'DuckDB'.replace('Duck', 'Goose').upper(); -- Returns 'GOOSEDB';`
441
+ - DuckDB has a JSON data type. It supports selecting fields from the JSON with a JSON-Path expression using the arrow operator, -> (returns JSON) or ->> (returns text) with JSONPath expressions. For example: `SELECT data->'$.user.id' AS user_id, data->>'$.event_type' AS event_type FROM events;`
442
+ - DuckDB has built-in functions for regex regexp_matches(column, regex), regexp_replace(column, regex), and regexp_extract(column, regex).
443
+ - DuckDB has a way to quickly get a subset of your data with `SELECT * FROM large_table USING SAMPLE 10%;`
444
+
445
+ DuckDB Functions:
446
+ `count`: Calculates the total number of rows returned by a SQL query result. This function is commonly used to determine the row count of a SELECT operation., Parameters: ['result: The result object']
447
+ `sum`: Calculates the total of all non-null values in a specified column or expression across rows., Parameters: ['arg: Values to be aggregated']
448
+ `max`: Returns the largest value from all values in a specified column or expression., Parameters: ['arg: expression to evaluate maximum', "n: top 'n' value list size(optional)"]
449
+ `min`: Finds the smallest value in a group of input values., Parameters: ['expression: The input value to consider']
450
+ `avg`: Calculates the average of non-null values., Parameters: ['arg: Data to be averaged']
451
+ `read_csv_auto`: Automatically reads a CSV file and infers the data types of its columns., Parameters: ['file_path: Path to the CSV file', 'MD_RUN: Execution control parameter(optional)']
452
+ `replace`: Replacement scans in DuckDB allow users to register a callback that gets triggered when a query references a non-existent table. The callback can replace this table with a custom table function, effectively 'replacing' the non-existent table in the query execution process., Parameters: ['db: Database object where replacement applies', 'replacement: Handler for when table is missing', 'extra_data: Extra data given to callback(optional)', 'delete_callback: Cleanup for extra data provided(optional)']
453
+ `length`: Returns the length of a string, Parameters: ['value: String to measure length of']
454
+ `read_json_auto`: Automatically infers the schema from JSON data and reads it into a table format., Parameters: ['filename: Path to the JSON file.', 'compression: File compression type.(optional)', 'auto_detect: Auto-detect key names/types.(optional)', 'columns: Manual specification of keys/types.(optional)', 'dateformat: Date format for parsing dates.(optional)', 'format: JSON file format.(optional)', 'hive_partitioning: Hive partitioned path interpretation.(optional)', 'ignore_errors: Ignore parse errors option.(optional)', 'maximum_depth: Max depth for schema detection.(optional)', 'maximum_object_size: Max size of JSON object.(optional)', 'records: JSON record unpacking option.(optional)', 'sample_size: Number of objects for sampling.(optional)', 'timestampformat: Timestamp parsing format.(optional)', 'union_by_name: Unify schemas of files.(optional)']
455
+ `regexp_extract`: If a string matches a given regular expression pattern, it returns the specified capturing group or groups with optional capture group names., Parameters: ['string: Input string to search in.', 'pattern: Regex pattern to match.', 'group: Specifies which group to capture.(optional)', 'name_list: Named capture groups struct.(optional)', 'options: Regex matching options.(optional)']
456
+ `upper`: Converts a given string to uppercase characters., Parameters: ['string: String to make uppercase']
457
+ `substring`: Extracts a substring from a given string starting at a specified position and with a specified length., Parameters: ['string: The original string to extract from', 'start: Starting position for extraction', 'length: Number of characters to extract']
458
+ `datediff`: Calculates the number of specified partition boundaries between two dates., Parameters: ['part: Time unit to measure', 'startdate: The starting date', 'enddate: The ending date']
459
+ `split_part`: Splits a string by a specified separator and returns the part at a given index., Parameters: ['string: The string to be split', 'separator: The delimiter to split by', 'index: 1-based index to retrieve']
460
+ `position`: Locates the position of the first occurrence of "search_string" after position 1 in the provided "string". It returns 0 if "search_string" is not found., Parameters: ['search_string: The substring to find.', 'string: The string to search in.']
461
+ `pragma_version`: Retrieves the current version of DuckDB., Parameters: []
462
+ `ascii`: Returns the Unicode code point of the first character of a given string., Parameters: ['string: Input string for conversion.']
463
+
464
+ DuckDB Statements:
465
+ `FROM`: The FROM clause specifies the source of the data for the query. It can include a single table, multiple joined tables, or subqueries. The JOIN clause is used to combine rows from two or more tables based on a related column between them. There are several types of joins, including INNER, OUTER, CROSS, NATURAL, SEMI, ANTI, LATERAL, POSITIONAL, ASOF, and self-joins., Examples: ['SELECT * FROM table_name;', 'FROM table_name SELECT *;', 'FROM table_name;', 'SELECT tn.* FROM table_name tn;', 'SELECT * FROM schema_name.table_name;', 'SELECT t.i FROM range(100) AS t(i);', "SELECT * FROM 'test.csv';", 'SELECT * FROM (SELECT * FROM table_name);', 'SELECT t FROM t;', "SELECT t FROM (SELECT unnest(generate_series(41, 43)) AS x, 'hello' AS y) t;", 'SELECT * FROM table_name JOIN other_table ON table_name.key = other_table.key;', 'SELECT * FROM table_name TABLESAMPLE 10%;', 'SELECT * FROM table_name TABLESAMPLE 10 ROWS;', 'FROM range(100) AS t(i) SELECT sum(t.i) WHERE i % 2 = 0;', 'SELECT a.*, b.* FROM a CROSS JOIN b;', 'SELECT a.*, b.* FROM a, b;', 'SELECT n.*, r.* FROM l_nations n JOIN l_regions r ON (n_regionkey = r_regionkey);', 'SELECT * FROM city_airport NATURAL JOIN airport_names;', 'SELECT * FROM city_airport JOIN airport_names USING (iata);', 'SELECT * FROM city_airport SEMI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata IN (SELECT iata FROM airport_names);', 'SELECT * FROM city_airport ANTI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata NOT IN (SELECT iata FROM airport_names WHERE iata IS NOT NULL);', 'SELECT * FROM range(3) t(i), LATERAL (SELECT i + 1) t2(j);', 'SELECT * FROM generate_series(0, 1) t(i), LATERAL (SELECT i + 10 UNION ALL SELECT i + 100) t2(j);', 'SELECT * FROM trades t ASOF JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF LEFT JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF JOIN prices p USING (symbol, "when");', 'SELECT t.symbol, t.when AS trade_when, p.when AS price_when, price FROM trades t ASOF LEFT JOIN prices p USING (symbol, "when");', 'SELECT * FROM t AS t t1 JOIN t t2 USING(x);', 'FROM tbl SELECT i, s;', 'FROM tbl;']
466
+ `SELECT`: The SELECT statement retrieves rows from the database. It is used to query the database and retrieve data according to specific requirements. The statement can include several clauses, such as FROM, WHERE, GROUP BY, ORDER BY, and LIMIT, to filter, organize, and limit the query results., Examples: ['SELECT * FROM tbl;', 'SELECT j FROM tbl WHERE i = 3;', 'SELECT i, sum(j) FROM tbl GROUP BY i;', 'SELECT * FROM tbl ORDER BY i DESC LIMIT 3;', 'SELECT * FROM t1 JOIN t2 USING (a, b);', 'SELECT #1, #3 FROM tbl;', 'SELECT DISTINCT city FROM addresses;', 'SELECT d FROM (SELECT 1 AS a, 2 AS b) d;', 'SELECT rowid, id, content FROM t;']
467
+ `WHERE`: The WHERE clause specifies filters to apply to the data being queried, allowing selection of a specific subset of data. It is logically applied immediately after the FROM clause in a SQL query., Examples: ['SELECT * FROM table_name WHERE id = 3;', "SELECT * FROM table_name WHERE name ILIKE '%mark%';", 'SELECT * FROM table_name WHERE id = 3 OR id = 7;']
468
+ `ORDER BY`: The ORDER BY clause is an output modifier used to sort the rows in a query result set according to specified sorting criteria. It allows sorting in either ascending or descending order, and can also specify the position of NULL values (either at the beginning or end). The clause can contain multiple expressions that determine the sort order, and supports the sorting of columns by name, column position number, or the ALL keyword, which sorts by all columns in left-to-right order., Examples: ['SELECT * FROM addresses ORDER BY city;', 'SELECT * FROM addresses ORDER BY city DESC NULLS LAST;', 'SELECT * FROM addresses ORDER BY city, zip;', 'SELECT * FROM addresses ORDER BY city COLLATE DE;', 'SELECT * FROM addresses ORDER BY ALL;', 'SELECT * FROM addresses ORDER BY ALL DESC;']
469
+ `GROUP BY`: The `GROUP BY` clause is used to specify which columns should be used for grouping when performing aggregations in a `SELECT` statement. It aggregates data based on matching data in the specified columns, allowing other columns to be combined using aggregate functions. The query becomes an aggregate query if a `GROUP BY` clause is specified, even if no aggregates are present in the `SELECT` clause., Examples: ['SELECT city, count(*) FROM addresses GROUP BY city;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY city, street_name;', 'SELECT city, street_name FROM addresses GROUP BY ALL;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY ALL;']
470
+ `JOIN`: The FROM clause specifies the source of the data for the query. It can include a single table, multiple joined tables, or subqueries. The JOIN clause is used to combine rows from two or more tables based on a related column between them. There are several types of joins, including INNER, OUTER, CROSS, NATURAL, SEMI, ANTI, LATERAL, POSITIONAL, ASOF, and self-joins., Examples: ['SELECT * FROM table_name;', 'FROM table_name SELECT *;', 'FROM table_name;', 'SELECT tn.* FROM table_name tn;', 'SELECT * FROM schema_name.table_name;', 'SELECT t.i FROM range(100) AS t(i);', "SELECT * FROM 'test.csv';", 'SELECT * FROM (SELECT * FROM table_name);', 'SELECT t FROM t;', "SELECT t FROM (SELECT unnest(generate_series(41, 43)) AS x, 'hello' AS y) t;", 'SELECT * FROM table_name JOIN other_table ON table_name.key = other_table.key;', 'SELECT * FROM table_name TABLESAMPLE 10%;', 'SELECT * FROM table_name TABLESAMPLE 10 ROWS;', 'FROM range(100) AS t(i) SELECT sum(t.i) WHERE i % 2 = 0;', 'SELECT a.*, b.* FROM a CROSS JOIN b;', 'SELECT a.*, b.* FROM a, b;', 'SELECT n.*, r.* FROM l_nations n JOIN l_regions r ON (n_regionkey = r_regionkey);', 'SELECT * FROM city_airport NATURAL JOIN airport_names;', 'SELECT * FROM city_airport JOIN airport_names USING (iata);', 'SELECT * FROM city_airport SEMI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata IN (SELECT iata FROM airport_names);', 'SELECT * FROM city_airport ANTI JOIN airport_names USING (iata);', 'SELECT * FROM city_airport WHERE iata NOT IN (SELECT iata FROM airport_names WHERE iata IS NOT NULL);', 'SELECT * FROM range(3) t(i), LATERAL (SELECT i + 1) t2(j);', 'SELECT * FROM generate_series(0, 1) t(i), LATERAL (SELECT i + 10 UNION ALL SELECT i + 100) t2(j);', 'SELECT * FROM trades t ASOF JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF LEFT JOIN prices p ON t.symbol = p.symbol AND t.when >= p.when;', 'SELECT * FROM trades t ASOF JOIN prices p USING (symbol, "when");', 'SELECT t.symbol, t.when AS trade_when, p.when AS price_when, price FROM trades t ASOF LEFT JOIN prices p USING (symbol, "when");', 'SELECT * FROM t AS t t1 JOIN t t2 USING(x);', 'FROM tbl SELECT i, s;', 'FROM tbl;']
471
+ `WITH`: The WITH clause in SQL is used to define common table expressions (CTEs), which are temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs simplify complex queries by breaking them into more manageable parts, and they can be recursive, allowing them to reference themselves. The WITH clause can include multiple CTEs, and DuckDB supports specifying whether a CTE should be materialized explicitly or not., Examples: ['WITH cte AS (SELECT 42 AS x) SELECT * FROM cte;', 'WITH cte1 AS (SELECT 42 AS i), cte2 AS (SELECT i * 100 AS x FROM cte1) SELECT * FROM cte2;', 'WITH t(x) AS (⟨complex_query⟩) SELECT * FROM t AS t1, t AS t2, t AS t3;', 'WITH t(x) AS MATERIALIZED (⟨complex_query⟩) SELECT * FROM t AS t1, t AS t2, t AS t3;', 'WITH RECURSIVE FibonacciNumbers (RecursionDepth, FibonacciNumber, NextNumber) AS (SELECT 0 AS RecursionDepth, 0 AS FibonacciNumber, 1 AS NextNumber UNION ALL SELECT fib.RecursionDepth + 1 AS RecursionDepth, fib.NextNumber AS FibonacciNumber, fib.FibonacciNumber + fib.NextNumber AS NextNumber FROM FibonacciNumbers fib WHERE fib.RecursionDepth + 1 < 10) SELECT fn.RecursionDepth AS FibonacciNumberIndex, fn.FibonacciNumber FROM FibonacciNumbers fn;']
472
+ `LIMIT`: The LIMIT clause restricts the number of rows returned by a query. The OFFSET clause specifies the starting point within the result set from which to begin returning rows. LIMIT is commonly used to return a specified number of rows from a result set, while OFFSET is used to skip a specified number of rows before beginning to return rows., Examples: ['SELECT * FROM addresses LIMIT 5;', 'SELECT * FROM addresses LIMIT 5 OFFSET 5;', 'SELECT city, count(*) AS population FROM addresses GROUP BY city ORDER BY population DESC LIMIT 5;']
473
+ `CREATE TABLE`: The `CREATE TABLE` statement is used to create a new table in the catalog. It allows for the definition of columns, data types, constraints, and primary keys. Additionally, it supports features like creating temporary tables, using `CREATE TABLE ... AS SELECT` for replicating schemas or importing data from CSV files, incorporating `OR REPLACE` to overwrite existing tables, using `IF NOT EXISTS` to conditionally create tables, and defining check and foreign key constraints., Examples: ['CREATE TABLE t1 (i INTEGER, j INTEGER);', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, j VARCHAR);', 'CREATE TABLE t1 (id INTEGER, j VARCHAR, PRIMARY KEY (id, j));', 'CREATE TABLE t1 (\n i INTEGER NOT NULL,\n decimalnr DOUBLE CHECK (decimalnr < 10),\n date DATE UNIQUE,\n time TIMESTAMP\n);', 'CREATE TABLE t1 AS SELECT 42 AS i, 84 AS j;', "CREATE TEMP TABLE t1 AS SELECT * FROM read_csv('path/file.csv');", 'CREATE OR REPLACE TABLE t1 (i INTEGER, j INTEGER);', 'CREATE TABLE IF NOT EXISTS t1 (i INTEGER, j INTEGER);', 'CREATE TABLE nums AS SELECT i FROM range(0, 3) t(i);', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, percentage INTEGER CHECK (0 <= percentage AND percentage <= 100));', 'CREATE TABLE t1 (id INTEGER PRIMARY KEY, j VARCHAR);\nCREATE TABLE t2 (\n id INTEGER PRIMARY KEY,\n t1_id INTEGER,\n FOREIGN KEY (t1_id) REFERENCES t1 (id)\n);', 'CREATE TABLE t1 (x FLOAT, two_x AS (2 * x));']
474
+ `SET`: The SET statement modifies a DuckDB configuration option at the specified scope, while the RESET statement changes the option to its default value. The scope can be GLOBAL, SESSION, or LOCAL (not yet implemented)., Examples: ["SET memory_limit = '10GB';", 'SET threads = 1;', 'SET threads TO 1;', 'RESET threads;', "SELECT current_setting('threads');", "SET GLOBAL search_path = 'db1,db2'", "SET SESSION default_collation = 'nocase';"]
475
+ `DROP`: The `DROP` statement in DuckDB is used to remove a catalog entry that was previously added with the `CREATE` command. It can drop various types of objects such as tables, views, functions, indexes, schemas, sequences, macros, and types. It also has options like `IF EXISTS` to prevent errors if the object does not exist and `CASCADE` to also drop all dependent objects., Examples: ['DROP TABLE tbl;', 'DROP VIEW IF EXISTS v1;', 'DROP FUNCTION fn;', 'DROP INDEX idx;', 'DROP SCHEMA sch;', 'DROP SEQUENCE seq;', 'DROP MACRO mcr;', 'DROP MACRO TABLE mt;', 'DROP TYPE typ;', 'DROP SCHEMA myschema CASCADE;']
476
+ `ALTER TABLE`: The `ALTER TABLE` statement is used to modify the schema of an existing table in the catalog. This includes adding, dropping, or modifying columns, renaming tables and columns, and setting or dropping default values and not null constraints. Changes made with `ALTER TABLE` are transactional, meaning they are not visible to other transactions until committed and can be rolled back., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers ADD COLUMN l INTEGER DEFAULT 10;', 'ALTER TABLE integers DROP k;', 'ALTER TABLE integers ALTER i TYPE VARCHAR;', "ALTER TABLE integers ALTER i SET DATA TYPE VARCHAR USING concat(i, '_', j);", 'ALTER TABLE integers ALTER COLUMN i SET DEFAULT 10;', 'ALTER TABLE integers ALTER COLUMN i DROP DEFAULT;', 'ALTER TABLE t ALTER COLUMN x SET NOT NULL;', 'ALTER TABLE t ALTER COLUMN x DROP NOT NULL;', 'ALTER TABLE integers RENAME TO integers_old;', 'ALTER TABLE integers RENAME i TO j;']
477
+ `HAVING`: The HAVING clause is used after the GROUP BY clause in SQL to filter the grouped results. It performs filtering based on aggregate functions and conditions imposed on the grouped data. Unlike the WHERE clause, which filters rows before grouping, the HAVING clause filters after the grouping has been completed., Examples: ['SELECT city, count(*) FROM addresses GROUP BY city HAVING count(*) >= 50;', 'SELECT city, street_name, avg(income) FROM addresses GROUP BY city, street_name HAVING avg(income) > 2 * median(income);']
478
+ `UPDATE`: The UPDATE statement modifies the values of rows in a table. It allows updating specific columns for rows that meet certain conditions, retaining previous values for unspecified columns. The statement can use data from other tables or the same table to determine the new values, using joins or subqueries., Examples: ['UPDATE tbl SET i = 0 WHERE i IS NULL;', 'UPDATE tbl SET i = 1, j = 2;', 'UPDATE original SET value = new.value FROM new WHERE original.key = new.key;', 'UPDATE original SET value = (SELECT new.value FROM new WHERE original.key = new.key);', "UPDATE original AS true_original SET value = (SELECT new.value || ' a change!' AS value FROM original AS new WHERE true_original.key = new.key);", "UPDATE city SET revenue = revenue + 100 FROM country WHERE city.country_code = country.code AND country.name = 'France';"]
479
+ `USE`: The `USE` statement selects a database and optional schema to use as the default for future operations, such as when creating tables without a fully qualified name., Examples: ['USE memory;', 'USE duck.main;']
480
+ `INSERT`: The INSERT statement is used to insert new data into a table in DuckDB. It can insert specific values, results from a query, handle conflicts with ON CONFLICT clauses, and return inserted rows using the RETURNING clause., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);', 'INSERT INTO tbl BY POSITION VALUES (5, 42);', 'INSERT INTO tbl BY NAME (SELECT 42 AS b, 32 AS a);', 'INSERT INTO tbl VALUES (1, 84) ON CONFLICT DO NOTHING;', 'INSERT INTO tbl VALUES (1, 84) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO tbl (j, i) VALUES (168, 1) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO tbl BY NAME (SELECT 84 AS j, 1 AS i) ON CONFLICT DO UPDATE SET j = EXCLUDED.j;', 'INSERT INTO t1 SELECT 42 RETURNING *;', 'INSERT INTO t2 SELECT 2 AS i, 3 AS j RETURNING *, i * j AS i_times_j;', "CREATE TABLE t3 (i INTEGER PRIMARY KEY, j INTEGER); CREATE SEQUENCE 't3_key'; INSERT INTO t3 SELECT nextval('t3_key') AS i, 42 AS j UNION ALL SELECT nextval('t3_key') AS i, 43 AS j RETURNING *;"]
481
+ `COPY`: The COPY statement in DuckDB is used to transfer data between DuckDB tables and external files. It supports various file formats such as CSV, Parquet, and JSON, and can either import data from these files into a DuckDB table or export data from a DuckDB table to these files. The statement is versatile, allowing customization of options to handle different data formats, delimiters, headers, and more, making it useful for bulk data operations., Examples: ["COPY lineitem FROM 'lineitem.csv';", "COPY lineitem FROM 'lineitem.csv' (DELIMITER '|');", "COPY lineitem FROM 'lineitem.pq' (FORMAT PARQUET);", "COPY lineitem FROM 'lineitem.json' (FORMAT JSON, AUTO_DETECT true);", "COPY lineitem TO 'lineitem.csv' (FORMAT CSV, DELIMITER '|', HEADER);", "COPY (SELECT l_orderkey, l_partkey FROM lineitem) TO 'lineitem.parquet' (COMPRESSION ZSTD);", 'COPY FROM DATABASE db1 TO db2;', 'COPY FROM DATABASE db1 TO db2 (SCHEMA);']
482
+ `ATTACH`: The ATTACH statement in DuckDB is used to add a new database file to the catalog, allowing the database to be read from and written to. It supports file paths as well as HTTP and S3 endpoints. Detached databases need to be re-attached in new sessions. The DETACH statement is used to close and detach such databases, thereby releasing any locks held on the database file. ATTACH and DETACH allow for operating on multiple database files simultaneously, enabling data transfer across different databases., Examples: ["ATTACH 'file.db';", "ATTACH 'file.db' AS file_db;", "ATTACH 'file.db' (READ_ONLY);", "ATTACH 'file.db' (BLOCK_SIZE 16384);", "ATTACH 'sqlite_file.db' AS sqlite_db (TYPE SQLITE);", "ATTACH IF NOT EXISTS 'file.db';", "ATTACH IF NOT EXISTS 'file.db' AS file_db;", 'CREATE TABLE file.new_table (i INTEGER);', 'DETACH file;', 'SHOW DATABASES;', 'USE file;', "ATTACH 'https://blobs.duckdb.org/databases/stations.duckdb' AS stations_db;", "ATTACH 's3://duckdb-blobs/databases/stations.duckdb' AS stations_db (READ_ONLY);"]
483
+ `CALL`: The CALL statement invokes the given table function and returns the results., Examples: ['CALL duckdb_functions();', "CALL pragma_table_info('pg_am');"]
484
+ `VALUES`: The VALUES clause is used to specify a fixed number of rows. It can be utilized as a stand-alone statement, as part of the FROM clause, or as input to an INSERT INTO statement., Examples: ["VALUES ('Amsterdam', 1), ('London', 2);", "SELECT * FROM (VALUES ('Amsterdam', 1), ('London', 2)) cities(name, id);", "INSERT INTO cities VALUES ('Amsterdam', 1), ('London', 2);", "CREATE TABLE cities AS SELECT * FROM (VALUES ('Amsterdam', 1), ('London', 2)) cities(name, id);"]
485
+ `EXPLAIN`: DuckDB supports profiling queries using the EXPLAIN and EXPLAIN ANALYZE statements. The EXPLAIN statement provides the query plan and estimated cardinalities for each operator without executing the query. The EXPLAIN ANALYZE statement executes the query and shows the actual cardinalities and the wall-clock time spent in each operator., Examples: ['EXPLAIN SELECT * FROM table_name;', 'EXPLAIN ANALYZE SELECT * FROM table_name;']
486
+ `SUMMARIZE`: The SUMMARIZE statement returns summary statistics for a table, view, or a query., Examples: ['SUMMARIZE tbl;', 'SUMMARIZE SELECT * FROM tbl;']
487
+ `SAMPLE`: The SAMPLE clause allows you to run a query on a sample from the base table, speeding up query processing at the expense of accuracy, and is applied after the FROM clause but before the WHERE clause or any aggregates., Examples: ['SELECT * FROM addresses USING SAMPLE 1%;', 'SELECT * FROM addresses USING SAMPLE 1% (bernoulli);', 'SELECT * FROM (SELECT * FROM addresses) USING SAMPLE 10 ROWS;']
488
+
489
+ DuckDB Types:
490
+ `VARCHAR`: `VARCHAR` is a versatile data type used to store variable-length character strings, accommodating a wide range of text and string data without enforcing a specific length., Examples: ['CREATE TABLE people (name VARCHAR, age INTEGER);', "INSERT INTO documents (text) VALUES ('This is a VARCHAR example text.');", "SELECT * FROM employees WHERE department = 'Engineering';", 'ALTER TABLE students ADD COLUMN email VARCHAR;', "UPDATE orders SET status = 'Shipped' WHERE order_id = 102;", "COPY products TO 'products.csv' DELIMITER ',' HEADER;"]
491
+ `INTEGER`: The INTEGER data type, with aliases such as int, signed, int4, int32, integer, and integral, represents whole numbers and is commonly used to store numeric data without fractional components., Examples: ['-- Assigning integer values to columns in a CREATE TABLE statement\nCREATE TABLE my_table (id INTEGER, age INTEGER);', '-- Inserting integer values as literals within an INSERT statement\nINSERT INTO my_table VALUES (1, 25);', '-- Using integer operations in a SELECT statement\nSELECT id + 10 AS new_id FROM my_table;', '-- Casting a float to an integer\nSELECT CAST(3.7 AS INTEGER) AS whole_number;', '-- Defining a column to only accept non-negative integers using a CHECK constraint\nCREATE TABLE my_table (id INTEGER CHECK (id >= 0));', '-- Using the INTEGER type in a primary key definition\nCREATE TABLE users (user_id INTEGER PRIMARY KEY, username VARCHAR);', '-- Updating integer columns\nUPDATE my_table SET age = age + 1 WHERE id = 1;', '-- Comparing integer values in a WHERE clause\nSELECT * FROM my_table WHERE age > 20;']
492
+ `NULL`: The `NULL` type in SQL represents a missing or unknown value, allowing for fields within a table to be uninitialized or absent in data., Examples: ['SELECT NULL = NULL;', 'SELECT NULL IS NULL;', "INSERT INTO table_name (column1, column2) VALUES (NULL, 'data');", "SELECT coalesce(NULL, 'default_value');", 'UPDATE table_name SET column1 = NULL WHERE condition;', "SELECT CASE WHEN column IS NULL THEN 'Value is NULL' ELSE column END FROM table_name;"]
493
+ `STRUCT`: The `STRUCT` data type in SQL is used to create a column that contains an ordered list of columns, referred to as entries, which are accessed using named keys. This type is ideal for nesting multiple columns into a single column, allowing a structured and consistent data schema across all rows., Examples: ["SELECT struct_pack(key1 := 'value1', key2 := 42) AS s;", "SELECT {{'key1': 'value1', 'key2': 42}} AS s;", "SELECT a.x FROM (SELECT {{'x': 1, 'y': 2, 'z': 3}} AS a);", "SELECT struct_insert({{'a': 1, 'b': 2, 'c': 3}}, d := 4) AS s;", 'CREATE TABLE t1 (s STRUCT(v VARCHAR, i INTEGER));', "INSERT INTO t1 VALUES (row('a', 42));", "SELECT a.* FROM (SELECT {{'x': 1, 'y': 2, 'z': 3}} AS a);", "SELECT struct_extract({{'x space': 1, 'y': 2, 'z': 3}}, 'x space');"]
494
+ `FLOAT`: The FLOAT data type, also known by aliases FLOAT4, REAL, or float, represents a single precision floating-point number, facilitating approximate calculations and efficient handling of numerical data with precision typically up to 6 decimal digits and a range of at least 1E-37 to 1E+37., Examples: ['-- Example: Creating a table with a FLOAT column\nCREATE TABLE example_table (id INTEGER, value FLOAT);', '-- Example: Inserting values into a FLOAT column\nINSERT INTO example_table VALUES (1, 3.14), (2, 2.718);', '-- Example: Performing arithmetic operations with FLOAT values\nSELECT id, value * 2.0::FLOAT AS doubled_value FROM example_table;', '-- Example: Casting a numeric value to FLOAT\nSELECT CAST(100 AS FLOAT) AS float_value;', '-- Example: Using FLOAT values in a mathematical function\nSELECT SQRT(value) FROM example_table WHERE value > 0;', '-- Example: Comparing FLOAT values\nSELECT * FROM example_table WHERE value > 3.0::FLOAT;']
495
+
496
+ DuckDB Other Keywords:
497
+ `AS`: The `AS` keyword in SQL is used to create an alias for columns or tables, helping to simplify query logic and improve readability., Examples: ['SELECT first_name AS name FROM employees;', 'SELECT department AS dept FROM company;', 'CREATE VIEW sales_report AS SELECT * FROM sales WHERE year = 2023;', 'SELECT product_name AS name, SUM(sales) AS total_sales FROM store GROUP BY product_name;', 'SELECT c.customer_id, c.name AS customer_name, o.order_id, o.total_amount AS amount FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id;']
498
+ `AND`: "AND" is a logical operator used to combine multiple conditions in SQL queries, returning true only if all conditions are true., Examples: ["SELECT * FROM employees WHERE department = 'Sales' AND salary > 50000;", "SELECT * FROM orders WHERE order_date >= '2023-01-01' AND status = 'Pending';", "SELECT * FROM customers WHERE age > 21 AND city = 'New York';", "SELECT * FROM products WHERE category = 'Electronics' AND stock_quantity > 0;", "SELECT * FROM students WHERE grade = 'A' AND attendance_percentage > 90;"]
499
+ `OR`: The `ORDER BY` clause sorts query results based on specified columns or expressions, with optional ascending or descending order and handling of NULL values., Examples: ['SELECT * FROM addresses ORDER BY city;', 'SELECT * FROM addresses ORDER BY city DESC NULLS LAST;', 'SELECT * FROM addresses ORDER BY city, zip;', 'SELECT * FROM addresses ORDER BY city COLLATE DE;', 'SELECT * FROM addresses ORDER BY ALL;', 'SELECT * FROM addresses ORDER BY ALL DESC;']
500
+ `ON`: The `ON` keyword in SQL is used with various clauses and statements such as JOIN, UPDATE, DELETE, and ON CONFLICT to specify conditions for selecting, updating, or deleting rows that match specific criteria in relational databases., Examples: ['SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;', "UPDATE employees SET salary = salary * 1.1 ON department.id = employees.department_id WHERE department.name = 'Engineering';", "DELETE FROM orders WHERE orders.date < '2023-01-01' ON CONFLICT DO NOTHING;", "INSERT INTO products (id, name) VALUES (1, 'Widget') ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name;"]
501
+ `NOT`: The `NOT` keyword in SQL is used to negate a condition, meaning it reverses the truth value of the condition it's used with., Examples: ['SELECT * FROM Students WHERE NOT (grade > 80);', 'SELECT * FROM Employees WHERE NOT EXISTS (SELECT * FROM Promotions WHERE Promotions.employee_id = Employees.id);', "SELECT * FROM Products WHERE NOT (name LIKE 'A%');", 'SELECT * FROM Orders WHERE order_date IS NOT NULL;', 'SELECT * FROM Library WHERE NOT (publication_year BETWEEN 1980 AND 2000);']
502
+ `DISTINCT`: The `DISTINCT` keyword is used in the SQL `SELECT` statement to ensure that only unique values are returned for specified columns, effectively removing duplicate rows from the result set., Examples: ['SELECT DISTINCT city FROM addresses;', 'SELECT DISTINCT ON(country) city, population FROM cities ORDER BY population DESC;']
503
+ `DESC`: The keyword `desc` is used in SQL to describe the DESCENDING order in which query results should be sorted, often associated with the `ORDER BY` clause., Examples: ['SELECT name FROM employees ORDER BY salary DESC;', 'CREATE TABLE example (id INTEGER, name VARCHAR);', 'DESCRIBE example;', 'DESCRIBE SELECT * FROM example WHERE id < 10 ORDER BY name DESC;']
504
+ `IS`: The "IS" keyword is used in SQL to perform tests on values to check for NULL values or to use as part of statements like IS DISTINCT FROM which can handle NULLs in equality comparisons., Examples: ['SELECT 4 IS DISTINCT FROM NULL;', 'SELECT 4 IS NOT DISTINCT FROM 4;', 'SELECT NULL IS NULL;', 'SELECT NULL IS NOT NULL;']
505
+ `IN`: The `IN` keyword is used in SQL to specify a list of discrete values for a column to match against, typically in a `WHERE` clause, allowing for multiple specific conditions to be evaluated at once., Examples: ["SELECT * FROM employees WHERE department IN ('HR', 'Engineering', 'Marketing');", 'SELECT id, name FROM students WHERE grade IN (10, 11, 12);', "DELETE FROM orders WHERE order_status IN ('Cancelled', 'Returned');", "UPDATE items SET status = 'Unavailable' WHERE item_id IN (1001, 1002, 1003);", "SELECT * FROM logs WHERE severity IN ('ERROR', 'CRITICAL') ORDER BY timestamp DESC;"]
506
+ `ALL`: The `ALL` keyword in SQL specifies that operations should retain all duplicate rows, as seen in commands like `UNION ALL`, `INTERSECT ALL`, and `EXCEPT ALL`, which follow bag semantics instead of eliminating duplicates., Examples: ['UNION ALL\n\n```sql\nSELECT * FROM range(2) t1(x)\nUNION ALL\nSELECT * FROM range(3) t2(x);\n```\nThis example demonstrates using `UNION ALL` to combine rows from two queries without eliminating duplicates.', 'INTERSECT ALL\n\n```sql\nSELECT unnest([5, 5, 6, 6, 6, 6, 7, 8]) AS x\nINTERSECT ALL\nSELECT unnest([5, 6, 6, 7, 7, 9]);\n```\nThis example shows using `INTERSECT ALL` to select rows that are present in both result sets, keeping duplicate values.', 'EXCEPT ALL\n\n```sql\nSELECT unnest([5, 5, 6, 6, 6, 6, 7, 8]) AS x\nEXCEPT ALL\nSELECT unnest([5, 6, 6, 7, 7, 9]);\n```\nThis example illustrates `EXCEPT ALL`, which selects all rows present in the first query but not in the second, without removing duplicates.', 'ORDER BY ALL\n\n```sql\nSELECT *\nFROM addresses\nORDER BY ALL;\n```\nThis SQL command uses `ORDER BY ALL` to sort the result set by all columns sequentially from left to right.']
507
+ `LIKE`: The `LIKE` expression is used to determine if a string matches a specified pattern, allowing wildcard characters such as `_` to represent any single character and `%` to match any sequence of characters., Examples: ["SELECT 'abc' LIKE 'abc'; -- true", "SELECT 'abc' LIKE 'a%'; -- true", "SELECT 'abc' LIKE '_b_'; -- true", "SELECT 'abc' LIKE 'c'; -- false", "SELECT 'abc' LIKE 'c%'; -- false", "SELECT 'abc' LIKE '%c'; -- true", "SELECT 'abc' NOT LIKE '%c'; -- false", "SELECT 'abc' ILIKE '%C'; -- true"]
508
+ `IF`: The `IF` keyword is used in conditional constructs, most commonly found in the `IF NOT EXISTS` or `IF EXISTS` clauses in SQL commands to prevent errors or skip operations, such as creating or dropping a database or table when certain conditions are met., Examples: ['CREATE DATABASE IF NOT EXISTS ducks_db;', 'CREATE TABLE IF NOT EXISTS t1 (i INTEGER, j INTEGER);', 'CREATE TABLE t1 (id INTEGER, PRIMARY KEY (id), UNIQUE (id)) IF NOT EXISTS;']
509
+ `EXISTS`: The `EXISTS` operator is used to determine if a subquery returns any rows, returning `true` if at least one row exists and `false` otherwise., Examples: ["SELECT EXISTS (FROM grades WHERE course = 'Math') AS math_grades_present;", "SELECT EXISTS (FROM grades WHERE course = 'History') AS history_grades_present;"]
510
+ `COLUMN`: The keyword 'column' is used to refer to the vertical entities in a table, defining the structure and type of data stored in a database table., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers DROP COLUMN k;', 'ALTER TABLE integers RENAME COLUMN i TO j;', 'SELECT * EXCLUDE (city) FROM addresses;', 'SELECT * REPLACE (lower(city) AS city) FROM addresses;', "SELECT COLUMNS(c -> c LIKE '%num%') FROM addresses;", "SELECT COLUMNS('(id|numbers?)') FROM numbers;"]
511
+ `INTO`: The `INSERT INTO` clause in SQL is used to add new records to a table, either by providing specific values or by inserting data from an existing query., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);', 'INSERT INTO tbl RETURNING *;', 'INSERT INTO tbl (a, b) BY POSITION VALUES (5, 42);', 'INSERT INTO tbl BY NAME (SELECT 42 AS b, 32 AS a);']
512
+ `TO`: The `TO` keyword in SQL is used within the `COPY` statement to export data from a database table or query result to an external file, supporting various formats such as CSV, Parquet, JSON, and databases., Examples: ["COPY lineitem TO 'lineitem.csv' (FORMAT CSV, DELIMITER '|', HEADER);", 'COPY lineitem TO "lineitem.csv";', "COPY (SELECT l_orderkey, l_partkey FROM lineitem) TO 'lineitem.parquet' (COMPRESSION ZSTD);", "COPY lineitem TO 'lineitem.tsv' (DELIMITER '\\t', HEADER false);", "COPY lineitem(l_orderkey) TO 'orderkey.tbl' (DELIMITER '|');", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.csv' (DELIMITER ',');", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.parquet' (FORMAT PARQUET);", "COPY (SELECT 42 AS a, 'hello' AS b) TO 'query.ndjson' (FORMAT JSON);"]
513
+ `EXCLUDE`: The `EXCLUDE` keyword is used in SQL to exclude certain columns from being selected when using the star (`*`) expression, providing more control over the columns included in the query results., Examples: ['SELECT * EXCLUDE (city) FROM addresses;', 'SELECT COLUMNS(* EXCLUDE (number)) FROM numbers;', 'INSERT INTO tbl SELECT * EXCLUDE (id) FROM source_table;']
514
+ `YEAR`: The keyword `YEAR` is used in intervals to specify periods of time, specifically one year, and is stored as 12 months in database systems., Examples: ['SELECT INTERVAL 1 YEAR;', "SELECT INTERVAL '1.5' YEARS; -- Returns 12 months due to truncation", "SELECT DATE '2000-01-01' + INTERVAL 1 YEAR;", "SELECT datepart('year', INTERVAL 14 MONTHS); -- Returns 1", 'PIVOT Cities ON Year USING sum(Population);']
515
+ `USING`: The `USING` clause in SQL is used in conjunction with a `JOIN` to specify columns to compare for equality when joining tables, simplifying syntax when column names are the same in both tables., Examples: ['SELECT * FROM employees JOIN departments USING (department_id);', 'PIVOT sales_data ON month USING sum(sales) GROUP BY category;', 'SELECT * FROM cities JOIN countries USING (country_code);', 'FROM test.csv USING SAMPLE 10 PERCENT;', 'SELECT product_id, order_date, SUM(quantity) FROM orders JOIN order_items USING (order_id) GROUP BY product_id, order_date;']
516
+ `ADD`: The `ADD COLUMN` clause in the `ALTER TABLE` statement allows for the inclusion of a new column with a specified data type and optional default value into an existing table., Examples: ['ALTER TABLE integers ADD COLUMN k INTEGER;', 'ALTER TABLE integers ADD COLUMN l INTEGER DEFAULT 10;', "UPDATE my_db.movies SET summary = prompt('summarize review in one sentence' || overview);", 'ALTER TABLE my_db.movies ADD COLUMN summary VARCHAR;', 'ALTER TABLE my_db.movies ADD COLUMN metadata STRUCT(main_characters VARCHAR[], genre VARCHAR, action INTEGER);']
517
+ `TYPE`: The `CREATE TYPE` statement is used to define a new data type in the catalog, allowing users to define enums, structs, unions, and type aliases for better schema and type management in SQL., Examples: ["CREATE TYPE mood AS ENUM ('happy', 'sad', 'curious');", 'CREATE TYPE many_things AS STRUCT(k INTEGER, l VARCHAR);', 'CREATE TYPE one_thing AS UNION(number INTEGER, string VARCHAR);', 'CREATE TYPE x_index AS INTEGER;']
518
+ `PRAGMA`: The 'PRAGMA' statement is a SQL extension used to modify the internal state of the database engine and influence its execution or behavior., Examples: ['PRAGMA database_list; -- List all databases', 'PRAGMA show_tables; -- List all tables', "PRAGMA table_info('table_name'); -- Get information about a specific table's columns", 'PRAGMA database_size; -- Get file and memory size of each database', "PRAGMA storage_info('table_name'); -- Retrieve storage information for a specific table", 'PRAGMA show_databases; -- Equivalent to SHOW DATABASES statement', 'PRAGMA version; -- Show DuckDB version information', 'PRAGMA platform; -- Show platform identifier where DuckDB is running', 'PRAGMA enable_progress_bar; -- Enable progress bar for running queries', 'PRAGMA disable_progress_bar; -- Disable progress bar for running queries', 'PRAGMA enable_profiling; -- Enable query profiling', 'PRAGMA disable_profiling; -- Disable query profiling', 'PRAGMA verify_external; -- Enable verification of external operators', 'PRAGMA disable_verify_external; -- Disable verification of external operators', 'PRAGMA enable_object_cache; -- Enable caching of objects', 'PRAGMA disable_object_cache; -- Disable caching of objects', 'PRAGMA force_checkpoint; -- Force a checkpoint even when no changes are made', 'PRAGMA enable_checkpoint_on_shutdown; -- Run checkpoint on shutdown', 'PRAGMA disable_checkpoint_on_shutdown; -- Do not run checkpoint on shutdown']
519
+ `COLUMNS`: The `COLUMNS` expression in SQL is used to apply operations across multiple columns, allowing dynamic column selection through patterns, regular expressions, or lambda functions for querying or modifying datasets efficiently., Examples: ["SELECT COLUMNS(c -> c LIKE '%num%') FROM addresses;", "SELECT COLUMNS('number\\d+') FROM addresses;", 'SELECT COLUMNS(* EXCLUDE (empid, dept)) INTO NAME month VALUE sales UNPIVOT monthly_sales;', 'SELECT min(COLUMNS(* REPLACE (number + id AS number))), count(COLUMNS(* EXCLUDE (number))) FROM numbers;', "SELECT coalesce(*COLUMNS(['a', 'b', 'c'])) AS result FROM (SELECT NULL a, 42 b, true c);"]
520
+ `NAME`: The `INSERT` statement is used to add new rows of data into an existing table, either by specifying explicit values or by using results from a query., Examples: ['INSERT INTO tbl VALUES (1), (2), (3);', 'INSERT INTO tbl SELECT * FROM other_tbl;', 'INSERT INTO tbl (i) VALUES (1), (2), (3);', 'INSERT INTO tbl (i) VALUES (1), (DEFAULT), (3);', 'INSERT OR IGNORE INTO tbl (i) VALUES (1);', 'INSERT OR REPLACE INTO tbl (i) VALUES (1);']
521
+ `DATA`: The `CREATE DATABASE` statement allows you to create a new database, either by copying it from a local environment or zero-copy cloning within the same environment., Examples: ['CREATE DATABASE ducks;', 'CREATE OR REPLACE DATABASE ducks;', 'CREATE DATABASE IF NOT EXISTS ducks_db;', "CREATE DATABASE flying_ducks FROM './databases/local_ducks.db';"]
522
+ `DELIMITER`: The 'delimiter' keyword is used in SQL statements to specify the character that separates columns in a data representation, such as in CSV files, allowing for precise parsing of column values during data import or export operations., Examples: ["COPY lineitem FROM 'lineitem.csv' (DELIMITER '|');", "COPY ontime FROM 'flights.csv' (DELIMITER '|', HEADER);", "COPY lineitem TO 'lineitem.tbl' (DELIMITER '|', HEADER);", "COPY (SELECT * FROM ontime) TO 'ontime.csv' (DELIMITER ',', HEADER);", "EXPORT DATABASE 'directory' (FORMAT CSV, DELIMITER '|');"]
523
+
524
+ Here is the schema of the DuckDB database that the SQL query will run on:
525
+ {schema}
526
+
527
+ Question:
528
+ Here is the question or an instruction the user provided:
529
+ {question}
530
+
531
+ Write a DuckDB SQL query for the given question!
532
+
533
  Answer:
534
  ```
535
  """