Skip to content

Conversation

@siddhuoo7
Copy link

When querying a Presto table with an array(varchar) column (e.g. from an Iceberg catalog), queries fail with:

cannot convert [ "CustomAnalytics Solutions", "PredictiveForecasting Module" ] (string) to slice

This happens because the Presto Go client’s typeConverter assumes that array(...) results are always []interface{}, but in practice the Presto REST API sometimes serializes array values as JSON-encoded strings. The SDK does not handle this case, resulting in runtime errors when scanning results.

Repro:

CREATE TABLE my_table (
  id bigint,
  products array(varchar)
);

INSERT INTO my_table (id, products)
VALUES (1, ARRAY['CustomAnalytics Solutions', 'PredictiveForecasting Module']);

Go code using the SDK:

rows, _ := conn.QueryContext(ctx, "SELECT id, products FROM my_table")

Result → ❌ cannot convert [...] (string) to slice


✅ Fix:

  • Updated the "array" case in typeConverter.ConvertValue to support both []interface{} and string (JSON-encoded) inputs.
  • If v is a string, attempt to json.Unmarshal it into a []interface{}.
  • If v is already a []interface{}, return as-is.
  • Preserves compatibility with existing behavior while handling additional response formats.

🔨 Implementation details:

case "array":
    switch val := v.(type) {
    case nil:
        return nil, nil
    case string:
        var arr []interface{}
        if err := json.Unmarshal([]byte(val), &arr); err != nil {
            return nil, fmt.Errorf("cannot parse array from string %q: %w", val, err)
        }
        return arr, nil
    case []interface{}:
        return val, nil
    default:
        return nil, fmt.Errorf("cannot convert %v (%T) to slice", v, v)
    }

🧪 Tests:

  • Verified with array(varchar) column (Iceberg catalog).
  • Verified with nested ARRAY[ARRAY['a','b']].
  • Verified backward compatibility when Presto returns []interface{} instead of JSON strings.

@sourcery-ai
Copy link

sourcery-ai bot commented Sep 29, 2025

Reviewer's Guide

Enhanced array handling in the Presto Go client by extending ConvertValue to parse JSON-encoded string arrays, refactoring slice normalization into a new helper, updating NullSliceString.Scan accordingly, bumping a dependency, and adding a CI workflow for integration tests.

Class diagram for updated array handling in Presto Go client

classDiagram
    class typeConverter {
        +ConvertValue(v interface{}) (driver.Value, error)
    }
    class NullSliceString {
        +SliceString []sql.NullString
        +Valid bool
        +Scan(value interface{}) error
    }
    class "normalizeToInterfaceSlice" {
        +normalizeToInterfaceSlice(value interface{}) ([]interface{}, error)
    }
    typeConverter --> "normalizeToInterfaceSlice": uses
    NullSliceString --> "normalizeToInterfaceSlice": uses
    NullSliceString --> sql.NullString: contains
Loading

Class diagram for new normalizeToInterfaceSlice helper

classDiagram
    class "normalizeToInterfaceSlice" {
        +normalizeToInterfaceSlice(value interface{}) ([]interface{}, error)
    }
    "normalizeToInterfaceSlice" <.. NullSliceString: used by
    "normalizeToInterfaceSlice" <.. typeConverter: used by
Loading

Flow diagram for array(varchar) value normalization

flowchart TD
    A["Presto REST API returns array(varchar) value"] --> B["typeConverter.ConvertValue receives value"]
    B --> C{Is value a string?}
    C -- Yes --> D["json.Unmarshal to []interface{}"]
    C -- No --> E{Is value []interface{}?}
    E -- Yes --> F["Return as-is"]
    E -- No --> G["Error: cannot convert to slice"]
    D --> H["Return []interface{}"]
    F --> H
    G --> I["Return error"]
Loading

File-Level Changes

Change Details Files
Support JSON-encoded string arrays in typeConverter.ConvertValue
  • Handle string inputs by unmarshaling JSON into []interface{}
  • Support existing []interface{} inputs unchanged
  • Return nil for nil values and error for other types
presto/presto.go
Remove obsolete slice validation
  • Deleted validateSlice function
  • Removed its invocation in ConvertValue
presto/presto.go
Introduce normalizeToInterfaceSlice helper
  • Add new helper to convert various slice shapes (JSON strings, []byte, concrete slices) to []interface{}
  • Use reflection to handle concrete slice types
presto/null_slices_helpers.go
Refactor NullSliceString.Scan to use normalization helper
  • Use normalizeToInterfaceSlice instead of manual type assertion
  • Preserve nil-as-empty behavior and update error formatting
  • Keep element scanning via existing scanNullString
presto/presto.go
Bump golang.org/x/crypto dependency
  • Update version to v0.31.0 in go.mod
go.mod
Add GitHub Actions workflow for integration tests
  • Create run-presto-tests.yml to set up Go, Docker and run integration tests
.github/workflows/run-presto-tests.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@siddhuoo7 siddhuoo7 changed the title 🐞 Bug Fix: Support array(varchar) results returned as JSON strings Bug Fix: Support array(varchar) results returned as JSON strings Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant