|
| 1 | +/* |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at: |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0/ |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific |
| 12 | + * language governing permissions and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package org.partiql.eval |
| 16 | + |
| 17 | +import org.partiql.annotations.ExperimentalPartiQLCompilerPipeline |
| 18 | +import org.partiql.annotations.ExperimentalWindowFunctions |
| 19 | +import org.partiql.eval.physical.operators.AggregateOperatorFactoryDefault |
| 20 | +import org.partiql.eval.physical.operators.FilterRelationalOperatorFactoryDefault |
| 21 | +import org.partiql.eval.physical.operators.JoinRelationalOperatorFactoryDefault |
| 22 | +import org.partiql.eval.physical.operators.LetRelationalOperatorFactoryDefault |
| 23 | +import org.partiql.eval.physical.operators.LimitRelationalOperatorFactoryDefault |
| 24 | +import org.partiql.eval.physical.operators.OffsetRelationalOperatorFactoryDefault |
| 25 | +import org.partiql.eval.physical.operators.RelationalOperatorFactory |
| 26 | +import org.partiql.eval.physical.operators.ScanRelationalOperatorFactoryDefault |
| 27 | +import org.partiql.eval.physical.operators.SortOperatorFactoryDefault |
| 28 | +import org.partiql.eval.physical.operators.UnpivotOperatorFactoryDefault |
| 29 | +import org.partiql.eval.physical.operators.WindowRelationalOperatorFactoryDefault |
| 30 | +import org.partiql.lang.eval.ExprFunction |
| 31 | +import org.partiql.lang.eval.ThunkReturnTypeAssertions |
| 32 | +import org.partiql.lang.eval.TypingMode |
| 33 | +import org.partiql.lang.eval.builtins.DynamicLookupExprFunction |
| 34 | +import org.partiql.lang.eval.builtins.SCALAR_BUILTINS_DEFAULT |
| 35 | +import org.partiql.lang.eval.builtins.definitionalBuiltins |
| 36 | +import org.partiql.lang.eval.builtins.storedprocedure.StoredProcedure |
| 37 | +import org.partiql.lang.planner.EvaluatorOptions |
| 38 | +import org.partiql.lang.types.CustomType |
| 39 | + |
| 40 | +/** |
| 41 | + * Builder class to instantiate a [PartiQLCompiler]. |
| 42 | + * |
| 43 | + * Example usages: |
| 44 | + * |
| 45 | + * ``` |
| 46 | + * // Default |
| 47 | + * val compiler = PartiQLCompilerBuilder.standard().build() |
| 48 | + * |
| 49 | + * // Fluent builder |
| 50 | + * val compiler = PartiQLCompilerBuilder.standard() |
| 51 | + * .customFunctions(myCustomFunctionList) |
| 52 | + * .build() |
| 53 | + * ``` |
| 54 | + */ |
| 55 | + |
| 56 | +@ExperimentalPartiQLCompilerPipeline |
| 57 | +class PartiQLCompilerBuilder private constructor() { |
| 58 | + |
| 59 | + private var options: EvaluatorOptions = EvaluatorOptions.standard() |
| 60 | + private var customTypes: List<CustomType> = emptyList() |
| 61 | + private var customFunctions: List<ExprFunction> = emptyList() |
| 62 | + private var customProcedures: List<StoredProcedure> = emptyList() |
| 63 | + private var customOperatorFactories: List<RelationalOperatorFactory> = emptyList() |
| 64 | + |
| 65 | + companion object { |
| 66 | + |
| 67 | + /** |
| 68 | + * A collection of all the default relational operator implementations provided by PartiQL. |
| 69 | + * |
| 70 | + * By default, the query planner will select these as the implementations for all relational operators, but |
| 71 | + * alternate implementations may be provided and chosen by physical plan passes. |
| 72 | + * |
| 73 | + * @see [org.partiql.lang.planner.PlannerPipeline.Builder.addPhysicalPlanPass] |
| 74 | + * @see [org.partiql.lang.planner.PlannerPipeline.Builder.addRelationalOperatorFactory] |
| 75 | + */ |
| 76 | + |
| 77 | + private val DEFAULT_RELATIONAL_OPERATOR_FACTORIES = listOf( |
| 78 | + AggregateOperatorFactoryDefault, |
| 79 | + SortOperatorFactoryDefault, |
| 80 | + UnpivotOperatorFactoryDefault, |
| 81 | + FilterRelationalOperatorFactoryDefault, |
| 82 | + ScanRelationalOperatorFactoryDefault, |
| 83 | + JoinRelationalOperatorFactoryDefault, |
| 84 | + OffsetRelationalOperatorFactoryDefault, |
| 85 | + LimitRelationalOperatorFactoryDefault, |
| 86 | + LetRelationalOperatorFactoryDefault, |
| 87 | + // Notice here we will not propagate the optin requirement to the user |
| 88 | + @OptIn(ExperimentalWindowFunctions::class) |
| 89 | + WindowRelationalOperatorFactoryDefault, |
| 90 | + ) |
| 91 | + |
| 92 | + @JvmStatic |
| 93 | + fun standard() = PartiQLCompilerBuilder() |
| 94 | + } |
| 95 | + |
| 96 | + fun build(): PartiQLCompiler { |
| 97 | + if (options.thunkOptions.thunkReturnTypeAssertions == ThunkReturnTypeAssertions.ENABLED) { |
| 98 | + TODO("ThunkReturnTypeAssertions.ENABLED requires a static type pass") |
| 99 | + } |
| 100 | + return PartiQLCompilerDefault( |
| 101 | + evaluatorOptions = options, |
| 102 | + customTypedOpParameters = customTypes.associateBy( |
| 103 | + keySelector = { it.name }, |
| 104 | + valueTransform = { it.typedOpParameter } |
| 105 | + ), |
| 106 | + functions = allFunctions(options.typingMode), |
| 107 | + procedures = customProcedures.associateBy( |
| 108 | + keySelector = { it.signature.name }, |
| 109 | + valueTransform = { it } |
| 110 | + ), |
| 111 | + operatorFactories = allOperatorFactories() |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + fun options(options: EvaluatorOptions) = this.apply { |
| 116 | + this.options = options |
| 117 | + } |
| 118 | + |
| 119 | + fun customFunctions(customFunctions: List<ExprFunction>) = this.apply { |
| 120 | + this.customFunctions = customFunctions |
| 121 | + } |
| 122 | + |
| 123 | + fun customTypes(customTypes: List<CustomType>) = this.apply { |
| 124 | + this.customTypes = customTypes |
| 125 | + } |
| 126 | + |
| 127 | + fun customProcedures(customProcedures: List<StoredProcedure>) = this.apply { |
| 128 | + this.customProcedures = customProcedures |
| 129 | + } |
| 130 | + |
| 131 | + fun customOperatorFactories(customOperatorFactories: List<RelationalOperatorFactory>) = this.apply { |
| 132 | + this.customOperatorFactories = customOperatorFactories |
| 133 | + } |
| 134 | + |
| 135 | + // --- Internal ---------------------------------- |
| 136 | + |
| 137 | + private fun allFunctions(typingMode: TypingMode): List<ExprFunction> { |
| 138 | + val definitionalBuiltins = definitionalBuiltins(typingMode) |
| 139 | + val builtins = SCALAR_BUILTINS_DEFAULT |
| 140 | + val allFunctions = definitionalBuiltins + builtins + customFunctions + DynamicLookupExprFunction() |
| 141 | + return allFunctions |
| 142 | + } |
| 143 | + |
| 144 | + private fun allOperatorFactories() = (DEFAULT_RELATIONAL_OPERATOR_FACTORIES + customOperatorFactories).apply { |
| 145 | + groupBy { it.key }.entries.firstOrNull { it.value.size > 1 }?.let { |
| 146 | + error( |
| 147 | + "More than one BindingsOperatorFactory for ${it.key.operator} named '${it.value}' was specified." |
| 148 | + ) |
| 149 | + } |
| 150 | + }.associateBy { it.key } |
| 151 | +} |
0 commit comments