Closed don-tay closed 2 years ago
In postgres, there are 2 cases for ORDER BY
GROUP BY
or DISTINCT
then the sort fields must be found in the GROUP BY
or DISTINCT
field.GROUP BY
or DISTINCT
then can sort on any field in the tables (beyond what is being projected).In terms of implementation, this means 2 cases:
Behaviour above now implemented: Test queries:
SQL> select distinct sname from student order by sid
sname
------
java.lang.RuntimeException: field sid not found.
at simpledb.query.DistinctScan.getVal(DistinctScan.java:101)
at simpledb.materialize.RecordComparator.compare(RecordComparator.java:41)
at simpledb.materialize.SortPlan.splitIntoRuns(SortPlan.java:102)
at simpledb.materialize.SortPlan.open(SortPlan.java:42)
at test.SimpleIJ.doQuery(SimpleIJ.java:69)
at test.SimpleIJ.main(SimpleIJ.java:39)
transaction 2 rolled back
SQL> select sname from student group by sname order by sid
sname
------
java.lang.RuntimeException: field sid not found.
at simpledb.materialize.GroupByScan.getVal(GroupByScan.java:93)
at simpledb.materialize.RecordComparator.compare(RecordComparator.java:41)
at simpledb.materialize.SortPlan.splitIntoRuns(SortPlan.java:102)
at simpledb.materialize.SortPlan.open(SortPlan.java:42)
at simpledb.plan.ProjectPlan.open(ProjectPlan.java:32)
at test.SimpleIJ.doQuery(SimpleIJ.java:69)
at test.SimpleIJ.main(SimpleIJ.java:39)
transaction 2 rolled back
Changes in this PR
Overview
This fixes following query (in #35):