Skip to content

Commit 4038ecf

Browse files
authored
Fix: enhance comparison operators to support Date and Datetime types (#228)
* Fix: enhance comparison operators to support Date and Datetime types * Releasing new version
1 parent d8ad7c9 commit 4038ecf

3 files changed

Lines changed: 65 additions & 12 deletions

File tree

docs/public/packages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"packageId": "04tRb000003z0hJIAQ",
2+
"packageId": "04tRb0000042CNlIAM",
33
"componentPackageId": "04tRb0000012Mv8IAE"
44
}

expression-src/main/src/interpreter/Interpreter.cls

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,72 @@ public virtual with sharing class Interpreter implements Visitor {
139139
return ((Decimal)left).pow(Integer.valueOf(right));
140140
}
141141
when GREATER {
142-
checkNumberOperand(binary.operator, left, right);
143-
return (Decimal)left > (Decimal)right;
142+
// Check for numbers
143+
if (left instanceof Decimal && right instanceof Decimal) {
144+
return (Decimal)left > (Decimal)right;
145+
}
146+
147+
if (left instanceof Date && right instanceof Date) {
148+
return (Date)left > (Date)right;
149+
}
150+
151+
if (left instanceof Datetime && right instanceof Datetime) {
152+
return (Datetime)left > (Datetime)right;
153+
}
154+
155+
throw new Exceptions.RuntimeException(
156+
binary.operator,
157+
'Error executing ' + binary.operator.lexeme + ' operator: operands must be numbers, dates, or datetimes.'
158+
);
144159
}
145160
when GREATER_EQUAL {
146-
checkNumberOperand(binary.operator, left, right);
147-
return (Decimal)left >= (Decimal)right;
161+
if (left instanceof Decimal && right instanceof Decimal) {
162+
return (Decimal)left >= (Decimal)right;
163+
}
164+
165+
if (left instanceof Date && right instanceof Date) {
166+
return (Date)left >= (Date)right;
167+
}
168+
169+
if (left instanceof Datetime && right instanceof Datetime) {
170+
return (Datetime)left >= (Datetime)right;
171+
}
172+
173+
throw new Exceptions.RuntimeException(
174+
binary.operator,
175+
'Error executing ' + binary.operator.lexeme + ' operator: operands must be numbers, dates, or datetimes.'
176+
);
148177
}
149178
when LESS {
150-
checkNumberOperand(binary.operator, left, right);
151-
return (Decimal)left < (Decimal)right;
179+
if (left instanceof Decimal && right instanceof Decimal) {
180+
return (Decimal)left < (Decimal)right;
181+
}
182+
183+
if (left instanceof Date && right instanceof Date) {
184+
return (Date)left < (Date)right;
185+
}
186+
187+
if (left instanceof Datetime && right instanceof Datetime) {
188+
return (Datetime)left < (Datetime)right;
189+
}
190+
191+
throw new Exceptions.RuntimeException(
192+
binary.operator,
193+
'Error executing ' + binary.operator.lexeme + ' operator: operands must be numbers, dates, or datetimes.'
194+
);
152195
}
153196
when LESS_EQUAL {
154-
checkNumberOperand(binary.operator, left, right);
155-
return (Decimal)left <= (Decimal)right;
197+
if (left instanceof Decimal && right instanceof Decimal) {
198+
return (Decimal)left <= (Decimal)right;
199+
}
200+
201+
if (left instanceof Date && right instanceof Date) {
202+
return (Date)left <= (Date)right;
203+
}
204+
205+
if (left instanceof Datetime && right instanceof Datetime) {
206+
return (Datetime)left <= (Datetime)right;
207+
}
156208
}
157209
when EQUAL, EQUAL_EQUAL {
158210
return left == right;
@@ -567,7 +619,7 @@ public virtual with sharing class Interpreter implements Visitor {
567619

568620
return new ShouldNotAdd();
569621
}
570-
622+
571623
public Object visit(Expr.PipelineFunction pipelineFunction) {
572624
throw new Exceptions.UnknownException(
573625
'Pipelines should first be desugared by the PipeResolver before reaching the Interpreter.'

sfdx-project_packaging.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"package": "Expression",
55
"versionName": "Version 1.36",
6-
"versionNumber": "1.44.0.NEXT",
6+
"versionNumber": "1.45.0.NEXT",
77
"path": "expression-src",
88
"default": false,
99
"versionDescription": "Expression core language",
@@ -73,6 +73,7 @@
7373
"Expression@1.41.0-1": "04tRb000003tVPdIAM",
7474
"Expression@1.42.0-1": "04tRb000003xe4XIAQ",
7575
"Expression@1.43.0-1": "04tRb000003xzXCIAY",
76-
"Expression@1.44.0-1": "04tRb000003z0hJIAQ"
76+
"Expression@1.44.0-1": "04tRb000003z0hJIAQ",
77+
"Expression@1.45.0-1": "04tRb0000042CNlIAM"
7778
}
7879
}

0 commit comments

Comments
 (0)