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

feat: support table without backtick #190

Merged
merged 1 commit into from
Dec 15, 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
1 change: 1 addition & 0 deletions langserver/internal/source/file/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (a *Analyzer) langOpt() (*zetasql.LanguageOptions, error) {
langOpt.EnableLanguageFeature(zetasql.FeatureV13Qualify)
langOpt.EnableLanguageFeature(zetasql.FeatureV13ScriptLabel)
langOpt.EnableLanguageFeature(zetasql.FeatureAnalyticFunctions)
langOpt.EnableLanguageFeature(zetasql.FeatureV13AllowDashesInTableName)
langOpt.SetSupportsAllStatementKinds()
langOpt.EnableAllReservableKeywords(true)
err := langOpt.EnableReservableKeyword("QUALIFY", true)
Expand Down
37 changes: 25 additions & 12 deletions langserver/internal/source/file/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ func (c *Catalog) FindTable(path []string) (types.Table, error) {
c.mu.Lock()
defer c.mu.Unlock()

table, err := c.catalog.FindTable(path)
projectID, datasetID, tableID, err := c.pathToProjectTable(path)
if err != nil {
return nil, err
}

formattedPath := []string{fmt.Sprintf("%s.%s.%s", projectID, datasetID, tableID)}

table, err := c.catalog.FindTable(formattedPath)
if err == nil {
return table, nil
}
Expand All @@ -66,25 +73,21 @@ func (c *Catalog) FindTable(path []string) (types.Table, error) {
errs = append(errs, fmt.Errorf("failed to add table: %w", err))
return nil, errors.Join(errs...)
}
return c.catalog.FindTable(path)
return c.catalog.FindTable(formattedPath)
}

func (c *Catalog) addTable(path []string) error {
tableSep := strings.Split(strings.Join(path, "."), ".")
var metadata *bq.TableMetadata
var err error
if len(tableSep) == 3 {
metadata, err = c.bqClient.GetTableMetadata(context.Background(), tableSep[0], tableSep[1], tableSep[2])
} else if len(tableSep) == 2 {
metadata, err = c.bqClient.GetTableMetadata(context.Background(), c.bqClient.GetDefaultProject(), tableSep[0], tableSep[1])
} else {
return fmt.Errorf("unknown table: %s", strings.Join(path, "."))
projectID, datasetID, tableID, err := c.pathToProjectTable(path)
if err != nil {
return err
}

metadata, err := c.bqClient.GetTableMetadata(context.Background(), projectID, datasetID, tableID)
if err != nil {
return fmt.Errorf("failed to get schema: %w", err)
}

tableName := strings.Join(path, ".")
tableName := fmt.Sprintf("%s.%s.%s", projectID, datasetID, tableID)

schema := metadata.Schema
columns := make([]types.Column, len(schema))
Expand All @@ -110,6 +113,16 @@ func (c *Catalog) addTable(path []string) error {
return nil
}

func (c *Catalog) pathToProjectTable(path []string) (projectID, datasetID, tableID string, err error) {
tableSep := strings.Split(strings.Join(path, "."), ".")
if len(tableSep) == 3 {
return tableSep[0], tableSep[1], tableSep[2], nil
} else if len(tableSep) == 2 {
return c.bqClient.GetDefaultProject(), tableSep[0], tableSep[1], nil
}
return "", "", "", fmt.Errorf(`unknown table "%s"`, strings.Join(tableSep, "."))
}

func bigqueryTypeToZetaSQLType(typ bq.FieldType, isRepeated bool, schema bq.Schema) (types.Type, error) {
result, err := literalBigqueryTypeToZetaSQLType(typ, schema)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions langserver/internal/source/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,20 @@ func TestProject_ParseFile(t *testing.T) {
},
expectedErrs: []file.Error{},
},
"parse without backtick": {
file: "SELECT * FROM project.dataset.table",
bqTableMetadataMap: map[string]*bq.TableMetadata{
"project.dataset.table": {
Schema: bq.Schema{
{
Name: "id",
Type: bq.IntegerFieldType,
},
},
},
},
expectedErrs: []file.Error{},
},
}

for n, tt := range tests {
Expand Down
Loading