Skip to content

Commit

Permalink
Updates partiql-eval and runner to fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Aug 5, 2024
1 parent d3d4789 commit fb1d183
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.partiql.eval.internal

import com.amazon.ionelement.api.createIonElementLoader
import com.amazon.ionelement.api.loadSingleElement
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
Expand All @@ -13,10 +14,11 @@ import org.partiql.parser.PartiQLParser
import org.partiql.plan.PartiQLPlan
import org.partiql.plan.debug.PlanPrinter
import org.partiql.planner.builder.PartiQLPlannerBuilder
import org.partiql.planner.catalog.Name
import org.partiql.planner.catalog.Session
import org.partiql.plugins.memory.MemoryCatalog
import org.partiql.plugins.memory.MemoryConnector
import org.partiql.spi.connector.ConnectorSession
import org.partiql.plugins.memory.MemoryTable
import org.partiql.types.PType
import org.partiql.types.StaticType
import org.partiql.value.CollectionValue
import org.partiql.value.PartiQLValue
Expand All @@ -28,6 +30,7 @@ import org.partiql.value.int32Value
import org.partiql.value.int64Value
import org.partiql.value.intValue
import org.partiql.value.io.PartiQLValueIonWriterBuilder
import org.partiql.value.ion.IonDatum
import org.partiql.value.listValue
import org.partiql.value.missingValue
import org.partiql.value.nullValue
Expand Down Expand Up @@ -1258,19 +1261,22 @@ class PartiQLEngineDefaultTest {

internal fun assert() {
val statement = parser.parse(input).root
val catalogBuilder = MemoryCatalog.builder().name("memory")
globals.forEach { global ->
catalogBuilder.define(global.name, global.type, loader.loadSingleElement(global.value))
}
val catalog = catalogBuilder.build()
val connector = MemoryConnector(catalog)
val connectorSession = object : ConnectorSession {
override fun getQueryId(): String = "q"
override fun getUserId(): String = "u"
}
val connector = MemoryConnector.builder()
.name("memory")
.apply {
globals.forEach {
val table = MemoryTable.of(
name = Name.of(it.name),
schema = PType.fromStaticType(it.type),
datum = IonDatum.of(loadSingleElement(it.value))
)
define(table)
}
}
.build()
val session = Session.builder()
.catalog("memory")
.catalogs("memory" to connector.getMetadata(connectorSession))
.catalogs(connector.getCatalog())
.build()
val plan = planner.plan(statement, session)
val prepared = engine.prepare(plan.plan, PartiQLEngine.Session(mapOf("memory" to connector), mode = mode))
Expand Down Expand Up @@ -1344,15 +1350,10 @@ class PartiQLEngineDefaultTest {

private fun run(mode: PartiQLEngine.Mode): Pair<PartiQLValue, PartiQLPlan> {
val statement = parser.parse(input).root
val catalog = MemoryCatalog.builder().name("memory").build()
val connector = MemoryConnector(catalog)
val connectorSession = object : ConnectorSession {
override fun getQueryId(): String = "q"
override fun getUserId(): String = "u"
}
val connector = MemoryConnector.builder().name("memory").build()
val session = Session.builder()
.catalog("memory")
.catalogs("memory" to connector.getMetadata(connectorSession))
.catalogs(connector.getCatalog())
.build()
val plan = planner.plan(statement, session)
val prepared = engine.prepare(plan.plan, PartiQLEngine.Session(mapOf("memory" to connector), mode = mode))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ public class MemoryConnector private constructor(

@JvmStatic
public fun builder(): Builder = Builder()
}

public class Builder internal constructor() {
public class Builder internal constructor() {

private var name: String? = null
private var tables: MutableMap<Name, MemoryTable> = mutableMapOf()
private var name: String? = null
private var tables: MutableMap<Name, MemoryTable> = mutableMapOf()

public fun name(name: String): Builder = apply { this.name = name }
public fun name(name: String): Builder = apply { this.name = name }

// TODO REMOVE AFTER CREATE TABLE IS ADDED TO CATALOG
public fun define(name: String, type: StaticType): Builder {
val table = MemoryTable.empty(name, PType.fromStaticType(type))
return define(table)
}
// TODO REMOVE AFTER CREATE TABLE IS ADDED TO CATALOG
public fun define(name: String, type: StaticType): Builder {
val table = MemoryTable.empty(name, PType.fromStaticType(type))
return define(table)
}

// TODO REMOVE AFTER CREATE TABLE IS ADDED TO CATALOG
public fun define(table: MemoryTable): Builder = apply { tables[table.getName()] = table }
// TODO REMOVE AFTER CREATE TABLE IS ADDED TO CATALOG
public fun define(table: MemoryTable): Builder = apply { tables[table.getName()] = table }

public fun build(): MemoryConnector = MemoryConnector(name!!, tables)
}
public fun build(): MemoryConnector = MemoryConnector(name!!, tables)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import com.amazon.ionelement.api.toIonValue
import org.partiql.eval.PartiQLEngine
import org.partiql.eval.PartiQLResult
import org.partiql.eval.PartiQLStatement
import org.partiql.eval.value.Datum
import org.partiql.lang.eval.CompileOptions
import org.partiql.lang.eval.TypingMode
import org.partiql.parser.PartiQLParser
import org.partiql.plan.Statement
import org.partiql.planner.PartiQLPlanner
import org.partiql.plugins.memory.MemoryCatalog
import org.partiql.planner.catalog.Name
import org.partiql.plugins.memory.MemoryConnector
import org.partiql.plugins.memory.MemoryObject
import org.partiql.plugins.memory.MemoryTable
import org.partiql.runner.ION
import org.partiql.runner.test.TestExecutor
import org.partiql.spi.BindingCase
import org.partiql.spi.BindingName
import org.partiql.spi.connector.Connector
import org.partiql.spi.connector.ConnectorSession
import org.partiql.types.PType
import org.partiql.types.StaticType
import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental
import org.partiql.value.io.PartiQLValueIonReaderBuilder
Expand All @@ -35,7 +32,7 @@ import org.partiql.planner.catalog.Session as PlannerSession
@OptIn(PartiQLValueExperimental::class)
class EvalExecutor(
private val plannerSession: PlannerSession,
private val evalSession: PartiQLEngine.Session
private val evalSession: PartiQLEngine.Session,
) : TestExecutor<PartiQLStatement<*>, PartiQLResult> {

override fun prepare(statement: String): PartiQLStatement<*> {
Expand Down Expand Up @@ -111,6 +108,7 @@ class EvalExecutor(
}
return false
}

companion object {
val parser = PartiQLParser.default()
val planner = PartiQLPlanner.standard()
Expand All @@ -128,12 +126,7 @@ class EvalExecutor(

val session = PlannerSession.builder()
.catalog(catalog)
.catalogs(
"default" to connector.getMetadata(object : ConnectorSession {
override fun getQueryId(): String = "query"
override fun getUserId(): String = "user"
})
)
.catalogs(connector.getCatalog())
.build()

val mode = when (options.typingMode) {
Expand Down Expand Up @@ -161,22 +154,20 @@ class EvalExecutor(
env.fields.forEach {
map[it.name] = inferEnv(it.value)
}
val catalog = MemoryCatalog.builder().name("default").build()
catalog.load(env)
return MemoryConnector(catalog)
return MemoryConnector.builder()
.name("default")
.apply { load(env) }
.build()
}

/**
* Uses the planner to infer the type of the environment.
*/
private fun inferEnv(env: AnyElement): PType {
val catalog = MemoryCatalog.builder().name("conformance_test").build()
val connector = MemoryConnector(catalog)
val catalog = MemoryConnector.builder().name("default").build().getCatalog()
val session = PlannerSession.builder()
.catalog("default")
.catalogs(
"default" to connector.getMetadata(object : ConnectorSession {
override fun getQueryId(): String = "query"
override fun getUserId(): String = "user"
})
)
.catalogs(catalog)
.build()
val stmt = parser.parse("`$env`").root
val plan = planner.plan(stmt, session)
Expand All @@ -188,17 +179,17 @@ class EvalExecutor(
*
* TODO until this point, PartiQL Kotlin has only done top-level bindings.
*/
private fun MemoryCatalog.load(env: StructElement) {
private fun MemoryConnector.Builder.load(env: StructElement) {
for (f in env.fields) {
val k = f.name
val v = f.value
// convert to binding
val path = BindingPath(steps = listOf(BindingName(k, BindingCase.SENSITIVE)))
val item = MemoryObject(
type = StaticType.ANY,
value = PartiQLValueIonReaderBuilder.standard().build(v).read(),
val name = Name.of(f.name)
val value = PartiQLValueIonReaderBuilder.standard().build(f.value).read()
val table = MemoryTable.of(
name = name,
schema = PType.typeDynamic(),
datum = Datum.nullValue(),
// datum = Datum.of(value)
)
this.insert(path, item)
define(table)
}
}
}
Expand Down

0 comments on commit fb1d183

Please sign in to comment.