From 8acf3bdf99e1303bec9174eb1d05f0f9af7fdb70 Mon Sep 17 00:00:00 2001 From: Ryan Kerr Date: Sat, 20 Apr 2024 01:31:47 -0400 Subject: [PATCH] Use a flag to track updates/deletes instead of guessing --- lib/arel/visitors/clickhouse.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/arel/visitors/clickhouse.rb b/lib/arel/visitors/clickhouse.rb index 6b5354d..eb36950 100644 --- a/lib/arel/visitors/clickhouse.rb +++ b/lib/arel/visitors/clickhouse.rb @@ -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 @@ -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) @@ -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 ' @@ -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' @@ -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