diff options
Diffstat (limited to 'sqlglot/executor/env.py')
-rw-r--r-- | sqlglot/executor/env.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/sqlglot/executor/env.py b/sqlglot/executor/env.py index 218a8e0..c51049b 100644 --- a/sqlglot/executor/env.py +++ b/sqlglot/executor/env.py @@ -106,6 +106,13 @@ def cast(this, to): return this if isinstance(this, str): return datetime.date.fromisoformat(this) + if to == exp.DataType.Type.TIME: + if isinstance(this, datetime.datetime): + return this.time() + if isinstance(this, datetime.time): + return this + if isinstance(this, str): + return datetime.time.fromisoformat(this) if to in (exp.DataType.Type.DATETIME, exp.DataType.Type.TIMESTAMP): if isinstance(this, datetime.datetime): return this @@ -139,7 +146,7 @@ def interval(this, unit): @null_if_any("this", "expression") -def arrayjoin(this, expression, null=None): +def arraytostring(this, expression, null=None): return expression.join(x for x in (x if x is not None else null for x in this) if x is not None) @@ -173,7 +180,7 @@ ENV = { "ABS": null_if_any(lambda this: abs(this)), "ADD": null_if_any(lambda e, this: e + this), "ARRAYANY": null_if_any(lambda arr, func: any(func(e) for e in arr)), - "ARRAYJOIN": arrayjoin, + "ARRAYTOSTRING": arraytostring, "BETWEEN": null_if_any(lambda this, low, high: low <= this and this <= high), "BITWISEAND": null_if_any(lambda this, e: this & e), "BITWISELEFTSHIFT": null_if_any(lambda this, e: this << e), @@ -212,6 +219,7 @@ ENV = { "ORDERED": ordered, "POW": pow, "RIGHT": null_if_any(lambda this, e: this[-e:]), + "ROUND": null_if_any(lambda this, decimals=None, truncate=None: round(this, ndigits=decimals)), "STRPOSITION": str_position, "SUB": null_if_any(lambda e, this: e - this), "SUBSTRING": substring, @@ -225,10 +233,12 @@ ENV = { "CURRENTTIME": datetime.datetime.now, "CURRENTDATE": datetime.date.today, "STRFTIME": null_if_any(lambda fmt, arg: datetime.datetime.fromisoformat(arg).strftime(fmt)), + "STRTOTIME": null_if_any(lambda arg, format: datetime.datetime.strptime(arg, format)), "TRIM": null_if_any(lambda this, e=None: this.strip(e)), "STRUCT": lambda *args: { args[x]: args[x + 1] for x in range(0, len(args), 2) if (args[x + 1] is not None and args[x] is not None) }, + "UNIXTOTIME": null_if_any(lambda arg: datetime.datetime.utcfromtimestamp(arg)), } |