Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[SPARK-62441][SQL] Collate string literals with collated inputs in listagg, array_insert, array_prepend, map_concat, map_contains_key and element_at - #61196

Open
SEPURI-SAI-KRISHNA wants to merge 3 commits into
apache:masterfrom
SEPURI-SAI-KRISHNA:collation-literal-coercion

Conversation

@SEPURI-SAI-KRISHNA

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

CollationTypeCoercion collates the string inputs of a fixed list of expressions to a single type, so that a string literal takes the collation of a collated input, as in array_append(arr, 'x') or concat_ws(',', s, 'x'). ListAgg, ArrayInsert, ArrayPrepend, MapConcat, MapContainsKey and ElementAt are not in it, while their type checks require the same type.

This PR adds them:

  • ArrayInsert, ArrayPrepend and MapConcat join the existing list, like ArrayAppend and ArrayPosition.
  • ListAgg collates only its value and delimiter. The WITHIN GROUP order expressions are not inputs of the result, so they keep their own types.
  • ElementAt and MapContainsKey on a map cast a string literal key to the key type of the map when their collations differ, as GetMapValue (m['K']) already does. Like m['K' COLLATE UNICODE], a key of another explicit collation still fails.

Why are the changes needed?

With a collated input, these functions fail to resolve when another argument is a plain string literal:

CREATE TEMPORARY VIEW c AS SELECT
  CAST('Ab' AS STRING COLLATE UTF8_LCASE) AS s,
  array(CAST('Ab' AS STRING COLLATE UTF8_LCASE)) AS arr,
  map(CAST('k' AS STRING COLLATE UTF8_LCASE), CAST('v' AS STRING COLLATE UTF8_LCASE)) AS m;

SELECT listagg(s, ',') FROM c;                 -- DATATYPE_MISMATCH.DATA_DIFF_TYPES
SELECT string_agg(s, ',') FROM c;              -- DATATYPE_MISMATCH.DATA_DIFF_TYPES
SELECT array_insert(arr, 1, 'x') FROM c;       -- DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES
SELECT array_prepend(arr, 'x') FROM c;         -- DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES
SELECT map_concat(m, map('k2', 'v2')) FROM c;  -- DATATYPE_MISMATCH.DATA_DIFF_TYPES
SELECT map_contains_key(m, 'K') FROM c;        -- DATATYPE_MISMATCH.MAP_FUNCTION_DIFF_TYPES
SELECT element_at(m, 'K') FROM c;              -- DATATYPE_MISMATCH.MAP_FUNCTION_DIFF_TYPES
SELECT try_element_at(m, 'K') FROM c;          -- DATATYPE_MISMATCH.MAP_FUNCTION_DIFF_TYPES

The same calls work for array_append, array_position, array_contains, concat, concat_ws and m['K']. Reproduced on 4.0.3 and on master.

Does this PR introduce any user-facing change?

Yes. The queries above now resolve, and their results take the collation of the collated input. For example, listagg(s, ',') returns a STRING COLLATE UTF8_LCASE, and map_contains_key(m, 'K') and element_at(m, 'K') find the key 'k' under UTF8_LCASE, as m['K'] does. Previously, they failed with the errors above. The rule changes only expressions with a collated string input whose string inputs had different collations, which failed the type checks before.

How was this patch tested?

New test in CollationTypePrecedenceSuite, run with the fixed-point and the single-pass analyzer, which fails without the change: listagg and string_agg with a delimiter, with WITHIN GROUP and DISTINCT, array_insert, array_prepend (also with an explicit collation that wins), map_concat, map_contains_key, element_at and try_element_at, and a map key of another explicit collation that still fails.

Ran the *Collation* suites of sql/core and sql/catalyst, TypeCoercionSuite, AnsiTypeCoercionSuite, and the collation golden files of SQLQueryTestSuite, all passing.

#61038 also targets SPARK-62441 with a similar change. This PR additionally runs the test under the single-pass analyzer and covers DISTINCT and a map key of another explicit collation.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added the SQL label Dec 5, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant