-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add match operator for tsvector and jsonb Shared with Jsonb and TsVector type Add MatchOperatorExpressionMixin for SqlExp and annotations Add PostgreSqlTypeResolver matchOperatorExpression boolean result types Operator resolver type e.g || concat * Add tests Integration tests for TsVector || concat test * add ts_rank function Useful text search function ts_rank returns a REAL type * Add function websearch_to_tsquery Seems to be commonly used for searches - returns ts_query type that is a text type Tested locally with ` SELECT * FROM Recipes WHERE search @@ websearch_to_tsquery('english', ?); `
- Loading branch information
Showing
9 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
...in/app/cash/sqldelight/dialects/postgresql/grammar/mixins/MatchOperatorExpressionMixin.kt
This file contains 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,35 @@ | ||
package app.cash.sqldelight.dialects.postgresql.grammar.mixins | ||
|
||
import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlMatchOperatorExpression | ||
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 "@@" match operator is used by TsVector and Jsonb | ||
* The type annotation is performed here for both types | ||
* For other json operators see JsonExpressionMixin | ||
*/ | ||
internal abstract class MatchOperatorExpressionMixin(node: ASTNode) : | ||
SqlCompositeElementImpl(node), | ||
SqlBinaryExpr, | ||
PostgreSqlMatchOperatorExpression { | ||
|
||
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 == "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>() | ||
} | ||
} |
This file contains 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
This file contains 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
8 changes: 8 additions & 0 deletions
8
...ts/postgresql/src/testFixtures/resources/fixtures_postgresql/text-search-functions/Test.s
This file contains 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,8 @@ | ||
CREATE TABLE t1 ( | ||
c1 TSVECTOR | ||
); | ||
|
||
INSERT INTO t1 (c1) VALUES ('the rain in spain falls mainly on the plains') ; | ||
|
||
SELECT c1 @@ 'fail' | ||
FROM t1; |
28 changes: 28 additions & 0 deletions
28
...n-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/TextSearch.sq
This file contains 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,28 @@ | ||
CREATE TABLE search( | ||
content TSVECTOR NOT NULL | ||
); | ||
|
||
insertLiteral: | ||
INSERT INTO search (content) VALUES (?); | ||
|
||
contains: | ||
SELECT content @@ ? | ||
FROM search; | ||
|
||
search: | ||
SELECT * | ||
FROM search WHERE content @@ ?; | ||
|
||
tsQuery: | ||
SELECT to_tsquery(?); | ||
|
||
tsVector: | ||
SELECT to_tsvector(?); | ||
|
||
concat: | ||
SELECT content || to_tsvector(?) | ||
FROM search; | ||
|
||
rank: | ||
SELECT ts_rank(content, ?) | ||
FROM search; |
This file contains 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