Skip to content

Commit

Permalink
perf(sql): prevent sqlglot from extensive deepcopying every time we c…
Browse files Browse the repository at this point in the history
…reate a sqlglot object
  • Loading branch information
kszucs committed Mar 12, 2024
1 parent bc9d757 commit d92e71a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 104 deletions.
11 changes: 3 additions & 8 deletions ibis/backends/clickhouse/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis import util
from ibis.backends.sql.compiler import (
NULL,
STAR,
SQLGlotCompiler,
parenthesize,
)
from ibis.backends.sql.compiler import NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import ClickHouseType
from ibis.backends.sql.dialects import ClickHouse
from ibis.backends.sql.rewrites import rewrite_sample_as_filter
Expand Down Expand Up @@ -163,11 +158,11 @@ def visit_ArrayRepeat(self, op, *, arg, times):
return self.f.arrayFlatten(self.f.arrayMap(func, self.f.range(times)))

def visit_ArraySlice(self, op, *, arg, start, stop):
start = parenthesize(op.start, start)
start = self._add_parens(op.start, start)
start_correct = self.if_(start < 0, start, start + 1)

if stop is not None:
stop = parenthesize(op.stop, stop)
stop = self._add_parens(op.stop, stop)

length = self.if_(
stop < 0,
Expand Down
14 changes: 4 additions & 10 deletions ibis/backends/datafusion/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis.backends.sql.compiler import (
FALSE,
NULL,
STAR,
SQLGlotCompiler,
paren,
)
from ibis.backends.sql.compiler import FALSE, NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import DataFusionType
from ibis.backends.sql.dialects import DataFusion
from ibis.backends.sql.rewrites import rewrite_sample_as_filter
Expand Down Expand Up @@ -261,7 +255,7 @@ def visit_DayOfWeekIndex(self, op, *, arg):

def visit_DayOfWeekName(self, op, *, arg):
return sg.exp.Case(
this=paren(self.f.date_part("dow", arg) + 6) % 7,
this=sge.paren(self.f.date_part("dow", arg) + 6, copy=False) % 7,
ifs=list(starmap(self.if_, enumerate(calendar.day_name))),
)

Expand Down Expand Up @@ -438,7 +432,7 @@ def visit_StringConcat(self, op, *, arg):
def visit_Aggregate(self, op, *, parent, groups, metrics):
"""Support `GROUP BY` expressions in `SELECT` since DataFusion does not."""
quoted = self.quoted
metrics = tuple(starmap(self._dedup_name, metrics.items()))
metrics = tuple(self._cleanup_names(metrics))

if groups:
# datafusion doesn't support count distinct aggregations alongside
Expand All @@ -459,7 +453,7 @@ def visit_Aggregate(self, op, *, parent, groups, metrics):
)
)
table = (
sg.select(*cols, *starmap(self._dedup_name, groups.items()))
sg.select(*cols, *self._cleanup_names(groups))
.from_(parent)
.subquery(parent.alias)
)
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/duckdb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
from ibis.backends.sql.compiler import NULL, STAR, SQLGlotCompiler, paren
from ibis.backends.sql.compiler import NULL, STAR, SQLGlotCompiler
from ibis.backends.sql.datatypes import DuckDBType

_INTERVAL_SUFFIXES = {
Expand Down Expand Up @@ -402,6 +402,7 @@ def visit_StructField(self, op, *, arg, field):
if not isinstance(op.arg, (ops.Field, sge.Struct)):
# parenthesize anything that isn't a simple field access
return sge.Dot(
this=paren(arg), expression=sg.to_identifier(field, quoted=self.quoted)
this=sge.paren(arg),
expression=sg.to_identifier(field, quoted=self.quoted),
)
return super().visit_StructField(op, arg=arg, field=field)
3 changes: 1 addition & 2 deletions ibis/backends/mssql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
STAR,
TRUE,
SQLGlotCompiler,
paren,
)
from ibis.backends.sql.datatypes import MSSQLType
from ibis.backends.sql.dialects import MSSQL
Expand Down Expand Up @@ -189,7 +188,7 @@ def visit_StringLength(self, op, *, arg):
Thanks to @arkanovicz for this glorious hack.
"""
return paren(self.f.len(self.f.concat("A", arg, "Z")) - 2)
return sge.paren(self.f.len(self.f.concat("A", arg, "Z")) - 2, copy=False)

def visit_GroupConcat(self, op, *, arg, sep, where):
if where is not None:
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _to_sqlglot(
assert not isinstance(sql, sge.Subquery)

if isinstance(sql, sge.Table):
sql = sg.select(STAR).from_(sql)
sql = sg.select(STAR, copy=False).from_(sql, copy=False)

assert not isinstance(sql, sge.Subquery)
return sql
Expand All @@ -117,7 +117,7 @@ def compile(
):
"""Compile an Ibis expression to a SQL string."""
query = self._to_sqlglot(expr, limit=limit, params=params, **kwargs)
sql = query.sql(dialect=self.dialect, pretty=True)
sql = query.sql(dialect=self.dialect, pretty=True, copy=False)
self._log(sql)
return sql

Expand Down
Loading

0 comments on commit d92e71a

Please sign in to comment.