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

Add Plan Evaluator #592

Merged
merged 24 commits into from
May 26, 2022
Merged

Conversation

dlurton
Copy link
Member

@dlurton dlurton commented Apr 30, 2022

Yes, there's a failed unit test here. I will need to fix that before this can be merged--that does not however, mean can't
get started reviewing this before then.
EDIT: No longer, this is now fixed.

This was originally part of #582 and is based on #590, which must be merged first.

Compilation of a query plan is now handled by two classes:

  • PhysicalBexprToThunkConverter, which compiles the physical algebra itself.
  • PhysicalExprToThunkConverterImpl, which is a copy and paste of EvaluatingCompiler with:
    • All references to "PartiqlAst" replaced with "PartiqlPhysical"
    • The following changes:
      • Removal of support for the (select ...) node (including the features out of scope for now: ORDER BY, GROUP BY, HAVING, aggregate functions (SUM, MAX, MIN, etc), PIVOT and UNPIVOT).
      • Support for the enhanced (struct ...) constructor (that supports struct unions)
      • The AST's id node removed. (This is handled now by either local_id or global_id, where variables can be resolved at compile time and by the $__dynamic_lookup__(...) ExprFunction if they cannot (and this is enabled).
      • Support for bindings_to_values expression added (see PIG domains for planner and plan evaluator #584)
    • (I'll try go to thru this class and add PR comments to illuminate where these changes are.)

The two classes are mutually recursive. Each invokes the other to compile value or relational expressions as needed.

Why Is the Duplication Needed

The duplication is unfortunate but necessary and can hopefully be removed in the short term. The goal is, once we're confident in the new plan compiler and it has reached feature parity, to delete EvaluatingCompiler. All of the physical plan's node types (value and relational expressions) exist in the PartiqlPhysical domain while the AST's node types exist in the PartiqlAst domain. If this duplication is to persist long term, we might need to look at how to eliminate it (possibly thru a new PIG feature), since maintaining both PhysicalExprToThunkConverterImpl and EvaluatingCompiler long term could get onerous.

Other Notables That Need Reviewing

  • EvaluatorState and its uses. This is the equivalent of Environment for the plan compiler.
  • sequence { }, RelationIterator etc and their uses. Kotlin coroutines are used during relational operator evaluation.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dlurton dlurton changed the base branch from main to physical-plan-staging-planner-pipeline April 30, 2022 02:59
- AST->logical
- logical->resolved
- lesolved->physical.

Not yet integrated with anything--that will come in a future commit.
@dlurton dlurton force-pushed the physical-plan-staging-planner-pipeline branch from cf7f832 to c20b466 Compare May 11, 2022 00:44
@dlurton dlurton force-pushed the physical-plan-staging-planner-pipeline branch from c20b466 to 10ebc0e Compare May 11, 2022 00:47
@dlurton dlurton force-pushed the physical-plan-staging-plan-evaluator branch from e2b4a98 to bb6ebb4 Compare May 11, 2022 00:59
@dlurton dlurton force-pushed the physical-plan-staging-plan-evaluator branch from bb6ebb4 to 3b898db Compare May 11, 2022 01:57
@dlurton dlurton mentioned this pull request May 13, 2022
*
* [1]: https://www.complang.tuwien.ac.at/anton/lvas/sem06w/fest.pdf
*/
internal class PhysicalExprToThunkConverterImpl(
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the copied & revised EvaluatingCompiler. You'll note that the compileSelect() function is gone.

It is not worth reviewing the entire file in detail since most of it's old code with only PartiqlAst replaced with PartiqlPhysical. I'll note the places below that are new, however.

Comment on lines +276 to +290
private fun compileBindingsToValues(expr: PartiqlPhysical.Expr.BindingsToValues): PhysicalPlanThunk {
val mapThunk = compileAstExpr(expr.exp)
val bexprThunk: RelationThunkEnv = PhysicalBexprToThunkConverter(this, thunkFactory.valueFactory)
.convert(expr.query)

return thunkFactory.thunkEnv(expr.metas) { env ->
val elements = sequence {
val relItr = bexprThunk(env)
while (relItr.nextRow()) {
yield(mapThunk(env))
}
}
valueFactory.newBag(elements)
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This compileBindingsToValues is a new function.

Comment on lines +906 to +912
@Suppress("UNUSED_PARAMETER")
private fun compileLocalId(expr: PartiqlPhysical.Expr.LocalId, metas: MetaContainer): PhysicalPlanThunk {
val localIndex = expr.index.value.toIntExact()
return thunkFactory.thunkEnv(metas) { env ->
env.registers[localIndex]
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

compileLocalId is new.

TODO: is there a reason I can't delete the unused metas parameter?

}
}

private fun compileStruct(expr: PartiqlPhysical.Expr.Struct): PhysicalPlanThunk {
Copy link
Member Author

Choose a reason for hiding this comment

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

This function was modified to support the enhanced struct constructors.

}

/**
* Given an AST node that represents a `LIKE` predicate, return an ExprThunk that evaluates a `LIKE` predicate.
Copy link
Member Author

Choose a reason for hiding this comment

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

Given a "plan" node

* Represents an element in a select list that is to be projected into the final result.
* i.e. an expression, or a (project_all) node.
*/
private sealed class CompiledStructPart {
Copy link
Member Author

Choose a reason for hiding this comment

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

This class is new and is part of support for the enhanced struct constructor.

@@ -98,10 +97,17 @@ class EvaluatorTests {
"selectDistinctWithAggregate", // TODO: Support aggregates in physical plans
"selectDistinctAggregationWithGroupBy", // TODO: Support GROUP BY in physical plans
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why, but these were missed in #586


/**
* Provides rules for basic AST sanity checks that should be performed before any attempt at further phsycial
* plan processing. This is provided as a distinct [PartiqlPhysical.Visitor] so that [PlanCompiler] may assume that
Copy link
Member Author

Choose a reason for hiding this comment

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

PlanCompiler is the old name.

/**
* Simple API that defines a method to convert a [PartiqlPhysical.Expr] to a [PhysicalPlanThunk].
*
* Intended to abstract prevent [PhysicalBexprToThunkConverter] from having to take a direct dependency on
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit.: abstract prevent -> prevent?

@dlurton dlurton changed the base branch from physical-plan-staging-planner-pipeline to physical-plan-staging May 25, 2022 18:29
Comment on lines +284 to +285
// customFunctions must be on the right side of + here to ensure that they overwrite any
// built-in functions with the same name.
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this for later but I'm noting—overwrite any built-int—as a design decision that we need to make on the open type system.

import kotlin.test.assertNotEquals
import kotlin.test.assertNull

internal class PlannerPipelineFactory : PipelineFactory {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could use a kdoc here too.

@dlurton dlurton merged commit e0a3519 into physical-plan-staging May 26, 2022
dlurton added a commit that referenced this pull request May 27, 2022
Merges the following PRs to `main`:

- #583 
- #584 
- #587 
- #588 
- #589 
- #609 
- #590 
- #592 

Also, this adds a TODO comment (ea40e4a, requested by @am357 in offline chat) and excludes `PlannerPipeline` from the new aggregate tests which were pulled in when merging from `main` and were not part of the other PRs (34025b3).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
RCHowell pushed a commit that referenced this pull request Jun 1, 2022
Merges the following PRs to `main`:

- #583 
- #584 
- #587 
- #588 
- #589 
- #609 
- #590 
- #592 

Also, this adds a TODO comment (ea40e4a, requested by @am357 in offline chat) and excludes `PlannerPipeline` from the new aggregate tests which were pulled in when merging from `main` and were not part of the other PRs (34025b3).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
RCHowell pushed a commit that referenced this pull request Jun 1, 2022
Merges the following PRs to `main`:

- #583 
- #584 
- #587 
- #588 
- #589 
- #609 
- #590 
- #592 

Also, this adds a TODO comment (ea40e4a, requested by @am357 in offline chat) and excludes `PlannerPipeline` from the new aggregate tests which were pulled in when merging from `main` and were not part of the other PRs (34025b3).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
RCHowell pushed a commit that referenced this pull request Jun 1, 2022
Merges the following PRs to `main`:

- #583 
- #584 
- #587 
- #588 
- #589 
- #609 
- #590 
- #592 

Also, this adds a TODO comment (ea40e4a, requested by @am357 in offline chat) and excludes `PlannerPipeline` from the new aggregate tests which were pulled in when merging from `main` and were not part of the other PRs (34025b3).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
@dlurton dlurton deleted the physical-plan-staging-plan-evaluator branch July 8, 2022 16:41
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.

2 participants