Skip to content

Commit 72504fe

Browse files
authored
Use a flag to track updates/deletes instead of guessing (#188)
1 parent f94d9b3 commit 72504fe

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

lib/arel/visitors/clickhouse.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ module Arel
44
module Visitors
55
class Clickhouse < ::Arel::Visitors::ToSql
66

7+
def compile(node, collector = Arel::Collectors::SQLString.new)
8+
@delete_or_update = false
9+
super
10+
end
11+
712
def aggregate(name, o, collector)
813
# replacing function name for materialized view
914
if o.expressions.first && o.expressions.first != '*' && !o.expressions.first.is_a?(String) && o.expressions.first.relation&.is_view
@@ -16,12 +21,11 @@ def aggregate(name, o, collector)
1621
# https://clickhouse.com/docs/en/sql-reference/statements/delete
1722
# DELETE and UPDATE in ClickHouse working only without table name
1823
def visit_Arel_Attributes_Attribute(o, collector)
19-
if collector.value.is_a?(String)
20-
collector << quote_table_name(o.relation.table_alias || o.relation.name) << '.' unless collector.value.start_with?('DELETE FROM ') || collector.value.include?(' UPDATE ')
21-
collector << quote_column_name(o.name)
22-
else
23-
super
24+
unless @delete_or_update
25+
join_name = o.relation.table_alias || o.relation.name
26+
collector << quote_table_name(join_name) << '.'
2427
end
28+
collector << quote_column_name(o.name)
2529
end
2630

2731
def visit_Arel_Nodes_SelectOptions(o, collector)
@@ -30,6 +34,7 @@ def visit_Arel_Nodes_SelectOptions(o, collector)
3034
end
3135

3236
def visit_Arel_Nodes_UpdateStatement(o, collector)
37+
@delete_or_update = true
3338
o = prepare_update_statement(o)
3439

3540
collector << 'ALTER TABLE '
@@ -40,6 +45,11 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
4045
maybe_visit o.limit, collector
4146
end
4247

48+
def visit_Arel_Nodes_DeleteStatement(o, collector)
49+
@delete_or_update = true
50+
super
51+
end
52+
4353
def visit_Arel_Nodes_Final(o, collector)
4454
visit o.expr, collector
4555
collector << ' FINAL'
@@ -64,7 +74,7 @@ def visit_Arel_Nodes_Settings(o, collector)
6474
collector
6575
end
6676

67-
def visit_Arel_Nodes_Using o, collector
77+
def visit_Arel_Nodes_Using(o, collector)
6878
collector << "USING "
6979
visit o.expr, collector
7080
collector

0 commit comments

Comments
 (0)