-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypertableScaffoldingExtractor.cs
More file actions
221 lines (197 loc) · 9.09 KB
/
HypertableScaffoldingExtractor.cs
File metadata and controls
221 lines (197 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using CmdScale.EntityFrameworkCore.TimescaleDB.Abstractions;
using System.Data;
using System.Data.Common;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Design.Scaffolding
{
/// <summary>
/// Extracts hypertable metadata from a TimescaleDB database for scaffolding.
/// </summary>
public sealed class HypertableScaffoldingExtractor : ITimescaleFeatureExtractor
{
public sealed record HypertableInfo(
string TimeColumnName,
string ChunkTimeInterval,
bool CompressionEnabled,
List<string> CompressionSegmentBy,
List<string> CompressionOrderBy,
List<string> ChunkSkipColumns,
List<Dimension> AdditionalDimensions
);
public Dictionary<(string Schema, string TableName), object> Extract(DbConnection connection)
{
bool wasOpen = connection.State == ConnectionState.Open;
if (!wasOpen)
{
connection.Open();
}
try
{
Dictionary<(string, string), HypertableInfo> hypertables = [];
Dictionary<(string, string), bool> compressionSettings = GetCompressionSettings(connection);
GetHypertableSettings(connection, hypertables, compressionSettings);
GetChunkSkipColumns(connection, hypertables);
GetCompressionConfiguration(connection, hypertables);
// Convert to object dictionary to match interface
return hypertables.ToDictionary(
kvp => kvp.Key,
kvp => (object)kvp.Value
);
}
finally
{
if (!wasOpen)
{
connection.Close();
}
}
}
private static Dictionary<(string, string), bool> GetCompressionSettings(DbConnection connection)
{
Dictionary<(string, string), bool> compressionSettings = [];
using DbCommand command = connection.CreateCommand();
command.CommandText = "SELECT hypertable_schema, hypertable_name, compression_enabled FROM timescaledb_information.hypertables;";
using DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
compressionSettings[(reader.GetString(0), reader.GetString(1))] = reader.GetBoolean(2);
}
return compressionSettings;
}
private static void GetHypertableSettings(
DbConnection connection,
Dictionary<(string, string), HypertableInfo> hypertables,
Dictionary<(string, string), bool> compressionSettings)
{
using DbCommand command = connection.CreateCommand();
command.CommandText = @"
SELECT
hypertable_schema,
hypertable_name,
column_name,
dimension_number,
num_partitions,
EXTRACT(EPOCH FROM time_interval) * 1000 AS time_interval_microseconds,
integer_interval
FROM timescaledb_information.dimensions
ORDER BY hypertable_schema, hypertable_name, dimension_number;";
using DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string schema = reader.GetString(0);
string name = reader.GetString(1);
string columnName = reader.GetString(2);
int dimensionNumber = reader.GetInt32(3);
(string schema, string name) key = (schema, name);
// If it's the first dimension, it defines the primary hypertable settings
if (dimensionNumber == 1)
{
long chunkInterval = reader.IsDBNull(5) ? DefaultValues.ChunkTimeIntervalLong : (long)reader.GetDouble(5);
bool compressionEnabled = compressionSettings.TryGetValue(key, out bool enabled) && enabled;
hypertables[key] = new HypertableInfo(
TimeColumnName: columnName,
ChunkTimeInterval: chunkInterval.ToString(),
CompressionEnabled: compressionEnabled,
CompressionSegmentBy: [],
CompressionOrderBy: [],
ChunkSkipColumns: [],
AdditionalDimensions: []
);
}
// For all other dimensions, add them to the AdditionalDimensions list
else
{
if (hypertables.TryGetValue(key, out HypertableInfo? info))
{
Dimension dimension;
if (!reader.IsDBNull(4) && reader.GetInt32(4) > 0)
{
// Hash dimension (space partitioning)
dimension = Dimension.CreateHash(columnName, reader.GetInt32(4));
}
else if (!reader.IsDBNull(5))
{
// Time-based range dimension
long interval = (long)reader.GetDouble(5);
dimension = Dimension.CreateRange(columnName, interval.ToString());
}
else if (!reader.IsDBNull(6))
{
// Integer-based range dimension
long integerInterval = reader.GetInt64(6);
dimension = Dimension.CreateRange(columnName, integerInterval.ToString());
}
else continue;
info.AdditionalDimensions.Add(dimension);
}
}
}
}
private static void GetChunkSkipColumns(DbConnection connection, Dictionary<(string, string), HypertableInfo> hypertables)
{
using DbCommand command = connection.CreateCommand();
command.CommandText = @"
SELECT
h.schema_name,
h.table_name,
ccs.column_name
FROM _timescaledb_catalog.chunk_column_stats AS ccs
JOIN _timescaledb_catalog.hypertable AS h ON ccs.hypertable_id = h.id;";
using DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string schema = reader.GetString(0);
string name = reader.GetString(1);
string columnName = reader.GetString(2);
if (hypertables.TryGetValue((schema, name), out HypertableInfo? info))
{
info.ChunkSkipColumns.Add(columnName);
}
}
}
private static void GetCompressionConfiguration(DbConnection connection, Dictionary<(string, string), HypertableInfo> hypertables)
{
using DbCommand command = connection.CreateCommand();
// This view provides the column-level details for compression.
// segmentby_column_index is not null for segment columns.
// orderby_column_index is not null for order columns.
command.CommandText = @"
SELECT
hypertable_schema,
hypertable_name,
attname,
segmentby_column_index,
orderby_column_index,
orderby_asc,
orderby_nullsfirst
FROM timescaledb_information.compression_settings
ORDER BY hypertable_schema, hypertable_name, segmentby_column_index, orderby_column_index;";
using DbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string schema = reader.GetString(0);
string name = reader.GetString(1);
string columnName = reader.GetString(2);
// Find the corresponding hypertable info
if (!hypertables.TryGetValue((schema, name), out HypertableInfo? info))
{
continue;
}
// Handle SegmentBy
if (!reader.IsDBNull(3)) // segmentby_column_index
{
info.CompressionSegmentBy.Add(columnName);
}
// Handle OrderBy
if (!reader.IsDBNull(4)) // orderby_column_index
{
bool isAscending = reader.GetBoolean(5);
bool isNullsFirst = reader.GetBoolean(6);
string direction = isAscending ? "ASC" : "DESC";
bool isDefaultNulls = (isAscending && !isNullsFirst) || (!isAscending && isNullsFirst);
string nulls = isDefaultNulls ? "" : (isNullsFirst ? " NULLS FIRST" : " NULLS LAST");
info.CompressionOrderBy.Add($"{columnName} {direction}{nulls}");
}
}
}
}
}