Skip to content

Commit

Permalink
fix: use schema object identifier in external tables (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Oct 12, 2023
1 parent 50f2935 commit f5d4aeb
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 173 deletions.
132 changes: 67 additions & 65 deletions pkg/sdk/external_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type ExternalTable struct {
OwnerRoleType string
}

func (v *ExternalTable) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
func (v *ExternalTable) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)
}

func (v *ExternalTable) ObjectType() ObjectType {
Expand All @@ -58,12 +58,12 @@ func (v *ExternalTable) ObjectType() ObjectType {

// CreateExternalTableOptions based on https://docs.snowflake.com/en/sql-reference/sql/create-external-table
type CreateExternalTableOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
CloudProviderParams *CloudProviderParams
PartitionBy []string `ddl:"keyword,parentheses" sql:"PARTITION BY"`
Location string `ddl:"parameter" sql:"LOCATION"`
Expand Down Expand Up @@ -192,12 +192,12 @@ var (

// CreateWithManualPartitioningExternalTableOptions based on https://docs.snowflake.com/en/sql-reference/sql/create-external-table
type CreateWithManualPartitioningExternalTableOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
CloudProviderParams *CloudProviderParams
PartitionBy []string `ddl:"keyword,parentheses" sql:"PARTITION BY"`
Location string `ddl:"parameter" sql:"LOCATION"`
Expand All @@ -211,12 +211,12 @@ type CreateWithManualPartitioningExternalTableOptions struct {

// CreateDeltaLakeExternalTableOptions based on https://docs.snowflake.com/en/sql-reference/sql/create-external-table
type CreateDeltaLakeExternalTableOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
Columns []ExternalTableColumn `ddl:"list,parentheses"`
CloudProviderParams *CloudProviderParams
PartitionBy []string `ddl:"keyword,parentheses" sql:"PARTITION BY"`
Location string `ddl:"parameter" sql:"LOCATION"`
Expand All @@ -233,12 +233,12 @@ type CreateDeltaLakeExternalTableOptions struct {

// CreateExternalTableUsingTemplateOptions based on https://docs.snowflake.com/en/sql-reference/sql/create-external-table#variant-syntax
type CreateExternalTableUsingTemplateOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
name AccountObjectIdentifier `ddl:"identifier"`
CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"`
Query []string `ddl:"parameter,no_equals,parentheses" sql:"USING TEMPLATE"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
externalTable bool `ddl:"static" sql:"EXTERNAL TABLE"`
name SchemaObjectIdentifier `ddl:"identifier"`
CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"`
Query []string `ddl:"parameter,no_equals,parentheses" sql:"USING TEMPLATE"`
CloudProviderParams *CloudProviderParams
PartitionBy []string `ddl:"keyword,parentheses" sql:"PARTITION BY"`
Location string `ddl:"parameter" sql:"LOCATION"`
Expand All @@ -254,9 +254,9 @@ type CreateExternalTableUsingTemplateOptions struct {

// AlterExternalTableOptions based on https://docs.snowflake.com/en/sql-reference/sql/alter-external-table
type AlterExternalTableOptions struct {
alterExternalTable bool `ddl:"static" sql:"ALTER EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
alterExternalTable bool `ddl:"static" sql:"ALTER EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
// One of
Refresh *RefreshExternalTable `ddl:"keyword" sql:"REFRESH"`
AddFiles []ExternalTableFile `ddl:"keyword,no_quotes,parentheses" sql:"ADD FILES"`
Expand All @@ -276,12 +276,12 @@ type ExternalTableFile struct {

// AlterExternalTablePartitionOptions based on https://docs.snowflake.com/en/sql-reference/sql/alter-external-table
type AlterExternalTablePartitionOptions struct {
alterExternalTable bool `ddl:"static" sql:"ALTER EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
AddPartitions []Partition `ddl:"keyword,parentheses" sql:"ADD PARTITION"`
DropPartition *bool `ddl:"keyword" sql:"DROP PARTITION"`
Location string `ddl:"parameter,no_equals,single_quotes" sql:"LOCATION"`
alterExternalTable bool `ddl:"static" sql:"ALTER EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
AddPartitions []Partition `ddl:"keyword,parentheses" sql:"ADD PARTITION"`
DropPartition *bool `ddl:"keyword" sql:"DROP PARTITION"`
Location string `ddl:"parameter,no_equals,single_quotes" sql:"LOCATION"`
}

type Partition struct {
Expand All @@ -291,9 +291,9 @@ type Partition struct {

// DropExternalTableOptions based on https://docs.snowflake.com/en/sql-reference/sql/drop-external-table
type DropExternalTableOptions struct {
dropExternalTable bool `ddl:"static" sql:"DROP EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
dropExternalTable bool `ddl:"static" sql:"DROP EXTERNAL TABLE"`
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"`
name SchemaObjectIdentifier `ddl:"identifier"`
DropOption *ExternalTableDropOption
}

Expand All @@ -314,39 +314,38 @@ type ShowExternalTableOptions struct {
}

type externalTableRow struct {
CreatedOn time.Time
Name string
DatabaseName string
SchemaName string
Invalid bool
InvalidReason sql.NullString
Owner string
Comment string
Stage string
Location string
FileFormatName string
FileFormatType string
Cloud string
Region string
NotificationChannel sql.NullString
LastRefreshedOn sql.NullTime
TableFormat string
LastRefreshDetails sql.NullString
OwnerRoleType string
CreatedOn time.Time `db:"created_on"`
Name string `db:"name"`
DatabaseName string `db:"database_name"`
SchemaName string `db:"schema_name"`
Invalid bool `db:"invalid"`
InvalidReason sql.NullString `db:"invalid_reason"`
Owner string `db:"owner"`
Comment string `db:"comment"`
Stage string `db:"stage"`
Location string `db:"location"`
FileFormatName string `db:"file_format_name"`
FileFormatType sql.NullString `db:"file_format_type"`
Cloud string `db:"cloud"`
Region string `db:"region"`
NotificationChannel sql.NullString `db:"notification_channel"`
LastRefreshedOn sql.NullTime `db:"last_refreshed_on"`
TableFormat string `db:"table_format"`
LastRefreshDetails sql.NullString `db:"last_refresh_details"`
OwnerRoleType string `db:"owner_role_type"`
}

func (e externalTableRow) convert() *ExternalTable {
et := &ExternalTable{
CreatedOn: e.CreatedOn,
Name: e.Name,
DatabaseName: e.DatabaseName,
SchemaName: e.DatabaseName,
SchemaName: e.SchemaName,
Invalid: e.Invalid,
Owner: e.Owner,
Stage: e.Stage,
Location: e.Location,
FileFormatName: e.FileFormatName,
FileFormatType: e.FileFormatType,
Cloud: e.Cloud,
Region: e.Region,
TableFormat: e.TableFormat,
Expand All @@ -365,14 +364,17 @@ func (e externalTableRow) convert() *ExternalTable {
if e.LastRefreshDetails.Valid {
et.LastRefreshDetails = e.LastRefreshDetails.String
}
if e.FileFormatType.Valid {
et.FileFormatType = e.FileFormatType.String
}
return et
}

// describeExternalTableColumnsOptions based on https://docs.snowflake.com/en/sql-reference/sql/desc-external-table
type describeExternalTableColumnsOptions struct {
describeExternalTable bool `ddl:"static" sql:"DESCRIBE EXTERNAL TABLE"`
name AccountObjectIdentifier `ddl:"identifier"`
columnsType bool `ddl:"static" sql:"TYPE = COLUMNS"`
describeExternalTable bool `ddl:"static" sql:"DESCRIBE EXTERNAL TABLE"`
name SchemaObjectIdentifier `ddl:"identifier"`
columnsType bool `ddl:"static" sql:"TYPE = COLUMNS"`
}

type ExternalTableColumnDetails struct {
Expand Down Expand Up @@ -432,9 +434,9 @@ func (r externalTableColumnDetailsRow) convert() *ExternalTableColumnDetails {
}

type describeExternalTableStageOptions struct {
describeExternalTable bool `ddl:"static" sql:"DESCRIBE EXTERNAL TABLE"`
name AccountObjectIdentifier `ddl:"identifier"`
stageType bool `ddl:"static" sql:"TYPE = STAGE"`
describeExternalTable bool `ddl:"static" sql:"DESCRIBE EXTERNAL TABLE"`
name SchemaObjectIdentifier `ddl:"identifier"`
stageType bool `ddl:"static" sql:"TYPE = STAGE"`
}

type ExternalTableStageDetails struct {
Expand Down
20 changes: 10 additions & 10 deletions pkg/sdk/external_tables_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
type CreateExternalTableRequest struct {
orReplace *bool
ifNotExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
columns []*ExternalTableColumnRequest
cloudProviderParams *CloudProviderParamsRequest
partitionBy []string
Expand Down Expand Up @@ -302,7 +302,7 @@ func (s *CreateExternalTableRequest) toOpts() *CreateExternalTableOptions {
type CreateWithManualPartitioningExternalTableRequest struct {
orReplace *bool
ifNotExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
columns []*ExternalTableColumnRequest
cloudProviderParams *CloudProviderParamsRequest
partitionBy []string
Expand Down Expand Up @@ -365,7 +365,7 @@ func (v *CreateWithManualPartitioningExternalTableRequest) toOpts() *CreateWithM
type CreateDeltaLakeExternalTableRequest struct {
orReplace *bool
ifNotExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
columns []*ExternalTableColumnRequest
cloudProviderParams *CloudProviderParamsRequest
partitionBy []string
Expand Down Expand Up @@ -433,7 +433,7 @@ func (v *CreateDeltaLakeExternalTableRequest) toOpts() *CreateDeltaLakeExternalT

type CreateExternalTableUsingTemplateRequest struct {
orReplace *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
copyGrants *bool
query string
cloudProviderParams *CloudProviderParamsRequest
Expand Down Expand Up @@ -493,7 +493,7 @@ func (v *CreateExternalTableUsingTemplateRequest) toOpts() *CreateExternalTableU

type AlterExternalTableRequest struct {
ifExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
refresh *RefreshExternalTableRequest
addFiles []*ExternalTableFileRequest
removeFiles []*ExternalTableFileRequest
Expand Down Expand Up @@ -557,7 +557,7 @@ func (v *AlterExternalTableRequest) toOpts() *AlterExternalTableOptions {

type AlterExternalTablePartitionRequest struct {
ifExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
addPartitions []*PartitionRequest
dropPartition *bool
location string
Expand Down Expand Up @@ -590,7 +590,7 @@ func (v *AlterExternalTablePartitionRequest) toOpts() *AlterExternalTablePartiti

type DropExternalTableRequest struct {
ifExists *bool
name AccountObjectIdentifier // required
name SchemaObjectIdentifier // required
dropOption *ExternalTableDropOptionRequest
}

Expand Down Expand Up @@ -681,15 +681,15 @@ func (v *ShowExternalTableRequest) toOpts() *ShowExternalTableOptions {
}

type ShowExternalTableByIDRequest struct {
id AccountObjectIdentifier // required
id SchemaObjectIdentifier // required
}

type DescribeExternalTableColumnsRequest struct {
id AccountObjectIdentifier // required
id SchemaObjectIdentifier // required
}

type DescribeExternalTableStageRequest struct {
id AccountObjectIdentifier // required
id SchemaObjectIdentifier // required
}

func (v *DescribeExternalTableColumnsRequest) toOpts() *describeExternalTableColumnsOptions {
Expand Down
20 changes: 10 additions & 10 deletions pkg/sdk/external_tables_dto_builders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f5d4aeb

Please sign in to comment.