Skip to content

Commit

Permalink
Use a flag to track updates/deletes instead of guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
leboshi committed Dec 10, 2024
1 parent f94d9b3 commit 8acf3bd
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/arel/visitors/clickhouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ module Arel
module Visitors
class Clickhouse < ::Arel::Visitors::ToSql

def compile(node, collector = Arel::Collectors::SQLString.new)
@delete_or_update = false
super
end

def aggregate(name, o, collector)
# replacing function name for materialized view
if o.expressions.first && o.expressions.first != '*' && !o.expressions.first.is_a?(String) && o.expressions.first.relation&.is_view
Expand All @@ -16,12 +21,11 @@ def aggregate(name, o, collector)
# https://clickhouse.com/docs/en/sql-reference/statements/delete
# DELETE and UPDATE in ClickHouse working only without table name
def visit_Arel_Attributes_Attribute(o, collector)
if collector.value.is_a?(String)
collector << quote_table_name(o.relation.table_alias || o.relation.name) << '.' unless collector.value.start_with?('DELETE FROM ') || collector.value.include?(' UPDATE ')
collector << quote_column_name(o.name)
else
super
unless @delete_or_update
join_name = o.relation.table_alias || o.relation.name
collector << quote_table_name(join_name) << '.'
end
collector << quote_column_name(o.name)
end

def visit_Arel_Nodes_SelectOptions(o, collector)
Expand All @@ -30,6 +34,7 @@ def visit_Arel_Nodes_SelectOptions(o, collector)
end

def visit_Arel_Nodes_UpdateStatement(o, collector)
@delete_or_update = true
o = prepare_update_statement(o)

collector << 'ALTER TABLE '
Expand All @@ -40,6 +45,11 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
maybe_visit o.limit, collector
end

def visit_Arel_Nodes_DeleteStatement(o, collector)
@delete_or_update = true
super
end

def visit_Arel_Nodes_Final(o, collector)
visit o.expr, collector
collector << ' FINAL'
Expand All @@ -64,7 +74,7 @@ def visit_Arel_Nodes_Settings(o, collector)
collector
end

def visit_Arel_Nodes_Using o, collector
def visit_Arel_Nodes_Using(o, collector)
collector << "USING "
visit o.expr, collector
collector
Expand Down

0 comments on commit 8acf3bd

Please sign in to comment.