Skip to content

Commit

Permalink
Support bool element types (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlurton committed Jun 14, 2021
1 parent 3e11da6 commit 5c6c1ad
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 3 deletions.
63 changes: 63 additions & 0 deletions pig-runtime/src/org/partiql/pig/runtime/BoolPrimitive.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 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://www.apache.org/licenses/LICENSE-2.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.pig.runtime

import com.amazon.ionelement.api.IonElement
import com.amazon.ionelement.api.MetaContainer
import com.amazon.ionelement.api.ionBool
import com.amazon.ionelement.api.ionInt
import com.amazon.ionelement.api.metaContainerOf

/**
* Represents a boolean value that is part of a generated type domain.
*
* This is needed to allow such values to have metas.
*/
class BoolPrimitive(val value: Boolean, override val metas: MetaContainer) : DomainNode {

/** Creates a copy of the current node with the specified values. */
fun copy(value: Boolean = this.value, metas: MetaContainer = this.metas): BoolPrimitive =
BoolPrimitive(value, metas)

/** Creates a copy of the current [LongPrimitive] with [newMetas] as the new metas. */
override fun copyMetas(newMetas: MetaContainer): BoolPrimitive =
BoolPrimitive(value, newMetas)

/** Creates a copy of the current [LongPrimitive] with the specified additional meta. */
override fun withMeta(metaKey: String, metaValue: Any): BoolPrimitive =
BoolPrimitive(this.value, metas + metaContainerOf(metaKey to metaValue))

/** Creates an `IonElement` representation of the current [LongPrimitive]. */
override fun toIonElement(): IonElement = ionBool(value, metas = metas)

/** Converts [value] to a string. */
override fun toString(): String = value.toString()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as BoolPrimitive

if (value != other.value) return false

return true
}

override fun hashCode(): Int {
return value.hashCode()
}
}
9 changes: 9 additions & 0 deletions pig-runtime/src/org/partiql/pig/runtime/DomainVisitorBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import com.amazon.ionelement.api.MetaContainer

open class DomainVisitorBase {

protected open fun visitBoolPrimitive(node: BoolPrimitive) {
// default does nothing
}

protected open fun visitLongPrimitive(node: LongPrimitive) {
// default does nothing
}
Expand All @@ -38,6 +42,11 @@ open class DomainVisitorBase {

///////////////////////////////////////////////////////

open fun walkBoolPrimitive(node: BoolPrimitive) {
visitBoolPrimitive(node)
walkMetas(node.metas)
}

open fun walkLongPrimitive(node: LongPrimitive) {
visitLongPrimitive(node)
walkMetas(node.metas)
Expand Down
10 changes: 10 additions & 0 deletions pig-runtime/src/org/partiql/pig/runtime/DomainVisitorFoldBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import com.amazon.ionelement.api.MetaContainer

open class DomainVisitorFoldBase<T> {

protected open fun visitBoolPrimitive(node: BoolPrimitive, accumulator: T): T =
// default does nothing

accumulator
protected open fun visitLongPrimitive(node: LongPrimitive, accumulator: T): T =
// default does nothing
accumulator
Expand All @@ -38,6 +42,12 @@ open class DomainVisitorFoldBase<T> {

///////////////////////////////////////////////////////

open fun walkBoolPrimitive(node: BoolPrimitive, accumulator: T): T {
var current = accumulator
current = visitBoolPrimitive(node, current)
return walkMetas(node.metas, current)
}

open fun walkLongPrimitive(node: LongPrimitive, accumulator: T): T {
var current = accumulator
current = visitLongPrimitive(node, current)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,19 @@ abstract class DomainVisitorTransformBase {

open fun transformLongPrimitiveMetas(sym: LongPrimitive) = transformMetas(sym.metas)

open fun transformBoolPrimitive(b: BoolPrimitive): BoolPrimitive {
val newValue = transformBoolPrimitiveValue(b)
val newMetas = transformBoolPrimitiveMetas(b)
return if(b.value != newValue || b.metas !== newMetas) {
BoolPrimitive(newValue, newMetas)
} else {
b
}
}

open fun transformBoolPrimitiveValue(sym: BoolPrimitive): Boolean = sym.value

open fun transformBoolPrimitiveMetas(sym: BoolPrimitive) = transformMetas(sym.metas)

}

6 changes: 6 additions & 0 deletions pig-runtime/src/org/partiql/pig/runtime/PrimitiveUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ import com.amazon.ionelement.api.MetaContainer
import com.amazon.ionelement.api.emptyMetaContainer


fun AnyElement.toBoolPrimitive() =
BoolPrimitive(this.booleanValue, this.metas)

fun AnyElement.toLongPrimitive() =
LongPrimitive(this.longValue, this.metas)

fun AnyElement.toSymbolPrimitive() =
SymbolPrimitive(this.symbolValue, this.metas)

fun Boolean.asPrimitive(metas: MetaContainer = emptyMetaContainer()) =
BoolPrimitive(this, metas)

fun Long.asPrimitive(metas: MetaContainer = emptyMetaContainer()) =
LongPrimitive(this, metas)

Expand Down
154 changes: 154 additions & 0 deletions pig-tests/src/org/partiql/pig/tests/generated/sample-universe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ class TestDomain private constructor() {

interface Builder {
// Tuples
/**
* Creates an instance of [TestDomain.BoolPair].
*/
fun boolPair(
first: Boolean,
second: Boolean,
metas: MetaContainer = emptyMetaContainer()
): TestDomain.BoolPair

/**
* Creates an instance of [TestDomain.BoolPair].
*
* Use this variant when metas must be passed to primitive child elements.
*
* (The "_" suffix is needed to work-around conflicts due to type erasure and ambiguities with null arguments.)
*/
fun boolPair_(
first: org.partiql.pig.runtime.BoolPrimitive,
second: org.partiql.pig.runtime.BoolPrimitive,
metas: MetaContainer = emptyMetaContainer()
): TestDomain.BoolPair


/**
* Creates an instance of [TestDomain.IntPair].
*/
Expand Down Expand Up @@ -735,6 +758,27 @@ class TestDomain private constructor() {

private object TestDomainBuilder : Builder {
// Tuples
override fun boolPair(
first: Boolean,
second: Boolean,
metas: MetaContainer
): TestDomain.BoolPair =
TestDomain.BoolPair(
first = first.asPrimitive(),
second = second.asPrimitive(),
metas = metas)

override fun boolPair_(
first: org.partiql.pig.runtime.BoolPrimitive,
second: org.partiql.pig.runtime.BoolPrimitive,
metas: MetaContainer
): TestDomain.BoolPair =
TestDomain.BoolPair(
first = first,
second = second,
metas = metas)


override fun intPair(
first: Long,
second: Long,
Expand Down Expand Up @@ -1399,6 +1443,63 @@ class TestDomain private constructor() {
/////////////////////////////////////////////////////////////////////////////
// Tuple Types
/////////////////////////////////////////////////////////////////////////////
class BoolPair(
val first: org.partiql.pig.runtime.BoolPrimitive,
val second: org.partiql.pig.runtime.BoolPrimitive,
override val metas: MetaContainer = emptyMetaContainer()
): TestDomainNode() {

override fun copyMetas(newMetas: MetaContainer): BoolPair =
BoolPair(
first = first,
second = second,
metas = newMetas)

override fun withMeta(metaKey: String, metaValue: Any): BoolPair =
BoolPair(
first = first,
second = second,
metas = metas + metaContainerOf(metaKey to metaValue))

override fun toIonElement(): SexpElement {
val elements = ionSexpOf(
ionSymbol("bool_pair"),
first.toIonElement(),
second.toIonElement(),
metas = metas)
return elements
}

fun copy(
first: org.partiql.pig.runtime.BoolPrimitive = this.first,
second: org.partiql.pig.runtime.BoolPrimitive = this.second,
metas: MetaContainer = this.metas
) =
BoolPair(
first,
second,
metas)

override fun equals(other: Any?): Boolean {
if (other == null) return false
if (this === other) return true
if (other.javaClass != BoolPair::class.java) return false

other as BoolPair
if (first != other.first) return false
if (second != other.second) return false
return true
}

private val myHashCode by lazy(LazyThreadSafetyMode.NONE) {
var hc = first.hashCode()
hc = 31 * hc + second.hashCode()
hc
}

override fun hashCode(): Int = myHashCode
}

class IntPair(
val first: org.partiql.pig.runtime.LongPrimitive,
val second: org.partiql.pig.runtime.LongPrimitive,
Expand Down Expand Up @@ -3338,6 +3439,15 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
"bool_pair" -> {
sexp.requireArityOrMalformed(IntRange(2, 2))
val first = sexp.getRequired(0).toBoolPrimitive()
val second = sexp.getRequired(1).toBoolPrimitive()
TestDomain.BoolPair(
first,
second,
metas = sexp.metas)
}
"int_pair" -> {
sexp.requireArityOrMalformed(IntRange(2, 2))
val first = sexp.getRequired(0).toLongPrimitive()
Expand Down Expand Up @@ -3658,6 +3768,7 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
open fun visitBoolPair(node: TestDomain.BoolPair) { }
open fun visitIntPair(node: TestDomain.IntPair) { }
open fun visitSymbolPair(node: TestDomain.SymbolPair) { }
open fun visitIonPair(node: TestDomain.IonPair) { }
Expand Down Expand Up @@ -3716,6 +3827,13 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
open fun walkBoolPair(node: TestDomain.BoolPair) {
visitBoolPair(node)
walkBoolPrimitive(node.first)
walkBoolPrimitive(node.second)
walkMetas(node.metas)
}

open fun walkIntPair(node: TestDomain.IntPair) {
visitIntPair(node)
walkLongPrimitive(node.first)
Expand Down Expand Up @@ -4005,6 +4123,7 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
open protected fun visitBoolPair(node: TestDomain.BoolPair, accumulator: T): T = accumulator
open protected fun visitIntPair(node: TestDomain.IntPair, accumulator: T): T = accumulator
open protected fun visitSymbolPair(node: TestDomain.SymbolPair, accumulator: T): T = accumulator
open protected fun visitIonPair(node: TestDomain.IonPair, accumulator: T): T = accumulator
Expand Down Expand Up @@ -4063,6 +4182,15 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
open fun walkBoolPair(node: TestDomain.BoolPair, accumulator: T): T {
var current = accumulator
current = visitBoolPair(node, current)
current = walkBoolPrimitive(node.first, current)
current = walkBoolPrimitive(node.second, current)
current = walkMetas(node.metas, current)
return current
}

open fun walkIntPair(node: TestDomain.IntPair, accumulator: T): T {
var current = accumulator
current = visitIntPair(node, current)
Expand Down Expand Up @@ -4414,6 +4542,32 @@ class TestDomain private constructor() {
//////////////////////////////////////
// Tuple Types
//////////////////////////////////////
// Tuple BoolPair
open fun transformBoolPair(node: TestDomain.BoolPair): TestDomain.BoolPair {
val new_first = transformBoolPair_first(node)
val new_second = transformBoolPair_second(node)
val new_metas = transformBoolPair_metas(node)
return if (
node.first !== new_first ||
node.second !== new_second ||
node.metas !== new_metas
) {
TestDomain.BoolPair(
first = new_first,
second = new_second,
metas = new_metas
)
} else {
node
}
}
open fun transformBoolPair_first(node: TestDomain.BoolPair) =
transformBoolPrimitive(node.first)
open fun transformBoolPair_second(node: TestDomain.BoolPair) =
transformBoolPrimitive(node.second)
open fun transformBoolPair_metas(node: TestDomain.BoolPair) =
transformMetas(node.metas)

// Tuple IntPair
open fun transformIntPair(node: TestDomain.IntPair): TestDomain.IntPair {
val new_first = transformIntPair_first(node)
Expand Down
Loading

0 comments on commit 5c6c1ad

Please sign in to comment.