Class: ClickHouse::SQL::Table

Inherits:
Object
  • Object
show all
Includes:
Part
Defined in:
lib/clickhouse/sql/table.rb

Overview

A validated table reference that qualifies column references.

A table handle renders as its table name in FROM/JOIN position, and builds qualified column references via #[] so shared fragments stay unambiguous when a query joins tables with overlapping column names.

Examples:

Qualified columns from a table handle

events = CH.table("events")
accounts = CH.table("accounts", as: "a")

CH.select(events[:account_id])
  .from(events)
  .left_join(accounts, final: true, on: [
    CH.fragment("{} = {}", accounts[:id], events[:account_id]),
  ])
  .where("{} >= {from:DateTime64(6)}", events[:timestamp], from: from_time)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Part

#placeholder_types, #placeholders

Constructor Details

#initialize(name, as: nil) ⇒ ClickHouse::SQL::Table

Builds a validated table handle.

Parameters:

  • name (String, Symbol, #to_s)

    Trusted table name.

  • as (String, Symbol, nil) (defaults to: nil)

    Optional table alias.



35
36
37
38
# File 'lib/clickhouse/sql/table.rb', line 35

def initialize(name, as: nil)
  @name = validate_identifier!(name)
  @alias_name = as.nil? ? nil : ClickHouse::SQL.validate_alias!(as)
end

Instance Attribute Details

#alias_nameString? (readonly)

Returns Validated table alias, if any.

Returns:

  • (String, nil)

    Validated table alias, if any.



28
29
30
# File 'lib/clickhouse/sql/table.rb', line 28

def alias_name
  @alias_name
end

#nameString (readonly)

Returns Validated table name.

Returns:

  • (String)

    Validated table name.



25
26
27
# File 'lib/clickhouse/sql/table.rb', line 25

def name
  @name
end

Instance Method Details

#[](column) ⇒ ClickHouse::SQL::Raw

Builds a qualified column reference using the alias when present.

Parameters:

  • column (String, Symbol, #to_s)

    Trusted column name or nested path.

Returns:



44
45
46
# File 'lib/clickhouse/sql/table.rb', line 44

def [](column)
  Raw.new("#{qualifier}.#{validate_identifier!(column)}")
end

#columns(*names) ⇒ Array<ClickHouse::SQL::Raw>

Builds qualified column references for many columns at once.

Accepts names as arguments or arrays (t.columns(%i[id timestamp])). SelectQuery#select flattens arrays, so the result can be passed with or without a splat.

Parameters:

  • names (Array<String, Symbol, Array>)

    Trusted column names or nested paths.

Returns:



56
57
58
# File 'lib/clickhouse/sql/table.rb', line 56

def columns(*names)
  names.flatten.map { |name| self[name] }
end

#qualifierString

Returns the name this table's columns are qualified with.

Returns:

  • (String)

    The table alias when present, otherwise the table name.



63
64
65
# File 'lib/clickhouse/sql/table.rb', line 63

def qualifier
  @alias_name || @name
end

#to_redacted_sql(_bind_index_manager = ClickHouse::SQL::BindIndexManager.new) ⇒ String

Renders the table name for redacted logs.

Parameters:

Returns:

  • (String)

    The table name.



78
79
80
# File 'lib/clickhouse/sql/table.rb', line 78

def to_redacted_sql(_bind_index_manager = ClickHouse::SQL::BindIndexManager.new)
  @name
end

#to_sqlString

Renders the table name for FROM/JOIN position.

Returns:

  • (String)

    The table name.



70
71
72
# File 'lib/clickhouse/sql/table.rb', line 70

def to_sql
  @name
end