Kirill Simonov avatar Kirill Simonov committed 6f4ee3c

Fixed a bug where `rewrite()` is not always applied to the base of an ordered space.

Thanks to Bricklen for the bug report.

Comments (0)

Files changed (7)

src/htsql/tr/rewrite.py

             order = [(self.state.rewrite(code, mask=self.space.base),
                       direction)
                      for code, direction in self.space.order]
-        if not self.space.is_expanding:
-            return self.space.clone(order=order)
-        base = self.state.rewrite(self.space.base)
+        if self.space.is_expanding:
+            base = self.state.rewrite(self.space.base)
+        else:
+            base = self.state.rewrite(self.space.base, mask=self.space.scalar)
         return self.space.clone(base=base, order=order)
 
 

test/regress/input/translation.yaml

       skip: true
     - uri: /(school^{true()})
       skip: true
+    # A bug `rewrite` is not applied to `base` of an ordered space.
+    - uri: /((program?school='art')^degree).limit(1)
 

test/regress/output/mssql.yaml

                FROM (SELECT 1 AS [^]
                      FROM [ad].[program] AS [program]
                      GROUP BY [program].[degree]) AS [program]
+          - uri: /((program?school='art')^degree).limit(1)
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |2
+               | ((program?school='art')^degree).limit(1) |
+              -+------------------------------------------+-
+               | degree                                   |
+              -+------------------------------------------+-
+               | ba                                       |
+                                                    (1 row)
+
+               ----
+               /((program?school='art')^degree).limit(1)
+               SELECT TOP 1
+                      [program].[degree]
+               FROM [ad].[program] AS [program]
+               WHERE ([program].[school] = 'art')
+               GROUP BY [program].[degree]
+               ORDER BY 1 ASC
   - include: test/regress/input/format.yaml
     output:
       id: format

test/regress/output/mysql.yaml

                FROM (SELECT TRUE AS `^`
                      FROM `htsql_regress`.`program` AS `program`
                      GROUP BY `program`.`degree`) AS `program`
+          - uri: /((program?school='art')^degree).limit(1)
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |2
+               | ((program?school='art')^degree).limit(1) |
+              -+------------------------------------------+-
+               | degree                                   |
+              -+------------------------------------------+-
+               | ba                                       |
+                                                    (1 row)
+
+               ----
+               /((program?school='art')^degree).limit(1)
+               SELECT `program`.`degree`
+               FROM `htsql_regress`.`program` AS `program`
+               WHERE (`program`.`school` = 'art')
+               GROUP BY 1
+               ORDER BY 1 ASC
+               LIMIT 1
   - include: test/regress/input/format.yaml
     output:
       id: format

test/regress/output/oracle.yaml

                FROM (SELECT 1 AS "^"
                      FROM "HTSQL_REGRESS"."PROGRAM" "PROGRAM"
                      GROUP BY "PROGRAM"."DEGREE") "PROGRAM"
+          - uri: /((program?school='art')^degree).limit(1)
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |2
+               | ((program?school='art')^degree).limit(1) |
+              -+------------------------------------------+-
+               | degree                                   |
+              -+------------------------------------------+-
+               | ba                                       |
+                                                    (1 row)
+
+               ----
+               /((program?school='art')^degree).limit(1)
+               SELECT "PROGRAM"."DEGREE"
+               FROM (SELECT "PROGRAM"."DEGREE"
+                     FROM (SELECT "PROGRAM"."DEGREE"
+                           FROM "HTSQL_REGRESS"."PROGRAM" "PROGRAM"
+                           WHERE ("PROGRAM"."SCHOOL" = 'art')
+                           GROUP BY "PROGRAM"."DEGREE"
+                           ORDER BY 1 ASC NULLS FIRST) "PROGRAM"
+                     WHERE (ROWNUM < 2)) "PROGRAM"
+               ORDER BY 1 ASC NULLS FIRST
   - include: test/regress/input/format.yaml
     output:
       id: format

test/regress/output/pgsql.yaml

                FROM (SELECT TRUE AS "^"
                      FROM "ad"."program" AS "program"
                      GROUP BY "program"."degree") AS "program"
+          - uri: /((program?school='art')^degree).limit(1)
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |2
+               | ((program?school='art')^degree).limit(1) |
+              -+------------------------------------------+-
+               | degree                                   |
+              -+------------------------------------------+-
+               | ba                                       |
+                                                    (1 row)
+
+               ----
+               /((program?school='art')^degree).limit(1)
+               SELECT "program"."degree"
+               FROM "ad"."program" AS "program"
+               WHERE ("program"."school" = 'art')
+               GROUP BY 1
+               ORDER BY 1 ASC NULLS FIRST
+               LIMIT 1
   - include: test/regress/input/format.yaml
     output:
       id: format

test/regress/output/sqlite.yaml

                FROM (SELECT 1 AS "^"
                      FROM "program" AS "program"
                      GROUP BY "program"."degree") AS "program"
+          - uri: /((program?school='art')^degree).limit(1)
+            status: 200 OK
+            headers:
+            - [Content-Type, text/plain; charset=UTF-8]
+            body: |2
+               | ((program?school='art')^degree).limit(1) |
+              -+------------------------------------------+-
+               | degree                                   |
+              -+------------------------------------------+-
+               | ba                                       |
+                                                    (1 row)
+
+               ----
+               /((program?school='art')^degree).limit(1)
+               SELECT "program"."degree"
+               FROM "program" AS "program"
+               WHERE ("program"."school" = 'art')
+               GROUP BY 1
+               ORDER BY 1 ASC
+               LIMIT 1
   - include: test/regress/input/format.yaml
     output:
       id: format
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.