From 4aa06cb4cf0877182d12e3ff2740fae2fb81b6ce Mon Sep 17 00:00:00 2001
From: "takeshi.nakata" <7553415+nktks@users.noreply.github.com>
Date: Tue, 7 Mar 2023 00:31:05 +0900
Subject: [PATCH] fix(spanner/spansql): fix SQL for CREATE CHANGE STREAM
 TableName; case

---
 spanner/spansql/sql.go      | 8 +++++---
 spanner/spansql/sql_test.go | 8 ++++++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/spanner/spansql/sql.go b/spanner/spansql/sql.go
index b05a0c6aae97..000edf11131a 100644
--- a/spanner/spansql/sql.go
+++ b/spanner/spansql/sql.go
@@ -102,12 +102,14 @@ func (cr CreateRole) SQL() string {
 
 func (cs CreateChangeStream) SQL() string {
 	str := "CREATE CHANGE STREAM "
-	str += cs.Name.SQL() + " FOR "
+	str += cs.Name.SQL()
 	if cs.WatchAllTables {
-		str += "ALL"
+		str += " FOR ALL"
 	} else {
 		for i, table := range cs.Watch {
-			if i > 0 {
+			if i == 0 {
+				str += " FOR "
+			} else {
 				str += ", "
 			}
 			str += table.SQL()
diff --git a/spanner/spansql/sql_test.go b/spanner/spansql/sql_test.go
index ca1de52228a1..25c558f702b5 100644
--- a/spanner/spansql/sql_test.go
+++ b/spanner/spansql/sql_test.go
@@ -459,6 +459,14 @@ func TestSQL(t *testing.T) {
 			"ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, optimizer_statistics_package=null, version_retention_period=null, enable_key_visualizer=null, default_leader=null)",
 			reparseDDL,
 		},
+		{
+			&CreateChangeStream{
+				Name:     "csname",
+				Position: line(1),
+			},
+			"CREATE CHANGE STREAM csname",
+			reparseDDL,
+		},
 		{
 			&CreateChangeStream{
 				Name: "csname",