Skip to content
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
10 changes: 10 additions & 0 deletions converters/salesforce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,21 @@ and [aggregation rules](https://developer.salesforce.com/docs/data/semantic-laye
| `+`, `-`, `*`, `/`, parentheses, numeric constants | Explicitly grouped arithmetic |
| Searched `CASE WHEN` | `IF … THEN … ELSEIF … ELSE … END` |
| Comparisons, `AND`, `OR`, `NOT` | Equivalent grouped operators |
| Field `IN (...)`, `NOT IN (...)` with scalar literals | Grouped equality comparisons joined by `OR`, optionally wrapped in `NOT` |
| `COALESCE(a, b, …)` | Nested `IFNULL` |
| `NULLIF(a, b)` | `IF a = b THEN NULL ELSE a END` |
| `IS NULL`, `IS NOT NULL` | `ISNULL`, `NOT ISNULL` |
| `ABS`, `ROUND`, `CEIL`, `FLOOR` | `ABS`, `ROUND`, `CEILING`, `FLOOR` |

Membership lists must be nonempty and contain only scalar literals (including NULL
and signed numbers), with a direct field on the left. Types must be compatible;
subqueries, tuples and computed operands are unsupported. NULL membership follows
SQL three-valued logic, including for `NOT IN`.
When combining membership with `AND` or `OR`, parenthesize each membership
predicate, for example `(orders.status IN ('paid', 'pending')) AND orders.active`.
The current SQL parser can misgroup unparenthesized membership before a Boolean
operator; those forms are rejected rather than translated.

These constructs compose. For example, with declared numeric fields `profit` and
`revenue` in dataset `orders`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Locale;
import java.util.Set;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
Expand Down Expand Up @@ -152,6 +153,7 @@ private Node adaptNode(Expression expression) {
Node result = new Unary("ISNULL", adapt(test.getLeftExpression()));
return test.isNot() ? new Unary("NOT", result) : result;
}
if (expression instanceof InExpression membership) return membership(membership);
if (expression instanceof BinaryExpression binary) {
if (binary instanceof net.sf.jsqlparser.expression.operators.relational.SupportsOldOracleJoinSyntax oracle
&& (oracle.getOldOracleJoinSyntax() != 0 || oracle.getOraclePriorPosition() != 0)) {
Expand Down Expand Up @@ -183,6 +185,39 @@ private Node adaptNode(Expression expression) {
throw unsupported(expression);
}

private Node membership(InExpression expression) {
if (expression.isGlobal() || expression.getOldOracleJoinSyntax() != 0
|| expression.getOraclePriorPosition() != 0
|| !(expression.getRightExpression() instanceof ParenthesedExpressionList<?> values)
|| values.isEmpty()) {
throw unsupported(expression);
}
Node field = adapt(expression.getLeftExpression());
if (!(field instanceof Field)) {
throw new IllegalArgumentException("IN requires a direct field on the left");
}
List<Node> comparisons = new ArrayList<>();
for (Expression value : values) {
Node literal = adapt(value);
if (!(literal instanceof Literal)
&& !(literal instanceof Unary unary && Set.of("+", "-").contains(unary.operator())
&& unary.operand() instanceof Literal number && number.value() instanceof Number)) {
throw new IllegalArgumentException("IN requires scalar literals in its list");
}
comparisons.add(new Binary("=", field, literal));
}
// Keep NULL comparisons: a non-match with a NULL list item is UNKNOWN, even for NOT IN.
Node result = disjunction(comparisons, 0, comparisons.size());
return expression.isNot() ? new Unary("NOT", result) : result;
}

private static Node disjunction(List<Node> nodes, int start, int end) {
if (end - start == 1) return nodes.get(start);
// Balance the tree so list length does not consume the expression nesting budget.
int middle = start + (end - start) / 2;
return new Binary("OR", disjunction(nodes, start, middle), disjunction(nodes, middle, end));
}

private Node function(Function function) {
String name = function.getName();
if (name == null || !name.matches("[A-Za-z_][A-Za-z_0-9]*")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

/**
Expand Down Expand Up @@ -129,6 +130,26 @@ void nullablePredicatesUseThreeValuedLogic(String dialect) {
"SUM(CASE WHEN orders.flag IS NULL THEN "
+ "CASE WHEN orders.amount IS NULL THEN 3 ELSE 2 END ELSE 0 END)",
ORDERS, 5.0);
assertValue(dialect, "SUM(CASE WHEN orders.flag IN (TRUE, FALSE) THEN 1 ELSE 0 END)",
ORDERS, 2.0);
assertValue(dialect, "SUM(CASE WHEN NOT (orders.amount IN (10)) OR orders.flag "
+ "AND (orders.amount NOT IN (-4)) THEN 1 ELSE 0 END)", ORDERS, 2.0);
}

@ParameterizedTest
@CsvSource(delimiter = '|', nullValues = "null", value = {
"10 | IN | 10, 10.0, -4 | 1", "-4 | NOT IN | -4, +10 | 0",
"10 | IN | 10 | 1", "-4 | IN | 10 | 0",
"-4 | NOT IN | 10 | 1", "null | IN | 10 | -1",
"null | NOT IN | 10 | -1", "10 | IN | NULL, 10 | 1",
"10 | NOT IN | NULL, 10 | 0", "-4 | IN | 10, NULL | -1",
"-4 | NOT IN | 10, NULL | -1", "10 | IN | NULL | -1"})
void membershipPreservesTrueFalseAndUnknown(Double amount, String operator, String values, double expected) {
String predicate = "orders.amount " + operator + " (" + values + ")";
for (String dialect : List.of("SNOWFLAKE", "ANSI_SQL", "OSSIE_SQL_2026")) {
assertValue(dialect, "SUM(CASE WHEN " + predicate + " THEN 1 WHEN NOT (" + predicate
+ ") THEN 0 ELSE -1 END)", List.of(row(amount, null, null)), expected);
}
}

@ParameterizedTest
Expand Down Expand Up @@ -165,6 +186,12 @@ void textPredicatesPreserveDuplicatesNullsAndEscapedApostrophes(String dialect)
assertValue(dialect,
"SUM(CASE WHEN orders.status = 'O''Brien' THEN orders.amount ELSE 0 END)",
orders, 2.0);
assertValue(dialect,
"SUM(CASE WHEN orders.status IN ('paid', 'pending', 'O''Brien', 'other') THEN orders.amount ELSE 0 END)",
orders, 23.0);
assertValue(dialect,
"SUM(CASE WHEN orders.status NOT IN ('paid', 'O''Brien') THEN orders.amount ELSE 0 END)",
orders, 4.0);
assertValue(dialect, "COUNT(DISTINCT orders.status)", List.of(), 0.0);
assertValue(dialect, "COUNT(DISTINCT orders.status)", List.of(row(null, null, null, null)), 0.0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ static Stream<Arguments> supported() {
"(SUM([orders].[profit]) / (IF (SUM([orders].[revenue]) = 0) THEN NULL ELSE SUM([orders].[revenue]) END))"),
Arguments.of("SUM(CASE WHEN orders.status = 'paid' THEN orders.amount ELSE 0 END)",
"SUM((IF ([orders].[status] = 'paid') THEN [orders].[amount] ELSE 0 END))"),
Arguments.of("SUM(CASE WHEN orders.status IN ('paid', 'pending') THEN orders.amount ELSE 0 END)",
"SUM((IF (([orders].[status] = 'paid') OR ([orders].[status] = 'pending')) THEN [orders].[amount] ELSE 0 END))"),
Arguments.of("SUM(CASE WHEN orders.amount NOT IN (-1, +2) THEN 1 ELSE 0 END)",
"SUM((IF (NOT (([orders].[amount] = (-1)) OR ([orders].[amount] = 2))) THEN 1 ELSE 0 END))"),
Arguments.of("COALESCE(SUM(orders.amount), AVG(orders.revenue), 0)",
"IFNULL(SUM([orders].[amount]), IFNULL(AVG([orders].[revenue]), 0))"),
Arguments.of("SUM(CASE WHEN orders.amount IS NOT NULL THEN orders.amount END)",
Expand Down Expand Up @@ -363,12 +367,35 @@ void temporalAggregatesCanBeUsedInNumericPredicates() {
"SUM(orders.amount).attribute", "private_schema.SUM(orders.amount)", "\"SUM\"(orders.amount)",
"SUM(CASE orders.amount WHEN 1 THEN 2 ELSE 0 END)", "N'prefixed'",
"orders.status ISNULL", "orders.status NOTNULL", "PRIOR orders.amount = orders.quantity",
"!orders.active", "(SELECT amount FROM orders)", "orders.amount IN (1, 2)",
"!orders.active", "(SELECT amount FROM orders)",
"SUM(orders.amount) AS alias", "orders.amount(+) = orders.quantity"})
void parserAcceptanceNeverDiscardsUnsupportedSqlModifiers(String expression) {
assertThrows(ConversionException.class, () -> translate("SNOWFLAKE", expression), expression);
}

@ParameterizedTest
@ValueSource(strings = {"orders.amount IN ()", "orders.amount IN (SELECT amount FROM orders)",
"(orders.amount, orders.quantity) IN ((1, 2))", "orders.amount IN ((1, 2))",
"orders.amount + 1 IN (1, 2)", "orders.amount IN (orders.quantity)",
"orders.amount IN (1 + 2)", "orders.amount IN (+NULL)", "orders.active IN (NOT TRUE)",
"orders.amount GLOBAL IN (1)", "orders.amount(+) IN (1)", "PRIOR orders.amount IN (1)",
"orders.amount IN ('1')", "orders.missing IN (1)",
"orders.amount IN (1) OR orders.active", "NOT orders.amount IN (1) AND orders.active"})
void rejectsUnsupportedMembershipOperandsAndModifiers(String predicate) {
for (String dialect : SQL_DIALECTS) {
assertThrows(ConversionException.class, () -> translate(dialect,
"SUM(CASE WHEN " + predicate + " THEN 1 ELSE 0 END)"), predicate);
}
}

@Test
void membershipListsDoNotIntroduceLinearNesting() {
String values = String.join(", ", java.util.Collections.nCopies(512, "1"));
String result = translate("SNOWFLAKE",
"SUM(CASE WHEN orders.amount IN (" + values + ") THEN 1 ELSE 0 END)");
assertEquals(result, translate("TABLEAU", result));
}

@ParameterizedTest
@ValueSource(strings = {"CEIL(AVG(orders.amount))", "FLOOR(AVG(orders.amount))",
"ROUND(AVG(orders.amount))", "ROUND(AVG(orders.amount), 0)", "ROUND(AVG(orders.amount), -2)"})
Expand Down
Loading