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

Fixes SqlDialect output for unary ops #1384

Merged
merged 1 commit into from
Mar 9, 2024
Merged
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 @@ -36,6 +36,7 @@ Thank you to all who have contributed!
### Deprecated

### Fixed
- partiql-ast: `SqlDialect` will wrap unary ops (`NOT`, `+`, `-`) in parens

### Removed

Expand Down
10 changes: 6 additions & 4 deletions partiql-ast/src/main/kotlin/org/partiql/ast/sql/SqlDialect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,15 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {

override fun visitExprUnary(node: Expr.Unary, head: SqlBlock): SqlBlock {
val op = when (node.op) {
Expr.Unary.Op.NOT -> "NOT "
Expr.Unary.Op.POS -> "+"
Expr.Unary.Op.NEG -> "-"
Expr.Unary.Op.NOT -> "NOT ("
Expr.Unary.Op.POS -> "+("
Expr.Unary.Op.NEG -> "-("
}
var h = head
h = h concat r(op)
return visitExprWrapped(node.expr, h)
h = visitExprWrapped(node.expr, h)
h = h concat r(")")
return h
}

override fun visitExprBinary(node: Expr.Binary, head: SqlBlock): SqlBlock {
Expand Down
45 changes: 42 additions & 3 deletions partiql-ast/src/test/kotlin/org/partiql/ast/sql/SqlDialectTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,63 @@ class SqlDialectTest {

@JvmStatic
fun exprOperators() = listOf(
expect("NOT NULL") {
expect("NOT (NULL)") {
exprUnary {
op = Expr.Unary.Op.NOT
expr = NULL
}
},
expect("+NULL") {
expect("+(NULL)") {
exprUnary {
op = Expr.Unary.Op.POS
expr = NULL
}
},
expect("-NULL") {
expect("-(NULL)") {
exprUnary {
op = Expr.Unary.Op.NEG
expr = NULL
}
},
expect("NOT (NOT (NULL))") {
exprUnary {
op = Expr.Unary.Op.NOT
expr = exprUnary {
op = Expr.Unary.Op.NOT
expr = NULL
}
}
},
expect("+(+(NULL))") {
exprUnary {
op = Expr.Unary.Op.POS
expr = exprUnary {
op = Expr.Unary.Op.POS
expr = NULL
}
}
},
expect("-(-(NULL))") {
exprUnary {
op = Expr.Unary.Op.NEG
expr = exprUnary {
op = Expr.Unary.Op.NEG
expr = NULL
}
}
},
expect("+(-(+(NULL)))") {
exprUnary {
op = Expr.Unary.Op.POS
expr = exprUnary {
op = Expr.Unary.Op.NEG
expr = exprUnary {
op = Expr.Unary.Op.POS
expr = NULL
}
}
}
},
expect("NULL + NULL") {
exprBinary {
op = Expr.Binary.Op.PLUS
Expand Down
Loading