Skip to content

Commit

Permalink
@W-15280006, W-15279960 (SDB null casting), W-14444417 (SDB WeekDay f…
Browse files Browse the repository at this point in the history
…unction) (#147)

* @W-15280006 null casting

* @W-15280006, W-15279960 (SDB null casting), W-14444417 (SDB WeekDay function)
  • Loading branch information
bairenlong-sfdc committed Jul 2, 2024
1 parent 21e1073 commit 9cdab70
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 426 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, St
String sql;
String guard;
if (inputDataType == FormulaDateTime.class) {
sql = getSqlHooks(context).sqlConvertDateTimeToDate(args[0], USERS_TIMEZONE_ID_MARKER, USERS_TIMEZONE_OFFSET_MARKER);
sql = getSqlHooks(context).sqlConvertDateTimeToDate(args[0], USERS_TIMEZONE_ID_MARKER, USERS_TIMEZONE_OFFSET_MARKER);
guard = SQLPair.generateGuard(guards, null);
} else {
sql = String.format(getSqlHooks(context).sqlToDateIso(), args[0]);
Expand All @@ -78,15 +78,15 @@ public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, St
} else {
// we know it's false
guard = SQLPair.generateGuard(guards, "0=0");
sql = String.format(getSqlHooks(context).sqlToDate(Date.class), "NULL");
sql = getSqlHooks(context).sqlCastNull("NULL", Date.class);
}
} else {
// Guard protects against malformed dates as strings. It assumes all months have 31 days. Validates invalid months. Accepts years from 0000-9999.
guard = SQLPair
.generateGuard(
guards,
String.format(
getSqlHooks(context).sqlDateValueGuard(),
getSqlHooks(context).sqlDateValueGuard(),
args[0]));
/*Other 2 options of guards, probably when we get versioning we can switch to either one of these:
Expand Down Expand Up @@ -159,7 +159,7 @@ public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[]

public static class OperatorDateValueFormulaCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;
private final boolean isDateTime;
private final boolean isDateTime;

public OperatorDateValueFormulaCommand(FormulaCommandInfo formulaCommandInfo, boolean isDateTime) {
super(formulaCommandInfo);
Expand Down Expand Up @@ -189,11 +189,11 @@ public void execute(FormulaRuntimeContext context, Deque<Object> stack) throws F
}
}
} else {
try {
value = parseDate(checkStringType(input));
} catch (FormulaDateException x) {
FormulaEngine.getHooks().handleFormulaDateException(x);
}
try {
value = parseDate(checkStringType(input));
} catch (FormulaDateException x) {
FormulaEngine.getHooks().handleFormulaDateException(x);
}
}
}

Expand All @@ -208,7 +208,7 @@ protected static boolean isDateValid(String date) {
return false;
}
}

private static Pattern DATE_PATTERN = Pattern.compile("\\d{4}-.*");

// Convert from string of the form "YYYY-MM-DD" to Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, St
} else {
// we know it's false
guard = SQLPair.generateGuard(guards, "0=0");
sql = "NULL";
sql = getSqlHooks(context).sqlCastNull("NULL", FormulaDateTime.class);
}
} else {
// Guard protects against malformed dates as strings
Expand All @@ -73,14 +73,14 @@ public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, St
guards,
String
.format(
getSqlHooks(context).sqlDatetimeValueGuard(),
getSqlHooks(context).sqlDatetimeValueGuard(),
args[0]));
}
}

return new SQLPair(sql, guard);


}

@Override
Expand All @@ -94,7 +94,7 @@ public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[]
return JsValue.forNonNullResult(context.getJsEngMod() + ".parseDateTime("+ args[0].js + ")", args);
}
}

@Override
public Type validate(FormulaAST node, FormulaContext context, FormulaProperties properties) throws FormulaException {
if (node.getNumberOfChildren() != 1) {
Expand All @@ -115,7 +115,7 @@ public Type validate(FormulaAST node, FormulaContext context, FormulaProperties
class OperatorDatetimeValueFormulaCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;

public OperatorDatetimeValueFormulaCommand(FormulaCommandInfo formulaCommandInfo) {
public OperatorDatetimeValueFormulaCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

Expand All @@ -130,11 +130,11 @@ public void execute(FormulaRuntimeContext context, Deque<Object> stack) throws F
} else if (input instanceof Date) {
value = new FormulaDateTime((Date)input);
} else {
try {
value = parseDateTime(checkStringType(input));
} catch (FormulaDateException ex) {
FormulaEngine.getHooks().handleFormulaDateException(ex);
}
try {
value = parseDateTime(checkStringType(input));
} catch (FormulaDateException ex) {
FormulaEngine.getHooks().handleFormulaDateException(ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.DAYS, Date.class), args[0]), guards[0]);
String str = getSqlHooks(context).sqlCastNull(args[0], Date.class);
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.DAYS, Date.class), str), guards[0]);
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
if (context.useHighPrecisionJs()) {
Expand All @@ -54,7 +55,7 @@ public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[]
class FunctionDayCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;

public FunctionDayCommand(FormulaCommandInfo formulaCommandInfo) {
public FunctionDayCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.force.formula.commands;

import java.math.BigDecimal;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Deque;

import com.force.formula.*;
import com.force.formula.FormulaCommand;
import com.force.formula.FormulaCommandType.AllowedContext;
import com.force.formula.FormulaCommandType.SelectorSection;
import com.force.formula.impl.*;
import com.force.formula.FormulaContext;
import com.force.formula.FormulaException;
import com.force.formula.FormulaRuntimeContext;
import com.force.formula.impl.FormulaAST;
import com.force.formula.impl.JsValue;
import com.force.formula.impl.TableAliasRegistry;
import com.force.formula.sql.SQLPair;
import com.force.formula.util.FormulaI18nUtils;
import com.force.i18n.BaseLocalizer;
Expand All @@ -30,32 +37,33 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
return new SQLPair(String.format(getSqlHooks(context).sqlGetDayOfYear(), args[0]), guards[0]);
String str = getSqlHooks(context).sqlCastNull(args[0], Date.class);
return new SQLPair(String.format(getSqlHooks(context).sqlGetDayOfYear(), str), guards[0]);
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
String js = jsToDec(context, context.getJsEngMod() + ".dayofyear(" + args[0] + ")");
return JsValue.generate(js, args, true, args[0]);
}

static class FunctionDayOfYearCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;
public FunctionDayOfYearCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}
@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null)
stack.push(null);
else {
Calendar c = FormulaI18nUtils.getLocalizer().getCalendar(BaseLocalizer.GMT);
c.setTime(d);
stack.push(new BigDecimal(c.get(Calendar.DAY_OF_YEAR)));
}
}
private static final long serialVersionUID = 1L;

public FunctionDayOfYearCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null)
stack.push(null);
else {
Calendar c = FormulaI18nUtils.getLocalizer().getCalendar(BaseLocalizer.GMT);
c.setTime(d);
stack.push(new BigDecimal(c.get(Calendar.DAY_OF_YEAR)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.HOURS, FormulaTime.class), args[0]), guards[0]);
String str = getSqlHooks(context).sqlCastNull(args[0], FormulaTime.class);
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.HOURS, FormulaTime.class), str), guards[0]);
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
if (context.useHighPrecisionJs()) {
Expand All @@ -53,7 +54,7 @@ public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[]
class FunctionHourCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;

public FunctionHourCommand(FormulaCommandInfo formulaCommandInfo) {
public FunctionHourCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

Expand Down
41 changes: 21 additions & 20 deletions impl/src/main/java/com/force/formula/commands/FunctionIsoWeek.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
return new SQLPair(String.format(getSqlHooks(context).sqlGetIsoWeek(), args[0]), guards[0]);
String str = getSqlHooks(context).sqlCastNull(args[0], Date.class);
return new SQLPair(String.format(getSqlHooks(context).sqlGetIsoWeek(), str), guards[0]);
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
String js = jsToDec(context, context.getJsEngMod() + ".isoweek(" + args[0] + ")");
return JsValue.generate(js, args, true, args[0]);
}


@Override
public Type validate(FormulaAST node, FormulaContext context, FormulaProperties properties)
throws FormulaException {
Expand All @@ -67,22 +68,22 @@ public Type validate(FormulaAST node, FormulaContext context, FormulaProperties
return BigDecimal.class;
}


static class FunctionIsoWeekCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;
public FunctionIsoWeekCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}
@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null) {
stack.push(null);
} else {
stack.push(new BigDecimal(LocalDateTime.ofInstant(d.toInstant(), ZoneId.of("GMT")).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)));
}
}
private static final long serialVersionUID = 1L;

public FunctionIsoWeekCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null) {
stack.push(null);
} else {
stack.push(new BigDecimal(LocalDateTime.ofInstant(d.toInstant(), ZoneId.of("GMT")).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)));
}
}
}
}
43 changes: 22 additions & 21 deletions impl/src/main/java/com/force/formula/commands/FunctionIsoYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
// Pass in the date twice because sqlserver uses is
return new SQLPair(String.format(getSqlHooks(context).sqlGetIsoYear(), args[0], args[0]), guards[0]);
// Pass in the date twice because sqlserver uses is
String str = getSqlHooks(context).sqlCastNull(args[0], Date.class);
return new SQLPair(String.format(getSqlHooks(context).sqlGetIsoYear(), str, str), guards[0]);
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
String js = jsToDec(context, context.getJsEngMod() + ".isoyear(" + args[0] + ")");
return JsValue.generate(js, args, true, args[0]);
}


@Override
public Type validate(FormulaAST node, FormulaContext context, FormulaProperties properties)
throws FormulaException {
Expand All @@ -68,22 +69,22 @@ public Type validate(FormulaAST node, FormulaContext context, FormulaProperties
return BigDecimal.class;
}


static class FunctionIsoWeekNumberCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;
public FunctionIsoWeekNumberCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}
@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null)
stack.push(null);
else {
stack.push(new BigDecimal(LocalDateTime.ofInstant(d.toInstant(), ZoneId.of("GMT")).get(IsoFields.WEEK_BASED_YEAR)));
}
}
private static final long serialVersionUID = 1L;

public FunctionIsoWeekNumberCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

@Override
public void execute(FormulaRuntimeContext context, Deque<Object> stack) {
Date d = checkDateType(stack.pop());
if (d == null)
stack.push(null);
else {
stack.push(new BigDecimal(LocalDateTime.ofInstant(d.toInstant(), ZoneId.of("GMT")).get(IsoFields.WEEK_BASED_YEAR)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.MILLIS, FormulaTime.class), args[0]), guards[0]);
String str = getSqlHooks(context).sqlCastNull(args[0], FormulaTime.class);
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.MILLIS, FormulaTime.class), str), guards[0]);
}

@Override
Expand All @@ -50,7 +51,7 @@ public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[]
class FunctionMillisecondCommand extends AbstractFormulaCommand {
private static final long serialVersionUID = 1L;

public FunctionMillisecondCommand(FormulaCommandInfo formulaCommandInfo) {
public FunctionMillisecondCommand(FormulaCommandInfo formulaCommandInfo) {
super(formulaCommandInfo);
}

Expand Down
Loading

0 comments on commit 9cdab70

Please sign in to comment.