Commit 0829d85
Handling datetime types (#1636)
## Why make this change?
Currently, we are incorrectly handling the datetime datatypes. A snippet
from `BaseSqlQueryStructure` class:
<img width="509" alt="image"
src="https://github.com/Azure/data-api-builder/assets/34566234/bc126221-f392-41cf-b1e3-767eefb2a54a">
As evident from the highlighted portion, even for `DateTime` .NET type,
we are parsing it with `DateTimeOffset` which is wrong. Just for an
example, if you execute a GET request on the `stocks_price` table which
has `(categoryid,pieceid,instant)` as PK, you would not get any result
back even when the record exists for that PK in the table. Why? Because
even though instant is a `datetime` column, it gets parsed as
`datetimeoffset` column. This is shown in the next 2 images.
<img width="642" alt="image"
src="https://github.com/Azure/data-api-builder/assets/34566234/24cd9003-1d30-4713-aa54-5953584ff574">
<img width="323" alt="image"
src="https://github.com/Azure/data-api-builder/assets/34566234/bac63094-adba-4544-814d-43e3661536d3">
Also, the existing tests (like the ones fixed in
`MsSqlGraphQLPaginationTests` and `GraphQLSupportedTypesTestsBase`) were
wrongly written, in which values in the range of `datetime2` were
supplied for `datetime` column.
## What is this change?
1. Correctly return value using `DateTime` field of the `DateTimeOffset`
parsed value in `BaseSqlQueryStructure` class,
2. Populate the DbType of the datetime parameters when they are sent to
the database so that the database knows the exact type of the parameter.
Eg. by default, if you send a `datetime2` param without a DbType, it is
assumed to be a `datetime` and an exception is thrown incorrectly by the
database. This is done using the map
`TypeHelper._timeSqlDbTypeToDbType`.
It should be noted 4 sql server types - `date`, `smalldatetime`,
`datetime`, `datetime2` map to the same .NET system type
`System.DateTime`. The existing `TypeHelper._systemTypeToDbTypeMap`
would not be of any use to us because it has mapping from system type to
DbType and all the above 3 sql server types have the same system type.
So, we need to rely on the actual mapping from sql server type to DbType
which is:
`datetime -> DbType.DateTime, smalldatetime -> DbType.DateTime,
datetime2 -> DbType.DateTime2`
`date -> DbType.Date`
3. Fix the existing tests.
## How was this tested?
- [x] Integration Tests - Added test for getting single record by PK
having a `datetime` column as a PK column to `FindApiTestBase`.
- [x] Unit Tests - Added filter/orderby and insert tests to
`GraphQLSupportedTypesTestsBase.QueryTypeColumnFilterAndOrderByDateTime`
and `GraphQLSupportedTypesTestsBase.InsertIntoTypeColumn`for datetime
types - `datetime2`, `smalldatetime`, `date`.
## Sample Request(s)
- Example REST request to demonstrate modifications
<img width="651" alt="image"
src="https://github.com/Azure/data-api-builder/assets/34566234/9e150a5b-63ce-4c44-b674-e2e4cb2f88b8">
- Example GQL request to demonstrate modifications
<img width="850" alt="image"
src="https://github.com/Azure/data-api-builder/assets/34566234/28c70b5a-fb2b-4dbe-9641-bab25823425e">
---------
Co-authored-by: Aniruddh Munde <anmunde@microsoft.com>
Co-authored-by: Abhishek Kumar <abhishekkuma@microsoft.com>
Co-authored-by: abhishekkumams <102276754+abhishekkumams@users.noreply.github.com>
Co-authored-by: Sean Leonard <sean.leonard@microsoft.com>1 parent da8b47b commit 0829d85
File tree
35 files changed
+602
-257
lines changed- config-generators
- src
- Core
- Models
- Resolvers/Sql Query Structures
- Services
- MetadataProviders
- Service.GraphQLBuilder
- GraphQLTypes
- Queries
- Sql
- Service.Tests
- GraphQLBuilder/Sql
- Snapshots
- SqlTests
- GraphQLSupportedTypesTests
- RestApiTests
- Find
35 files changed
+602
-257
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
564 | 564 | | |
565 | 565 | | |
566 | 566 | | |
567 | | - | |
| 567 | + | |
568 | 568 | | |
569 | 569 | | |
570 | 570 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
364 | | - | |
| 364 | + | |
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
475 | 475 | | |
476 | 476 | | |
477 | 477 | | |
478 | | - | |
| 478 | + | |
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| |||
Lines changed: 156 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
| 18 | + | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
| |||
80 | 85 | | |
81 | 86 | | |
82 | 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 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
83 | 239 | | |
84 | 240 | | |
Lines changed: 18 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
323 | | - | |
| 323 | + | |
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
378 | 385 | | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
| 386 | + | |
386 | 387 | | |
387 | 388 | | |
388 | 389 | | |
| |||
1097 | 1098 | | |
1098 | 1099 | | |
1099 | 1100 | | |
1100 | | - | |
1101 | 1101 | | |
1102 | 1102 | | |
1103 | 1103 | | |
1104 | 1104 | | |
1105 | | - | |
1106 | | - | |
| 1105 | + | |
1107 | 1106 | | |
1108 | 1107 | | |
1109 | 1108 | | |
| |||
1117 | 1116 | | |
1118 | 1117 | | |
1119 | 1118 | | |
1120 | | - | |
| 1119 | + | |
| 1120 | + | |
1121 | 1121 | | |
1122 | 1122 | | |
1123 | 1123 | | |
| |||
1400 | 1400 | | |
1401 | 1401 | | |
1402 | 1402 | | |
1403 | | - | |
| 1403 | + | |
1404 | 1404 | | |
1405 | | - | |
| 1405 | + | |
1406 | 1406 | | |
1407 | 1407 | | |
1408 | 1408 | | |
| |||
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
| 1422 | + | |
| 1423 | + | |
1422 | 1424 | | |
1423 | 1425 | | |
1424 | 1426 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
0 commit comments