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

Nested SubQueries do not work as expected #1924

Open
bc-saranya opened this issue May 22, 2024 · 8 comments
Open

Nested SubQueries do not work as expected #1924

bc-saranya opened this issue May 22, 2024 · 8 comments

Comments

@bc-saranya
Copy link

bc-saranya commented May 22, 2024

Description -
Nested subqueries leads to an error TypeError: Cannot read properties of undefined (reading '0')

I have created three tables like below:

alasql("CREATE TABLE cities (city string, population number)");
alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424),  ('Madrid',3041579),('Easingwold',4627)");

alasql("CREATE TABLE countries (name string, population number, city string)");
alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");

alasql("CREATE TABLE population (number int)");
alasql("INSERT INTO population VALUES (89764679009), (165247191)");

I use a SELECT query with nested subqueries, e.g -

SELECT * FROM cities WHERE city IN (SELECT DISTINCT city FROM countries WHERE population IN (SELECT DISTINCT number from population))

Expected output - Rome, Paris

Actual Output - TypeError: Cannot read properties of undefined (reading '0')

@SuvitsonHarrese
Copy link

SuvitsonHarrese commented Jun 25, 2024

alasql("CREATE TABLE cities (city string, population number)");
alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424), ('Madrid',3041579),('Easingwold',4627)");

alasql("CREATE TABLE countries (name string, population number, city string)");
^^^^^^ -> population column is declared as number which is not a valid type replcaing it with int has yield the expected output

alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");

alasql("CREATE TABLE population (number int)");
alasql("INSERT INTO population VALUES (89764679009), (165247191)");

SELECT * FROM cities WHERE city IN (SELECT DISTINCT city FROM countries WHERE population IN (SELECT DISTINCT number from population))

@paulrutter
Copy link
Contributor

paulrutter commented Jun 30, 2024

@SuvitsonHarrese https://github.com/AlaSQL/alasql#traditional-sql-table does show number as possible column value though.

@bc-saranya we can check this out.

It would be nice if the parser would throw an error on unknown data types though.

Somewhere here probably

if (this.dbtypeid) {

@paulrutter
Copy link
Contributor

@SuvitsonHarrese I tried by changing the data type to int, but still the same error occurs.
What did you try to resolve it?

@SuvitsonHarrese
Copy link

yeah now i checked again I got the exact same error
dont know how it worked on that day

@SuvitsonHarrese
Copy link

SuvitsonHarrese commented Jun 30, 2024

@paulrutter i think the problem is with the nesting in subqueries that means a query can have one subquery and no subquery within subquery of main query

alasql("CREATE TABLE t1 (points int)");
alasql("INSERT INTO  t1 (1),(2),(3),(4)");

alasql("CREATE TABLE t2 (height int)");

alasql("INSERT INTO t2 VALUES (2), (3), (4)");

alasql("CREATE TABLE t3 (breadth int)");
alasql("INSERT INTO t3 VALUES (3), (4)");
console.log(
  alasql("SELECT * FROM t2 WHERE height IN (SELECT breadth from t3)")
);

console.log(alasql("SELECT * FROM t2 WHERE height IN (SELECT points from t1)"));

console.log(
  alasql("SELECT * FROM t3 WHERE breadth IN (SELECT points from t1)")
);
console.log(
  alasql("SELECT * FROM t3 WHERE breadth IN (SELECT breadth from t3)")
);

console.log(
  alasql(
    "SELECT * FROM t3 WHERE breadth IN (SELECT * FROM t3 WHERE breadth IN (SELECT breadth from t3))"
  )
);
// last line of code is returning the same error whereas other query with different combination of subqueries are working fine

@choia4
Copy link

choia4 commented Jul 17, 2024

hi, I was wondering if this issue got resolved. If not, can I pick it up?

@paulrutter
Copy link
Contributor

It's not resolved as far as i know, so you can work on it for sure. @choia4

@znenuko
Copy link

znenuko commented Aug 21, 2024

Verify Subqueries Individually:

Subquery 1:
SELECT DISTINCT city FROM countries WHERE population IN (
    SELECT DISTINCT number FROM population
);

Ensure this subquery returns expected city values.

Subquery 2:

SELECT DISTINCT number FROM population;

Ensure this subquery returns the correct numbers.

Execute the Main Query:

With verified subqueries, combine them into the main query:

alasql("CREATE TABLE cities (city string, population number)");
alasql("INSERT INTO cities VALUES ('Rome',2863223), ('Paris',2249975), ('Berlin',3517424),  ('Madrid',3041579),('Easingwold',4627)");

alasql("CREATE TABLE countries (name string, population number, city string)");
alasql("INSERT INTO countries VALUES ('Italy', 89764679009, 'Rome'), ('France', 165247191, 'Paris'), ('Germany', 346186257, 'Berlin')");

alasql("CREATE TABLE population (number int)");
alasql("INSERT INTO population VALUES (89764679009), (165247191)");

// Execute the query
let result = alasql(`
    SELECT * FROM cities WHERE city IN (
        SELECT DISTINCT city FROM countries WHERE population IN (
            SELECT DISTINCT number FROM population
        )
    )
`);

console.log(result);  // Expected to show 'Rome' and 'Paris'

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

6 participants