Skip to content

Commit 0db7ab4

Browse files
author
Keith Gabryelski
committed
ensure redshift partition tables have same dist,sort, and encoding as parent
1 parent 4246fc3 commit 0db7ab4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/partitioned/partitioned_base/redshift_sql_adapter.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,48 @@ def partition_table_alias_name(*partition_key_values)
116116
# Create a single child table.
117117
#
118118
def create_partition_table(*partition_key_values)
119-
execute("select * into #{configurator.table_name(*partition_key_values)} from #{configurator.parent_table_name(*partition_key_values)} where false")
119+
# XXX needs to set search path if parent table is not in search path
120+
# show search_path
121+
# set search_path to '$user', 'public', 'bids_partitions';
122+
123+
# select * from pg_table_def where tablename = 'bids' and schemaname = 'public';
124+
## column, type, encoding, distkey, sortkey, not null
125+
sortkeys = []
126+
sql_columns = []
127+
128+
sql = "select * from pg_table_def where tablename = '#{configurator.parent_table_name(*partition_key_values)}' and schemaname = '#{configurator.parent_table_schema_name(*partition_key_values)}'"
129+
sql_column_rows = execute(sql)
130+
sql_column_rows.each do |row|
131+
column_info = []
132+
column_name = row['column']
133+
column_info << column_name
134+
column_info << row['type']
135+
if row['notnull'] == "t"
136+
column_info << "not null"
137+
end
138+
if row['encoding'] != 'none'
139+
column_info << "encode #{row['encoding']}"
140+
end
141+
if row['sortkey'] != "0"
142+
sortkeys[row['sortkey'].to_i - 1] = column_name
143+
end
144+
sql_columns << column_info.join(" ")
145+
end
146+
147+
if sortkeys.blank?
148+
sql_sortkeys = ""
149+
else
150+
sql_sortkeys = " sortkey (#{sortkeys.join(',')})"
151+
end
152+
sql = <<-SQL
153+
create table #{configurator.table_name(*partition_key_values)}
154+
(
155+
#{sql_columns.join(', ')}
156+
) #{sql_sortkeys}
157+
SQL
158+
execute(sql)
159+
160+
# unset search_path
120161
end
121162

122163
#

lib/partitioned/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module Partitioned
22
# the current version of this gem
3-
VERSION = "1.1.5"
3+
VERSION = "1.1.6"
44
end

0 commit comments

Comments
 (0)