Skip to content

Commit

Permalink
refactor(ir): give unbound tables namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Feb 12, 2024
1 parent 6edc047 commit 8ac2d2a
Show file tree
Hide file tree
Showing 48 changed files with 225 additions and 195 deletions.
39 changes: 26 additions & 13 deletions ibis/backends/base/sqlglot/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import abc
import calendar
import functools
import itertools
import math
import operator
import string
from collections.abc import Iterator, Mapping
from functools import partial, singledispatchmethod
from functools import partial, reduce, singledispatchmethod
from itertools import starmap
from typing import TYPE_CHECKING, Any, Callable

Expand Down Expand Up @@ -244,8 +243,7 @@ def fn(node, _, **kwargs):
}

op = op.replace(
replace_scalar_parameter(params)
| functools.reduce(operator.or_, self.rewrites)
replace_scalar_parameter(params) | reduce(operator.or_, self.rewrites)
)
op = sqlize(op)
# apply translate rules in topological order
Expand Down Expand Up @@ -553,7 +551,7 @@ def visit_DayOfWeekName(self, op, *, arg):
# Saturday == 6
return sge.Case(
this=(self.f.dayofweek(arg) + 6) % 7,
ifs=list(starmap(self.if_, enumerate(calendar.day_name))),
ifs=list(itertools.starmap(self.if_, enumerate(calendar.day_name))),
)

@visit_node.register(ops.IntervalFromInteger)
Expand Down Expand Up @@ -874,10 +872,11 @@ def visit_ArrayConcat(self, op, *, arg):
def _dedup_name(
self, key: str, value: sge.Expression
) -> Iterator[sge.Alias | sge.Column]:
"""Don't alias columns that are already named the same as their alias."""
return (
value.as_(key, quoted=self.quoted)
if not isinstance(value, sge.Column) or key != value.name
else value
value
if isinstance(value, sge.Column) and key == value.name
else value.as_(key, quoted=self.quoted)
)

@visit_node.register(Select)
Expand Down Expand Up @@ -906,15 +905,29 @@ def visit_DummyTable(self, op, *, values):
return sg.select(*starmap(self._dedup_name, values.items()))

@visit_node.register(ops.UnboundTable)
def visit_UnboundTable(self, op, *, name: str, schema: sch.Schema):
return sg.table(name, quoted=self.quoted)
def visit_UnboundTable(
self, op, *, name: str, schema: sch.Schema, namespace: ops.Namespace
) -> sg.Table:
return sg.table(
name, db=namespace.schema, catalog=namespace.database, quoted=self.quoted
)

@visit_node.register(ops.InMemoryTable)
def visit_InMemoryTable(self, op, *, name: str, schema: sch.Schema, data):
def visit_InMemoryTable(
self, op, *, name: str, schema: sch.Schema, data
) -> sg.Table:
return sg.table(name, quoted=self.quoted)

@visit_node.register(ops.DatabaseTable)
def visit_DatabaseTable(self, op, *, name, namespace, schema, source):
def visit_DatabaseTable(
self,
op,
*,
name: str,
schema: sch.Schema,
source: Any,
namespace: ops.Namespace,
) -> sg.Table:
return sg.table(
name, db=namespace.schema, catalog=namespace.database, quoted=self.quoted
)
Expand Down Expand Up @@ -1108,7 +1121,7 @@ def visit_DropNa(self, op, *, parent, how, subset):
]

if subset:
predicate = functools.reduce(
predicate = reduce(
sg.and_ if how == "any" else sg.or_,
(sg.not_(col.is_(NULL)) for col in subset),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ FROM (
"t0"."L_SHIPINSTRUCT" AS "l_shipinstruct",
"t0"."L_SHIPMODE" AS "l_shipmode",
"t0"."L_COMMENT" AS "l_comment"
FROM "LINEITEM" AS "t0"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."LINEITEM" AS "t0"
WHERE
"t0"."L_SHIPDATE" <= DATE_FROM_PARTS(1998, 9, 2)
) AS "t1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ FROM (
"t0"."l_shipinstruct",
"t0"."l_shipmode",
"t0"."l_comment"
FROM "lineitem" AS "t0"
FROM "hive"."ibis_sf1"."lineitem" AS "t0"
WHERE
"t0"."l_shipdate" <= FROM_ISO8601_DATE('1998-09-02')
) AS "t1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ FROM (
"t0"."P_CONTAINER" AS "p_container",
"t0"."P_RETAILPRICE" AS "p_retailprice",
"t0"."P_COMMENT" AS "p_comment"
FROM "PART" AS "t0"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."PART" AS "t0"
) AS "t10"
INNER JOIN (
SELECT
Expand All @@ -57,7 +57,7 @@ FROM (
"t1"."PS_AVAILQTY" AS "ps_availqty",
"t1"."PS_SUPPLYCOST" AS "ps_supplycost",
"t1"."PS_COMMENT" AS "ps_comment"
FROM "PARTSUPP" AS "t1"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."PARTSUPP" AS "t1"
) AS "t11"
ON "t10"."p_partkey" = "t11"."ps_partkey"
INNER JOIN (
Expand All @@ -69,7 +69,7 @@ FROM (
"t2"."S_PHONE" AS "s_phone",
"t2"."S_ACCTBAL" AS "s_acctbal",
"t2"."S_COMMENT" AS "s_comment"
FROM "SUPPLIER" AS "t2"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."SUPPLIER" AS "t2"
) AS "t13"
ON "t13"."s_suppkey" = "t11"."ps_suppkey"
INNER JOIN (
Expand All @@ -78,15 +78,15 @@ FROM (
"t3"."N_NAME" AS "n_name",
"t3"."N_REGIONKEY" AS "n_regionkey",
"t3"."N_COMMENT" AS "n_comment"
FROM "NATION" AS "t3"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."NATION" AS "t3"
) AS "t15"
ON "t13"."s_nationkey" = "t15"."n_nationkey"
INNER JOIN (
SELECT
"t4"."R_REGIONKEY" AS "r_regionkey",
"t4"."R_NAME" AS "r_name",
"t4"."R_COMMENT" AS "r_comment"
FROM "REGION" AS "t4"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."REGION" AS "t4"
) AS "t17"
ON "t15"."n_regionkey" = "t17"."r_regionkey"
) AS "t26"
Expand Down Expand Up @@ -146,7 +146,7 @@ WHERE
"t1"."PS_AVAILQTY" AS "ps_availqty",
"t1"."PS_SUPPLYCOST" AS "ps_supplycost",
"t1"."PS_COMMENT" AS "ps_comment"
FROM "PARTSUPP" AS "t1"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."PARTSUPP" AS "t1"
) AS "t12"
INNER JOIN (
SELECT
Expand All @@ -157,7 +157,7 @@ WHERE
"t2"."S_PHONE" AS "s_phone",
"t2"."S_ACCTBAL" AS "s_acctbal",
"t2"."S_COMMENT" AS "s_comment"
FROM "SUPPLIER" AS "t2"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."SUPPLIER" AS "t2"
) AS "t14"
ON "t14"."s_suppkey" = "t12"."ps_suppkey"
INNER JOIN (
Expand All @@ -166,15 +166,15 @@ WHERE
"t3"."N_NAME" AS "n_name",
"t3"."N_REGIONKEY" AS "n_regionkey",
"t3"."N_COMMENT" AS "n_comment"
FROM "NATION" AS "t3"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."NATION" AS "t3"
) AS "t16"
ON "t14"."s_nationkey" = "t16"."n_nationkey"
INNER JOIN (
SELECT
"t4"."R_REGIONKEY" AS "r_regionkey",
"t4"."R_NAME" AS "r_name",
"t4"."R_COMMENT" AS "r_comment"
FROM "REGION" AS "t4"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."REGION" AS "t4"
) AS "t18"
ON "t16"."n_regionkey" = "t18"."r_regionkey"
) AS "t27"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ FROM (
"t0"."p_container",
CAST("t0"."p_retailprice" AS DECIMAL(15, 2)) AS "p_retailprice",
"t0"."p_comment"
FROM "part" AS "t0"
FROM "hive"."ibis_sf1"."part" AS "t0"
) AS "t14"
INNER JOIN (
SELECT
Expand All @@ -57,7 +57,7 @@ FROM (
"t1"."ps_availqty",
CAST("t1"."ps_supplycost" AS DECIMAL(15, 2)) AS "ps_supplycost",
"t1"."ps_comment"
FROM "partsupp" AS "t1"
FROM "hive"."ibis_sf1"."partsupp" AS "t1"
) AS "t15"
ON "t14"."p_partkey" = "t15"."ps_partkey"
INNER JOIN (
Expand All @@ -69,7 +69,7 @@ FROM (
"t2"."s_phone",
CAST("t2"."s_acctbal" AS DECIMAL(15, 2)) AS "s_acctbal",
"t2"."s_comment"
FROM "supplier" AS "t2"
FROM "hive"."ibis_sf1"."supplier" AS "t2"
) AS "t17"
ON "t17"."s_suppkey" = "t15"."ps_suppkey"
INNER JOIN (
Expand All @@ -78,15 +78,15 @@ FROM (
"t3"."n_name",
"t3"."n_regionkey",
"t3"."n_comment"
FROM "nation" AS "t3"
FROM "hive"."ibis_sf1"."nation" AS "t3"
) AS "t10"
ON "t17"."s_nationkey" = "t10"."n_nationkey"
INNER JOIN (
SELECT
"t4"."r_regionkey",
"t4"."r_name",
"t4"."r_comment"
FROM "region" AS "t4"
FROM "hive"."ibis_sf1"."region" AS "t4"
) AS "t12"
ON "t10"."n_regionkey" = "t12"."r_regionkey"
) AS "t26"
Expand Down Expand Up @@ -146,7 +146,7 @@ WHERE
"t1"."ps_availqty",
CAST("t1"."ps_supplycost" AS DECIMAL(15, 2)) AS "ps_supplycost",
"t1"."ps_comment"
FROM "partsupp" AS "t1"
FROM "hive"."ibis_sf1"."partsupp" AS "t1"
) AS "t16"
INNER JOIN (
SELECT
Expand All @@ -157,7 +157,7 @@ WHERE
"t2"."s_phone",
CAST("t2"."s_acctbal" AS DECIMAL(15, 2)) AS "s_acctbal",
"t2"."s_comment"
FROM "supplier" AS "t2"
FROM "hive"."ibis_sf1"."supplier" AS "t2"
) AS "t18"
ON "t18"."s_suppkey" = "t16"."ps_suppkey"
INNER JOIN (
Expand All @@ -166,15 +166,15 @@ WHERE
"t3"."n_name",
"t3"."n_regionkey",
"t3"."n_comment"
FROM "nation" AS "t3"
FROM "hive"."ibis_sf1"."nation" AS "t3"
) AS "t11"
ON "t18"."s_nationkey" = "t11"."n_nationkey"
INNER JOIN (
SELECT
"t4"."r_regionkey",
"t4"."r_name",
"t4"."r_comment"
FROM "region" AS "t4"
FROM "hive"."ibis_sf1"."region" AS "t4"
) AS "t13"
ON "t11"."n_regionkey" = "t13"."r_regionkey"
) AS "t27"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ FROM (
"t0"."C_ACCTBAL" AS "c_acctbal",
"t0"."C_MKTSEGMENT" AS "c_mktsegment",
"t0"."C_COMMENT" AS "c_comment"
FROM "CUSTOMER" AS "t0"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."CUSTOMER" AS "t0"
) AS "t6"
INNER JOIN (
SELECT
Expand All @@ -104,7 +104,7 @@ FROM (
"t1"."O_CLERK" AS "o_clerk",
"t1"."O_SHIPPRIORITY" AS "o_shippriority",
"t1"."O_COMMENT" AS "o_comment"
FROM "ORDERS" AS "t1"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."ORDERS" AS "t1"
) AS "t7"
ON "t6"."c_custkey" = "t7"."o_custkey"
INNER JOIN (
Expand All @@ -125,7 +125,7 @@ FROM (
"t2"."L_SHIPINSTRUCT" AS "l_shipinstruct",
"t2"."L_SHIPMODE" AS "l_shipmode",
"t2"."L_COMMENT" AS "l_comment"
FROM "LINEITEM" AS "t2"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."LINEITEM" AS "t2"
) AS "t8"
ON "t8"."l_orderkey" = "t7"."o_orderkey"
) AS "t11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ FROM (
CAST("t0"."c_acctbal" AS DECIMAL(15, 2)) AS "c_acctbal",
"t0"."c_mktsegment",
"t0"."c_comment"
FROM "customer" AS "t0"
FROM "hive"."ibis_sf1"."customer" AS "t0"
) AS "t6"
INNER JOIN (
SELECT
Expand All @@ -104,7 +104,7 @@ FROM (
"t1"."o_clerk",
"t1"."o_shippriority",
"t1"."o_comment"
FROM "orders" AS "t1"
FROM "hive"."ibis_sf1"."orders" AS "t1"
) AS "t7"
ON "t6"."c_custkey" = "t7"."o_custkey"
INNER JOIN (
Expand All @@ -125,7 +125,7 @@ FROM (
"t2"."l_shipinstruct",
"t2"."l_shipmode",
"t2"."l_comment"
FROM "lineitem" AS "t2"
FROM "hive"."ibis_sf1"."lineitem" AS "t2"
) AS "t8"
ON "t8"."l_orderkey" = "t7"."o_orderkey"
) AS "t11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ FROM (
"t0"."O_CLERK" AS "o_clerk",
"t0"."O_SHIPPRIORITY" AS "o_shippriority",
"t0"."O_COMMENT" AS "o_comment"
FROM "ORDERS" AS "t0"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."ORDERS" AS "t0"
) AS "t2"
WHERE
EXISTS(
SELECT
1 AS "1"
FROM "LINEITEM" AS "t1"
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."LINEITEM" AS "t1"
WHERE
(
"t1"."L_ORDERKEY" = "t2"."o_orderkey"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ FROM (
"t0"."o_clerk",
"t0"."o_shippriority",
"t0"."o_comment"
FROM "orders" AS "t0"
FROM "hive"."ibis_sf1"."orders" AS "t0"
) AS "t2"
WHERE
EXISTS(
SELECT
1 AS "1"
FROM "lineitem" AS "t1"
FROM "hive"."ibis_sf1"."lineitem" AS "t1"
WHERE
(
"t1"."l_orderkey" = "t2"."o_orderkey"
Expand Down
Loading

0 comments on commit 8ac2d2a

Please sign in to comment.