Ejemplo n.º 1
0
 def test_expr_binaryop(self):
     self.assertEqual(
         qc.EvalEqual(qe.DateColumn(),
                      qc.EvalConstant(datetime.date(2014, 1, 1))),
         qc.compile_expression(
             qp.Equal(qp.Column('date'),
                      qp.Constant(datetime.date(2014, 1, 1))),
             qe.TargetsEnvironment()))
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.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_is_aggregate_derived(self):
        columns, aggregates = qc.get_columns_and_aggregates(
            qc.Operator(qp.And, [
                qc.Operator(qp.Equal, [
                    qe.LineNoColumn(),
                    qc.EvalConstant(42),
                ]),
                qc.Operator(qp.Or, [
                    qc.Operator(qp.Not, [
                        qc.Operator(qp.Equal, [
                            qe.DateColumn(),
                            qc.EvalConstant(datetime.date(2014, 1, 1)),
                        ]),
                    ]),
                    qc.EvalConstant(False),
                ]),
            ]))
        self.assertEqual((2, 0), (len(columns), len(aggregates)))

        columns, aggregates = qc.get_columns_and_aggregates(
            qc.Operator(
                qp.And,
                [
                    qc.Operator(qp.Equal, [
                        qe.LineNoColumn(),
                        qc.EvalConstant(42),
                    ]),
                    qc.Operator(
                        qp.Or,
                        [
                            qc.Operator(qp.Not, [
                                qc.Operator(qp.Not, [
                                    qc.Operator(qp.Equal, [
                                        qe.DateColumn(),
                                        qc.EvalConstant(
                                            datetime.date(2014, 1, 1)),
                                    ]),
                                ]),
                            ]),
                            # Aggregation node deep in the tree.
                            qe.SumInt([qc.EvalConstant(1)]),
                        ]),
                ]))
        self.assertEqual((2, 1), (len(columns), len(aggregates)))
Ejemplo n.º 4
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.º 5
0
    def test_is_aggregate_derived(self):
        columns, aggregates = qc.get_columns_and_aggregates(
            qc.EvalAnd(
                qc.EvalEqual(qe.PositionColumn(), qc.EvalConstant(42)),
                qc.EvalOr(
                    qc.EvalNot(
                        qc.EvalEqual(
                            qe.DateColumn(),
                            qc.EvalConstant(datetime.date(2014, 1, 1)))),
                    qc.EvalConstant(False))))
        self.assertEqual((2, 0), (len(columns), len(aggregates)))

        columns, aggregates = qc.get_columns_and_aggregates(
            qc.EvalAnd(
                qc.EvalEqual(qe.PositionColumn(), qc.EvalConstant(42)),
                qc.EvalOr(
                    qc.EvalNot(
                        qc.EvalEqual(
                            qe.DateColumn(),
                            qc.EvalConstant(datetime.date(2014, 1, 1)))),
                    # Aggregation node deep in the tree.
                    qe.Sum([qc.EvalConstant(1)]))))
        self.assertEqual((2, 1), (len(columns), len(aggregates)))
Ejemplo n.º 6
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)")