Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions go/cmd/sqlflowserver/e2e_common_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,50 @@ INTO ` + resultTable + `;`
a.True(reflect.DeepEqual(decodedRows[1], []interface{}{"train", int64(1)}))
}

func caseTestOptimizeClauseWithoutConstraint(t *testing.T) {
a := assert.New(t)

dbName := "optimize_test_db"
resultTable := fmt.Sprintf("%s.%s", dbName, "woodcarving_result")

woodCarvingSQL := `SELECT * FROM optimize_test_db.woodcarving
TO MINIMIZE SUM((price - materials_cost - other_cost) * amount)
WITH
variables="amount(product)",
var_type="PositiveIntegers"
USING glpk
INTO ` + resultTable + `;`

_, _, _, err := connectAndRunSQL(woodCarvingSQL)
a.NoError(err)

queryResultSQL := fmt.Sprintf("SELECT product, amount FROM %s;", resultTable)

header, rows, _, err := connectAndRunSQL(queryResultSQL)
header = removeColumnNamePrefix(header)
a.NoError(err)
a.Equal(2, len(header))

a.Equal("product", header[0])
a.Equal("amount", header[1])
a.Equal(2, len(rows))
decodedRows, err := decodeAnyTypedRowData(rows)
a.NoError(err)
a.Equal(len(rows), len(decodedRows))
for i := 0; i < len(decodedRows); i++ {
a.Equal(2, len(decodedRows[i]))
a.IsType("", decodedRows[i][0])
a.IsType(int64(0), decodedRows[i][1])
}

sort.Slice(decodedRows, func(i int, j int) bool {
return decodedRows[i][0].(string) < decodedRows[j][0].(string)
})

a.True(reflect.DeepEqual(decodedRows[0], []interface{}{"soldier", int64(1)}))
a.True(reflect.DeepEqual(decodedRows[1], []interface{}{"train", int64(1)}))
}

func caseTestOptimizeClauseWithGroupBy(t *testing.T) {
a := assert.New(t)

Expand Down
1 change: 1 addition & 0 deletions go/cmd/sqlflowserver/e2e_hive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ func TestEnd2EndHive(t *testing.T) {
t.Run("CaseTestOptimizeClauseWithoutGroupBy", caseTestOptimizeClauseWithoutGroupBy)
t.Run("CaseTestOptimizeClauseWithGroupBy", caseTestOptimizeClauseWithGroupBy)
t.Run("CaseTestOptimizeClauseWithBinaryVarType", caseTestOptimizeClauseWithBinaryVarType)
t.Run("CaseTestOptimizeClauseWithoutConstraint", caseTestOptimizeClauseWithoutConstraint)
}
1 change: 1 addition & 0 deletions go/cmd/sqlflowserver/e2e_mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestEnd2EndMySQL(t *testing.T) {
t.Run("CaseTestOptimizeClauseWithoutGroupBy", caseTestOptimizeClauseWithoutGroupBy)
t.Run("CaseTestOptimizeClauseWithGroupBy", caseTestOptimizeClauseWithGroupBy)
t.Run("CaseTestOptimizeClauseWithBinaryVarType", caseTestOptimizeClauseWithBinaryVarType)
t.Run("CaseTestOptimizeClauseWithoutConstraint", caseTestOptimizeClauseWithoutConstraint)
}

func CaseShouldError(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go/ir/ir_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,8 @@ func GenerateOptimizeStmt(optimizeStmt *parser.SQLFlowSelectStmt) (*OptimizeStmt
ExpressionTokens: optimizeStmt.Objective.ToTokens(),
}

constraints := make([]*OptimizeExpr, len(optimizeStmt.Constrants))
for i, c := range optimizeStmt.Constrants {
constraints := make([]*OptimizeExpr, len(optimizeStmt.Constraints))
for i, c := range optimizeStmt.Constraints {
constraints[i] = &OptimizeExpr{
ExpressionTokens: c.ToTokens(),
GroupBy: c.GroupBy,
Expand Down
45 changes: 26 additions & 19 deletions go/parser/extended_syntax_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type OptimizeClause struct {
// Direction can be MAXIMIZE or MINIMIZE
Direction string
Objective *Expr
Constrants ConstraintList
Constraints ConstraintList
OptimizeAttrs Attributes
Solver string
OptimizeInto string
Expand Down Expand Up @@ -161,6 +161,7 @@ func attrsUnion(as1, as2 Attributes) Attributes {
expl ExprList
ctexp *Constraint
ctexpl ConstraintList
octexpl ConstraintList
atrs Attributes
eslt *SQLFlowSelectStmt
slct StandardSelect
Expand Down Expand Up @@ -190,6 +191,7 @@ func attrsUnion(as1, as2 Attributes) Attributes {
%type <expl> ExprList pythonlist columns
%type <ctexp> constraint
%type <ctexpl> constraint_list
%type <octexpl> optional_constraint_list
%type <atrs> attr
%type <atrs> attrs
%type <tbls> stringlist, identlist
Expand Down Expand Up @@ -323,36 +325,41 @@ run_clause
| TO RUN IDENT CMD stringlist INTO identlist { $$.ImageName = $3; $$.Parameters = $5; $$.OutputTables = $7 }
;

optional_constraint_list
: /* empty */ { $$ = ConstraintList{} }
| CONSTRAINT constraint_list { $$ = $2 }
;

optimize_clause
: TO MAXIMIZE expr CONSTRAINT constraint_list WITH attrs USING IDENT INTO IDENT {
: TO MAXIMIZE expr optional_constraint_list WITH attrs USING IDENT INTO IDENT {
$$.Direction = "MAXIMIZE";
$$.Objective = $3;
$$.Constrants = $5;
$$.OptimizeAttrs = $7;
$$.Solver = $9;
$$.OptimizeInto = $11;
$$.Constraints = $4;
$$.OptimizeAttrs = $6;
$$.Solver = $8;
$$.OptimizeInto = $10;
}
| TO MAXIMIZE expr CONSTRAINT constraint_list WITH attrs INTO IDENT {
| TO MAXIMIZE expr optional_constraint_list WITH attrs INTO IDENT {
$$.Direction = "MAXIMIZE";
$$.Objective = $3;
$$.Constrants = $5;
$$.OptimizeAttrs = $7;
$$.OptimizeInto = $9;
$$.Constraints = $4;
$$.OptimizeAttrs = $6;
$$.OptimizeInto = $8;
}
| TO MINIMIZE expr CONSTRAINT constraint_list WITH attrs USING IDENT INTO IDENT {
| TO MINIMIZE expr optional_constraint_list WITH attrs USING IDENT INTO IDENT {
$$.Direction = "MINIMIZE";
$$.Objective = $3;
$$.Constrants = $5;
$$.OptimizeAttrs = $7;
$$.Solver = $9;
$$.OptimizeInto = $11;
$$.Constraints = $4;
$$.OptimizeAttrs = $6;
$$.Solver = $8;
$$.OptimizeInto = $10;
}
| TO MINIMIZE expr CONSTRAINT constraint_list WITH attrs INTO IDENT {
| TO MINIMIZE expr optional_constraint_list WITH attrs INTO IDENT {
$$.Direction = "MINIMIZE";
$$.Objective = $3;
$$.Constrants = $5;
$$.OptimizeAttrs = $7;
$$.OptimizeInto = $9;
$$.Constraints = $4;
$$.OptimizeAttrs = $6;
$$.OptimizeInto = $8;
};

show_train_clause
Expand Down
4 changes: 2 additions & 2 deletions go/parser/extended_syntax_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ INTO db.table;`
a.True(r.Optimize)
a.Equal("MAXIMIZE", r.Direction)
a.Equal("SUM((price - materials_cost - other_cost) * product)", r.Objective.String())
a.Equal("SUM(finishing * product) <= 100", r.Constrants[0].String())
a.Equal("SUM(finishing * product) <= 100", r.Constraints[0].String())
a.Equal("db.table", r.OptimizeInto)
a.Equal("glpk", r.Solver)

Expand All @@ -259,7 +259,7 @@ INTO db.table;`
a.NoError(e)
a.Equal("MINIMIZE", r.Direction)
a.Equal("db.table", r.OptimizeInto)
a.Equal("product", r.Constrants[0].GroupBy)
a.Equal("product", r.Constraints[0].GroupBy)
a.Equal("", r.Solver)
}

Expand Down