Skip to content

MLE-31969: Support special characters in column names when aggregating structured data - #711

Open
prgs-rsingh wants to merge 2 commits into
developfrom
feature/mle-31969-column-references
Open

prgs-rsingh wants to merge 2 commits into
developfrom
feature/mle-31969-column-references

Conversation

@prgs-rsingh

@prgs-rsingh prgs-rsingh commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

--group-by, --aggregate, and --aggregate-order-by failed when a column name contained a period, a space, parentheses, %, or a similar character. This affected import-avro-files, import-delimited-files, import-jdbc, import-orc-files, and import-parquet-files, which all share StructuredDataParams.

  • A period in any column failed every aggregation with UNRESOLVED_COLUMN, even when that column was not referenced by an option. Flux calls first() on every non-aggregated column, and Spark read the name as a path to a nested field.
  • A space or other special character in --aggregate-order-by failed with PARSE_SYNTAX_ERROR, because the name was inserted unquoted into an array_sort SQL expression.
  • --column-name-strategy simple (MLE-12345: PDPS-2211 Add --column-name-strategy for structured-data imports #678, not yet released) did not help, because it ran after the steps that failed.

Changes

Commit 1: column references and sanitization order

Quoting column references:

  • A reference that matches a top-level column is quoted with backticks before it is passed to Spark, so the name is treated literally.
  • A reference is matched regardless of case unless spark.sql.caseSensitive is enabled, which is how Spark itself resolves column names.
  • In a path to a field in a struct column (e.g. --group-by address.city for Avro, ORC, or Parquet), only the name of the column is quoted and the rest is left for Spark to parse. The column may itself be wrapped in backticks, e.g. `Home Address`.city. Any other reference is passed to Spark unchanged.
  • A name that the user has already wrapped in backticks is recognized as that column.
  • The logic lives in a new package-private ColumnReferences class that has no dependency on the Spark Dataset API.

Applying --column-name-strategy first:

  • Column names are now sanitized before --where, --group-by, --aggregate, --aggregate-order-by, and --drop, rather than after them.
  • Every option therefore sees the sanitized names, and the fields within an aggregation are sanitized too. Previously they kept the original names in the documents that were written.
  • --group-by, --aggregate, --aggregate-order-by, and --drop still accept the original name of a column, including in a path to a field in a struct column.
  • No new option or API method is added. The story proposed --normalize-column-names; this uses --column-name-strategy simple from MLE-12345: PDPS-2211 Add --column-name-strategy for structured-data imports #678 instead, so that there is one way to rename columns.

Error messages:

  • A problem with the --group-by column is now reported as Unable to group by '<name>'; cause: .... Because the column is now passed to Spark as a Column, Spark no longer checks it until the aggregation runs, so without this a typo in --group-by was reported as a failure to aggregate columns that were valid.
  • That error is a FluxException. Previously, a problem with the --group-by column surfaced as Spark's own AnalysisException.
  • The existing Unable to aggregate columns: ... error now ends with the cause reported by Spark, without the query plan.

Docs are updated: aggregating-rows.md, filtering-data.md, sanitizing-column-names.md, and the javadoc for StructuredDataImporter#columnNameStrategy.

Commit 2: sort direction of single-column aggregations

  • --aggregate-order-by on an aggregation of a single column sorted in the opposite direction to the one requested, because the asc flag passed to Spark's sort_array was negated. This has been the behavior since 2.1.0 and was found while testing this story.
  • It is a separate commit so that it can be moved to its own ticket if preferred.

Behavior changes for existing jobs

These affect jobs that use the default strategy and should be covered by a release note.

  • Column referenced in a different letter case. --group-by orderid against a source column OrderId used to write the column twice, as orderid and OrderId. It is now written once, as OrderId. A --uri-template that used the name as typed, such as /x/{orderid}.json, must be updated to use the name in the data source, because the connector looks up template values in a case-sensitive manner. The same applies to --aggregate: fields in the aggregation are named after the source columns, and those columns are no longer also kept at the top level.
  • Column wrapped in backticks. In --group-by and --aggregate this used to write the column twice; it is now written once. In --drop it used to be ignored; the column is now dropped.
  • Single-column aggregation order. --aggregate-order-by now sorts in the requested direction (commit 2).
  • Error messages. The text of the errors for an unknown --group-by column and for a failed aggregation has changed, as described above.
  • Exception type for --group-by errors. A problem with the --group-by column, such as an unknown column or an invalid path into a struct column, used to be thrown as Spark's org.apache.spark.sql.AnalysisException. It is now thrown as a FluxException whose cause is that Spark exception. This only matters to users of the Java API who catch a specific exception type; the CLI reports the error either way.

Behavior changes relative to #678

These only affect --column-name-strategy simple, which has not been released.

  • Sanitization is applied first instead of last.
  • The name of an aggregation defined via --aggregate is used as given and is no longer sanitized.
  • Fields within an aggregation now have sanitized names.
  • A collision or empty-name error is raised before --drop is applied, so dropping the offending column no longer avoids the error. collidingNamesCannotBeAvoidedByDroppingColumn captures this.
  • Two columns whose sanitized names differ only by case, such as Item Name and item_name, now fail with --group-by, because Spark cannot tell Item_Name and item_name apart. This is documented as a limitation and captured by sanitizedNamesDifferingOnlyByCase. The duplicate check in SimpleColumnNameSanitizer is left as it is.
  • Using --group-by on a field in a struct column produces a column named after that field. Field names within a struct column are not sanitized, so a field named Zip Code now produces a column named Zip Code, where it previously became Zip_Code because sanitization ran last. This is documented as a limitation.

The simple rule itself is unchanged.

Testing

  • ColumnReferencesTest: unit tests for quoting and resolving references, including case handling and struct paths.
  • StructuredDataParamsTest: runs the transformations against a local Spark session, with no MarkLogic required. It reads the test file with inferred types, as import-delimited-files does. Covers group-by, aggregation, ordering, --where, --drop, and the error messages for both strategies, plus struct columns.
  • ImportDelimitedFilesTest and DelimitedFilesImporterTest: three end-to-end tests via the CLI and the API.

The first two classes and SimpleColumnNameSanitizerTest were run locally against Spark 4.2.0.

CI notes:

Questions for reviewers

  1. Is it acceptable to reverse the ordering that MLE-12345: PDPS-2211 Add --column-name-strategy for structured-data imports #678 documented, given that the original column names are still accepted?
  2. Is it acceptable that --drop can no longer be used to avoid a collision or empty-name error? For CSV, Avro, ORC, and Parquet sources the only workaround is to change the source file.
  3. Should the duplicate check in SimpleColumnNameSanitizer ignore case, so that names differing only by case are reported as a collision rather than failing in --group-by? That would also reject the no-grouping case, which works today.
  4. Should the sort direction fix stay in this PR or move to its own ticket?

Out of scope

  • The simple rule is not changed for TDE validity (MLE-27356): a name that starts with a digit or is longer than 128 characters is still not a valid TDE column name.
  • An aggregation of a single column converts its values to strings, because the column is passed through concat(). A numeric column is therefore written as an array of strings and sorted as text, e.g. ["10","100","9"]. This predates this PR and is not changed here.
  • --aggregate-order-by cannot sort by a field within a struct column. This predates this PR.

@prgs-rsingh
prgs-rsingh force-pushed the feature/mle-31969-column-references branch from 21b6067 to 9b5a269 Compare November 18, 2026 21:30
@NiteshMehta
NiteshMehta marked this pull request as ready for review November 20, 2026 11:57
Copilot AI balanced review requested due to automatic review settings November 20, 2026 11:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved column-name handling can create duplicate output columns, drop the wrong column, and leave nested group-by references unresolvable after sanitization.

3 open issues
What changed in this PR

This PR updates Flux’s shared structured-data import transformations to support column names containing special characters across Avro, delimited, JDBC, ORC, and Parquet imports.

Changes:

  • Adds case-aware column reference resolution and backtick quoting.
  • Moves sanitization before filtering and aggregation, and corrects single-column aggregation sort direction.
  • Adds unit and integration coverage and documents the updated naming behavior.
File Description
flux-cli/​src/​test/​resources/​delimited-files/​special-column-names.csv Adds sample data with special-character column names.
flux-cli/​src/​test/​java/​com/​marklogic/​flux/​impl/​importdata/​StructuredDataParamsTest.java Tests transformations with special, sanitized, quoted, and nested references.
flux-cli/​src/​test/​java/​com/​marklogic/​flux/​impl/​importdata/​ImportDelimitedFilesTest.java Adds CLI aggregation and sanitization integration tests.
flux-cli/​src/​test/​java/​com/​marklogic/​flux/​impl/​importdata/​ColumnReferencesTest.java Tests quoting, case handling, and original-name resolution.
flux-cli/​src/​test/​java/​com/​marklogic/​flux/​api/​DelimitedFilesImporterTest.java Adds API aggregation coverage with sanitization enabled.
flux-cli/​src/​main/​java/​com/​marklogic/​flux/​impl/​importdata/​StructuredDataParams.java Updates transformation order, column resolution, and array sorting.
flux-cli/​src/​main/​java/​com/​marklogic/​flux/​impl/​importdata/​ColumnReferences.java Introduces shared column-name resolution and quoting.
flux-cli/​src/​main/​java/​com/​marklogic/​flux/​api/​StructuredDataImporter.java Updates Javadoc for sanitization order and aggregation naming.
docs/​import/​structured-data/​sanitizing-column-names.md Documents early sanitization, name references, and limitations.
docs/​import/​structured-data/​filtering-data.md Explains SQL backtick quoting and shell escaping.
docs/​import/​structured-data/​aggregating-rows.md Adds special-character examples and option syntax restrictions.

Review effort: Balanced

result;

return sanitizeColumnNames(applyDrop(sorted));
return applyDrop(sorted, references);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a user error we don't need to guard against.

Comment on lines +90 to +91
String columnName = resolve(reference);
return columnNames.contains(columnName) ? quote(columnName) : reference;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 166b884. When a path starts with a name wrapped in backticks, that name is now resolved through the original-to-sanitized mapping, and the rest of the path keeps the quoting it was given. So with simple, `home address`.city is passed to Spark as `home_address`.city.

Covered by ColumnReferencesTest.pathWithQuotedStructColumn and by StructuredDataParamsTest.fieldsInStructColumnWithSimpleStrategy, which groups by a field of a renamed struct column using both the quoted and unquoted forms.

Comment on lines +356 to +359
// The references are those of the columns read from the data source, even when this is called after
// grouping. That is intentional: it is what allows a column to be dropped via its original name. Any
// other name, such as that of an aggregation, is not a known reference and is passed to Spark as-is.
dataset = dataset.drop(columnsToDrop.stream().map(references::resolve).toArray(String[]::new));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like above too - this is the user shooting themselves in the foot by picking overlapping column names for an aggregation.

@prgs-rsingh
prgs-rsingh force-pushed the feature/mle-31969-column-references branch from 9b5a269 to 12c26f2 Compare November 23, 2026 11:42
Column names passed to --group-by, --aggregate, and --aggregate-order-by were
handed to Spark APIs that parse their input, so a period was read as a path to
a nested field and a space broke the generated array_sort expression. A period
in any column, aggregated or not, was enough to fail every aggregation.

A reference that matches a top-level column is now quoted with backticks. It
is matched regardless of case unless spark.sql.caseSensitive is enabled, as
that is how Spark resolves column names. In a path to a field in a struct
column, only the name of the column is quoted.

--column-name-strategy is now applied first instead of last. Every option sees
the sanitized names, and the fields in an aggregation are named after the
sanitized columns. The original name of a column is still accepted by every
option, including in a path to a field in a struct column.

An unknown 'group by' column is now reported as such, and the error for a
failed aggregation includes the cause reported by Spark.
--aggregate-order-by on an aggregation of a single column sorted in the
opposite direction to the one requested, as the 'ascending' flag passed to
Spark's sort_array function was negated.
@prgs-rsingh
prgs-rsingh force-pushed the feature/mle-31969-column-references branch from 12c26f2 to a720166 Compare November 26, 2026 09:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants