Skip to content

Commit

Permalink
Refactor EXCLUDE plan compiler impl (part 1)
Browse files Browse the repository at this point in the history
- Port over List/Bag/Sexp ExprValue to partiql-eval
- Port over IonValue to ExprValue conversion to partiql-eval
- Change PhysicalPlanCompilerImpl (and other partiql-eval) to use
  internal partiql-eval apis
  • Loading branch information
alancai98 committed Dec 8, 2023
1 parent 4bd3d1f commit 0b46b02
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.ExprValueType
import org.partiql.lang.eval.StructOrdering
import org.partiql.lang.eval.internal.ext.isUnknown
import org.partiql.lang.eval.internal.ext.namedValue
import org.partiql.lang.eval.name
import org.partiql.lang.eval.namedValue
import org.partiql.lang.types.StaticTypeUtils
import org.partiql.types.AnyOfType
import org.partiql.types.AnyType
Expand Down Expand Up @@ -228,9 +228,9 @@ internal class AnyOfCastTable(
val children = source.asSequence().map { cast(it) }

when (targetType) {
ExprValueType.LIST -> ExprValue.newList(children)
ExprValueType.SEXP -> ExprValue.newSexp(children)
ExprValueType.BAG -> ExprValue.newBag(children)
ExprValueType.LIST -> ListExprValue(children)
ExprValueType.SEXP -> SexpExprValue(children)
ExprValueType.BAG -> BagExprValue(children)
ExprValueType.STRUCT -> {
if (source.type != ExprValueType.STRUCT) {
// Should not be possible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/

package org.partiql.lang.eval.internal

import com.amazon.ion.IonStruct
import com.amazon.ion.IonValue
import org.partiql.errors.ErrorCode
import org.partiql.errors.Property
import org.partiql.lang.eval.BindingCase
import org.partiql.lang.eval.BindingName
import org.partiql.lang.eval.Bindings
import org.partiql.lang.eval.EvaluationException
import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.internal.ext.namedValue
import org.partiql.lang.util.propertyValueMapOf
import org.partiql.lang.util.to

internal fun errAmbiguousBinding(bindingName: String, matchingNames: List<String>): Nothing {
err(
"Multiple matches were found for the specified identifier",
ErrorCode.EVALUATOR_AMBIGUOUS_BINDING,
propertyValueMapOf(
Property.BINDING_NAME to bindingName,
Property.BINDING_NAME_MATCHES to matchingNames.joinToString(", ")
),
internal = false
)
}

/**
* Custom implementation of [Bindings] that lazily computes case sensitive or insensitive hash tables which
* will speed up the lookup of bindings within structs.
*
* The key difference in behavior between this and other [Bindings] implementations is that it
* can throw an ambiguous binding [EvaluationException] even for case-sensitive lookups as it is
* entirely possible that fields with identical names can appear within [IonStruct]s.
*
* Important: this class is critical to performance for many queries. Change with caution.
*/
internal class IonStructBindings(private val myStruct: IonStruct) : Bindings<ExprValue> {

private val caseInsensitiveFieldMap by lazy {
HashMap<String, ArrayList<IonValue>>().apply {
for (field in myStruct) {
val entries = getOrPut(field.fieldName.lowercase()) { ArrayList(1) }
entries.add(field)
}
}
}

private val caseSensitiveFieldMap by lazy {
HashMap<String, ArrayList<IonValue>>().apply {
for (field in myStruct) {
val entries = getOrPut(field.fieldName) { ArrayList(1) }
entries.add(field)
}
}
}

private fun caseSensitiveLookup(fieldName: String): IonValue? =
caseSensitiveFieldMap[fieldName]?.let { entries -> handleMatches(entries, fieldName) }

private fun caseInsensitiveLookup(fieldName: String): IonValue? =
caseInsensitiveFieldMap[fieldName.lowercase()]?.let { entries -> handleMatches(entries, fieldName) }

private fun handleMatches(entries: List<IonValue>, fieldName: String): IonValue? =
when (entries.size) {
0 -> null
1 -> entries[0]
else ->
errAmbiguousBinding(fieldName, entries.map { it.fieldName })
}

override operator fun get(bindingName: BindingName): ExprValue? =
when (bindingName.bindingCase) {
BindingCase.SENSITIVE -> caseSensitiveLookup(bindingName.name)
BindingCase.INSENSITIVE -> caseInsensitiveLookup(bindingName.name)
}?.let {
ionValueToExprValue(it).namedValue(ExprValue.newString(it.fieldName))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.partiql.lang.eval.internal

import com.amazon.ion.IonBlob
import com.amazon.ion.IonBool
import com.amazon.ion.IonClob
import com.amazon.ion.IonDatagram
import com.amazon.ion.IonDecimal
import com.amazon.ion.IonFloat
import com.amazon.ion.IonInt
import com.amazon.ion.IonList
import com.amazon.ion.IonSexp
import com.amazon.ion.IonString
import com.amazon.ion.IonStruct
import com.amazon.ion.IonSymbol
import com.amazon.ion.IonTimestamp
import com.amazon.ion.IonValue
import org.partiql.lang.eval.BAG_ANNOTATION
import org.partiql.lang.eval.Bindings
import org.partiql.lang.eval.DATE_ANNOTATION
import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.GRAPH_ANNOTATION
import org.partiql.lang.eval.MISSING_ANNOTATION
import org.partiql.lang.eval.TIME_ANNOTATION
import org.partiql.lang.eval.internal.ext.namedValue
import org.partiql.lang.eval.time.Time
import org.partiql.lang.graph.ExternalGraphReader
import org.partiql.lang.util.bytesValue
import java.math.BigDecimal

/**
* Creates a new [ExprValue] instance from any Ion value.
*
* If possible, prefer the use of the other methods instead because they might return [ExprValue] instances
* that are better optimized for their specific data type (depending on implementation).
*/
internal fun ionValueToExprValue(value: IonValue): ExprValue {
return when {
value.isNullValue && value.hasTypeAnnotation(MISSING_ANNOTATION) -> ExprValue.missingValue // MISSING
value.isNullValue -> ExprValue.newNull(value.type) // NULL
value is IonBool -> ExprValue.newBoolean(value.booleanValue()) // BOOL
value is IonInt -> ExprValue.newInt(value.longValue()) // INT
value is IonFloat -> ExprValue.newFloat(value.doubleValue()) // FLOAT
value is IonDecimal -> ExprValue.newDecimal(value.decimalValue()) // DECIMAL
value is IonTimestamp && value.hasTypeAnnotation(DATE_ANNOTATION) -> { // DATE
val timestampValue = value.timestampValue()
ExprValue.newDate(timestampValue.year, timestampValue.month, timestampValue.day)
}
value is IonTimestamp -> ExprValue.newTimestamp(value.timestampValue()) // TIMESTAMP
value is IonStruct && value.hasTypeAnnotation(TIME_ANNOTATION) -> { // TIME
val hourValue = (value["hour"] as IonInt).intValue()
val minuteValue = (value["minute"] as IonInt).intValue()
val secondInDecimal = (value["second"] as IonDecimal).decimalValue()
val secondValue = secondInDecimal.toInt()
val nanoValue = secondInDecimal.remainder(BigDecimal.ONE).multiply(NANOS_PER_SECOND.toBigDecimal()).toInt()
val timeZoneHourValue = (value["timezone_hour"] as IonInt).intValue()
val timeZoneMinuteValue = (value["timezone_minute"] as IonInt).intValue()
ExprValue.newTime(
Time.of(
hourValue,
minuteValue,
secondValue,
nanoValue,
secondInDecimal.scale(),
timeZoneHourValue * 60 + timeZoneMinuteValue
)
)
}
value is IonStruct && value.hasTypeAnnotation(GRAPH_ANNOTATION) -> // GRAPH
ExprValue.newGraph(ExternalGraphReader.read(value))
value is IonSymbol -> ExprValue.newSymbol(value.stringValue()) // SYMBOL
value is IonString -> ExprValue.newString(value.stringValue()) // STRING
value is IonClob -> ExprValue.newClob(value.bytesValue()) // CLOB
value is IonBlob -> ExprValue.newBlob(value.bytesValue()) // BLOB
value is IonList && value.hasTypeAnnotation(BAG_ANNOTATION) -> BagExprValue(value.map { ionValueToExprValue(it) }) // BAG
value is IonList -> ListExprValue(value.map { ionValueToExprValue(it) }) // LIST
value is IonSexp -> SexpExprValue(value.map { ionValueToExprValue(it) }) // SEXP
value is IonStruct -> IonStructExprValue(value) // STRUCT
value is IonDatagram -> BagExprValue(value.map { ionValueToExprValue(it) }) // DATAGRAM represented as BAG ExprValue
else -> error("Unrecognized IonValue to transform to ExprValue: $value")
}
}

private class IonStructExprValue(
ionStruct: IonStruct
) : StructExprValue(
StructOrdering.UNORDERED,
ionStruct.asSequence().map { ionValueToExprValue(it).namedValue(ExprValue.newString(it.fieldName)) }
) {
override val bindings: Bindings<ExprValue> =
IonStructBindings(ionStruct)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.partiql.lang.eval.internal

import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.Named
import org.partiql.lang.eval.stringify
import org.partiql.lang.util.downcast

/**
* An [ExprValue] that also implements [Named].
*/
internal class NamedExprValue(override val name: ExprValue, val value: ExprValue) : ExprValue by value, Named {
override fun <T : Any?> asFacet(type: Class<T>?): T? = downcast(type) ?: value.asFacet(type)

override fun toString(): String = stringify()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.partiql.lang.eval.internal

import org.partiql.lang.eval.BaseExprValue
import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.ExprValueType
import org.partiql.lang.eval.OrdinalBindings
import org.partiql.lang.eval.internal.ext.namedValue

internal class ListExprValue(val values: Sequence<ExprValue>) : BaseExprValue() {
override val type = ExprValueType.LIST
override val ordinalBindings by lazy { OrdinalBindings.ofList(toList()) }
override fun iterator() = values.mapIndexed { i, v -> v.namedValue(ExprValue.newInt(i)) }.iterator()

constructor(values: List<ExprValue>) : this(values.asSequence())
}

internal class BagExprValue(val values: Sequence<ExprValue>) : BaseExprValue() {
override val type = ExprValueType.BAG
override val ordinalBindings = OrdinalBindings.EMPTY
override fun iterator() = values.iterator()

constructor(values: List<ExprValue>) : this(values.asSequence())
}

internal class SexpExprValue(val values: Sequence<ExprValue>) : BaseExprValue() {
override val type = ExprValueType.SEXP
override val ordinalBindings by lazy { OrdinalBindings.ofList(toList()) }
override fun iterator() = values.mapIndexed { i, v -> v.namedValue(ExprValue.newInt(i)) }.iterator()

constructor(values: List<ExprValue>) : this(values.asSequence())
}

/**
* Returns an [ExprValue] created from a sequence of [seq]. Requires [type] to be a sequence type
* (i.e. [ExprValueType.isSequence] == true).
*/
internal fun newSequenceExprValue(type: ExprValueType, seq: Sequence<ExprValue>): ExprValue {
return when (type) {
ExprValueType.LIST -> ListExprValue(seq)
ExprValueType.BAG -> BagExprValue(seq)
ExprValueType.SEXP -> SexpExprValue(seq)
else -> error("Sequence type required")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.ExprValueType
import org.partiql.lang.eval.NaturalExprValueComparators
import org.partiql.lang.eval.booleanValue
import org.partiql.lang.eval.internal.BagExprValue
import org.partiql.lang.eval.internal.ExprAggregator
import org.partiql.lang.eval.internal.errNoContext
import org.partiql.lang.eval.internal.ext.bigDecimalOf
Expand Down Expand Up @@ -173,7 +174,7 @@ internal class AccumulatorGroupAs(
exprValues.add(value)
}

override fun compute(): ExprValue = ExprValue.newBag(exprValues)
override fun compute(): ExprValue = BagExprValue(exprValues)
}

private fun comparisonAccumulator(comparator: NaturalExprValueComparators): (ExprValue?, ExprValue) -> ExprValue =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import org.partiql.lang.eval.EvaluationSession
import org.partiql.lang.eval.ExprFunction
import org.partiql.lang.eval.ExprValue
import org.partiql.lang.eval.ExprValueType
import org.partiql.lang.eval.internal.BagExprValue
import org.partiql.lang.eval.internal.DateTimePart
import org.partiql.lang.eval.internal.ListExprValue
import org.partiql.lang.eval.internal.NANOS_PER_SECOND
import org.partiql.lang.eval.internal.err
import org.partiql.lang.eval.internal.errNoContext
import org.partiql.lang.eval.internal.ext.adjustPrecisionTo
import org.partiql.lang.eval.internal.ext.bigDecimalValue
import org.partiql.lang.eval.internal.ext.intValue
import org.partiql.lang.eval.internal.ext.toOffsetDateTime
import org.partiql.lang.eval.internal.ext.unnamedValue
import org.partiql.lang.eval.internal.timestamp.TimestampParser
import org.partiql.lang.eval.internal.timestamp.TimestampTemporalAccessor
import org.partiql.lang.eval.stringValue
import org.partiql.lang.eval.time.Time
import org.partiql.lang.eval.timestampValue
import org.partiql.lang.eval.unnamedValue
import org.partiql.lang.types.FunctionSignature
import org.partiql.lang.types.UnknownArguments
import org.partiql.lang.util.propertyValueMapOf
Expand Down Expand Up @@ -134,8 +136,8 @@ internal object ExprFunctionFilterDistinct : ExprFunction {
}
}
return when (argument.type) {
ExprValueType.LIST -> ExprValue.newList(seq)
else -> ExprValue.newBag(seq)
ExprValueType.LIST -> ListExprValue(seq)
else -> BagExprValue(seq)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ import org.partiql.lang.eval.Named
import org.partiql.lang.eval.NaturalExprValueComparators
import org.partiql.lang.eval.OrderedBindNames
import org.partiql.lang.eval.TypedOpBehavior
import org.partiql.lang.eval.internal.BagExprValue
import org.partiql.lang.eval.internal.DateTimePart
import org.partiql.lang.eval.internal.ListExprValue
import org.partiql.lang.eval.internal.NANOS_PER_SECOND
import org.partiql.lang.eval.internal.NamedExprValue
import org.partiql.lang.eval.internal.SexpExprValue
import org.partiql.lang.eval.internal.StructExprValue
import org.partiql.lang.eval.internal.err
import org.partiql.lang.eval.internal.errIntOverflow
Expand Down Expand Up @@ -108,25 +112,12 @@ internal fun ExprValue.asNamed(): Named = object : Named {
}

/** Binds the given name value as a [Named] facet delegate over this [ExprValue]. */
internal fun ExprValue.namedValue(nameValue: ExprValue): ExprValue = object : ExprValue by this, Named {
override val name = nameValue
override fun <T : Any?> asFacet(type: Class<T>?): T? =
downcast(type) ?: this@namedValue.asFacet(type)
override fun toString(): String = stringify()
}
internal fun ExprValue.namedValue(nameValue: ExprValue): ExprValue = NamedExprValue(nameValue, this)

/** Wraps this [ExprValue] in a delegate that always masks the [Named] facet. */
internal fun ExprValue.unnamedValue(): ExprValue = when (asFacet(Named::class.java)) {
null -> this
else -> object : ExprValue by this {
override fun <T : Any?> asFacet(type: Class<T>?): T? =
when (type) {
// always mask the name facet
Named::class.java -> null
else -> this@unnamedValue.asFacet(type)
}
override fun toString(): String = stringify()
}
internal fun ExprValue.unnamedValue(): ExprValue = when (this) {
is NamedExprValue -> this.value
else -> this
}

internal val ExprValue.name: ExprValue?
Expand Down Expand Up @@ -615,9 +606,9 @@ internal fun ExprValue.cast(
is BlobType -> when {
type.isLob -> return ExprValue.newBlob(bytesValue())
}
is ListType -> if (type.isSequence) return ExprValue.newList(asSequence())
is SexpType -> if (type.isSequence) return ExprValue.newSexp(asSequence())
is BagType -> if (type.isSequence) return ExprValue.newBag(asSequence())
is ListType -> if (type.isSequence) return ListExprValue(asSequence())
is SexpType -> if (type.isSequence) return SexpExprValue(asSequence())
is BagType -> if (type.isSequence) return BagExprValue(asSequence())
// no support for anything else
else -> {}
}
Expand Down
Loading

0 comments on commit 0b46b02

Please sign in to comment.