8000 Care INFORMATION_SCHEMA by goccy · Pull Request #118 · goccy/go-zetasqlite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Care INFORMATION_SCHEMA #118

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

Merged
merged 2 commits into from
Jun 20, 2023
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
4 changes: 1 addition & 3 deletions internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func (a *Analyzer) SetNamePath(path []string) error {
}

func (a *Analyzer) SetMaxNamePath(num int) {
if num > 0 {
a.namePath.maxNum = num
}
a.namePath.setMaxNum(num)
}

func (a *Analyzer) MaxNamePath() int {
Expand Down
47 changes: 42 additions & 5 deletions internal/name_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ type NamePath struct {
maxNum int
}

func (p *NamePath) isInformationSchema(path []string) bool {
if len(path) == 0 {
return false
}
// If INFORMATION_SCHEMA is at the end of path, ignore it.
for _, subPath := range path[:len(path)-1] {
if strings.ToLower(subPath) == "information_schema" {
return true
}
}
return false
}

func (p *NamePath) setMaxNum(num int) {
if num > 0 {
p.maxNum = num
}
}

func (p *NamePath) getMaxNum(path []string) int {
if p.maxNum == 0 {
return 0
}
// INFORMATION_SCHEMA is a special View.
// This means that one path is added to the rules for specifying a normal Table/Function.
if p.isInformationSchema(path) {
return p.maxNum + 1
}
return p.maxNum
}

func (p *NamePath) normalizePath(path []string) []string {
ret := []string{}
for _, p := range path {
Expand All @@ -21,7 +52,8 @@ func (p *NamePath) normalizePath(path []string) []string {

func (p *NamePath) mergePath(path []string) []string {
path = p.normalizePath(path)
if p.maxNum > 0 && len(path) == p.maxNum {
maxNum := p.getMaxNum(path)
if maxNum > 0 && len(path) == maxNum {
return path
}
if len(path) == 0 {
Expand All @@ -32,6 +64,9 @@ func (p *NamePath) mergePath(path []string) []string {
if path[0] == basePath {
break
}
if maxNum > 0 && len(merged)+len(path) >= maxNum {
break
}
merged = append(merged, basePath)
}
return append(merged, path...)
Expand All @@ -47,8 +82,9 @@ func formatPath(path []string) string {

func (p *NamePath) setPath(path []string) error {
normalizedPath := p.normalizePath(path)
if p.maxNum > 0 && len(normalizedPath) > p.maxNum {
return fmt.Errorf("specified too many name paths %v(%d). max name path is %d", path, len(normalizedPath), p.maxNum)
maxNum := p.getMaxNum(path)
if maxNum > 0 && len(normalizedPath) > maxNum {
return fmt.Errorf("specified too many name paths %v(%d). max name path is %d", path, len(normalizedPath), maxNum)
}
p.path = normalizedPath
return nil
Expand All @@ -57,12 +93,13 @@ func (p *NamePath) setPath(path []string) error {
func (p *NamePath) addPath(path string) error {
normalizedPath := p.normalizePath([]string{path})
totalPath := len(p.path) + len(normalizedPath)
if p.maxNum > 0 && totalPath > p.maxNum {
maxNum := p.getMaxNum(normalizedPath)
if maxNum > 0 && totalPath > maxNum {
return fmt.Errorf(
"specified too many name paths %v(%d). max name path is %d",
append(p.path, normalizedPath...),
totalPath,
p.maxNum,
maxNum,
)
}
p.path = append(p.path, normalizedPath...)
Expand Down
39 changes: 39 additions & 0 deletions internal/name_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestNamePath(t *testing.T) {
namePath := new(NamePath)
if err := namePath.setPath([]string{"project1", "dataset1"}); err != nil {
t.Fatal(err)
}
namePath.setMaxNum(3)
if diff := cmp.Diff(namePath.mergePath([]string{"project1", "dataset1", "table1"}), []string{"project1", "dataset1", "table1"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"dataset1", "table1"}), []string{"project1", "dataset1", "table1"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"project2", "dataset2", "table1"}), []string{"project2", "dataset2", "table1"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"dataset2", "table1"}), []string{"project1", "dataset2", "table1"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"table1"}), []string{"project1", "dataset1", "table1"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"project2", "dataset2", "INFORMATION_SCHEMA", "TABLES"}), []string{"project2", "dataset2", "INFORMATION_SCHEMA", "TABLES"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"dataset2", "INFORMATION_SCHEMA", "TABLES"}), []string{"project1", "dataset2", "INFORMATION_SCHEMA", "TABLES"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
if diff := cmp.Diff(namePath.mergePath([]string{"INFORMATION_SCHEMA", "TABLES"}), []string{"project1", "dataset1", "INFORMATION_SCHEMA", "TABLES"}); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
0