Skip to content

Commit

Permalink
Support for DBCC CHECKIDENT (babelfish-for-postgresql#1646)
Browse files Browse the repository at this point in the history
This update introduces support for the `DBCC CHECKIDENT` command within Babelfish, allowing users to reset the current value of an identity column directly through T-SQL. Previously, this operation required the use of the `Setval()` command from the PostgreSQL endpoint. Notably, this marks the initial implementation of a DBCC command in Babelfish, encompassing the introduction of syntax for DBCC commands. The design has been built with scalability in mind, making it easier to incorporate additional DBCC commands in the future.

Issues Resolved
BABEL-3201

Signed-off-by: Sandeep Kumawat <[email protected]>
  • Loading branch information
aadityavermaa authored Oct 23, 2023
1 parent 3d46cd6 commit 2ad7351
Show file tree
Hide file tree
Showing 22 changed files with 2,470 additions and 10 deletions.
29 changes: 29 additions & 0 deletions contrib/babelfishpg_tsql/antlr/TSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,22 @@ CHANGETABLE: C H A N G E T A B L E;
CHANGE_RETENTION: C H A N G E UNDERLINE R E T E N T I O N;
CHANGE_TRACKING: C H A N G E UNDERLINE T R A C K I N G;
CHECK: C H E C K;
CHECKDB: C H E C K D B;
CHECKFILEGROUP: C H E C K F I L E G R O U P;
CHECKIDENT: C H E C K I D E N T;
CHECKPOINT: C H E C K P O I N T;
CHECKSUM: C H E C K S U M;
CHECKSUM_AGG: C H E C K S U M UNDERLINE A G G;
CHECKTABLE: C H E C K T A B L E;
CHECK_EXPIRATION: C H E C K UNDERLINE E X P I R A T I O N;
CHECK_POLICY: C H E C K UNDERLINE P O L I C Y;
CLASSIFIER: C L A S S I F I E R;
CLASSIFIER_FUNCTION: C L A S S I F I E R UNDERLINE F U N C T I O N;
CLEANUP: C L E A N U P;
CLEANTABLE: C L E A N T A B L E;
CLEANUP_POLICY: C L E A N U P UNDERLINE P O L I C Y;
CLEAR: C L E A R;
CLONEDATABASE: C L O N E D A T A B A S E;
CLOSE: C L O S E;
CLUSTER: C L U S T E R;
CLUSTERED: C L U S T E R E D;
Expand Down Expand Up @@ -248,6 +254,7 @@ DATE_CORRELATION_OPTIMIZATION: D A T E UNDERLINE C O R R E L
DAY: D A Y;
DAYS: D A Y S;
DBCC: D B C C;
DBREINDEX: D B R E I N D E X;
DB_CHAINING: D B UNDERLINE C H A I N I N G;
DB_FAILOVER: D B UNDERLINE F A I L O V E R;
DDL: D D L;
Expand Down Expand Up @@ -298,6 +305,7 @@ DOLLAR_ROWGUID: DOLLAR R O W G U I D;
DOLLAR_TO_ID: DOLLAR T O UNDERLINE I D; // graph
DOUBLE: D O U B L E;
DROP: D R O P;
DROPCLEANBUFFERS: D R O P C L E A N B U F F E R S;
DTC_SUPPORT: D T C UNDERLINE S U P P O R T;
DUMP: D U M P;
DYNAMIC: D Y N A M I C;
Expand Down Expand Up @@ -387,6 +395,9 @@ FORMAT: F O R M A T;
FORWARD_ONLY: F O R W A R D UNDERLINE O N L Y;
FORMAT_OPTIONS: F O R M A T UNDERLINE O P T I O N S;
FORMAT_TYPE: F O R M A T UNDERLINE T Y P E;
FREEPROCCACHE: F R E E P R O C C A C H E;
FREESESSIONCACHE: F R E E S E S S I O N C A C H E;
FREESYSTEMCACHE: F R E E S Y S T E M C A C H E;
FREETEXT: F R E E T E X T;
FREETEXTTABLE: F R E E T E X T T A B L E;
FROM: F R O M;
Expand Down Expand Up @@ -424,6 +435,7 @@ HASHED: H A S H E D;
HAVING: H A V I N G;
HEALTHCHECKTIMEOUT: H E A L T H C H E C K T I M E O U T;
HEALTH_CHECK_TIMEOUT: H E A L T H UNDERLINE C H E C K UNDERLINE T I M E O U T;
HELP: H E L P;
HIDDEN_RENAMED: H I D D E N;
HIGH: H I G H;
HINT: H I N T;
Expand All @@ -433,6 +445,7 @@ HOLDLOCK: H O L D L O C K;
HONOR_BROKER_PRIORITY: H O N O R UNDERLINE B R O K E R UNDERLINE P R I O R I T Y;
HOUR: H O U R;
HOURS: H O U R S;
INDEXDEFRAG: I N D E X D E F R A G;
IDENTITY: I D E N T I T Y;
IDENTITYCOL: I D E N T I T Y C O L;
IDENTITY_INSERT: I D E N T I T Y UNDERLINE I N S E R T;
Expand All @@ -455,6 +468,7 @@ INIT: I N I T;
INITIATOR: I N I T I A T O R;
INNER: I N N E R;
INPUT: I N P U T;
INPUTBUFFER: I N P U T B U F F E R;
INSENSITIVE: I N S E N S I T I V E;
INSERT: I N S E R T;
INSERTED: I N S E R T E D;
Expand Down Expand Up @@ -603,6 +617,7 @@ NONE: N O N E;
NON_TRANSACTED_ACCESS: N O N UNDERLINE T R A N S A C T E D UNDERLINE A C C E S S;
NORECOMPUTE: N O R E C O M P U T E;
NORECOVERY: N O R E C O V E R Y;
NORESEED: N O R E S E E D;
NOREWIND: N O R E W I N D;
NOSKIP: N O S K I P;
NOT: N O T;
Expand Down Expand Up @@ -643,6 +658,7 @@ OPENDATASOURCE: O P E N D A T A S O U R C E;
OPENJSON: O P E N J S O N;
OPENQUERY: O P E N Q U E R Y;
OPENROWSET: O P E N R O W S E T;
OPENTRAN: O P E N T R A N;
OPENXML: O P E N X M L;
OPEN_EXISTING: O P E N UNDERLINE E X I S T I N G;
OPERATIONS: O P E R A T I O N S;
Expand All @@ -655,6 +671,7 @@ ORDER: O R D E R;
OUT: O U T;
OUTER: O U T E R;
OUTPUT: O U T P U T;
OUTPUTBUFFER: O U T P U T B U F F E R;
OVER: O V E R;
OVERRIDE: O V E R R I D E;
OWNER: O W N E R;
Expand Down Expand Up @@ -711,6 +728,7 @@ PRIVATE: P R I V A T E;
PRIVATE_KEY: P R I V A T E UNDERLINE K E Y;
PRIVILEGES: P R I V I L E G E S;
PROC: P R O C;
PROCCACHE: P R O C C A C H E;
PROCEDURE: P R O C E D U R E;
PROCEDURE_CACHE: P R O C E D U R E UNDERLINE C A C H E;
PROCEDURE_NAME: P R O C E D U R E UNDERLINE N A M E;
Expand Down Expand Up @@ -781,6 +799,7 @@ REQUEST_MEMORY_GRANT_TIMEOUT_SEC: R E Q U E S T UNDERLINE M E M
REQUIRED: R E Q U I R E D;
REQUIRED_SYNCHRONIZED_SECONDARIES_TO_COMMIT: R E Q U I R E D UNDERLINE S Y N C H R O N I Z E D UNDERLINE S E C O N D A R I E S UNDERLINE T O UNDERLINE C O M M I T;
RESAMPLE: R E S A M P L E;
RESEED: R E S E E D;
RESERVE_DISK_SPACE: R E S E R V E UNDERLINE D I S K UNDERLINE S P A C E;
RESET: R E S E T;
RESOURCE: R E S O U R C E;
Expand Down Expand Up @@ -871,10 +890,14 @@ SETS: S E T S;
SETTINGS: S E T T I N G S;
SETUSER: S E T U S E R;
SHARE: S H A R E;
SHOWCONTIG: S H O W C O N T I G;
SHOWPLAN: S H O W P L A N;
SHOWPLAN_ALL: S H O W P L A N UNDERLINE A L L;
SHOWPLAN_TEXT: S H O W P L A N UNDERLINE T E X T;
SHOWPLAN_XML: S H O W P L A N UNDERLINE X M L;
SHOW_STATISTICS: S H O W UNDERLINE S T A T I S T I C S;
SHRINKDATABASE: S H R I N K D A T A B A S E;
SHRINKFILE: S H R I N K F I L E;
SHRINKLOG: S H R I N K L O G;
SHUTDOWN: S H U T D O W N;
SID: S I D;
Expand All @@ -899,6 +922,7 @@ SQL: S Q L;
SQLDUMPERFLAGS: S Q L D U M P E R F L A G S;
SQLDUMPERPATH: S Q L D U M P E R P A T H;
SQLDUMPERTIMEOUT: S Q L D U M P E R T I M E O U T S;
SQLPERF: S Q L P E R F;
STALE_CAPTURE_POLICY_THRESHOLD: S T A L E UNDERLINE C A P T U R E UNDERLINE P O L I C Y UNDERLINE T H R E S H O L D;
STALE_QUERY_THRESHOLD_DAYS: S T A L E UNDERLINE Q U E R Y UNDERLINE T H R E S H O L D UNDERLINE D A Y S;
STANDBY: S T A N D B Y;
Expand Down Expand Up @@ -970,7 +994,10 @@ TOSTRING: T O S T R I N G;
TOTAL_COMPILE_CPU_TIME_MS: T O T A L UNDERLINE C O M P I L E UNDERLINE C P U UNDERLINE T I M E UNDERLINE M S;
TOTAL_EXECUTION_CPU_TIME_MS: T O T A L UNDERLINE E X E C U T I O N UNDERLINE C P U UNDERLINE T I M E UNDERLINE M S;
TRACE: T R A C E;
TRACEOFF: T R A C E O F F;
TRACEON: T R A C E O N;
TRACKING: T R A C K I N G;
TRACESTATUS: T R A C E S T A T U S;
TRACK_CAUSALITY: T R A C K UNDERLINE C A U S A L I T Y;
TRACK_COLUMNS_UPDATED: T R A C K UNDERLINE C O L U M N S UNDERLINE U P D A T E D;
TRAN: T R A N;
Expand Down Expand Up @@ -1009,13 +1036,15 @@ UNPIVOT: U N P I V O T;
UNSAFE: U N S A F E;
UOW: U O W;
UPDATE: U P D A T E;
UPDATEUSAGE: U P D A T E U S A G E;
UPDATETEXT: U P D A T E T E X T;
UPDLOCK: U P D L O C K;
URL: U R L;
USE: U S E;
USE_TYPE_DEFAULT: U S E UNDERLINE T Y P E UNDERLINE D E F A U L T;
USED: U S E D;
USER: U S E R;
USEROPTIONS: U S E R O P T I O N S;
USING: U S I N G;
VALIDATION: V A L I D A T I O N;
VALID_XML: V A L I D UNDERLINE X M L;
Expand Down
72 changes: 66 additions & 6 deletions contrib/babelfishpg_tsql/antlr/TSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -3094,19 +3094,53 @@ shutdown_statement
;

dbcc_statement
: DBCC name=dbcc_command ( LR_BRACKET expression_list RR_BRACKET )? (WITH dbcc_options)? SEMI?
: DBCC CHECKIDENT ( LR_BRACKET table_name_string ( (COMMA NORESEED) | (COMMA RESEED (COMMA MINUS? new_value=(DECIMAL | FLOAT))?) )? RR_BRACKET ) (WITH dbcc_options)? SEMI?
| DBCC name=dbcc_command ( LR_BRACKET expression_list RR_BRACKET )? (WITH dbcc_options)? SEMI?
//These are dbcc commands with strange syntax that doesn't fit the regular dbcc syntax
| DBCC SHRINKLOG ( LR_BRACKET SIZE EQUAL (constant_expression| id | DEFAULT) (KB | MB | GB | TB)? RR_BRACKET )? (WITH dbcc_options)? SEMI?
;
;

dbcc_command
: ID | keyword
: ID
| CHECKDB
| CHECKFILEGROUP
| CHECKTABLE
| CLEANTABLE
| CLONEDATABASE
| DBREINDEX
| DROPCLEANBUFFERS
| FREEPROCCACHE
| FREESESSIONCACHE
| FREESYSTEMCACHE
| HELP
| INDEXDEFRAG
| INPUTBUFFER
| OPENTRAN
| OUTPUTBUFFER
| PROCCACHE
| SHOW_STATISTICS
| SHOWCONTIG
| SHRINKDATABASE
| SHRINKFILE
| SQLPERF
| TRACEOFF
| TRACEON
| TRACESTATUS
| UPDATEUSAGE
| USEROPTIONS
;

dbcc_options
: ID (COMMA ID)?
;


table_name_string
: table = id
| char_string
;


execute_as_clause
: (EXECUTE|EXEC) AS (CALLER | SELF | OWNER | char_string)
;
Expand Down Expand Up @@ -4271,15 +4305,20 @@ keyword
| CHANGETABLE
| CHANGE_RETENTION
| CHANGE_TRACKING
| CHECKDB
| CHECKFILEGROUP
| CHECKSUM
| CHECKSUM_AGG
| CHECKTABLE
| CHECK_EXPIRATION
| CHECK_POLICY
| CLASSIFIER
| CLASSIFIER_FUNCTION
| CLEANTABLE
| CLEANUP
| CLEANUP_POLICY
| CLEAR
| CLEAR
| CLONEDATABASE
| CLUSTER
| COALESCE
| COLLECTION
Expand Down Expand Up @@ -4343,6 +4382,7 @@ keyword
| DATE_FORMAT
| DAY
| DAYS
| DBREINDEX
| DB_CHAINING
| DB_FAILOVER
| DDL
Expand Down Expand Up @@ -4376,6 +4416,7 @@ keyword
| DISTRIBUTED_AGG
| DISTRIBUTION
| DOCUMENT
| DROPCLEANBUFFERS
| DTC_SUPPORT
| DYNAMIC
| EDGE
Expand Down Expand Up @@ -4449,6 +4490,9 @@ keyword
| FORMAT_OPTIONS
| FORMAT_TYPE
| FORWARD_ONLY
| FREEPROCCACHE
| FREESESSIONCACHE
| FREESYSTEMCACHE
| FULLSCAN
| FULLTEXT
| GB
Expand Down Expand Up @@ -4477,6 +4521,7 @@ keyword
| HASHED
| HEALTHCHECKTIMEOUT
| HEALTH_CHECK_TIMEOUT
| HELP
| HIDDEN_RENAMED
| HIGH
| HINT
Expand All @@ -4499,10 +4544,12 @@ keyword
| INCLUDE_NULL_VALUES
| INCREMENT
| INCREMENTAL
| INDEXDEFRAG
| INFINITE
| INIT
| INITIATOR
| INPUT
| INPUTBUFFER
| INSENSITIVE
| INSERTED
| INSTEAD
Expand Down Expand Up @@ -4668,13 +4715,15 @@ keyword
| ONLY
| ON_FAILURE
| OPENJSON
| OPENTRAN
| OPEN_EXISTING
| OPERATIONS
| OPERATION_MODE
| OPTIMISTIC
| OPTIMIZE
| OUT
| OUTPUT
| OUTPUTBUFFER
| OVERRIDE
| OWNER
| OWNERSHIP
Expand Down Expand Up @@ -4722,6 +4771,7 @@ keyword
| PRIVATE
| PRIVATE_KEY
| PRIVILEGES
| PROCCACHE
| PROCEDURE_CACHE
| PROCEDURE_NAME
| PROCESS
Expand Down Expand Up @@ -4850,10 +4900,14 @@ keyword
| SETS
| SETTINGS
| SHARE
| SHOWCONTIG
| SHOWPLAN
| SHOWPLAN_ALL
| SHOWPLAN_TEXT
| SHOWPLAN_XML
| SHOW_STATISTICS
| SHRINKDATABASE
| SHRINKFILE
| SHRINKLOG
| SID
| SIGNATURE
Expand All @@ -4876,6 +4930,7 @@ keyword
| SQLDUMPERFLAGS
| SQLDUMPERPATH
| SQLDUMPERTIMEOUT
| SQLPERF
| STALE_CAPTURE_POLICY_THRESHOLD
| STALE_QUERY_THRESHOLD_DAYS
| STANDBY
Expand Down Expand Up @@ -4937,6 +4992,9 @@ keyword
| TOTAL_COMPILE_CPU_TIME_MS
| TOTAL_EXECUTION_CPU_TIME_MS
| TRACE
| TRACEOFF
| TRACEON
| TRACESTATUS
| TRACKING
| TRACK_CAUSALITY
| TRACK_COLUMNS_UPDATED
Expand Down Expand Up @@ -4966,8 +5024,10 @@ keyword
| UNMASK
| UNSAFE
| UOW
| UPDATEUSAGE
| URL
| USED
| USEROPTIONS
| USE_TYPE_DEFAULT
| USING
| VALIDATION
Expand Down Expand Up @@ -5103,8 +5163,8 @@ id
| DOUBLE_QUOTE_ID // this is a double-quoted identifier in case of SET QUOTED_IDENTIFIER ON
| SQUARE_BRACKET_ID
| keyword
| DOLLAR_IDENTITY
| DOLLAR_ROWGUID
| DOLLAR_IDENTITY
| DOLLAR_ROWGUID
| id colon_colon id
;

Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ stmt_default_act(Walker_context *ctx, PLtsql_stmt *stmt)
case PLTSQL_STMT_GRANTDB:
case PLTSQL_STMT_GRANTSCHEMA:
case PLTSQL_STMT_INSERT_BULK:
case PLTSQL_STMT_DBCC:
case PLTSQL_STMT_SET_EXPLAIN_MODE:
/* TSQL-only executable node */
case PLTSQL_STMT_SAVE_CTX:
Expand Down
3 changes: 3 additions & 0 deletions contrib/babelfishpg_tsql/src/iterative_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,9 @@ dispatch_stmt(PLtsql_execstate *estate, PLtsql_stmt *stmt)
}
exec_stmt_restore_ctx_partial(estate, (PLtsql_stmt_restore_ctx_partial *) stmt);
break;
case PLTSQL_STMT_DBCC:
exec_stmt_dbcc(estate, (PLtsql_stmt_dbcc *) stmt);
break;
case PLTSQL_STMT_KILL:
exec_stmt_kill(estate, (PLtsql_stmt_kill *) stmt);
break;
Expand Down
Loading

0 comments on commit 2ad7351

Please sign in to comment.