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

SNOW-1348106: Terraform crashes when creating a stored procedure that returns a NUMBER #1195

Closed
lepagea01 opened this issue Aug 31, 2022 · 4 comments
Labels
bug Used to mark issues with provider's incorrect behavior category:resource resource:procedure Issue connected to the snowflake_procedure resource

Comments

@lepagea01
Copy link

Provider Version
provider registry.terraform.io/snowflake-labs/snowflake v0.42.1

Terraform Version
Terraform v1.2.8
on darwin_arm64

Describe the bug
The provider/plugin crashes when creating a stored procedure written in Snowflake Scripting that returns a NUMBER. Note that the crashes happen after the procedure is created in Snowflake. The stored procedure is not saved in terraform's state.

Expected behavior
To create such a stored procedure successfully (in both Snowflake and terraform's state) and resume gracefully.

Code samples and commands
The crash is systematic and can be reproduced with the following code sample:

main.tf

terraform {
  backend "local" {}
}

provider "snowflake" {
  role = "SYSADMIN"
}

data "snowflake_database" "this" {
  name = "DEMO_DB"
}

resource "snowflake_procedure" "sample_sql" {
  database            = data.snowflake_database.this.name
  schema              = "PUBLIC"
  name                = "SP_SAMPLE_SQL"
  execute_as          = "CALLER"
  language            = "SQL"
  null_input_behavior = "RETURNS NULL ON NULL INPUT"
  return_type         = "NUMBER"
  comment             = "Sample procedure."
  statement           = <<-EOT
  begin
    return 10;
  end
  ;
  EOT  
}

versions.tf

terraform {
  required_providers {
    snowflake = {
      source  = "Snowflake-Labs/snowflake"
      version = ">= 0.42.0, < 0.43.0"
    }
  }
}

Additional context
Based on the partial debug output below (a complete debug output is attached), especially the line panic: runtime error: index out of range [1] with length 0, it seems to me that the plugin does not expect NUMBER as a possible type returned by a stored procedure (it is new to stored procedures written in Snowflake Scripting). I may be wrong.

Thanks,

2022-08-31T12:40:58.662-0400 [TRACE] statemgr.Filesystem: writing snapshot at terraform.tfstate
╷
│ Error: Request cancelled
│ 
│   with snowflake_procedure.sample_sql,
│   on main.tf line 13, in resource "snowflake_procedure" "sample_sql":
│   13: resource "snowflake_procedure" "sample_sql" {
│ 
│ The plugin.(*GRP
[debug_output.log](/~https://github.com/Snowflake-Labs/terraform-provider-snowflake/files/9463487/debug_output.log)
CProvider).ApplyResourceChange request was cancelled.
╵
2022-08-31T12:40:58.683-0400 [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info
2022-08-31T12:40:58.690-0400 [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock

Stack trace from the terraform-provider-snowflake_v0.42.1 plugin:

panic: runtime error: index out of range [1] with length 0

goroutine 47 [running]:
github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.ReadProcedure(0x14000117580, {0x1057a9100?, 0x14000129ad0})
        github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/procedure.go:274 +0xe00
github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.CreateProcedure(0x0?, {0x1057a9100?, 0x14000129ad0})
        github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/procedure.go:200 +0x444
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0x1057d2100?, {0x1057d2100?, 0x14000783dd0?}, 0xd?, {0x1057a9100?, 0x14000129ad0?})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/resource.go:695 +0x134
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0x14000424460, {0x1057d2100, 0x14000783dd0}, 0x1400073b450, 0x14000117400, {0x1057a9100, 0x14000129ad0})
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/resource.go:837 +0x86c
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0x140003fde48, {0x1057d2100?, 0x14000783cb0?}, 0x14000784960)
        github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/grpc_provider.go:1021 +0xb70
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0x140005b41e0, {0x1057d2100?, 0x140007834a0?}, 0x1400078c2a0)
        github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/tf5server/server.go:818 +0x3b8
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x105775d40?, 0x140005b41e0}, {0x1057d2100, 0x140007834a0}, 0x1400078c230, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0x140005d4000, {0x1057d7a60, 0x14000582b60}, 0x140001aeea0, 0x1400052e8a0, 0x105f10220, 0x0)
        google.golang.org/grpc@v1.48.0/server.go:1295 +0x9c4
google.golang.org/grpc.(*Server).handleStream(0x140005d4000, {0x1057d7a60, 0x14000582b60}, 0x140001aeea0, 0x0)
        google.golang.org/grpc@v1.48.0/server.go:1636 +0x82c
google.golang.org/grpc.(*Server).serveStreams.func1.2()
        google.golang.org/grpc@v1.48.0/server.go:932 +0x84
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.48.0/server.go:930 +0x290

Error: The terraform-provider-snowflake_v0.42.1 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

2022-08-31T12:40:58.690-0400 [DEBUG] provider: plugin exited

debug_output.log

@lepagea01 lepagea01 added the bug Used to mark issues with provider's incorrect behavior label Aug 31, 2022
@michael-robbins
Copy link
Contributor

michael-robbins commented Sep 6, 2022

We are seeing this exact same thing but with a PROCEDURE that returns a VARCHAR (I don't think that's the problem)...

We see the PROCEDURE correctly created, and then it appears to read back the PROCEDURE with DESCRIBE PROCEDURE, and the provider is looping over the resulting rows and is expecting... more rows than is returned?

2022-09-06T11:19:31.280+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-conn-query [query DESCRIBE PROCEDURE "DB"."SCHEMA"."STORED_PROC_NAME"() err <nil> duration 111.556797ms args {}]: timestamp=2022-09-06T11:19:31.280+1000
2022-09-06T11:19:31.281+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 589.883µs]: timestamp=2022-09-06T11:19:31.280+1000
2022-09-06T11:19:31.281+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 387.16µs]: timestamp=2022-09-06T11:19:31.281+1000
2022-09-06T11:19:31.281+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 333.23µs]: timestamp=2022-09-06T11:19:31.281+1000
2022-09-06T11:19:31.282+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 349.619µs]: timestamp=2022-09-06T11:19:31.282+1000
2022-09-06T11:19:31.282+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 331.312µs]: timestamp=2022-09-06T11:19:31.282+1000
2022-09-06T11:19:31.282+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 299.313µs]: timestamp=2022-09-06T11:19:31.282+1000
2022-09-06T11:19:31.283+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err <nil> duration 241.914µs]: timestamp=2022-09-06T11:19:31.283+1000
2022-09-06T11:19:31.283+1000 [INFO]  provider.terraform-provider-snowflake_v0.43.0: 2022/09/06 11:19:31 [DEBUG] sql-rows-next [err EOF duration 401ns]: timestamp=2022-09-06T11:19:31.283+1000
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: panic: runtime error: index out of range [1] with length 0
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0:
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: goroutine 21 [running]:
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.ReadProcedure(0xc0006cf000, {0x1e59740?, 0xc00037dee0})
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/procedure.go:274 +0x11c9
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.CreateProcedure(0x0?, {0x1e59740?, 0xc00037dee0})
2022-09-06T11:19:31.285+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/procedure.go:200 +0x5e5
2022-09-06T11:19:31.286+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0x20ac340?, {0x20ac340?, 0xc0005289c0?}, 0xd?, {0x1e59740?, 0xc00037dee0?})
2022-09-06T11:19:31.286+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/resource.go:695 +0x178
2022-09-06T11:19:31.286+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc0006c4380, {0x20ac340, 0xc0005289c0}, 0xc0008c25b0, 0xc0006cee80, {0x1e59740, 0xc00037dee0})
2022-09-06T11:19:31.286+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/resource.go:837 +0xa85
2022-09-06T11:19:31.286+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc0002a0318, {0x20ac340?, 0xc0005288a0?}, 0xc00054a230)
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.21.0/helper/schema/grpc_provider.go:1021 +0xe8d
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc00061a140, {0x20ac340?, 0xc000528060?}, 0xc00053c070)
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/tf5server/server.go:818 +0x574
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1e26380?, 0xc00061a140}, {0x20ac340, 0xc000528060}, 0xc00053c000, 0x0)
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	github.com/hashicorp/terraform-plugin-go@v0.14.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: google.golang.org/grpc.(*Server).processUnaryRPC(0xc000028000, {0x20b1d80, 0xc000503a00}, 0xc000a3e000, 0xc0006bd080, 0x2813420, 0x0)
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	google.golang.org/grpc@v1.48.0/server.go:1295 +0xb2b
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: google.golang.org/grpc.(*Server).handleStream(0xc000028000, {0x20b1d80, 0xc000503a00}, 0xc000a3e000, 0x0)
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	google.golang.org/grpc@v1.48.0/server.go:1636 +0xa2f
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: google.golang.org/grpc.(*Server).serveStreams.func1.2()
2022-09-06T11:19:31.287+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	google.golang.org/grpc@v1.48.0/server.go:932 +0x98
2022-09-06T11:19:31.288+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: created by google.golang.org/grpc.(*Server).serveStreams.func1
2022-09-06T11:19:31.288+1000 [DEBUG] provider.terraform-provider-snowflake_v0.43.0: 	google.golang.org/grpc@v1.48.0/server.go:930 +0x28a
2022-09-06T11:19:31.288+1000 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/snowflake-labs/snowflake/0.43.0/darwin_amd64/terraform-provider-snowflake_v0.43.0 pid=14156 error="exit status 2"
2022-09-06T11:19:31.288+1000 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = transport is closing"
2022-09-06T11:19:31.288+1000 [ERROR] plugin.(*GRPCProvider).ApplyResourceChange: error="rpc error: code = Canceled desc = context canceled"
2022-09-06T11:19:31.289+1000 [ERROR] vertex "resource-name" error: Request cancelled

@lepagea01
Copy link
Author

I had a specific stored procedure written in Snowflake Scripting that originally returned a NUMBER, hence the title of this post and the sample procedure provided. The provider fails to generate this procedure when NUMBER or NUMBER(p, s) is the data type returned but succeeds when FLOAT is the data type returned! For this specific procedure, this is my current workaround.

I initially thought that the provider had problems handling procedures returning Snowflake data types somehow incompatible with JavaScript (as far as Snowflake is concerned). Seeing an issue now reported when the returned data type is VARCHAR suggests something else. Parenthesis?

Finally, I can confirm that the provider succeeds in generating Snowflake Scripting procedures that return semi-structured data types (ex. ARRAY, VARIANT). In the solution I'm currently working on, such procedures are frequent and, so far , things run smoothly.

The problem has to be linked to the returned data type, somehow.

Hope this helps,

AL

@sfc-gh-jcieslak sfc-gh-jcieslak added category:resource resource:procedure Issue connected to the snowflake_procedure resource labels May 20, 2024
@sfc-gh-asawicki
Copy link
Collaborator

Hey @lepagea01.

In v0.100.0 we have introduced new procedure resources. Handling different data types was greatly improved, so they should work correctly in the presented case. Please check it out and let us know.

@sfc-gh-dszmolka
Copy link
Collaborator

closing issue per above comment. Do let us know please if you still see this issue even in v0.100.0 / v1 of the provider.

@sfc-gh-dszmolka sfc-gh-dszmolka changed the title Terraform crashes when creating a stored procedure that returns a NUMBER SNOW-1348106: Terraform crashes when creating a stored procedure that returns a NUMBER Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Used to mark issues with provider's incorrect behavior category:resource resource:procedure Issue connected to the snowflake_procedure resource
Projects
None yet
Development

No branches or pull requests

5 participants