Skip to content

Commit 45ac9a9

Browse files
kszucscpcloud
authored andcommitted
depr(schema): schedule Schema.delete() and Schema.append() for removal
1 parent 911a080 commit 45ac9a9

File tree

7 files changed

+39
-30
lines changed

7 files changed

+39
-30
lines changed

ibis/backends/base/sql/ddl.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ def _pieces(self):
241241
part_fields = {name: self.schema[name] for name in part_schema}
242242
part_schema = sch.Schema(part_fields)
243243

244-
to_delete = []
245-
for name in self.partition:
246-
if name in self.schema:
247-
to_delete.append(name)
248-
249-
if len(to_delete):
250-
main_schema = main_schema.delete(to_delete)
244+
to_delete = {name for name in self.partition if name in self.schema}
245+
fields = {
246+
name: dtype
247+
for name, dtype in main_schema.items()
248+
if name not in to_delete
249+
}
250+
main_schema = sch.Schema(fields)
251251

252252
yield format_schema(main_schema)
253253
yield f'PARTITIONED BY {format_schema(part_schema)}'

ibis/backends/impala/tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,12 @@ def impala_create_test_database(con, env):
497497

498498

499499
PARQUET_SCHEMAS = {
500-
'functional_alltypes': TEST_TABLES["functional_alltypes"].delete(
501-
["index", "Unnamed: 0"]
500+
"functional_alltypes": ibis.schema(
501+
{
502+
name: dtype
503+
for name, dtype in TEST_TABLES["functional_alltypes"].items()
504+
if name not in {"index", "Unnamed: 0"}
505+
}
502506
),
503507
"tpch_region": ibis.schema(
504508
[

ibis/expr/datatypes/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from public import public
1010

1111
import ibis.expr.types as ir
12-
from ibis.common.annotations import optional
12+
from ibis.common.annotations import attribute, optional
1313
from ibis.common.exceptions import IbisTypeError
1414
from ibis.common.grounds import Concrete, Singleton
1515
from ibis.common.validators import (
@@ -734,12 +734,12 @@ def from_dict(
734734
def pairs(self) -> Mapping[str, DataType]:
735735
return self.fields
736736

737-
@property
737+
@attribute.default
738738
def names(self) -> tuple[str, ...]:
739739
"""Return the names of the struct's fields."""
740740
return tuple(self.fields.keys())
741741

742-
@property
742+
@attribute.default
743743
def types(self) -> tuple[DataType, ...]:
744744
"""Return the types of the struct's fields."""
745745
return tuple(self.fields.values())

ibis/expr/operations/relations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __init__(self, left, right, predicates, **kwargs):
181181
@property
182182
def schema(self):
183183
# For joins retaining both table schemas, merge them together here
184-
return self.left.schema.append(self.right.schema)
184+
return self.left.schema.merge(self.right.schema)
185185

186186

187187
@public

ibis/expr/schema.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ def copy(self, fields=None):
7474
fields = self.fields
7575
return type(self)(fields)
7676

77-
@attribute.default
78-
def _name_locs(self) -> dict[str, int]:
79-
# validate unique field names
80-
name_locs = {v: i for i, v in enumerate(self.names)}
81-
if len(name_locs) < len(self.names):
82-
duplicate_names = list(self.names)
83-
for v in name_locs:
84-
duplicate_names.remove(v)
85-
raise IntegrityError(f'Duplicate column name(s): {duplicate_names}')
86-
return name_locs
87-
8877
def __repr__(self) -> str:
8978
space = 2 + max(map(len, self.names), default=0)
9079
return "ibis.Schema {{{}\n}}".format(
@@ -109,14 +98,18 @@ def __contains__(self, name: str) -> bool:
10998
def __getitem__(self, name: str) -> dt.DataType:
11099
return self.fields[name]
111100

112-
@property
101+
@attribute.default
113102
def names(self):
114103
return tuple(self.fields.keys())
115104

116-
@property
105+
@attribute.default
117106
def types(self):
118107
return tuple(self.fields.values())
119108

109+
@attribute.default
110+
def _name_locs(self) -> dict[str, int]:
111+
return {v: i for i, v in enumerate(self.names)}
112+
120113
def equals(self, other: Schema) -> bool:
121114
"""Return whether `other` is equal to `self`.
122115
@@ -140,6 +133,11 @@ def equals(self, other: Schema) -> bool:
140133
)
141134
return self.__cached_equals__(other)
142135

136+
@deprecated(
137+
as_of="4.1",
138+
removed_in="5.0",
139+
instead="construct a new Schema without the undesired names instead",
140+
)
143141
def delete(self, names_to_delete: Iterable[str]) -> Schema:
144142
"""Remove `names_to_delete` names from `self`.
145143
@@ -247,8 +245,14 @@ def __ge__(self, other: Schema) -> bool:
247245
"""Return whether `self` is a superset of or equal to `other`."""
248246
return set(self.items()) >= set(other.items())
249247

248+
@deprecated(as_of="4.1", removed_in="5.0", instead="use Schema.merge() instead")
250249
def append(self, other: Schema) -> Schema:
251-
"""Append `schema` to `self`.
250+
return self.merge(other)
251+
252+
def merge(self, other: Schema) -> Schema:
253+
"""Merge `other` to `self`.
254+
255+
Raise an `IntegrityError` if there are duplicate column names.
252256
253257
Parameters
254258
----------
@@ -265,7 +269,7 @@ def append(self, other: Schema) -> Schema:
265269
>>> import ibis
266270
>>> first = ibis.Schema.from_dict({"a": "int", "b": "string"})
267271
>>> second = ibis.Schema.from_dict({"c": "float", "d": "int16"})
268-
>>> first.append(second)
272+
>>> first.merge(second)
269273
ibis.Schema {
270274
a int64
271275
b string

ibis/tests/expr/test_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def test_names_types():
217217

218218
def test_schema_delete():
219219
s1 = ibis.schema({"a": "int64", "b": "string", "c": "float64", "d": "int64"})
220-
s2 = s1.delete(["b", "d"])
220+
with pytest.warns(FutureWarning):
221+
s2 = s1.delete(["b", "d"])
221222

222223
assert s2 == ibis.schema({"a": "int64", "c": "float64"})

ibis/tests/expr/test_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def test_cross_join(table):
10121012

10131013
joined = table.cross_join(scalar_aggs)
10141014
agg_schema = api.Schema({'sum_a': 'int64', 'mean_b': 'double'})
1015-
ex_schema = table.schema().append(agg_schema)
1015+
ex_schema = table.schema().merge(agg_schema)
10161016
assert_equal(joined.schema(), ex_schema)
10171017

10181018

0 commit comments

Comments
 (0)