Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve normalizer cpu usage #43

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading