Skip to content

Commit

Permalink
depr(schema): schedule Schema.delete() and Schema.append() for re…
Browse files Browse the repository at this point in the history
…moval
  • Loading branch information
kszucs authored and cpcloud committed Jan 25, 2023
1 parent 911a080 commit 45ac9a9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
14 changes: 7 additions & 7 deletions ibis/backends/base/sql/ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ def _pieces(self):
part_fields = {name: self.schema[name] for name in part_schema}
part_schema = sch.Schema(part_fields)

to_delete = []
for name in self.partition:
if name in self.schema:
to_delete.append(name)

if len(to_delete):
main_schema = main_schema.delete(to_delete)
to_delete = {name for name in self.partition if name in self.schema}
fields = {
name: dtype
for name, dtype in main_schema.items()
if name not in to_delete
}
main_schema = sch.Schema(fields)

yield format_schema(main_schema)
yield f'PARTITIONED BY {format_schema(part_schema)}'
Expand Down
8 changes: 6 additions & 2 deletions ibis/backends/impala/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,12 @@ def impala_create_test_database(con, env):


PARQUET_SCHEMAS = {
'functional_alltypes': TEST_TABLES["functional_alltypes"].delete(
["index", "Unnamed: 0"]
"functional_alltypes": ibis.schema(
{
name: dtype
for name, dtype in TEST_TABLES["functional_alltypes"].items()
if name not in {"index", "Unnamed: 0"}
}
),
"tpch_region": ibis.schema(
[
Expand Down
6 changes: 3 additions & 3 deletions ibis/expr/datatypes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from public import public

import ibis.expr.types as ir
from ibis.common.annotations import optional
from ibis.common.annotations import attribute, optional
from ibis.common.exceptions import IbisTypeError
from ibis.common.grounds import Concrete, Singleton
from ibis.common.validators import (
Expand Down Expand Up @@ -734,12 +734,12 @@ def from_dict(
def pairs(self) -> Mapping[str, DataType]:
return self.fields

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

@property
@attribute.default
def types(self) -> tuple[DataType, ...]:
"""Return the types of the struct's fields."""
return tuple(self.fields.values())
Expand Down
2 changes: 1 addition & 1 deletion ibis/expr/operations/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __init__(self, left, right, predicates, **kwargs):
@property
def schema(self):
# For joins retaining both table schemas, merge them together here
return self.left.schema.append(self.right.schema)
return self.left.schema.merge(self.right.schema)


@public
Expand Down
34 changes: 19 additions & 15 deletions ibis/expr/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ def copy(self, fields=None):
fields = self.fields
return type(self)(fields)

@attribute.default
def _name_locs(self) -> dict[str, int]:
# validate unique field names
name_locs = {v: i for i, v in enumerate(self.names)}
if len(name_locs) < len(self.names):
duplicate_names = list(self.names)
for v in name_locs:
duplicate_names.remove(v)
raise IntegrityError(f'Duplicate column name(s): {duplicate_names}')
return name_locs

def __repr__(self) -> str:
space = 2 + max(map(len, self.names), default=0)
return "ibis.Schema {{{}\n}}".format(
Expand All @@ -109,14 +98,18 @@ def __contains__(self, name: str) -> bool:
def __getitem__(self, name: str) -> dt.DataType:
return self.fields[name]

@property
@attribute.default
def names(self):
return tuple(self.fields.keys())

@property
@attribute.default
def types(self):
return tuple(self.fields.values())

@attribute.default
def _name_locs(self) -> dict[str, int]:
return {v: i for i, v in enumerate(self.names)}

def equals(self, other: Schema) -> bool:
"""Return whether `other` is equal to `self`.
Expand All @@ -140,6 +133,11 @@ def equals(self, other: Schema) -> bool:
)
return self.__cached_equals__(other)

@deprecated(
as_of="4.1",
removed_in="5.0",
instead="construct a new Schema without the undesired names instead",
)
def delete(self, names_to_delete: Iterable[str]) -> Schema:
"""Remove `names_to_delete` names from `self`.
Expand Down Expand Up @@ -247,8 +245,14 @@ def __ge__(self, other: Schema) -> bool:
"""Return whether `self` is a superset of or equal to `other`."""
return set(self.items()) >= set(other.items())

@deprecated(as_of="4.1", removed_in="5.0", instead="use Schema.merge() instead")
def append(self, other: Schema) -> Schema:
"""Append `schema` to `self`.
return self.merge(other)

def merge(self, other: Schema) -> Schema:
"""Merge `other` to `self`.
Raise an `IntegrityError` if there are duplicate column names.
Parameters
----------
Expand All @@ -265,7 +269,7 @@ def append(self, other: Schema) -> Schema:
>>> import ibis
>>> first = ibis.Schema.from_dict({"a": "int", "b": "string"})
>>> second = ibis.Schema.from_dict({"c": "float", "d": "int16"})
>>> first.append(second)
>>> first.merge(second)
ibis.Schema {
a int64
b string
Expand Down
3 changes: 2 additions & 1 deletion ibis/tests/expr/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def test_names_types():

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

assert s2 == ibis.schema({"a": "int64", "c": "float64"})
2 changes: 1 addition & 1 deletion ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ def test_cross_join(table):

joined = table.cross_join(scalar_aggs)
agg_schema = api.Schema({'sum_a': 'int64', 'mean_b': 'double'})
ex_schema = table.schema().append(agg_schema)
ex_schema = table.schema().merge(agg_schema)
assert_equal(joined.schema(), ex_schema)


Expand Down

0 comments on commit 45ac9a9

Please sign in to comment.