diff --git a/converters/salesforce/src/main/java/org/apache/ossie/converter/FieldMappingHandler.java b/converters/salesforce/src/main/java/org/apache/ossie/converter/FieldMappingHandler.java index 7a00b0c7b..9348d70fb 100644 --- a/converters/salesforce/src/main/java/org/apache/ossie/converter/FieldMappingHandler.java +++ b/converters/salesforce/src/main/java/org/apache/ossie/converter/FieldMappingHandler.java @@ -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; @@ -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 @@ -504,13 +519,26 @@ private ExpressionInfo unwrapExpression(Map ossieField) { * * * @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) diff --git a/converters/salesforce/src/test/java/org/apache/ossie/MetricExportIntegrationTest.java b/converters/salesforce/src/test/java/org/apache/ossie/MetricExportIntegrationTest.java index 7a317ecb4..4c496d998 100644 --- a/converters/salesforce/src/test/java/org/apache/ossie/MetricExportIntegrationTest.java +++ b/converters/salesforce/src/test/java/org/apache/ossie/MetricExportIntegrationTest.java @@ -252,6 +252,21 @@ void declaredButOmittedCalculatedSqlFieldCannotSatisfyAMetricReference() throws assertTrue(error.getMessage().contains("not exported"), error.getMessage()); } + @Test + void metricCanReferenceAFieldWhoseColumnNameIsASqlKeyword() throws Exception { + Map source = model("sales", List.of(metric("total", "ANSI_SQL", "SUM(orders.profit)"))); + Map 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 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( diff --git a/converters/salesforce/src/test/java/org/apache/ossie/OssieToSalesforceConverterTest.java b/converters/salesforce/src/test/java/org/apache/ossie/OssieToSalesforceConverterTest.java index 6eba9e859..fa1c96936 100644 --- a/converters/salesforce/src/test/java/org/apache/ossie/OssieToSalesforceConverterTest.java +++ b/converters/salesforce/src/test/java/org/apache/ossie/OssieToSalesforceConverterTest.java @@ -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 results = converter.convert(yamlWithKeywordColumn); + Map sfModel = jsonMapper.readValue(results.get(0), new TypeReference>() {}); + + List> dataObjects = (List>) sfModel.get("semanticDataObjects"); + Map productsDataset = dataObjects.stream() + .filter(d -> "Products".equals(d.get("apiName"))) + .findFirst() + .orElse(null); + assertNotNull(productsDataset); + + List> productDimensions = + (List>) productsDataset.get("semanticDimensions"); + Map 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> calculatedDimensions = + (List>) 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 results = converter.convert(yamlWithTableauLiteral); + Map sfModel = jsonMapper.readValue(results.get(0), new TypeReference>() {}); + + List> calculatedDimensions = + (List>) sfModel.get("semanticCalculatedDimensions"); + assertNotNull(calculatedDimensions); + Map 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> dataObjects = (List>) sfModel.get("semanticDataObjects"); + Map productsDataset = dataObjects.stream() + .filter(d -> "Products".equals(d.get("apiName"))) + .findFirst() + .orElse(null); + assertNotNull(productsDataset); + assertTrue(((List>) 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