summaryrefslogtreecommitdiffstats
path: root/sqlglot/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/parser.py')
-rw-r--r--sqlglot/parser.py52
1 files changed, 24 insertions, 28 deletions
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
index 894d68e..90fdade 100644
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -31,15 +31,20 @@ def parse_var_map(args):
)
+def binary_range_parser(
+ expr_type: t.Type[exp.Expression],
+) -> t.Callable[[Parser, t.Optional[exp.Expression]], t.Optional[exp.Expression]]:
+ return lambda self, this: self._parse_escape(
+ self.expression(expr_type, this=this, expression=self._parse_bitwise())
+ )
+
+
class _Parser(type):
def __new__(cls, clsname, bases, attrs):
klass = super().__new__(cls, clsname, bases, attrs)
klass._show_trie = new_trie(key.split(" ") for key in klass.SHOW_PARSERS)
klass._set_trie = new_trie(key.split(" ") for key in klass.SET_PARSERS)
- if not klass.INTEGER_DIVISION:
- klass.FACTOR = {**klass.FACTOR, TokenType.SLASH: exp.FloatDiv}
-
return klass
@@ -102,6 +107,7 @@ class Parser(metaclass=_Parser):
}
TYPE_TOKENS = {
+ TokenType.BIT,
TokenType.BOOLEAN,
TokenType.TINYINT,
TokenType.SMALLINT,
@@ -503,29 +509,15 @@ class Parser(metaclass=_Parser):
RANGE_PARSERS = {
TokenType.BETWEEN: lambda self, this: self._parse_between(this),
- TokenType.GLOB: lambda self, this: self._parse_escape(
- self.expression(exp.Glob, this=this, expression=self._parse_bitwise())
- ),
- TokenType.OVERLAPS: lambda self, this: self._parse_escape(
- self.expression(exp.Overlaps, this=this, expression=self._parse_bitwise())
- ),
+ TokenType.GLOB: binary_range_parser(exp.Glob),
+ TokenType.OVERLAPS: binary_range_parser(exp.Overlaps),
TokenType.IN: lambda self, this: self._parse_in(this),
TokenType.IS: lambda self, this: self._parse_is(this),
- TokenType.LIKE: lambda self, this: self._parse_escape(
- self.expression(exp.Like, this=this, expression=self._parse_bitwise())
- ),
- TokenType.ILIKE: lambda self, this: self._parse_escape(
- self.expression(exp.ILike, this=this, expression=self._parse_bitwise())
- ),
- TokenType.IRLIKE: lambda self, this: self.expression(
- exp.RegexpILike, this=this, expression=self._parse_bitwise()
- ),
- TokenType.RLIKE: lambda self, this: self.expression(
- exp.RegexpLike, this=this, expression=self._parse_bitwise()
- ),
- TokenType.SIMILAR_TO: lambda self, this: self.expression(
- exp.SimilarTo, this=this, expression=self._parse_bitwise()
- ),
+ TokenType.LIKE: binary_range_parser(exp.Like),
+ TokenType.ILIKE: binary_range_parser(exp.ILike),
+ TokenType.IRLIKE: binary_range_parser(exp.RegexpILike),
+ TokenType.RLIKE: binary_range_parser(exp.RegexpLike),
+ TokenType.SIMILAR_TO: binary_range_parser(exp.SimilarTo),
}
PROPERTY_PARSERS = {
@@ -707,7 +699,7 @@ class Parser(metaclass=_Parser):
STRICT_CAST = True
- INTEGER_DIVISION = True
+ CONVERT_TYPE_FIRST = False
__slots__ = (
"error_level",
@@ -2542,7 +2534,7 @@ class Parser(metaclass=_Parser):
def _parse_type(self) -> t.Optional[exp.Expression]:
if self._match(TokenType.INTERVAL):
- return self.expression(exp.Interval, this=self._parse_term(), unit=self._parse_var())
+ return self.expression(exp.Interval, this=self._parse_term(), unit=self._parse_field())
index = self._index
type_token = self._parse_types(check_func=True)
@@ -3285,15 +3277,19 @@ class Parser(metaclass=_Parser):
def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]:
to: t.Optional[exp.Expression]
- this = self._parse_column()
+ this = self._parse_bitwise()
if self._match(TokenType.USING):
to = self.expression(exp.CharacterSet, this=self._parse_var())
elif self._match(TokenType.COMMA):
- to = self._parse_types()
+ to = self._parse_bitwise()
else:
to = None
+ # Swap the argument order if needed to produce the correct AST
+ if self.CONVERT_TYPE_FIRST:
+ this, to = to, this
+
return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to)
def _parse_position(self, haystack_first: bool = False) -> exp.Expression: