Skip to content

Commit 2acaa16

Browse files
committed
refactor(sql): clean up unnecessary use of explicit visit methods
1 parent ebee0e9 commit 2acaa16

File tree

11 files changed

+15
-38
lines changed

11 files changed

+15
-38
lines changed

ibis/backends/sql/compilers/base.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ class SQLGlotCompiler(abc.ABC):
381381
ops.Translate: "translate",
382382
ops.Unnest: "explode",
383383
ops.Uppercase: "upper",
384+
ops.RandomUUID: "uuid",
385+
ops.RandomScalar: "rand",
384386
}
385387

386388
BINARY_INFIX_OPS = {
@@ -869,14 +871,6 @@ def visit_Floor(self, op, *, arg):
869871
def visit_Round(self, op, *, arg, digits):
870872
return self.cast(self.f.round(arg, digits), op.dtype)
871873

872-
### Random Noise
873-
874-
def visit_RandomScalar(self, op, **kwargs):
875-
return self.f.rand()
876-
877-
def visit_RandomUUID(self, op, **kwargs):
878-
return self.f.uuid()
879-
880874
### Dtype Dysmorphia
881875

882876
def visit_TryCast(self, op, *, arg, to):

ibis/backends/sql/compilers/bigquery/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ class BigQueryCompiler(SQLGlotCompiler):
199199
ops.TimeFromHMS: "time_from_parts",
200200
ops.TimestampNow: "current_timestamp",
201201
ops.ExtractHost: "net.host",
202-
ops.ArgMin: "min_by",
203-
ops.ArgMax: "max_by",
202+
ops.RandomUUID: "generate_uuid",
204203
}
205204

206205
def to_sqlglot(
@@ -997,9 +996,6 @@ def visit_CountDistinct(self, op, *, arg, where):
997996
arg = self.if_(where, arg, NULL)
998997
return self.f.count(sge.Distinct(expressions=[arg]))
999998

1000-
def visit_RandomUUID(self, op, **kwargs):
1001-
return self.f.generate_uuid()
1002-
1003999
def visit_ExtractFile(self, op, *, arg):
10041000
return self._pudf("cw_url_extract_file", arg)
10051001

ibis/backends/sql/compilers/clickhouse.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class ClickHouseCompiler(SQLGlotCompiler):
115115
ops.TimestampNow: "now",
116116
ops.TypeOf: "toTypeName",
117117
ops.Unnest: "arrayJoin",
118+
ops.RandomUUID: "generateUUIDv4",
119+
ops.RandomScalar: "randCanonical",
118120
}
119121

120122
@staticmethod
@@ -719,12 +721,6 @@ def visit_TimestampRange(self, op, *, start, stop, step):
719721
def visit_RegexSplit(self, op, *, arg, pattern):
720722
return self.f.splitByRegexp(pattern, self.cast(arg, dt.String(nullable=False)))
721723

722-
def visit_RandomScalar(self, op, **kwargs):
723-
return self.f.randCanonical()
724-
725-
def visit_RandomUUID(self, op, **kwargs):
726-
return self.f.generateUUIDv4()
727-
728724
@staticmethod
729725
def _generate_groups(groups):
730726
return groups

ibis/backends/sql/compilers/datafusion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def visit_GroupConcat(self, op, *, arg, sep, where, order_by):
530530
def visit_ArrayFlatten(self, op, *, arg):
531531
return self.if_(arg.is_(NULL), NULL, self.f.flatten(arg))
532532

533-
def visit_RandomUUID(self, op, **kw):
533+
def visit_RandomUUID(self, op):
534534
return self.f.anon.uuid()
535535

536536

ibis/backends/sql/compilers/duckdb.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class DuckDBCompiler(SQLGlotCompiler):
103103
ops.GeoWithin: "st_within",
104104
ops.GeoX: "st_x",
105105
ops.GeoY: "st_y",
106+
ops.RandomScalar: "random",
106107
}
107108

108109
def to_sqlglot(
@@ -608,12 +609,6 @@ def visit_StructField(self, op, *, arg, field):
608609
)
609610
return super().visit_StructField(op, arg=arg, field=field)
610611

611-
def visit_RandomScalar(self, op, **kwargs):
612-
return self.f.random()
613-
614-
def visit_RandomUUID(self, op, **kwargs):
615-
return self.f.uuid()
616-
617612
def visit_TypeOf(self, op, *, arg):
618613
return self.f.coalesce(self.f.nullif(self.f.typeof(arg), '"NULL"'), "NULL")
619614

ibis/backends/sql/compilers/impala.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def visit_CountDistinct(self, op, *, arg, where):
130130
def visit_Xor(self, op, *, left, right):
131131
return sg.and_(sg.or_(left, right), sg.not_(sg.and_(left, right)))
132132

133-
def visit_RandomScalar(self, op, **_):
133+
def visit_RandomScalar(self, op):
134134
return self.f.rand(self.f.utc_to_unix_micros(self.f.utc_timestamp()))
135135

136136
def visit_DayOfWeekIndex(self, op, *, arg):

ibis/backends/sql/compilers/mssql.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class MSSQLCompiler(SQLGlotCompiler):
135135
ops.TimestampNow: "sysdatetime",
136136
ops.Min: "min",
137137
ops.Max: "max",
138+
ops.RandomUUID: "newid",
138139
}
139140

140141
NAN = sg.func("double", sge.convert("NaN"))
@@ -177,10 +178,7 @@ def to_sqlglot(
177178
table_expr = table_expr.mutate(**conversions)
178179
return super().to_sqlglot(table_expr, limit=limit, params=params)
179180

180-
def visit_RandomUUID(self, op, **_):
181-
return self.f.newid()
182-
183-
def visit_RandomScalar(self, op, **_):
181+
def visit_RandomScalar(self, op):
184182
# By default RAND() will generate the same value for all calls within a
185183
# query. The standard way to work around this is to pass in a unique
186184
# value per call, which `CHECKSUM(NEWID())` provides.

ibis/backends/sql/compilers/oracle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def visit_Log(self, op, *, arg, base):
214214
def visit_IsInf(self, op, *, arg):
215215
return arg.isin(self.POS_INF, self.NEG_INF)
216216

217-
def visit_RandomScalar(self, op, **_):
217+
def visit_RandomScalar(self, op):
218218
# Not using FuncGen here because of dotted function call
219219
return sg.func("dbms_random.value")
220220

ibis/backends/sql/compilers/postgres.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class PostgresCompiler(SQLGlotCompiler):
119119
ops.MapValues: "avals",
120120
ops.RegexSearch: "regexp_like",
121121
ops.TimeFromHMS: "make_time",
122+
ops.RandomUUID: "gen_random_uuid",
122123
}
123124

124125
def to_sqlglot(
@@ -179,9 +180,6 @@ def _compile_python_udf(self, udf_node: ops.ScalarUDF):
179180
args=", ".join(argnames),
180181
)
181182

182-
def visit_RandomUUID(self, op, **kwargs):
183-
return self.f.gen_random_uuid()
184-
185183
def visit_Mode(self, op, *, arg, where):
186184
expr = self.f.mode()
187185
expr = sge.WithinGroup(

ibis/backends/sql/compilers/snowflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def visit_MapLength(self, op, *, arg):
374374
def visit_Log(self, op, *, arg, base):
375375
return self.f.log(base, arg)
376376

377-
def visit_RandomScalar(self, op, **_):
377+
def visit_RandomScalar(self, op):
378378
return self.f.uniform(
379379
self.f.to_double(0.0), self.f.to_double(1.0), self.f.random()
380380
)

ibis/backends/sql/compilers/sqlite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ def visit_Clip(self, op, *, arg, lower, upper):
194194

195195
return arg
196196

197-
def visit_RandomScalar(self, op, **kwargs):
197+
def visit_RandomScalar(self, op):
198198
return 0.5 + self.f.random() / sge.Literal.number(float(-1 << 64))
199199

200200
def visit_Cot(self, op, *, arg):
201-
return 1 / self.f.tan(arg)
201+
return 1.0 / self.f.tan(arg)
202202

203203
def visit_ArgMin(self, *args, **kwargs):
204204
return self._visit_arg_reduction("min", *args, **kwargs)

0 commit comments

Comments
 (0)