Skip to content

Commit

Permalink
chore: more lit and array cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 26, 2023
1 parent fcfaad8 commit dbf34e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/base/sqlglot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ def make_cast(
converter: SqlglotType,
) -> Callable[[sg.exp.Expression, dt.DataType], sg.exp.Cast]:
def cast(arg: sg.exp.Expression, to: dt.DataType) -> sg.exp.Cast:
return sg.cast(arg, to=converter.from_ibis(to))
return sg.cast(_to_sqlglot(arg), to=converter.from_ibis(to))

return cast
43 changes: 20 additions & 23 deletions ibis/backends/duckdb/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,36 @@ def _literal(op, *, value, dtype, **kw):
#
# float will be upcast to double if necessary by duckdb
if not math.isfinite(value):
return cast(lit(str(value)), to=dt.float32 if dtype.is_decimal() else dtype)
return cast(lit(value), dtype)
return cast(str(value), to=dt.float32 if dtype.is_decimal() else dtype)
return cast(value, dtype)

Check warning on line 86 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L85-L86

Added lines #L85 - L86 were not covered by tests
elif dtype.is_time():
return cast(lit(value), dtype)
return cast(value, dtype)

Check warning on line 88 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L88

Added line #L88 was not covered by tests
elif dtype.is_timestamp():
year = lit(value.year)
month = lit(value.month)
day = lit(value.day)
hour = lit(value.hour)
minute = lit(value.minute)
second = lit(value.second)
year = value.year
month = value.month
day = value.day
hour = value.hour
minute = value.minute
second = value.second

Check warning on line 95 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L90-L95

Added lines #L90 - L95 were not covered by tests
if us := value.microsecond:
microsecond = lit(us / 1e6)
second += microsecond
second += us / 1e6

Check warning on line 97 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L97

Added line #L97 was not covered by tests
if (tz := dtype.timezone) is not None:
timezone = lit(tz)
return f.make_timestamptz(year, month, day, hour, minute, second, timezone)
return f.make_timestamptz(year, month, day, hour, minute, second, tz)

Check warning on line 99 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L99

Added line #L99 was not covered by tests
else:
return f.make_timestamp(year, month, day, hour, minute, second)

Check warning on line 101 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L101

Added line #L101 was not covered by tests
elif dtype.is_date():
year = lit(value.year)
month = lit(value.month)
day = lit(value.day)
return sg.exp.DateFromParts(year=year, month=month, day=day)
return sg.exp.DateFromParts(

Check warning on line 103 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L103

Added line #L103 was not covered by tests
year=lit(value.year), month=lit(value.month), day=lit(value.day)
)
elif dtype.is_array():
value_type = dtype.value_type

Check warning on line 107 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L107

Added line #L107 was not covered by tests
return sg.exp.Array.from_arg_list(
[
return f.array(
*(
_literal(
ops.Literal(v, dtype=value_type), value=v, dtype=value_type, **kw
)
for v in value
]
)
)
elif dtype.is_map():
key_type = dtype.key_type
Expand Down Expand Up @@ -146,9 +143,9 @@ def _literal(op, *, value, dtype, **kw):
[sg.exp.Slice(this=k, expression=v) for k, v in zip(keys, values)]
)
elif dtype.is_uuid():
return cast(lit(str(value)), dtype)
return cast(str(value), dtype)
elif dtype.is_binary():
return cast(lit("".join(map("\\x{:02x}".format, value))), dtype)
return cast("".join(map("\\x{:02x}".format, value)), dtype)

Check warning on line 148 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L146-L148

Added lines #L146 - L148 were not covered by tests
else:
raise NotImplementedError(f"Unsupported type: {dtype!r}")

Expand Down Expand Up @@ -974,7 +971,7 @@ def _map_get(op, *, arg, key, default, **_):

@translate_val.register(ops.MapContains)

Check warning on line 972 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L972

Added line #L972 was not covered by tests
def _map_contains(op, *, arg, key, **_):
return f.len(f.element_at(arg, key)).neq(lit(0))
return f.len(f.element_at(arg, key)).neq(0)

Check warning on line 974 in ibis/backends/duckdb/compiler/values.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/compiler/values.py#L974

Added line #L974 was not covered by tests


def _binary_infix(sg_expr: sg.exp._Expression):
Expand Down

0 comments on commit dbf34e5

Please sign in to comment.