|
7 | 7 | import operator |
8 | 8 | import string |
9 | 9 | from functools import partial, reduce |
10 | | -from typing import TYPE_CHECKING, Any, Callable, ClassVar |
| 10 | +from typing import TYPE_CHECKING, Any, Callable, ClassVar, overload |
11 | 11 |
|
12 | 12 | import sqlglot as sg |
13 | 13 | import sqlglot.expressions as sge |
|
17 | 17 | import ibis.common.patterns as pats |
18 | 18 | import ibis.expr.datatypes as dt |
19 | 19 | import ibis.expr.operations as ops |
| 20 | +import ibis.expr.schema as sch |
20 | 21 | from ibis.backends.sql.rewrites import ( |
21 | 22 | FirstValue, |
22 | 23 | LastValue, |
@@ -53,7 +54,6 @@ def AlterTable(*args, kind="TABLE", **kwargs): |
53 | 54 | if TYPE_CHECKING: |
54 | 55 | from collections.abc import Iterable, Mapping |
55 | 56 |
|
56 | | - import ibis.expr.schema as sch |
57 | 57 | import ibis.expr.types as ir |
58 | 58 | from ibis.backends.sql.datatypes import SqlglotType |
59 | 59 |
|
@@ -575,16 +575,52 @@ def _prepare_params(self, params): |
575 | 575 | result[node] = value |
576 | 576 | return result |
577 | 577 |
|
| 578 | + @overload |
578 | 579 | def to_sqlglot( |
579 | 580 | self, |
580 | | - expr: ir.Expr, |
| 581 | + x: dt.DataType, |
| 582 | + *, |
| 583 | + params: Mapping[ir.Expr, Any] | None = None, |
| 584 | + ) -> sge.DataType: ... |
| 585 | + |
| 586 | + @overload |
| 587 | + def to_sqlglot( |
| 588 | + self, |
| 589 | + x: sch.Schema, |
| 590 | + *, |
| 591 | + params: Mapping[ir.Expr, Any] | None = None, |
| 592 | + ) -> list[sge.ColumnDef]: ... |
| 593 | + |
| 594 | + @overload |
| 595 | + def to_sqlglot( |
| 596 | + self, |
| 597 | + x: ir.Expr, |
581 | 598 | *, |
582 | 599 | limit: str | None = None, |
583 | 600 | params: Mapping[ir.Expr, Any] | None = None, |
584 | | - ): |
| 601 | + ) -> sge.Expression: ... |
| 602 | + |
| 603 | + def to_sqlglot( |
| 604 | + self, |
| 605 | + x: ir.Expr | dt.DataType | sch.Schema, |
| 606 | + *, |
| 607 | + limit: str | None = None, |
| 608 | + params: Mapping[ir.Expr, Any] | None = None, |
| 609 | + ) -> sge.Expression: |
| 610 | + if isinstance(x, (dt.DataType, sch.Schema)): |
| 611 | + return x.to_sqlglot(self.dialect) |
| 612 | + return self._to_sqlglot_expr(x, limit=limit, params=params) |
| 613 | + |
| 614 | + def _to_sqlglot_expr( |
| 615 | + self, |
| 616 | + x: ir.Expr, |
| 617 | + *, |
| 618 | + limit: str | None = None, |
| 619 | + params: Mapping[ir.Expr, Any] | None = None, |
| 620 | + ) -> sge.Expression: |
585 | 621 | import ibis |
586 | 622 |
|
587 | | - table_expr = expr.as_table() |
| 623 | + table_expr = x.as_table() |
588 | 624 |
|
589 | 625 | if limit == "default": |
590 | 626 | limit = ibis.options.sql.default_limit |
|
0 commit comments