summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/sqlite.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/dialects/sqlite.py')
-rw-r--r--sqlglot/dialects/sqlite.py58
1 files changed, 35 insertions, 23 deletions
diff --git a/sqlglot/dialects/sqlite.py b/sqlglot/dialects/sqlite.py
index ed7c741..ab78b6e 100644
--- a/sqlglot/dialects/sqlite.py
+++ b/sqlglot/dialects/sqlite.py
@@ -1,10 +1,11 @@
from __future__ import annotations
-from sqlglot import exp, generator, parser, tokens
+from sqlglot import exp, generator, parser, tokens, transforms
from sqlglot.dialects.dialect import (
Dialect,
arrow_json_extract_scalar_sql,
arrow_json_extract_sql,
+ count_if_to_sum,
no_ilike_sql,
no_tablesample_sql,
no_trycast_sql,
@@ -13,23 +14,6 @@ from sqlglot.dialects.dialect import (
from sqlglot.tokens import TokenType
-# https://www.sqlite.org/lang_aggfunc.html#group_concat
-def _group_concat_sql(self, expression):
- this = expression.this
- distinct = expression.find(exp.Distinct)
- if distinct:
- this = distinct.expressions[0]
- distinct = "DISTINCT "
-
- if isinstance(expression.this, exp.Order):
- self.unsupported("SQLite GROUP_CONCAT doesn't support ORDER BY.")
- if expression.this.this and not distinct:
- this = expression.this.this
-
- separator = expression.args.get("separator")
- return f"GROUP_CONCAT({distinct or ''}{self.format_args(this, separator)})"
-
-
def _date_add_sql(self, expression):
modifier = expression.expression
modifier = expression.name if modifier.is_string else self.sql(modifier)
@@ -78,20 +62,32 @@ class SQLite(Dialect):
TRANSFORMS = {
**generator.Generator.TRANSFORMS, # type: ignore
+ **transforms.ELIMINATE_QUALIFY, # type: ignore
+ exp.CountIf: count_if_to_sum,
+ exp.CurrentDate: lambda *_: "CURRENT_DATE",
+ exp.CurrentTime: lambda *_: "CURRENT_TIME",
+ exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
exp.DateAdd: _date_add_sql,
+ exp.DateStrToDate: lambda self, e: self.sql(e, "this"),
exp.ILike: no_ilike_sql,
exp.JSONExtract: arrow_json_extract_sql,
exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
exp.JSONBExtract: arrow_json_extract_sql,
exp.JSONBExtractScalar: arrow_json_extract_scalar_sql,
exp.Levenshtein: rename_func("EDITDIST3"),
+ exp.LogicalOr: rename_func("MAX"),
+ exp.LogicalAnd: rename_func("MIN"),
exp.TableSample: no_tablesample_sql,
- exp.DateStrToDate: lambda self, e: self.sql(e, "this"),
exp.TimeStrToTime: lambda self, e: self.sql(e, "this"),
exp.TryCast: no_trycast_sql,
- exp.GroupConcat: _group_concat_sql,
}
+ def cast_sql(self, expression: exp.Cast) -> str:
+ if expression.to.this == exp.DataType.Type.DATE:
+ return self.func("DATE", expression.this)
+
+ return super().cast_sql(expression)
+
def datediff_sql(self, expression: exp.DateDiff) -> str:
unit = expression.args.get("unit")
unit = unit.name.upper() if unit else "DAY"
@@ -119,16 +115,32 @@ class SQLite(Dialect):
return f"CAST({sql} AS INTEGER)"
- def fetch_sql(self, expression):
+ def fetch_sql(self, expression: exp.Fetch) -> str:
return self.limit_sql(exp.Limit(expression=expression.args.get("count")))
- def least_sql(self, expression):
+ # https://www.sqlite.org/lang_aggfunc.html#group_concat
+ def groupconcat_sql(self, expression):
+ this = expression.this
+ distinct = expression.find(exp.Distinct)
+ if distinct:
+ this = distinct.expressions[0]
+ distinct = "DISTINCT "
+
+ if isinstance(expression.this, exp.Order):
+ self.unsupported("SQLite GROUP_CONCAT doesn't support ORDER BY.")
+ if expression.this.this and not distinct:
+ this = expression.this.this
+
+ separator = expression.args.get("separator")
+ return f"GROUP_CONCAT({distinct or ''}{self.format_args(this, separator)})"
+
+ def least_sql(self, expression: exp.Least) -> str:
if len(expression.expressions) > 1:
return rename_func("MIN")(self, expression)
return self.expressions(expression)
- def transaction_sql(self, expression):
+ def transaction_sql(self, expression: exp.Transaction) -> str:
this = expression.this
this = f" {this}" if this else ""
return f"BEGIN{this} TRANSACTION"