示例#1
0
    def testComplexSelect(self):
        query = ("(SELECT proc.parent.pid AS ppid, proc.pid FROM pslist(10) "
                 "WHERE COUNT(proc.open_files) > 10) and True")

        expected = ast.Intersection(
            ast.Map(
                ast.Filter(
                    ast.Apply(ast.Var("pslist"), ast.Literal(10)),
                    ast.StrictOrderedSet(
                        ast.Apply(
                            ast.Var("COUNT"),
                            ast.Resolve(ast.Var("proc"),
                                        ast.Literal("open_files"))),
                        ast.Literal(10))),
                ast.Bind(
                    ast.Pair(
                        ast.Literal("ppid"),
                        ast.Resolve(
                            ast.Resolve(ast.Var("proc"),
                                        ast.Literal("parent")),
                            ast.Literal("pid"))),
                    ast.Pair(ast.Literal("pid"),
                             ast.Resolve(ast.Var("proc"),
                                         ast.Literal("pid"))))),
            ast.Literal(True))

        self.assertQueryMatches(query, expected)
示例#2
0
    def testSubscript(self):
        self.assertQueryMatches("d['foo']",
                                ast.Select(ast.Var("d"), ast.Literal("foo")))

        self.assertQueryMatches(
            "d['foo'] + 10",
            ast.Sum(ast.Select(ast.Var("d"), ast.Literal("foo")),
                    ast.Literal(10)))

        self.assertQueryMatches(
            "obj.props[0]",
            ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                       ast.Literal(0)))

        self.assertQueryMatches(
            "obj.props[0].foo",
            ast.Resolve(
                ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                           ast.Literal(0)), ast.Literal("foo")))

        self.assertQueryMatches(
            "obj.props[10 + 10].foo",
            ast.Resolve(
                ast.Select(ast.Resolve(ast.Var("obj"), ast.Literal("props")),
                           ast.Sum(ast.Literal(10), ast.Literal(10))),
                ast.Literal("foo")))

        self.assertQueryMatches(
            "w['x'][y[5] + 5] * 10",
            ast.Product(
                ast.Select(
                    ast.Select(ast.Var("w"), ast.Literal("x")),
                    ast.Sum(ast.Select(ast.Var("y"), ast.Literal(5)),
                            ast.Literal(5))), ast.Literal(10)))
示例#3
0
 def testSelectWhat(self):
     self.assertQueryMatches(
         "SELECT proc.parent.pid AS ppid,"
         "proc.pid,"
         "'foo',"
         "asdate(proc.starttime),"
         "proc.fd[5]"
         "FROM pslist()",
         ast.Map(
             ast.Apply(ast.Var("pslist")),
             ast.Bind(
                 ast.Pair(
                     ast.Literal("ppid"),
                     ast.Resolve(
                         ast.Resolve(ast.Var("proc"),
                                     ast.Literal("parent")),
                         ast.Literal("pid"))),
                 ast.Pair(ast.Literal("pid"),
                          ast.Resolve(ast.Var("proc"), ast.Literal("pid"))),
                 ast.Pair(ast.Literal("column_2"), ast.Literal("foo")),
                 ast.Pair(
                     ast.Literal("asdate"),
                     ast.Apply(
                         ast.Var("asdate"),
                         ast.Resolve(ast.Var("proc"),
                                     ast.Literal("starttime")))),
                 ast.Pair(
                     ast.Literal("fd_5"),
                     ast.Select(
                         ast.Resolve(ast.Var("proc"), ast.Literal("fd")),
                         ast.Literal(5))))))
示例#4
0
 def testInfix(self):
     self.assertQueryMatches(
         "x == 'foo' and y.z contains 'bar'",
         ast.Intersection(
             ast.Equivalence(ast.Var("x"), ast.Literal("foo")),
             ast.Membership(ast.Literal("bar"),
                            ast.Resolve(ast.Var("y"), ast.Literal("z")))))
示例#5
0
    def testInfix(self):
        self.assertQueryMatches("x + y", ast.Sum(ast.Var("x"), ast.Var("y")))

        self.assertQueryMatches(
            "w.x.y.z",
            ast.Resolve(
                ast.Resolve(ast.Resolve(ast.Var("w"), ast.Literal("x")),
                            ast.Literal("y")), ast.Literal("z")))

        # Operator precedence should work correctly.
        self.assertQueryMatches(
            "x + y * z",
            ast.Sum(ast.Var("x"), ast.Product(ast.Var("y"), ast.Var("z"))))

        self.assertQueryMatches(
            "x * y + z",
            ast.Sum(ast.Product(ast.Var("x"), ast.Var("y")), ast.Var("z")))
示例#6
0
def NormalizeResolve(x, y, **kwargs):
    if isinstance(y, ast.Var):
        literal_y = ast.Literal(y.value, start=y.start, end=y.end,
                                source=y.source)
    else:
        raise TypeError("Type of RHS must be Var. Got %r." % y)

    return ast.Resolve(x, literal_y, **kwargs)
示例#7
0
    def testFullSelect(self):
        query = ("SELECT proc.parent.pid AS ppid_column, proc.pid"
                 " FROM pslist(pid: 10, ppid: 20)"
                 " WHERE count(proc.open_files) > 10"
                 " ORDER BY proc.command DESC"
                 " LIMIT 10 - 9 OFFSET add(5, 10)")

        expected = ast.Map(
            ast.Apply(
                ast.Var("take"), ast.Difference(ast.Literal(10),
                                                ast.Literal(9)),
                ast.Apply(
                    ast.Var("drop"),
                    ast.Apply(ast.Var("add"), ast.Literal(5), ast.Literal(10)),
                    ast.Apply(
                        ast.Var("reverse"),
                        ast.Sort(
                            ast.Filter(
                                ast.Apply(
                                    ast.Var("pslist"),
                                    ast.Pair(ast.Var("pid"), ast.Literal(10)),
                                    ast.Pair(ast.Var("ppid"),
                                             ast.Literal(20))),
                                ast.StrictOrderedSet(
                                    ast.Literal(10),
                                    ast.Apply(
                                        ast.Var("count"),
                                        ast.Resolve(
                                            ast.Var("proc"),
                                            ast.Literal("open_files"))),
                                )),
                            ast.Resolve(ast.Var("proc"),
                                        ast.Literal("command")))))),
            ast.Bind(
                ast.Pair(
                    ast.Literal("ppid_column"),
                    ast.Resolve(
                        ast.Resolve(ast.Var("proc"), ast.Literal("parent")),
                        ast.Literal("pid"))),
                ast.Pair(ast.Literal("pid"),
                         ast.Resolve(ast.Var("proc"), ast.Literal("pid")))))

        self.assertQueryMatches(query, expected)
示例#8
0
    def testBuiltins(self):
        self.assertQueryMatches(
            "filter(pslist(), proc.pid == 1)",
            ast.Filter(
                ast.Apply(ast.Var("pslist")),
                ast.Equivalence(
                    ast.Resolve(ast.Var("proc"), ast.Literal("pid")),
                    ast.Literal(1))))

        self.assertQueryMatches(
            "map(pslist(), [proc.pid, proc['command']])",
            ast.Map(
                ast.Apply(ast.Var("pslist")),
                ast.Tuple(ast.Resolve(ast.Var("proc"), ast.Literal("pid")),
                          ast.Select(ast.Var("proc"),
                                     ast.Literal("command")))))

        self.assertQueryMatches(
            "bind(x: 1, y: 2)",
            ast.Bind(ast.Pair(ast.Var("x"), ast.Literal(1)),
                     ast.Pair(ast.Var("y"), ast.Literal(2))))

        self.assertQueryRaises("bind (x: 1, y: 2)")