-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
EXCLUDE
plan compiler impl (part 1)
- 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
Showing
16 changed files
with
419 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
partiql-eval/src/main/kotlin/org/partiql/lang/eval/internal/IonStructBindings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
partiql-eval/src/main/kotlin/org/partiql/lang/eval/internal/IonValueToExprValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
15 changes: 15 additions & 0 deletions
15
partiql-eval/src/main/kotlin/org/partiql/lang/eval/internal/NamedExprValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
44 changes: 44 additions & 0 deletions
44
partiql-eval/src/main/kotlin/org/partiql/lang/eval/internal/SequenceExprValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.