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

Bitwise and plan typer and WIki. #1232

Merged
merged 7 commits into from
Sep 28, 2023
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Thank you to all who have contributed!
## [Unreleased]

### Added
- Support parsing and evaluation of Bitwise AND operator (&).
- The Bitwise And Operator only works for integer operands.
- The operator precedence may change based on the pending operator precedence [RFC](https://github.com/partiql/partiql-docs/issues/50).
- Support parsing, planning, and evaluation of Bitwise AND operator (&).
- The Bitwise And Operator only works for integer operands.
- The operator precedence may change based on the pending operator precedence [RFC](https://github.com/partiql/partiql-docs/issues/50).

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,29 @@ Example
CAST(5 as decimal(3,2)) % CAST(2 as DECIMAL(3,2)) -- 1
-- IN HONOR PARAMETER MODE
CAST(5 as decimal(3,2)) % CAST(2 as DECIMAL(3,2)) -- 1.00
```

### Bitwise And
Performs a bitwise logical AND operation between two integer values.

Syntax
: `expression & expression`

Example:
```sql
2 & 6 -- 0010 & 0110 = 0010 = 2
```

Return Type:
: If one operand is of `INT` type, returns `INT` type.
: Else if one operand is of INT8 type, returns `INT8` type.
: Else if one operand is of INT4 type, returns `INT4` type.
: Else return `INT2` type.

Note:
: Type precedence of the bitwise operator is lower than the plus/minus operator and higher than the predicates.
Copy link
Member

Choose a reason for hiding this comment

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

Could link the partiql-docs issue since this could change: https://github.com/partiql/partiql-docs/issues/50.


```sql
2 & 6 + 1 -- 2 & (6 + 1) = 2 & 7 = 2
(2 & 6) + 1 -- 2 + 1 = 3
```
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {
Expr.Binary.Op.GTE -> ">="
Expr.Binary.Op.LT -> "<"
Expr.Binary.Op.LTE -> "<="
Expr.Binary.Op.BITWISE_AND -> "&"
}
var h = head
h = visitExpr(node.lhs, h)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ internal object PlanTyper : PlanRewriter<PlanTyper.Context>() {
true -> computeReturnTypeForNAry(args, PlanTyper::inferBinaryArithmeticOp)
false -> StaticType.NUMERIC // continuation type to prevent incompatible types and unknown errors from propagating
}
Rex.Binary.Op.BITWISE_AND -> when (hasValidOperandTypes(args, node.op.name, ctx) { it is IntType }) {
true -> computeReturnTypeForNAry(args, PlanTyper::inferBinaryArithmeticOp)
false -> StaticType.unionOf(StaticType.INT2, StaticType.INT4, StaticType.INT8, StaticType.INT) // continuation type to prevent incompatible types and unknown errors from propagating
}

Rex.Binary.Op.CONCAT -> when (hasValidOperandTypes(args, node.op.name, ctx) { it.isText() }) {
true -> computeReturnTypeForNAry(args, PlanTyper::inferConcatOp)
false -> StaticType.STRING // continuation type to prevent incompatible types and unknown errors from propagating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ internal object RexConverter : PartiqlAst.VisitorFold<RexConverter.Ctx>() {
)
}

override fun walkExprBitwiseAnd(node: PartiqlAst.Expr.BitwiseAnd, accumulator: Ctx) = visit(node) {
Plan.rexBinary(
lhs = convert(node.operands[0]),
rhs = convert(node.operands[1]),
op = Rex.Binary.Op.BITWISE_AND,
type = StaticType.unionOf(
StaticType.INT2, StaticType.INT4, StaticType.INT8, StaticType.INT
)
)
}

override fun walkExprConcat(node: PartiqlAst.Expr.Concat, ctx: Ctx) = visit(node) {
Plan.rexBinary(
lhs = convert(node.operands[0]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import org.partiql.types.BagType
import org.partiql.types.ListType
import org.partiql.types.StaticType
import org.partiql.types.StaticType.Companion.INT
import org.partiql.types.StaticType.Companion.MISSING
import org.partiql.types.StaticType.Companion.NULL
import org.partiql.types.StaticType.Companion.STRING
import org.partiql.types.StaticType.Companion.unionOf
import org.partiql.types.StructType
Expand Down Expand Up @@ -1070,6 +1072,95 @@ class PartiQLSchemaInferencerTests {
)
}
),
SuccessTestCase(
name = "BITWISE_AND_1",
query = "1 & 2",
expected = StaticType.INT
),
// casting to a parameterized type produced Missing.
SuccessTestCase(
name = "BITWISE_AND_2",
query = "CAST(1 AS INT2) & CAST(2 AS INT2)",
expected = StaticType.unionOf(StaticType.INT2, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_3",
query = "CAST(1 AS INT4) & CAST(2 AS INT4)",
expected = StaticType.unionOf(StaticType.INT4, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_4",
query = "CAST(1 AS INT8) & CAST(2 AS INT8)",
expected = StaticType.unionOf(StaticType.INT8, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_5",
query = "CAST(1 AS INT2) & CAST(2 AS INT4)",
expected = StaticType.unionOf(StaticType.INT4, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_6",
query = "CAST(1 AS INT2) & CAST(2 AS INT8)",
expected = StaticType.unionOf(StaticType.INT8, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_7",
query = "CAST(1 AS INT2) & 2",
expected = StaticType.unionOf(StaticType.INT, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_8",
query = "CAST(1 AS INT4) & CAST(2 AS INT8)",
expected = StaticType.unionOf(StaticType.INT8, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_9",
query = "CAST(1 AS INT4) & 2",
expected = StaticType.unionOf(StaticType.INT, MISSING)
),
SuccessTestCase(
name = "BITWISE_AND_10",
query = "CAST(1 AS INT8) & 2",
expected = StaticType.unionOf(StaticType.INT, MISSING)
),
ErrorTestCase(
name = "BITWISE_AND_NULL_OPERAND",
query = "1 & NULL",
expected = StaticType.NULL,
problemHandler = assertProblemExists {
Problem(
UNKNOWN_PROBLEM_LOCATION,
SemanticProblemDetails.ExpressionAlwaysReturnsNullOrMissing
)
}
),
ErrorTestCase(
name = "BITWISE_AND_MISSING_OPERAND",
query = "1 & MISSING",
expected = StaticType.MISSING,
problemHandler = assertProblemExists {
Problem(
UNKNOWN_PROBLEM_LOCATION,
SemanticProblemDetails.ExpressionAlwaysReturnsNullOrMissing
)
}
),
ErrorTestCase(
name = "BITWISE_AND_NON_INT_OPERAND",
query = "1 & 'NOT AN INT'",
expected = StaticType.MISSING,
problemHandler = assertProblemExists {
Problem(
UNKNOWN_PROBLEM_LOCATION,
SemanticProblemDetails.IncompatibleDatatypesForOp(
listOf(
INT, STRING
),
Rex.Binary.Op.BITWISE_AND.name
)
)
}
),
)

private fun assertProblemExists(problem: () -> Problem) = ProblemHandler { problems, ignoreSourceLocation ->
Expand Down
2 changes: 1 addition & 1 deletion partiql-parser/src/main/antlr/PartiQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ joinType
* 2. Unary plus, minus (ex: -a, +a)
* 3. Multiplication, Division, Modulo (ex: a * b)
* 4. Addition, Subtraction (ex: a + b)
* 5. Other operators (ex: a || b)
* 5. Other operators (ex: a || b, a & b)
* 6. Predicates (ex: a LIKE b, a < b, a IN b, a = b)
* 7. IS true/false. Not yet implemented in PartiQL, but defined in SQL-92. (ex: a IS TRUE)
* 8. NOT (ex: NOT a)
Expand Down
2 changes: 1 addition & 1 deletion partiql-plan/src/main/resources/partiql_plan.ion
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ rex::[
lhs: rex,
rhs: rex,
op: [
PLUS, MINUS, TIMES, DIV, MODULO, CONCAT,
PLUS, MINUS, TIMES, DIV, MODULO, CONCAT, BITWISE_AND,
AND, OR,
EQ, NEQ, GTE, GT, LT, LTE,
],
Expand Down
Loading