-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHypertableOperationGenerator.cs
More file actions
201 lines (170 loc) · 9.25 KB
/
HypertableOperationGenerator.cs
File metadata and controls
201 lines (170 loc) · 9.25 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
using CmdScale.EntityFrameworkCore.TimescaleDB.Abstractions;
using CmdScale.EntityFrameworkCore.TimescaleDB.Operations;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Generators
{
public class HypertableOperationGenerator
{
private readonly string quoteString = "\"";
private readonly SqlBuilderHelper sqlHelper;
public HypertableOperationGenerator(bool isDesignTime = false)
{
if (isDesignTime)
{
quoteString = "\"\"";
}
sqlHelper = new SqlBuilderHelper(quoteString);
}
public List<string> Generate(CreateHypertableOperation operation)
{
string qualifiedTableName = sqlHelper.Regclass(operation.TableName, operation.Schema);
string qualifiedIdentifier = sqlHelper.QualifiedIdentifier(operation.TableName, operation.Schema);
string migrateDataParam = operation.MigrateData ? ", migrate_data => true" : "";
List<string> statements =
[
$"SELECT create_hypertable({qualifiedTableName}, '{operation.TimeColumnName}'{migrateDataParam});"
];
// ChunkTimeInterval
if (!string.IsNullOrEmpty(operation.ChunkTimeInterval))
{
// Check if the interval is a plain number (e.g., for microseconds).
if (long.TryParse(operation.ChunkTimeInterval, out _))
{
// If it's a number, don't wrap it in quotes.
statements.Add($"SELECT set_chunk_time_interval({qualifiedTableName}, {operation.ChunkTimeInterval}::bigint);");
}
else
{
// If it's a string like '7 days', wrap it in quotes.
statements.Add($"SELECT set_chunk_time_interval({qualifiedTableName}, INTERVAL '{operation.ChunkTimeInterval}');");
}
}
// EnableCompression
if (operation.EnableCompression || operation.ChunkSkipColumns?.Count > 0)
{
bool enableCompression = operation.EnableCompression || operation.ChunkSkipColumns != null && operation.ChunkSkipColumns.Count > 0;
statements.Add($"ALTER TABLE {qualifiedIdentifier} SET (timescaledb.compress = {enableCompression.ToString().ToLower()});");
}
// ChunkSkipColumns
if (operation.ChunkSkipColumns != null && operation.ChunkSkipColumns.Count > 0)
{
statements.Add("SET timescaledb.enable_chunk_skipping = 'ON';");
foreach (string column in operation.ChunkSkipColumns)
{
statements.Add($"SELECT enable_chunk_skipping({qualifiedTableName}, '{column}');");
}
}
// AdditionalDimensions
if (operation.AdditionalDimensions != null && operation.AdditionalDimensions.Count > 0)
{
foreach (Dimension dimension in operation.AdditionalDimensions)
{
if (dimension.Type == EDimensionType.Range)
{
// Detect if interval is numeric (integer range) or time-based (timestamp range)
bool isIntegerRange = long.TryParse(dimension.Interval, out _);
string intervalExpression = isIntegerRange
? dimension.Interval!
: $"INTERVAL '{dimension.Interval}'";
statements.Add($"SELECT add_dimension({qualifiedTableName}, by_range('{dimension.ColumnName}', {intervalExpression}));");
}
else if (dimension.Type == EDimensionType.Hash)
{
statements.Add($"SELECT add_dimension({qualifiedTableName}, by_hash('{dimension.ColumnName}', {dimension.NumberOfPartitions}));");
}
}
}
return statements;
}
public List<string> Generate(AlterHypertableOperation operation)
{
string qualifiedTableName = sqlHelper.Regclass(operation.TableName, operation.Schema);
string qualifiedIdentifier = sqlHelper.QualifiedIdentifier(operation.TableName, operation.Schema);
List<string> statements = [];
// Check for ChunkTimeInterval change
if (operation.ChunkTimeInterval != operation.OldChunkTimeInterval)
{
// Check if the interval is a plain number (e.g., for microseconds).
if (long.TryParse(operation.ChunkTimeInterval, out _))
{
// If it's a number, don't wrap it in quotes.
statements.Add($"SELECT set_chunk_time_interval({qualifiedTableName}, {operation.ChunkTimeInterval}::bigint);");
}
else
{
// If it's a string like '7 days', wrap it in quotes.
statements.Add($"SELECT set_chunk_time_interval({qualifiedTableName}, INTERVAL '{operation.ChunkTimeInterval}');");
}
}
// Check for EnableCompression change
bool newCompressionState = operation.EnableCompression || operation.ChunkSkipColumns != null && operation.ChunkSkipColumns.Any();
bool oldCompressionState = operation.OldEnableCompression || operation.OldChunkSkipColumns != null && operation.OldChunkSkipColumns.Any();
if (newCompressionState != oldCompressionState)
{
string compressionValue = newCompressionState.ToString().ToLower();
statements.Add($"ALTER TABLE {qualifiedIdentifier} SET (timescaledb.compress = {compressionValue});");
}
// Handle ChunkSkipColumns
IReadOnlyList<string> newColumns = operation.ChunkSkipColumns ?? [];
IReadOnlyList<string> oldColumns = operation.OldChunkSkipColumns ?? [];
List<string> addedColumns = [.. newColumns.Except(oldColumns)];
if (addedColumns.Count != 0)
{
statements.Add("SET timescaledb.enable_chunk_skipping = 'ON';");
foreach (string column in addedColumns)
{
statements.Add($"SELECT enable_chunk_skipping({qualifiedTableName}, '{column}');");
}
}
List<string> removedColumns = [.. oldColumns.Except(newColumns)];
if (removedColumns.Count != 0)
{
foreach (string column in removedColumns)
{
statements.Add($"SELECT disable_chunk_skipping({qualifiedTableName}, '{column}');");
}
}
// Handle AdditionalDimensions - only add new dimensions
// NOTE: TimescaleDB does NOT support removing dimensions from hypertables.
// Once a dimension is added, it cannot be removed. Therefore, we only generate
// SQL for adding new dimensions and ignore dimension removals.
IReadOnlyList<Dimension> newDimensions = operation.AdditionalDimensions ?? [];
IReadOnlyList<Dimension> oldDimensions = operation.OldAdditionalDimensions ?? [];
// Find dimensions that are in new but not in old (added dimensions)
foreach (Dimension newDim in newDimensions)
{
bool exists = oldDimensions.Any(oldDim =>
oldDim.ColumnName == newDim.ColumnName &&
oldDim.Type == newDim.Type &&
oldDim.Interval == newDim.Interval &&
oldDim.NumberOfPartitions == newDim.NumberOfPartitions);
if (!exists)
{
if (newDim.Type == EDimensionType.Range)
{
// Detect if interval is numeric (integer range) or time-based (timestamp range)
bool isIntegerRange = long.TryParse(newDim.Interval, out _);
string intervalExpression = isIntegerRange
? newDim.Interval!
: $"INTERVAL '{newDim.Interval}'";
statements.Add($"SELECT add_dimension({qualifiedTableName}, by_range('{newDim.ColumnName}', {intervalExpression}));");
}
else if (newDim.Type == EDimensionType.Hash)
{
statements.Add($"SELECT add_dimension({qualifiedTableName}, by_hash('{newDim.ColumnName}', {newDim.NumberOfPartitions}));");
}
}
}
// Warn if dimensions were removed (which cannot be reversed in TimescaleDB)
List<Dimension> removedDimensions = [.. oldDimensions
.Where(oldDim => !newDimensions.Any(newDim =>
oldDim.ColumnName == newDim.ColumnName &&
oldDim.Type == newDim.Type))];
if (removedDimensions.Count > 0)
{
string dimensionList = string.Join(", ", removedDimensions.Select(d => $"'{d.ColumnName}'"));
statements.Add($"-- WARNING: TimescaleDB does not support removing dimensions. The following dimensions cannot be removed: {dimensionList}");
}
return statements;
}
}
}