Ejemplo n.º 1
0
def compile_targets(targets, environ):
    """Compile the targets and check for their validity. Process wildcard.

    Args:
      targets: A list of target expressions from the parser.
      environ: A compilation context for the targets.
    Returns:
      A list of compiled target expressions with resolved names.
    """
    # Bind the targets expressions to the execution context.
    if isinstance(targets, query_parser.Wildcard):
        # Insert the full list of available columns.
        targets = [
            query_parser.Target(query_parser.Column(name), None)
            for name in environ.wildcard_columns
        ]

    # Compile targets.
    c_targets = []
    target_names = set()
    for target in targets:
        c_expr = compile_expression(target.expression, environ)
        target_name = find_unique_name(
            target.name or query_parser.get_expression_name(target.expression),
            target_names)
        target_names.add(target_name)
        c_targets.append(EvalTarget(c_expr, target_name, is_aggregate(c_expr)))

    # Figure out if this query is an aggregate query and check validity of each
    # target's aggregation type.
    for index, c_target in enumerate(c_targets):
        columns, aggregates = get_columns_and_aggregates(c_target.c_expr)

        # Check for mixed aggregates and non-aggregates.
        if columns and aggregates:
            raise CompilationError(
                "Mixed aggregates and non-aggregates are not allowed")

        if aggregates:
            # Check for aggregates of aggregates.
            for aggregate in aggregates:
                for child in aggregate.childnodes():
                    if is_aggregate(child):
                        raise CompilationError(
                            "Aggregates of aggregates are not allowed")

    return c_targets
Ejemplo n.º 2
0
 def test_binary(self):
     self.assertEqual(
         'and_account_date',
         qp.get_expression_name(
             qp.And(qp.Column('account'), qp.Column('date'))))
Ejemplo n.º 3
0
 def test_unary(self):
     self.assertEqual('not_account',
                      qp.get_expression_name(qp.Not(qp.Column('account'))))
Ejemplo n.º 4
0
 def test_constant(self):
     self.assertEqual('c17', qp.get_expression_name(qp.Constant(17)))
     self.assertEqual(
         'c2014_01_01',
         qp.get_expression_name(qp.Constant(datetime.date(2014, 1, 1))))
Ejemplo n.º 5
0
 def test_function(self):
     self.assertEqual(
         'length_date',
         qp.get_expression_name(qp.Function('length', [qp.Column('date')])))
Ejemplo n.º 6
0
 def test_column(self):
     self.assertEqual('date', qp.get_expression_name(qp.Column('date')))