Class: DWH::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/dwh/table.rb

Overview

Container to map to a data warehouse table. If you initialize with a fuly qualified table name , it will automatically create catalog and schema components.

This is the object returned from metadata method call of an adapter

Examples

Table.new("dwh.public.hello_world_table")

table_stats_instance = adapter.stats("my_table", schema: "dwh")
Table.new("my_table", schema: "dwh", stats: table_stats_instance)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(physical_name, schema: nil, catalog: nil, table_stats: nil) ⇒ Table

Returns a new instance of Table.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dwh/table.rb', line 20

def initialize(physical_name, schema: nil, catalog: nil, table_stats: nil)
  parts = physical_name.split('.')

  @physical_name = parts.last
  @table_stats = table_stats
  @catalog = catalog
  @schema = schema

  @catalog = parts.first if @catalog.nil? && parts.length > 2

  if @schema.nil?
    if parts.length == 2
      @schema = parts.first
    elsif parts.length > 2
      @schema = parts[1]
    end
  end

  @columns = []
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



18
19
20
# File 'lib/dwh/table.rb', line 18

def catalog
  @catalog
end

#columnsObject (readonly)

Returns the value of attribute columns.



18
19
20
# File 'lib/dwh/table.rb', line 18

def columns
  @columns
end

#physical_nameObject (readonly)

Returns the value of attribute physical_name.



18
19
20
# File 'lib/dwh/table.rb', line 18

def physical_name
  @physical_name
end

#schemaObject (readonly)

Returns the value of attribute schema.



18
19
20
# File 'lib/dwh/table.rb', line 18

def schema
  @schema
end

#table_statsObject (readonly)

Returns the value of attribute table_stats.



18
19
20
# File 'lib/dwh/table.rb', line 18

def table_stats
  @table_stats
end

Class Method Details

.from_hash_or_json(physical_name, metadata) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dwh/table.rb', line 83

def self.from_hash_or_json(physical_name, )
   = JSON.parse() if .is_a?(String)
  .symbolize_keys!

  stats = TableStats.new(**[:stats].symbolize_keys) if .key?(:stats)
  table = new(physical_name, table_stats: stats)

  [:columns]&.each do |col|
    col.symbolize_keys!
    table << Column.new(
      name: col[:name],
      data_type: col[:data_type],
      precision: col[:precision],
      scale: col[:scale],
      max_char_length: col[:max_char_length],
      schema_type: col[:schema_type]
    )
  end

  table
end

Instance Method Details

#<<(column) ⇒ Object



41
42
43
# File 'lib/dwh/table.rb', line 41

def <<(column)
  @columns << column
end

#catalog_and_schema?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/dwh/table.rb', line 53

def catalog_and_schema?
  catalog && schema
end

#catalog_or_schema?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dwh/table.rb', line 57

def catalog_or_schema?
  catalog || schema
end

#find_column(name) ⇒ Object



79
80
81
# File 'lib/dwh/table.rb', line 79

def find_column(name)
  columns.find { |c| c.name.downcase == name.downcase }
end

#fully_qualified_schema_nameObject



49
50
51
# File 'lib/dwh/table.rb', line 49

def fully_qualified_schema_name
  [catalog, schema].compact.join('.')
end

#fully_qualified_table_nameObject



45
46
47
# File 'lib/dwh/table.rb', line 45

def fully_qualified_table_name
  [catalog, schema, physical_name].compact.join('.')
end

#sizeObject



75
76
77
# File 'lib/dwh/table.rb', line 75

def size
  @table_stats&.row_count || 0
end

#statsObject



61
62
63
# File 'lib/dwh/table.rb', line 61

def stats
  @table_stats
end

#to_hObject



65
66
67
68
69
70
71
72
73
# File 'lib/dwh/table.rb', line 65

def to_h
  {
    physical_name: physical_name,
    schema: schema,
    catalog: catalog,
    columns: columns.map(&:to_h),
    stats: table_stats&.to_h
  }
end