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

Add 4933 PostgreSql array contains operators #5228

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
IntermediateType(PostgreSqlType.JSON)
}
}
matchOperatorExpression != null || regexMatchOperatorExpression != null || booleanNotExpression != null -> {
matchOperatorExpression != null || regexMatchOperatorExpression != null || containsOperatorExpression != null || booleanNotExpression != null -> {
IntermediateType(BOOLEAN)
}
atTimeZoneOperatorExpression != null -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ compound_select_stmt ::= [ {with_clause} ] {select_stmt} ( {compound_operator}
override = true
}

extension_expr ::= at_time_zone_operator_expression | regex_match_operator_expression | match_operator_expression | array_agg_stmt| string_agg_stmt | json_expression | boolean_not_expression | window_function_expr {
extension_expr ::= contains_operator_expression | at_time_zone_operator_expression | regex_match_operator_expression | match_operator_expression | array_agg_stmt| string_agg_stmt | json_expression | boolean_not_expression | window_function_expr {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlExtensionExprImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlExtensionExpr"
override = true
Expand Down Expand Up @@ -434,9 +434,15 @@ json_expression ::= {column_expr} ( jsona_binary_operator | jsonb_binary_operato
}
jsona_binary_operator ::= '->' | '->>' | '#>' | '#>>'
jsonb_binary_operator ::= '#-'
jsonb_boolean_operator ::= '@>' | '<@' | '@?' | '??|' | '??&' | '??'
jsonb_boolean_operator ::= '@?' | '??|' | '??&' | '??'
contains_operator ::= '@>' | '<@' | '&&'
match_operator ::= '@@'

contains_operator_expression ::= ( {function_expr} | {column_expr} ) contains_operator <<expr '-1'>> {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.ContainsOperatorExpressionMixin"
pin = 2
}

match_operator_expression ::= ( {function_expr} | {column_expr} ) match_operator <<expr '-1'>> {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.MatchOperatorExpressionMixin"
pin = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app.cash.sqldelight.dialects.postgresql.grammar.mixins

import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlContainsOperatorExpression
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.SqlBinaryExpr
import com.alecstrong.sql.psi.core.psi.SqlColumnDef
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.intellij.lang.ASTNode

/**
* The "@>" and "<@" contains operators are used by Array, TsVector and Jsonb
* The type annotation is performed here for these types
* For additional json operators see JsonExpressionMixin
*/
internal abstract class ContainsOperatorExpressionMixin(node: ASTNode) :
SqlCompositeElementImpl(node),
SqlBinaryExpr,
PostgreSqlContainsOperatorExpression {

override fun annotate(annotationHolder: SqlAnnotationHolder) {
val columnType = ((firstChild.firstChild.reference?.resolve() as? SqlColumnName)?.parent as? SqlColumnDef)?.columnType?.typeName?.text
when {
columnType == null -> super.annotate(annotationHolder)
columnType.endsWith("[]") -> super.annotate(annotationHolder)
columnType == "JSONB" -> super.annotate(annotationHolder)
columnType == "JSON" -> annotationHolder.createErrorAnnotation(firstChild.firstChild, "Left side of jsonb expression must be a jsonb column.")
columnType != "TSVECTOR" -> annotationHolder.createErrorAnnotation(firstChild.firstChild, "Left side of match expression must be a tsvector column.")
}
super.annotate(annotationHolder)
}
override fun getExprList(): List<SqlExpr> {
return children.filterIsInstance<SqlExpr>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PostgreSqlFixturesTest(name: String, fixtureRoot: File) : FixturesTest(nam
"BLOB" to "TEXT",
"id TEXT GENERATED ALWAYS AS (2) UNIQUE NOT NULL" to "id TEXT GENERATED ALWAYS AS (2) STORED UNIQUE NOT NULL",
"'(', ')', ',', '.', <binary like operator real>, BETWEEN or IN expected, got ','"
to "'#-', '(', ')', ',', '.', <binary like operator real>, <jsona binary operator real>, <jsonb boolean operator real>, <regex match operator real>, '@@', AT, BETWEEN or IN expected, got ','",
to "'#-', '(', ')', ',', '.', <binary like operator real>, <contains operator real>, <jsona binary operator real>, <jsonb boolean operator real>, <regex match operator real>, '@@', AT, BETWEEN or IN expected, got ','",
)

override fun setupDialect() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE T(
a INT[],
b INT[]
);

SELECT a @> ?, b <@ ?
FROM T;

SELECT *
FROM T
WHERE a @> ?;

SELECT *
FROM T
WHERE b <@ a;

SELECT *
FROM T
WHERE b && a;
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
CREATE TABLE arrays(
intArray INTEGER[] AS kotlin.Array<kotlin.UInt>,
textArray TEXT[]
textArray VARCHAR[],
vcharArray VARCHAR[]
);

insertAndReturn:
INSERT INTO arrays
VALUES (?, ?)
VALUES (?, ?, ?)
RETURNING *;

contains:
SELECT intArray @> ?, vcharArray <@ ?
FROM arrays;

containsFirst:
SELECT *
FROM arrays
WHERE intArray @> ?;

containsSecond:
SELECT *
FROM arrays
WHERE vcharArray <@ ?;

overlaps:
SELECT *
FROM arrays
WHERE intArray && ?;
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,45 @@ class PostgreSqlTest {
}

@Test fun testArrays() {
with(database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two")).executeAsOne()) {
with(database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two"), arrayOf("a", "b")).executeAsOne()) {
assertThat(intArray!!.asList()).containsExactly(1u, 2u).inOrder()
assertThat(textArray!!.asList()).containsExactly("one", "two").inOrder()
assertThat(vcharArray!!.asList()).containsExactly("a", "b").inOrder()
}
}

@Test fun testArrayContains() {
database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two"), arrayOf("a", "b")).executeAsOne()
with(database.arraysQueries.contains(arrayOf(1u, 2u), arrayOf("a", "b")).executeAsList()) {
assertThat(first().expr).isTrue()
assertThat(first().expr_).isTrue()
}
}

@Test fun testArrayContainsFirst() {
database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two"), arrayOf("a", "b")).executeAsOne()
with(database.arraysQueries.containsFirst(arrayOf(1u, 2u)).executeAsList()) {
assertThat(first().intArray!!.asList()).containsExactly(1u, 2u).inOrder()
assertThat(first().textArray!!.asList()).containsExactly("one", "two").inOrder()
assertThat(first().vcharArray!!.asList()).containsExactly("a", "b").inOrder()
}
}

@Test fun testArrayContainsSecond() {
database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two"), arrayOf("a", "b")).executeAsOne()
with(database.arraysQueries.containsSecond(arrayOf("a", "b")).executeAsList()) {
assertThat(first().intArray!!.asList()).containsExactly(1u, 2u).inOrder()
assertThat(first().textArray!!.asList()).containsExactly("one", "two").inOrder()
assertThat(first().vcharArray!!.asList()).containsExactly("a", "b").inOrder()
}
}

@Test fun testArrayOverlaps() {
database.arraysQueries.insertAndReturn(arrayOf(1u, 2u), arrayOf("one", "two"), arrayOf("a", "b")).executeAsOne()
with(database.arraysQueries.overlaps(arrayOf(1u, 2u)).executeAsList()) {
assertThat(first().intArray!!.asList()).containsExactly(1u, 2u).inOrder()
assertThat(first().textArray!!.asList()).containsExactly("one", "two").inOrder()
assertThat(first().vcharArray!!.asList()).containsExactly("a", "b").inOrder()
}
}

Expand Down