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

UNION error when using same columns and add CORRESPONDING option #75

Closed
mathiasrw opened this issue Mar 4, 2015 · 12 comments
Closed

UNION error when using same columns and add CORRESPONDING option #75

mathiasrw opened this issue Mar 4, 2015 · 12 comments

Comments

@mathiasrw
Copy link
Member

When I UNION a table I expect that what I select from the second table to be unioned with the first table based on the order

CREATE TABLE t1(a int,b int); 
INSERT INTO t1 VALUES(1,2); 
SELECT a, b FROM t1 UNION  SELECT b, a FROM t1;

in http://alasql.org/console/ I would expect

#   a   b   
1   1   2
2   2   1

but I get

#   b   a
1   2   1

The issue can also be seen for statements like

SELECT 1, 2 UNION SELECT 3, 4;
@mathiasrw mathiasrw reopened this Mar 4, 2015
@agershun agershun added the ! Bug label Mar 5, 2015
@agershun
Copy link
Member

I think that the problem in my wrong understanding of UNION operator.
I will redesign it in the way, like from your example:

    CREATE TABLE t1(a int,b int); 
    INSERT INTO t1 VALUES(1,2);  
    SELECT a, b FROM t1 UNION  SELECT b, a FROM t1;
    -- gives
    #   a   b   
    1   1   2
    2   2   1

@mathiasrw
Copy link
Member Author

👍

@mathiasrw
Copy link
Member Author

Is this on the road map?

@agershun
Copy link
Member

agershun commented Apr 6, 2015

... yes. I will do it ASAP. but not for 100% for current version, because Alasql does not keep order of columns in the record. So, it is impossible to predict results of this union operation:

    SELECT * FROM one UNION SELECT * FROM two

For Alasql2 it should be ok (I want to keep internal data in the format of array instead of object).

@agershun
Copy link
Member

It is hard to fix this...

@mathiasrw
Copy link
Member Author

is it because we do not track the original order?

@agershun
Copy link
Member

I need to think how to convert merging arrays. Probably as a first step AlaSQL need to convert them to array of arrays, then merge, and then convert to array of objects back

@pietersv
Copy link
Collaborator

Whoa -- AlaSQL pays attention to the column names and matches columns when stacking tables vertically without worrying about order? For me that is an awesome capability, which stats packages like SAS and SPSS offer, so would love to keep it personally. But I believe that @mathiasrw is correct that SQL typically stacks only by column order and ignores the name, which is my only big complaint with SQL.

http://stackoverflow.com/questions/5201653/union-tables-by-field-name-i-e-columns-order-cannot-be-guaranteed

@agershun
Copy link
Member

Hmm... I like this CORRESPONDING notation (thank you, Peter!).

"These forty years now, I've been speaking in prose without knowing it! "
(The Bourgeois Gentleman, Moliere)

At least we can keep for compatibility two versions of UNION/UNION ALL/INTERSECT/EXCEPT operators: one with keeping order and one - based on column names. Like:

    SELECT a,b FROM one UNION ALL SELECT b,a FROM one
    -- gives
    -- a b
    -- 1 2
    -- 2 1

and

    SELECT a,b FROM one UNION ALL CORRESPONDING  SELECT b,a FROM one
    -- gives
    -- a b
    -- 1 2
    -- 1 2

@agershun agershun changed the title UNION error when using same columns UNION error when using same columns and add CORRESPONDING option Apr 15, 2015
@mathiasrw
Copy link
Member Author

Love the CORRESPONDING notation... 👍

@agershun
Copy link
Member

agershun commented May 3, 2015

I FIXED THIS!!! See test254.js. Now we can release 0.1!!!

@mathiasrw
Copy link
Member Author

Yayyyyy :)

so so awesome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants