|
11 | 11 | from sqlmesh.utils.errors import SQLMeshError |
12 | 12 | from sqlmesh.utils.metaprogramming import Executable |
13 | 13 | from sqlmesh.core.macros import RuntimeStage |
| 14 | +from sqlmesh.core.model import load_sql_based_model |
14 | 15 |
|
15 | 16 |
|
16 | 17 | @pytest.fixture |
@@ -1313,3 +1314,51 @@ def render(dialect: str, hash_function: str) -> str: |
1313 | 1314 | render("snowflake", "SHA256") |
1314 | 1315 | == "SELECT SHA256(CONCAT(COALESCE(CAST(a AS VARCHAR), '_sqlmesh_surrogate_key_null_'))) FROM foo" |
1315 | 1316 | ) |
| 1317 | + |
| 1318 | + |
| 1319 | +@pytest.mark.parametrize( |
| 1320 | + "projection, expected", |
| 1321 | + [ |
| 1322 | + # GitHub issue #5649: once the optimizer resolves `1 IS NULL`, the CASE collapses into its |
| 1323 | + # ELSE branch, and the arithmetic must stay grouped inside the surrounding multiplication. |
| 1324 | + ( |
| 1325 | + "(@SAFE_SUB(price, amount_off)) * (@SAFE_SUB(1, percent_off / 100))", |
| 1326 | + 'CASE WHEN "s"."amount_off" IS NULL AND "s"."price" IS NULL THEN NULL ELSE (COALESCE("s"."price", 0) - COALESCE("s"."amount_off", 0)) END * (COALESCE(1, 0) - COALESCE("s"."percent_off" / 100, 0))', |
| 1327 | + ), |
| 1328 | + ("x * @SAFE_SUB(1, y)", '"s"."x" * (COALESCE(1, 0) - COALESCE("s"."y", 0))'), |
| 1329 | + ("-@SAFE_SUB(1, y)", '-(COALESCE(1, 0) - COALESCE("s"."y", 0))'), |
| 1330 | + ("x - @SAFE_ADD(1, y)", '"s"."x" - (COALESCE(1, 0) + COALESCE("s"."y", 0))'), |
| 1331 | + ("x * @SAFE_ADD(1, y)", '"s"."x" * (COALESCE(1, 0) + COALESCE("s"."y", 0))'), |
| 1332 | + ( |
| 1333 | + "@SAFE_DIV(@SAFE_SUB(1, y), x)", |
| 1334 | + '(COALESCE(1, 0) - COALESCE("s"."y", 0)) / NULLIF("s"."x", 0)', |
| 1335 | + ), |
| 1336 | + # The quotient is a single operand of the enclosing operator. |
| 1337 | + ("x / @SAFE_DIV(price, y)", '"s"."x" / ("s"."price" / NULLIF("s"."y", 0))'), |
| 1338 | + ("x * @SAFE_DIV(price, y)", '"s"."x" * ("s"."price" / NULLIF("s"."y", 0))'), |
| 1339 | + # Standalone usages: the optimizer drops the redundant parentheses around the quotient, |
| 1340 | + # while the ELSE branch keeps its grouping. |
| 1341 | + ("@SAFE_DIV(price, y)", '"s"."price" / NULLIF("s"."y", 0)'), |
| 1342 | + ( |
| 1343 | + "@SAFE_SUB(price, amount_off) + 1", |
| 1344 | + 'CASE WHEN "s"."amount_off" IS NULL AND "s"."price" IS NULL THEN NULL ELSE (COALESCE("s"."price", 0) - COALESCE("s"."amount_off", 0)) END + 1', |
| 1345 | + ), |
| 1346 | + ], |
| 1347 | +) |
| 1348 | +def test_safe_arithmetic_macros_keep_precedence_after_optimization( |
| 1349 | + projection: str, expected: str |
| 1350 | +) -> None: |
| 1351 | + model = load_sql_based_model( |
| 1352 | + d.parse( |
| 1353 | + f""" |
| 1354 | + MODEL (name db.safe_arithmetic); |
| 1355 | +
|
| 1356 | + SELECT {projection} AS result |
| 1357 | + FROM (SELECT 1 AS x, 2 AS y, 100 AS price, 25 AS amount_off, 20 AS percent_off) AS s |
| 1358 | + """ |
| 1359 | + ) |
| 1360 | + ) |
| 1361 | + |
| 1362 | + rendered_projection = model.render_query_or_raise().selects[0] |
| 1363 | + |
| 1364 | + assert rendered_projection.sql() == f'{expected} AS "result"' |
0 commit comments