Repository: tobymao/sqlglot

Issue:
Empty 'sources' in scope when using `FINAL` modifier in ClickHouse SQL query
```python
import sqlglot
from sqlglot.optimizer import traverse_scope

exp = sqlglot.parse_one('SELECT * FROM x FINAL', read="clickhouse")
scopes = traverse_scope(exp)
print(len(scopes[0].sources))
```

Discussion:

Solution patch:
diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py
index 0bd7ab26f0..6612f6102c 100644
--- a/sqlglot/optimizer/scope.py
+++ b/sqlglot/optimizer/scope.py
@@ -679,6 +679,8 @@ def _traverse_tables(scope):
     expressions.extend(scope.expression.args.get("laterals") or [])
 
     for expression in expressions:
+        if isinstance(expression, exp.Final):
+            expression = expression.this
         if isinstance(expression, exp.Table):
             table_name = expression.name
             source_name = expression.alias_or_name


Test patch:
diff --git a/tests/dialects/test_clickhouse.py b/tests/dialects/test_clickhouse.py
index 3603603fad..56ff06ff4d 100644
--- a/tests/dialects/test_clickhouse.py
+++ b/tests/dialects/test_clickhouse.py
@@ -2,6 +2,7 @@
 from sqlglot import exp, parse_one
 from sqlglot.dialects import ClickHouse
 from sqlglot.expressions import convert
+from sqlglot.optimizer import traverse_scope
 from tests.dialects.test_dialect import Validator
 from sqlglot.errors import ErrorLevel
 
@@ -1123,3 +1124,9 @@ def test_array_join(self):
         self.validate_identity(
             "SELECT * FROM arrays_test ARRAY JOIN [1, 2, 3] AS arr_external1, ['a', 'b', 'c'] AS arr_external2, splitByString(',', 'asd,qwerty,zxc') AS arr_external3"
         )
+
+    def test_traverse_scope(self):
+        sql = "SELECT * FROM t FINAL"
+        scopes = traverse_scope(parse_one(sql, dialect=self.dialect))
+        self.assertEqual(len(scopes), 1)
+        self.assertEqual(set(scopes[0].sources), {"t"})