Skip to content

Commit

Permalink
feat(api): accept Schema objects in public ibis.schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jun 30, 2022
1 parent bdaa50c commit 0daac6c
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import datetime
import functools
from typing import Iterable, Mapping, Sequence, TypeVar
from typing import Iterable, Mapping, Sequence
from typing import Tuple as _Tuple
from typing import TypeVar
from typing import Union as _Union

import dateutil.parser
import numpy as np
Expand Down Expand Up @@ -216,6 +219,13 @@

negate = ir.NumericValue.negate

SupportsSchema = TypeVar(
"SupportsSchema",
Iterable[_Tuple[str, _Union[str, dt.DataType]]],
Mapping[str, dt.DataType],
sch.Schema,
)


def param(type: dt.DataType) -> ir.Scalar:
"""Create a deferred parameter of a given type.
Expand Down Expand Up @@ -261,9 +271,7 @@ def sequence(values: Sequence[T | None]) -> ir.ValueList:


def schema(
pairs: Iterable[tuple[str, dt.DataType]]
| Mapping[str, dt.DataType]
| None = None,
pairs: SupportsSchema | None = None,
names: Iterable[str] | None = None,
types: Iterable[str | dt.DataType] | None = None,
) -> sch.Schema:
Expand All @@ -273,55 +281,53 @@ def schema(
----------
pairs
List or dictionary of name, type pairs. Mutually exclusive with `names`
and `types`.
and `types` arguments.
names
Field names. Mutually exclusive with `pairs`.
types
Field types. Mutually exclusive with `pairs`.
Examples
--------
>>> from ibis import schema
>>> from ibis import schema, Schema
>>> sc = schema([('foo', 'string'),
... ('bar', 'int64'),
... ('baz', 'boolean')])
>>> sc2 = schema(names=['foo', 'bar', 'baz'],
... types=['string', 'int64', 'boolean'])
>>> sc = schema(names=['foo', 'bar', 'baz'],
... types=['string', 'int64', 'boolean'])
>>> sc = schema(dict(foo="string"))
>>> sc = schema(Schema(['foo'], ['string'])) # no-op
Returns
-------
Schema
An ibis schema
""" # noqa: E501
if pairs is not None:
return Schema.from_dict(dict(pairs))
return sch.schema(pairs)
else:
return Schema(names, types)

return sch.schema(names, types)

_schema = schema


def table(schema: sch.Schema, name: str | None = None) -> ir.Table:
"""Create an unbound table for build expressions without data.

def table(
schema: SupportsSchema,
name: str | None = None,
) -> ir.Table:
"""Create an unbound table for building expressions without data.
Parameters
----------
schema
A schema for the table
name
Name for the table
Name for the table. One is generated if this value is `None`.
Returns
-------
Table
An unbound table expression
"""
if not isinstance(schema, Schema):
schema = _schema(pairs=schema)

node = ops.UnboundTable(schema, name=name)
node = ops.UnboundTable(sch.schema(schema), name=name)
return node.to_expr()


Expand Down

0 comments on commit 0daac6c

Please sign in to comment.