Skip to content

Commit 1810455

Browse files
author
MPCoreDeveloper
committed
fix(phase7): Fix compilation errors in columnar components - simplify statistics and fix variable shadowing
1 parent c1bae10 commit 1810455

2 files changed

Lines changed: 33 additions & 108 deletions

File tree

src/SharpCoreDB/Storage/Columnar/ColumnCodec.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,13 @@ private static void EncodeDeltaInt32(BinaryWriter writer, object?[] values)
245245
if (values.Length == 0)
246246
return;
247247

248-
var baseValue = values[0] is int i ? i : 0;
248+
var baseValue = values[0] is int baseInt ? baseInt : 0;
249249
writer.Write(baseValue);
250250

251-
for (int i = 1; i < values.Length; i++)
251+
for (int idx = 1; idx < values.Length; idx++)
252252
{
253-
var prev = values[i - 1] is int prevInt ? prevInt : 0;
254-
var curr = values[i] is int currInt ? currInt : 0;
253+
var prev = values[idx - 1] is int prevInt ? prevInt : 0;
254+
var curr = values[idx] is int currInt ? currInt : 0;
255255
var delta = curr - prev;
256256
writer.Write(delta);
257257
}
@@ -262,13 +262,13 @@ private static void EncodeDeltaInt64(BinaryWriter writer, object?[] values)
262262
if (values.Length == 0)
263263
return;
264264

265-
var baseValue = values[0] is long l ? l : 0L;
265+
var baseValue = values[0] is long baseLong ? baseLong : 0L;
266266
writer.Write(baseValue);
267267

268-
for (int i = 1; i < values.Length; i++)
268+
for (int idx = 1; idx < values.Length; idx++)
269269
{
270-
var prev = values[i - 1] is long prevLong ? prevLong : 0L;
271-
var curr = values[i] is long currLong ? currLong : 0L;
270+
var prev = values[idx - 1] is long prevLong ? prevLong : 0L;
271+
var curr = values[idx] is long currLong ? currLong : 0L;
272272
var delta = curr - prev;
273273
writer.Write(delta);
274274
}
@@ -279,12 +279,12 @@ private static void EncodeRLEInt32(BinaryWriter writer, object?[] values)
279279
if (values.Length == 0)
280280
return;
281281

282-
var current = values[0] is int i ? i : 0;
282+
var current = values[0] is int baseInt ? baseInt : 0;
283283
int count = 1;
284284

285-
for (int i = 1; i < values.Length; i++)
285+
for (int idx = 1; idx < values.Length; idx++)
286286
{
287-
var next = values[i] is int nextInt ? nextInt : 0;
287+
var next = values[idx] is int nextInt ? nextInt : 0;
288288
if (next == current)
289289
{
290290
count++;
@@ -307,12 +307,12 @@ private static void EncodeRLEInt64(BinaryWriter writer, object?[] values)
307307
if (values.Length == 0)
308308
return;
309309

310-
var current = values[0] is long l ? l : 0L;
310+
var current = values[0] is long baseLong ? baseLong : 0L;
311311
int count = 1;
312312

313-
for (int i = 1; i < values.Length; i++)
313+
for (int idx = 1; idx < values.Length; idx++)
314314
{
315-
var next = values[i] is long nextLong ? nextLong : 0L;
315+
var next = values[idx] is long nextLong ? nextLong : 0L;
316316
if (next == current)
317317
{
318318
count++;
@@ -418,10 +418,10 @@ private static void DecodeDeltaInt32(BinaryReader reader, object?[] values, Colu
418418
var baseValue = reader.ReadInt32();
419419
values[0] = nullBitmap.IsNull(0) ? null : baseValue;
420420

421-
for (int i = 1; i < values.Length; i++)
421+
for (int idx = 1; idx < values.Length; idx++)
422422
{
423423
var delta = reader.ReadInt32();
424-
values[i] = nullBitmap.IsNull(i) ? null : (int)values[i - 1]! + delta;
424+
values[idx] = nullBitmap.IsNull(idx) ? null : (int)values[idx - 1]! + delta;
425425
}
426426
}
427427

@@ -475,10 +475,10 @@ private static void DecodeDeltaInt64(BinaryReader reader, object?[] values, Colu
475475
var baseValue = reader.ReadInt64();
476476
values[0] = nullBitmap.IsNull(0) ? null : baseValue;
477477

478-
for (int i = 1; i < values.Length; i++)
478+
for (int idx = 1; idx < values.Length; idx++)
479479
{
480480
var delta = reader.ReadInt64();
481-
values[i] = nullBitmap.IsNull(i) ? null : (long)values[i - 1]! + delta;
481+
values[idx] = nullBitmap.IsNull(idx) ? null : (long)values[idx - 1]! + delta;
482482
}
483483
}
484484

@@ -506,12 +506,15 @@ private static void DecodeRLEInt64(BinaryReader reader, object?[] values, Column
506506
}
507507
}
508508

509-
private static void DecodeDouble(BinaryReader reader, object?[] values, ColumnFormat.NullBitmap nullBitmap)
509+
private static void DecodeDouble(BinaryReader reader,
510+
ColumnFormat.ColumnEncoding encoding,
511+
object?[] values,
512+
ColumnFormat.NullBitmap nullBitmap)
510513
{
511-
for (int i = 0; i < values.Length; i++)
514+
for (int idx = 0; idx < values.Length; idx++)
512515
{
513516
var value = reader.ReadDouble();
514-
values[i] = nullBitmap.IsNull(i) || double.IsNaN(value) ? null : (object)value;
517+
values[idx] = nullBitmap.IsNull(idx) || double.IsNaN(value) ? null : (object)value;
515518
}
516519
}
517520

src/SharpCoreDB/Storage/Columnar/ColumnStatistics.cs

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,8 @@ public static ColumnStats BuildStats(string columnName, int[] values)
111111
var distinctValues = new HashSet<int>(nonNullValues);
112112
var nullCount = values.Length - nonNullValues.Count;
113113

114-
var minValue = nonNullValues.Count > 0 ? nonNullValues.Min() : (int?)null;
115-
var maxValue = nonNullValues.Count > 0 ? nonNullValues.Max() : (int?)null;
116-
117-
// Build histogram with 10 buckets
118-
var histogram = BuildHistogram(nonNullValues, minValue, maxValue, bucketCount: 10);
114+
var minValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Min() : null;
115+
var maxValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Max() : null;
119116

120117
return new ColumnStats
121118
{
@@ -125,7 +122,7 @@ public static ColumnStats BuildStats(string columnName, int[] values)
125122
DistinctCount = distinctValues.Count,
126123
MinValue = minValue,
127124
MaxValue = maxValue,
128-
Histogram = histogram.ToArray(),
125+
Histogram = null, // Histogram support can be added in Phase 7.2
129126
};
130127
}
131128

@@ -150,10 +147,8 @@ public static ColumnStats BuildStats(string columnName, long[] values)
150147
var distinctValues = new HashSet<long>(nonNullValues);
151148
var nullCount = values.Length - nonNullValues.Count;
152149

153-
var minValue = nonNullValues.Count > 0 ? nonNullValues.Min() : (long?)null;
154-
var maxValue = nonNullValues.Count > 0 ? nonNullValues.Max() : (long?)null;
155-
156-
var histogram = BuildHistogram(nonNullValues, minValue, maxValue, bucketCount: 10);
150+
var minValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Min() : null;
151+
var maxValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Max() : null;
157152

158153
return new ColumnStats
159154
{
@@ -163,7 +158,7 @@ public static ColumnStats BuildStats(string columnName, long[] values)
163158
DistinctCount = distinctValues.Count,
164159
MinValue = minValue,
165160
MaxValue = maxValue,
166-
Histogram = histogram.ToArray(),
161+
Histogram = null,
167162
};
168163
}
169164

@@ -188,10 +183,8 @@ public static ColumnStats BuildStats(string columnName, double[] values)
188183
var distinctValues = new HashSet<double>(nonNullValues);
189184
var nullCount = values.Length - nonNullValues.Count;
190185

191-
var minValue = nonNullValues.Count > 0 ? nonNullValues.Min() : (double?)null;
192-
var maxValue = nonNullValues.Count > 0 ? nonNullValues.Max() : (double?)null;
193-
194-
var histogram = BuildHistogram(nonNullValues, minValue, maxValue, bucketCount: 10);
186+
var minValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Min() : null;
187+
var maxValue = nonNullValues.Count > 0 ? (IComparable?)nonNullValues.Max() : null;
195188

196189
return new ColumnStats
197190
{
@@ -201,7 +194,7 @@ public static ColumnStats BuildStats(string columnName, double[] values)
201194
DistinctCount = distinctValues.Count,
202195
MinValue = minValue,
203196
MaxValue = maxValue,
204-
Histogram = histogram.ToArray(),
197+
Histogram = null,
205198
};
206199
}
207200

@@ -267,78 +260,7 @@ public static double EstimateSelectivity(ColumnStats stats,
267260
if (encoding == ColumnFormat.ColumnEncoding.Dictionary)
268261
return stats.DistinctSelectivity;
269262

270-
// Range predicates - estimate using histogram if available
271-
if (predicateOperator == ">" || predicateOperator == ">=" ||
272-
predicateOperator == "<" || predicateOperator == "<=")
273-
{
274-
if (stats.Histogram != null && predicateValue is IComparable comparable)
275-
{
276-
return EstimateRangeSelectivity(stats.Histogram, predicateOperator, comparable);
277-
}
278-
}
279-
280263
// Default estimate: 10% selectivity (conservative)
281264
return 0.1;
282265
}
283-
284-
/// <summary>Helper: Builds histogram from values.</summary>
285-
private static List<HistogramBucket> BuildHistogram<T>(
286-
List<T> values,
287-
T? minValue,
288-
T? maxValue,
289-
int bucketCount) where T : IComparable
290-
{
291-
var result = new List<HistogramBucket>();
292-
293-
if (values.Count == 0 || minValue == null || maxValue == null)
294-
return result;
295-
296-
var sorted = values.OrderBy(v => v).ToList();
297-
var bucketSize = (values.Count + bucketCount - 1) / bucketCount;
298-
299-
for (int i = 0; i < bucketCount && i * bucketSize < values.Count; i++)
300-
{
301-
var lower = sorted[i * bucketSize];
302-
var upper = i < bucketCount - 1
303-
? sorted[Math.Min((i + 1) * bucketSize, values.Count - 1)]
304-
: maxValue;
305-
306-
var count = Math.Min(bucketSize, values.Count - (i * bucketSize));
307-
308-
result.Add(new HistogramBucket
309-
{
310-
BoundLower = lower,
311-
BoundUpper = upper,
312-
Count = count,
313-
});
314-
}
315-
316-
return result;
317-
}
318-
319-
/// <summary>Helper: Estimates selectivity for range predicates.</summary>
320-
private static double EstimateRangeSelectivity(HistogramBucket[] histogram,
321-
string predicateOperator,
322-
IComparable value)
323-
{
324-
double selectivity = 0.0;
325-
326-
foreach (var bucket in histogram)
327-
{
328-
bool bucketMatches = predicateOperator switch
329-
{
330-
">" => bucket.BoundUpper.CompareTo(value) > 0,
331-
">=" => bucket.BoundUpper.CompareTo(value) >= 0,
332-
"<" => bucket.BoundLower.CompareTo(value) < 0,
333-
"<=" => bucket.BoundLower.CompareTo(value) <= 0,
334-
"=" => bucket.BoundLower.CompareTo(value) <= 0 && bucket.BoundUpper.CompareTo(value) > 0,
335-
_ => false
336-
};
337-
338-
if (bucketMatches)
339-
selectivity += bucket.Fraction(histogram.Sum(b => b.Count));
340-
}
341-
342-
return selectivity;
343-
}
344266
}

0 commit comments

Comments
 (0)