-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup.sql
4014 lines (3506 loc) · 106 KB
/
Setup.sql
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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET NOCOUNT ON;
THROW 51000, 'Make sure to designate the right database and then comment this out', 1;
/* Create etl Schema */
IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'etl')
BEGIN
EXEC('CREATE SCHEMA etl AUTHORIZATION dbo')
END
/* Create Tables */
SET NOCOUNT ON
DROP TABLE IF EXISTS etl.AuditColumnLog
DROP TABLE IF EXISTS etl.AuditCustomLog
DROP TABLE IF EXISTS etl.TimelinessRunTimeLog
DROP TABLE IF EXISTS etl.RunTime
CREATE TABLE etl.RunTime (
ParentRunTimeID BIGINT NULL,
RunTimeID BIGINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_RunTime_RunTimeID PRIMARY KEY,
PipelineID INT NOT NULL,
PipelineStart DATETIMEOFFSET(2) NOT NULL,
PipelineEnd DATETIMEOFFSET(2) NULL,
RunTimeSeconds INT NULL,
Completed BIT NOT NULL
CONSTRAINT DF_etl_RunTime_Completed DEFAULT(0),
Inserts INT NULL,
Updates INT NULL,
SoftDeletes INT NULL,
TotalRows INT NULL,
FullLoad BIT NULL,
WaterMark VARCHAR(50) NULL,
NextWaterMark VARCHAR(50) NULL
)
CREATE NONCLUSTERED INDEX IX_etl_RunTime_PipelineID
ON etl.RunTime(PipelineID)
CREATE NONCLUSTERED INDEX IX_etl_RunTime_PipelineEnd
ON etl.RunTime(PipelineEnd) WHERE (PipelineEnd IS NOT NULL)
DROP TABLE IF EXISTS etl.AuditColumnRule
DROP TABLE IF EXISTS etl.AuditCustomRule
DROP TABLE IF EXISTS etl.TimelinessRunTimeLog
DROP TABLE IF EXISTS etl.Pipeline
CREATE TABLE etl.Pipeline (
PipelineID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_Pipeline_PipelineID PRIMARY KEY,
PipelineName VARCHAR(150) NOT NULL,
PipelineTypeID TINYINT NOT NULL,
WaterMark VARCHAR(50) NULL,
NextWaterMark VARCHAR(50) NULL,
SourceAddressID INT NULL,
TargetAddressID INT NULL ,
LastTargetInsert DATETIMEOFFSET(2) NULL,
LastTargetUpdate DATETIMEOFFSET(2) NULL,
LastTargetDelete DATETIMEOFFSET(2) NULL,
PipelineArgs VARCHAR(4000) NULL,
TimelyNumber INT NULL,
TimelyDatePart VARCHAR(20) NULL
CONSTRAINT CK_etl_Pipeline_TimelyDatePart CHECK(TimelyDatePart IN (NULL, 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'YEAR')),
MuteTimelyCheck BIT NOT NULL
CONSTRAINT DF_etl_Pipeline_MuteTimelyCheck DEFAULT (0),
LoadLineage BIT NOT NULL
CONSTRAINT DF_etl_Pipeline_LoadLineage DEFAULT(0),
Active BIT NOT NULL
CONSTRAINT DF_etl_Pipeline_Active DEFAULT(1)
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_Pipeline_PipelineName
ON etl.Pipeline (PipelineName) INCLUDE (LoadLineage, Active)
CREATE NONCLUSTERED INDEX IX_etl_Pipeline_MuteTimelyCheck
ON etl.Pipeline (PipelineTypeID)
ALTER TABLE etl.RunTime
ADD CONSTRAINT FK_etl_RunTime_PipelineID FOREIGN KEY (PipelineID)
REFERENCES etl.Pipeline (PipelineID)
DROP TABLE IF EXISTS etl.PipelineType
CREATE TABLE etl.PipelineType (
PipelineTypeID TINYINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_PipelineType_PipelineTypeID PRIMARY KEY,
PipelineType VARCHAR(150) NOT NULL,
TimelyNumber INT NULL,
TimelyDatePart VARCHAR(20) NULL
CONSTRAINT CK_etl_PipelineType_TimelyDatePart CHECK(TimelyDatePart IN (NULL, 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'YEAR')),
MuteTimelyCheck BIT NOT NULL
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_PipelineType_PipelineType
ON etl.PipelineType (PipelineType)
INSERT INTO etl.PipelineType (PipelineType, TimelyNumber, TimelyDatePart, MuteTimelyCheck)
VALUES
('Controller', NULL, NULL, 1),
('Extraction', 7, 'DAY', 0),
('Audit', NULL, NULL, 1),
('Publish', 7, 'DAY', 0),
('Transformation', 7, 'DAY', 0),
('Fact', 25, 'HOUR', 0),
('Dimension', 25, 'HOUR', 0)
ALTER TABLE etl.Pipeline
ADD CONSTRAINT FK_etl_Pipeline_PipelineTypeID FOREIGN KEY (PipelineTypeID)
REFERENCES etl.PipelineType (PipelineTypeID)
DROP TABLE IF EXISTS etl.PipelineUser
CREATE TABLE etl.PipelineUser (
PipelineUserID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_PipelineUser_PipelineUserID PRIMARY KEY,
UserName VARCHAR(150)
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_PipelineUser_UserName
ON etl.PipelineUser (UserName)
DROP TABLE IF EXISTS etl.AddressDependency
DROP TABLE IF EXISTS etl.Address
CREATE TABLE etl.Address (
AddressID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_Address_AddressID PRIMARY KEY,
FullAddress VARCHAR(150) NOT NULL,
AddressTypeID TINYINT NOT NULL,
DatabaseName VARCHAR(50) NULL,
SchemaName VARCHAR(50) NULL,
TableName VARCHAR(50) NULL,
PrimaryKey VARCHAR(50) NULL,
Deprecated BIT NOT NULL
CONSTRAINT DF_etl_Address_Deprecated DEFAULT(0)
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_Address_FullAddress
ON etl.Address (FullAddress)
DROP TABLE IF EXISTS etl.AddressType
CREATE TABLE etl.AddressType (
AddressTypeID TINYINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AddressType_AddressTypeID PRIMARY KEY,
AddressType VARCHAR(150) NOT NULL
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_AddressType_AddressType
ON etl.AddressType (AddressType)
INSERT INTO etl.AddressType (AddressType)
VALUES
('Database'),
('File'),
('Report'),
('API')
ALTER TABLE etl.Address
ADD CONSTRAINT FK_etl_Address_AddressTypeID FOREIGN KEY (AddressTypeID)
REFERENCES etl.AddressType (AddressTypeID)
ALTER TABLE etl.Pipeline
ADD CONSTRAINT FK_etl_Pipeline_SourceAddressID FOREIGN KEY (SourceAddressID)
REFERENCES etl.Address (AddressID)
ALTER TABLE etl.Pipeline
ADD CONSTRAINT FK_etl_Pipeline_TargetAddressID FOREIGN KEY (TargetAddressID)
REFERENCES etl.Address (AddressID)
DROP TABLE IF EXISTS etl.AuditType
CREATE TABLE etl.AuditType (
AuditTypeID TINYINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AuditType_AuditTypeID PRIMARY KEY,
AuditType VARCHAR(150) NOT NULL
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_AuditType_AuditType
ON etl.AuditType (AuditType)
INSERT INTO etl.AuditType (AuditType)
VALUES
('Completeness'),
('Accuracy'),
('Timeliness'),
('Custom')
DROP TABLE IF EXISTS etl.AuditColumnRule
CREATE TABLE etl.AuditColumnRule (
AuditColumnRuleID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AuditColumnRule_AuditColumnRuleID PRIMARY KEY,
PipelineID INT NOT NULL,
AuditTypeID TINYINT NOT NULL,
AuditColumn VARCHAR(50) NOT NULL,
MinimumBound VARCHAR(50) NULL,
MaximumBound VARCHAR(50) NULL,
LookbackDays INT NOT NULL,
StdDeviationFactor DECIMAL(17,2) NOT NULL,
InfoAlert BIT NOT NULL,
ErrorAlert BIT NOT NULL,
RuleStartDate DATE NOT NULL,
RuleEndDate DATE NOT NULL,
LastAuditColumnLogID BIGINT NULL,
Active BIT NOT NULL
CONSTRAINT DF_etl_AuditColumnRule_Active DEFAULT(1)
)
ALTER TABLE etl.AuditColumnRule
ADD CONSTRAINT FK_etl_AuditColumnRule_PipelineID FOREIGN KEY (PipelineID)
REFERENCES etl.Pipeline (PipelineID)
ALTER TABLE etl.AuditColumnRule
ADD CONSTRAINT FK_etl_AuditColumnRule_AuditTypeID FOREIGN KEY (AuditTypeID)
REFERENCES etl.AuditType (AuditTypeID)
CREATE NONCLUSTERED INDEX IX_etl_AuditColumnRule_PipelineID_AuditTypeID_Active
ON etl.AuditColumnRule (PipelineID, AuditTypeID, Active)
DROP TABLE IF EXISTS etl.AuditColumnLog
CREATE TABLE etl.AuditColumnLog (
AuditColumnLogID BIGINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AuditColumnLog_AuditColumnLogID PRIMARY KEY,
RunTimeID BIGINT NOT NULL,
AuditColumnRuleID INT NOT NULL,
DateRecorded DATE NOT NULL,
HourRecorded TINYINT NOT NULL,
RecordCount BIGINT NOT NULL,
RecordViolationCount BIGINT NOT NULL,
ResultScore DECIMAL(20,4) NOT NULL,
Threshold DECIMAL(20,4) NULL,
Success BIT NULL,
FullLoad BIT NOT NULL,
ChunkOut BIT NOT NULL
)
ALTER TABLE etl.AuditColumnLog
ADD CONSTRAINT FK_etl_AuditColumnLog_RunTimeID FOREIGN KEY (RunTimeID)
REFERENCES etl.RunTime (RunTimeID)
ALTER TABLE etl.AuditColumnLog
ADD CONSTRAINT FK_etl_AuditColumnLog_AuditColumnRuleID FOREIGN KEY (AuditColumnRuleID)
REFERENCES etl.AuditColumnRule (AuditColumnRuleID)
CREATE NONCLUSTERED INDEX IX_etl_AuditColumnLog_AuditColumnRuleID
ON etl.AuditColumnLog (AuditColumnRuleID, HourRecorded, DateRecorded)
INCLUDE (ResultScore)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_AuditColumnLog_RunTimeID_AuditColumnRuleID
ON etl.AuditColumnLog (RunTimeID, AuditColumnRuleID)
DROP TABLE IF EXISTS etl.AddressDependency
CREATE TABLE etl.AddressDependency (
AddressDependencyID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AddressDependency_AddressDependencyID PRIMARY KEY,
SourceAddressID INT NOT NULL,
TargetAddressID INT NOT NULL
)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_AddressDependency_TargetAddresses
ON etl.AddressDependency (TargetAddressID, SourceAddressID)
CREATE UNIQUE NONCLUSTERED INDEX IX_etl_AddressDependency_SourceAddresses
ON etl.AddressDependency (SourceAddressID, TargetAddressID)
ALTER TABLE etl.AddressDependency
ADD CONSTRAINT FK_etl_AddressDependency_SourceAddressID FOREIGN KEY (SourceAddressID)
REFERENCES etl.Address (AddressID)
ALTER TABLE etl.AddressDependency
ADD CONSTRAINT FK_etl_AddressDependency_TargetAddressID FOREIGN KEY (TargetAddressID)
REFERENCES etl.Address (AddressID)
DROP TABLE IF EXISTS etl.Deployment
CREATE TABLE etl.Deployment (
Ticket VARCHAR(20) NOT NULL
CONSTRAINT PK_etl_Deployment_Ticket PRIMARY KEY
)
DROP TABLE IF EXISTS etl.TimelinessRunTimeLog
CREATE TABLE etl.TimelinessRunTimeLog (
TimelinessRunTimeLogID BIGINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_TimelinessRunTimeLog_TimelinessRunTimeLogID PRIMARY KEY,
RunTimeID BIGINT NOT NULL,
PipelineID INT NOT NULL,
RunTimeSeconds BIGINT NOT NULL,
SecondsThreshold INT NOT NULL
)
CREATE UNIQUE INDEX IX_etl_TimelinessRunTimeLog_RunTimeID
ON etl.TimelinessRunTimeLog (RunTimeID)
ALTER TABLE etl.TimelinessRunTimeLog
ADD CONSTRAINT FK_etl_TimelinessRunTimeLog_RunTimeID FOREIGN KEY (RunTimeID)
REFERENCES etl.RunTime (RunTimeID)
ALTER TABLE etl.TimelinessRunTimeLog
ADD CONSTRAINT FK_etl_TimelinessRunTimeLog_PipelineID FOREIGN KEY (PipelineID)
REFERENCES etl.Pipeline (PipelineID)
DROP TABLE IF EXISTS etl.AuditCustomRule
CREATE TABLE etl.AuditCustomRule (
AuditCustomRuleID INT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_AuditCustomRule_AuditCustomRuleID PRIMARY KEY,
PipelineID INT NOT NULL,
CustomSQL NVARCHAR(MAX),
CustomAlertMessage VARCHAR(400),
RuleStartDate DATE NOT NULL,
RuleEndDate DATE NOT NULL,
Active BIT NOT NULL
CONSTRAINT DF_etl_AuditCustomRule_Active DEFAULT (1),
InfoAlert BIT NOT NULL,
ErrorAlert BIT NOT NULL
)
ALTER TABLE etl.AuditCustomRule
ADD CONSTRAINT FK_etl_AuditCustomRule_PipelineID FOREIGN KEY (PipelineID)
REFERENCES etl.Pipeline (PipelineID)
CREATE NONCLUSTERED INDEX IX_etl_AuditCustomRule_PipelineID_Active
ON etl.AuditCustomRule (PipelineID, Active)
DROP TABLE IF EXISTS etl.AuditCustomLog
CREATE TABLE etl.AuditCustomLog (
AuditCustomLogID BIGINT IDENTITY(1,1) NOT NULL
CONSTRAINT PK_etl_AuditCustomLog_AuditCustomLogID PRIMARY KEY,
RunTimeID BIGINT NOT NULL,
AuditCustomRuleID INT NOT NULL,
CustomQueryStart DATETIMEOFFSET(2) NOT NULL,
CustomQueryEnd DATETIMEOFFSET(2) NULL,
RunTimeSeconds INT NULL,
Completed BIT NOT NULL
CONSTRAINT DF_etl_AuditCustomLog_Completed DEFAULT(0),
Success BIT NULL
)
CREATE UNIQUE INDEX IX_etl_AuditCustomLog_RunTimeID_AuditCustomRuleID
ON etl.AuditCustomLog (RunTimeID, AuditCustomRuleID)
ALTER TABLE etl.AuditCustomLog
ADD CONSTRAINT FK_etl_AuditCustomLog_RunTimeID FOREIGN KEY (RunTimeID)
REFERENCES etl.RunTime (RunTimeID)
ALTER TABLE etl.AuditCustomLog
ADD CONSTRAINT FK_etl_AuditCustomLog_AuditCustomRuleID FOREIGN KEY (AuditCustomRuleID)
REFERENCES etl.AuditCustomRule (AuditCustomRuleID)
DROP TABLE IF EXISTS etl.DeprecationDeclared
CREATE TABLE etl.DeprecationDeclared (
DeprecationDeclaredID BIGINT NOT NULL IDENTITY(1,1)
CONSTRAINT PK_etl_DeprecationDeclared_DeprecationDeclaredID PRIMARY KEY,
AddressID INT NOT NULL,
ID INT NOT NULL,
IDType VARCHAR(150) NOT NULL,
IDName VARCHAR(150) NULL,
MarkedForDeprecation DATETIMEOFFSET(2) NOT NULL,
TicketsCreated BIT NOT NULL
CONSTRAINT DF_etl_DeprecationDeclared_TicketsCreated DEFAULT(0)
)
CREATE UNIQUE INDEX IX_etl_DeprecationDeclared_AddressID_ID_IDType
ON etl.DeprecationDeclared (AddressID, ID, IDType)
CREATE NONCLUSTERED INDEX IX_etl_DeprecationDeclared_MarkedForDeprecation
ON etl.DeprecationDeclared (MarkedForDeprecation) INCLUDE (TicketsCreated)
/* Create Types */
DROP PROCEDURE IF EXISTS etl.AddressDependency_DI
DROP TYPE IF EXISTS etl.SourceAddresses
CREATE TYPE etl.SourceAddresses AS TABLE (
FullAddress VARCHAR(150) NOT NULL,
AddressType VARCHAR(150) NOT NULL
)
/* Create Stored Procedures */
DROP PROCEDURE IF EXISTS etl.Address_SI
GO
CREATE PROCEDURE [etl].[Address_SI] @FullAddress VARCHAR(150),
@AddressType VARCHAR(150),
@AddressID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
SELECT @AddressID = AddressID
FROM etl.Address
WHERE FullAddress = @FullAddress
/* If address cannot be found, create it */
IF @AddressID IS NULL
BEGIN
DECLARE @AddressTypeID TINYINT,
@DatabaseName VARCHAR(50) = NULL,
@SchemaName VARCHAR(50) = NULL,
@TableName VARCHAR(50) = NULL
SELECT @AddressTypeID = AddressTypeID
FROM etl.AddressType
WHERE AddressType = @AddressType
IF @AddressTypeID IS NULL
BEGIN
DECLARE @ErrorMessage VARCHAR(100) = 'Unable to find AddressType: ' + @AddressType + '';
THROW 51000, @ErrorMessage, 1;
END
IF @AddressType = 'Database'
BEGIN
SET @DatabaseName = PARSENAME(@FullAddress, 3)
SET @SchemaName = PARSENAME(@FullAddress, 2)
SET @TableName = PARSENAME(@FullAddress, 1)
END
INSERT INTO etl.Address (FullAddress, AddressTypeID, DatabaseName, SchemaName, TableName)
VALUES (@FullAddress, @AddressTypeID, @DatabaseName, @SchemaName, @TableName)
SET @AddressID = SCOPE_IDENTITY()
END
END
GO
DROP PROCEDURE IF EXISTS etl.AddressDependency_DI
GO
CREATE PROCEDURE [etl].[AddressDependency_DI] @TargetFullAddress VARCHAR(150),
@TargetAddressType VARCHAR(150),
@SourceAddresses etl.SourceAddresses READONLY
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @TargetAddressID INT,
@SourceFullAddress VARCHAR(150),
@SourceAddressType VARCHAR(150),
@SourceAddressID INT
DECLARE @SourceIDs TABLE (SourceAddressID INT)
/* Insert source addresses that we don't have yet */
DECLARE address_cursor CURSOR FAST_FORWARD FOR
SELECT
FullAddress,
AddressType
FROM @SourceAddresses
ORDER BY FullAddress
OPEN address_cursor
FETCH NEXT FROM address_cursor INTO
@SourceFullAddress, @SourceAddressType
WHILE @@FETCH_STATUS = 0
BEGIN
/* This is required to have the variable be overwritten correctly */
SET @SourceAddressID = NULL
EXEC etl.Address_SI @FullAddress = @SourceFullAddress,
@AddressType = @SourceAddressType,
@AddressID = @SourceAddressID OUTPUT
INSERT INTO @SourceIDs (SourceAddressID)
VALUES (@SourceAddressID)
FETCH NEXT FROM address_cursor INTO
@SourceFullAddress, @SourceAddressType
END
CLOSE address_cursor
DEALLOCATE address_cursor
/* Grab target address */
SELECT @TargetAddressID = AddressID
FROM etl.Address
WHERE FullAddress = @TargetFullAddress
/* If target address can't be found, create it */
IF @TargetAddressID IS NULL
BEGIN
EXEC etl.Address_SI @FullAddress = @TargetFullAddress,
@AddressType = @TargetAddressType,
@AddressID = @TargetAddressID OUTPUT
END
/* Create table so we can cross join for combinations */
DECLARE @TargetAddressTable TABLE (TargetAddressID INT)
INSERT INTO @TargetAddressTable (TargetAddressID) VALUES (@TargetAddressID)
/* Create combinations */
DROP TABLE IF EXISTS #TableDependency
CREATE TABLE #TableDependency (
SourceAddressID INT NOT NULL,
TargetAddressID INT NOT NULL
)
INSERT INTO #TableDependency (SourceAddressID, TargetAddressID)
SELECT
s.SourceAddressID,
t.TargetAddressID
FROM @SourceIDs AS s
CROSS JOIN @TargetAddressTable AS t
/* Delete any source addresses that are not found in inputted list */
DELETE td
FROM etl.AddressDependency AS td
WHERE td.TargetAddressID = @TargetAddressID
AND NOT EXISTS (
SELECT 1/0
FROM #TableDependency AS d
WHERE d.SourceAddressID = td.SourceAddressID
)
/* Input given combinations into dependency table */
INSERT INTO etl.AddressDependency (SourceAddressID, TargetAddressID)
SELECT
SourceAddressID,
TargetAddressID
FROM #TableDependency AS td
WHERE NOT EXISTS (
SELECT 1/0
FROM etl.AddressDependency AS ad
WHERE ad.SourceAddressID = td.SourceAddressID
AND ad.TargetAddressID = td.TargetAddressID
)
END
GO
DROP PROCEDURE IF EXISTS etl.AuditAccuracy_I
GO
CREATE PROCEDURE etl.AuditAccuracy_I @ParentRunTimeID BIGINT,
@RunTimeID BIGINT,
@PipelineID INT,
@FullLoad BIT,
@Debug BIT = 0
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @Timestamp DATETIMEOFFSET(2),
@DateRecorded DATE,
@HourRecorded TINYINT,
@ChunkOut BIT = 0,
@AuditType VARCHAR(150) = 'Accuracy',
@AuditTypeID TINYINT,
@SQLQuery NVARCHAR(MAX),
@ErrorMessage VARCHAR(100)
IF @FullLoad = 0 AND @ParentRunTimeID IS NULL
BEGIN
SET @ErrorMessage = '@ParentRunTimeID is required for incremental audits, @FullLoad=0';
THROW 51000, @ErrorMessage, 1;
END
/* Can't depend on certain ID, so seek based upon text */
SELECT @AuditTypeID = AuditTypeID
FROM etl.AuditType
WHERE AuditType = @AuditType
/* Use pipeline start of the audit pipeline to determine date/hour comparisons */
SELECT @Timestamp = PipelineStart
FROM etl.RunTime
WHERE RunTimeID = @RunTimeID
SET @DateRecorded = CAST(@Timestamp AS DATE)
SET @HourRecorded = DATEPART(HOUR, @Timestamp)
/* Grab necessary accuracy rule information */
DROP TABLE IF EXISTS #AccuracyRules
;WITH CTE AS (
SELECT
AuditColumnRuleID
FROM etl.AuditColumnRule
WHERE PipelineID = @PipelineID
AND AuditTypeID = @AuditTypeID
AND Active = 1
)
SELECT
a.AuditColumnRuleID,
ap.DatabaseName,
ap.SchemaName,
ap.TableName,
ap.PrimaryKey,
a.AuditColumn,
a.MinimumBound,
a.MaximumBound,
0 AS Complete /* For cursor work */
INTO #AccuracyRules
FROM etl.AuditColumnRule AS a
INNER JOIN CTE
ON CTE.AuditColumnRuleID = a.AuditColumnRuleID
INNER JOIN etl.Pipeline AS p
ON p.PipelineID = a.PipelineID
INNER JOIN etl.Address AS ap
ON ap.AddressID = p.TargetAddressID
/* Begin string construction for dynamic query */
DECLARE @TargetTable NVARCHAR(150) = (SELECT DISTINCT CONCAT_WS('.', DatabaseName, SchemaName, TableName) FROM #AccuracyRules),
@PrimaryKey NVARCHAR(50) = (SELECT DISTINCT PrimaryKey FROM #AccuracyRules),
@AggColumnList NVARCHAR(MAX),
@CountColumnList NVARCHAR(MAX),
@Agg_List NVARCHAR(MAX) = '',
@Count_List NVARCHAR(MAX),
@RecordCount INT
/* Each list needs unique tag added to prevent identity clash */
SET @AggColumnList = (SELECT STRING_AGG(AuditColumn + '#Violation', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
SET @CountColumnList = (SELECT STRING_AGG(AuditColumn + '#Count', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
SET @Count_List = (SELECT STRING_AGG('COUNT(' + AuditColumn + ') AS ' + AuditColumn + '#Count', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
DECLARE @ColumnName VARCHAR(50), @MinimumBound VARCHAR(50), @MaximumBound VARCHAR(50)
DECLARE accuracy_cursor CURSOR FOR
SELECT AuditColumn, MinimumBound, MaximumBound
FROM #AccuracyRules
WHERE Complete = 0
ORDER BY AuditColumnRuleID
OPEN accuracy_cursor
FETCH NEXT FROM accuracy_cursor INTO
@ColumnName, @MinimumBound, @MaximumBound
/* Form case strings for bound checks */
WHILE @@FETCH_STATUS = 0
BEGIN
IF @MaximumBound IS NULL
BEGIN /* Test if we need to wrap with quotes */
IF TRY_CAST(@MinimumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MinimumBound AS TIME(7)) IS NOT NULL
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' <= ' + '''' + @MinimumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' <= ' + @MinimumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @MinimumBound IS NULL
BEGIN
IF TRY_CAST(@MaximumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MaximumBound AS TIME(7)) IS NOT NULL
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + '''' + @MaximumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + @MaximumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @MinimumBound IS NOT NULL AND @MaximumBound IS NOT NULL
BEGIN
IF (TRY_CAST(@MaximumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MaximumBound AS TIME(7)) IS NOT NULL)
AND
(TRY_CAST(@MinimumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MinimumBound AS TIME(7)) IS NOT NULL)
BEGIN /* If boths are time stamps, wrap around quotes */
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + '''' + @MaximumBound + '''' + ' OR ' + @ColumnName + ' <= ' + '''' + @MinimumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN /* otherwise don't add quotes */
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + @MaximumBound + ' OR ' + @ColumnName + ' <= ' + @MinimumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @Debug = 1
PRINT @Agg_List
UPDATE #AccuracyRules
SET Complete = 1
WHERE AuditColumn = @ColumnName
FETCH NEXT FROM accuracy_cursor INTO
@ColumnName, @MinimumBound, @MaximumBound
END
CLOSE accuracy_cursor
DEALLOCATE accuracy_cursor
DROP TABLE IF EXISTS #Results
CREATE TABLE #Results (
ColumnName VARCHAR(50),
RecordCount BIGINT,
RecordViolationCount BIGINT
)
IF @FullLoad = 0
BEGIN
/* Only check against parent's last run time */
SET @SQLQuery = '
DROP TABLE IF EXISTS #temp
;WITH CTE AS (
SELECT
' + @PrimaryKey + '
FROM ' + @TargetTable + '
WHERE RunTimeID = ' + CAST(@ParentRunTimeID AS VARCHAR(50)) + '
)
SELECT
{agg_list}
{count_list}
INTO #temp
FROM ' + @TargetTable + '
INNER JOIN CTE
ON CTE.' + @PrimaryKey + ' = ' + @TargetTable + '.' + @PrimaryKey + '
DROP TABLE IF EXISTS #first
SELECT
REPLACE(ColumnName, ''#Count'', '''') AS ColumnName,
RecordCount
INTO #first
FROM (
SELECT
{count_column_list}
FROM #temp
) AS Subquery
UNPIVOT
( RecordCount FOR ColumnName IN (
{count_column_list}
)) AS pvt
DROP TABLE IF EXISTS #second
SELECT
REPLACE(ColumnName, ''#Violation'', '''') AS ColumnName,
RecordViolationCount
INTO #second
FROM (
SELECT
{agg_column_list}
FROM #temp
) AS Subquery
UNPIVOT
( RecordViolationCount FOR ColumnName IN (
{agg_column_list}
)) AS pvt
DROP TABLE #temp
INSERT INTO #Results (ColumnName, RecordCount, RecordViolationCount)
SELECT
f.ColumnName,
f.RecordCount,
s.RecordViolationCount
FROM #first AS f
INNER JOIN #second AS s
ON s.ColumnName = f.ColumnName'
END
ELSE
BEGIN
/* Double unpivot and join back query string */
SET @SQLQuery = '
DROP TABLE IF EXISTS #temp
SELECT
{agg_list}
{count_list}
INTO #temp
FROM ' + @TargetTable + '
DROP TABLE IF EXISTS #first
SELECT
REPLACE(ColumnName, ''#Count'', '''') AS ColumnName,
RecordCount
INTO #first
FROM (
SELECT
{count_column_list}
FROM #temp
) AS Subquery
UNPIVOT
( RecordCount FOR ColumnName IN (
{count_column_list}
)) AS pvt
DROP TABLE IF EXISTS #second
SELECT
REPLACE(ColumnName, ''#Violation'', '''') AS ColumnName,
RecordViolationCount
INTO #second
FROM (
SELECT
{agg_column_list}
FROM #temp
) AS Subquery
UNPIVOT
( RecordViolationCount FOR ColumnName IN (
{agg_column_list}
)) AS pvt
DROP TABLE #temp
INSERT INTO #Results (ColumnName, RecordCount, RecordViolationCount)
SELECT
f.ColumnName,
f.RecordCount,
s.RecordViolationCount
FROM #first AS f
INNER JOIN #second AS s
ON s.ColumnName = f.ColumnName'
END
/* inject constructed column lists and agg lists into string */
SET @SQLQuery = REPLACE(@SQLQuery, '{agg_list}', @Agg_List)
SET @SQLQuery = REPLACE(@SQLQuery, '{agg_column_list}', @AggColumnList)
SET @SQLQuery = REPLACE(@SQLQuery, '{count_list}', @Count_List)
SET @SQLQuery = REPLACE(@SQLQuery, '{count_column_list}', @CountColumnList)
IF @Debug=1
BEGIN
PRINT @SQLQuery
END
ELSE
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
EXEC sys.sp_executesql @SQLQuery
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
END
IF @Debug=1
BEGIN
SELECT
@RunTimeID AS RunTimeID,
cr.AuditColumnRuleID,
@DateRecorded AS DateRecorded,
@HourRecorded AS HourRecorded,
r.RecordCount,
r.RecordViolationCount,
CAST(CAST((r.RecordCount - r.RecordViolationCount) AS DECIMAL) / CAST(r.RecordCount AS DECIMAL) AS DECIMAL(20,4)) AS ResultScore
FROM #Results AS r
INNER JOIN #AccuracyRules AS cr
ON cr.AuditColumn = r.ColumnName
END
ELSE
BEGIN
/* Used to update LastAuditColumnLogID */
DECLARE @AccuracyTable TABLE (
AuditColumnRuleID INT,
AuditColumnLogID BIGINT
)
INSERT INTO etl.AuditColumnLog(
RunTimeID,
AuditColumnRuleID,
DateRecorded,
HourRecorded,
RecordCount,
RecordViolationCount,
ResultScore,
FullLoad,
ChunkOut
)
OUTPUT inserted.AuditColumnRuleID, inserted.AuditColumnLogID
INTO @AccuracyTable
SELECT
@RunTimeID,
cr.AuditColumnRuleID,
@DateRecorded,
@HourRecorded,
r.RecordCount,
r.RecordViolationCount,
CAST(CAST((r.RecordCount - r.RecordViolationCount) AS DECIMAL) / CAST(r.RecordCount AS DECIMAL) AS DECIMAL(20,4)) AS ResultScore,
@FullLoad,
@ChunkOut
FROM #Results AS r
INNER JOIN #AccuracyRules AS cr
ON cr.AuditColumn = r.ColumnName
DROP TABLE #AccuracyRules
DROP TABLE #Results
UPDATE A
SET LastAuditColumnLogID = t.AuditColumnLogID
FROM etl.AuditColumnRule AS A
INNER JOIN @AccuracyTable AS t
ON t.AuditColumnRuleID = A.AuditColumnRuleID
END
END
GO
DROP PROCEDURE IF EXISTS etl.AuditAccuracyBatch_I
GO
CREATE PROCEDURE etl.AuditAccuracyBatch_I @ParentRunTimeID BIGINT,
@RunTimeID BIGINT,
@PipelineID INT,
@FullLoad BIT,
@Debug BIT = 0,
@DefaultBatchSize INT = 100000
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @Timestamp DATETIMEOFFSET(2),
@DateRecorded DATE,
@HourRecorded TINYINT,
@ChunkOut BIT = 1,
@AuditType VARCHAR(150) = 'Accuracy',
@AuditTypeID TINYINT,
@ErrorMessage VARCHAR(100)
IF @FullLoad = 0 AND @ParentRunTimeID IS NULL
BEGIN
SET @ErrorMessage = '@ParentRunTimeID is required for incremental audits, @FullLoad=0';
THROW 51000, @ErrorMessage, 1;
END
/* Can't depend on certain ID, so seek based upon text */
SELECT @AuditTypeID = AuditTypeID
FROM etl.AuditType
WHERE AuditType = @AuditType
/* Use pipeline start of the audit pipeline to determine date/hour comparisons */
SELECT @Timestamp = PipelineStart
FROM etl.RunTime
WHERE RunTimeID = @RunTimeID
SET @DateRecorded = CAST(@Timestamp AS DATE)
SET @HourRecorded = DATEPART(HOUR, @Timestamp)
/* Grab necessary accuracy rule information */
DROP TABLE IF EXISTS #AccuracyRules
;WITH CTE AS (
SELECT
AuditColumnRuleID
FROM etl.AuditColumnRule
WHERE PipelineID = @PipelineID
AND AuditTypeID = @AuditTypeID
AND Active = 1
)
SELECT
a.AuditColumnRuleID,
ap.DatabaseName,
ap.SchemaName,
ap.TableName,
ap.PrimaryKey,
a.AuditColumn,
a.MinimumBound,
a.MaximumBound,
ISNULL(JSON_VALUE(p.PipelineArgs, '$.info.audit.batch_size'), @DefaultBatchSize) AS _BatchSize, /*Pipeline Args*/
0 AS Complete /* For cursor work */
INTO #AccuracyRules
FROM etl.AuditColumnRule AS a
INNER JOIN CTE
ON CTE.AuditColumnRuleID = a.AuditColumnRuleID
INNER JOIN etl.Pipeline AS p
ON p.PipelineID = a.PipelineID
INNER JOIN etl.Address AS ap
ON ap.AddressID = p.TargetAddressID
/* Begin string construction for dynamic query */
DECLARE @TargetTable NVARCHAR(150) = (SELECT DISTINCT CONCAT_WS('.', DatabaseName, SchemaName, TableName) FROM #AccuracyRules),
@PrimaryKey NVARCHAR(50) = (SELECT DISTINCT PrimaryKey FROM #AccuracyRules),
@AggColumnList NVARCHAR(MAX),
@CountColumnList NVARCHAR(MAX),
@Agg_List NVARCHAR(MAX) = '',
@Count_List NVARCHAR(MAX),
@BatchSize INT = (SELECT DISTINCT _BatchSize FROM #AccuracyRules),
@RecordCount INT
/* Each list needs unique tag added to prevent identity clash */
SET @AggColumnList = (SELECT STRING_AGG(AuditColumn + '#Violation', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
SET @CountColumnList = (SELECT STRING_AGG(AuditColumn + '#Count', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
SET @Count_List = (SELECT STRING_AGG('COUNT(' + AuditColumn + ') AS ' + AuditColumn + '#Count', ', ') WITHIN GROUP (ORDER BY AuditColumnRuleID) FROM #AccuracyRules)
DECLARE @ColumnName VARCHAR(50), @MinimumBound VARCHAR(50), @MaximumBound VARCHAR(50), @RowNum INT
DECLARE accuracy_cursor CURSOR FOR
SELECT AuditColumn, MinimumBound, MaximumBound
FROM #AccuracyRules
WHERE Complete=0
ORDER BY AuditColumnRuleID
OPEN accuracy_cursor
FETCH NEXT FROM accuracy_cursor INTO
@ColumnName, @MinimumBound, @MaximumBound
/* Form case strings for bound checks */
WHILE @@FETCH_STATUS = 0
BEGIN
IF @MaximumBound IS NULL
BEGIN /* Test if we need to wrap with quotes */
IF TRY_CAST(@MinimumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MinimumBound AS TIME(7)) IS NOT NULL
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' <= ' + '''' + @MinimumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' <= ' + @MinimumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @MinimumBound IS NULL
BEGIN
IF TRY_CAST(@MaximumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MaximumBound AS TIME(7)) IS NOT NULL
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + '''' + @MaximumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + @MaximumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @MinimumBound IS NOT NULL AND @MaximumBound IS NOT NULL
BEGIN
IF (TRY_CAST(@MaximumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MaximumBound AS TIME(7)) IS NOT NULL)
AND
(TRY_CAST(@MinimumBound AS DATETIMEOFFSET(2)) IS NOT NULL
OR TRY_CAST(@MinimumBound AS TIME(7)) IS NOT NULL)
BEGIN /* If boths are time stamps, wrap around quotes */
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + '''' + @MaximumBound + '''' + ' OR ' + @ColumnName + ' <= ' + '''' + @MinimumBound + '''' + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
ELSE
BEGIN /* otherwise don't add quotes */
SET @Agg_List += 'SUM(CASE WHEN ' + @ColumnName + ' >= ' + @MaximumBound + ' OR ' + @ColumnName + ' <= ' + @MinimumBound + ' THEN 1 ELSE 0 END) AS ' + @ColumnName + '#Violation,' + CHAR(13)
END
END
IF @Debug = 1
PRINT @Agg_List
UPDATE #AccuracyRules
SET Complete=1
WHERE AuditColumn = @ColumnName
FETCH NEXT FROM accuracy_cursor INTO
@ColumnName, @MinimumBound, @MaximumBound
END
CLOSE accuracy_cursor
DEALLOCATE accuracy_cursor
/* Form Dynamic Query Loop */