-
Notifications
You must be signed in to change notification settings - Fork 63
Add implementation of LET clause and LetTests #1745
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
259 changes: 259 additions & 0 deletions
259
partiql-eval/src/test/kotlin/org/partiql/eval/internal/LetTests.kt
This file contains hidden or 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,259 @@ | ||
package org.partiql.eval.internal | ||
|
||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.parallel.Execution | ||
import org.junit.jupiter.api.parallel.ExecutionMode | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import org.partiql.spi.value.Datum | ||
import org.partiql.spi.value.Field | ||
|
||
/** | ||
* This test file exercises the `LET` clause in PartiQL. | ||
*/ | ||
class LetTests { | ||
|
||
@ParameterizedTest | ||
@MethodSource("successTestCases") | ||
@Execution(ExecutionMode.CONCURRENT) | ||
fun successTests(tc: SuccessTestCase) = tc.run() | ||
|
||
@ParameterizedTest | ||
@MethodSource("failureTestCases") | ||
@Execution(ExecutionMode.CONCURRENT) | ||
fun failureTests(tc: FailureTestCase) = tc.run() | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun successTestCases() = listOf( | ||
SuccessTestCase( | ||
name = "Basic LET usage 1", | ||
input = """ | ||
SELECT t.a, c | ||
FROM <<{ 'a': 1 , 'b': 2}>> AS t | ||
LET t.a*5 AS c | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("a", Datum.integer(1)), | ||
Field.of("c", Datum.integer(5)) | ||
) | ||
) | ||
), | ||
|
||
SuccessTestCase( | ||
name = "Basic LET usage 2", | ||
input = """ | ||
SELECT t.x, t.y, t.z * 2 AS double_z | ||
FROM ( | ||
SELECT A AS x, B AS y, new_val AS z | ||
FROM <<{ 'A': 1, 'B': 2, 'C': 3}>> | ||
LET B + C AS new_val | ||
) AS t; | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("x", Datum.integer(1)), | ||
Field.of("y", Datum.integer(2)), | ||
Field.of("double_z", Datum.integer(10)) | ||
) | ||
) | ||
), | ||
|
||
SuccessTestCase( | ||
name = "LET with JOIN operation", | ||
input = """ | ||
SELECT t.customer_name, t.order_total | ||
FROM ( | ||
SELECT | ||
c.name AS customer_name, | ||
total AS order_total | ||
FROM << | ||
{ 'id': 1, 'name': 'Alice' }, | ||
{ 'id': 2, 'name': 'Bob' } | ||
>> AS c | ||
JOIN << | ||
{ 'customer_id': 1, 'amount': 100 }, | ||
{ 'customer_id': 1, 'amount': 200 }, | ||
{ 'customer_id': 2, 'amount': 150 } | ||
>> AS o | ||
ON c.id = o.customer_id | ||
LET o.amount * c.id AS total | ||
) AS t; | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("customer_name", Datum.string("Alice")), | ||
Field.of("order_total", Datum.integer(100)) | ||
), | ||
Datum.struct( | ||
Field.of("customer_name", Datum.string("Alice")), | ||
Field.of("order_total", Datum.integer(200)) | ||
), | ||
Datum.struct( | ||
Field.of("customer_name", Datum.string("Bob")), | ||
Field.of("order_total", Datum.integer(300)) | ||
) | ||
) | ||
), | ||
|
||
SuccessTestCase( | ||
name = "LET with multiple items in data", | ||
input = """ | ||
SELECT t.x, t.y, t.z AS total | ||
FROM ( | ||
SELECT A AS x, B AS y, sum_val AS z | ||
FROM << | ||
{ 'A': 1, 'B': 2, 'C': 3 }, | ||
{ 'A': 10, 'B': 20, 'C': 30 } | ||
>> | ||
LET B + C AS sum_val | ||
) AS t; | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("x", Datum.integer(1)), | ||
Field.of("y", Datum.integer(2)), | ||
Field.of("total", Datum.integer(5)) | ||
), | ||
Datum.struct( | ||
Field.of("x", Datum.integer(10)), | ||
Field.of("y", Datum.integer(20)), | ||
Field.of("total", Datum.integer(50)) | ||
) | ||
) | ||
), | ||
SuccessTestCase( | ||
name = "LET referencing prior expressions", | ||
input = """ | ||
SELECT t.x, t.sum_val, t.double_sum | ||
FROM ( | ||
SELECT | ||
A AS x, | ||
sum_val, | ||
sum_val * 2 AS double_sum | ||
FROM << | ||
{ 'A': 3, 'B': 5 }, | ||
{ 'A': 10, 'B': 2 } | ||
>> | ||
LET A + B AS sum_val | ||
) AS t; | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("x", Datum.integer(3)), | ||
Field.of("sum_val", Datum.integer(8)), | ||
Field.of("double_sum", Datum.integer(16)) | ||
), | ||
Datum.struct( | ||
Field.of("x", Datum.integer(10)), | ||
Field.of("sum_val", Datum.integer(12)), | ||
Field.of("double_sum", Datum.integer(24)) | ||
) | ||
) | ||
), | ||
SuccessTestCase( | ||
name = "LET with multiple LET clauses 1", | ||
input = """ | ||
SELECT t.a, b, c | ||
FROM << { 'a': 1 }>> AS t | ||
LET t.a + 2 AS b, t.a * 3 AS c | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("a", Datum.integer(1)), | ||
Field.of("b", Datum.integer(3)), | ||
Field.of("c", Datum.integer(3)) | ||
) | ||
) | ||
), | ||
SuccessTestCase( | ||
name = "LET with multiple LET clauses 2", | ||
input = """ | ||
SELECT a, b, c, d, e | ||
FROM << { 'a': 1 , 'b':2}>> AS t | ||
LET t.a + 5 AS c, t.b+ 10 AS d, t.a + 15 AS e | ||
""".trimIndent(), | ||
expected = Datum.bagVararg( | ||
Datum.struct( | ||
Field.of("a", Datum.integer(1)), | ||
Field.of("b", Datum.integer(2)), | ||
Field.of("c", Datum.integer(6)), | ||
Field.of("d", Datum.integer(12)), | ||
Field.of("e", Datum.integer(16)) | ||
) | ||
) | ||
) | ||
) | ||
|
||
@JvmStatic | ||
fun failureTestCases() = listOf( | ||
FailureTestCase( | ||
name = "LET referencing undefined variable", | ||
input = """ | ||
SELECT t.x | ||
FROM ( | ||
SELECT A AS x | ||
FROM << { 'A': 1, 'B': 2 } >> | ||
LET nonexistent + B AS new_val | ||
) AS t; | ||
""".trimIndent() | ||
), | ||
FailureTestCase( | ||
name = "LET clause with ambiguous reference", | ||
input = """ | ||
SELECT t.z | ||
FROM ( | ||
SELECT new_val AS z | ||
FROM << { 'A': 1, 'B': 2 } >> | ||
-- 'new_val' references itself in LET, which is not allowed | ||
LET new_val + B AS new_val | ||
) AS t; | ||
""".trimIndent() | ||
), | ||
FailureTestCase( | ||
name = "Outside clauses referencing subquery's LET bindings", | ||
input = """ | ||
SELECT t.x, t.y, new_val | ||
FROM ( | ||
SELECT A AS x, B AS y | ||
FROM <<{ 'A': 1, 'B': 2, 'C': 3}>> | ||
LET B + C AS new_val | ||
) AS t; | ||
""".trimIndent() | ||
), | ||
FailureTestCase( | ||
name = "LET with invalid JOIN reference", | ||
input = """ | ||
SELECT t.customer_name, t.calculated_total | ||
FROM ( | ||
SELECT | ||
c.name AS customer_name, | ||
total AS calculated_total | ||
FROM << | ||
{ 'id': 1, 'name': 'Alice' }, | ||
{ 'id': 2, 'name': 'Bob' } | ||
>> AS c | ||
LEFT JOIN << | ||
{ 'customer_id': 1, 'amount': 100 }, | ||
{ 'customer_id': 2, 'amount': 150 } | ||
>> AS o | ||
ON c.id = o.customer_id | ||
-- This should fail because we're trying to reference 'missing_field' | ||
-- which doesn't exist in either joined table | ||
LET missing_field + o.amount AS total | ||
) AS t; | ||
""".trimIndent(), | ||
) | ||
) | ||
} | ||
|
||
// Example of a test that might need special handling or a skip | ||
@Test | ||
@Disabled("Demonstration of a scenario needing further investigation.") | ||
fun disabledTestExample() { | ||
// Implementation left blank or used for demonstration | ||
} | ||
} |
This file contains hidden or 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.
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.
👏
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.
will add tests in amendment