Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL: UPDATE using subselect fails "subquery must return only one column" #473

Closed
guspower opened this issue Jun 30, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@guspower
Copy link
Contributor

Introduction

A update using subselect query that succeeds when run directly against postgreSQL, and parses successfully using sqlparser, fails to run in SQLPage with error error returned from database: subquery must return only one column.

To Reproduce

DROP TABLE IF EXISTS customer_groups;
DROP TABLE IF EXISTS customers;

CREATE TEMPORARY TABLE customer_groups (
   customer_group_id bigserial NOT NULL
   , activation int
   , deposit int
);

INSERT INTO customer_groups (activation, deposit)
VALUES (100, 100);

CREATE TEMPORARY TABLE customers (
   month int NOT NULL
   , won_customers int
   , total_deposits bigint
);

INSERT INTO customers (month)
    SELECT * FROM generate_series(1,60);

UPDATE customers c SET (won_customers, total_deposits) =
    (SELECT 
        activation
        , activation * deposit        
    FROM
        customer_groups
    WHERE
        customer_group_id = 1
    AND
        c.month = 1
    )
    ; 

SELECT * FROM customers ORDER BY month ASC;

Expectation: a 3 column table output with the first row as 1, 100, 10000

Actual behavior

Failed to execute SQL statement:
UPDATE customers AS c SET won_customers.total_deposits = (SELECT activation, activation * deposit FROM customer_groups WHERE customer_group_id = 1 AND c.month = 1);
                                                          ⬆️
line 1, character 58

error returned from database: subquery must return only one column

You can see that the SET won_customers.total_deposits looks incorrect.
A related example from the postgreSQL docs is:

UPDATE summary s SET (sum_x, sum_y, avg_x, avg_y) =
    (SELECT sum(x), sum(y), avg(x), avg(y) FROM data d
     WHERE d.group_id = s.group_id);

Screenshots

N/A

Expected behavior

The SQL should execute successfully

Version information

  • OS: Linux 6.6.13-gentoo
  • Database: PostgreSQL 16.3 on x86_64-pc-linux-gnu
  • SQLPage Version: v0.24.0

Additional context

I verified that sqlparser is ok with it by modifying the parse example on the README.md

@guspower guspower added the bug Something isn't working label Jun 30, 2024
@lovasoa
Copy link
Collaborator

lovasoa commented Jun 30, 2024

Hello! Thank you for reporting the issue. I think this is a duplicate of #403. I have already submitted a patch for it upstream, and it was accepted, we are just waiting for a new release of sqlparser.

@lovasoa
Copy link
Collaborator

lovasoa commented Jul 1, 2024

The new sqlparser with the fix is scheduled for release today: apache/datafusion-sqlparser-rs#1296

@guspower
Copy link
Contributor Author

guspower commented Jul 1, 2024

That's great news. And of course I should have picked up that while sqlparser did parse the SQL statement without error, what it produced was wrong, i.e. I should have run and inspected

println!("{}", ast[0].to_string());

not just

println!("AST: {:?}", ast);

@lovasoa
Copy link
Collaborator

lovasoa commented Jul 10, 2024

I merged the latest sqlparser !

@lovasoa lovasoa closed this as completed Jul 10, 2024
@guspower
Copy link
Contributor Author

Excellent will test today!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants