Skip to content

Commit f67e77f

Browse files
committed
tests for unary
1 parent 2bf206a commit f67e77f

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

sqlglot/optimizer/annotate_types.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,16 @@ def _annotate_binary(self, expression: B) -> B:
450450
if isinstance(expression, (exp.Connector, exp.Predicate)):
451451
self._set_type(expression, exp.DataType.Type.BOOLEAN)
452452
expr_type = expression.type
453-
left_is_nonnull = left_type and (left_type.args.get("nonnull") is True)
454-
right_is_nonnull = right_type and (right_type.args.get("nonnull") is True)
455-
if expr_type:
456-
expr_type.set(
457-
"nonnull",
458-
isinstance(expression, exp.Is) or (left_is_nonnull and right_is_nonnull),
453+
if expr_type and (
454+
isinstance(expression, exp.Is)
455+
or (
456+
left_type
457+
and (left_type.args.get("nonnull") is True)
458+
and right_type
459+
and (right_type.args.get("nonnull") is True)
459460
)
461+
):
462+
expr_type.set("nonnull", True)
460463
elif (left_type_this, right_type_this) in self.binary_coercions:
461464
self._set_type(
462465
expression, self.binary_coercions[(left_type_this, right_type_this)](left, right)

tests/test_optimizer.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ def test_nonnull_annotation(self):
17271727
):
17281728
sql = f"SELECT {sql_predicate} FROM foo"
17291729
query = parse_one(sql)
1730-
annotated = annotate_types(query)
1730+
annotated = annotate_types(query, schema=schema)
17311731
assert annotated.selects[0].type == exp.DataType.build(
17321732
"BOOLEAN", nonnull=nonnull
17331733
)
@@ -1738,5 +1738,15 @@ def test_nonnull_annotation(self):
17381738
):
17391739
sql = f"SELECT {sql_predicate} FROM foo"
17401740
query = parse_one(sql)
1741-
annotated = annotate_types(query)
1741+
annotated = annotate_types(query, schema=schema)
17421742
assert annotated.selects[0].type == exp.DataType.build("BOOLEAN", nonnull=True)
1743+
1744+
for unary, unary_type in (("NOT", "BOOLEAN"), ("-", "INT")):
1745+
for value, nonnull in (("1", True), ("foo.id", False)):
1746+
with self.subTest(f"Test NULL propagation for unary: {unary} with value: {value}"):
1747+
sql = f"SELECT {unary} {value} FROM foo"
1748+
query = parse_one(sql)
1749+
annotated = annotate_types(query, schema=schema)
1750+
assert annotated.selects[0].type == exp.DataType.build(
1751+
unary_type, nonnull=nonnull
1752+
)

0 commit comments

Comments
 (0)