Repository: tobymao/sqlglot

Issue:
Column Reference Not Recognized in `IN` Operator with DuckDB Dialect
Hi there! I hope you're doing well! 😊

I think I might have found a small issue with `sqlglot` when handling column references in the `IN` operator while using the DuckDB dialect. I'm currently using `sqlglot` version 25.24.5.

Here's a quick example: I create a table called `foo` and insert some values into the `flags` column.

```sql
create table foo (flags varchar[]);
insert into foo values (['red', 'green']);
select * from foo where 'red' in flags;
┌──────────────┐
│    flags     │
│  varchar[]   │
├──────────────┤
│ [red, green] │
└──────────────┘
```

The `flags` field is of type `VARCHAR[]`, but when I run this in Python:

```python
import sqlglot

statement = sqlglot.parse_one("SELECT * from foo where 'red' in flags")

print(repr(statement))
```

I get this result:

```
Select(
  expressions=[
    Star()],
  from=From(
    this=Table(
      this=Identifier(this=foo, quoted=False))),
  where=Where(
    this=In(
      this=Literal(this=red, is_string=True),
      field=Identifier(this=flags, quoted=False))))
```

It seems like `sqlglot` is interpreting `flags` as an identifier instead of a column reference. When I supply a schema for the table there is no change in the parse tree.

Discussion:
@rustyconover: Thank you @georgesittas 

Solution patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 46f8836831..36b1310086 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -4477,7 +4477,11 @@ def _parse_in(self, this: t.Optional[exp.Expression], alias: bool = False) -> ex
             elif not self._match(TokenType.R_BRACKET, expression=this):
                 self.raise_error("Expecting ]")
         else:
-            this = self.expression(exp.In, this=this, field=self._parse_field())
+            field = self._parse_field()
+            if isinstance(field, exp.Identifier):
+                field = exp.Column(this=field)
+
+            this = self.expression(exp.In, this=this, field=field)
 
         return this
 


Test patch:
diff --git a/tests/dialects/test_duckdb.py b/tests/dialects/test_duckdb.py
index 6b58934ccc..02b58b24f8 100644
--- a/tests/dialects/test_duckdb.py
+++ b/tests/dialects/test_duckdb.py
@@ -256,6 +256,7 @@ def test_duckdb(self):
             parse_one("a // b", read="duckdb").assert_is(exp.IntDiv).sql(dialect="duckdb"), "a // b"
         )
 
+        self.validate_identity("'red' IN flags").args["field"].assert_is(exp.Column)
         self.validate_identity("CREATE TABLE tbl1 (u UNION(num INT, str TEXT))")
         self.validate_identity("INSERT INTO x BY NAME SELECT 1 AS y")
         self.validate_identity("SELECT 1 AS x UNION ALL BY NAME SELECT 2 AS x")