Skip to content

Commit

Permalink
Renames PhysicalExprToThunkConverter as PhysicalPlanCompiler (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchowell authored Sep 26, 2022
1 parent 706acd9 commit 2e19331
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
10 changes: 5 additions & 5 deletions lang/src/org/partiql/lang/compiler/PartiQLCompilerDefault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import org.partiql.lang.eval.PartiQLResult
import org.partiql.lang.eval.PartiQLStatement
import org.partiql.lang.eval.builtins.storedprocedure.StoredProcedure
import org.partiql.lang.eval.physical.PhysicalBexprToThunkConverter
import org.partiql.lang.eval.physical.PhysicalExprToThunkConverter
import org.partiql.lang.eval.physical.PhysicalExprToThunkConverterImpl
import org.partiql.lang.eval.physical.PhysicalPlanCompiler
import org.partiql.lang.eval.physical.PhysicalPlanCompilerImpl
import org.partiql.lang.eval.physical.PhysicalPlanThunk
import org.partiql.lang.eval.physical.operators.RelationalOperatorFactory
import org.partiql.lang.eval.physical.operators.RelationalOperatorFactoryKey
Expand All @@ -47,17 +47,17 @@ internal class PartiQLCompilerDefault(
private val operatorFactories: Map<RelationalOperatorFactoryKey, RelationalOperatorFactory>
) : PartiQLCompiler {

private lateinit var exprConverter: PhysicalExprToThunkConverterImpl
private lateinit var exprConverter: PhysicalPlanCompilerImpl
private val bexprConverter = PhysicalBexprToThunkConverter(
valueFactory = this.valueFactory,
exprConverter = object : PhysicalExprToThunkConverter {
exprConverter = object : PhysicalPlanCompiler {
override fun convert(expr: PartiqlPhysical.Expr): PhysicalPlanThunk = exprConverter.convert(expr)
},
relationalOperatorFactory = operatorFactories
)

init {
exprConverter = PhysicalExprToThunkConverterImpl(
exprConverter = PhysicalPlanCompilerImpl(
valueFactory = valueFactory,
functions = functions,
customTypedOpParameters = customTypedOpParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal typealias PhysicalPlanThunk = Thunk<EvaluatorState>
internal typealias PhysicalPlanThunkValue<T> = ThunkValue<EvaluatorState, T>

internal class PhysicalBexprToThunkConverter(
private val exprConverter: PhysicalExprToThunkConverter,
private val exprConverter: PhysicalPlanCompiler,
private val valueFactory: ExprValueFactory,
private val relationalOperatorFactory: Map<RelationalOperatorFactoryKey, RelationalOperatorFactory>
) : PartiqlPhysical.Bexpr.Converter<RelationThunkEnv> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import org.partiql.lang.domains.PartiqlPhysical
* Intended to prevent [PhysicalBexprToThunkConverter] from having to take a direct dependency on
* [org.partiql.lang.eval.EvaluatingCompiler].
*/
internal interface PhysicalExprToThunkConverter {
internal interface PhysicalPlanCompiler {
fun convert(expr: PartiqlPhysical.Expr): PhysicalPlanThunk
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ import java.math.BigDecimal
import java.util.LinkedList
import java.util.TreeSet
import java.util.regex.Pattern
import kotlin.collections.ArrayList

/**
* A basic "compiler" that converts an instance of [PartiqlPhysical.Expr] to an [Expression].
Expand All @@ -132,14 +131,14 @@ import kotlin.collections.ArrayList
*
* [1]: https://www.complang.tuwien.ac.at/anton/lvas/sem06w/fest.pdf
*/
internal class PhysicalExprToThunkConverterImpl(
internal class PhysicalPlanCompilerImpl(
private val valueFactory: ExprValueFactory,
private val functions: Map<String, ExprFunction>,
private val customTypedOpParameters: Map<String, TypedOpParameter>,
private val procedures: Map<String, StoredProcedure>,
private val evaluatorOptions: EvaluatorOptions = EvaluatorOptions.standard(),
private val bexperConverter: PhysicalBexprToThunkConverter,
) : PhysicalExprToThunkConverter {
) : PhysicalPlanCompiler {
private val errorSignaler = evaluatorOptions.typingMode.createErrorSignaler(valueFactory)
private val thunkFactory = evaluatorOptions.typingMode.createThunkFactory<EvaluatorState>(
evaluatorOptions.thunkOptions,
Expand Down
36 changes: 20 additions & 16 deletions lang/src/org/partiql/lang/planner/PlannerPipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import org.partiql.lang.eval.builtins.DynamicLookupExprFunction
import org.partiql.lang.eval.builtins.createBuiltinFunctions
import org.partiql.lang.eval.builtins.storedprocedure.StoredProcedure
import org.partiql.lang.eval.physical.PhysicalBexprToThunkConverter
import org.partiql.lang.eval.physical.PhysicalExprToThunkConverter
import org.partiql.lang.eval.physical.PhysicalExprToThunkConverterImpl
import org.partiql.lang.eval.physical.PhysicalPlanCompiler
import org.partiql.lang.eval.physical.PhysicalPlanCompilerImpl
import org.partiql.lang.eval.physical.PhysicalPlanThunk
import org.partiql.lang.eval.physical.operators.DEFAULT_RELATIONAL_OPERATOR_FACTORIES
import org.partiql.lang.eval.physical.operators.RelationalOperatorFactory
Expand Down Expand Up @@ -171,7 +171,8 @@ interface PlannerPipeline {

/** Kotlin style builder for [PlannerPipeline]. If calling from Java instead use [builder]. */
@Deprecated(WARNING)
fun build(valueFactory: ExprValueFactory, block: Builder.() -> Unit) = Builder(valueFactory).apply(block).build()
fun build(valueFactory: ExprValueFactory, block: Builder.() -> Unit) =
Builder(valueFactory).apply(block).build()

/** Fluent style builder. If calling from Kotlin instead use the [build] method. */
@JvmStatic
Expand Down Expand Up @@ -380,7 +381,8 @@ interface PlannerPipeline {
val compileOptionsToUse = evaluatorOptions ?: EvaluatorOptions.standard()

when (compileOptionsToUse.thunkOptions.thunkReturnTypeAssertions) {
ThunkReturnTypeAssertions.DISABLED -> { /* take no action */ }
ThunkReturnTypeAssertions.DISABLED -> { /* take no action */
}
ThunkReturnTypeAssertions.ENABLED -> error(
"TODO: Support ThunkReturnTypeAssertions.ENABLED " +
"need a static type pass first)"
Expand All @@ -389,14 +391,15 @@ interface PlannerPipeline {

// check for duplicate operator factories. Unlike [ExprFunctions], we do not allow the default
// operator implementations to be overridden.
val allPhysicalOperatorFactories = (DEFAULT_RELATIONAL_OPERATOR_FACTORIES + physicalOperatorFactories).apply {
groupBy { it.key }.entries.firstOrNull { it.value.size > 1 }?.let {
throw IllegalArgumentException(
"More than one BindingsOperatorFactory for ${it.key.operator} " +
"named '${it.value}' was specified."
)
val allPhysicalOperatorFactories =
(DEFAULT_RELATIONAL_OPERATOR_FACTORIES + physicalOperatorFactories).apply {
groupBy { it.key }.entries.firstOrNull { it.value.size > 1 }?.let {
throw IllegalArgumentException(
"More than one BindingsOperatorFactory for ${it.key.operator} " +
"named '${it.value}' was specified."
)
}
}
}

val builtinFunctions = createBuiltinFunctions(valueFactory) + DynamicLookupExprFunction()
val builtinFunctionsMap = builtinFunctions.associateBy {
Expand All @@ -408,7 +411,8 @@ interface PlannerPipeline {
val allFunctionsMap = builtinFunctionsMap + customFunctions
return PlannerPipelineImpl(
valueFactory = valueFactory,
parser = parser ?: PartiQLParserBuilder().ionSystem(valueFactory.ion).customTypes(this.customDataTypes).build(),
parser = parser ?: PartiQLParserBuilder().ionSystem(valueFactory.ion).customTypes(this.customDataTypes)
.build(),
evaluatorOptions = compileOptionsToUse,
functions = allFunctionsMap,
customDataTypes = customDataTypes,
Expand Down Expand Up @@ -540,20 +544,20 @@ internal class PlannerPipelineImpl(

override fun compile(physicalPlan: PartiqlPhysical.Plan): PlannerPassResult<QueryPlan> =
plannerEventCallback.doEvent("compile", physicalPlan) {
// PhysicalBExprToThunkConverter and PhysicalExprToThunkConverterImpl are mutually recursive therefore
// PhysicalBExprToThunkConverter and PhysicalPlanCompilerImpl are mutually recursive therefore
// we have to fall back on mutable variables to allow them to reference each other.
var exprConverter: PhysicalExprToThunkConverterImpl? = null
var exprConverter: PhysicalPlanCompilerImpl? = null

val bexperConverter = PhysicalBexprToThunkConverter(
valueFactory = this.valueFactory,
exprConverter = object : PhysicalExprToThunkConverter {
exprConverter = object : PhysicalPlanCompiler {
override fun convert(expr: PartiqlPhysical.Expr): PhysicalPlanThunk =
exprConverter!!.convert(expr)
},
relationalOperatorFactory = bindingsOperatorFactories
)

exprConverter = PhysicalExprToThunkConverterImpl(
exprConverter = PhysicalPlanCompilerImpl(
valueFactory = valueFactory,
functions = functions,
customTypedOpParameters = customTypedOpParameters,
Expand Down

0 comments on commit 2e19331

Please sign in to comment.