Skip to content
Open
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
24 changes: 24 additions & 0 deletions pylegend/core/language/shared/column_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
PyLegendExpressionDateReturn,
PyLegendExpressionDateTimeReturn,
PyLegendExpressionStrictDateReturn,
PyLegendExpressionTimeReturn,
)
from pylegend.core.sql.metamodel import (
Expression,
Expand All @@ -53,6 +54,9 @@
"PyLegendDateColumnExpression",
"PyLegendDateTimeColumnExpression",
"PyLegendStrictDateColumnExpression",
"PyLegendTimeColumnExpression",
"PyLegendVariantColumnExpression",
"PyLegendBinaryColumnExpression",
]


Expand Down Expand Up @@ -130,3 +134,23 @@ class PyLegendStrictDateColumnExpression(PyLegendDateColumnExpression, PyLegendE

def __init__(self, row: "AbstractTdsRow", column: str) -> None:
super().__init__(row=row, column=column)


class PyLegendTimeColumnExpression(PyLegendColumnExpression, PyLegendExpressionTimeReturn):

def __init__(self, row: "AbstractTdsRow", column: str) -> None:
super().__init__(row=row, column=column)


class PyLegendVariantColumnExpression(PyLegendColumnExpression, PyLegendExpressionStringReturn):
"""Column expression for VARIANT/JSON columns; string-compatible for path-access operations."""

def __init__(self, row: "AbstractTdsRow", column: str) -> None:
super().__init__(row=row, column=column)


class PyLegendBinaryColumnExpression(PyLegendColumnExpression, PyLegendExpressionStringReturn):
"""Column expression for BINARY/VARBINARY columns; string-compatible for hex-string access."""

def __init__(self, row: "AbstractTdsRow", column: str) -> None:
super().__init__(row=row, column=column)
10 changes: 9 additions & 1 deletion pylegend/core/language/shared/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"PyLegendExpressionDateReturn",
"PyLegendExpressionDateTimeReturn",
"PyLegendExpressionStrictDateReturn",
"PyLegendExpressionNullReturn"
"PyLegendExpressionNullReturn",
"PyLegendExpressionTimeReturn",
]


Expand Down Expand Up @@ -105,3 +106,10 @@ class PyLegendExpressionStrictDateReturn(PyLegendExpressionDateReturn, metaclass
class PyLegendExpressionNullReturn(PyLegendExpression, metaclass=ABCMeta):
def get_leaf_expressions(self) -> PyLegendSequence["PyLegendExpression"]:
return [self]


class PyLegendExpressionTimeReturn(PyLegendExpression, metaclass=ABCMeta):
"""Expression return type for TIME values (time-of-day without a date component)."""

def get_leaf_expressions(self) -> PyLegendSequence["PyLegendExpression"]:
return [self]
6 changes: 6 additions & 0 deletions pylegend/core/language/shared/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
PyLegendFloat4,
PyLegendDouble,
PyLegendNumeric,
PyLegendVariant,
PyLegendBinary,
)
from pylegend.core.language.shared.primitives.time import PyLegendTime


__all__: PyLegendSequence[str] = [
Expand Down Expand Up @@ -68,4 +71,7 @@
"PyLegendFloat4",
"PyLegendDouble",
"PyLegendNumeric",
"PyLegendTime",
"PyLegendVariant",
"PyLegendBinary",
]
40 changes: 40 additions & 0 deletions pylegend/core/language/shared/primitives/precise_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"PyLegendUSmallInt",
"PyLegendInt",
"PyLegendUInt",
"PyLegendVariant",
"PyLegendBinary",
"PyLegendBigInt",
"PyLegendUBigInt",
"PyLegendVarchar",
Expand Down Expand Up @@ -254,3 +256,41 @@ def to_sql_expression(
config: FrameToSqlConfig
) -> Expression:
return super().to_sql_expression(frame_name_to_base_query_map, config)


class PyLegendVariant(PyLegendString):
"""Precise primitive: Variant – semi-structured / JSON column (Snowflake VARIANT, DuckDB JSON).

String-compatible so JSON-path operations and equality checks work without
a separate operation set. Dedicated JSON-extraction helpers can be added
as follow-up PRs once the Pure built-ins are exposed.
"""

def __init__(self, value: PyLegendExpressionStringReturn) -> None:
super().__init__(value)

def to_sql_expression(
self,
frame_name_to_base_query_map: PyLegendDict[str, QuerySpecification],
config: FrameToSqlConfig
) -> Expression:
return super().to_sql_expression(frame_name_to_base_query_map, config)


class PyLegendBinary(PyLegendString):
"""Precise primitive: Binary – raw byte array column (BINARY / VARBINARY / BYTEA).

String-compatible because binary data is most commonly accessed via
hex-string representation in SQL. Byte-level operations can be added
in follow-up PRs.
"""

def __init__(self, value: PyLegendExpressionStringReturn) -> None:
super().__init__(value)

def to_sql_expression(
self,
frame_name_to_base_query_map: PyLegendDict[str, QuerySpecification],
config: FrameToSqlConfig
) -> Expression:
return super().to_sql_expression(frame_name_to_base_query_map, config)
71 changes: 71 additions & 0 deletions pylegend/core/language/shared/primitives/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2026 Goldman Sachs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Time-of-day primitive type for the PyLegend expression language.

``PyLegendTime`` represents a TIME column value (hour, minute, second,
optional fractional seconds) with no date component. It maps to the
``Time`` primitive type in Legend's Pure type system, and to the SQL
``TIME`` type in Snowflake, DuckDB, and Databricks.

Instances are produced when a column whose Legend type is ``Time`` is
accessed on a TDS frame.
"""

from pylegend._typing import (
PyLegendSequence,
PyLegendDict,
)
from pylegend.core.language.shared.primitives.primitive import PyLegendPrimitive
from pylegend.core.language.shared.expression import PyLegendExpressionTimeReturn
from pylegend.core.sql.metamodel import (
Expression,
QuerySpecification,
)
from pylegend.core.tds.tds_frame import FrameToSqlConfig
from pylegend.core.tds.tds_frame import FrameToPureConfig


__all__: PyLegendSequence[str] = [
"PyLegendTime",
]


class PyLegendTime(PyLegendPrimitive):
"""TIME expression type — time-of-day without a date component.

Supports equality, null checks, and string conversion (inherited from
``PyLegendPrimitive``). Time-arithmetic and extraction functions
(e.g. ``hour()``, ``minute()``) can be added in follow-up PRs as
the corresponding Pure built-ins are exposed.
"""

__value: PyLegendExpressionTimeReturn

def __init__(self, value: PyLegendExpressionTimeReturn) -> None:
self.__value = value

def to_sql_expression(
self,
frame_name_to_base_query_map: PyLegendDict[str, QuerySpecification],
config: FrameToSqlConfig
) -> Expression:
return self.__value.to_sql_expression(frame_name_to_base_query_map, config)

def to_pure_expression(self, config: FrameToPureConfig) -> str:
return self.__value.to_pure_expression(config)

def value(self) -> PyLegendExpressionTimeReturn:
return self.__value
20 changes: 20 additions & 0 deletions pylegend/core/language/type_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
# Parameterized types
"varchar",
"numeric",

# Unstructured / binary types
"time",
"variant",
"binary",
]


Expand Down Expand Up @@ -179,3 +184,18 @@ def numeric(precision: builtins.int, scale: builtins.int) -> CastTarget: # noqa
frame.cast({"amount": pylegend.type_factory.numeric(10, 2)})
"""
return (PrimitiveType.Numeric, precision, scale) # type: ignore


def time() -> CastTarget:
"""Cast to Time (time-of-day without date; maps to SQL TIME)."""
return PrimitiveType.Time


def variant() -> CastTarget:
"""Cast to Variant (semi-structured / JSON; maps to Snowflake VARIANT or DuckDB JSON)."""
return PrimitiveType.Variant


def binary() -> CastTarget:
"""Cast to Binary (raw byte array; maps to SQL BINARY / VARBINARY / BYTEA)."""
return PrimitiveType.Binary
15 changes: 15 additions & 0 deletions pylegend/core/tds/tds_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class PrimitiveType(Enum):
Float4 = 21
Double = 22
Numeric = 23
Time = 24
Variant = 25
Binary = 26


class PrimitiveTdsColumn(TdsColumn):
Expand Down Expand Up @@ -184,6 +187,18 @@ def double_column(cls, name: str) -> "PrimitiveTdsColumn":
def numeric_column(cls, name: str) -> "PrimitiveTdsColumn":
return PrimitiveTdsColumn(name, PrimitiveType.Numeric)

@classmethod
def time_column(cls, name: str) -> "PrimitiveTdsColumn":
return PrimitiveTdsColumn(name, PrimitiveType.Time)

@classmethod
def variant_column(cls, name: str) -> "PrimitiveTdsColumn":
return PrimitiveTdsColumn(name, PrimitiveType.Variant)

@classmethod
def binary_column(cls, name: str) -> "PrimitiveTdsColumn":
return PrimitiveTdsColumn(name, PrimitiveType.Binary)


class EnumTdsColumn(TdsColumn):
__enum_type: str
Expand Down
Loading