Skip to content

Commit

Permalink
[release-16.0] handle large number of predicates without timing out (#…
Browse files Browse the repository at this point in the history
…13979) (#13981)

Co-authored-by: Andrés Taylor <[email protected]>
  • Loading branch information
vitess-bot[bot] and systay authored Sep 19, 2023
1 parent b6ce299 commit 2b52532
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 77 deletions.
39 changes: 14 additions & 25 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,29 +989,14 @@ func (node *Select) GetParsedComments() *ParsedComments {
// AddWhere adds the boolean expression to the
// WHERE clause as an AND condition.
func (node *Select) AddWhere(expr Expr) {
if node.Where == nil {
node.Where = &Where{
Type: WhereClause,
Expr: expr,
}
return
}
exprs := SplitAndExpression(nil, node.Where.Expr)
node.Where.Expr = AndExpressions(append(exprs, expr)...)
node.Where = addPredicate(node.Where, expr)
}

// AddHaving adds the boolean expression to the
// HAVING clause as an AND condition.
func (node *Select) AddHaving(expr Expr) {
if node.Having == nil {
node.Having = &Where{
Type: HavingClause,
Expr: expr,
}
return
}
exprs := SplitAndExpression(nil, node.Having.Expr)
node.Having.Expr = AndExpressions(append(exprs, expr)...)
node.Having = addPredicate(node.Having, expr)
node.Having.Type = HavingClause
}

// AddGroupBy adds a grouping expression, unless it's already present
Expand All @@ -1028,17 +1013,21 @@ func (node *Select) AddGroupBy(expr Expr) {
// AddWhere adds the boolean expression to the
// WHERE clause as an AND condition.
func (node *Update) AddWhere(expr Expr) {
if node.Where == nil {
node.Where = &Where{
node.Where = addPredicate(node.Where, expr)
}

func addPredicate(where *Where, pred Expr) *Where {
if where == nil {
return &Where{
Type: WhereClause,
Expr: expr,
Expr: pred,
}
return
}
node.Where.Expr = &AndExpr{
Left: node.Where.Expr,
Right: expr,
where.Expr = &AndExpr{
Left: where.Expr,
Right: pred,
}
return where
}

// AddOrder adds an order by element
Expand Down
61 changes: 25 additions & 36 deletions go/vt/sqlparser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,35 @@ func TestAppend(t *testing.T) {
}

func TestSelect(t *testing.T) {
tree, err := Parse("select * from t where a = 1")
e1, err := ParseExpr("a = 1")
require.NoError(t, err)
expr := tree.(*Select).Where.Expr

sel := &Select{}
sel.AddWhere(expr)
buf := NewTrackedBuffer(nil)
sel.Where.Format(buf)
assert.Equal(t, " where a = 1", buf.String())
sel.AddWhere(expr)
buf = NewTrackedBuffer(nil)
sel.Where.Format(buf)
assert.Equal(t, " where a = 1", buf.String())

sel = &Select{}
sel.AddHaving(expr)
buf = NewTrackedBuffer(nil)
sel.Having.Format(buf)
assert.Equal(t, " having a = 1", buf.String())

sel.AddHaving(expr)
buf = NewTrackedBuffer(nil)
sel.Having.Format(buf)
assert.Equal(t, " having a = 1", buf.String())

tree, err = Parse("select * from t where a = 1 or b = 1")
e2, err := ParseExpr("b = 2")
require.NoError(t, err)
expr = tree.(*Select).Where.Expr
sel = &Select{}
sel.AddWhere(expr)
buf = NewTrackedBuffer(nil)
sel.Where.Format(buf)
assert.Equal(t, " where a = 1 or b = 1", buf.String())
t.Run("single predicate where", func(t *testing.T) {
sel := &Select{}
sel.AddWhere(e1)
assert.Equal(t, " where a = 1", String(sel.Where))
})

sel = &Select{}
sel.AddHaving(expr)
buf = NewTrackedBuffer(nil)
sel.Having.Format(buf)
assert.Equal(t, " having a = 1 or b = 1", buf.String())
t.Run("single predicate having", func(t *testing.T) {
sel := &Select{}
sel.AddHaving(e1)
assert.Equal(t, " having a = 1", String(sel.Having))
})

t.Run("double predicate where", func(t *testing.T) {
sel := &Select{}
sel.AddWhere(e1)
sel.AddWhere(e2)
assert.Equal(t, " where a = 1 and b = 2", String(sel.Where))
})

t.Run("double predicate having", func(t *testing.T) {
sel := &Select{}
sel.AddHaving(e1)
sel.AddHaving(e2)
assert.Equal(t, " having a = 1 and b = 2", String(sel.Having))
})
}

func TestUpdate(t *testing.T) {
Expand Down
31 changes: 31 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions go/vt/vtgate/planbuilder/testdata/info_schema57_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@
"Sharded": false
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"user\"), VARCHAR(\"main\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
Expand All @@ -757,7 +757,7 @@
"Sharded": false
},
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname",
"Query": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"user\"), VARCHAR(\"main\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
Expand Down Expand Up @@ -1219,7 +1219,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\")]",
"Table": "information_schema.`tables`"
},
Expand All @@ -1231,7 +1231,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.views where 1 != 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.views"
}
Expand All @@ -1249,7 +1249,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1 union all (select 1 as found from information_schema.views where 1 != 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1)",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\"), VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.`tables`"
}
Expand All @@ -1272,7 +1272,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\")]",
"Table": "information_schema.`tables`"
},
Expand All @@ -1284,7 +1284,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.views where 1 != 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.views"
}
Expand All @@ -1302,7 +1302,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1 union all (select 1 as found from information_schema.views where 1 != 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1)",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\"), VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.`tables`"
}
Expand Down
16 changes: 8 additions & 8 deletions go/vt/vtgate/planbuilder/testdata/info_schema80_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@
"Sharded": false
},
"FieldQuery": "select * from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname",
"Query": "select * from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"user\"), VARCHAR(\"main\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
Expand All @@ -815,7 +815,7 @@
"Sharded": false
},
"FieldQuery": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where 1 != 1",
"Query": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname",
"Query": "select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, `ENGINE`, VERSION, `ROW_FORMAT`, TABLE_ROWS, `AVG_ROW_LENGTH`, DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, `AUTO_INCREMENT`, CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, `CHECKSUM`, CREATE_OPTIONS, TABLE_COMMENT from INFORMATION_SCHEMA.`TABLES` where TABLE_SCHEMA = :__vtschemaname and TABLE_SCHEMA = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"user\"), VARCHAR(\"main\")]",
"Table": "INFORMATION_SCHEMA.`TABLES`"
}
Expand Down Expand Up @@ -1277,7 +1277,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\")]",
"Table": "information_schema.`tables`"
},
Expand All @@ -1289,7 +1289,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.views where 1 != 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.views"
}
Expand All @@ -1307,7 +1307,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1 union all (select 1 as found from information_schema.views where 1 != 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1)",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\"), VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.`tables`"
}
Expand All @@ -1330,7 +1330,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\")]",
"Table": "information_schema.`tables`"
},
Expand All @@ -1342,7 +1342,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.views where 1 != 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1",
"Query": "select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.views"
}
Expand All @@ -1360,7 +1360,7 @@
"Sharded": false
},
"FieldQuery": "select 1 as found from information_schema.`tables` where 1 != 1 union all (select 1 as found from information_schema.views where 1 != 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname limit 1)",
"Query": "select 1 as found from information_schema.`tables` where table_schema = :__vtschemaname and table_schema = :__vtschemaname union all (select 1 as found from information_schema.views where table_schema = :__vtschemaname and table_schema = :__vtschemaname limit 1)",
"SysTableTableSchema": "[VARCHAR(\"music\"), VARCHAR(\"Music\"), VARCHAR(\"music\"), VARCHAR(\"user\")]",
"Table": "information_schema.`tables`"
}
Expand Down

0 comments on commit 2b52532

Please sign in to comment.