Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: format CAST as :: less aggressively #2555

Merged
merged 5 commits into from
May 2, 2024
Merged
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
2 changes: 1 addition & 1 deletion examples/wursthall/models/src/customer_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def execute(
for _ in range(random.choice(range(10, 20))):
customer_details.append(
CustomerDetails(
id=faker.uuid4(),
id=str(faker.uuid4()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI this change was triggered due to a mypy error, probably caused by the latest Faker release.

I verified that the uuid is a string value, but I went ahead and used str here to enforce this instead of t.cast.

>>> import faker
>>> type(faker.Faker().uuid4())
<class 'str'>

name=faker.name(),
phone=faker.phone_number(),
email=faker.ascii_email(),
Expand Down
2 changes: 1 addition & 1 deletion examples/wursthall/models/src/order_item_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def execute(
faker = Faker()
order_ds = order_date.strftime("%Y-%m-%d")
for _ in range(random.choice(range(50, 100))):
order_item_id = faker.uuid4()
order_item_id = str(faker.uuid4())
table_id = np.random.choice(range(0, 20))
is_registered_customer = np.random.choice([True, False], p=[0.1, 0.9])
if is_registered_customer:
Expand Down
8 changes: 7 additions & 1 deletion sqlmesh/core/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,13 @@ def format_model_expressions(
*statements, query = expressions

def cast_to_colon(node: exp.Expression) -> exp.Expression:
if isinstance(node, exp.Cast) and not node.args.get("format"):
if isinstance(node, exp.Cast) and not any(
# Only convert CAST into :: if it doesn't have additional args set, otherwise this
# conversion could alter the semantics (eg. changing SAFE_CAST in BigQuery to CAST)
arg
for name, arg in node.args.items()
if name not in ("this", "to")
):
this = node.this

if not isinstance(this, (exp.Binary, exp.Unary)) or isinstance(this, exp.Paren):
Expand Down
21 changes: 21 additions & 0 deletions tests/core/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ def test_format_model_expressions():
JINJA_END;"""
)

x = format_model_expressions(
parse(
"""
MODEL(name a.b, kind FULL, dialect bigquery);
SELECT SAFE_CAST('bla' AS INT64) AS FOO
"""
),
dialect="bigquery",
)
assert (
x
== """MODEL (
name a.b,
kind FULL,
dialect bigquery
);

SELECT
SAFE_CAST('bla' AS INT64) AS FOO"""
)


def test_macro_format():
assert parse_one("@EACH(ARRAY(1,2), x -> x)").sql() == "@EACH(ARRAY(1, 2), x -> x)"
Expand Down
Loading