0
votes

I'm using the standard go sql package to interface with AWS Athena. My query returns for each record a uuid (string) and an array of emails.

Here is the code:

package main


import (
  "fmt"
  "database/sql"
  _ "github.com/segmentio/go-athena"
  _"encoding/json"
)

type Contact struct {
  userid string
  emails []string
}


func main() {
  fmt.Println("hello")
  db, err := sql.Open("athena", "db=example")
  if err != nil {
    panic(err)
  }
  rows, err := db.Query("SELECT userid, transform(value.emails, x -> x.value) from database LIMIT 10")
  // Returns 
  // Row 1: "abc-123", ["[email protected]", "[email protected]"] 
  // Row 2: "def-456", ["[email protected]"]

  if err != nil {
    panic(err)
  }

  for rows.Next() {
    var contact Contact
    rows.Scan(&contact.userid, &contact.emails)
    fmt.Println(contact)
  }
}

However, I get this error in the for loop:

panic: unknown type `array` with value [[email protected]]

I'm confused about the array type mentioned and I can't make sense of the error. How can I map the list of emails returned to a slice of strings in the Contact struct ?

1
if you take a look at the go-athena source code here github.com/segmentio/go-athena/blob/… , you can see that it does not handle array type, the issue is also opened github.com/segmentio/go-athena/issues/18 here - Andy

1 Answers

0
votes

Athena supports structural data types. For example the structural data type array:

Structural types

    ARRAY < data_type >

From the message you get I assume the column email is of type ARRAY<VARCHAR>. In addition segmentio/go-athena panics on unsupported operations like Begin for a transaction (which are not supported in Athena). To read data in a Go array you have to put in some logic. See read "SELECT *" columns into []string in go or Read a Postgresql array directly into a Golang Slice for a starter. As you can see with the pq driver, reading an array might be implemented differently than just scanning a row