Skip to content

Commit

Permalink
fix(bigquery): preserve log argument order when parsing and generatio…
Browse files Browse the repository at this point in the history
…n dialects match (#2293)
  • Loading branch information
cpcloud authored Sep 22, 2023
1 parent 06e0869 commit 5aa7e2a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class BigQuery(Dialect):
UNNEST_COLUMN_ONLY = True
SUPPORTS_USER_DEFINED_TYPES = False
SUPPORTS_SEMI_ANTI_JOIN = False
LOG_BASE_FIRST = False

# https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#case_sensitivity
RESOLVES_IDENTIFIERS_AS_UPPERCASE = None
Expand Down Expand Up @@ -265,7 +266,6 @@ class Tokenizer(tokens.Tokenizer):
class Parser(parser.Parser):
PREFIXED_PIVOT_COLUMNS = True

LOG_BASE_FIRST = False
LOG_DEFAULTS_TO_LN = True

FUNCTIONS = {
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class TSQL(Dialect):
NULL_ORDERING = "nulls_are_small"
TIME_FORMAT = "'yyyy-mm-dd hh:mm:ss'"
SUPPORTS_SEMI_ANTI_JOIN = False
LOG_BASE_FIRST = False

TIME_MAPPING = {
"year": "%Y",
Expand Down Expand Up @@ -400,7 +401,6 @@ class Parser(parser.Parser):
TokenType.END: lambda self: self._parse_command(),
}

LOG_BASE_FIRST = False
LOG_DEFAULTS_TO_LN = True

CONCAT_NULL_OUTPUTS_STRING = True
Expand Down
9 changes: 9 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class Generator:
exp.WithJournalTableProperty: lambda self, e: f"WITH JOURNAL TABLE={self.sql(e, 'this')}",
}

# Whether the base comes first
LOG_BASE_FIRST = True

# Whether or not null ordering is supported in order by
NULL_ORDERING_SUPPORTED = True

Expand Down Expand Up @@ -2524,6 +2527,12 @@ def sub_sql(self, expression: exp.Sub) -> str:
def trycast_sql(self, expression: exp.TryCast) -> str:
return self.cast_sql(expression, safe_prefix="TRY_")

def log_sql(self, expression: exp.Log) -> str:
args = list(expression.args.values())
if not self.LOG_BASE_FIRST:
args.reverse()
return self.func("LOG", *args)

def use_sql(self, expression: exp.Use) -> str:
kind = self.sql(expression, "kind")
kind = f" {kind}" if kind else ""
Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ def test_bigquery(self):
pretty=True,
)

self.validate_identity("LOG(n, b)")

def test_user_defined_functions(self):
self.validate_identity(
"CREATE TEMPORARY FUNCTION a(x FLOAT64, y FLOAT64) RETURNS FLOAT64 NOT DETERMINISTIC LANGUAGE js AS 'return x*y;'"
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def test_tsql(self):
},
)
self.validate_identity("HASHBYTES('MD2', 'x')")
self.validate_identity("LOG(n, b)")

def test_types(self):
self.validate_identity("CAST(x AS XML)")
Expand Down

0 comments on commit 5aa7e2a

Please sign in to comment.