Skip to content

Commit

Permalink
improve normalizer cpu usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zhengda committed Dec 16, 2024
1 parent 96160b1 commit 33b632d
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 153 deletions.
Binary file added cpu.prof
Binary file not shown.
16 changes: 8 additions & 8 deletions normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func (n *Normalizer) collectMetadata(token *Token, lastToken *Token, statementMe
token.Value = tokenVal
}
}
if n.config.CollectCommands && isCommand(strings.ToUpper(tokenVal)) {
if n.config.CollectCommands && isCommand(tokenVal) {
// Collect commands
statementMetadata.Commands = append(statementMetadata.Commands, strings.ToUpper(tokenVal))
} else if strings.ToUpper(lastToken.Value) == "WITH" && token.Type == IDENT {
statementMetadata.Commands = append(statementMetadata.Commands, tokenVal)
} else if isWith(lastToken.Value) && token.Type == IDENT {
// Collect CTEs so we can skip them later in table collection
ctes[tokenVal] = true
} else if n.config.CollectTables && isTableIndicator(strings.ToUpper(lastToken.Value)) && !isSQLKeyword(token) {
} else if n.config.CollectTables && isTableIndicator(lastToken.Value) && !isSQLKeyword(tokenVal) {
// Collect table names the token is not a CTE
if _, ok := ctes[tokenVal]; !ok {
statementMetadata.Tables = append(statementMetadata.Tables, tokenVal)
Expand Down Expand Up @@ -212,16 +212,16 @@ func (n *Normalizer) normalizeSQL(token *Token, lastToken *Token, normalizedSQLB

if !n.config.KeepSQLAlias {
// discard SQL alias
if strings.ToUpper(token.Value) == "AS" {
if isAs(token.Value) {
// if current token is AS, then continue to next token
// because without seeing the next token, we cannot
// determine if the current token is an alias or not
*lastToken = *token
return
}

if strings.ToUpper(lastToken.Value) == "AS" {
if token.Type == IDENT && !isSQLKeyword(token) {
if isAs(lastToken.Value) {
if token.Type == IDENT && !isSQLKeyword(token.Value) {
// if the last token is AS and the current token is IDENT,
// then the current token is an alias, so we discard it
*lastToken = *token
Expand Down Expand Up @@ -252,7 +252,7 @@ func (n *Normalizer) normalizeSQL(token *Token, lastToken *Token, normalizedSQLB
}

func (n *Normalizer) writeToken(token *Token, normalizedSQLBuilder *strings.Builder) {
if n.config.UppercaseKeywords && isSQLKeyword(token) {
if token.Type == IDENT && n.config.UppercaseKeywords && isSQLKeyword(token.Value) {
normalizedSQLBuilder.WriteString(strings.ToUpper(token.Value))
} else {
normalizedSQLBuilder.WriteString(token.Value)
Expand Down
2 changes: 1 addition & 1 deletion normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ multiline comment */
statementMetadata: StatementMetadata{
Tables: []string{"users", "test_users", "user?"},
Comments: []string{},
Commands: []string{"CREATE", "BEGIN", "SELECT", "UPDATE", "DELETE"},
Commands: []string{"CREATE", "BEGIN", "SELECT", "Update", "Delete"},
Procedures: []string{},
Size: 49,
},
Expand Down
6 changes: 3 additions & 3 deletions obfuscate_and_normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ multiline comment */
statementMetadata: StatementMetadata{
Tables: []string{"v$sql"},
Comments: []string{},
Commands: []string{"SELECT"},
Commands: []string{"select"},
Procedures: []string{},
Size: 11,
},
Expand Down Expand Up @@ -163,7 +163,7 @@ multiline comment */
statementMetadata: StatementMetadata{
Tables: []string{},
Comments: []string{},
Commands: []string{"BEGIN", "EXECUTE"},
Commands: []string{"begin", "execute"},
Procedures: []string{},
Size: 12,
},
Expand Down Expand Up @@ -234,7 +234,7 @@ multiline comment */
statementMetadata: StatementMetadata{
Tables: []string{`public.users`},
Comments: []string{},
Commands: []string{"SELECT"},
Commands: []string{"select"},
Procedures: []string{},
Size: 18,
},
Expand Down
Loading

0 comments on commit 33b632d

Please sign in to comment.