Skip to content

Commit

Permalink
Update JoinClauseMixin to support more ANSI SQL joins (#616)
Browse files Browse the repository at this point in the history
* Update JoinClauseMixin

Add Ansi join operators RIGHT and FULL

Also LATERAL modifier

These are added as forward declarations in the SqlLexer.flex that are needed for dialects to override

Keep core grammar with LEFT join

This allows the existing dialects to work with LEFT join and enables dialects to add RIGHT, FULL where supported, e.g PostgreSql and latest Sqlite dialect

* Remove LATERAL

LATERAL keyword is used as table/subquery and join operation.

Needs special handling
  • Loading branch information
griffio authored Mar 27, 2024
1 parent 6ea26b9 commit 5c953c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ STRING=('([^'])*'|\"([^\"])*\")
"INDEXED" { return INDEXED; }
"NATURAL" { return NATURAL; }
"LEFT" { return LEFT; }
"RIGHT" { return RIGHT; }
"FULL" { return FULL; }
"OUTER" { return OUTER; }
"INNER" { return INNER; }
"CROSS" { return CROSS; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.alecstrong.sql.psi.core.psi.QueryElement.QueryResult
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.SqlJoinClause
import com.alecstrong.sql.psi.core.psi.SqlJoinConstraint
import com.alecstrong.sql.psi.core.psi.SqlJoinOperator
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNamedElement
import com.intellij.psi.tree.TokenSet

internal abstract class JoinClauseMixin(
node: ASTNode,
Expand Down Expand Up @@ -61,10 +63,7 @@ internal abstract class JoinClauseMixin(
var columns = query.flatMap { it.columns }
var synthesizedColumns = query.flatMap { it.synthesizedColumns }

if (operator.node.findChildByType(
SqlTypes.LEFT,
) != null
) {
if (supportsJoinOperator(operator)) {
columns = columns.map { it.copy(nullable = true) }
synthesizedColumns = synthesizedColumns.map { it.copy(nullable = true) }
}
Expand All @@ -90,5 +89,15 @@ internal abstract class JoinClauseMixin(
return@ModifiableFileLazy queryAvailable
}

private fun supportsJoinOperator(operator: SqlJoinOperator): Boolean {
return operator.node.findChildByType(
TokenSet.create(
SqlTypes.LEFT_JOIN_OPERATOR,
SqlTypes.RIGHT_JOIN_OPERATOR,
SqlTypes.FULL_JOIN_OPERATOR,
),
) != null
}

override fun queryExposed() = queryExposed.forFile(containingFile)
}
7 changes: 6 additions & 1 deletion core/src/main/kotlin/com/alecstrong/sql/psi/core/sql.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,13 @@ result_column ::= ( MULTIPLY
"com.alecstrong.sql.psi.core.psi.SqlCompositeElement"
]
}

left_join_operator ::= LEFT
right_join_operator ::= RIGHT
full_join_operator ::= FULL

join_operator ::= ( COMMA
| [ NATURAL ] [ LEFT [ OUTER ] | INNER | CROSS ] JOIN )
| [ NATURAL ] [ left_join_operator [ OUTER ] | INNER | CROSS ] JOIN )
join_constraint ::= [ ON expr | USING LP column_name ( COMMA column_name ) * RP ]
ordering_term ::= expr [ COLLATE collation_name ] [ ASC | DESC ] {
mixin = "com.alecstrong.sql.psi.core.psi.mixins.OrderByMixin"
Expand Down

0 comments on commit 5c953c5

Please sign in to comment.