Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
unit_to_var,
strposition_sql,
groupconcat_sql,
sha2_digest_sql,
)
from sqlglot.generator import unsupported_args
from sqlglot.helper import seq_get, split_num_words
Expand Down Expand Up @@ -593,7 +594,9 @@ class Parser(parser.Parser):
"REGEXP_EXTRACT_ALL": _build_regexp_extract(
exp.RegexpExtractAll, default_group=exp.Literal.number(0)
),
"SHA256": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(256)),
"SHA256": lambda args: exp.SHA2Digest(
this=seq_get(args, 0), length=exp.Literal.number(256)
),
"SHA512": lambda args: exp.SHA2(this=seq_get(args, 0), length=exp.Literal.number(512)),
"SPLIT": lambda args: exp.Split(
# https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
Expand Down Expand Up @@ -1153,6 +1156,7 @@ class Generator(generator.Generator):
exp.SHA: rename_func("SHA1"),
exp.SHA2: sha256_sql,
exp.SHA1Digest: rename_func("SHA1"),
exp.SHA2Digest: sha2_digest_sql,
exp.StabilityProperty: lambda self, e: (
"DETERMINISTIC" if e.name == "IMMUTABLE" else "NOT DETERMINISTIC"
),
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
timestamptrunc_sql,
unit_to_var,
trim_sql,
sha2_digest_sql,
)
from sqlglot.generator import Generator
from sqlglot.helper import is_int, seq_get
Expand Down Expand Up @@ -1201,6 +1202,7 @@ class Generator(generator.Generator):
exp.SHA: rename_func("SHA1"),
exp.SHA1Digest: rename_func("SHA1"),
exp.SHA2: sha256_sql,
exp.SHA2Digest: sha2_digest_sql,
exp.Split: lambda self, e: self.func(
"splitByString", e.args.get("expression"), e.this, e.args.get("limit")
),
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,10 @@ def sha256_sql(self: Generator, expression: exp.SHA2) -> str:
return self.func(f"SHA{expression.text('length') or '256'}", expression.this)


def sha2_digest_sql(self: Generator, expression: exp.SHA2Digest) -> str:
return self.func(f"SHA{expression.text('length') or '256'}", expression.this)


def sequence_sql(self: Generator, expression: exp.GenerateSeries | exp.GenerateDateArray) -> str:
start = expression.args.get("start")
end = expression.args.get("end")
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
groupconcat_sql,
inline_array_unless_query,
regexp_replace_global_modifier,
sha2_digest_sql,
)
from sqlglot.generator import unsupported_args
from sqlglot.helper import is_date_unit, seq_get
Expand Down Expand Up @@ -884,6 +885,7 @@ class Generator(generator.Generator):
exp.Initcap: _initcap_sql,
exp.MD5Digest: lambda self, e: self.func("UNHEX", self.func("MD5", e.this)),
exp.SHA1Digest: lambda self, e: self.func("UNHEX", self.func("SHA1", e.this)),
exp.SHA2Digest: lambda self, e: self.func("UNHEX", sha2_digest_sql(self, e)),
exp.MonthsBetween: lambda self, e: self.func(
"DATEDIFF",
"'month'",
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
count_if_to_sum,
groupconcat_sql,
regexp_replace_global_modifier,
sha2_digest_sql,
)
from sqlglot.generator import unsupported_args
from sqlglot.helper import is_int, seq_get
Expand Down Expand Up @@ -698,6 +699,7 @@ class Generator(generator.Generator):
]
),
exp.SHA2: sha256_sql,
exp.SHA2Digest: sha2_digest_sql,
exp.StrPosition: lambda self, e: strposition_sql(self, e, func_name="POSITION"),
exp.StrToDate: lambda self, e: self.func("TO_DATE", e.this, self.format_time(e)),
exp.StrToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this, self.format_time(e)),
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
sequence_sql,
build_regexp_extract,
explode_to_unnest_sql,
sha2_digest_sql,
)
from sqlglot.dialects.hive import Hive
from sqlglot.dialects.mysql import MySQL
Expand Down Expand Up @@ -547,6 +548,7 @@ class Generator(generator.Generator):
exp.SHA: rename_func("SHA1"),
exp.SHA1Digest: rename_func("SHA1"),
exp.SHA2: sha256_sql,
exp.SHA2Digest: sha2_digest_sql,
}

RESERVED_KEYWORDS = {
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/dialects/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ class Generator(Postgres.Generator):
exp.TsOrDsAdd: date_delta_sql("DATEADD"),
exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
exp.UnixToTime: lambda self, e: self._unix_to_time_sql(e),
exp.SHA2Digest: lambda self, e: self.func(
"SHA2", e.this, e.args.get("length") or exp.Literal.number(256)
),
Comment on lines +222 to +224
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a bug in how exp.SHA2 is generated in redshift? Why did we need this entry and not reuse the new sha2_digest_sql helper?

Copy link
Collaborator Author

@fivetran-felixhuang fivetran-felixhuang Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so in redshift it should be something like SHA2('abc', 256), while sha2_digest_sql produces SHA256('abc)

}

# Postgres maps exp.Pivot to no_pivot_sql, but Redshift support pivots
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,9 @@ class Generator(generator.Generator):
exp.ArrayConcatAgg: lambda self, e: self.func(
"ARRAY_FLATTEN", exp.ArrayAgg(this=e.this)
),
exp.SHA2Digest: lambda self, e: self.func(
"SHA2_BINARY", e.this, e.args.get("length") or exp.Literal.number(256)
),
}

SUPPORTED_JSON_PATH_PARTS = {
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/dialects/spark2.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ class Generator(Hive.Generator):
transforms.any_to_exists,
]
),
exp.SHA2Digest: lambda self, e: self.func(
"SHA2", e.this, e.args.get("length") or exp.Literal.number(256)
),
Comment on lines +285 to +287
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, similar to Redshift.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like redshift, spark2 also only has SHA2 function

exp.StrToDate: _str_to_date,
exp.StrToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this, self.format_time(e)),
exp.TimestampTrunc: lambda self, e: self.func("DATE_TRUNC", unit_to_str(e), e.this),
Expand Down
1 change: 1 addition & 0 deletions sqlglot/typing/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def _annotate_array(self: TypeAnnotator, expression: exp.Array) -> exp.Array:
exp.SHA,
exp.SHA2,
exp.SHA1Digest,
exp.SHA2Digest,
exp.Unhex,
}
},
Expand Down
4 changes: 2 additions & 2 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,8 @@ def test_bigquery(self):
"presto": "SHA256(x)",
"redshift": "SHA2(x, 256)",
"trino": "SHA256(x)",
"duckdb": "SHA256(x)",
"snowflake": "SHA2(x, 256)",
"duckdb": "UNHEX(SHA256(x))",
"snowflake": "SHA2_BINARY(x, 256)",
},
)
self.validate_all(
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ def test_duckdb(self):
)
self.validate_identity("DATE_SUB('YEAR', col, '2020-01-01')").assert_is(exp.Anonymous)
self.validate_identity("DATESUB('YEAR', col, '2020-01-01')").assert_is(exp.Anonymous)
self.validate_identity("SELECT SHA256('abc')")

self.validate_all("0b1010", write={"": "0 AS b1010"})
self.validate_all("0x1010", write={"": "0 AS x1010"})
Expand Down