|
| 1 | +using System.Text.Json; |
| 2 | +using KeyLoad.Core; |
| 3 | +using KeyLoad.Storage; |
| 4 | + |
| 5 | +namespace KeyLoad.Query.QueryExecution; |
| 6 | + |
| 7 | +/// <summary>Seeks scalar inequalities through the same ordered index used by equality queries.</summary> |
| 8 | +internal static class QueryIndexRangePlanner |
| 9 | +{ |
| 10 | + private const string IndexSpace = "index"; |
| 11 | + |
| 12 | + internal static (bool HasValue, QueryIndexRange? Value) Create(IndexDefinition index, AstQueryRequest request) |
| 13 | + { |
| 14 | + if (index.Fields.Length != 1 || index.Fields[0] is SqlSyntax.MetadataIdPath or SqlSyntax.MetadataRevisionPath) |
| 15 | + { |
| 16 | + return (false, default(QueryIndexRange?)); |
| 17 | + } |
| 18 | + var prefix = KeySpace.Partition(IndexSpace, request.Partition, request.Query.Collection, index.Name); |
| 19 | + byte[]? lower = null; |
| 20 | + byte[]? upper = null; |
| 21 | + Type? scalarType = null; |
| 22 | + var found = false; |
| 23 | + foreach (var comparison in Comparisons(request.Query.Filter)) |
| 24 | + { |
| 25 | + var field = comparison.Left as FieldOperand ?? comparison.Right as FieldOperand; |
| 26 | + if (field?.Path != index.Fields[0]) |
| 27 | + { |
| 28 | + continue; |
| 29 | + } |
| 30 | + var operation = comparison.Left is FieldOperand ? comparison.Operator : Reverse(comparison.Operator); |
| 31 | + if (operation is not (SqlSyntax.Greater or SqlSyntax.GreaterOrEqual or SqlSyntax.Less or SqlSyntax.LessOrEqual)) |
| 32 | + { |
| 33 | + continue; |
| 34 | + } |
| 35 | + var operand = comparison.Left is FieldOperand ? comparison.Right : comparison.Left; |
| 36 | + var value = Value(operand, request.Parameters).Value; |
| 37 | + if (value is null or MissingValue) |
| 38 | + { |
| 39 | + continue; |
| 40 | + } |
| 41 | + if (scalarType is not null && scalarType != value.GetType()) |
| 42 | + { |
| 43 | + return (false, default(QueryIndexRange?)); |
| 44 | + } |
| 45 | + scalarType = value.GetType(); |
| 46 | + var key = KeySpace.Partition(IndexSpace, request.Partition, request.Query.Collection, index.Name, value); |
| 47 | + if (operation is SqlSyntax.Greater or SqlSyntax.GreaterOrEqual) |
| 48 | + { |
| 49 | + var bound = operation == SqlSyntax.Greater ? StorageKeyBounds.PrefixSuccessor(key).Value : key; |
| 50 | + lower = lower is null || bound is not null && lower.AsSpan().SequenceCompareTo(bound) < 0 ? bound : lower; |
| 51 | + found = true; |
| 52 | + } |
| 53 | + if (operation is SqlSyntax.Less or SqlSyntax.LessOrEqual) |
| 54 | + { |
| 55 | + var bound = operation == SqlSyntax.LessOrEqual ? StorageKeyBounds.PrefixSuccessor(key).Value : key; |
| 56 | + upper = upper is null || bound is not null && upper.AsSpan().SequenceCompareTo(bound) > 0 ? bound : upper; |
| 57 | + found = true; |
| 58 | + } |
| 59 | + } |
| 60 | + return found ? (true, new(prefix, lower, upper)) : (false, default(QueryIndexRange?)); |
| 61 | + } |
| 62 | + |
| 63 | + private static (bool HasValue, object? Value) Value(Operand operand, Dictionary<string, JsonElement>? parameters) |
| 64 | + => operand switch |
| 65 | + { |
| 66 | + ValueOperand value => (true, JsonData.Scalar(value.Value, string.Empty)), |
| 67 | + ParameterOperand parameter when parameters?.TryGetValue(parameter.Name, out var value) == true |
| 68 | + => (true, JsonData.Scalar(value, string.Empty)), |
| 69 | + _ => (false, default(object?)) |
| 70 | + }; |
| 71 | + |
| 72 | + private static string Reverse(string operation) => operation switch |
| 73 | + { |
| 74 | + SqlSyntax.Greater => SqlSyntax.Less, |
| 75 | + SqlSyntax.GreaterOrEqual => SqlSyntax.LessOrEqual, |
| 76 | + SqlSyntax.Less => SqlSyntax.Greater, |
| 77 | + SqlSyntax.LessOrEqual => SqlSyntax.GreaterOrEqual, |
| 78 | + _ => operation |
| 79 | + }; |
| 80 | + |
| 81 | + private static IEnumerable<Comparison> Comparisons(Predicate? predicate) |
| 82 | + { |
| 83 | + if (predicate is Logical { Operator: SqlSyntax.And } conjunction) |
| 84 | + { |
| 85 | + foreach (var comparison in Comparisons(conjunction.Left).Concat(Comparisons(conjunction.Right))) |
| 86 | + { |
| 87 | + yield return comparison; |
| 88 | + } |
| 89 | + } |
| 90 | + else if (predicate is Comparison comparison) |
| 91 | + { |
| 92 | + yield return comparison; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments