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))
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)
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)
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))
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)
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
def test_unrecognized_keyword(self): query = """lambda t: t.id == foo""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_unrecognized_function(self): query = """lambda t: foo(t.id) == 1""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_substring_step(self): query = """lambda t: t.id[1:10:3] == 'x'""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_ast(self): t = ast.parse("lambda: 0") e = convert(t, None) self.assertEqual(e, NumericLiteral(0))
def test_lambda(self): f = lambda: 0 e = convert(f, None) self.assertEqual(e, NumericLiteral(0))
def test_function(self): def f(): return 0 e = convert(f, None) self.assertEqual(e, NumericLiteral(0))
def test_undefined_token(self): query = """lambda t: foo[0] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_syntax_error(self): query = """lambda t: t[0] = 6""" self.assertRaises(PythonSyntaxException, lambda: convert(query, [self.schema]))
def test_invalid_dotted_attribute(self): query = """lambda t: t[0].foo == 6""" self.assertRaises(PythonUnsupportedOperationException, lambda: convert(query, [self.schema]))
def test_invalid_attribute(self): query = """lambda t: t.foo == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_mismatched_parenthesis(self): query = """lambda t: (t[999] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_partial_keyword(self): query = """foolambda t: t[999] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_string(self): f = "lambda: 0" e = convert(f, None) self.assertEqual(e, NumericLiteral(0))
def test_out_of_range_index(self): query = """lambda t: t[999] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_negative_index(self): query = """lambda t: t[-1] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_slice(self): query = """lambda t: t[1:2] == 6""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))
def test_substring_negative_index(self): query = """lambda t: t.id[-5] == 'x'""" self.assertRaises(PythonConvertException, lambda: convert(query, [self.schema]))