|
| 1 | +using SqlServerSimulator.Parser.Expressions; |
| 2 | +using SqlServerSimulator.Parser.Tokens; |
| 3 | +using SqlServerSimulator.Storage; |
| 4 | + |
| 5 | +namespace SqlServerSimulator.Parser; |
| 6 | + |
| 7 | +partial class Selection |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Built-in system TVF <c>fn_virtualfilestats</c> (invoked bare or |
| 11 | + /// <c>sys.</c>-qualified). Two args <c>(database_id, file_id)</c>, both |
| 12 | + /// nullable; <c>NULL</c> is the wildcard (all databases / all files), |
| 13 | + /// probe-confirmed against SQL Server 2025 (2026-07-19). A non-NULL id |
| 14 | + /// that names no database / file yields zero rows (including negative |
| 15 | + /// ids such as <c>-1</c>). Column shape mirrors real: |
| 16 | + /// <c>(DbId smallint, FileId smallint, TimeStamp bigint, NumberReads |
| 17 | + /// bigint, BytesRead bigint, IoStallReadMS bigint, NumberWrites bigint, |
| 18 | + /// BytesWritten bigint, IoStallWriteMS bigint, IoStallMS bigint, |
| 19 | + /// BytesOnDisk bigint, FileHandle varbinary(8))</c>. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// The simulator has no physical file model, so it reports one row per |
| 23 | + /// (database, <c>file_id 1</c>) with all IO counters and <c>BytesOnDisk</c> |
| 24 | + /// at 0 and an all-zero <c>FileHandle</c> — the honest reading for an |
| 25 | + /// in-process store that performs no disk IO. The wildcard / filter |
| 26 | + /// semantics still match real so a caller enumerating files gets the right |
| 27 | + /// row cardinality per database. The legacy <c>::fn_virtualfilestats(...)</c> |
| 28 | + /// prefix form isn't tokenized; the bare and <c>sys.</c>-qualified forms |
| 29 | + /// cover the documented invocations. |
| 30 | + /// </remarks> |
| 31 | + public static Selection ParseVirtualFileStats(ParserContext context, string functionName) |
| 32 | + { |
| 33 | + // On entry the cursor rests on the function name's leaf segment |
| 34 | + // (ParseObjectName leaves Token on the leaf); advance to the '('. |
| 35 | + if (context.GetNextRequired() is not Operator { Character: '(' }) |
| 36 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 37 | + |
| 38 | + context.MoveNextRequired(); |
| 39 | + var dbArg = Expression.Parse(context); |
| 40 | + |
| 41 | + if (context.Token is Operator { Character: ')' }) |
| 42 | + throw SimulatedSqlException.InsufficientArgumentsToFunction(functionName); |
| 43 | + if (context.Token is not Operator { Character: ',' }) |
| 44 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 45 | + |
| 46 | + context.MoveNextRequired(); |
| 47 | + var fileArg = Expression.Parse(context); |
| 48 | + |
| 49 | + // A trailing comma means a third argument was supplied. |
| 50 | + if (context.Token is Operator { Character: ',' }) |
| 51 | + throw SimulatedSqlException.TooManyArgumentsToFunction(functionName); |
| 52 | + if (context.Token is not Operator { Character: ')' }) |
| 53 | + throw SimulatedSqlException.SyntaxErrorNear(context); |
| 54 | + context.MoveNextOptional(); |
| 55 | + |
| 56 | + SqlType[] schema = |
| 57 | + [ |
| 58 | + SqlType.SmallInt, SqlType.SmallInt, SqlType.BigInt, SqlType.BigInt, |
| 59 | + SqlType.BigInt, SqlType.BigInt, SqlType.BigInt, SqlType.BigInt, |
| 60 | + SqlType.BigInt, SqlType.BigInt, SqlType.BigInt, VarbinarySqlType.Get(8), |
| 61 | + ]; |
| 62 | + string[] columnNames = |
| 63 | + [ |
| 64 | + "DbId", "FileId", "TimeStamp", "NumberReads", "BytesRead", "IoStallReadMS", |
| 65 | + "NumberWrites", "BytesWritten", "IoStallWriteMS", "IoStallMS", "BytesOnDisk", "FileHandle", |
| 66 | + ]; |
| 67 | + |
| 68 | + return new Selection(schema, columnNames, |
| 69 | + hasOrderBy: false, |
| 70 | + hasTopOrOffsetOrFetch: false, |
| 71 | + (batch, outerResolver) => EnumerateVirtualFileStatsRows(schema, dbArg, fileArg, batch, outerResolver)); |
| 72 | + } |
| 73 | + |
| 74 | + private static IEnumerable<byte[]> EnumerateVirtualFileStatsRows( |
| 75 | + SqlType[] schema, |
| 76 | + Expression dbExpr, |
| 77 | + Expression fileExpr, |
| 78 | + BatchContext batch, |
| 79 | + Func<MultiPartName, SqlValue>? outerResolver) |
| 80 | + { |
| 81 | + var resolver = outerResolver ?? (n => throw SimulatedSqlException.InvalidColumnName(n)); |
| 82 | + var runtime = new RuntimeContext(resolver, batch); |
| 83 | + var dbFilter = EvalNullableInt(dbExpr, runtime); |
| 84 | + var fileFilter = EvalNullableInt(fileExpr, runtime); |
| 85 | + |
| 86 | + // Only file_id 1 is modeled; a non-NULL file argument naming any other |
| 87 | + // file yields no rows (NULL is the all-files wildcard). |
| 88 | + if (fileFilter is { } wantFile && wantFile != 1) |
| 89 | + yield break; |
| 90 | + |
| 91 | + var fileHandle = new byte[8]; |
| 92 | + foreach (var (_, id) in DbId.DatabasesWithIds(batch.Connection.Simulation)) |
| 93 | + { |
| 94 | + if (dbFilter is { } wantDb && wantDb != id) |
| 95 | + continue; |
| 96 | + |
| 97 | + yield return RowEncoder.EncodeRow(schema, |
| 98 | + [ |
| 99 | + SqlValue.FromInt16(id), |
| 100 | + SqlValue.FromInt16(1), |
| 101 | + SqlValue.FromInt64(0), |
| 102 | + SqlValue.FromInt64(0), |
| 103 | + SqlValue.FromInt64(0), |
| 104 | + SqlValue.FromInt64(0), |
| 105 | + SqlValue.FromInt64(0), |
| 106 | + SqlValue.FromInt64(0), |
| 107 | + SqlValue.FromInt64(0), |
| 108 | + SqlValue.FromInt64(0), |
| 109 | + SqlValue.FromInt64(0), |
| 110 | + SqlValue.FromVarbinary(VarbinarySqlType.Get(8), fileHandle), |
| 111 | + ]); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static int? EvalNullableInt(Expression expr, RuntimeContext runtime) |
| 116 | + { |
| 117 | + var value = expr.Run(runtime); |
| 118 | + return value.IsNull ? null : value.CoerceTo(SqlType.Int32).AsInt32; |
| 119 | + } |
| 120 | +} |
0 commit comments