-
Notifications
You must be signed in to change notification settings - Fork 62
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
Add exclude physical plan compiler impl #1280
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2089da5
WIP to add 'EXCLUDE' to physical plan compiler
alancai98 8999b29
Refactor `EXCLUDE` plan compiler impl (part 1)
alancai98 84f3603
Fix build failures; port more to `partiql-eval`
alancai98 636c142
Change `RemoveAndOtherSteps` to tree representation in `partiql-eval`
alancai98 c2ea303
Add back subsumption tests, docs, minor refactoring
alancai98 4c4062f
Remove CompiledExcludeExpr usage of PartiqlPhysical.ExcludeStep
alancai98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These sequence |
||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ported from
ExprValue.kt
and renamed fromExprValue.of(value)
toionValueToExprValue(value)