Repository: tobymao/sqlglot

Issue:
List index out of range for Snowflake SQL transpilation
Ran the issue using the latest `main` build `sqlglot-19.0.4.dev18`

Read: `snowflake`
Write `spark` 

When running the following query on Snowflake's publically available documentation, we get an `IndexError: list index out of range`. It's unclear which part of the query is unable to be tokenized. 

Code snippet 

https://docs.snowflake.com/en/sql-reference/sql/create-external-table

```
query_1666 = 'SELECT metadata$filename FROM @s1/;'

transpiled_sqls = transpile(query_1666, read='snowflake', write='spark', pretty=True, error_level=None)
```


Discussion:
georgesittas: This doesn't fix the transpilation issue when targeting Spark because it doesn't support unquoted identifiers that contain `$`:

```
spark-sql (default)> select 1 as a$b;

[PARSE_SYNTAX_ERROR] Syntax error at or near '$'.(line 1, pos 13)

== SQL ==
select 1 as a$b
-------------^^^
```

This is a pain because we need to either change the tokenizer representation of `a$b` or add an expensive check for `Var` to see if there's `$` in its name in order to preserve that info for transpilation.

cc @tobymao
tobymao: ok for now

Patch:
diff --git a/sqlglot/dialects/snowflake.py b/sqlglot/dialects/snowflake.py
index 01f7512a..62b1cd0c 100644
--- a/sqlglot/dialects/snowflake.py
+++ b/sqlglot/dialects/snowflake.py
@@ -349,7 +349,7 @@ class Snowflake(Dialect):
             table: t.Optional[exp.Expression] = None
             if self._match_text_seq("@"):
                 table_name = "@"
-                while True:
+                while self._curr:
                     self._advance()
                     table_name += self._prev.text
                     if not self._match_set(self.STAGED_FILE_SINGLE_TOKENS, advance=False):


Test patch:
diff --git a/tests/dialects/test_snowflake.py b/tests/dialects/test_snowflake.py
index ede42f47..7452a8c9 100644
--- a/tests/dialects/test_snowflake.py
+++ b/tests/dialects/test_snowflake.py
@@ -552,6 +552,7 @@ class TestSnowflake(Validator):
             staged_file.sql(dialect="snowflake"),
         )
 
+        self.validate_identity("SELECT metadata$filename FROM @s1/")
         self.validate_identity("SELECT * FROM @~")
         self.validate_identity("SELECT * FROM @~/some/path/to/file.csv")
         self.validate_identity("SELECT * FROM @mystage")