Skip to content

Commit

Permalink
improve normalizer cpu usage (#43)
Browse files Browse the repository at this point in the history
* improve normalizer cpu usage

* remove cpu.prof

* improve format

* quick check for ascii letter

* upper case collected commands
  • Loading branch information
lu-zhengda authored Dec 17, 2024
1 parent dc83dd8 commit a418b44
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 155 deletions.
14 changes: 7 additions & 7 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 {
} 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
Loading

0 comments on commit a418b44

Please sign in to comment.