Class: Arel::Visitors::Clickhouse

Inherits:
ToSql
  • Object
show all
Defined in:
lib/arel/visitors/clickhouse.rb

Instance Method Summary collapse

Instance Method Details

#aggregate(name, o, collector) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/arel/visitors/clickhouse.rb', line 7

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
    super("#{name.downcase}Merge", o, collector)
  else
    super
  end
end

#sanitize_as_setting_name(value) ⇒ Object



104
105
106
107
# File 'lib/arel/visitors/clickhouse.rb', line 104

def sanitize_as_setting_name(value)
  return value if Arel::Nodes::SqlLiteral === value
  @connection.sanitize_as_setting_name(value)
end

#sanitize_as_setting_value(value) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/arel/visitors/clickhouse.rb', line 96

def sanitize_as_setting_value(value)
  if value == :default
    'DEFAULT'
  else
    quote(value)
  end
end

#visit_Arel_Attributes_Attribute(o, collector) ⇒ Object

clickhouse.com/docs/en/sql-reference/statements/delete DELETE and UPDATE in ClickHouse working only without table name



18
19
20
21
22
23
24
25
# File 'lib/arel/visitors/clickhouse.rb', line 18

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
  end
end

#visit_Arel_Nodes_DoesNotMatch(o, collector) ⇒ Object



83
84
85
86
# File 'lib/arel/visitors/clickhouse.rb', line 83

def visit_Arel_Nodes_DoesNotMatch(o, collector)
  op = o.case_sensitive ? " NOT LIKE " : " NOT ILIKE "
  infix_value o, collector, op
end

#visit_Arel_Nodes_Final(o, collector) ⇒ Object



43
44
45
46
47
# File 'lib/arel/visitors/clickhouse.rb', line 43

def visit_Arel_Nodes_Final(o, collector)
  visit o.expr, collector
  collector << ' FINAL'
  collector
end

#visit_Arel_Nodes_GroupingSets(o, collector) ⇒ Object



49
50
51
52
# File 'lib/arel/visitors/clickhouse.rb', line 49

def visit_Arel_Nodes_GroupingSets(o, collector)
  collector << 'GROUPING SETS '
  grouping_array_or_grouping_element(o.expr, collector)
end

#visit_Arel_Nodes_LimitBy(o, collector) ⇒ Object



73
74
75
76
# File 'lib/arel/visitors/clickhouse.rb', line 73

def visit_Arel_Nodes_LimitBy(o, collector)
  collector << "LIMIT #{o.expr} BY #{o.column}"
  collector
end

#visit_Arel_Nodes_Matches(o, collector) ⇒ Object



78
79
80
81
# File 'lib/arel/visitors/clickhouse.rb', line 78

def visit_Arel_Nodes_Matches(o, collector)
  op = o.case_sensitive ? " LIKE " : " ILIKE "
  infix_value o, collector, op
end

#visit_Arel_Nodes_Rows(o, collector) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/arel/visitors/clickhouse.rb', line 88

def visit_Arel_Nodes_Rows(o, collector)
  if o.expr.is_a?(String)
    collector << "ROWS #{o.expr}"
  else
    super
  end
end

#visit_Arel_Nodes_SelectOptions(o, collector) ⇒ Object



27
28
29
30
# File 'lib/arel/visitors/clickhouse.rb', line 27

def visit_Arel_Nodes_SelectOptions(o, collector)
  maybe_visit o.limit_by, collector
  maybe_visit o.settings, super
end

#visit_Arel_Nodes_Settings(o, collector) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arel/visitors/clickhouse.rb', line 54

def visit_Arel_Nodes_Settings(o, collector)
  return collector if o.expr.empty?

  collector << "SETTINGS "
  o.expr.each_with_index do |(key, value), i|
    collector << ", " if i > 0
    collector << key.to_s.gsub(/\W+/, "")
    collector << " = "
    collector << sanitize_as_setting_value(value)
  end
  collector
end

#visit_Arel_Nodes_UpdateStatement(o, collector) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/arel/visitors/clickhouse.rb', line 32

def visit_Arel_Nodes_UpdateStatement(o, collector)
  o = prepare_update_statement(o)

  collector << 'ALTER TABLE '
  collector = visit o.relation, collector
  collect_nodes_for o.values, collector, ' UPDATE '
  collect_nodes_for o.wheres, collector, ' WHERE ', ' AND '
  collect_nodes_for o.orders, collector, ' ORDER BY '
  maybe_visit o.limit, collector
end

#visit_Arel_Nodes_Using(o, collector) ⇒ Object



67
68
69
70
71
# File 'lib/arel/visitors/clickhouse.rb', line 67

def visit_Arel_Nodes_Using o, collector
  collector << "USING "
  visit o.expr, collector
  collector
end