// Create prediction table using the `PredictStmt`.
Create prediction table using the `PredictStmt`.
// Create prediction table using the ` PredictStmt ` .
func createPredictionResultTable(predStmt *ir.PredictStmt, db *database.DB, session *pb.Session) error {
	dropStmt := fmt.Sprintf("drop table if exists %s;", predStmt.ResultTable)
	if _, e := db.Exec(dropStmt); e != nil {
		return fmt.Errorf("failed executing %s: %q", dropStmt, e)
	}
	flds, fts, e := getSQLFieldType(predStmt.Select, db)
	if e != nil {
		return e
	}
	var b bytes.Buffer
	trainLabelColumn := ""
	if predStmt.TrainStmt != nil {
		trainLabelColumn = predStmt.TrainStmt.Label.GetFieldDesc()[0].Name
	}
	resultColumnName := predStmt.ResultColumn
	resultColumnType := ""
	fmt.Fprintf(&b, "create table %s (", predStmt.ResultTable)
	for idx, colType := range fts {
		stype, e := fieldType(db.DriverName, colType)
		if e != nil {
			return e
		}
		fldName := flds[idx]
		if fldName == trainLabelColumn {
			resultColumnType = stype
			if resultColumnName == trainLabelColumn {
				continue
			}
		}
		if fldName == resultColumnName {
			resultColumnType = stype
			continue
		}
		fmt.Fprintf(&b, "%s %s, ", fldName, stype)
	}
	 label column not found in predict table, create a column specified by PREDICT clause:
	if resultColumnType == "" {
		 NOTE(typhoonzero): Clustering model may not have label in select statement, default use INT type
		resultColumnType = "INT"
	}
	 mapping to DBMS column type from the result column type
	stype, e := fieldType(db.DriverName, resultColumnType)
	if e != nil {
		return fmt.Errorf("mapping to DBMS column type failed, %s", e)
	}
	if db.DriverName == "hive" {
		fmt.Fprintf(&b, "%s %s) ROW FORMAT DELIMITED FIELDS TERMINATED BY \"\\001\" STORED AS TEXTFILE;", resultColumnName, stype)
	} else {
		fmt.Fprintf(&b, "%s %s);", resultColumnName, stype)
	}
	createStmt := b.String()
	if _, e := db.Exec(createStmt); e != nil {
		return fmt.Errorf("failed executing %s: %q", createStmt, e)
	}
	return nil
}
func createPredictionResultTable ( predStmt * ir . PredictStmt , db * database . DB , session * pb . Session ) error { dropStmt := fmt . Sprintf ( " " , predStmt . ResultTable ) if _ , e := db . Exec ( dropStmt ) ; e != nil { return fmt . Errorf ( " " , dropStmt , e ) } flds , fts , e := getSQLFieldType ( predStmt . Select , db ) if e != nil { return e } var b bytes . Buffer trainLabelColumn := " " if predStmt . TrainStmt != nil { trainLabelColumn = predStmt . TrainStmt . Label . GetFieldDesc ( ) [ 0 ] . Name } resultColumnName := predStmt . ResultColumn resultColumnType := " " fmt . Fprintf ( & b , " " , predStmt . ResultTable ) for idx , colType := range fts { stype , e := fieldType ( db . DriverName , colType ) if e != nil { return e } fldName := flds [ idx ] if fldName == trainLabelColumn { resultColumnType = stype if resultColumnName == trainLabelColumn { continue } } if fldName == resultColumnName { resultColumnType = stype continue } fmt . Fprintf ( & b , " " , fldName , stype ) } if resultColumnType == " " { resultColumnType = " " } stype , e := fieldType ( db . DriverName , resultColumnType ) if e != nil { return fmt . Errorf ( " " , e ) } if db . DriverName == " " { fmt . Fprintf ( & b , " \" \\ \" " , resultColumnName , stype ) } else { fmt . Fprintf ( & b , " " , resultColumnName , stype ) } createStmt := b . String ( ) if _ , e := db . Exec ( createStmt ) ; e != nil { return fmt . Errorf ( " " , createStmt , e ) } return nil }
Create prediction table using the `PredictStmt`.
Create prediction table using the ` PredictStmt ` .
// NOTE(typhoonzero): predStmt.TrainStmt may be nil, because the model may not loaded when // creating prediction table. // When predicting use validation table, we should find the label column type // using the label column name from train table. // result column have the same name, do not add as feature column // TODO(Yancey1989): For the current implementation, the prediction result column // type is derivated by the pred-select-statement, the better way is derivating // the result column type by the prediction result. // // label column not found in predict table, create a column specified by PREDICT clause: // NOTE(typhoonzero): Clustering model may not have label in select statement, default use INT type // mapping to DBMS column type from the result column type