class ActiveRecord::ConnectionAdapters::Kudu::SchemaCreation

Private Instance Methods

add_column_options!(sql, options) click to toggle source
# File lib/active_record/connection_adapters/kudu/schema_creation.rb, line 60
def add_column_options!(sql, options)
  # [NOT] NULL
  if options[:primary_key]
    sql += ' NOT NULL'
  else
    options[:null] = true if options[:null].nil?
    sql += options[:null] ? ' NULL' : ' NOT NULL'
  end

  # Encodings:
  #  AUTO_ENCODING, PLAIN_ENCODING, RLE, DICT_ENCODING, BIT_SHUFFLE, PREFIX_ENCODING
  sql += " ENCODING #{options[:encoding].to_s}" if options[:encoding]

  # Compressions:
  #  LZ4, SNAPPY, and ZLIB
  sql += " COMPRESSION #{options[:compression].to_s}" if options[:compression]

  # Default values
  sql += " DEFAULT #{quote_default_expression(options[:default], options[:column])}" unless options[:default].nil?

  # Block size
  sql += " BLOCK SIZE #{options[:block_size].to_i}" if options[:block_size]

  sql
end
visit_AddColumnDefinition(obj) click to toggle source
# File lib/active_record/connection_adapters/kudu/schema_creation.rb, line 13
def visit_AddColumnDefinition(obj)
  "ADD COLUMNS (#{accept(obj.column)})"
end
visit_ColumnDefinition(obj) click to toggle source
# File lib/active_record/connection_adapters/kudu/schema_creation.rb, line 17
def visit_ColumnDefinition(obj)
  obj.sql_type = type_to_sql(obj.type, obj.options)
  column_sql = "#{quote_column_name(obj.name)} #{obj.sql_type}".dup
  add_column_options!(column_sql, column_options(obj))
end
visit_TableDefinition(table_def) click to toggle source

@param table_def [::ActiveRecord::ConnectionAdapters::Kudu::TableDefinition]

# File lib/active_record/connection_adapters/kudu/schema_creation.rb, line 24
def visit_TableDefinition(table_def)
  create_sql = "CREATE#{' EXTERNAL' if table_def.external} TABLE #{quote_table_name(table_def.name)} "

  statements = table_def.columns.map { |col| accept col }

  primary_keys = if table_def.primary_keys&.any?
                   table_def.primary_keys
                 else
                   table_def.columns.select { |col| col.options[:primary_key] }.map(&:name)
                 end

  raise "Table #{table_def.name} does not have primary key(s) defined" if primary_keys.empty?
  quoted_names = primary_keys.map { |pk| quote_column_name(pk) }

  statements << "PRIMARY KEY (#{quoted_names.join(', ')})"

  create_sql += "(#{statements.join(', ')})" if statements.present?
  add_table_options!(create_sql, table_options(table_def))

  # For managed Kudu tables partitioning must be defined
  unless table_def.external
    # If no partition columns will be provided, we will use all primary keys defined
    partition_columns = table_def.partition_columns || primary_keys
    if (partition_columns - table_def.columns.map(&:name)).any?
      raise 'Non-existing columns have been selected as partition indicators'
    end

    partitions_count = table_def.partitions_count || 2
    quoted_names = partition_columns.map { |pc| quote_column_name(pc) }
    create_sql += " PARTITION BY HASH(#{quoted_names.join(', ')}) PARTITIONS #{partitions_count.to_i}"
    # TODO: partitions range
  end

  create_sql + ' STORED AS KUDU'
end