Skip to content
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

Split PartiQLSchemaInferencer Test Inputs #1246

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
rchowell marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ class PartiQLSchemaInferencerTests {
@Execution(ExecutionMode.CONCURRENT)
fun testOrderBy(tc: TestCase) = runTest(tc)

// @Test
rchowell marked this conversation as resolved.
Show resolved Hide resolved
// @Disabled
// fun testPrint() {
// val cases = orderByCases()
// val prefix = "orderByCases"
// cases.forEachIndexed { i, t ->
// val name = "$prefix-${"%02d".format(i + 1)}"
// println("--#[$name]")
// println(t.query + ";")
// println()
// }
// cases.forEachIndexed { i, t ->
// val name = "$prefix-${"%02d".format(i + 1)}"
// println("case::{")
// println(" input: \"$name\",")
// println(" catalog: \"default\",")
// println(" schema: ${t.expected.toIon()}")
// println("}")
// println()
// }
// }

companion object {

private val root = this::class.java.getResource("/catalogs")!!.toURI().toPath().pathString
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.partiql.planner
rchowell marked this conversation as resolved.
Show resolved Hide resolved

import com.amazon.ionelement.api.IonElement
import com.amazon.ionelement.api.ListElement
import com.amazon.ionelement.api.StringElement
import com.amazon.ionelement.api.StructElement
import org.partiql.plugins.local.getAngry
import org.partiql.plugins.local.toStaticType
import org.partiql.types.StaticType

data class PlannerTestCase(
public val input: String,
public val catalog: String,
public val catalogPath: List<String>,
public val schema: StaticType,
) {

companion object {

fun load(ion: StructElement): PlannerTestCase {
// Required
val input = ion.getAngry<StringElement>("input").textValue
val catalog = ion.getAngry<StringElement>("catalog").textValue
val schema = ion.getAngry<IonElement>("schema").toStaticType()
// Optional
val catalogPath = mutableListOf<String>()
val pathArr = ion.getOptional("catalogPath")
if (pathArr != null && pathArr is ListElement) {
pathArr.asList().values.forEach { v ->
if (v !is StringElement) {
error("catalogPath must be a list of strings")
}
catalogPath.add((v as StringElement).textValue)
}
}
return PlannerTestCase(input, catalog, catalogPath, schema)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.partiql.planner

import com.amazon.ionelement.api.StructElement
import com.amazon.ionelement.api.loadAllElements
import java.io.File

data class PlannerTestGroup(
public val name: String,
public val cases: List<PlannerTestCase>,
) {

companion object {

public fun load(dir: File): PlannerTestGroup {
val tests = dir.listFiles()!!.flatMap { loadAllTests(it) }
return PlannerTestGroup(dir.name, tests)
}

public fun loadAllTests(file: File): List<PlannerTestCase> {
val text = file.readText()
val ion = loadAllElements(text)
return ion.map { PlannerTestCase.load(it as StructElement) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ import org.junit.jupiter.api.fail
import org.partiql.errors.ProblemSeverity
import org.partiql.parser.PartiQLParserBuilder
import org.partiql.plan.Statement
import org.partiql.planner.test.PlannerTest
import org.partiql.planner.test.PlannerTestProvider
import org.partiql.planner.test.PlannerTestSuite
import org.partiql.planner.test.toIon
import org.partiql.planner.test.PartiQLTestCase
import org.partiql.planner.test.PartiQLTestProvider
import org.partiql.plugins.local.LocalPlugin
import org.partiql.plugins.local.toIon
import java.util.stream.Stream
import kotlin.io.path.pathString
import kotlin.io.path.toPath

/**
* PlannerTestJunit is responsible for constructing JUnit test suites from all input queries in the testFixtures.
*
* I believe this can be more generic and added to testFixtures; but that is outside the scope of current work.
*/
class PlannerTestJunit {

@TestFactory
fun mapSuitesToJunitTests(): Stream<DynamicNode> {
val provider = PlannerTestProvider()
return provider.suites().map { suiteNode(it) }
val inputs = PartiQLTestProvider().inputs()
val cases = PlannerTestProvider().groups()
return cases.map { groupNode(it, inputs) }
}

companion object {

private val root = PlannerTest::class.java.getResource("/catalogs")!!.toURI().toPath().pathString
private val root = PartiQLTestProvider::class.java.getResource("/catalogs")!!.toURI().path

private val parser = PartiQLParserBuilder.standard().build()

Expand All @@ -42,35 +45,52 @@ class PlannerTestJunit {
field("connector_name", ionString("local")),
field("root", ionString("$root/default")),
),
"tpc_ds" to ionStructOf(
field("connector_name", ionString("local")),
field("root", ionString("$root/tpc_ds")),
),
)

private fun suiteNode(suite: PlannerTestSuite): DynamicContainer {
private fun groupNode(group: PlannerTestGroup, inputs: Map<String, PartiQLTestCase>): DynamicContainer {
val plugin = LocalPlugin()
val planner = PartiQLPlannerBuilder()
.plugins(listOf(plugin))
.build()
val tests = suite.tests.map { (name, test) ->
val testName = "${suite.name}__$name"
// Map all cases to an input
val tests = group.cases.map { case ->
val key = "${group.name}__${case.input}"
val input = inputs[key]
// Report bad input mapping
if (input == null) {
return@map failTestNode(key, "Missing input for `$key`")
}
val session = PartiQLPlanner.Session(
queryId = "q__$testName",
queryId = key,
userId = "Planner_test_runner",
currentCatalog = suite.session.catalog,
currentDirectory = suite.session.path,
currentCatalog = case.catalog,
currentDirectory = case.catalogPath,
catalogConfig = catalogConfig,
)
testNode(testName, planner, session, test)
testNode(key, planner, session, input.statement, case)
}
return dynamicContainer(group.name, tests.stream())
}

private fun failTestNode(id: String, message: String): DynamicTest {
return dynamicTest(id) {
fail { message }
}
return dynamicContainer(suite.name, tests.stream())
}

private fun testNode(
displayName: String,
planner: PartiQLPlanner,
session: PartiQLPlanner.Session,
test: PlannerTest,
statement: String,
case: PlannerTestCase,
): DynamicTest {
return dynamicTest(displayName) {
val ast = parser.parse(test.statement).root
val ast = parser.parse(statement).root
val result = planner.plan(ast, session)
for (problem in result.problems) {
if (problem.details.severity == ProblemSeverity.ERROR) {
Expand All @@ -81,7 +101,7 @@ class PlannerTestJunit {
if (statement !is Statement.Query) {
fail { "Expected plan statement to be a Statement.Query" }
}
val expected = test.schema.toIon()
val expected = case.schema.toIon()
val actual = statement.root.type.toIon()
assertEquals(expected, actual)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.partiql.planner

import java.util.stream.Stream
import kotlin.io.path.toPath

/**
* TODO converge with PartiQLTestProvider.
*/
class PlannerTestProvider {

private val groups: List<PlannerTestGroup>

init {
val default = PlannerTestProvider::class.java.getResource("/cases")!!.toURI().toPath()
val casesDir = default.toFile()
groups = casesDir.listFiles { f -> f.isDirectory }!!.map { PlannerTestGroup.load(it) }
}

/**
* Return test cases associated by groups.
*/
public fun groups(): Stream<PlannerTestGroup> {
return groups.stream()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
case::{
input: "collections-01",
catalog: "default",
schema: {
type: "bag",
items: "int",
}
}

case::{
input: "collections-02",
catalog: "default",
schema: {
type: "list",
items: "int",
}
}

case::{
input: "collections-03",
catalog: "default",
schema: {
type: "list",
items: "int",
}
}

case::{
input: "collections-04",
catalog: "default",
schema: {
type: "sexp",
items: "int",
}
}

case::{
input: "collections-05",
catalog: "default",
schema: {
type: "bag",
items: "int",
}
}

case::{
input: "collections-06",
catalog: "default",
schema: {
type: "bag",
items: {
type: "struct",
constraints: [
closed,
unique,
ordered
],
fields: [
{
name: "x",
type: "int",
}
]
}
}
}
Loading