Skip to content

Commit

Permalink
Sqlite 3 Support
Browse files Browse the repository at this point in the history
Sqlite3 is the default database in iOS, and generating custom formulas offline in javascript is
  great, but being able to query the offline cache directly is even better.

General Changes
- DatePart extraction moved to hooks to make it easier to reason with
- Log & NaturalLog moved to hooks as well, since Sqlite doesn't have a standard implementation
  • Loading branch information
steventamm committed Oct 22, 2022
1 parent 9cdef1b commit 2fb8c0f
Show file tree
Hide file tree
Showing 37 changed files with 1,116 additions and 139 deletions.
8 changes: 7 additions & 1 deletion api/src/main/java/com/force/formula/sql/FormulaSqlStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ default boolean isPrestoStyle() {
default boolean isGoogleStyle() {
return false;
}


/**
* @return whether or not to default to Sqlite style.
*/
default boolean isSqliteStyle() {
return false;
}

/**
* @return the function name for taking a substring.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<testCase name="testMillisecWithValidDateTimeString">
<testInstance formula="MilliSecond(TIMEVALUE(&quot;2015-03-17 17:00:00&quot;))" returntype="Double" precision="12" scale="2">
<SqlOutput nullAsNull="true">
<Sql>TRUNC((NULL -TRUNC(NULL/1000) * 1000))</Sql>
<Sql>TRUNC(NULL -TRUNC(NULL/1000) * 1000)</Sql>
<Guard>0=0</Guard>
</SqlOutput>
<SqlOutput nullAsNull="false">
<Sql>TRUNC((NULL -TRUNC(NULL/1000) * 1000))</Sql>
<Sql>TRUNC(NULL -TRUNC(NULL/1000) * 1000)</Sql>
<Guard>0=0</Guard>
</SqlOutput>
<result>
<inputvalues>No data</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: INVALID_ARGUMENT: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Operands of / cannot be literal NULL [at 1:62] ...WHEN 0=0 THEN NULL ELSE ROUND(TRUNC((NULL -TRUNC(NULL/1000) * 1000)), 32) ... ^ - Statement: 'SELECT CASE WHEN 0=0 THEN NULL ELSE ROUND(TRUNC((NULL -TRUNC(NULL/1000) * 1000)), 32) END FROM (SELECT 1 as fake) c'</sql>
<sql>Error: INVALID_ARGUMENT: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Operands of / cannot be literal NULL [at 1:61] ...WHEN 0=0 THEN NULL ELSE ROUND(TRUNC(NULL -TRUNC(NULL/1000) * 1000), 32) EN... ^ - Statement: 'SELECT CASE WHEN 0=0 THEN NULL ELSE ROUND(TRUNC(NULL -TRUNC(NULL/1000) * 1000), 32) END FROM (SELECT 1 as fake) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: INVALID_ARGUMENT: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Operands of / cannot be literal NULL [at 1:62] ...WHEN 0=0 THEN NULL ELSE ROUND(TRUNC((NULL -TRUNC(NULL/1000) * 1000)), 32) ... ^ - Statement: 'SELECT CASE WHEN 0=0 THEN NULL ELSE ROUND(TRUNC((NULL -TRUNC(NULL/1000) * 1000)), 32) END FROM (SELECT 1 as fake) c'</sqlNullAsNull>
<sqlNullAsNull>Error: INVALID_ARGUMENT: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Operands of / cannot be literal NULL [at 1:61] ...WHEN 0=0 THEN NULL ELSE ROUND(TRUNC(NULL -TRUNC(NULL/1000) * 1000), 32) EN... ^ - Statement: 'SELECT CASE WHEN 0=0 THEN NULL ELSE ROUND(TRUNC(NULL -TRUNC(NULL/1000) * 1000), 32) END FROM (SELECT 1 as fake) c'</sqlNullAsNull>
</result>
</testInstance>
</testCase>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<testCase name="testMillisecondValueWithValidInValid">
<testInstance formula="MilliSecond(TimeValue(DATETIMEVALUE(dateString__c)))" returntype="Double" precision="12" scale="2">
<SqlOutput nullAsNull="true">
<Sql>TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000)/1000) * 1000))</Sql>
<Sql>TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000)/1000) * 1000)</Sql>
<Guard>1=0</Guard>
</SqlOutput>
<SqlOutput nullAsNull="false">
<Sql>TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000)/1000) * 1000))</Sql>
<Sql>TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM($!s0s!$.dateString__c), 'UTC')),86400000)/1000) * 1000)</Sql>
<Guard>1=0</Guard>
</SqlOutput>
<result>
Expand Down Expand Up @@ -61,30 +61,30 @@
<result>
<inputvalues>[2011-13-29 00:00:09]</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2011-13-29 00:00:09&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2011-13-29 00:00:09&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2011-13-29 00:00:09&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2011-13-29 00:00:09&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
</result>
<result>
<inputvalues>[2012:02:02]</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Mismatch between format character '-' and string character ':' - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Mismatch between format character '-' and string character ':' - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Mismatch between format character '-' and string character ':' - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Mismatch between format character '-' and string character ':' - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
</result>
<result>
<inputvalues>[2012-01-01 24:00:00]</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 24:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 24:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 24:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 24:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
</result>
<result>
<inputvalues>[2012-01-01 23:60:00]</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 23:60:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 23:60:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 23:60:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-01-01 23:60:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
</result>
<result>
<inputvalues>[2012-01-01 23:00:60]</inputvalues>
Expand All @@ -103,9 +103,9 @@
<result>
<inputvalues>[2012-10-34 00:00:00]</inputvalues>
<formula>Error: com.force.formula.FormulaDateException</formula>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-10-34 00:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<sql>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-10-34 00:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sql>
<formulaNullAsNull>Error: com.force.formula.FormulaDateException</formulaNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-10-34 00:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC((MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000)), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
<sqlNullAsNull>Error: OUT_OF_RANGE: io.grpc.StatusRuntimeException: OUT_OF_RANGE: Failed to parse input string &quot;2012-10-34 00:00:00&quot; - Statement: 'SELECT CASE WHEN 1=0 THEN NULL ELSE ROUND(TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000) -TRUNC(MOD(UNIX_MILLIS(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', TRIM(c.dateString__c), 'UTC')),86400000)/1000) * 1000), 32) END FROM (SELECT 1 as fake, @p1 as dateString__c) c'</sqlNullAsNull>
</result>
<result>
<inputvalues>[2012-02-07 5:2:33]</inputvalues>
Expand Down
23 changes: 12 additions & 11 deletions impl/src/main/java/com/force/formula/commands/FunctionDay.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.force.formula.commands;

import java.math.BigDecimal;
import java.util.*;
import java.time.temporal.ChronoUnit;
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,14 +38,7 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards, TableAliasRegistry registry) {
if (context.getSqlStyle().isTransactSqlStyle()) {
return new SQLPair("DAY(" + args[0] + ")", guards[0]);
}
String sql = "EXTRACT(DAY FROM " + args[0] + ")";
if (context.getSqlStyle().isPostgresStyle()) {
sql = sql + "::numeric";
}
return new SQLPair(sql, guards[0]);
return new SQLPair(String.format(getSqlHooks(context).sqlChronoUnit(ChronoUnit.DAYS, Date.class), args[0]), guards[0]);
}

@Override
Expand Down
16 changes: 2 additions & 14 deletions impl/src/main/java/com/force/formula/commands/FunctionHour.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.force.formula.commands;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Deque;

Expand All @@ -14,9 +15,7 @@
import com.force.formula.impl.FormulaAST;
import com.force.formula.impl.JsValue;
import com.force.formula.impl.TableAliasRegistry;
import com.force.formula.sql.FormulaSqlStyle;
import com.force.formula.sql.SQLPair;
import com.force.formula.util.FormulaDateUtil;
import com.force.formula.util.FormulaI18nUtils;
import com.force.i18n.BaseLocalizer;

Expand All @@ -39,20 +38,9 @@ public FormulaCommand getCommand(FormulaAST node, FormulaContext context) {

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

public static String getHourExpr(String arg, FormulaContext context) {
FormulaSqlStyle style = context.getSqlStyle();
if (style.isMysqlStyle() || style.isPrestoStyle()) {
return "HOUR(" + arg + ")";
} else if (style.isTransactSqlStyle()) {
return "DATEPART(hour,"+arg+")";
}
return "TRUNC(" + arg + "/" + FormulaDateUtil.HOUR_IN_MILLIS + ")";
}

@Override
public JsValue getJavascript(FormulaAST node, FormulaContext context, JsValue[] args) throws FormulaException {
if (context.useHighPrecisionJs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ protected BigDecimal execute(BigDecimal arg) {

@Override
public SQLPair getSQL(FormulaAST node, FormulaContext context, String[] args, String[] guards) {
String sql;
FormulaSqlHooks hooks = (FormulaSqlHooks) context.getSqlStyle();
if (hooks.isTransactSqlStyle() || hooks.isGoogleStyle()) {
sql = String.format(hooks.sqlToNumber(),"LOG10(" + args[0] + ")");
} else if (hooks.isPrestoStyle()) {
sql = String.format(hooks.sqlToNumber(),"CAST(LOG10(" + args[0] + ") AS DECIMAL(38,18))");
} else {
sql = "LOG(10, " + args[0] + ")";
}
String sql = hooks.sqlLogBase10(args[0]);
String guard = SQLPair.generateGuard(guards, args[0] + "<=0");
return new SQLPair(sql, guard);
}
Expand Down
Loading

0 comments on commit 2fb8c0f

Please sign in to comment.