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(ir): repr iterables when constructing name of operations #9480

Merged
merged 5 commits into from
Jul 1, 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
8 changes: 8 additions & 0 deletions ibis/backends/bigquery/tests/system/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,11 @@ def test_parameters_in_url_connect(mocker):
parsed = urlparse("bigquery://ibis-gbq?location=us-east1")
ibis.connect("bigquery://ibis-gbq?location=us-east1")
spy.assert_called_once_with(parsed, location="us-east1")


def test_complex_column_name(con):
expr = ibis.literal(1).name(
"StringToTimestamp_StringConcat_date_string_col_' America_New_York'_'%F %Z'"
)
result = con.to_pandas(expr)
assert result == 1
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
parse_timestamp('%F %Z', CONCAT(`t0`.`date_string_col`, ' America/New_York'), 'UTC') AS `StringToTimestamp_StringConcat_'%F %Z'`
parse_timestamp('%F %Z', CONCAT(`t0`.`date_string_col`, ' America/New_York'), 'UTC') AS `StringToTimestamp_StringConcat_date_string_col_' America_New_York'_'%F %Z'`
cpcloud marked this conversation as resolved.
Show resolved Hide resolved
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
GREATEST("t0"."int_col", 10) AS "Greatest()"
GREATEST("t0"."int_col", 10) AS "Greatest((int_col, 10))"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
GREATEST("t0"."int_col", "t0"."bigint_col") AS "Greatest()"
GREATEST("t0"."int_col", "t0"."bigint_col") AS "Greatest((int_col, bigint_col))"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
LEAST("t0"."int_col", 10) AS "Least()"
LEAST("t0"."int_col", 10) AS "Least((int_col, 10))"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
LEAST("t0"."int_col", "t0"."bigint_col") AS "Least()"
LEAST("t0"."int_col", "t0"."bigint_col") AS "Least((int_col, bigint_col))"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
indexOf(['a','b','c'], "t0"."string_col") - 1 AS "FindInSet(string_col)"
indexOf(['a','b','c'], "t0"."string_col") - 1 AS "FindInSet(string_col, ('a', 'b', 'c'))"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN "t0"."float_col" < 0
THEN "t0"."int_col"
ELSE 0
END AS "SearchedCase(0)"
END AS "SearchedCase((Greater(float_col, 0), Less(float_col, 0)), (Multiply(int_col, 2), int_col), 0)"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
CASE "t0"."string_col" WHEN 'foo' THEN 'bar' WHEN 'baz' THEN 'qux' ELSE 'default' END AS "SimpleCase(string_col, 'default')"
CASE "t0"."string_col" WHEN 'foo' THEN 'bar' WHEN 'baz' THEN 'qux' ELSE 'default' END AS "SimpleCase(string_col, ('foo', 'baz'), ('bar', 'qux'), 'default')"
FROM "functional_alltypes" AS "t0"
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SELECT
)
THEN 2
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SELECT
)
THEN 2
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ SELECT
WHEN 50 <= `t0`.`f`
THEN 4
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SELECT
)
THEN 2
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ SELECT
)
THEN 3
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN 10 < `t0`.`f`
THEN 1
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SELECT
)
THEN 2
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN 10 <= `t0`.`f`
THEN 1
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN 10 <= `t0`.`f`
THEN 1
ELSE CAST(NULL AS TINYINT)
END AS INT) AS `Cast(Bucket(f), int32)`
END AS INT) AS `Cast(Bucket(f, ()), int32)`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN 10 <= `t0`.`f`
THEN 1
ELSE CAST(NULL AS TINYINT)
END AS DOUBLE) AS `Cast(Bucket(f), float64)`
END AS DOUBLE) AS `Cast(Bucket(f, ()), float64)`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ SELECT
)
THEN 3
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ SELECT
WHEN 50 < `t0`.`f`
THEN 4
ELSE CAST(NULL AS TINYINT)
END AS `Bucket(f)`
END AS `Bucket(f, ())`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`l_extendedprice`, 0) AS `Coalesce()`
COALESCE(`t0`.`l_extendedprice`, 0) AS `Coalesce((l_extendedprice, 0))`
FROM `tpch_lineitem` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`l_extendedprice`, 0.0) AS `Coalesce()`
COALESCE(`t0`.`l_extendedprice`, 0.0) AS `Coalesce((l_extendedprice, 0.0))`
FROM `tpch_lineitem` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`l_quantity`, 0) AS `Coalesce()`
COALESCE(`t0`.`l_quantity`, 0) AS `Coalesce((l_quantity, 0))`
FROM `tpch_lineitem` AS `t0`
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ SELECT
WHEN `t0`.`c` < 0
THEN `t0`.`a` * 2
ELSE CAST(NULL AS BIGINT)
END AS `SearchedCase(Cast(None, int64))`
END AS `SearchedCase((Greater(f, 0), Less(c, 0)), (Multiply(d, 2), Multiply(a, 2)), Cast(None, int64))`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
CASE `t0`.`g` WHEN 'foo' THEN 'bar' WHEN 'baz' THEN 'qux' ELSE 'default' END AS `SimpleCase(g, 'default')`
CASE `t0`.`g` WHEN 'foo' THEN 'bar' WHEN 'baz' THEN 'qux' ELSE 'default' END AS `SimpleCase(g, ('foo', 'baz'), ('bar', 'qux'), 'default')`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`int_col`, `t0`.`bigint_col`) AS `Coalesce()`
COALESCE(`t0`.`int_col`, `t0`.`bigint_col`) AS `Coalesce((int_col, bigint_col))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`string_col`, 'foo') AS `Coalesce()`
COALESCE(`t0`.`string_col`, 'foo') AS `Coalesce((string_col, 'foo'))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
GREATEST(`t0`.`int_col`, `t0`.`bigint_col`) AS `Greatest()`
GREATEST(`t0`.`int_col`, `t0`.`bigint_col`) AS `Greatest((int_col, bigint_col))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
GREATEST(`t0`.`string_col`, 'foo') AS `Greatest()`
GREATEST(`t0`.`string_col`, 'foo') AS `Greatest((string_col, 'foo'))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
LEAST(`t0`.`int_col`, `t0`.`bigint_col`) AS `Least()`
LEAST(`t0`.`int_col`, `t0`.`bigint_col`) AS `Least((int_col, bigint_col))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
LEAST(`t0`.`string_col`, 'foo') AS `Least()`
LEAST(`t0`.`string_col`, 'foo') AS `Least((string_col, 'foo'))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
`t0`.`g` IN ('foo', 'bar', 'baz') AS `InValues(g)`
`t0`.`g` IN ('foo', 'bar', 'baz') AS `InValues(g, ('foo', 'bar', 'baz'))`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
NOT (
`t0`.`g` IN ('foo', 'bar', 'baz')
) AS `Not(InValues(g))`
) AS `Not(InValues(g, ('foo', 'bar', 'baz')))`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
2 IN (`t0`.`a`, `t0`.`b`, `t0`.`c`) AS `InValues(2)`
2 IN (`t0`.`a`, `t0`.`b`, `t0`.`c`) AS `InValues(2, (a, b, c))`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SELECT
NOT (
2 IN (`t0`.`a`, `t0`.`b`, `t0`.`c`)
) AS `Not(InValues(2))`
) AS `Not(InValues(2, (a, b, c)))`
FROM `alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
FIND_IN_SET(`t0`.`string_col`, CONCAT_WS(',', 'a', 'b')) - 1 AS `FindInSet(string_col)`
FIND_IN_SET(`t0`.`string_col`, CONCAT_WS(',', 'a', 'b')) - 1 AS `FindInSet(string_col, ('a', 'b'))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
FIND_IN_SET(`t0`.`string_col`, CONCAT_WS(',', 'a')) - 1 AS `FindInSet(string_col)`
FIND_IN_SET(`t0`.`string_col`, CONCAT_WS(',', 'a')) - 1 AS `FindInSet(string_col, ('a',))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
CONCAT_WS(',', 'a', 'b') AS `StringJoin(',')`
CONCAT_WS(',', 'a', 'b') AS `StringJoin(('a', 'b'), ',')`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`double_col`, 0) AS `Coalesce()`
COALESCE(`t0`.`double_col`, 0) AS `Coalesce((double_col, 0))`
FROM `functional_alltypes` AS `t0`
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SELECT
COALESCE(`t0`.`int_col`, 0) AS `Coalesce()`
COALESCE(`t0`.`int_col`, 0) AS `Coalesce((int_col, 0))`
FROM `functional_alltypes` AS `t0`
3 changes: 1 addition & 2 deletions ibis/backends/postgres/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ def test_coalesce_all_na_double(con):
def test_numeric_builtins_work(alltypes, df):
expr = alltypes.double_col.fill_null(0)
result = expr.execute()
expected = df.double_col.fillna(0)
expected.name = "Coalesce()"
expected = df.double_col.fillna(0).rename(expr.get_name())
tm.assert_series_equal(result, expected)


Expand Down
3 changes: 1 addition & 2 deletions ibis/backends/risingwave/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ def test_coalesce_all_na_double(con):
def test_numeric_builtins_work(alltypes, df):
expr = alltypes.double_col.fill_null(0)
result = expr.execute()
expected = df.double_col.fillna(0)
expected.name = "Coalesce()"
expected = df.double_col.fillna(0).rename(expr.get_name())
tm.assert_series_equal(result, expected)


Expand Down
19 changes: 14 additions & 5 deletions ibis/expr/operations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ibis.common.grounds import Concrete
from ibis.common.patterns import Coercible, CoercionError
from ibis.common.typing import DefaultTypeVars
from ibis.util import is_iterable


@public
Expand Down Expand Up @@ -79,11 +80,19 @@ def __coerce__(
# TODO(kszucs): figure out how to represent not named arguments
@property
def name(self) -> str:
names = (
name
for arg in self.__args__
if (name := getattr(arg, "name", None)) is not None
)
names = []
for arg in self.__args__:
if is_iterable(arg):
elements = [
element_name
for element in arg
if (element_name := getattr(element, "name", None)) is not None
]
joined = ", ".join(elements)
fmt = "({})" if len(elements) != 1 else "({},)"
names.append(fmt.format(joined))
elif (name := getattr(arg, "name", None)) is not None:
names.append(name)
return f"{self.__class__.__name__}({', '.join(names)})"

@property
Expand Down
7 changes: 7 additions & 0 deletions ibis/expr/operations/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def __init__(self, names, values):
)
super().__init__(names=names, values=values)

@property
def name(self) -> str:
pairs = ", ".join(
f"{name!r}: {op.name}" for name, op in zip(self.names, self.values)
)
return f"{self.__class__.__name__}({{{pairs}}})"

@attribute
def dtype(self) -> dt.DataType:
dtypes = (value.dtype for value in self.values)
Expand Down
16 changes: 16 additions & 0 deletions ibis/expr/tests/test_newrels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,3 +1748,19 @@ def test_filters_are_allowed_to_have_the_same_name():
assert f1.equals(f2)
assert f1.equals(f3)
assert f1.equals(f4)


def test_projections_with_similar_expressions_have_different_names():
t = ibis.table({"a": "string", "b": "string"}, name="t")

a = t.a.fill_null("")
b = t.b.fill_null("")
assert a.op().name != b.op().name

expr = t.select(a, b)
fields = expr.op().fields

assert a.op().name in fields
assert b.op().name in fields

assert expr.schema() == ibis.schema({a.op().name: "string", b.op().name: "string"})
Loading