Beispiel #1
0
 def test_case(self):
     self.assert_compiled_select(
         'SELECT CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 END',
         typed_ast.Select(
             select_fields=[
                 typed_ast.SelectField(
                     typed_ast.FunctionCall(
                         runtime.get_func('if'),
                     [
                         typed_ast.Literal(True, tq_types.BOOL),
                         typed_ast.Literal(1, tq_types.INT),
                         typed_ast.FunctionCall(
                             runtime.get_func('if'),
                             [
                                 typed_ast.Literal(False, tq_types.BOOL),
                                 typed_ast.Literal(2, tq_types.INT),
                                 typed_ast.Literal(None, tq_types.NONETYPE),
                             ],
                             tq_types.INT)
                     ],
                     tq_types.INT),
                 'f0_', None)
             ],
             table=typed_ast.NoTable(),
             where_expr=typed_ast.Literal(True, tq_types.BOOL),
             group_set=None,
             having_expr=typed_ast.Literal(True, tq_types.BOOL),
             orderings=None,
             limit=None,
             type_ctx=self.make_type_context(
                 [(None, 'f0_', tq_types.INT)],
                 self.make_type_context([]))))
Beispiel #2
0
 def test_unary_operator(self):
     self.assert_compiled_select(
         'SELECT -5',
         typed_ast.Select(
             [typed_ast.SelectField(
                 typed_ast.FunctionCall(
                     runtime.get_unary_op('-'),
                     [typed_ast.Literal(5, tq_types.INT)],
                     tq_types.INT),
                 'f0_'
             )],
             typed_ast.NoTable(),
             typed_ast.Literal(True, tq_types.BOOL),
             None,
             None,
             self.make_type_context(
                 [(None, 'f0_', tq_types.INT)],
                 self.make_type_context([]))
         )
     )
Beispiel #3
0
    def compile_table_expr(self, table_expr):
        """Compile a table expression and determine its result type context.

        Arguments:
            table_expr: Either None (indicating that there no table being
                selected or a TableId.

        Returns: A typed_ast.TableExpression.
        """
        if table_expr is None:
            return typed_ast.NoTable()
        else:
            try:
                method = getattr(
                    self,
                    'compile_table_expr_' + table_expr.__class__.__name__)
            except AttributeError:
                raise NotImplementedError('Missing handler for type {}'.format(
                    table_expr.__class__.__name__))
            return method(table_expr)
Beispiel #4
0
 def test_function_calls(self):
     self.assert_compiled_select(
         'SELECT ABS(-3), POW(2, 3), NOW()',
         typed_ast.Select([
             typed_ast.SelectField(
                 typed_ast.FunctionCall(
                     runtime.get_func('abs'),
                     [typed_ast.FunctionCall(
                         runtime.get_unary_op('-'),
                         [typed_ast.Literal(3, tq_types.INT)],
                         tq_types.INT
                     )],
                     tq_types.INT),
                 'f0_', None),
             typed_ast.SelectField(
                 typed_ast.FunctionCall(
                     runtime.get_func('pow'), [
                         typed_ast.Literal(2, tq_types.INT),
                         typed_ast.Literal(3, tq_types.INT)],
                     tq_types.INT
                 ),
                 'f1_', None
             ),
             typed_ast.SelectField(
                 typed_ast.FunctionCall(
                     runtime.get_func('now'), [], tq_types.INT
                 ),
                 'f2_', None
             )],
             typed_ast.NoTable(),
             typed_ast.Literal(True, tq_types.BOOL),
             None,
             typed_ast.Literal(True, tq_types.BOOL),
             None,
             None,
             self.make_type_context([
                 (None, 'f0_', tq_types.INT), (None, 'f1_', tq_types.INT),
                 (None, 'f2_', tq_types.INT)],
                 self.make_type_context([]))
         )
     )