DuckDB Binder错误:FROM子句中未找到引用的列

huangapple go评论65阅读模式
英文:

DuckDB Binder Error: Referenced column not found in FROM clause

问题

我在DuckDB中处理从json中读取的数据库

这是json

```json
[{
    "account": "abcde",
    "data": [
        {
            "name": "hey",
            "amount": 1,
            "flow": "INFLOW"
        },
        {
            "name": "hello",
            "amount": -2,
            "flow": null
        }
    ]
},
{
    "account": "hijkl",
    "data": [
        {
            "name": "bonjour",
            "amount": 1,
            "flow": "INFLOW"
        },
        {
            "name": "hallo",
            "amount": -3,
            "flow": "OUTFLOW"
        }
    ]
}
]

我在Python中这样打开它:

import duckdb

duckdb.sql("""
CREATE OR REPLACE TABLE mytable AS SELECT * FROM "example2.json"
""")

这一切都很顺利,我得到了我的表的副本,但是然后我尝试更新它:

duckdb.sql("""
UPDATE mytable SET data = NULL WHERE account = "abcde"
""")

这导致崩溃:

---------------------------------------------------------------------------
BinderException                           Traceback (most recent call last)
Cell In[109], line 1
----> 1 duckdb.sql("""
      2 UPDATE mytable SET data = NULL WHERE account = "abcde"
      3 """)
      6 # duckdb.sql("""
      7 # DELETE FROM mytable WHERE account = "abcde"
      8 # """)
     10 duckdb.sql("""
     11 SELECT * FROM mytable
     12 """)
BinderException: Binder Error: Referenced column "abcde" not found in FROM clause!
Candidate bindings: "mytable.data"
LINE 2: ...mytable SET data = NULL WHERE account = "abcde"
                                                  ^

我已经搜索了文档和错误,但我就是找不到我在这里做错了什么。


<details>
<summary>英文:</summary>

I am working in DuckDB in a database that I read from json.

Here is the json:

```json
[{
    &quot;account&quot;: &quot;abcde&quot;,
    &quot;data&quot;: [
        {
            &quot;name&quot;: &quot;hey&quot;,
            &quot;amount&quot;:1,
            &quot;flow&quot;:&quot;INFLOW&quot;
        },
        {
            &quot;name&quot;: &quot;hello&quot;,
            &quot;amount&quot;:-2,
            &quot;flow&quot;: null
        }
    ]
},
{
    &quot;account&quot;: &quot;hijkl&quot;,
    &quot;data&quot;: [
        {
            &quot;name&quot;: &quot;bonjour&quot;,
            &quot;amount&quot;:1,
            &quot;flow&quot;:&quot;INFLOW&quot;
        },
        {
            &quot;name&quot;: &quot;hallo&quot;,
            &quot;amount&quot;:-3,
            &quot;flow&quot;:&quot;OUTFLOW&quot;
        }
    ]
}
]

I am opening it in Python as follows:

import duckdb

duckdb.sql(&quot;&quot;&quot;
CREATE OR REPLACE TABLE mytable AS SELECT * FROM &quot;example2.json&quot;
&quot;&quot;&quot;)

This all works fine and I get a copy of my table, but then I try to update it:

duckdb.sql(&quot;&quot;&quot;
UPDATE mytable SET data = NULL WHERE account = &quot;abcde&quot;
&quot;&quot;&quot;)

which crashes with

---------------------------------------------------------------------------
BinderException                           Traceback (most recent call last)
Cell In[109], line 1
----&gt; 1 duckdb.sql(&quot;&quot;&quot;
      2 UPDATE mytable SET data = NULL WHERE account = &quot;abcde&quot;
      3 &quot;&quot;&quot;)
      6 # duckdb.sql(&quot;&quot;&quot;
      7 # DELETE FROM mytable WHERE account = &quot;abcde&quot;
      8 # &quot;&quot;&quot;)
     10 duckdb.sql(&quot;&quot;&quot;
     11 SELECT * FROM mytable
     12 &quot;&quot;&quot;)

BinderException: Binder Error: Referenced column &quot;abcde&quot; not found in FROM clause!
Candidate bindings: &quot;mytable.data&quot;
LINE 2: ...mytable SET data = NULL WHERE account = &quot;abcde&quot;
                                                  ^

I have searched the documentation and the error but I just can't find what I am doing wrong here.

答案1

得分: 1

I actually solved the issue. I had to use single quotes ' instead of double quotes " in the string comparison...

Solution

duckdb.sql('''
UPDATE mytable SET data = NULL WHERE account = 'abcde'
''')

correctly does

┌─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────┐
 account                                                data                                               
 varchar                       struct("name" varchar, amount bigint, flow varchar)[]                       
├─────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
 hijkl    [{'name': bonjour, 'amount': 1, 'flow': INFLOW}, {'name': hallo, 'amount': -3, 'flow': OUTFLOW}] 
 abcde    NULL                                                                                             
└─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────┘

Interestingly, ChatGPT helped me spot this mistake. (There is a ban on posting AI answers, but it's OK if they are human-verified).

英文:

I actually solved the issue. I had to use single quotes &#39; instead of double quotes &quot; in the string comparison...

Solution

duckdb.sql(&quot;&quot;&quot;
UPDATE mytable SET data = NULL WHERE account = &#39;abcde&#39;
&quot;&quot;&quot;)

correctly does

┌─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────┐
 account                                                data                                               
 varchar                       struct(&quot;name&quot; varchar, amount bigint, flow varchar)[]                       
├─────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
 hijkl    [{&#39;name&#39;: bonjour, &#39;amount&#39;: 1, &#39;flow&#39;: INFLOW}, {&#39;name&#39;: hallo, &#39;amount&#39;: -3, &#39;flow&#39;: OUTFLOW}] │
 abcde    NULL                                                                                             
└─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────┘

Interestingly, ChatGPT helped me spot this mistake. (There is a ban on posting AI answers, but it's OK if they are human-verified).

答案2

得分: 0

在处理 DuckDB 中的 varchar 时,请使用单引号 '' 而不是双引号 ""。

英文:

When dealing with varchar in DuckDB, use single quotes '' instead of double quotes "".

huangapple
  • 本文由 发表于 2023年3月8日 17:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定