Skip to content

Commit

Permalink
bigquery: make standard SQL the default
Browse files Browse the repository at this point in the history
BREAKING: queries and views will use Standard SQL by default.

Change-Id: Iec52dac7161ef4dc5d1d22744eaa44ab3db62b4d
Reviewed-on: https://code-review.googlesource.com/17210
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Michael Darakananda <pongad@google.com>
  • Loading branch information
jba committed Sep 27, 2017
1 parent adbf666 commit 6c30825
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
5 changes: 3 additions & 2 deletions bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ func TestIntegration_LegacyQuery(t *testing.T) {
}
for _, c := range testCases {
q := client.Query(c.query)
q.UseLegacySQL = true
it, err := q.Read(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1101,11 +1102,11 @@ var useLegacySqlTests = []struct {
}{
{t: legacyName, std: false, legacy: true, err: false},
{t: legacyName, std: true, legacy: false, err: true},
{t: legacyName, std: false, legacy: false, err: false}, // legacy SQL is default
{t: legacyName, std: false, legacy: false, err: true}, // standard SQL is default
{t: legacyName, std: true, legacy: true, err: true},
{t: stdName, std: false, legacy: true, err: true},
{t: stdName, std: true, legacy: false, err: false},
{t: stdName, std: false, legacy: false, err: true}, // legacy SQL is default
{t: stdName, std: false, legacy: false, err: false}, // standard SQL is default
{t: stdName, std: true, legacy: true, err: true},
}

Expand Down
10 changes: 4 additions & 6 deletions bigquery/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ type QueryConfig struct {
// used.
MaxBytesBilled int64

// UseStandardSQL causes the query to use standard SQL.
// The default is false (using legacy SQL).
// UseStandardSQL causes the query to use standard SQL. The default.
UseStandardSQL bool

// UseLegacySQL causes the query to use legacy SQL.
Expand Down Expand Up @@ -189,12 +188,11 @@ func (q *QueryConfig) populateJobQueryConfig(conf *bq.JobConfigurationQuery) err
if len(q.Parameters) > 0 && q.UseLegacySQL {
return errors.New("bigquery: cannot provide both Parameters (implying standard SQL) and UseLegacySQL")
}
if q.UseStandardSQL || len(q.Parameters) > 0 {
conf.UseLegacySql = false
conf.ForceSendFields = append(conf.ForceSendFields, "UseLegacySql")
}
if q.UseLegacySQL {
conf.UseLegacySql = true
} else {
conf.UseLegacySql = false
conf.ForceSendFields = append(conf.ForceSendFields, "UseLegacySql")
}
if q.Dst != nil && !q.Dst.implicitTable() {
conf.DestinationTable = q.Dst.tableRefProto()
Expand Down
22 changes: 18 additions & 4 deletions bigquery/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func defaultQueryJob() *bq.Job {
ProjectId: "def-project-id",
DatasetId: "def-dataset-id",
},
UseLegacySql: false,
ForceSendFields: []string{"UseLegacySql"},
},
},
}
Expand Down Expand Up @@ -260,10 +262,20 @@ func TestQuery(t *testing.T) {
DefaultDatasetID: "def-dataset-id",
UseStandardSQL: true,
},
want: defaultQueryJob(),
},
{
dst: c.Dataset("dataset-id").Table("table-id"),
src: &QueryConfig{
Q: "query string",
DefaultProjectID: "def-project-id",
DefaultDatasetID: "def-dataset-id",
UseLegacySQL: true,
},
want: func() *bq.Job {
j := defaultQueryJob()
j.Configuration.Query.UseLegacySql = false
j.Configuration.Query.ForceSendFields = []string{"UseLegacySql"}
j.Configuration.Query.UseLegacySql = true
j.Configuration.Query.ForceSendFields = nil
return j
}(),
},
Expand Down Expand Up @@ -304,6 +316,8 @@ func TestConfiguringQuery(t *testing.T) {
ProjectId: "def-project-id",
DatasetId: "def-dataset-id",
},
UseLegacySql: false,
ForceSendFields: []string{"UseLegacySql"},
},
},
JobReference: &bq.JobReference{
Expand All @@ -315,8 +329,8 @@ func TestConfiguringQuery(t *testing.T) {
if _, err := query.Run(context.Background()); err != nil {
t.Fatalf("err calling Query.Run: %v", err)
}
if !testutil.Equal(s.Job, want) {
t.Errorf("querying: got:\n%v\nwant:\n%v", s.Job, want)
if diff := testutil.Diff(s.Job, want); diff != "" {
t.Errorf("querying: -got +want:\n%s", diff)
}
}

Expand Down
3 changes: 1 addition & 2 deletions bigquery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,7 @@ func bqTableFromMetadata(tm *TableMetadata) (*bq.Table, error) {
t.View = &bq.ViewDefinition{Query: tm.ViewQuery}
if tm.UseLegacySQL {
t.View.UseLegacySql = true
}
if tm.UseStandardSQL {
} else {
t.View.UseLegacySql = false
t.View.ForceSendFields = append(t.View.ForceSendFields, "UseLegacySql")
}
Expand Down
10 changes: 10 additions & 0 deletions bigquery/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ func TestBQTableFromMetadata(t *testing.T) {
ExpirationTime: aTimeMillis,
},
},
{
&TableMetadata{ViewQuery: "q"},
&bq.Table{
View: &bq.ViewDefinition{
Query: "q",
UseLegacySql: false,
ForceSendFields: []string{"UseLegacySql"},
},
},
},
{
&TableMetadata{
ViewQuery: "q",
Expand Down
5 changes: 3 additions & 2 deletions bigquery/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ type TableMetadata struct {
// The query to use for a view. If provided on create, Schema must be nil.
ViewQuery string

// Use Legacy SQL for the view query. The default.
// Use Legacy SQL for the view query.
// At most one of UseLegacySQL and UseStandardSQL can be true.
UseLegacySQL bool

// Use Legacy SQL for the view query.
// Use Legacy SQL for the view query. The default.
// At most one of UseLegacySQL and UseStandardSQL can be true.
UseStandardSQL bool

// If non-nil, the table is partitioned by time.
Expand Down

0 comments on commit 6c30825

Please sign in to comment.