Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix a compilation error with migration files when CREATE UNIQUE INDEX referenced by FOREIGN KEY (https://github.com/sqldelight/sql-psi/pull/732)
- Fix insert statement exposes columns to a select statement when used as the row source (https://github.com/sqldelight/sql-psi/pull/750)

## [0.7.3] - 2026-03-13
[0.7.3]: https://github.com/sqldelight/sql-psi/releases/tag/0.7.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.alecstrong.sql.psi.core.psi.SqlColumnExpr
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.alecstrong.sql.psi.core.psi.SqlInsertStmt
import com.alecstrong.sql.psi.core.psi.SqlIsExpr
import com.alecstrong.sql.psi.core.psi.SqlLiteralExpr
import com.alecstrong.sql.psi.core.psi.SqlParenExpr
Expand All @@ -21,6 +22,7 @@ import com.alecstrong.sql.psi.core.psi.asColumns
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNamedElement
import com.intellij.psi.util.PsiTreeUtil

internal abstract class SelectStmtMixin(node: ASTNode) :
SqlCompositeElementImpl(node), SqlSelectStmt, FromQuery {
Expand Down Expand Up @@ -57,9 +59,7 @@ internal abstract class SelectStmtMixin(node: ASTNode) :

override fun queryAvailable(child: PsiElement): Collection<QueryResult> {
if (child in exprList || child in (groupBy?.exprList ?: emptyList())) {
val available =
fromQuery().map { it.copy(adjacent = true) } +
super.queryAvailable(this).map { it.copy(adjacent = false) }
val available = fromQuery().map { it.copy(adjacent = true) } + availableFromParent(this)
if (ignoreParentProjection) return available

val projection =
Expand All @@ -80,11 +80,12 @@ internal abstract class SelectStmtMixin(node: ASTNode) :
return available + projection
}
if (child in resultColumnList) {
return fromQuery().map { it.copy(adjacent = true) } +
super.queryAvailable(this).map { it.copy(adjacent = false) }
return fromQuery().map { it.copy(adjacent = true) } + availableFromParent(this)
}
if (child == joinClause) return super.queryAvailable(child)
return super.queryAvailable(child)

return if (child == joinClause && this.isInsertSelect())
keepSingleRowTables(super.queryAvailable(child))
else super.queryAvailable(child)
}

override fun queryExposed() = queryExposed.forFile(containingFile)
Expand All @@ -96,6 +97,19 @@ internal abstract class SelectStmtMixin(node: ASTNode) :
return emptyList()
}

private fun availableFromParent(child: PsiElement): Collection<QueryResult> {
val available = super.queryAvailable(child).map { it.copy(adjacent = false) }
return if (isInsertSelect()) keepSingleRowTables(available) else available
}

private fun PsiElement.isInsertSelect(): Boolean {
return PsiTreeUtil.getParentOfType(this, SqlInsertStmt::class.java) != null
}

private fun keepSingleRowTables(queryResults: Collection<QueryResult>): Collection<QueryResult> {
return queryResults.filter { it.table is SingleRow }
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include only inherited SingleRow tables that are trigger table rows and on conflict table rows.

}

private fun PsiElement.nonNullIn(whereExpr: SqlExpr): Boolean {
if (this is SqlColumnAlias) return source().nonNullIn(whereExpr)
if (this is SqlResultColumn) return expr?.nonNullIn(whereExpr) ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE destination (
destination_id INTEGER PRIMARY KEY,
t TEXT NOT NULL
);

CREATE TABLE source (
source_id INTEGER PRIMARY KEY,
t TEXT NOT NULL
);

INSERT INTO destination (destination_id, t)
SELECT destination_id, t FROM source;

INSERT INTO destination (destination_id, t)
SELECT source_id, t FROM source
WHERE destination_id > 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test.s line 12:7 - No column found with name destination_id
Test.s line 16:6 - No column found with name destination_id
14 changes: 14 additions & 0 deletions core/src/testFixtures/resources/fixtures/insert-select-join/Test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE destination (
name TEXT
);

CREATE TABLE source (
name TEXT NOT NULL
);

INSERT INTO destination (name)
SELECT source.name
FROM source
LEFT JOIN destination ON source.name = destination.name
WHERE destination.name IS NULL;