@@ -17,6 +17,7 @@ import (
17
17
"bytes"
18
18
"encoding/json"
19
19
"fmt"
20
+ "io/ioutil"
20
21
"os"
21
22
"os/exec"
22
23
"reflect"
@@ -46,8 +47,6 @@ func fillEnvFromSession(envs *map[string]string, session *pb.Session) {
46
47
fillMapIfValueNotEmpty (* envs , "SQLFLOW_HADOOP_USER" , session .HdfsUser )
47
48
fillMapIfValueNotEmpty (* envs , "SQLFLOW_HADOOP_PASS" , session .HdfsUser )
48
49
fillMapIfValueNotEmpty (* envs , "SQLFLOW_submitter" , session .Submitter )
49
- fillMapIfValueNotEmpty (* envs , "SQLFLOW_WORKFLOW_STEP_LOG_FILE" , os .Getenv ("SQLFLOW_WORKFLOW_STEP_LOG_FILE" ))
50
- fillMapIfValueNotEmpty (* envs , "SQLFLOW_WORKFLOW_EXIT_TIME_WAIT" , os .Getenv ("SQLFLOW_WORKFLOW_EXIT_TIME_WAIT" ))
51
50
}
52
51
53
52
// GetStepEnvs returns a map of envs used for couler workflow.
@@ -100,6 +99,14 @@ func GetSecret() (string, string, error) {
100
99
return name , string (value ), nil
101
100
}
102
101
102
+ func escapeStepSQL (sql string ) string {
103
+ sql = strings .Replace (sql , `\` , `\\\` , - 1 )
104
+ sql = strings .Replace (sql , `"` , `\\\"` , - 1 )
105
+ sql = strings .Replace (sql , "`" , "\\ `" , - 1 )
106
+ sql = strings .Replace (sql , "$" , "\\ $" , - 1 )
107
+ return sql
108
+ }
109
+
103
110
// GenFiller generates Filler to fill the template
104
111
func GenFiller (programIR []ir.SQLFlowStmt , session * pb.Session ) (* Filler , error ) {
105
112
stepEnvs , err := GetStepEnvs (session )
@@ -123,13 +130,14 @@ func GenFiller(programIR []ir.SQLFlowStmt, session *pb.Session) (*Filler, error)
123
130
stepLogFile := os .Getenv ("SQLFLOW_WORKFLOW_STEP_LOG_FILE" )
124
131
125
132
r := & Filler {
126
- DataSource : session .DbConnStr ,
127
- StepEnvs : stepEnvs ,
128
- WorkflowTTL : workflowTTL ,
129
- SecretName : secretName ,
130
- SecretData : secretData ,
131
- Resources : os .Getenv (envResource ),
132
- StepLogFile : stepLogFile ,
133
+ DataSource : session .DbConnStr ,
134
+ StepEnvs : stepEnvs ,
135
+ WorkflowTTL : workflowTTL ,
136
+ SecretName : secretName ,
137
+ SecretData : secretData ,
138
+ Resources : os .Getenv (envResource ),
139
+ StepLogFile : stepLogFile ,
140
+ ClusterConfigFn : os .Getenv ("SQLFLOW_WORKFLOW_CLUSTER_CONFIG" ),
133
141
}
134
142
// NOTE(yancey1989): does not use ModelImage here since the Predict statement
135
143
// does not contain the ModelImage field in SQL Program IR.
@@ -138,11 +146,12 @@ func GenFiller(programIR []ir.SQLFlowStmt, session *pb.Session) (*Filler, error)
138
146
}
139
147
140
148
for _ , sqlIR := range programIR {
149
+ escapedSQL := escapeStepSQL (sqlIR .GetOriginalSQL ())
141
150
switch i := sqlIR .(type ) {
142
151
case * ir.NormalStmt , * ir.PredictStmt , * ir.ExplainStmt , * ir.EvaluateStmt :
143
152
// TODO(typhoonzero): get model image used when training.
144
153
sqlStmt := & sqlStatement {
145
- OriginalSQL : sqlIR . GetOriginalSQL () , IsExtendedSQL : sqlIR .IsExtended (),
154
+ OriginalSQL : escapedSQL , IsExtendedSQL : sqlIR .IsExtended (),
146
155
DockerImage : defaultDockerImage }
147
156
r .SQLStatements = append (r .SQLStatements , sqlStmt )
148
157
case * ir.TrainStmt :
@@ -158,20 +167,20 @@ func GenFiller(programIR []ir.SQLFlowStmt, session *pb.Session) (*Filler, error)
158
167
r .SQLStatements = append (r .SQLStatements , sqlStmt )
159
168
} else {
160
169
sqlStmt := & sqlStatement {
161
- OriginalSQL : sqlIR . GetOriginalSQL () , IsExtendedSQL : sqlIR .IsExtended (),
170
+ OriginalSQL : escapedSQL , IsExtendedSQL : sqlIR .IsExtended (),
162
171
DockerImage : stepImage }
163
172
r .SQLStatements = append (r .SQLStatements , sqlStmt )
164
173
}
165
174
case * ir.ShowTrainStmt , * ir.OptimizeStmt :
166
175
sqlStmt := & sqlStatement {
167
- OriginalSQL : sqlIR . GetOriginalSQL () ,
176
+ OriginalSQL : escapedSQL ,
168
177
IsExtendedSQL : sqlIR .IsExtended (),
169
178
DockerImage : defaultDockerImage ,
170
179
}
171
180
r .SQLStatements = append (r .SQLStatements , sqlStmt )
172
181
case * ir.RunStmt :
173
182
sqlStmt := & sqlStatement {
174
- OriginalSQL : sqlIR . GetOriginalSQL () ,
183
+ OriginalSQL : escapedSQL ,
175
184
IsExtendedSQL : sqlIR .IsExtended (),
176
185
DockerImage : i .ImageName ,
177
186
Select : i .Select ,
@@ -199,16 +208,15 @@ func GenCode(programIR []ir.SQLFlowStmt, session *pb.Session) (string, error) {
199
208
200
209
// GenYAML translate the Couler program into Argo YAML
201
210
func GenYAML (coulerProgram string ) (string , error ) {
202
- cmdline := bytes.Buffer {}
203
- fmt .Fprintf (& cmdline , "couler run --mode argo --workflow_name sqlflow " )
204
- if c := os .Getenv ("SQLFLOW_WORKFLOW_CLUSTER_CONFIG" ); len (c ) > 0 {
205
- fmt .Fprintf (& cmdline , "--cluster_config %s " , c )
211
+ file , e := ioutil .TempFile ("/tmp" , "sqlflow.py" )
212
+ if e != nil {
213
+ return "" , e
206
214
}
207
- fmt . Fprintf ( & cmdline , "-- file -" )
208
-
209
- coulerExec := strings . Split ( cmdline . String (), " " )
210
- // execute command: `cat sqlflow.couler | couler run --mode argo --workflow_name sqlflow --file -`
211
- cmd := exec .Command (coulerExec [ 0 ], coulerExec [ 1 :] ... )
215
+ defer os . Remove ( file . Name () )
216
+ if e := ioutil . WriteFile ( file . Name (), [] byte ( coulerProgram ), 0644 ); e != nil {
217
+ return "" , e
218
+ }
219
+ cmd := exec .Command ("python" , file . Name () )
212
220
cmd .Env = append (os .Environ ())
213
221
cmd .Stdin = strings .NewReader (coulerProgram )
214
222
out , err := cmd .CombinedOutput ()
0 commit comments