Beispiel #1
0
def _get_column_index(inputs, aliases, attribute):
    """
    Find the column index of for given attribute
    :param inputs: Input queries with schema used to match against
    :param aliases: An alias for each input for dotted references
    :param attribute:  The attribute to map to an index
    :return: An UnnamedAttributeRef mapped to the given attribute
    """
    # $0
    if isinstance(attribute, (int, long)):
        return UnnamedAttributeRef(attribute)
    # alias.attribute
    elif isinstance(attribute, basestring) and '.' in attribute:
        assert aliases
        alias, attribute = map(str.strip, attribute.split('.'))
        index = aliases.index(alias)
        # ProjectingJoin will not accept a NamedAttributeRef :(
        return UnnamedAttributeRef(
            sum(len(q.query.scheme())
                for q in inputs[:index]) + NamedAttributeRef(
                    attribute).get_position(inputs[index].query.scheme()))
    # attribute
    elif isinstance(attribute, basestring):
        # ProjectingJoin will not accept a NamedAttributeRef :(
        return UnnamedAttributeRef(
            NamedAttributeRef(attribute).get_position(
                sum((q.query.scheme() for q in inputs), Scheme())))
    # lambda t1, t2: t1.attribute
    elif callable(attribute):
        ref = convert(attribute, [q.query.scheme() for q in inputs])
        schema = sum((q.query.scheme() for q in inputs), Scheme())
        return UnnamedAttributeRef(ref.get_position(schema))
Beispiel #2
0
    def _execute(self, query, expected):
        projection = convert(query, [self.schema])
        self.assertIsNotNone(projection)

        expression = Apply([('out', projection)], self.scan)
        plan = self.get_query(expression)
        return self.check_result(plan, expected)
Beispiel #3
0
    def _execute(self, query, expected):
        predicate = convert(query, [self.schema])
        self.assertIsNotNone(predicate)

        expression = Select(predicate, self.scan)
        plan = self.get_query(expression)
        return self.check_result(plan, expected)
Beispiel #4
0
def _get_column_index(inputs, aliases, attribute):
    """
    Find the column index of for given attribute
    :param inputs: Input queries with schema used to match against
    :param aliases: An alias for each input for dotted references
    :param attribute:  The attribute to map to an index
    :return: An UnnamedAttributeRef mapped to the given attribute
    """
    # $0
    if isinstance(attribute, (int, long)):
        return UnnamedAttributeRef(attribute)
    # alias.attribute
    elif isinstance(attribute, basestring) and '.' in attribute:
        assert aliases
        alias, attribute = map(str.strip, attribute.split('.'))
        index = aliases.index(alias)
        # ProjectingJoin will not accept a NamedAttributeRef :(
        return UnnamedAttributeRef(
            sum(len(q.query.scheme()) for q in inputs[:index]) +
            NamedAttributeRef(attribute).get_position(
                inputs[index].query.scheme()))
    # attribute
    elif isinstance(attribute, basestring):
        # ProjectingJoin will not accept a NamedAttributeRef :(
        return UnnamedAttributeRef(
            NamedAttributeRef(attribute).get_position(
                sum((q.query.scheme() for q in inputs), Scheme())))
    # lambda t1, t2: t1.attribute
    elif callable(attribute):
        ref = convert(attribute, [q.query.scheme() for q in inputs])
        schema = sum((q.query.scheme() for q in inputs), Scheme())
        return UnnamedAttributeRef(ref.get_position(schema))
Beispiel #5
0
    def _execute(self, query, expected):
        projection = convert(query, [self.schema])
        self.assertIsNotNone(projection)

        expression = Apply([("out", projection)], self.scan)
        plan = self.get_query(expression)
        return self.check_result(plan, expected)
Beispiel #6
0
 def _convert(self, source_or_ast_or_callable,
              scheme=None, out_type=None, multivalued=False):
     scheme = scheme or [self.query.scheme()]
     try:
         return convert(source_or_ast_or_callable, scheme, udfs=self.udfs)
     except PythonConvertException:
         udf = _create_udf(source_or_ast_or_callable, scheme,
                           connection=self.connection,
                           out_type=out_type,
                           multivalued=multivalued)
         self.udfs.append([udf.name, len(udf.arguments), udf.typ])
         return udf
Beispiel #7
0
 def _convert(self,
              source_or_ast_or_callable,
              scheme=None,
              out_type=None,
              multivalued=False):
     scheme = scheme or [self.query.scheme()]
     try:
         return convert(source_or_ast_or_callable, scheme, udfs=self.udfs)
     except PythonConvertException:
         udf = _create_udf(source_or_ast_or_callable,
                           scheme,
                           connection=self.connection,
                           out_type=out_type,
                           multivalued=multivalued)
         self.udfs.append([udf.name, len(udf.arguments), udf.typ])
         return udf
Beispiel #8
0
 def test_unrecognized_keyword(self):
     query = """lambda t: t.id == foo"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #9
0
 def test_unrecognized_function(self):
     query = """lambda t: foo(t.id) == 1"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #10
0
 def test_substring_step(self):
     query = """lambda t: t.id[1:10:3] == 'x'"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #11
0
 def test_ast(self):
     t = ast.parse("lambda: 0")
     e = convert(t, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #12
0
 def test_lambda(self):
     f = lambda: 0
     e = convert(f, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #13
0
 def test_function(self):
     def f():
         return 0
     e = convert(f, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #14
0
 def test_undefined_token(self):
     query = """lambda t: foo[0] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #15
0
 def test_syntax_error(self):
     query = """lambda t: t[0] = 6"""
     self.assertRaises(PythonSyntaxException,
                       lambda: convert(query, [self.schema]))
Beispiel #16
0
 def test_invalid_dotted_attribute(self):
     query = """lambda t: t[0].foo == 6"""
     self.assertRaises(PythonUnsupportedOperationException,
                       lambda: convert(query, [self.schema]))
Beispiel #17
0
 def test_invalid_attribute(self):
     query = """lambda t: t.foo == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #18
0
 def test_mismatched_parenthesis(self):
     query = """lambda t: (t[999] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #19
0
 def test_partial_keyword(self):
     query = """foolambda t: t[999] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #20
0
 def test_string(self):
     f = "lambda: 0"
     e = convert(f, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #21
0
 def test_out_of_range_index(self):
     query = """lambda t: t[999] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #22
0
 def test_lambda(self):
     f = lambda: 0
     e = convert(f, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #23
0
 def test_negative_index(self):
     query = """lambda t: t[-1] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #24
0
 def test_slice(self):
     query = """lambda t: t[1:2] == 6"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #25
0
 def test_ast(self):
     t = ast.parse("lambda: 0")
     e = convert(t, None)
     self.assertEqual(e, NumericLiteral(0))
Beispiel #26
0
 def test_substring_negative_index(self):
     query = """lambda t: t.id[-5] == 'x'"""
     self.assertRaises(PythonConvertException,
                       lambda: convert(query, [self.schema]))
Beispiel #27
0
    def test_function(self):
        def f():
            return 0

        e = convert(f, None)
        self.assertEqual(e, NumericLiteral(0))
Beispiel #28
0
 def test_string(self):
     f = "lambda: 0"
     e = convert(f, None)
     self.assertEqual(e, NumericLiteral(0))