Example #1
0
    def test_column_alias(self):
        with self.subTest("full form"):
            query = "x AS y"
            parser = Parser(Lexer(query))
            result = parser.parse_column_expression()
            self.assertParsedAll(parser, result)
            self.assertParsedStructure(parser, result, {
                "value": {
                    "column": "x"
                },
                "as_": "AS",
                "alias": "y"
            })

        with self.subTest("shorthand form"):
            query = "x y"
            parser = Parser(Lexer(query))
            result = parser.parse_column_expression()
            self.assertParsedAll(parser, result)
            self.assertParsedStructure(parser, result, {
                "value": {
                    "column": "x"
                },
                "alias": "y"
            })
Example #2
0
    def test_column_expression_call(self):
        with self.subTest("no args"):
            query = "now()"
            parser = Parser(Lexer(query))
            result = parser.parse_column_expression()
            self.assertParsedAll(parser, result)
            self.assertParsedStructure(parser, result, {"function": "now"})

        with self.subTest("composition"):
            query = "upper('hello') || concat(' ', lower('world'))"
            parser = Parser(Lexer(query))
            result = parser.parse_column_expression()
            self.assertParsedAll(parser, result)
            self.assertParsedStructure(
                parser,
                result,
                {
                    "left": {
                        "function": "upper",
                        "0": "'hello'"
                    },
                    "operator": "||",
                    "right": {
                        "function": "concat",
                        "0": "' '",
                        "1": {
                            "function": "lower",
                            "0": "'world'"
                        },
                    },
                },
            )
Example #3
0
    def test_column_expression_precedence(self):
        query = "- 1 + 2 * 3 + 4"
        parser = Parser(Lexer(query))
        result = parser.parse_column_expression()
        self.assertParsedAll(parser, result)
        self.assertParsedStructure(
            parser,
            result,
            {
                "left": {
                    "left": {
                        "operator": "-",
                        "right": "1"
                    },
                    "operator": "+",
                    "right": {
                        "left": "2",
                        "operator": "*",
                        "right": "3"
                    },
                },
                "operator": "+",
                "right": "4",
            },
        )

        query = "- 1 + 2 * (3 + 4)"
        parser = Parser(Lexer(query))
        result = parser.parse_column_expression()
        self.assertParsedAll(parser, result)
        self.assertParsedStructure(
            parser,
            result,
            {
                "left": {
                    "operator": "-",
                    "right": "1"
                },
                "operator": "+",
                "right": {
                    "left": "2",
                    "operator": "*",
                    "right": {
                        "expression": {
                            "left": "3",
                            "operator": "+",
                            "right": "4"
                        }
                    },
                },
            },
        )
Example #4
0
 def test_column_expression_and(self):
     query = "x = 1 AND y = 2"
     parser = Parser(Lexer(query))
     result = parser.parse_column_expression()
     self.assertParsedAll(parser, result)
     self.assertParsedStructure(
         parser,
         result,
         {
             "left": {
                 "left": {
                     "column": "x"
                 },
                 "operator": "=",
                 "right": "1"
             },
             "operator": "AND",
             "right": {
                 "left": {
                     "column": "y"
                 },
                 "operator": "=",
                 "right": "2"
             },
         },
     )
Example #5
0
 def test_column_expression_is_not_null(self):
     query = "x IS NOT NULL"
     parser = Parser(Lexer(query))
     result = parser.parse_column_expression()
     self.assertParsedAll(parser, result)
     self.assertParsedStructure(
         parser,
         result,
         {
             "left": {
                 "column": "x"
             },
             "operator": "IS",
             "right": {
                 "operator": "NOT",
                 "right": "NULL"
             },
         },
     )
Example #6
0
 def test_column_expression(self):
     query = "- 1 + 2 * 3 * 4"
     parser = Parser(Lexer(query))
     result = parser.parse_column_expression()
     self.assertParsedAll(parser, result)
Example #7
0
 def test_column_expression_between(self):
     query = "x BETWEEN a AND b"
     parser = Parser(Lexer(query))
     result = parser.parse_column_expression()
     self.assertParsedAll(parser, result)