Skip to content

Commit

Permalink
Fix proper set result field names when using join clause
Browse files Browse the repository at this point in the history
Closes cznic#137
Context cznic#136

This PR make sure the field names for results nested select are set properly.

The observation from postgresql  shows that  for the following sql.

```sql
      BEGIN TRANSACTION;
		CREATE TABLE department (
		    DepartmentID   integer,
			DepartmentName text
		);

		INSERT INTO department (DepartmentID,DepartmentName) VALUES
			(31, 'Sales'),
			(33, 'Engineering'),
			(34, 'Clerical'),
			(35, 'Marketing');

		CREATE TABLE employee (
			LastName     text,
			DepartmentID integer
		);

		INSERT INTO employee VALUES
			('Rafferty', 31),
			('Jones', 33),
			('Heisenberg', 33),
			('Robinson', 34),
			('Smith', 34),
			('Williams', NULL);

     COMMIT;
```

```
gernest=# select * from (select * from employee) as a,(select * from department ) as d  order by d.DepartmentID  desc;
  lastname  | departmentid | departmentid | departmentname
------------+--------------+--------------+----------------
 Rafferty   |           31 |           35 | Marketing
 Robinson   |           34 |           35 | Marketing
 Williams   |              |           35 | Marketing
 Jones      |           33 |           35 | Marketing
 Smith      |           34 |           35 | Marketing
 Heisenberg |           33 |           35 | Marketing
 Jones      |           33 |           34 | Clerical
 Williams   |              |           34 | Clerical
 Robinson   |           34 |           34 | Clerical
 Heisenberg |           33 |           34 | Clerical
 Rafferty   |           31 |           34 | Clerical
 Smith      |           34 |           34 | Clerical
 Smith      |           34 |           33 | Engineering
 Jones      |           33 |           33 | Engineering
 Williams   |              |           33 | Engineering
 Heisenberg |           33 |           33 | Engineering
 Robinson   |           34 |           33 | Engineering
 Rafferty   |           31 |           33 | Engineering
 Robinson   |           34 |           31 | Sales
 Jones      |           33 |           31 | Sales
 Heisenberg |           33 |           31 | Sales
 Rafferty   |           31 |           31 | Sales
 Smith      |           34 |           31 | Sales
 Williams   |              |           31 | Sales
(24 rows)
```

Previously the names were set to empty strings .
  • Loading branch information
gernest committed May 5, 2017
1 parent 3497a9e commit e2cc236
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
65 changes: 65 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"runtime/debug"
"strconv"
Expand Down Expand Up @@ -3501,3 +3502,67 @@ COMMIT;
}
wg.Wait()
}

func TestIssue136(t *testing.T) {
RegisterMemDriver()
db, err := sql.Open("ql-mem", "")
if err != nil {
t.Fatal(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
_, err = tx.Exec(`
create table category (key int, name string);
create table condition (key int, name string);
create table product (key int, catkey int, condkey int);
insert into category values (1, "foo"), (2, "hello");
insert into condition values (1, "bar"), (2, "baz");
insert into product values (1, 1, 1), (2, 2, null), (3, null, 2), (4, null, null);
`)
if err != nil {
t.Fatal(err)
}
err = tx.Commit()
if err != nil {
t.Fatal(err)
}

rows, err := db.Query(`
select *
from
(select
product.key as product_key,
category.name as category_name,
product.condkey as product_condkey
from
product
left join category on category.key == product.catkey)
left join condition on condition.key == product_condkey;
`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
c, _ := rows.Columns()
e := []string{
"product_key",
"category_name",
"product_condkey",
"condition.key",
"condition.name"}
if !reflect.DeepEqual(e, c) {
t.Errorf("expected %v got %v", e, c)
}
for rows.Next() {
var pk, pck, ck sql.NullInt64
var cn, cndn sql.NullString
err = rows.Scan(&pk, &cn, &pck, &ck, &cndn)
if err != nil {
t.Error(err)
}
}
}
13 changes: 4 additions & 9 deletions ql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1765,23 +1765,18 @@ func (r *joinRset) plan(ctx *execCtx) (plan, error) {
if f != "" && nm != "" {
f = fmt.Sprintf("%s.%s", nm, f)
}
if nm == "" {
f = ""
}
fields = append(fields, f)
}
}
rsets[i] = q
}

if len(rsets) == 1 {
return rsets[0], nil
}

if len(rsets) == 0 {
switch len(rsets) {
case 0:
return nil, nil
case 1:
return rsets[0], nil
}

right := len(rsets[len(rsets)-1].fieldNames())
switch r.typ {
case crossJoin:
Expand Down
18 changes: 9 additions & 9 deletions testdata.log
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ SELECT * FROM (SELECT * FROM employee;), (SELECT * FROM department;);
│ └Output field names ["LastName" "DepartmentID"]
│ ┌Iterate all rows of table "department"
│ └Output field names ["DepartmentID" "DepartmentName"]
└Output field names ["" "" "" ""]
└Output field names ["LastName" "DepartmentID" "DepartmentID" "DepartmentName"]

---- 75
SELECT * FROM (SELECT * FROM employee;) AS e, (SELECT * FROM department;) ORDER BY e.LastName, e.DepartmentID;
Expand All @@ -413,9 +413,9 @@ SELECT * FROM (SELECT * FROM employee;) AS e, (SELECT * FROM department;) ORDER
│ └Output field names ["LastName" "DepartmentID"]
│ ┌Iterate all rows of table "department"
│ └Output field names ["DepartmentID" "DepartmentName"]
└Output field names ["e.LastName" "e.DepartmentID" "" ""]
└Output field names ["e.LastName" "e.DepartmentID" "DepartmentID" "DepartmentName"]
┌Order by e.LastName, e.DepartmentID,
└Output field names ["e.LastName" "e.DepartmentID" "" ""]
└Output field names ["e.LastName" "e.DepartmentID" "DepartmentID" "DepartmentName"]

---- 76
SELECT * FROM (SELECT * FROM employee;), (SELECT * FROM department;) AS d ORDER BY d.DepartmentID DESC;
Expand All @@ -424,9 +424,9 @@ SELECT * FROM (SELECT * FROM employee;), (SELECT * FROM department;) AS d ORDER
│ └Output field names ["LastName" "DepartmentID"]
│ ┌Iterate all rows of table "department"
│ └Output field names ["DepartmentID" "DepartmentName"]
└Output field names ["" "" "d.DepartmentID" "d.DepartmentName"]
└Output field names ["LastName" "DepartmentID" "d.DepartmentID" "d.DepartmentName"]
┌Order descending by d.DepartmentID,
└Output field names ["" "" "d.DepartmentID" "d.DepartmentName"]
└Output field names ["LastName" "DepartmentID" "d.DepartmentID" "d.DepartmentName"]

---- 77
SELECT * FROM employee, (SELECT * FROM department;) ORDER BY employee.LastName;
Expand All @@ -435,9 +435,9 @@ SELECT * FROM employee, (SELECT * FROM department;) ORDER BY employee.LastName;
│ └Output field names ["LastName" "DepartmentID"]
│ ┌Iterate all rows of table "department"
│ └Output field names ["DepartmentID" "DepartmentName"]
└Output field names ["employee.LastName" "employee.DepartmentID" "" ""]
└Output field names ["employee.LastName" "employee.DepartmentID" "DepartmentID" "DepartmentName"]
┌Order by employee.LastName,
└Output field names ["employee.LastName" "employee.DepartmentID" "" ""]
└Output field names ["employee.LastName" "employee.DepartmentID" "DepartmentID" "DepartmentName"]

---- 78
SELECT * FROM (SELECT * FROM employee;) AS e, (SELECT * FROM department;) AS d WHERE e.DepartmentID == d.DepartmentID ORDER BY d.DepartmentName, e.LastName;
Expand Down Expand Up @@ -879,9 +879,9 @@ SELECT * FROM employee AS e, (SELECT * FROM department;) ORDER BY e.LastName;
│ └Output field names ["LastName" "DepartmentID"]
│ ┌Iterate all rows of table "department"
│ └Output field names ["DepartmentID" "DepartmentName"]
└Output field names ["e.LastName" "e.DepartmentID" "" ""]
└Output field names ["e.LastName" "e.DepartmentID" "DepartmentID" "DepartmentName"]
┌Order by e.LastName,
└Output field names ["e.LastName" "e.DepartmentID" "" ""]
└Output field names ["e.LastName" "e.DepartmentID" "DepartmentID" "DepartmentName"]

---- 155
SELECT * FROM employee AS e, (SELECT * FROM department;) AS d ORDER BY e.LastName;
Expand Down
10 changes: 5 additions & 5 deletions testdata.ql
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ FROM
SELECT *
FROM department
);
|"", "", "", ""
|"LastName", "DepartmentID", "DepartmentID", "DepartmentName"
[Williams <nil> 35 Marketing]
[Williams <nil> 34 Clerical]
[Williams <nil> 33 Engineering]
Expand Down Expand Up @@ -849,7 +849,7 @@ FROM
FROM department
)
ORDER BY e.LastName, e.DepartmentID;
|"e.LastName", "e.DepartmentID", "", ""
|"e.LastName", "e.DepartmentID", "DepartmentID", "DepartmentName"
[Heisenberg 33 35 Marketing]
[Heisenberg 33 34 Clerical]
[Heisenberg 33 33 Engineering]
Expand Down Expand Up @@ -887,7 +887,7 @@ FROM
FROM department
) AS d
ORDER BY d.DepartmentID DESC;
|"", "", "d.DepartmentID", "d.DepartmentName"
|"LastName", "DepartmentID", "d.DepartmentID", "d.DepartmentName"
[Rafferty 31 35 Marketing]
[Jones 33 35 Marketing]
[Heisenberg 33 35 Marketing]
Expand Down Expand Up @@ -922,7 +922,7 @@ FROM
FROM department
)
ORDER BY employee.LastName;
|"employee.LastName", "employee.DepartmentID", "", ""
|"employee.LastName", "employee.DepartmentID", "DepartmentID", "DepartmentName"
[Heisenberg 33 35 Marketing]
[Heisenberg 33 34 Clerical]
[Heisenberg 33 33 Engineering]
Expand Down Expand Up @@ -1654,7 +1654,7 @@ FROM
employee AS e,
( SELECT * FROM department)
ORDER BY e.LastName;
|"e.LastName", "e.DepartmentID", "", ""
|"e.LastName", "e.DepartmentID", "DepartmentID", "DepartmentName"
[Heisenberg 33 35 Marketing]
[Heisenberg 33 34 Clerical]
[Heisenberg 33 33 Engineering]
Expand Down

0 comments on commit e2cc236

Please sign in to comment.