Commits

Kirill Simonov  committed 2a917d5

Replaced `div` function with a division operator.

  • Participants
  • Parent commits a7df387

Comments (0)

Files changed (4)

File src/htsql/tr/fn/function.py

 
 class DivisionOperator(ProperFunction):
 
-    #adapts(named['_/_'])
-    adapts(named['div'])
+    adapts(named['_/_'])
 
     parameters = [
             Parameter('left'),

File src/htsql/tr/parser.py

                                          expression )?
 
         expression      ::= term ( ( '+' | '-' ) term )*
-        term            ::= factor ( ( '*' | identifier ) factor )*
+        term            ::= factor ( ( '*' | '/' | identifier ) factor )*
         factor          ::= ( '+' | '-' ) factor | power
         power           ::= sieve ( '^' power )?
 
     @classmethod
     def process(cls, tokens):
         # Parses the production:
-        #   term            ::= factor ( ( '*' | identifier ) factor )*
+        #   term            ::= factor ( ( '*' | '/' | identifier ) factor )*
         expression = FactorParser << tokens
-        while (tokens.peek(SymbolToken, ['*'])
+        while (tokens.peek(SymbolToken, ['*', '/'])
                or tokens.peek(NameToken)):
-            if tokens.peek(SymbolToken, ['*']):
-                symbol_token = tokens.pop(SymbolToken, ['*'])
+            if tokens.peek(SymbolToken, ['*', '/']):
+                symbol_token = tokens.pop(SymbolToken, ['*', '/'])
                 symbol = symbol_token.value
                 left = expression
                 right = FactorParser << tokens

File test/input/pgsql.yaml

         # Multiplication.
         - uri: /{5*5,5*5.0,5*5e0,5.0*5.0,5.0*5e0,5e0*5e0}
         # Division.
-        - uri: /{1 div 2,1 div 2.0,1 div 2e0,1.0 div 2.0,1.0 div 2e0, 1e0 div 2e0}
+        - uri: /{1/2,1/2.0,1/2e0,1.0/2.0,1.0/2e0, 1e0/2e0}
         # Division by zero.
-        - uri: /{1 div 0}
+        - uri: /{1/0}
           expect: 409
         # Addition: invalid types.
         - uri: /{1+'1'}

File test/output/pgsql.yaml

              ----
              /{5*5,5*5.0,5*5e0,5.0*5.0,5.0*5e0,5e0*5e0}
              SELECT (5 * 5), (CAST(5 AS NUMERIC) * 5.0), (CAST(5 AS FLOAT) * 5.0::float8), (5.0 * 5.0), (CAST(5.0 AS FLOAT) * 5.0::float8), (5.0::float8 * 5.0::float8)
-        - uri: /{1 div 2,1 div 2.0,1 div 2e0,1.0 div 2.0,1.0 div 2e0, 1e0 div 2e0}
+        - uri: /{1/2,1/2.0,1/2e0,1.0/2.0,1.0/2e0, 1e0/2e0}
           status: 200 OK
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |2
-             | /{1 div 2,1 div 2.0,1 div 2e0,1.0 div 2.0,1.0 div 2e0,1e0 div 2e0}                                               |
-            -+------------------------------------------------------------------------------------------------------------------+-
-             | 1 div 2                | 1 div 2.0              | 1 div 2e0 | 1.0 div 2.0            | 1.0 div 2e0 | 1e0 div 2e0 |
-            -+------------------------+------------------------+-----------+------------------------+-------------+-------------+-
-             | 0.50000000000000000000 | 0.50000000000000000000 |       0.5 | 0.50000000000000000000 |         0.5 |         0.5 |
-                                                                                                                          (1 row)
+             | /{1/2,1/2.0,1/2e0,1.0/2.0,1.0/2e0,1e0/2e0}                                                           |
+            -+------------------------------------------------------------------------------------------------------+-
+             | 1/2                    | 1/2.0                  | 1/2e0 | 1.0/2.0                | 1.0/2e0 | 1e0/2e0 |
+            -+------------------------+------------------------+-------+------------------------+---------+---------+-
+             | 0.50000000000000000000 | 0.50000000000000000000 |   0.5 | 0.50000000000000000000 |     0.5 |     0.5 |
+                                                                                                              (1 row)
 
              ----
-             /{1 div 2,1 div 2.0,1 div 2e0,1.0 div 2.0,1.0 div 2e0,1e0 div 2e0}
+             /{1/2,1/2.0,1/2e0,1.0/2.0,1.0/2e0,1e0/2e0}
              SELECT (CAST(1 AS NUMERIC) / CAST(2 AS NUMERIC)), (CAST(1 AS NUMERIC) / 2.0), (CAST(1 AS FLOAT) / 2.0::float8), (1.0 / 2.0), (CAST(1.0 AS FLOAT) / 2.0::float8), (1.0::float8 / 2.0::float8)
-        - uri: /{1 div 0}
+        - uri: /{1/0}
           status: 409 Conflict
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
             engine failure: error while executing 'SELECT (CAST(1 AS NUMERIC) / CAST(0 AS NUMERIC))': division by zero
             :
-                /{1 div 0}
-                ^^^^^^^^^^
+                /{1/0}
+                ^^^^^^
         - uri: /{1+'1'}
           status: 400 Bad Request
           headers: