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()))
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)
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)))
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.EvalAnd(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.EvalAnd(qe.First([qe.AccountColumn()]), qe.Last([qe.AccountColumn()])) 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.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.EvalAnd(qe.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))