Repository: sqlfluff/sqlfluff
Pull request: Allow Bare Functions in column constraints
<!--Thanks for adding this feature!-->

<!--Please give the Pull Request a meaningful title for the release notes-->

### Brief summary of the change made
<!--Please include `fixes #XXXX` to automatically close any corresponding issue when the pull request is merged. Alternatively if not fully closed you can say `makes progress on #XXXX`.-->
Fixes #2606 

### Are there any other side effects of this change that we should be aware of?
Nope

### Pull Request checklist
- [x] Please confirm you have completed any of the necessary steps below.

- Included test cases to demonstrate any code changes, which may be one or more of the following:
  - `.yml` rule test cases in `test/fixtures/rules/std_rule_cases`.
  - `.sql`/`.yml` parser test cases in `test/fixtures/dialects` (note YML files can be auto generated with `tox -e generate-fixture-yml`).
  - Full autofix test cases in `test/fixtures/linter/autofix`.
  - Other.
- Added appropriate documentation for the change.
- Created GitHub issues for any relevant followup/future enhancements if appropriate.


Problem statement:
SQLITE: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP not parsable
<!--If this is a parsing or linting issue, please include a minimal SQL example which reproduces the issue, along with the `sqlfluff parse` output, `sqlfluff lint` output and `sqlfluff fix` output when relevant.-->

## Expected Behaviour

sqlite3 is parsed successfully

## Observed Behaviour

```
Found unparsable section: 'PRAGMA
Found unparsable section: 'CREATE TABLE IF NOT
```

## Steps to Reproduce

```
$ rm -rf a.sqlite; echo 'CREATE TABLE "tbl" ( "col" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );' | sqlite3 "a.sqlite"; echo '.dump' | sqlite3 a.sqlite | tee dump.sqlite
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "tbl" ( "col" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
COMMIT;

$ cat dump.sqlite | sqlfluff parse --dialect=sqlite -
...
==== parsing violations ====
L:   1 | P:   1 |  PRS | Line 1, Position 1: Found unparsable section: 'PRAGMA
                       | foreign_keys=OFF'
L:   3 | P:   1 |  PRS | Line 3, Position 1: Found unparsable section: 'CREATE TABLE IF NOT
                       | EXISTS "tbl" ( "col"...'
$
```

## Dialect

sqlite3

## Version
<!-- Include the output of `sqlfluff --version` along with your Python version.
If you are using dbt then please additionally include the dbt version. -->

```
$ sqlfluff --version
sqlfluff, version 0.9.4
$ python3 --version
Python 3.8.10
```

## Configuration

none

Hints from issue discussion:
Fixed in #2607 

Can you raise a separate issue if yoiu also need the `PRAGMA` statement to be parsable?

Commit message: Merge branch 'main' into barefunction-column-constraint

Patch to review:
diff --git a/src/sqlfluff/dialects/dialect_ansi.py b/src/sqlfluff/dialects/dialect_ansi.py
index 93197d8d72c..a8a67472853 100644
--- a/src/sqlfluff/dialects/dialect_ansi.py
+++ b/src/sqlfluff/dialects/dialect_ansi.py
@@ -2321,6 +2321,7 @@ class ColumnConstraintSegment(BaseSegment):
                 OneOf(
                     Ref("LiteralGrammar"),
                     Ref("FunctionSegment"),
+                    Ref("BareFunctionSegment"),
                     # ?? Ref('IntervalExpressionSegment')
                 ),
             ),
diff --git a/test/fixtures/dialects/ansi/create_table_constraint_default.sql b/test/fixtures/dialects/ansi/create_table_constraint_default.sql
new file mode 100644
index 00000000000..d51affda605
--- /dev/null
+++ b/test/fixtures/dialects/ansi/create_table_constraint_default.sql
@@ -0,0 +1,3 @@
+BEGIN TRANSACTION;
+CREATE TABLE IF NOT EXISTS "tbl" ( "col" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
+COMMIT;
diff --git a/test/fixtures/dialects/ansi/create_table_constraint_default.yml b/test/fixtures/dialects/ansi/create_table_constraint_default.yml
new file mode 100644
index 00000000000..24559cca740
--- /dev/null
+++ b/test/fixtures/dialects/ansi/create_table_constraint_default.yml
@@ -0,0 +1,39 @@
+# YML test files are auto-generated from SQL files and should not be edited by
+# hand. To help enforce this, the "hash" field in the file must match a hash
+# computed by SQLFluff when running the tests. Please run
+# `python test/generate_parse_fixture_yml.py`  to generate them after adding or
+# altering SQL files.
+_hash: 5edc8237281865faa18cc8d39b37eb227d501b9a1a7dfcb67d1df4a6299b83a7
+file:
+- statement:
+    transaction_statement:
+    - keyword: BEGIN
+    - keyword: TRANSACTION
+- statement_terminator: ;
+- statement:
+    create_table_statement:
+    - keyword: CREATE
+    - keyword: TABLE
+    - keyword: IF
+    - keyword: NOT
+    - keyword: EXISTS
+    - table_reference:
+        identifier: '"tbl"'
+    - bracketed:
+        start_bracket: (
+        column_definition:
+        - identifier: '"col"'
+        - data_type:
+            keyword: TIMESTAMP
+        - column_constraint_segment:
+          - keyword: NOT
+          - keyword: 'NULL'
+        - column_constraint_segment:
+            keyword: DEFAULT
+            bare_function: CURRENT_TIMESTAMP
+        end_bracket: )
+- statement_terminator: ;
+- statement:
+    transaction_statement:
+      keyword: COMMIT
+- statement_terminator: ;
diff --git a/test/fixtures/dialects/sqlite/create_table_constraint_default.sql b/test/fixtures/dialects/sqlite/create_table_constraint_default.sql
new file mode 100644
index 00000000000..d51affda605
--- /dev/null
+++ b/test/fixtures/dialects/sqlite/create_table_constraint_default.sql
@@ -0,0 +1,3 @@
+BEGIN TRANSACTION;
+CREATE TABLE IF NOT EXISTS "tbl" ( "col" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
+COMMIT;
diff --git a/test/fixtures/dialects/sqlite/create_table_constraint_default.yml b/test/fixtures/dialects/sqlite/create_table_constraint_default.yml
new file mode 100644
index 00000000000..24559cca740
--- /dev/null
+++ b/test/fixtures/dialects/sqlite/create_table_constraint_default.yml
@@ -0,0 +1,39 @@
+# YML test files are auto-generated from SQL files and should not be edited by
+# hand. To help enforce this, the "hash" field in the file must match a hash
+# computed by SQLFluff when running the tests. Please run
+# `python test/generate_parse_fixture_yml.py`  to generate them after adding or
+# altering SQL files.
+_hash: 5edc8237281865faa18cc8d39b37eb227d501b9a1a7dfcb67d1df4a6299b83a7
+file:
+- statement:
+    transaction_statement:
+    - keyword: BEGIN
+    - keyword: TRANSACTION
+- statement_terminator: ;
+- statement:
+    create_table_statement:
+    - keyword: CREATE
+    - keyword: TABLE
+    - keyword: IF
+    - keyword: NOT
+    - keyword: EXISTS
+    - table_reference:
+        identifier: '"tbl"'
+    - bracketed:
+        start_bracket: (
+        column_definition:
+        - identifier: '"col"'
+        - data_type:
+            keyword: TIMESTAMP
+        - column_constraint_segment:
+          - keyword: NOT
+          - keyword: 'NULL'
+        - column_constraint_segment:
+            keyword: DEFAULT
+            bare_function: CURRENT_TIMESTAMP
+        end_bracket: )
+- statement_terminator: ;
+- statement:
+    transaction_statement:
+      keyword: COMMIT
+- statement_terminator: ;


Code review comments:
diff_hunk: @@ -2321,6 +2321,7 @@ class ColumnConstraintSegment(BaseSegment):
                 OneOf(
                     Ref("LiteralGrammar"),
                     Ref("FunctionSegment"),
+                    Ref("BareFunctionSegment"),
                     # ?? Ref('IntervalExpressionSegment')
path: src/sqlfluff/dialects/dialect_ansi.py
text: @user1:
Not related to the PR, but should we remove this line?

@author:
Yeah let’s do it. Looks like it was added here for BigQuery but didn’t need to be in ANSI: https://github.com/sqlfluff/sqlfluff/pull/221

```suggestion
```