{"id":28603997,"url":"https://github.com/uber/athenadriver","last_synced_at":"2026-01-12T02:42:51.248Z","repository":{"id":43832006,"uuid":"238810193","full_name":"uber/athenadriver","owner":"uber","description":"A fully-featured AWS Athena database driver (+ athenareader https://github.com/uber/athenadriver/tree/master/athenareader)","archived":false,"fork":false,"pushed_at":"2025-01-21T05:36:17.000Z","size":3106,"stargazers_count":156,"open_issues_count":13,"forks_count":37,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-01-21T06:25:00.648Z","etag":null,"topics":["atg","athena","aws","database","driver","go","golang","hiveql","prestosql","s3","sandbox","sql","uber"],"latest_commit_sha":null,"homepage":"https://uber.github.io/athenadriver","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uber.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-07T00:04:14.000Z","updated_at":"2025-01-21T05:36:21.000Z","dependencies_parsed_at":"2024-06-18T12:42:32.150Z","dependency_job_id":"6a286430-bd4b-4faf-a942-8203667e5dc1","html_url":"https://github.com/uber/athenadriver","commit_stats":{"total_commits":180,"total_committers":17,"mean_commits":"10.588235294117647","dds":"0.23333333333333328","last_synced_commit":"551c53d1f594c012982f168b2c756f757bf32220"},"previous_names":["uber/athenasql"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/uber/athenadriver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber%2Fathenadriver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber%2Fathenadriver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber%2Fathenadriver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber%2Fathenadriver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uber","download_url":"https://codeload.github.com/uber/athenadriver/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uber%2Fathenadriver/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259308162,"owners_count":22837974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atg","athena","aws","database","driver","go","golang","hiveql","prestosql","s3","sandbox","sql","uber"],"created_at":"2025-06-11T17:39:57.000Z","updated_at":"2026-01-12T02:42:51.242Z","avatar_url":"https://github.com/uber.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n![](resources/logo.png)\n\n[![CodeCov][cov-img]][cov]\n[![GoDoc][doc-img]][doc]\n[![Github release][release-img]][release]\n[![Go Report Card][report-card-img]][report-card]\n[![lic][license-img]][license]\n[![made][made-img]][made]\n\n----\n\n:package: [athenadriver](https://github.com/uber/athenadriver/tree/master/go) - A fully-featured AWS Athena database driver for Go  \n:shell: [athenareader](https://github.com/uber/athenadriver/tree/master/athenareader) - A moneywise command line utililty to query athena in command line.\n\n----\n\n## Overview\n\n(This project is a sandbox project and the development status is STABLE.)\n\n`athenadriver` is a fully-featured AWS Athena database driver for Go developed at Uber Technologies Inc.\nIt provides a hassle-free way of querying AWS Athena database with Go standard\nlibrary. It not only provides basic features of Athena Go SDK, but \naddresses some SDK's limitation, improves and extends it. Moreover, it also includes\nadvanced features like Athena workgroup and tagging creation, driver read-only mode and so on.\n\nThe PDF version of AthenaDriver document is available at [ :scroll: ](resources/athenadriver.pdf)\n\n## Features\n\nExcept the basic features provided by Go `database/sql` like error handling, database pool and reconnection, `athenadriver` supports the following features out of box:\n\n- Support multiple AWS authorization methods [:link:](#support-multiple-aws-authentication-methods)\n- Full support of [Athena Basic Data Types](https://docs.aws.amazon.com/athena/latest/ug/data-types.html)\n- Full support of [Athena Advanced Type](https://docs.aws.amazon.com/athena/latest/ug/querying-athena-tables.html) for queries with Geospatial identifiers, ML and UDFs \n- Full support of *ALL* Athena Query Statements, including [DDL](https://docs.aws.amazon.com/athena/latest/ug/language-reference.html), [DML](https://docs.aws.amazon.com/athena/latest/ug/functions-operators-reference-section.html) and [UTILITY](https://github.com/aws/aws-sdk-go/blob/master/service/athena/api.go#L5002) [:link:](#full-support-of-all-data-types)\n- Support newly added [`INSERT INTO...VALUES`](https://aws.amazon.com/about-aws/whats-new/2019/09/amazon-athena-adds-support-inserting-data-into-table-results-of-select-query/)\n- Athena workgroup and tagging support including remote workgroup creation [:link:](#query-with-workgroup-and-tag)\n- Go sql's prepared statement support [:link:](#prepared-statement-support-for-athena-db)\n- Go sql's `DB.Exec()` and `db.ExecContext()` support [:link:](#dbexec-and-dbexeccontext)\n- Query cancelling support [:link:](#query-cancellation)\n- Override default query timeout limits [:link:](#overriding-athena-service-limits-for-query-timeout)  \n- Mask columns with specific values [:link:](#mask-columns-with-specific-values)\n- Database missing value handling [:link:](#missing-value-handling)\n- Read-Only mode - disable database write in driver level [:link:](#read-only-mode)\n- Moneywise mode :moneybag: - print out query cost(USD) for each query\n- Query with Athena Query ID(QID) - (the ultimate money saver! :money_with_wings: )\n- Pseudo commands from database/sql interface: `get_driver_version`, `get_query_id`, `get_query_id_status`, `stop_query_id`, `get_workgroup`, `list_workgroups`, `update_workgroup`, `get_cost`, `get_execution_report` etc [:link:](#pseudo-commands)\n- Builtin logging support with zap [:link:](#enable-driver-logging)\n- Builtin metrics support with tally [:link:](#enable-metrics)\n\n`athenadriver` can extremely simplify your code. Check [athenareader](https://github.com/uber/athenadriver/tree/master/athenareader) out as an example and a convenient tool for your Athena query in command line. \n\n## How to set up/install/test `athenadriver`\n\n### Prerequisites - AWS Credentials \u0026 S3 Query Result Bucket \n\nTo be able to query AWS Athena, you need to have an AWS account at [Amazon AWS's website](https://aws.amazon.com/). To\n give it a shot, a free\n tier account is enough. You also need to have a pair of AWS `access key ID` and `secret access key`.\nYou can get it from [AWS Security Credentials section of Identity and Access Management (IAM)](https://docs.aws.amazon.com/general/latest/gr/aws-security-credentials.html).\nIf you don't have one, please create it. The following is a screenshot from my temporary free tier account:\n\n![How to create AWS credentials](resources/aws_keys.png)\n\nIn addition to AWS credentials, you also need an s3 bucket to store query result. Just go to \n[AWS S3 web console page](https://s3.console.aws.amazon.com/s3/home) to create one.\nIn the examples below, the s3 bucket I use is `s3://myqueryresults/`.\n\nIn most cases, you need the following 4 prerequisites:\n\n- S3 Output bucket\n- `access key ID`\n- `secret access key`\n- AWS region\n\nFor more details on `athenadriver`'s support on AWS credentials \u0026 S3 query result bucket, please refer to section\n [Support Multiple AWS Authorization Methods](#support-multiple-aws-authentication-methods).\n\n### Installation\n\nBefore Go 1.17, `go get` can be used to install athenadriver:\n\n```go\ngo get -u github.com/uber/athenadriver\n```\n\nStarting in Go 1.17, installing executables with `go get` is deprecated. `go install` may be used instead.\n\n```go\ngo install github.com/uber/athenadriver@latest\n```\n\n\n### Tests\n\nWe provide unit tests and integration tests in the codebase.\n\n#### Unit Test\n\nAll the unit tests are self-contained and passed even in no-internet environment. Test coverage is 100%. \n\n```bash\n$ cd $GOPATH/src/github.com/uber/athenadriver/go\n✔ /opt/share/go/path/src/github.com/uber/athenadriver [uber|✚ 1…12] \n21:35 $ go test -coverprofile=coverage.out github.com/uber/athenadriver/go  \u0026\u0026 \\\n go tool cover -func=coverage.out |grep -v 100.0%\nok  \tgithub.com/uber/athenadriver/go\t9.255s\tcoverage: 100.0% of statements\n```\n\n\n#### Integration Test\n\nAll integration tests are under [`examples`](https://github.com/uber/athenadriver/tree/master/examples) folder.\nPlease make sure all prerequisites are met so that you can run the code on your own machine.\n\nAll the code snippets in `examples` folder are fully tested in our machines. For example, \nto run some stress and crash test, you can use `examples/perf/concurrency.go`. Build it first:\n\n```go\n$cd $GOPATH/src/github.com/uber/athenadriver\n$go build examples/perf/concurrency.go\n```\n\nRun it, wait for some output but not all, and unplug your network cable:\n\n```go\n$./concurrency \u003e examples/perf/concurrency.output.`date +\"%Y-%m-%d-%H-%M-%S\"`.log\n58,13,53,54,78,96,32,48,40,11,35,31,65,61,1,73,74,22,34,49,80,5,69,37,0,79,\n2020/02/09 13:49:29 error [38]RequestError: send request failed\ncaused by: Post https://athena.us-east-1.amazonaws.com/: dial tcp: \nlookup athena.us-east-1.amazonaws.com: no such host\n...\n2020/02/09 13:49:29 error [89]RequestError: send request failed\ncaused by: Post https://athena.us-east-1.amazonaws.com/: dial tcp: \nlookup athena.us-east-1.amazonaws.com: no such host\n```\nYou can see `RequestError` is thrown out from the code. The active Athena queries failed because the network is down.\nNow re-plugin your cable and wait for network coming back, you can see the program automatically reconnects to Athena, and resumes to output data correctly:\n\n```go\n72,25,92,98,15,93,41,7,8,90,81,56,66,2,18,84,87,63,\n44,45,82,99,86,3,52,76,71,16,39,67,23,12,42,17,4,\n```\n\n## How to use `athenadriver`\n\n`athenadriver` is very easy to use. What you need to do it to import it in your code and then use the standard Go `database/sql` as usual.\n\n```go\nimport athenadriver \"github.com/uber/athenadriver/go\"\n```\n\nThe following are coding examples to demonstrate `athenadriver`'s features and how you should use `athenadriver` in your Go application.\nPlease be noted the code is for demonstration purpose only, so please follow your own coding style or best practice if necessary.\n\n### Get Started - A Simple Query\n\nThe following is the simplest example for demonstration purpose. The source code is available at [dml_select_simple.go](https://github.com/uber/athenadriver/blob/master/examples/query/dml_select_simple.go)\\.\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// Step 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\t// Step 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// Step 3. Query and print results\n\tvar url string\n\t_ = db.QueryRow(\"SELECT url from sampledb.elb_logs limit 1\").Scan(\u0026url)\n\tprintln(url)\n}\n```\n\nTo make it work for you, please replace `OutputBucket`, `Region`, `AccessID` and\n `SecretAccessKey` with your own values. `sampledb` is provided by Amazon so you don't have to worry about it.\n\nTo Build it:\n```go\n$ go build examples/query/dml_select_simple.go \n```\n\nRun it and you can see output like:\n```go\n$ ./dml_select_simple \nhttps://www.example.com/articles/553\n```\n\n### Support Multiple AWS Authentication Methods\n\n`athenadriver` uses access keys(Access Key ID and Secret Access Key) to sign programmatic requests to AWS.\nWhen if the AWS_SDK_LOAD_CONFIG environment variable was set, `athenadriver` uses [`Shared Config`](https://docs.aws.amazon.com/sdk-for-go/api/aws/session/#pkg-overview), respects [AWS CLI Configuration and Credential File Settings](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html) and gives it even higher priority over the values set in `athenadriver.Config`.\n\n#### Use AWS CLI Config For Authentication\n\nWhen environment variable `AWS_SDK_LOAD_CONFIG` is set, it will read `aws_access_key_id`(AccessID) and `aws_secret_access_key`(SecretAccessKey)\nfrom `~/.aws/credentials`, `region` from `~/.aws/config`. For details about `~/.aws/credentials` and `~/.aws/config`, please check [here](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).\n\nBut you still need to specify correct `OutputBucket` in `athenadriver.Config` because it is not in the AWS client config.\n\n`OutputBucket` is critical in Athena. Even if you have a default value set in Athena web console, you must pass one programmatically or you will get error:\n`No output location provided. An output location is required either through the Workgroup result configuration setting or as an API input.`\n\n\nThe sample code below enforces AWS_SDK_LOAD_CONFIG is set, so `athenadriver`'s AWS Session will be created from the configuration values from the shared config (`~/.aws/config`) and shared credentials (`~/.aws/credentials`) files.\nEven if we pass all dummy values as parameters in `NewDefaultConfig()` except `OutputBucket`, they are overridden by\nthe values in AWS CLI config files, so it doesn't really matter.\n\n```go\n// To use AWS CLI's Config for authentication\nfunc useAWSCLIConfigForAuth() {\n\tos.Setenv(\"AWS_SDK_LOAD_CONFIG\", \"1\")\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, err := drv.NewDefaultConfig(secret.OutputBucketProd, drv.DummyRegion,\n\t\tdrv.DummyAccessID, drv.DummySecretAccessKey)\n\tif err != nil {\n\t\treturn\n\t}\n\t// 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// 3. Query and print results\n\tvar i int\n\t_ = db.QueryRow(\"SELECT 456\").Scan(\u0026i)\n\tprintln(\"with AWS CLI Config:\", i)\n\tos.Unsetenv(\"AWS_SDK_LOAD_CONFIG\")\n}\n```\n\nIf your AWS CLI setting is valid like mine, this function should output:\n\n```go\nwith AWS CLI Config: 456\n```\n\nThe above authentication method also works for querying Athena in [AWS Lambda](https://aws.amazon.com/lambda/). In lambda, you don't have to provide access ID, key and region, and you don't need AWS CLI config files either. You just need to specify the correct output bucket.\nPlease check the AWS Lambda Go same code [here](https://github.com/uber/athenadriver/tree/master/examples/lambda/Go).\n\n#### Use `athenadriver` Config For Authentication\n\nWhen environment variable `AWS_SDK_LOAD_CONFIG` is NOT set, you may explicitly define credentials by passing valid (NOT dummy) `accessID`, `secretAccessKey`, `region`, and `outputBucket` into `athenadriver.NewDefaultConfig()`.\n\nThe sample code below ensure `AWS_SDK_LOAD_CONFIG` is not set, then pass four valid parameters into `NewDefaultConfig()`:\n\n```go\n// To use athenadriver's Config for authentication\nfunc useAthenaDriverConfigForAuth() {\n\tos.Unsetenv(\"AWS_SDK_LOAD_CONFIG\")\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, err := drv.NewDefaultConfig(secret.OutputBucketDev, secret.Region,\n\t\tsecret.AccessID, secret.SecretAccessKey)\n\tif err != nil {\n\t\treturn\n\t}\n\t// 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// 3. Query and print results\n\tvar i int\n\t_ = db.QueryRow(\"SELECT 123\").Scan(\u0026i)\n\tprintln(\"with AthenaDriver Config:\", i)\n}\n```\nThe sample output:\n\n```go\nwith AthenaDriver Config: 123\n```\n\nThe full code is here at [examples/auth.go](https://github.com/uber/athenadriver/tree/master/examples/auth.go).\n\n#### Use AWS SDK Default Credentials Resolution for Authentication\nIf environment variable `AWS_SDK_LOAD_CONFIG` is NOT set and credentials are not supplied in the `athenadriver` configuration, the AWS SDK will look up credentials using its default methodology described here: https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials.\n\n`Region` and `OutputBucket` bucket still need to be explictly defined.\n\nThe sample code below ensures `AWS_SDK_LOAD_CONFIG` is not set, then creates a athenadriver config with OutputBucket and Region values set.\n\n```go\n// To use AWS SDK Default Credentials\nfunc useAthenaDriverConfigForAuth() {\n\tos.Unsetenv(\"AWS_SDK_LOAD_CONFIG\")\n\t// 1. Set OutputBucket and Region in Driver Config.\n\tconf := drv.NewNoOpsConfig()\n\tconf.SetOutputBucket(secret.OutputBucketDev)\n\tconf.SetRegion(secret.Region)\n\n\t// 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// 3. Query and print results\n\tvar i int\n\t_ = db.QueryRow(\"SELECT 123\").Scan(\u0026i)\n\tprintln(\"with AthenaDriver Config:\", i)\n}\n```\nThe sample output:\n\n```go\nwith AthenaDriver Config: 123\n```\n\n### Full Support of All Data Types \n\nAs we said, `athenadriver` supports all Athena data types. \nIn the following sample code, we use an SQL statement to `SELECT` som simple data of all the advanced types and then print them out.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, err := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t// 3. Query and print results\n\tquery := \"SELECT JSON '\\\"Hello Athena\\\"', \" +\n\t\t\"ST_POINT(-74.006801, 40.70522), \" +\n\t\t\"ROW(1, 2.0),  INTERVAL '2' DAY, \" +\n\t\t\"INTERVAL '3' MONTH, \" +\n\t\t\"TIME '01:02:03.456', \" +\n\t\t\"TIME '01:02:03.456 America/Los_Angeles', \" +\n\t\t\"TIMESTAMP '2001-08-22 03:04:05.321 America/Los_Angeles';\"\n\trows, err := db.Query(query)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer rows.Close()\n\tprintln(drv.ColsRowsToCSV(rows))\n}\n```\n\nSample output:\n```bash\n\"Hello Athena\",00 00 00 00 01 01 00 00 00 20 25 76 6d 6f 80 52 c0 18 3e 22 a6 44 5a 44 40,\n{field0=1, field1=2.0},2 00:00:00.000,0-3,0000-01-01T01:02:03.456-07:52,\n0000-01-01T01:02:03.456-07:52,2001-08-22T03:04:05.321-07:00\n```\n\nwe can see `athenadriver` can handle all these advanced types correctly.\n\n\n### Query With Workgroup and Tag \n\n`athenadriver` supports workgroup and tagging features of Athena. When you query Athena, you can specify the\n workgroup and tags attached with your query. Resource/cost tagging are based on workgroup. If the workgroup doesn't\n exist , by default it will be created programmatically.\n \nIf you want to disable programmatically creating workgroup and tags, you need to explicitly call:\n```go\nConfig.SetWGRemoteCreationAllowed(false)\n```\nIn this case, you need to make sure the workgroup you specifies must exist, or you will get error. An example is like\n below:\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"log\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\twgTags := drv.NewWGTags()\n\twgTags.AddTag(\"Uber User\", \"henry.wu\")\n\twgTags.AddTag(\"Uber ID\", \"123456\")\n\twgTags.AddTag(\"Uber Role\", \"SDE\")\n\t// Specify that workgroup `henry_wu` is used for the following query\n\twg := drv.NewWG(\"henry_wu\", nil, wgTags)\n\tconf.SetWorkGroup(wg)\n\t// comment out the line below to allow remote workgroup creation and\n\t// the query will be successful!!!\n\tconf.SetWGRemoteCreationAllowed(false)\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t// 3. Query and print results\n\trows, err := db.Query(\"select url from sampledb.elb_logs limit 3\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n\n\tvar url string\n\tfor rows.Next() {\n\t\tif err := rows.Scan(\u0026url); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tprintln(url)\n\t}\n}\n```\n\nBut I don't have a workgroup named `henry_wu` in AWS Athena, so I got sample output:\n```go\n2020/01/20 15:29:52 Workgroup henry_wu doesn't exist and workgroup remote creation\n is disabled.\n```\n\n\nAfter commenting out `conf.SetWGRemoteCreationAllowed(false)` at line 27, the output becomes:\n\n```go\nhttps://www.example.com/articles/553\nhttp://www.example.com/images/501\nhttps://www.example.com/images/183\n```\n\nand I can see a new workgroup named `henry_wu` is created in AWS Athena console: [https://us-east-2.console.aws\n.amazon.com/athena/workgroups/home](https://us-east-2.console.aws.amazon.com/athena/workgroups/home)\n\n![Athena Workgroup and Tags Automatic Creation](resources/workgroup.png)\n\n###  Prepared Statement Support for Athena DB \n\nAthena doesn't support prepared statement originally. However, it could be very helpful in some \nscenarios like where part of the query is from user input. `athenadriver` supports prepared statements \nto help you to deal with those scenarios. An example is as follows:\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\t// 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// 3. Prepared Statement\n\tstatement, err := db.Prepare(\"CREATE TABLE sampledb.urls AS \" +\n\t\t\"SELECT url FROM sampledb.elb_logs where request_ip=? limit ?\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\t// 4. Execute prepared Statement\n\tif result, e := statement.Exec(\"244.157.42.179\", 2); e == nil {\n\t\tif rowsAffected, err := result.RowsAffected(); err == nil {\n\t\t\tprintln(rowsAffected)\n\t\t}\n\t}\n}\n```\n\nSample output:\n```go\n2\n```\n\n### Parameterized Queries\n\nAthena supports parameterized queries: https://docs.aws.amazon.com/athena/latest/ug/querying-with-prepared-statements.html.\nParameterized queries allow for re-running the same query with different parameter values at runtime, and help guard \nagainst SQL injection attacks. This is especially useful if some of your parameter values are derived from user input.\n\nTo use parameterized queries, use `?` as placeholders in the query you pass to `DB.Query()` or `DB.Exec()`.\nFor each parameter, pass in arguments in the order they should replace `?`. For strings and byte slice arguments, use \n`drv.FormatString()` and `drv.FormatBytes()` to escape special characters and format per Athena's requirements.\n\nExample:\n\n```go\nquery := \"SELECT request_timestamp, elb_name FROM sampledb.elb_logs WHERE url=? limit 1\"\nargs := []any{drv.FormatString(\"https://www.example.com/jobs/878\")}\nrows, err := db.Query(query, args)\nif err != nil {\n    return\n}\nprintln(drv.ColsRowsToCSV(rows))\n```\n\nSample Output:\n```bash\nrequest_timestamp,elb_name\n2015-01-06T04:03:01.351843Z,elb_demo_006\n```\n\n\n###  `DB.Exec()` and `DB.ExecContext()` \n\nAccording to Go source code, `DB.Exec()` and `DB.ExecContext()` execute a query that doesn't return rows, \nsuch as an `INSERT` or `UPDATE`.\nIt's true that you can use `DB.Exec()` and `DB.Query()` interchangeably to execute the same SQL statements.\n\\\nHowever, the two methods are for different use cases and return different types of results. According to Go `database/sql` library, the result \nreturned from `DB.Exec()` can tell you how many rows were affected by the query and the last inserted ID for `INSERT INTO` statement, \nwhich is always *-1* for Athena because auto-increment primary key feature is not supported by Athena.\\\nIn contrast, `DB.Query()` will return a `sql.Rows` object which includes all columns and rows details.\n\nWhen the only concern is if the execution is successful or not, `DB.Exec()` is \npreferred to `DB.Query()` . The best coding practice is:\n\n```go\nif _, err := DB.Exec(`\u003cSQL_STATEMENT\u003e`); err != nil {\n    log_or_panic(err)\n}\n```\n\nIn cases of `INSERT INTO`, `CTAG` and `CVAS`, you may want to know when the execution \nis successful how many rows are affected by your query. Then you can use `result.RowsAffected()` as \ndemonstrated in the following example:\n\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tvar conf *drv.Config\n\tvar err error\n\tif conf, err = drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\"); err != nil {\n\t\tpanic(err)\n\t}\n\t// 2. Open Connection.\n\tdb, _ := sql.Open(drv.DriverName, conf.Stringify())\n\t// 3. Execute and print results\n\tif _, err = db.ExecContext(context.Background(),\n\t\t\"DROP TABLE IF EXISTS sampledb.urls\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tvar result sql.Result\n\tif result, err = db.Exec(\"CREATE TABLE sampledb.urls AS \"+\n\t\t\"SELECT url FROM sampledb.elb_logs where request_ip=? limit ?\",\n\t\t\"244.157.42.179\", 1); err != nil {\n\t\tpanic(err)\n\t}\n\tprintln(result.RowsAffected())\n\n\tif result, err = db.Exec(\"INSERT INTO sampledb.urls VALUES (?),(?),(?)\",\n\t\t\"abc\", \"efg\", \"xyz\"); err != nil {\n\t\tpanic(err)\n\t}\n\tprintln(result.RowsAffected())\n\tprintln(result.LastInsertId()) // not supported by Athena\n}\n```\n\nSample output:\n```go\n1\n3\n```\n\n### Mask Columns with Specific Values \n\nSometimes, database contains sensitive information and you may need to mask columns with specific values. If you don't\n want to display some columns, you can mask them by calling:\n\n```go\nConfig.SetMaskedColumnValue(\"columnName\", \"maskValue\")\n```\n\nFor example, if you want to mask all rows of column `password`, you can specify:\n\n```go\nConfig.SetMaskedColumnValue(\"password\", \"xxx\")\n```\n\nThen all the passwords will be displayed as `xxx` in the query result set. The following is an example to\n mask column `url` in the result set.\n\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"log\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\tconf.SetMaskedColumnValue(\"url\", \"xxx\")\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t// 3. Query and print results\n\trows, err := db.Query(\"select request_timestamp, url from \" +\n\t\t\"sampledb.elb_logs limit 3\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n\n\tvar requestTimestamp string\n\tvar url string\n\tfor rows.Next() {\n\t\tif err := rows.Scan(\u0026requestTimestamp, \u0026url); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tprintln(requestTimestamp + \",\" + url)\n\t}\n}\n```\n\n\nSample Output:\n```go\n2015-01-03T12:00:00.516940Z,xxx\n2015-01-03T12:00:00.902953Z,xxx\n2015-01-03T12:00:01.206255Z,xxx\n```\n\n\n### Query Cancellation \n\nAWS Athena is priced upon the data size it scanned. To save money, `athenadriver` supports query cancellation. In\n the following example, the query is cancelled if it is not complete after 2 seconds.  \n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"log\"\n\t\"time\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t// 3. Query cancellation after 2 seconds\n\tctx, _ := context.WithTimeout(context.Background(), 2*time.Second)\n\trows, err := db.QueryContext(ctx, \"select count(*) from sampledb.elb_logs\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n\n\tvar requestTimestamp string\n\tvar url string\n\tfor rows.Next() {\n\t\tif err := rows.Scan(\u0026requestTimestamp, \u0026url); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tprintln(requestTimestamp + \",\" + url)\n\t}\n}\n```\n\n\nSample Output:\n```go\n2020/01/20 15:28:35 context deadline exceeded\n```\n\n### Overriding Athena Service Limits for Query Timeout\nThis library assumes default [Athena service limits](https://docs.aws.amazon.com/athena/latest/ug/service-limits.html) for DDL and DML query timeouts, as can be found in `athenadriver/go/constants.go`.\nIf you've increased your service limits, for example via the [Athena Service Quotas](https://console.aws.amazon.com/servicequotas/home/services/athena/quotas) console,\nyou can override them on your `Config`.\n\nHere's the same example found at [Query Cancellation](#query-cancellation), but with an *increased* query timeout.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"log\"\n\t\"time\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\t\n\t// 2. Override the DML query timeout to 60 minutes (3600 seconds). \n\tserviceLimitOverride := drv.NewServiceLimitOverride()\n\tserviceLimitOverride.SetDMLQueryTimeout(3600)\n\tconf.SetServiceLimitOverride(*serviceLimitOverride)\n\n\t// 3. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t\n\t// 4. Run the query.\n\trows, err := db.QueryContext(context.Background(), \"select count(*) from sampledb.elb_logs\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n\n\tvar requestTimestamp string\n\tvar url string\n\tfor rows.Next() {\n\t\tif err := rows.Scan(\u0026requestTimestamp, \u0026url); err != nil {\n\t\t\tlog.Fatal(err)\n\t\t}\n\t\tprintln(requestTimestamp + \",\" + url)\n\t}\n}\n```\n\n\n### Missing Value Handling \n\nIt is common to have missing values in S3 file, or Athena DB. When this happens, you can specify if you want to use\n`empty string`, `default data`, or `nil` as the missing value, whichever is better to facilitate your data processing or ETL job. The default data for Athena column type are defined as below:\n\n```go\nfunc (r *Rows) getDefaultValueForColumnType(athenaType string) interface{} {\n\tswitch athenaType {\n\tcase \"tinyint\", \"smallint\", \"integer\", \"bigint\":\n\t\treturn 0\n\tcase \"boolean\":\n\t\treturn false\n\tcase \"float\", \"double\", \"real\":\n\t\treturn 0.0\n\tcase \"date\", \"time\", \"time with time zone\", \"timestamp\",\n\t\t\"timestamp with time zone\":\n\t\treturn time.Time{}\n\tdefault:\n\t\treturn \"\"\n\t}\n}\n```\n\nBy default, we use empty string to replace missing values and empty string is preferred to default data, or `nil`. To use\n`default data`, you have to explicitly call:\n\n```go\nConfig.SetMissingAsEmptyString(false)\nConfig.SetMissingAsDefault(true)\n```\n\nIf you need to use `nil` as missing value, you can call:\n\n```go\nConfig.SetMissingAsEmptyString(false)\nConfig.SetMissingAsDefault(false)\nConfig.SetMissingAsNil(true)\n```\n\nBut if you are strict with your data integrity and want an error raised when data are missing, you can set all three of them to `false`.\n\n\n### Read-Only Mode \n\nWhen read-only mode is enabled in `athenadriver`, it only allows retrieving information from Athena database.\nAny writing and modification to the database will raise an error. This is useful in some cases. By default, read-only mode\nis disabled. To enable it, you need to explicitly call:\n\n```go\nConfig.SetReadOnly(true)\n```\n\nThe following is one example. It enables read-only mode in line 19, but tries to create a new table with CTAS statement.\nIt ends up with raising an error.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"log\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://myqueryresults/\",\n\t\t\"us-east-2\", \"DummyAccessID\", \"DummySecretAccessKey\")\n\tconf.SetReadOnly(true)\n\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\t// 3. Create Table with CTAS statement\n\trows, err := db.QueryContext(context.Background(), \n\t  \"CREATE TABLE sampledb.elb_logs_new AS \" +\n\t\t\"SELECT * FROM sampledb.elb_logs limit 10;\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n}\n```\n\nSample Output:\n```bash\n2020/01/26 01:10:28 writing to Athena database is disallowed in read-only mode\n```\n\n### Pseudo Commands\n\n`athenadriver` provides `pseudo command` to support some special use cases beyond Go's standard database/sql framework.\nOne sample use case is [Asynchronous Query Support](https://github.com/uber/athenadriver/issues/6#issuecomment-624132881).\n`pseudo command` is a special prefix string you can put in `db.QueryContext` or `db.QueryRow` or `db.ExecuteContext` etc.\n\nIt is easier to explain with an example like  [pc_get_query_id.go](https://github.com/uber/athenadriver/blob/master/examples/pc_get_query_id.go).\n\n```go\npackage main\n\nimport (\n\t\"database/sql\"\n\t\"os\"\n\tsecret \"github.com/uber/athenadriver/examples/constants\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tos.Setenv(\"AWS_SDK_LOAD_CONFIG\", \"1\")\n\tconf, err := drv.NewDefaultConfig(secret.OutputBucket, secret.Region, secret.AccessID, secret.SecretAccessKey)\n\tconf.SetLogging(true)\n\tif err != nil {\n\t\tpanic(err)\n\t\treturn\n\t}\n\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DriverName, dsn)\n\n\t// 3. Query with pseudo command `pc:get_query_id`\n\tvar qid string\n\t_ = db.QueryRow(\"pc:get_query_id select url from sampledb.elb_logs limit 2\").Scan(\u0026qid)\n\tprintln(\"Query ID: \", qid)\n}\n```\n\nIn [pc_get_query_id.go](https://github.com/uber/athenadriver/blob/master/examples/pc_get_query_id.go#L27), we only want to get the `Query ID` of the SQL statement, so we\njust to add `pc:get_query_id` before the sql statement. So the final string we pass to `db.QueryRow` is `pc:get_query_id select url from sampledb.elb_logs limit 2`. The return value is one row with an Athena Query ID inside. A sample Output is:\n```\nQuery ID: c89088ab-595d-4ee6-a9ce-73b55aeb8953\n```\n\nNow we support three pseudo commands: `get_query_id`, `get_query_id_status`, `stop_query_id`.\n\nThe syntax is `pc:pseudo_command parameter`.\n\n### get_query_id\n\n`pc:get_query_id SQL_STATEMENT` - Will return Query ID of the `SQL_STATEMENT`, no matter request fails or succeeds. Example: [pc_get_query_id.go](https://github.com/uber/athenadriver/blob/master/examples/pc_get_query_id.go).\n\n### get_query_id_status\n\n`pc:get_query_id_status Query_ID` - Return status of the Query ID. Example: [pc_get_query_id_status.go](https://github.com/uber/athenadriver/blob/master/examples/pc_get_query_id_status.go).\n\n### stop_query_id\n\n`pc:stop_query_id Query_ID` - To stop the Query corresponding the Query ID. If there is no error, a one row string with `OK` will be returned. Example: [pc_stop_query_id.go](https://github.com/uber/athenadriver/blob/master/examples/pc_stop_query_id.go).\n\n### get_driver_version\n\n`pc:get_driver_version` - To return the version of athenadriver. Example: [pc_get_driver_version.go](https://github.com/uber/athenadriver/blob/master/examples/pc_get_driver_version.go).\n\n\n###  Enable Driver Logging\n\nYou can enable driver logging to help you to debug, monitoring and know more details about the running system. Logging\n is by default enabled and implemented as a no-op Logger. You need to pass a workable logger to make it work. If you don't want to log at all, you need to explicitly call:\n\n```go\n  Config.SetLogging(false)\n```\n\nThe following example is to pass in a zap Production logger.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"log\"\n\t\"time\"\n\t\"go.uber.org/zap\"\n\tdrv \"github.com/uber/athenadriver/go\"\n)\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://query-results-bucket-test/\",\n\t\t\"us-east-2\",\n\t\t\"dummy-to-be-replaced\",\n\t\t\"dummy-to-be-replaced\")\n\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DBDriverName, dsn)\n\n\tlogger, _ := zap.NewProduction()\n\tdefer logger.Sync()\n\t// 3. Query cancellation after 2 seconds\n\tctx, _ := context.WithTimeout(context.Background(), 2*time.Second)\n\tctx = context.WithValue(ctx, drv.LoggerKey, logger)\n\trows, err := db.QueryContext(ctx, \"select count(*) from sampledb.elb_logs\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n}\n```\n\n\nSample Output:\n```go\n{\"level\":\"warn\",\"ts\":1579556666.3372262,\"caller\":\"athenadriver/observability.go:72\",\"msg\":\"query canceled\",\"resp.QueryExecutionId\":\n \"ef4f3f09-a480-445c-84ad-96ecd97a8e90\"}\n2020/01/20 13:44:26 context deadline exceeded\n```\n\n###  Enable Metrics\n\n`athenadriver` supports tally metrics reporting builtin. Metrics reporting is by default enabled but implemented as a\n no-op Scope. You need to pass a workable scope to make it work. If you don't want metrics at all, you need to explicitly  call:\n\n```go\n  Config.SetMetrics(false)\n```\n\nThe following example is to pass in a scope with `statsd` reporter.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"io\"\n\t\"log\"\n\t\"time\"\n\t\"github.com/cactus/go-statsd-client/statsd\"\n\ttallystatsd \"github.com/uber-go/tally/v4/statsd\"\n\tdrv \"github.com/uber/athenadriver/go\"\n\t\"github.com/uber-go/tally/v4\"\n)\n\nfunc newScope() (tally.Scope, io.Closer) {\n\tstatter, _ := statsd.NewBufferedClient(\"127.0.0.1:8125\", \"stats\", 100*time.Millisecond, 1440)\n\treporter := tallystatsd.NewReporter(statter, tallystatsd.Options{\n\t\tSampleRate: 1.0,\n\t})\n\tscope, closer := tally.NewRootScope(tally.ScopeOptions{\n\t\tPrefix:   \"my_test_metrics_service\",\n\t\tTags:     map[string]string{},\n\t\tReporter: reporter,\n\t}, time.Second)\n\treturn scope, closer\n}\n\nfunc main() {\n\t// 1. Set AWS Credential in Driver Config.\n\tconf, _ := drv.NewDefaultConfig(\"s3://query-results-bucket-test/\",\n\t\t\"us-east-2\", \"dummy-to-be-replaced\", \"dummy-to-be-replaced\")\n\n\t// 2. Open Connection.\n\tdsn := conf.Stringify()\n\tdb, _ := sql.Open(drv.DBDriverName, dsn)\n\n\t// 3. Query cancellation after 2 seconds\n\t// Create tally scope\n\tscope, _ := newScope()\n\t// Create context and attach tally scope with context\n\tctx, _ := context.WithTimeout(context.Background(), 5*time.Second)\n\tctx = context.WithValue(ctx, drv.MetricsKey, scope)\n\trows, err := db.QueryContext(ctx, \"select count(*) from sampledb.elb_logs\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t\treturn\n\t}\n\tdefer rows.Close()\n}\n```\n\nRun netcat(`nc`) in another terminal to listen at port `8125` with command:\n\n```bash\nnc 8125 -l -u\n```\n\nThen run the code above, you can see the underlying details of driver are reported as metrics like below:\n\n```bash\n$ nc 8125 -l -u\nstats.my_test_metrics_service.awsathena.connector.connect:0.140147|ms\nstats.my_test_metrics_service.awsathena.query.workgroup:0.000607|ms\nstats.my_test_metrics_service.awsathena.query.startqueryexecution:1191.644566|ms\nstats.my_test_metrics_service.awsathena.query.queryexecutionstatesucceeded:3320.820154|ms\n```\n\n## Limitations of Go/Athena SDK's and `athenadriver`'s Solution\n\n### Column number mismatch in `GetQueryResults` of Athena Go SDK\n\n#### `ColumnInfo` has more number of cloumns than `Rows[0].Data`\n\n\u003e ![](resources/pin.png)**Affected Statements: DESCRIBE TABLE/VIEW, SHOW SCHEMA/TABLE/...**\n\n- Sample Query:\n\n```sql\nDESC sampledb.elb_logs\n```\n\n- Analysis:\n\n![Column number mismatch issue example 1](resources/issue_1.png)\n\nWe can see there are 3 columns according to `ColumnInfo` under `ResultSetMetadata`. But in the first row `Rows[0]`, we see there is only 1 field: `\"elb_name \\tstring    \\t    \"`. I would imagine there could have been 3 items in the `Data[0]`, but somehow the code author doesn't split it with tab(`\\t`), so it ends up with only 1 item. The same issue happens for `SHOW` statement.\n\nFor more sample code, please check [util_desc_table.go](https://github.com/uber/athenadriver/blob/master/examples/query/util_desc_table.go), [util_desc_view.go](https://github.com/uber/athenadriver/blob/master/examples/query/util_desc_view.go), and [util_show.go](https://github.com/uber/athenadriver/blob/master/examples/query/util_show.go).\n\n- `athenadriver`'s Solution:\n\n`athenadriver` fixes this issue by splitting `Rows[0].Data[0]` string with tab, and replace the original row with a new row which has the same number of data with columns.\n\n#### `ColumnInfo` has cloumns but `Rows` are empty\n\n\u003e ![](resources/pin.png)**Affected Statements: [`CTAS`](https://docs.aws.amazon.com/athena/latest/ug/ctas.html), CVAS, INSERT INTO**\n\nSample Query:\n\n```sql\nCREATE TABLE sampledb.elb_logs_copy WITH (\n    format = 'TEXTFILE',\n    external_location = 's3://external-location-test/elb_logs_copy', \n    partitioned_by = ARRAY['ssl_protocol'])\nAS SELECT * FROM sampledb.elb_logs\n```\n\nAnalysis:\n\n![Column number mismatch issue example 2](resources/issue_3.png)\n\nIn the above [`CTAS`](https://docs.aws.amazon.com/athena/latest/ug/ctas.html) statement, we see there is one column of type `bigint` named\n `\"rows\"` in the resultset, but `ResultSet.Rows` is empty. Since there is no\n  row, that one column doesn't make sense, or at least is confusing. The same\n  issue happens for `INSERT INTO` statement.\n\n- `athenadriver`'s Solution:\n\nBecause this issue happens only in statements [`CTAS`](https://docs.aws.amazon.com/athena/latest/ug/ctas.html), `CVAS`, and `INSERT INTO\n`, where `UpdateCount` is always valid and is the only meaningful information\n returned from Athena, `athenadriver` sets `UpdateCount` as the value of\n  the returned row.\n\nFor more sample code, please check [ddl_ctas.go](https://github.com/uber/athenadriver/blob/master/examples/query/ddl_ctas.go), [ddl_cvas.go](https://github.com/uber/athenadriver/blob/master/examples/query/ddl_cvas.go), and [dml_insert_into.go](https://github.com/uber/athenadriver/blob/master/examples/query/dml_insert_into.go).\n\n### Type Loss for map, struct, array etc\n\nOne of Athena Go SDK's limitations is the type information could be lost after \nquerying. I think there are two reasons for this type information loss.\n\nThe first reason is Athena SDK doesn't provide the full type information for complex type data.\nIt assumes the application developers know the data schema and should take the responsibility of data serialization.\n\nTo dig into the code, all query results are stored in data structure \n[`ResultSet`](https://docs.aws.amazon.com/athena/latest/APIReference/API_ResultSet.html).\nFrom the UML class graph of `ResultSet` below, we can see the type \ninformation are stored in `ColumnInfo`'s pointer to string variable `Type`, \nwhich is only a type name of data type, not containing any type metadata. \nFor example, querying a map of `string-\u003eboolean` will return the type name `map`, \nbut you cannot find the information `string-\u003eboolean` in the `ResultSet`. For simple type like `integer`, \n`boolean` or `string`, it is sufficient to serialize them to Go type, but for more complex types like `array`, \n`struct`, `map` or nested types, the type information is lost here.\n\n![UML class graph of `ResultSet`](resources/ResultSet_Uml.png)\n\nThe second reason is the difference between Athena data type and Go data type. \nSome Athena builtin data type like `Row`, `DECIMAL(p, s)`, `varbinary`, `interval year to month`\nare not supported in Go standard library. Therefore, there is no way to serialize them in driver level.\n\n- `athenadriver`'s Solution:\n\nFor data types: `array`, `map`, `json`, `char`, `varchar`, `varbinary`, `row`, `string`, `binary`, `struct`, `interval year to month`, `interval day to second`, `decimal`, `athenadriver` returns the string representation of the data. The developers can firstly retrieve the string representation, and then serialize to user defined type on their own.\n\nFor time and date types: `date`, `time`, `time with time zone`, `timestamp`, `timestamp with time zone`, `athenadriver` returns Go's [`time.Time`](https://golang.org/pkg/time/#Time).\n\nSome sample code are available at [dml_select_array.go](https://github.com/uber/athenadriver/blob/master/examples/query/dml_select_array.go),\n[dml_select_map.go](https://github.com/uber/athenadriver/blob/master/examples/query/dml_select_map.go), [dml_select_time.go](https://github.com/uber/athenadriver/blob/master/examples/query/dml_select_time.go).\n\n\n## FAQ\n\nThe following is a collection of questions from our software developers and data scientists.\n\n### Does `athenadriver` support database reconnection?\n\nYes. `database/sql` maintains a connection pool internally and handles connection pooling, reconnecting, and retry logic for you.\nOne pitfall of writing Go sql application is cluttering the code with error-handling and retry.\nI tested in my application with `athenadriver` by turning off and on Wifi and VPN, it works very well with database reconnection.\n\n### Does `athenadriver` support batched query?\n  \nNo. `athenadriver` is an implementation of `sql.driver` in Go `database/sql`, where there is no batch query support.\nThere might be some workaround for some specific case though. For instance, \nif you want to insert many rows, you can use [db.Exec](https://golang.org/pkg/database/sql/#DB.Exec) \nby replacing multiple inserts with one insert and multiple VALUES.\n \n### How to use `athenadriver` to get total row number of result set?\n\nYou have to use `rows.Next()` to iterate all rows and use a counter to get row number. It is because Go `database/sql` was designed in a streaming query way with big data considered. That is why it only supports using `Next()` to iterate. So there is no way for random access of row. In Athena case, we only have random access of all the rows within one result page as the picture shown below:\n\n![Encapsulation of driver.Rows in sql.Rows](resources/sql_Rows.png) \n\nBut due to encapsulation, more sepcifically the `rowsi` is _private_, we\n cannot access it directly like when we using Athena Go SDK. We have to use `Next()` to access it one by one.\n\n### Is there any way to randomly access row with `athenadriver`?\n\nNo. The reason is the same as answer to the previous question.\n\n### Does `athenadriver` support getting the rows affected by my query?\n  \nTo put it simple, YES. But there is some limitation and best practice to follow.\n  \nThe recommended way is to use `DB.Exec()` to get it. Please refer to [ :link: ](#dbexec-and-dbexeccontext).\n\nYou can get it with `DB.Query()` too. In the returned `ResultSet`, there is\n an `UpdateCount` member variable. If the query is one of [`CTAS`](https://docs.aws.amazon.com/athena/latest/ug/ctas.html), `CVAS` and `INSERT INTO`, `UpdateCount` will contain meaningful value. The result will be of a one row and one column. The column name is `rows`, and the row is an `int`, which is exactly `UpdateCount`. I would suggest to use `QueryRow` or `QueryRowContext` since it is a one-row result. By the way, the document for [`GetQueryResults`](https://docs.aws.amazon.com/athena/latest/APIReference/API_GetQueryResults.html) seems not very accurate.\n\n![UpdateCount for CTAS, VTAS, and INSERT INTO](resources/issue_2.png)\n\nIn practice, not only [`CTAS`](https://docs.aws.amazon.com/athena/latest/ug/ctas.html) statement but also `CVAS` and `INSERT INTO` will make a meaningful `UpdateCount`.\n\n## Development Status: Stable\n\nAll APIs are finalized, and no breaking changes will be made in the 1.x series of releases.\n\nThis library is now at version 1 and follows [SemVer](http://semver.org/) strictly.\n\n\n## Contributing\n\nWe encourage and support an active, healthy community of contributors \u0026mdash;\nincluding you! Details are in the [contribution guide](resources/CONTRIBUTING.md) and\nthe [code of conduct](resources/CODE_OF_CONDUCT.md). The athenadriver maintainers keep an eye on\nissues and pull requests, but you can also report any negative conduct to\n[**oss-conduct@uber.com**](oss-conduct@uber.com). That email list is a private, safe space; even the athenadriver\nmaintainers don't have access, so don't hesitate to hold us to a high\nstandard.\n\n\n### `athenadriver` UML Class Diagram\n\nFor the contributors, the following is `athenadriver` Package's UML Class Diagram which may help you to\n understand the code. You can also check the reference section below for some useful materials. \n\n\n![`athenadriver` Package's UML Class Diagram](resources/athenadriver.png)\n\n\n## Reference \n\n- [Amazon Athena User Guide](https://docs.aws.amazon.com/athena/latest/ug/what-is.html)\n- [Amazon Athena API Reference - Describes the Athena API operations in detail.](https://docs.aws.amazon.com/athena/latest/APIReference/Welcome.html)\n- [Amazon Athena Go Doc](https://godoc.org/github.com/aws/aws-sdk-go/service/athena)\n- [Data type mappings that the JDBC driver supports between Athena, JDBC, and Java](https://s3.amazonaws.com/athena-downloads/drivers/JDBC/SimbaAthenaJDBC_2.0.5/docs/Simba+Athena+JDBC+Driver+Install+and+Configuration+Guide.pdf#page=37)\n- [Service Quotas](https://docs.aws.amazon.com/athena/latest/ug/service-limits.html)\n- [Go sql connection pool](http://go-database-sql.org/connection-pool.html)\n- [Common Pitfalls When Using database/sql in Go](https://www.vividcortex.com/blog/2015/09/22/common-pitfalls-go/)\n- [Implement Sql Database Driver in 100 Lines of Go](https://vyskocil.org/blog/implement-sql-database-driver-in-100-lines-of-go/)\n\n## ChangeLog\n\n### v1.1.15 - Merge community contribution (March 03, 2024)\n\n  - Rename S3 bucket in test code (@jonathanbaker7 Jonathan Baker, @henrywoo)\n  - Make poll interval configurable (@keshav-dataco Keshav Murthy)\n  - Add microseconds and nanosecond time format parsing (@Sly1024 Szilveszter Safar)\n  - Add option to return missing values as nil (@kevinwcyu Kevin Yu)\n\n### v1.1.14 - Merge community contribution (August 19, 2022)\n\n  - Adding default AWS SDK credential resolution to connector (@dfreiman-hbo, Dan Freiman)\n  - Bump go-pretty version to most recent version (@nyergler, Nathan Yergler)\n  - Expose DriverTracer factory functions (@andresmgot, Andres Martinez Gotor)\n  - Add support to go 1.17+ (@henrywoo, Henry Fuheng Wu)\n  - README cleanup (@henrywoo, Henry Fuheng Wu)\n\n\n----\n\n💡 `athenadriver` and `athenareader` are created and maintained by [Henry Fuheng Wu](mailto:wufuheng@gmail.com) and brought to you by [Uber Technologies](https://en.wikipedia.org/wiki/Uber).\n\n\n[doc-img]: https://img.shields.io/badge/GoDoc-Reference-red.svg\n[doc]: https://pkg.go.dev/mod/github.com/uber/athenadriver\n\n[cov-img]: https://codecov.io/gh/uber/athenadriver/branch/master/graph/badge.svg\n[cov]: https://codecov.io/gh/uber/athenadriver\n\n[release-img]: https://img.shields.io/badge/release-v1.1.15-red\n[release]: https://github.com/uber/athenadriver/releases\n\n[report-card-img]: https://goreportcard.com/badge/github.com/uber/athenadriver\n[report-card]: https://goreportcard.com/report/github.com/uber/athenadriver\n\n[license-img]: https://img.shields.io/badge/License-Apache--2.0-red\n[license]: https://github.com/uber/athenadriver/blob/master/LICENSE\n\n[fossa-img]: https://app.fossa.com/api/projects/custom%2B4458%2Fgit%40github.com%3Auber%2Fathenadriver.git.svg?type=shield\n[fossa]: https://app.fossa.com/attribution/988b94c8-7015-4f6f-9a19-2ba6b240f992\n\n[release-policy]: https://golang.org/doc/devel/release.html#policy\n\n[made-img]: https://img.shields.io/badge/By-Uber%20Tech-red\n[made]: https://www.uber.com\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuber%2Fathenadriver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuber%2Fathenadriver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuber%2Fathenadriver/lists"}