summaryrefslogtreecommitdiffstats
path: root/sqlglot/executor/env.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-02 23:59:11 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-02 23:59:11 +0000
commitcaea5267cb8e1fea3702adbdf6f68fd37d13b3b7 (patch)
treef06f1da1ab3b6906beca1c3c7222d28ff00766ac /sqlglot/executor/env.py
parentAdding upstream version 12.2.0. (diff)
downloadsqlglot-0c9fd0a27262a4b82d2347fe92db95748c7421d4.tar.xz
sqlglot-0c9fd0a27262a4b82d2347fe92db95748c7421d4.zip
Adding upstream version 15.0.0.upstream/15.0.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/executor/env.py')
-rw-r--r--sqlglot/executor/env.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/sqlglot/executor/env.py b/sqlglot/executor/env.py
index 8f64cce..51cffbd 100644
--- a/sqlglot/executor/env.py
+++ b/sqlglot/executor/env.py
@@ -5,6 +5,7 @@ import statistics
from functools import wraps
from sqlglot import exp
+from sqlglot.generator import Generator
from sqlglot.helper import PYTHON_VERSION
@@ -102,6 +103,8 @@ def cast(this, to):
return datetime.date.fromisoformat(this)
if to == exp.DataType.Type.DATETIME:
return datetime.datetime.fromisoformat(this)
+ if to == exp.DataType.Type.BOOLEAN:
+ return bool(this)
if to in exp.DataType.TEXT_TYPES:
return str(this)
if to in {exp.DataType.Type.FLOAT, exp.DataType.Type.DOUBLE}:
@@ -119,9 +122,11 @@ def ordered(this, desc, nulls_first):
@null_if_any
def interval(this, unit):
- if unit == "DAY":
- return datetime.timedelta(days=float(this))
- raise NotImplementedError
+ unit = unit.lower()
+ plural = unit + "s"
+ if plural in Generator.TIME_PART_SINGULARS:
+ unit = plural
+ return datetime.timedelta(**{unit: float(this)})
ENV = {
@@ -147,7 +152,9 @@ ENV = {
"COALESCE": lambda *args: next((a for a in args if a is not None), None),
"CONCAT": null_if_any(lambda *args: "".join(args)),
"CONCATWS": null_if_any(lambda this, *args: this.join(args)),
+ "DATESTRTODATE": null_if_any(lambda arg: datetime.date.fromisoformat(arg)),
"DIV": null_if_any(lambda e, this: e / this),
+ "DOT": null_if_any(lambda e, this: e[this]),
"EQ": null_if_any(lambda this, e: this == e),
"EXTRACT": null_if_any(lambda this, e: getattr(e, this)),
"GT": null_if_any(lambda this, e: this > e),
@@ -162,6 +169,7 @@ ENV = {
"LOWER": null_if_any(lambda arg: arg.lower()),
"LT": null_if_any(lambda this, e: this < e),
"LTE": null_if_any(lambda this, e: this <= e),
+ "MAP": null_if_any(lambda *args: dict(zip(*args))), # type: ignore
"MOD": null_if_any(lambda e, this: e % this),
"MUL": null_if_any(lambda e, this: e * this),
"NEQ": null_if_any(lambda this, e: this != e),
@@ -180,4 +188,5 @@ ENV = {
"CURRENTTIMESTAMP": datetime.datetime.now,
"CURRENTTIME": datetime.datetime.now,
"CURRENTDATE": datetime.date.today,
+ "STRFTIME": null_if_any(lambda fmt, arg: datetime.datetime.fromisoformat(arg).strftime(fmt)),
}