Skip to content

A Golang library for building an SQL query based on a filter represented by a given structure

License

Notifications You must be signed in to change notification settings

arquivei/bigqueryutil

Repository files navigation

BigQueryUtil

A Golang library for building an SQL query based on a filter represented by a given structure.


Table of Contents

BigQueryUtil is a library to build a SQL query and Big Query Parameters based on provided columns, a query template and a filter.

Stack Version
Golang v1.18
golangci-lint v1.45
    • Any Golang programming language version installed, preferred 1.18 or later.
  • go get -u github.com/arquivei/bigqueryutil
    
  • go mod vendor
    go mod tidy
    
    • Import the package

      import (
          "github.com/arquivei/bigqueryutil"
      )
    • Instantiate the QueryBuilderSpec struct that represents the spec for the query builder

      var queryBuilderExample = bigqueryutil.QueryBuilderSpec{
        RepeatedColumns: map[string]struct{}{
          "AccessKey": {},
          "Owner":     {},
        },
        SQLQuery: "SELECT * EXCEPT(r) FROM (SELECT %s, " +
          "ROW_NUMBER() OVER (PARTITION BY AccessKey, Owner order by Version desc) r " +
          "FROM %s WHERE %s) WHERE r = 1;",
      }
    • Declare the TimeRange struct that represents a time with a beginning and an end.

      var TimeRangeExample = bigqueryutil.TimeRange{
        From: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
        To:   time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
      }
    • Declare and instantiante the struct that represents a filter for a query with Big Query's tags.

      type filter struct {
        Namespace string                  `bq:",omitempty"`
        CreatedAt *bigqueryutil.TimeRange `bq:",omitempty"`
        Owners    []string                `bq:"Owner,omitempty"`
        IsTaker   *bool                   `bq:",omitempty"`
      }
      
      var filterExample = filter{
        Namespace: "namespace",
        CreatedAt: &TimeRangeExample,
        Owners:    []string{"owner1", "owner2"},
        IsTaker:   ref.Bool(false),
      }
    • Declare the projection fields that will be required field on the HTTP API.

      var projectionExample = []string{"AccessKey"}
    • Instantiate the BuildColumnsClause that will return the string that represents the columnsClauseBuilder with all columns required by projection, or "*" if projection is nil.

      returnedColumns := bigqueryutil.BuildColumnsClause(queryBuilderExample,projectionExample)
    • Instantiate the EncodeBigqueryWhereClause that transforms a struct into a bigquery's query and parameters list.

      whereExample, queryParametersExample, err := bigqueryutil.EncodeBigqueryWhereClause(filterExample)
      if err != nil {
        panic(err)
      }
    • These parameters will be passed to Big Query and will be used in the query.

      fmt.Printf("Big Query Parameters: \n%+v\n\n", queryParametersExample)
    • The query is the string that will be used in the BigQuery.

      sqlQuery := fmt.Sprintf(queryBuilderExample.SQLQuery, returnedColumns, "`TABLE_EXAMPLE`", whereExample)
      fmt.Printf("Sql Query: \n%+v\n", sqlQuery)
    • Output

      /*
         Big Query Parameters:
         [{Name:Namespace Value:namespace} {Name:CreatedAtFrom Value:2022-01-01T00:00:00Z} {Name:CreatedAtTo Value:2022-02-01T00:00:00Z} {Name:Owner0 Value:owner1} {Name:Owner1 Value:owner2}]
      
         Sql Query:
         SELECT * EXCEPT(r) FROM (SELECT AccessKey, ROW_NUMBER() OVER (PARTITION BY AccessKey, Owner order by Version desc) r FROM `TABLE_EXAMPLE` WHERE Namespace = @Namespace AND CreatedAt BETWEEN @CreatedAtFrom AND @CreatedAtTo AND Owner IN (@Owner0,@Owner1) AND NOT IsTaker) WHERE r = 1;
      */    
  • bigqueryutil 0.1.0 (DATE)

    • [New] Decoupling this package from Arquivei's API projects.
    • [New] Setting github's workflow with golangci-lint
    • [New] Example for usage.
    • [New] Documents: Code of Conduct, Contributing, License and Readme.

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

We use Semantic Versioning for versioning. For the versions available, see the tags on this repository.

This project is licensed under the BSD 3-Clause - see the LICENSE.md file for details.

Contacts can be made by email: rilder.almeida@arquivei.com.br

About

A Golang library for building an SQL query based on a filter represented by a given structure

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages