Repository: prql/prql
Pull request #551 (2022-05-20 06:01:59)

Change versions to integers, add tests


Patch:
diff --git a/prql-compiler/src/ast/query.rs b/prql-compiler/src/ast/query.rs
index 497cbe5f..4ea95e7f 100644
--- a/prql-compiler/src/ast/query.rs
+++ b/prql-compiler/src/ast/query.rs
@@ -5,7 +5,7 @@ use super::{Dialect, Ident, Node, Range, Type};
 
 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Default)]
 pub struct Query {
-    pub version: Option<String>,
+    pub version: Option<i64>,
     #[serde(default)]
     pub dialect: Dialect,
     pub nodes: Vec<Node>,
diff --git a/prql-compiler/src/parser.rs b/prql-compiler/src/parser.rs
index 0d8d8de3..3d57bbf6 100644
--- a/prql-compiler/src/parser.rs
+++ b/prql-compiler/src/parser.rs
@@ -73,7 +73,9 @@ fn ast_of_parse_tree(pairs: Pairs<Rule>) -> Result<Vec<Node>> {
 
                     let version = params
                         .remove("version")
-                        .map(|v| v.unwrap(|i| i.into_ident(), "string"))
+                        .map(|v| v.unwrap(|i| i.into_literal(), "literal"))
+                        .transpose()?
+                        .map(|x| x.into_integer())
                         .transpose()?;
 
                     let dialect = if let Some(node) = params.remove("dialect") {
@@ -1600,6 +1602,63 @@ select [
         "### )
     }
 
+    #[test]
+    fn test_header() {
+        assert_yaml_snapshot!(parse(r###"
+        prql dialect:mssql version:1
+
+        from employees
+        "###).unwrap(), @r###"
+        ---
+        version: 1
+        dialect: MsSql
+        nodes:
+          - Pipeline:
+              value: ~
+              functions:
+                - FuncCall:
+                    name: from
+                    args:
+                      - Ident: employees
+                    named_args: {}
+        "### );
+
+        assert_yaml_snapshot!(parse(r###"
+        prql dialect:bigquery version:2
+
+        from employees
+        "###).unwrap(), @r###"
+        ---
+        version: 2
+        dialect: BigQuery
+        nodes:
+          - Pipeline:
+              value: ~
+              functions:
+                - FuncCall:
+                    name: from
+                    args:
+                      - Ident: employees
+                    named_args: {}
+        "### );
+
+        assert!(parse(
+            r###"
+        prql dialect:bigquery version:foo
+        from employees
+        "###,
+        )
+        .is_err());
+
+        assert!(parse(
+            r###"
+        prql dialect:yah version:foo
+        from employees
+        "###,
+        )
+        .is_err());
+    }
+
     #[test]
     fn test_parse_coalesce() {
         assert_yaml_snapshot!(parse(r###"
diff --git a/reference/src/queries/dialect_and_version.md b/reference/src/queries/dialect_and_version.md
index 9e3e0711..174cc2cc 100644
--- a/reference/src/queries/dialect_and_version.md
+++ b/reference/src/queries/dialect_and_version.md
@@ -42,13 +42,13 @@ take 10
 
 ## Version
 
-While not yet implemented, PRQL will allow specifying a version of the language
-in the PRQL header, like:
+PRQL allows specifying a version of the language in the PRQL header, like:
 
-```prql_no_test
+```prql
 prql version:1
 
 from employees
 ```
 
-This will allow the language to evolve without breaking existing queries.
+When the functionality is implemented, it will allow the language to evolve
+without breaking existing queries.


Test patch:
diff --git a/reference/tests/prql/queries/dialect_and_version-2.prql b/reference/tests/prql/queries/dialect_and_version-2.prql
new file mode 100644
index 00000000..b3d012a4
--- /dev/null
+++ b/reference/tests/prql/queries/dialect_and_version-2.prql
@@ -0,0 +1,3 @@
+prql version:1
+
+from employees
diff --git a/reference/tests/snapshots/snapshot__run_reference_prql@dialect_and_version-2.prql.snap b/reference/tests/snapshots/snapshot__run_reference_prql@dialect_and_version-2.prql.snap
new file mode 100644
index 00000000..ecba15a0
--- /dev/null
+++ b/reference/tests/snapshots/snapshot__run_reference_prql@dialect_and_version-2.prql.snap
@@ -0,0 +1,9 @@
+---
+source: reference/tests/snapshot.rs
+expression: sql
+input_file: reference/tests/prql/queries/dialect_and_version-2.prql
+---
+SELECT
+  employees.*
+FROM
+  employees