Skip to content

Commit

Permalink
Merge pull request #190 from kitagry/support-table-without-backtick
Browse files Browse the repository at this point in the history
feat: support table without backtick
  • Loading branch information
kitagry authored Dec 15, 2024
2 parents 891b812 + 536e003 commit 2a2e578
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
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

0 comments on commit 2a2e578

Please sign in to comment.