Skip to content

Commit

Permalink
fix(ir): ibis.parse_sql() removes where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs authored and cpcloud committed Nov 13, 2023
1 parent ad930b8 commit 522f3a4
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 26 deletions.
14 changes: 9 additions & 5 deletions ibis/expr/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def convert_join(join, catalog):
catalog = catalog.overlay(join)

left_name = join.name
left_table = catalog[left_name]
for right_name, desc in join.joins.items():
left_table = catalog[left_name]
right_table = catalog[right_name]
join_kind = _join_types[desc["side"]]

Expand All @@ -139,11 +139,15 @@ def convert_join(join, catalog):
else:
predicate &= left_key == right_key

catalog[left_name] = left_table.join(
right_table, predicates=predicate, how=join_kind
)
left_table = left_table.join(right_table, predicates=predicate, how=join_kind)

return catalog[left_name]
if join.condition:
predicate = convert(join.condition, catalog=catalog)
left_table = left_table.filter(predicate)

catalog[left_name] = left_table

return left_table


@convert.register(sgp.Aggregate)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
call_outcome = ibis.table(
name="call_outcome", schema={"outcome_text": "string", "id": "int64"}
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
innerjoin = employee.inner_join(call, employee.id == call.employee_id)

result = (
innerjoin.inner_join(call_outcome, call.call_outcome_id == call_outcome.id)
.select(
[
innerjoin.first_name,
innerjoin.last_name,
innerjoin.id,
innerjoin.start_time,
innerjoin.end_time,
innerjoin.employee_id,
innerjoin.call_outcome_id,
innerjoin.call_attempts,
call_outcome.outcome_text,
call_outcome.id.name("id_right"),
]
)
.group_by(call.employee_id)
.aggregate(call.call_attempts.mean().name("avg_attempts"))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)

result = call.group_by(call.employee_id).aggregate(
call.call_attempts.sum().name("attempts")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ibis


employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
leftjoin = employee.left_join(call, employee.id == call.employee_id)

result = leftjoin.group_by(leftjoin.id).aggregate(
call.call_attempts.sum().name("attempts")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
proj = employee.inner_join(call, employee.id == call.employee_id).filter(
employee.id < 5
)

result = proj.select(
[
proj.first_name,
proj.last_name,
proj.id,
call.start_time,
call.end_time,
call.employee_id,
call.call_outcome_id,
call.call_attempts,
proj.first_name.name("first"),
]
).order_by(proj.id.desc())
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
proj = employee.left_join(call, employee.id == call.employee_id).filter(employee.id < 5)

result = proj.select(
[
proj.first_name,
proj.last_name,
proj.id,
call.start_time,
call.end_time,
call.employee_id,
call.call_outcome_id,
call.call_attempts,
proj.first_name.name("first"),
]
).order_by(proj.id.desc())
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
proj = employee.right_join(call, employee.id == call.employee_id).filter(
employee.id < 5
)

result = proj.select(
[
proj.first_name,
proj.last_name,
proj.id,
call.start_time,
call.end_time,
call.employee_id,
call.call_outcome_id,
call.call_attempts,
proj.first_name.name("first"),
]
).order_by(proj.id.desc())
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ibis


employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
proj = employee.filter(employee.id < 5)

result = proj.select(
[proj.first_name, proj.last_name, proj.id, proj.first_name.name("first")]
).order_by(proj.id.desc())
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
proj = employee.left_join(call, employee.id == call.employee_id).filter(employee.id < 5)

result = proj.select(
[
proj.first_name,
proj.last_name,
proj.id,
call.start_time,
call.end_time,
call.employee_id,
call.call_outcome_id,
call.call_attempts,
proj.first_name.name("first"),
]
).order_by(proj.id.desc())
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ibis


call_outcome = ibis.table(
name="call_outcome", schema={"outcome_text": "string", "id": "int64"}
)
employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)
call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
innerjoin = employee.inner_join(call, employee.id == call.employee_id)

result = innerjoin.inner_join(
call_outcome, call.call_outcome_id == call_outcome.id
).select(
[
innerjoin.first_name,
innerjoin.last_name,
innerjoin.id,
innerjoin.start_time,
innerjoin.end_time,
innerjoin.employee_id,
innerjoin.call_outcome_id,
innerjoin.call_attempts,
call_outcome.outcome_text,
call_outcome.id.name("id_right"),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)
agg = call.aggregate(call.call_attempts.mean().name("mean"))

result = call.inner_join(agg, [])
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ibis


call = ibis.table(
name="call",
schema={
"start_time": "timestamp",
"end_time": "timestamp",
"employee_id": "int64",
"call_outcome_id": "int64",
"call_attempts": "int64",
},
)

result = call.aggregate(call.call_attempts.mean().name("mean"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ibis


employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)

result = employee.aggregate(employee.first_name.count().name("_col_0"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ibis


employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)

result = employee.select([employee.first_name, employee.last_name, employee.id])
Loading

0 comments on commit 522f3a4

Please sign in to comment.