Class: ActiveRecordADBCAdapter::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/activerecord_adbc_adapter/result.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend, table) ⇒ Result

Returns a new instance of Result.



5
6
7
8
9
# File 'lib/activerecord_adbc_adapter/result.rb', line 5

def initialize(backend, table)
  @backend = backend
  @table = table
  @schema = @table.schema
end

Instance Method Details

#attach_model(model) ⇒ Object

This must be called before calling other methods.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/activerecord_adbc_adapter/result.rb', line 12

def attach_model(model)
  return unless @backend == "sqlite"

  model_columns_hash = model.columns_hash
  casted = false
  new_chunked_arrays = []
  new_fields = []
  @table.columns.zip(@schema.fields) do |column, field|
    chunked_array = nil
    model_column = model_columns_hash[field.name]
    if model_column
      casted_type = nil
      case model_column..type
      when :boolean
        case field.data_type
        when Arrow::IntegerDataType
          casted_type = Arrow::BooleanDataType.new
        end
      when :date
        case field.data_type
        when Arrow::StringDataType
          casted_type = Arrow::Date32DataType.new
        end
      when :datetime
        case field.data_type
        when Arrow::StringDataType
          casted_type = Arrow::TimestampDataType.new(:nano)
        end
      end
      if casted_type
        chunked_array = column.cast(casted_type)
        field = Arrow::Field.new(field.name, casted_type)
        casted = true
      end
    end
    new_chunked_arrays << (chunked_array || column.data)
    new_fields << field
  end
  return unless casted

  @schema = Arrow::Schema.new(new_fields)
  @table = Arrow::Table.new(@schema, new_chunked_arrays)
end

#cast_values(type_overrides = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/activerecord_adbc_adapter/result.rb', line 95

def cast_values(type_overrides = {})
  # TODO: type_overrides support
  if fields.size == 1
    rows.map(&:first)
  else
    rows
  end
end

#column_typesObject



60
61
62
63
64
65
# File 'lib/activerecord_adbc_adapter/result.rb', line 60

def column_types
  @column_types ||= fields.inject({}) do |types, field|
    types[field.name] = resolve_type(field.data_type)
    types
  end
end

#columnsObject



56
57
58
# File 'lib/activerecord_adbc_adapter/result.rb', line 56

def columns
  @columns ||= fields.collect(&:name)
end

#each(&block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/activerecord_adbc_adapter/result.rb', line 83

def each(&block)
  return to_enum(__method__) unless block_given?

  rows.each do |record|
    yield(Hash[@columns.zip(record)])
  end
end

#each_record_batchObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/activerecord_adbc_adapter/result.rb', line 108

def each_record_batch
  return to_enum(__method__) unless block_given?

  reader = Arrow::TableBatchReader.new(@table)
  loop do
    record_batch = reader.read_next
    break if record_batch.nil?
    yield(record_batch)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/activerecord_adbc_adapter/result.rb', line 79

def empty?
  length.zero?
end

#includes_column?(name) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/activerecord_adbc_adapter/result.rb', line 67

def includes_column?(name)
  columns.include?(name)
end

#indexed_rowsObject



91
92
93
# File 'lib/activerecord_adbc_adapter/result.rb', line 91

def indexed_rows
  @indexed_rows ||= to_a
end

#lengthObject



75
76
77
# File 'lib/activerecord_adbc_adapter/result.rb', line 75

def length
  to_arrow.length
end

#rowsObject



71
72
73
# File 'lib/activerecord_adbc_adapter/result.rb', line 71

def rows
  @rows ||= to_arrow.raw_records
end

#to_arrowObject



104
105
106
# File 'lib/activerecord_adbc_adapter/result.rb', line 104

def to_arrow
  @table
end