bigquery: make standard SQL the default

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>
diff --git a/bigquery/integration_test.go b/bigquery/integration_test.go
index 33c5d42..876eb4c 100644
--- a/bigquery/integration_test.go
+++ b/bigquery/integration_test.go
@@ -992,6 +992,7 @@
 	}
 	for _, c := range testCases {
 		q := client.Query(c.query)
+		q.UseLegacySQL = true
 		it, err := q.Read(ctx)
 		if err != nil {
 			t.Fatal(err)
@@ -1101,11 +1102,11 @@
 }{
 	{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},
 }
 
diff --git a/bigquery/query.go b/bigquery/query.go
index 624390a..efc038a 100644
--- a/bigquery/query.go
+++ b/bigquery/query.go
@@ -89,8 +89,7 @@
 	// 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.
@@ -189,12 +188,11 @@
 	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()
diff --git a/bigquery/query_test.go b/bigquery/query_test.go
index 46c82f4..985b63d 100644
--- a/bigquery/query_test.go
+++ b/bigquery/query_test.go
@@ -39,6 +39,8 @@
 					ProjectId: "def-project-id",
 					DatasetId: "def-dataset-id",
 				},
+				UseLegacySql:    false,
+				ForceSendFields: []string{"UseLegacySql"},
 			},
 		},
 	}
@@ -260,10 +262,20 @@
 				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
 			}(),
 		},
@@ -304,6 +316,8 @@
 					ProjectId: "def-project-id",
 					DatasetId: "def-dataset-id",
 				},
+				UseLegacySql:    false,
+				ForceSendFields: []string{"UseLegacySql"},
 			},
 		},
 		JobReference: &bq.JobReference{
@@ -315,8 +329,8 @@
 	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)
 	}
 }
 
diff --git a/bigquery/service.go b/bigquery/service.go
index 7c84a31..122acd8 100644
--- a/bigquery/service.go
+++ b/bigquery/service.go
@@ -542,8 +542,7 @@
 		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")
 		}
diff --git a/bigquery/service_test.go b/bigquery/service_test.go
index bc214ba..ffb192c 100644
--- a/bigquery/service_test.go
+++ b/bigquery/service_test.go
@@ -114,6 +114,16 @@
 			},
 		},
 		{
+			&TableMetadata{ViewQuery: "q"},
+			&bq.Table{
+				View: &bq.ViewDefinition{
+					Query:           "q",
+					UseLegacySql:    false,
+					ForceSendFields: []string{"UseLegacySql"},
+				},
+			},
+		},
+		{
 			&TableMetadata{
 				ViewQuery:        "q",
 				UseLegacySQL:     true,
diff --git a/bigquery/table.go b/bigquery/table.go
index 8c29e89..3a62dbb 100644
--- a/bigquery/table.go
+++ b/bigquery/table.go
@@ -53,11 +53,12 @@
 	// 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.