Skip to content

Commit 1ebae47

Browse files
committed
refactor: Remove unnecessary RAISE NOTICE statements from SQL procedures
- Eliminated commented-out RAISE NOTICE statements across multiple SQL procedures to clean up the code and improve readability. - This change enhances the clarity of the SQL scripts by removing clutter while maintaining functionality.
1 parent fecad4a commit 1ebae47

10 files changed

+7
-117
lines changed

.git-hooks/pre-commit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ if [ -n "$STAGED_SQL_FILES" ]; then
140140

141141
for file in $STAGED_SQL_FILES; do
142142
if [ -f "$file" ]; then
143+
# Skip files with template variables (e.g., ${YEAR}) as they cannot be parsed
144+
if grep -q '\${' "$file" 2>/dev/null; then
145+
echo -e "${YELLOW}⚠️ Skipping template file: $file${NC}"
146+
continue
147+
fi
148+
143149
if ! sqlfluff lint "$file" > /dev/null 2>&1; then
144150
SQLFLUFF_FAILED=1
145151
echo -e "${RED}❌ SQL format check failed for: $file${NC}"

sql/dwh/ETL_41_addConstraintsIndexesTriggers.sql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ BEGIN
119119
WHERE fact_id IN (
120120
SELECT fact_id FROM duplicates WHERE rn > 1
121121
);
122-
122+
123123
GET DIAGNOSTICS duplicates_count = ROW_COUNT;
124124
IF duplicates_count > 0 THEN
125125
RAISE NOTICE 'Removed % duplicate facts before creating unique index', duplicates_count;
@@ -311,11 +311,9 @@ CREATE OR REPLACE FUNCTION dwh.update_days_to_resolution()
311311
ON f.action_dimension_id_date = d.dimension_day_id
312312
WHERE id_note = NEW.id_note
313313
AND action_comment = 'reopened';
314-
--RAISE NOTICE 'Reopen date: %.', m_reopen_date;
315314
IF (m_reopen_date IS NOT NULL) THEN
316315
-- Days from the last reopen.
317316
m_days := m_close_date - m_reopen_date;
318-
--RAISE NOTICE 'Difference dates %-%: %.', m_close_date, m_reopen_date, m_days;
319317
UPDATE dwh.facts
320318
SET days_to_resolution_from_reopen = m_days
321319
WHERE fact_id = NEW.fact_id;
@@ -376,4 +374,3 @@ CREATE INDEX IF NOT EXISTS idx_fact_hashtags_composite
376374
ON dwh.fact_hashtags(dimension_hashtag_id, fact_id);
377375
COMMENT ON INDEX dwh.idx_fact_hashtags_composite IS
378376
'Composite index for common hashtag + fact join patterns';
379-

sql/dwh/Staging_31_createBaseStagingObjects.sql

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ CREATE OR REPLACE FUNCTION staging.get_hashtag_id (
4545
m_id_dimension_hashtag INTEGER;
4646
r RECORD;
4747
BEGIN
48-
--RAISE NOTICE 'Requesting id for hashtag: %.', m_hashtag_name;
4948
IF (m_hashtag_name IS NULL) THEN
5049
m_id_dimension_hashtag := 1;
5150
ELSE
@@ -85,15 +84,10 @@ CREATE OR REPLACE PROCEDURE staging.get_hashtag (
8584
BEGIN
8685
pos := STRPOS(m_text_comment, '#');
8786
IF (pos <> 0) THEN
88-
--RAISE NOTICE 'Position number sign: %.', pos;
8987
substr_after := SUBSTR(m_text_comment, pos+1);
90-
--RAISE NOTICE 'Substring after number sign: %.', substr_after;
9188
m_hashtag_name := ARRAY_TO_STRING(REGEXP_MATCHES(substr_after, '^\w+'), ';');
92-
--RAISE NOTICE 'Hashtag name: %.', m_hashtag_name;
9389
length := LENGTH(m_hashtag_name);
94-
--RAISE NOTICE 'Length hashtag name: %.', length;
9590
m_text_comment := SUBSTR(substr_after, length+2);
96-
--RAISE NOTICE 'New substring: %.', m_text_comment;
9791
ELSE
9892
m_text_comment := NULL;
9993
END IF;

sql/dwh/Staging_32_createStagingObjects.sql

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
5454
SELECT COALESCE(MAX(note_id), 0)
5555
INTO max_note_id_snapshot
5656
FROM public.notes;
57-
-- RAISE NOTICE 'Day % started.', max_processed_timestamp;
5857

59-
--RAISE NOTICE 'Flag 1: %', CLOCK_TIMESTAMP();
6058
-- Note: For partitioned tables, ON CONFLICT doesn't work with indexes on the main table
6159
-- We need indexes on each partition. Since this is complex and the unique index
6260
-- will prevent duplicates at the database level, we'll insert without ON CONFLICT
@@ -68,7 +66,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
6866
-- and should ideally be executed in READ ONLY mode for better concurrency, but this procedure also
6967
-- performs writes, so READ ONLY cannot be applied to the entire transaction.
7068
IF (m_equals) THEN
71-
--RAISE NOTICE 'Processing equals';
7269
OPEN notes_on_day FOR EXECUTE('
7370
SELECT /* Notes-staging */
7471
c.note_id id_note, c.sequence_action sequence_action,
@@ -94,7 +91,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
9491
ORDER BY c.note_id, c.sequence_action
9592
');
9693
ELSE
97-
--RAISE NOTICE 'Processing greater than';
9894
OPEN notes_on_day FOR EXECUTE('
9995
SELECT /* Notes-staging */
10096
c.note_id id_note, c.sequence_action sequence_action,
@@ -121,14 +117,10 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
121117
');
122118
END IF;
123119
LOOP
124-
--RAISE NOTICE 'Flag 2: %', CLOCK_TIMESTAMP();
125-
--RAISE NOTICE 'before fetch % - %.', CLOCK_TIMESTAMP(), m_count;
126120
FETCH notes_on_day INTO rec_note_action;
127-
--RAISE NOTICE 'after fetch % - %.', CLOCK_TIMESTAMP(), m_count;
128121
-- Exit when no more rows to fetch.
129122
EXIT WHEN NOT FOUND;
130123

131-
--RAISE NOTICE 'note_id %, sequence %', rec_note_action.id_note,
132124
-- rec_note_action.sequence_action;
133125

134126
-- Gets the country of the comment.
@@ -175,15 +167,13 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
175167
END IF;
176168
END IF;
177169
END IF;
178-
--RAISE NOTICE 'Flag 3: %', CLOCK_TIMESTAMP();
179170

180171
-- Gets the user who created the note.
181172
-- Handle anonymous notes (NULL user_id) by using Anonymous user (user_id = -1)
182173
SELECT /* Notes-staging */ dimension_user_id
183174
INTO m_dimension_user_open
184175
FROM dwh.dimension_users
185176
WHERE user_id = COALESCE(rec_note_action.created_id_user, -1) AND is_current;
186-
--RAISE NOTICE 'Flag 4: %', CLOCK_TIMESTAMP();
187177

188178
-- Gets the user who performed the action (if action is opened, then it
189179
-- is the same).
@@ -192,7 +182,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
192182
INTO m_dimension_user_action
193183
FROM dwh.dimension_users
194184
WHERE user_id = COALESCE(rec_note_action.action_id_user, -1) AND is_current;
195-
--RAISE NOTICE 'Flag 5: %', CLOCK_TIMESTAMP();
196185

197186
-- Gets the days of the actions
198187
m_opened_id_date := dwh.get_date_id(rec_note_action.created_at);
@@ -201,21 +190,18 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
201190
m_action_id_date := dwh.get_date_id(rec_note_action.action_at);
202191
m_action_id_hour_of_week :=
203192
dwh.get_hour_of_week_id(rec_note_action.action_at);
204-
--RAISE NOTICE 'Flag 6: %', CLOCK_TIMESTAMP();
205193

206194
-- When the action is 'closed' it copies the data from the 'action'.
207195
IF (rec_note_action.action_comment = 'closed') THEN
208196
m_closed_id_date := m_action_id_date;
209197
m_closed_id_hour_of_week := m_action_id_hour_of_week;
210198
m_dimension_user_close := m_dimension_user_action;
211199
END IF;
212-
--RAISE NOTICE 'Flag 7: %', CLOCK_TIMESTAMP();
213200

214201
-- Gets the id of the app, if the action is opening.
215202
IF (rec_note_action.action_comment = 'opened') THEN
216203
-- Use body from cursor (already loaded via LEFT JOIN in query)
217204
m_text_comment := rec_note_action.body;
218-
--RAISE NOTICE 'Flag 8: %', CLOCK_TIMESTAMP();
219205
m_application := staging.get_application(m_text_comment);
220206
-- Try to parse version simple pattern N.N or N.N.N
221207
IF (m_text_comment ~* '\\d+\\.\\d+(\\.\\d+)?') THEN
@@ -224,7 +210,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
224210
(SELECT regexp_match(m_text_comment, '(\\d+\\.\\d+(?:\\.\\d+)?)')::text)
225211
);
226212
END IF;
227-
--RAISE NOTICE 'Flag 9: %', CLOCK_TIMESTAMP();
228213
ELSE
229214
m_application := NULL;
230215
m_application_version := NULL;
@@ -236,7 +221,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
236221
ELSIF (rec_note_action.action_comment = 'reopened') THEN
237222
m_recent_opened_dimension_id_date := m_action_id_date;
238223
ELSE
239-
--RAISE NOTICE 'Flag 10: %', CLOCK_TIMESTAMP();
240224
SELECT /* Notes-staging */ recent_opened_dimension_id_date
241225
INTO m_recent_opened_dimension_id_date
242226
FROM dwh.facts f
@@ -246,7 +230,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
246230
WHERE f.id_note = rec_note_action.id_note
247231
);
248232
END IF;
249-
--RAISE NOTICE 'Flag 11: %', CLOCK_TIMESTAMP();
250233
-- Handle case where we can't find opening date (fallback to opened_dimension_id_date)
251234
-- This can happen when processing a note for the first time in incremental mode
252235
IF (m_recent_opened_dimension_id_date IS NULL) THEN
@@ -256,7 +239,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
256239
-- Use opened_dimension_id_date as fallback (always available for any note)
257240
m_recent_opened_dimension_id_date := m_opened_id_date;
258241
END IF;
259-
--RAISE NOTICE 'Flag 12: %', CLOCK_TIMESTAMP();
260242

261243
-- Gets hashtags (UNLIMITED)
262244
m_hashtag_number := 0;
@@ -279,7 +261,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
279261
END;
280262
END LOOP;
281263
END IF;
282-
--RAISE NOTICE 'Flag 22: %', CLOCK_TIMESTAMP();
283264

284265
-- Prepare local/timezone/season using note position
285266
-- Note: This SELECT reads from ingestion table (notes) and should ideally
@@ -334,7 +315,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
334315
-- This can happen if the same comment is processed multiple times
335316
m_fact_id := NULL;
336317
END;
337-
--RAISE NOTICE 'Flag 23: %', CLOCK_TIMESTAMP();
338318

339319
-- Populate bridge table for hashtags (ALL hashtags - unlimited)
340320
-- Only insert hashtags if fact_id is not NULL (fact was successfully inserted)
@@ -357,12 +337,10 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
357337
UPDATE /* Notes-ETL */ dwh.dimension_users
358338
SET modified = TRUE
359339
WHERE dimension_user_id = m_dimension_user_action;
360-
--RAISE NOTICE 'Flag 24: %', CLOCK_TIMESTAMP();
361340

362341
UPDATE /* Notes-ETL */ dwh.dimension_countries
363342
SET modified = TRUE
364343
WHERE dimension_country_id = m_dimension_country_id;
365-
--RAISE NOTICE 'Flag 25: %', CLOCK_TIMESTAMP();
366344

367345
-- Resets the variables.
368346
m_dimension_country_id := null;
@@ -383,17 +361,14 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
383361
m_hashtag_name := null;
384362
m_hashtag_number := 0;
385363
m_all_hashtag_ids := ARRAY[]::INTEGER[]; -- Reset array
386-
--RAISE NOTICE 'Flag 26: %', CLOCK_TIMESTAMP();
387364

388365
m_count := m_count + 1;
389-
--RAISE NOTICE 'Flag 27: %', CLOCK_TIMESTAMP();
390366
IF (MOD(m_count, 10000) = 0) THEN
391367
RAISE NOTICE '%: % processed facts until %.', CLOCK_TIMESTAMP(), m_count,
392368
max_processed_timestamp;
393369
END IF;
394370

395371
END LOOP;
396-
--RAISE NOTICE 'Flag 28: %', CLOCK_TIMESTAMP();
397372

398373
CLOSE notes_on_day;
399374
END
@@ -434,7 +409,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
434409
-- All note_comments processed will have their corresponding notes
435410
-- Base case, when at least the first day of notes is processed.
436411
-- There are 231 note actions this day: 2013-04-24 (Epoch's OSM notes).
437-
--RAISE NOTICE '1Flag 1: %', CLOCK_TIMESTAMP();
438412
-- Check if initial load flag exists in properties FIRST
439413
-- This is more reliable than counting facts, as the flag indicates the state
440414
-- The flag can be 'true' (set during table creation) or 'completed' (set after initial load)
@@ -536,15 +510,12 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
536510

537511
RETURN; -- Exit procedure after initial load
538512
END IF;
539-
--RAISE NOTICE '1Flag 2: %', CLOCK_TIMESTAMP();
540513

541514
-- Incremental case: process only NEW comments since last ETL execution
542515
-- Get the most recent timestamp processed in DWH (not just the date)
543516
SELECT /* Notes-staging */ MAX(action_at)
544517
INTO max_note_on_dwh_timestamp
545518
FROM dwh.facts;
546-
--RAISE NOTICE 'Max timestamp processed in DWH: %', max_note_on_dwh_timestamp;
547-
--RAISE NOTICE '1Flag 3: %', CLOCK_TIMESTAMP();
548519

549520
-- If no facts exist, this should have been caught by initial load check above
550521
-- But handle it gracefully just in case
@@ -558,17 +529,13 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
558529
INTO max_note_action_date
559530
FROM public.note_comments
560531
WHERE note_id <= max_note_id_snapshot;
561-
--RAISE NOTICE 'Max date with comments in base tables: %', max_note_action_date;
562-
--RAISE NOTICE '1Flag 4: %', CLOCK_TIMESTAMP();
563532

564533
-- Get the date of the most recent note processed on the DWH
565534
SELECT /* Notes-staging */ MAX(date_id)
566535
INTO max_processed_date
567536
FROM dwh.facts f
568537
JOIN dwh.dimension_days d
569538
ON (f.action_dimension_id_date = d.dimension_day_id);
570-
--RAISE NOTICE 'Max date processed in DWH: %', max_processed_date;
571-
--RAISE NOTICE '1Flag 5: %', CLOCK_TIMESTAMP();
572539

573540
-- Validation: DWH should not have more recent data than base tables
574541
IF (max_note_action_date < max_processed_date) THEN
@@ -584,8 +551,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
584551
-- Process comments incrementally: only process dates that have NEW comments
585552
-- Start from the date after the last processed date, or the last processed date if it has new comments
586553
WHILE (max_processed_date <= max_note_action_date) LOOP
587-
--RAISE NOTICE '1Flag 5: %', CLOCK_TIMESTAMP();
588-
--RAISE NOTICE 'test % < %.', max_processed_date, max_note_action_date;
589554
-- Timestamp of the max processed note on DWH.
590555
-- It is on the same DATE of max_processed_date.
591556
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage
@@ -594,12 +559,9 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
594559
FROM dwh.facts
595560
WHERE action_at >= max_processed_date::TIMESTAMP
596561
AND action_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP;
597-
--RAISE NOTICE '1Flag 6: %', CLOCK_TIMESTAMP();
598-
--RAISE NOTICE 'max timestamp dwh %.', max_note_on_dwh_timestamp;
599562
IF (max_note_on_dwh_timestamp IS NULL) THEN
600563
max_note_on_dwh_timestamp := max_processed_date::TIMESTAMP;
601564
END IF;
602-
--RAISE NOTICE 'max note on dwh %', max_note_on_dwh_timestamp;
603565

604566
-- Gets the number of notes that have not being processed on the date being
605567
-- processed.
@@ -612,9 +574,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
612574
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
613575
AND created_at > max_note_on_dwh_timestamp
614576
AND note_id <= max_note_id_snapshot;
615-
--RAISE NOTICE 'count notes to process on date %: %.', max_processed_date,
616577
--qty_notes_on_date;
617-
--RAISE NOTICE '1Flag 7: %', CLOCK_TIMESTAMP();
618578

619579
-- If there are 0 notes to process, then skip to next date that has comments
620580
-- OPTIMIZATION: Instead of incrementing day by day, jump directly to next date with comments
@@ -663,7 +623,6 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
663623
use_equals := FALSE;
664624
END IF;
665625

666-
--RAISE NOTICE 'Skipped to next date with comments: %.', max_processed_date;
667626

668627
-- Gets the number of notes that have not being processed on the new date
669628
-- being processed.
@@ -676,29 +635,21 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
676635
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
677636
AND created_at > max_note_on_dwh_timestamp
678637
AND note_id <= max_note_id_snapshot;
679-
--RAISE NOTICE 'Notes to process for %: %.', max_processed_date,
680638
--qty_notes_on_date;
681-
--RAISE NOTICE '1Flag 8: %', CLOCK_TIMESTAMP();
682639

683640
-- Process notes for the new date with appropriate m_equals value
684641
CALL staging.process_notes_at_date(max_note_on_dwh_timestamp,
685642
qty_dwh_notes, use_equals);
686-
--RAISE NOTICE '1Flag 9: %', CLOCK_TIMESTAMP();
687643
ELSE
688644
-- There are comments not processed on the DHW for the currently processing
689645
-- day.
690-
--RAISE NOTICE 'Processing facts for %: %.', max_processed_date,
691646
--qty_notes_on_date;
692-
--RAISE NOTICE '1Flag 10: % - %', CLOCK_TIMESTAMP(), max_note_on_dwh_timestamp;
693647

694648
CALL staging.process_notes_at_date(max_note_on_dwh_timestamp,
695649
qty_dwh_notes, TRUE);
696-
--RAISE NOTICE '1Flag 11: %', CLOCK_TIMESTAMP();
697650
END IF;
698-
--RAISE NOTICE 'loop % - % - %.', max_processed_date,
699651
--max_note_on_dwh_timestamp, qty_notes_on_date;
700652
END LOOP;
701-
--RAISE NOTICE 'No facts to process (% !> %).', max_processed_date,
702653
--max_note_action_date;
703654

704655
-- Restore original statement_timeout

sql/dwh/Staging_33_initialFactsBaseObjects.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ CREATE OR REPLACE FUNCTION staging.update_days_to_resolution_${YEAR}()
6767
ON f.action_dimension_id_date = d.dimension_day_id
6868
WHERE f.id_note = NEW.id_note
6969
AND f.action_comment = 'reopened';
70-
--RAISE NOTICE 'Reopen date: %.', m_reopen_date;
7170
IF (m_reopen_date IS NOT NULL) THEN
7271
-- Days from the last reopen.
7372
m_days := m_close_date - m_reopen_date;
74-
--RAISE NOTICE 'Difference dates %-%: %.', m_close_date, m_reopen_date,
75-
-- m_days;
7673
UPDATE staging.facts_${YEAR}
7774
SET days_to_resolution_from_reopen = m_days
7875
WHERE fact_id = NEW.fact_id;
@@ -127,7 +124,6 @@ BEGIN
127124
RAISE NOTICE 'Min and max dates % - %.', m_day_year, m_max_day_year;
128125
WHILE (m_day_year <= m_max_day_year) LOOP
129126
m_dummy := dwh.get_date_id(m_day_year);
130-
--RAISE NOTICE 'Processed date %.', m_day_year;
131127
SELECT /* Notes-staging */ m_day_year + 1
132128
INTO m_day_year;
133129
END LOOP;

0 commit comments

Comments
 (0)