diff options
Diffstat (limited to 'sqlglot/expressions.py')
-rw-r--r-- | sqlglot/expressions.py | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py index f7717c8..eb7854a 100644 --- a/sqlglot/expressions.py +++ b/sqlglot/expressions.py @@ -2411,6 +2411,11 @@ class TimeTrunc(Func, TimeUnit): arg_types = {"this": True, "unit": True, "zone": False} +class DateFromParts(Func): + _sql_names = ["DATEFROMPARTS"] + arg_types = {"year": True, "month": True, "day": True} + + class DateStrToDate(Func): pass @@ -2554,7 +2559,7 @@ class Quantile(AggFunc): class ApproxQuantile(Quantile): - pass + arg_types = {"this": True, "quantile": True, "accuracy": False} class Reduce(Func): @@ -2569,6 +2574,10 @@ class RegexpSplit(Func): arg_types = {"this": True, "expression": True} +class Repeat(Func): + arg_types = {"this": True, "times": True} + + class Round(Func): arg_types = {"this": True, "decimals": False} @@ -2690,7 +2699,7 @@ class TsOrDiToDi(Func): class UnixToStr(Func): - arg_types = {"this": True, "format": True} + arg_types = {"this": True, "format": False} class UnixToTime(Func): @@ -3077,6 +3086,8 @@ def update(table, properties, where=None, from_=None, dialect=None, **opts): ) if from_: update.set("from", maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts)) + if isinstance(where, Condition): + where = Where(this=where) if where: update.set("where", maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts)) return update @@ -3518,6 +3529,41 @@ def replace_tables(expression, mapping): return expression.transform(_replace_tables) +def replace_placeholders(expression, *args, **kwargs): + """Replace placeholders in an expression. + + Args: + expression (sqlglot.Expression): Expression node to be transformed and replaced + args: Positional names that will substitute unnamed placeholders in the given order + kwargs: Keyword arguments that will substitute named placeholders + + Examples: + >>> from sqlglot import exp, parse_one + >>> replace_placeholders( + ... parse_one("select * from :tbl where ? = ?"), "a", "b", tbl="foo" + ... ).sql() + 'SELECT * FROM foo WHERE a = b' + + Returns: + The mapped expression + """ + + def _replace_placeholders(node, args, **kwargs): + if isinstance(node, Placeholder): + if node.name: + new_name = kwargs.get(node.name) + if new_name: + return to_identifier(new_name) + else: + try: + return to_identifier(next(args)) + except StopIteration: + pass + return node + + return expression.transform(_replace_placeholders, iter(args), **kwargs) + + TRUE = Boolean(this=True) FALSE = Boolean(this=False) NULL = Null() |