Ejemplo n.º 1
0
    def test_operaotors(self):
        expr = self.compile("SELECT 1 + 1 AS expr")
        self.assertEqual(
            expr,
            qc.EvalQuery([
                qc.EvalTarget(
                    qc.Operator(qp.Add,
                                [qc.EvalConstant(1),
                                 qc.EvalConstant(1)]), 'expr', False)
            ], None, None, None, None, None, None, None))

        expr = self.compile("SELECT 1 + meta('int') AS expr")
        self.assertEqual(
            expr,
            qc.EvalQuery([
                qc.EvalTarget(
                    qc.Operator(qp.Add, [
                        qc.EvalConstant(1),
                        qe.Function('decimal', [
                            qe.Function('meta', [
                                qc.EvalConstant('int'),
                            ]),
                        ]),
                    ]), 'expr', False)
            ], None, None, None, None, None, None, None))
Ejemplo n.º 2
0
 def test_compile_targets_named(self):
     # Test the wildcard expansion.
     query = self.compile("SELECT length(account), account as a, date;")
     self.assertEqual([
         qc.EvalTarget(qe.Function('length', [qe.AccountColumn()]),
                       'length(account)', False),
         qc.EvalTarget(qe.AccountColumn(), 'a', False),
         qc.EvalTarget(qe.DateColumn(), 'date', False)
     ], query.c_targets)
Ejemplo n.º 3
0
    def test_get_columns_and_aggregates(self):
        # Simple column.
        c_query = qe.PositionColumn()
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((1, 0), (len(columns), len(aggregates)))
        self.assertFalse(qc.is_aggregate(c_query))

        # Multiple columns.
        c_query = qc.Operator(qp.And, [qe.PositionColumn(), qe.DateColumn()])
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((2, 0), (len(columns), len(aggregates)))
        self.assertFalse(qc.is_aggregate(c_query))

        # Simple aggregate.
        c_query = qe.SumPosition([qe.PositionColumn()])
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((0, 1), (len(columns), len(aggregates)))
        self.assertTrue(qc.is_aggregate(c_query))

        # Multiple aggregates.
        c_query = qc.Operator(
            qp.And, [qe.First([qe.DateColumn()]),
                     qe.Last([qe.FlagColumn()])])
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((0, 2), (len(columns), len(aggregates)))
        self.assertTrue(qc.is_aggregate(c_query))

        # Simple non-aggregate function.
        c_query = qe.Function('length', [qe.AccountColumn()])
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((1, 0), (len(columns), len(aggregates)))
        self.assertFalse(qc.is_aggregate(c_query))

        # Mix of column and aggregates (this is used to detect this illegal case).
        c_query = qc.Operator(qp.And, [
            qe.Function('length', [qe.AccountColumn()]),
            qe.SumPosition([qe.PositionColumn()]),
        ])
        columns, aggregates = qc.get_columns_and_aggregates(c_query)
        self.assertEqual((1, 1), (len(columns), len(aggregates)))
        self.assertTrue(qc.is_aggregate(c_query))
Ejemplo n.º 4
0
    def test_coalesce(self):
        expr = self.compile(
            "SELECT coalesce(narration, str(date), '~') AS expr")
        self.assertEqual(
            expr,
            qc.EvalQuery([
                qc.EvalTarget(
                    qc.EvalCoalesce([
                        qe.NarrationColumn(),
                        qe.Function('str', [qe.DateColumn()]),
                        qc.EvalConstant('~'),
                    ]), 'expr', False)
            ], None, None, None, None, None, None, None))

        with self.assertRaises(qc.CompilationError):
            self.compile("SELECT coalesce(narration, date, 1)")
Ejemplo n.º 5
0
 def test_compile_Length(self):
     c_length = qe.Function('length', [qc.EvalConstant('testing')])
     self.assertEqual(int, c_length.dtype)