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

Make ThunkFactory generic #587

Merged
merged 13 commits into from
May 10, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ParserErrorExample(out: PrintStream) : Example(out) {

throw Exception("ParserException was not thrown")
} catch (e: ParserException) {
val errorContext = e.errorContext!!
val errorContext = e.errorContext

val errorInformation = "errorCode: ${e.errorCode}" +
"\nLINE_NUMBER: ${errorContext[Property.LINE_NUMBER]}" +
Expand Down
303 changes: 303 additions & 0 deletions lang/resources/org/partiql/type-domains/partiql.ion

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/CompilerPipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ interface CompilerPipeline {
fun build(): CompilerPipeline {
val compileOptionsToUse = compileOptions ?: CompileOptions.standard()

when (compileOptionsToUse.thunkReturnTypeAssertions) {
when (compileOptionsToUse.thunkOptions.thunkReturnTypeAssertions) {
ThunkReturnTypeAssertions.DISABLED -> { /* intentionally blank */ }
ThunkReturnTypeAssertions.ENABLED -> {
check(this.globalTypeBindings != null) {
Expand Down
8 changes: 5 additions & 3 deletions lang/src/org/partiql/lang/SqlException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.partiql.lang.errors.ErrorCode
import org.partiql.lang.errors.Property
import org.partiql.lang.errors.PropertyValueMap
import org.partiql.lang.errors.UNKNOWN
import org.partiql.lang.util.propertyValueMapOf

/**
* General exception class for the interpreter.
Expand All @@ -43,10 +44,11 @@ import org.partiql.lang.errors.UNKNOWN
open class SqlException(
override var message: String,
val errorCode: ErrorCode,
val errorContext: PropertyValueMap? = null,
errorContextArg: PropertyValueMap? = null,
Copy link
Member

Choose a reason for hiding this comment

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

kdoc for this parameter needs to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

cause: Throwable? = null
) :
RuntimeException(message, cause) {
) : RuntimeException(message, cause) {

val errorContext: PropertyValueMap = errorContextArg ?: propertyValueMapOf()

/**
* Indicates if this exception is due to an internal error or not.
Expand Down
6 changes: 3 additions & 3 deletions lang/src/org/partiql/lang/eval/CompileOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ enum class ThunkReturnTypeAssertions {
* @param defaultTimezoneOffset Default timezone offset to be used when TIME WITH TIME ZONE does not explicitly
* specify the time zone. Defaults to [ZoneOffset.UTC]
*/
@Suppress("DataClassPrivateConstructor")
data class CompileOptions private constructor (
val undefinedVariable: UndefinedVariableBehavior,
val projectionIteration: ProjectionIterationBehavior = ProjectionIterationBehavior.FILTER_MISSING,
val visitorTransformMode: VisitorTransformMode = VisitorTransformMode.DEFAULT,
val thunkOptions: ThunkOptions = ThunkOptions.standard(),
val typingMode: TypingMode = TypingMode.LEGACY,
val typedOpBehavior: TypedOpBehavior = TypedOpBehavior.LEGACY,
val thunkReturnTypeAssertions: ThunkReturnTypeAssertions = ThunkReturnTypeAssertions.DISABLED,
val defaultTimezoneOffset: ZoneOffset = ZoneOffset.UTC
) {

Expand Down Expand Up @@ -177,7 +177,7 @@ data class CompileOptions private constructor (
fun build(options: CompileOptions, block: Builder.() -> Unit) = Builder(options).apply(block).build()

/**
* Creates a [CompileOptions] instance with the standard values.
* Creates a [CompileOptions] instance with the standard values for use by the legacy AST compiler.
*/
@JvmStatic
fun standard() = Builder().build()
Expand All @@ -194,7 +194,7 @@ data class CompileOptions private constructor (
fun typingMode(value: TypingMode) = set { copy(typingMode = value) }
fun typedOpBehavior(value: TypedOpBehavior) = set { copy(typedOpBehavior = value) }
fun thunkOptions(value: ThunkOptions) = set { copy(thunkOptions = value) }
fun evaluationTimeTypeChecks(value: ThunkReturnTypeAssertions) = set { copy(thunkReturnTypeAssertions = value) }
fun thunkOptions(build: ThunkOptions.Builder.() -> Unit) = set { copy(thunkOptions = ThunkOptions.build(build)) }
fun defaultTimezoneOffset(value: ZoneOffset) = set { copy(defaultTimezoneOffset = value) }

private inline fun set(block: CompileOptions.() -> CompileOptions): Builder {
Expand Down
21 changes: 20 additions & 1 deletion lang/src/org/partiql/lang/eval/EvaluatingCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ import java.util.TreeSet
import java.util.regex.Pattern
import kotlin.Comparator

/**
* A thunk with no parameters other than the current environment.
*
* See https://en.wikipedia.org/wiki/Thunk
*
* This name was chosen because it is a thunk that accepts an instance of `Environment`.
*/
private typealias ThunkEnv = Thunk<Environment>

/**
* A thunk taking a single [T] argument and the current environment.
*
* See https://en.wikipedia.org/wiki/Thunk
*
* This name was chosen because it is a thunk that accepts an instance of `Environment` and an [ExprValue] as
* its arguments.
*/
private typealias ThunkEnvValue<T> = ThunkValue<Environment, T>

/**
* A basic compiler that converts an instance of [PartiqlAst] to an [Expression].
*
Expand Down Expand Up @@ -116,7 +135,7 @@ internal class EvaluatingCompiler(
private val compileOptions: CompileOptions = CompileOptions.standard()
) {
private val errorSignaler = compileOptions.typingMode.createErrorSignaler(valueFactory)
private val thunkFactory = compileOptions.typingMode.createThunkFactory(compileOptions, valueFactory)
private val thunkFactory = compileOptions.typingMode.createThunkFactory<Environment>(compileOptions.thunkOptions, valueFactory)

private val compilationContextStack = Stack<CompilationContext>()

Expand Down
4 changes: 4 additions & 0 deletions lang/src/org/partiql/lang/eval/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ fun fillErrorContext(errorContext: PropertyValueMap, metaContainer: MetaContaine
}
}

/**
* Returns the [SourceLocationMeta] as an error context if the [SourceLocationMeta.TAG] exists in the passed
* [metaContainer]. Otherwise, returns an empty map.
*/
fun errorContextFrom(metaContainer: MetaContainer?): PropertyValueMap {
if (metaContainer == null) {
return PropertyValueMap()
Expand Down
Loading