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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public class FieldMappingHandler implements PipelineStep {
"COUNT|SUM|AVG|MIN|MAX|DATE|YEAR|MONTH|DAY)\\b"
);

// A bare SQL identifier: customer_name, DATE
private static final String BARE_IDENTIFIER = "[A-Za-z_][A-Za-z0-9_$]*";

// A SQL identifier, bare or double-quoted: customer_name, "Customer Name"
private static final String SQL_IDENTIFIER = "(?:" + BARE_IDENTIFIER + "|\"[^\"]+\")";

// A column reference, optionally qualified: column, table.column, schema.table.column
private static final Pattern IDENTIFIER_PATH_PATTERN =
Pattern.compile(SQL_IDENTIFIER + "(?:\\." + SQL_IDENTIFIER + ")*");

// The same reference in the Tableau dialect, where double quotes delimit a string literal
// rather than an identifier, so "Not Available" is a constant and not a column.
private static final Pattern BARE_IDENTIFIER_PATH_PATTERN =
Pattern.compile(BARE_IDENTIFIER + "(?:\\." + BARE_IDENTIFIER + ")*");

private final ConversionDirection direction;
private final CustomExtensionHandler customExtensionHandler;

Expand Down Expand Up @@ -299,12 +314,12 @@ private void processFieldsForDataset(
String dialect = expressionInfo.dialect();

// Skip calculated fields for non-Tableau dialects till we agree on a common dialect.
if (!DIALECT_TABLEAU.equals(dialect) && isCalculatedExpression(expression)) {
if (!DIALECT_TABLEAU.equals(dialect) && isCalculatedExpression(expression, dialect)) {
continue;
}

// Check if this is a calculated field (Tableau dialect with calculated expression)
boolean isCalculated = DIALECT_TABLEAU.equals(dialect) && isCalculatedExpression(expression);
boolean isCalculated = DIALECT_TABLEAU.equals(dialect) && isCalculatedExpression(expression, dialect);

if (isCalculated) {
// Create a semantic calculated dimension
Expand Down Expand Up @@ -504,13 +519,26 @@ private ExpressionInfo unwrapExpression(Map<String, Object> ossieField) {
* </ul>
*
* @param expression The SQL expression to evaluate
* @param dialect The dialect the expression is written in
* @return true if calculated, false if direct reference
*/
private boolean isCalculatedExpression(String expression) {
private boolean isCalculatedExpression(String expression, String dialect) {
if (expression == null || expression.isEmpty()) {
return false;
}

// An identifier path is always a direct reference, even when a segment spells a SQL
// keyword. Warehouses routinely expose columns named DATE, MONTH or COUNT. Double
// quotes delimit an identifier in SQL but a string literal in Tableau, so a quoted
// segment reads as a column everywhere except the Tableau dialect, where "Not
// Available" stays a calculation.
Pattern directReference = DIALECT_TABLEAU.equals(dialect)
? BARE_IDENTIFIER_PATH_PATTERN
: IDENTIFIER_PATH_PATTERN;
if (directReference.matcher(expression.trim()).matches()) {
return false;
}

String normalized = expression.trim().toUpperCase();

// Check for function calls (presence of parentheses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ void declaredButOmittedCalculatedSqlFieldCannotSatisfyAMetricReference() throws
assertTrue(error.getMessage().contains("not exported"), error.getMessage());
}

@Test
void metricCanReferenceAFieldWhoseColumnNameIsASqlKeyword() throws Exception {
Map<String, Object> source = model("sales", List.of(metric("total", "ANSI_SQL", "SUM(orders.profit)")));
Map<String, Object> keywordColumn = field("profit", "Decimal");
keywordColumn.put("expression", Map.of("dialects", List.of(dialect("ANSI_SQL", "COUNT"))));
items(source, "datasets").get(0).put("fields", List.of(keywordColumn));

Map<String, Object> output = convertOne(source);

assertEquals(List.of("COUNT"), items(items(output, "semanticDataObjects").get(0),
"semanticMeasurements").stream().map(item -> item.get("dataObjectFieldName")).toList());
assertEquals("SUM([orders].[profit])",
items(output, "semanticCalculatedMeasurements").get(0).get("expression"));
}

@Test
void duplicateMetricNamesAreRejectedBeforeExport() throws Exception {
String input = document(List.of(model("sales", List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,81 @@ void testCustomExtensionsRestoration() throws Exception {
"Exact Salesforce extension type should win over portable Decimal");
}

@Test
void testFieldWhoseColumnNameIsASqlKeywordIsNotDroppedAsCalculated() throws Exception {
// A warehouse column may legitimately be named DATE, MONTH or COUNT. Such a field is a
// direct reference, not a calculation, and must still reach the data object.
String yamlWithKeywordColumn = ossieYaml.replace("\r\n", "\n")
.replace(" expression: product_name__c\n", " expression: DATE\n");
assertFalse(yamlWithKeywordColumn.contains("expression: product_name__c"),
"fixture text substitution did not match");

List<String> results = converter.convert(yamlWithKeywordColumn);
Map<String, Object> sfModel = jsonMapper.readValue(results.get(0), new TypeReference<Map<String, Object>>() {});

List<Map<String, Object>> dataObjects = (List<Map<String, Object>>) sfModel.get("semanticDataObjects");
Map<String, Object> productsDataset = dataObjects.stream()
.filter(d -> "Products".equals(d.get("apiName")))
.findFirst()
.orElse(null);
assertNotNull(productsDataset);

List<Map<String, Object>> productDimensions =
(List<Map<String, Object>>) productsDataset.get("semanticDimensions");
Map<String, Object> productName = productDimensions.stream()
.filter(d -> "product_name".equals(d.get("apiName")))
.findFirst()
.orElse(null);
assertNotNull(productName, "a dimension whose column is named DATE must not be dropped");
assertEquals("DATE", productName.get("dataObjectFieldName"),
"the keyword-named column should be carried through as a direct reference");

List<Map<String, Object>> calculatedDimensions =
(List<Map<String, Object>>) sfModel.get("semanticCalculatedDimensions");
if (calculatedDimensions != null) {
assertTrue(calculatedDimensions.stream()
.noneMatch(d -> "product_name".equals(d.get("apiName"))),
"a keyword-named column must not be routed to calculated dimensions");
}
}

@Test
void testTableauQuotedLiteralIsStillCalculated() throws Exception {
// Double quotes delimit an identifier in SQL but a string literal in Tableau, so a
// Tableau expression such as "Not Available" is a constant, not a column reference,
// and must still be routed to the calculated dimensions.
String yamlWithTableauLiteral = ossieYaml.replace("\r\n", "\n")
.replace(" - dialect: ANSI_SQL\n"
+ " expression: product_name__c\n",
" - dialect: TABLEAU\n"
+ " expression: '\"Not Available\"'\n");
assertFalse(yamlWithTableauLiteral.contains("expression: product_name__c"),
"fixture text substitution did not match");

List<String> results = converter.convert(yamlWithTableauLiteral);
Map<String, Object> sfModel = jsonMapper.readValue(results.get(0), new TypeReference<Map<String, Object>>() {});

List<Map<String, Object>> calculatedDimensions =
(List<Map<String, Object>>) sfModel.get("semanticCalculatedDimensions");
assertNotNull(calculatedDimensions);
Map<String, Object> productName = calculatedDimensions.stream()
.filter(d -> "product_name".equals(d.get("apiName")))
.findFirst()
.orElse(null);
assertNotNull(productName, "a Tableau quoted literal must be exported as a calculated dimension");
assertEquals("\"Not Available\"", productName.get("expression"));

List<Map<String, Object>> dataObjects = (List<Map<String, Object>>) sfModel.get("semanticDataObjects");
Map<String, Object> productsDataset = dataObjects.stream()
.filter(d -> "Products".equals(d.get("apiName")))
.findFirst()
.orElse(null);
assertNotNull(productsDataset);
assertTrue(((List<Map<String, Object>>) productsDataset.get("semanticDimensions")).stream()
.noneMatch(d -> "product_name".equals(d.get("apiName"))),
"a Tableau quoted literal must not be exported as a direct column reference");
}

@Test
void testFieldLabelDefaultsToApiNameWhenOssieHasNoLabel() throws Exception {
// Normalize line endings first: the fixture file may check out with CRLF depending on
Expand Down
Loading