Skip to content
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ codegen-extensions:
--input third_party/substrait/text/simple_extensions_schema.yaml \
--output src/substrait/gen/json/simple_extensions.py \
--output-model-type dataclasses.dataclass \
--target-python-version 3.10 \
--disable-timestamp

lint:
Expand Down
39 changes: 23 additions & 16 deletions src/substrait/builders/extended_expression.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from datetime import date
import itertools
from datetime import date
from typing import Any, Callable, Iterable, Union

import substrait.gen.proto.algebra_pb2 as stalg
import substrait.gen.proto.type_pb2 as stp
import substrait.gen.proto.extended_expression_pb2 as stee
import substrait.gen.proto.extensions.extensions_pb2 as ste
import substrait.gen.proto.type_pb2 as stp
from substrait.extension_registry import ExtensionRegistry
from substrait.type_inference import infer_extended_expression_schema
from substrait.utils import (
type_num_names,
merge_extension_urns,
merge_extension_uris,
merge_extension_declarations,
merge_extension_uris,
merge_extension_urns,
type_num_names,
)
from substrait.type_inference import infer_extended_expression_schema
from typing import Callable, Any, Union, Iterable

UnboundExtendedExpression = Callable[
[stp.NamedStruct, ExtensionRegistry], stee.ExtendedExpression
Expand All @@ -21,7 +22,7 @@


def _alias_or_inferred(
alias: Union[Iterable[str], str],
alias: Union[Iterable[str], str, None],
op: str,
args: Iterable[str],
):
Expand All @@ -44,7 +45,7 @@ def resolve_expression(


def literal(
value: Any, type: stp.Type, alias: Union[Iterable[str], str] = None
value: Any, type: stp.Type, alias: Union[Iterable[str], str, None] = None
) -> UnboundExtendedExpression:
"""Builds a resolver for ExtendedExpression containing a literal expression"""

Expand Down Expand Up @@ -154,7 +155,7 @@ def resolve(
return resolve


def column(field: Union[str, int], alias: Union[Iterable[str], str] = None):
def column(field: Union[str, int], alias: Union[Iterable[str], str, None] = None):
"""Builds a resolver for ExtendedExpression containing a FieldReference expression

Accepts either an index or a field name of a desired field.
Expand Down Expand Up @@ -208,7 +209,7 @@ def scalar_function(
urn: str,
function: str,
expressions: Iterable[ExtendedExpressionOrUnbound],
alias: Union[Iterable[str], str] = None,
alias: Union[Iterable[str], str, None] = None,
):
"""Builds a resolver for ExtendedExpression containing a ScalarFunction expression"""

Expand Down Expand Up @@ -306,7 +307,7 @@ def aggregate_function(
urn: str,
function: str,
expressions: Iterable[ExtendedExpressionOrUnbound],
alias: Union[Iterable[str], str] = None,
alias: Union[Iterable[str], str, None] = None,
):
"""Builds a resolver for ExtendedExpression containing a AggregateFunction measure"""

Expand Down Expand Up @@ -402,7 +403,7 @@ def window_function(
function: str,
expressions: Iterable[ExtendedExpressionOrUnbound],
partitions: Iterable[ExtendedExpressionOrUnbound] = [],
alias: Union[Iterable[str], str] = None,
alias: Union[Iterable[str], str, None] = None,
):
"""Builds a resolver for ExtendedExpression containing a WindowFunction expression"""

Expand Down Expand Up @@ -512,7 +513,7 @@ def resolve(
def if_then(
ifs: Iterable[tuple[ExtendedExpressionOrUnbound, ExtendedExpressionOrUnbound]],
_else: ExtendedExpressionOrUnbound,
alias: Union[Iterable[str], str] = None,
alias: Union[Iterable[str], str, None] = None,
):
"""Builds a resolver for ExtendedExpression containing an IfThen expression"""

Expand Down Expand Up @@ -767,7 +768,11 @@ def resolve(
return resolve


def cast(input: ExtendedExpressionOrUnbound, type: stp.Type):
def cast(
input: ExtendedExpressionOrUnbound,
type: stp.Type,
alias: Union[Iterable[str], str, None] = None,
):
"""Builds a resolver for ExtendedExpression containing a cast expression"""

def resolve(
Expand All @@ -785,7 +790,9 @@ def resolve(
failure_behavior=stalg.Expression.Cast.FAILURE_BEHAVIOR_RETURN_NULL,
)
),
output_names=["cast"], # TODO construct name from inputs
output_names=_alias_or_inferred(
alias, "cast", [bound_input.referred_expr[0].output_names[0]]
),
)
],
base_schema=base_schema,
Expand Down
26 changes: 13 additions & 13 deletions src/substrait/gen/json/simple_extensions.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tests/builders/extended_expression/test_cast.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import substrait.gen.proto.algebra_pb2 as stalg
import substrait.gen.proto.type_pb2 as stt
import substrait.gen.proto.extended_expression_pb2 as stee
import substrait.gen.proto.type_pb2 as stt
from substrait.builders.extended_expression import cast, literal
from substrait.builders.type import i8, i16
from substrait.extension_registry import ExtensionRegistry
Expand Down Expand Up @@ -37,7 +37,7 @@ def test_cast():
failure_behavior=stalg.Expression.Cast.FAILURE_BEHAVIOR_RETURN_NULL,
)
),
output_names=["cast"],
output_names=["cast(Literal(3))"],
)
],
base_schema=named_struct,
Expand All @@ -48,6 +48,7 @@ def test_cast():

def test_cast_with_extension():
import yaml

import substrait.gen.proto.extensions.extensions_pb2 as ste
from substrait.builders.extended_expression import scalar_function

Expand Down Expand Up @@ -134,7 +135,7 @@ def test_cast_with_extension():
failure_behavior=stalg.Expression.Cast.FAILURE_BEHAVIOR_RETURN_NULL,
)
),
output_names=["cast"],
output_names=["cast(add(Literal(1),Literal(2)))"],
)
],
base_schema=named_struct,
Expand Down