Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix applying bindvars to filters and table functions #1230

Merged
merged 34 commits into from
Sep 7, 2022
Merged

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Aug 31, 2022

Changes:

  • Allow bindvars to be applied to FilteredTables using a new DeferredFilteredTable node
  • No longer pushdown filters in post prepared analysis

Test PR:

Copy link
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much there but couple concerns.

@@ -387,6 +387,7 @@ func prePrepareRuleSelector(id RuleId) bool {
case
// OnceBeforeDefault
reresolveTablesId,
validatePriviledgesId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're skipping privilege checks in pre and post?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to just skip in pre, will add it back in post.
I did this because the analyzer would call CheckPrivileges on an unresolved TableFunction in pre, and give panics.

@@ -49,6 +49,18 @@ func resolveTableFunctions(ctx *sql.Context, a *Analyzer, n sql.Node, _ *Scope,
database = privilegedDatabase.Unwrap()
}

var hasBindVarArgs bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would do at least some of the work upfront to frontload/prepare analyzer logic. This makes this rule more expensive and we run it a whole second time. Not the end of the world but we can't find and swap table functions in ApplyBindings? Or were error tests failing because of invalid argument types? In which case the table function resolve and semantic validation should maybe be split into different rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by swap table functions?
The tests were failing because of invalid argument types (i think); the BindVars never get replaced, so we try to evaluate them during pre, leading to panics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops meant arguments, can we not go and replace the arguments in ApplyBindings

Copy link
Contributor Author

@jycor jycor Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do in the code I added in plan/bindvar.go

I think this does the same thing as the DeferredTable; the first pass leaves it unresolved, and it should work normally in the second pass.

@@ -92,6 +92,31 @@ func applyBindingsHelper(n sql.Node, bindings map[string]sql.Expression) (sql.No
}
ne, _, err := transform.NodeExprs(n.WithSource(newSource), fixBindingsTransform)
return ne, transform.NewTree, err
case *ResolvedTable:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like calling these ResolvedTable is misleading, like they will panic if you try to read them. I know we talked about something like DeferredFilteredTable, is that not an easy add to avoid the confusion?

Copy link
Contributor Author

@jycor jycor Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible, but I think it is definitely a bit harder.

DeferredTable is created in resolveTable (in BeforeDefault), but during that time filters haven't yet been evaluated, so I can't save them there.
I could add a new rule right before pushdownFilters that prevents the filters from being pusheddown? and then applyBindings might just work?
or I could add a new rule/modify pushdownFilters (in AfterDefault), to create a DeferredTable there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithFilters has one use in pushdown. I think a helper function should look for a BindVar in the WithFilters argument, and we switch on the result to make either a ResolvedTable or DeferredFiltersTable. Deferred table will implemented UnresolvedTable and look a lot like DeferredAsOf, and will be a target for bindvar replacement.

Copy link
Contributor

@max-hoffman max-hoffman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM nice work! Couple cleanup comments and i'd suggest running against both Dolt format enginetests before merging (it looks like you sent me PRs earlier but good to confirm)

@@ -42,6 +42,8 @@ func GetDeletable(node sql.Node) (sql.DeletableTable, error) {
return GetDeletable(node.ResolvedTable)
case *ResolvedTable:
return getDeletableTable(node.Table)
//case *DeferredFilteredTable:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stale

@@ -70,7 +70,7 @@ func applyBindingsHelper(n sql.Node, bindings map[string]sql.Expression) (sql.No
fixBindingsTransform := func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
return fixBindings(e, bindings)
}
return transform.NodeWithOpaque(n, func(node sql.Node) (sql.Node, transform.TreeIdentity, error) {
node, same, err := transform.NodeWithOpaque(n, func(node sql.Node) (sql.Node, transform.TreeIdentity, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably reset back to the return?

return pushdownFiltersAtNode(ctx, a, n, scope, sel)
node, same, err := pushdownFiltersAtNode(ctx, a, n, scope, sel)
var hasBindVars bool
transform.Inspect(n, func(node sql.Node) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you use this elsewhere, maybe a good candidate for a helper method filtersHasBindvar, or something similar


// OnceAfterDefault
pushdownFiltersId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@jycor jycor changed the title fix applying bindvars to filters fix applying bindvars to filters and table functions Sep 7, 2022
@jycor jycor merged commit f1bd8e0 into main Sep 7, 2022
@Hydrocharged Hydrocharged deleted the james/prepareds4 branch October 13, 2022 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants