|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership |
| 3 | +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements |
| 4 | + |
| 5 | +//! The pre-analysis UNION schema must describe the output types (#27023). |
| 6 | +
|
| 7 | +use arrow::datatypes::{DataType, TimeUnit}; |
| 8 | +use datafusion::prelude::*; |
| 9 | +use datafusion_common::{Result, ScalarValue}; |
| 10 | + |
| 11 | +fn assert_union_schema(df: &DataFrame, expected: &[(&str, DataType)]) { |
| 12 | + let fields = df.schema().fields(); |
| 13 | + assert_eq!(fields.len(), expected.len()); |
| 14 | + for (field, (name, data_type)) in fields.iter().zip(expected) { |
| 15 | + assert_eq!(field.name(), name); |
| 16 | + assert_eq!(field.data_type(), data_type); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +#[tokio::test] |
| 21 | +async fn union_infers_types_before_analysis() -> Result<()> { |
| 22 | + let ctx = SessionContext::new(); |
| 23 | + let cases = [ |
| 24 | + ( |
| 25 | + r#"arrow_cast(arrow_cast(1, 'Timestamp(Microsecond, None)'), 'Timestamp(Microsecond, Some("UTC"))')"#, |
| 26 | + "CAST(NULL AS TIMESTAMP)", |
| 27 | + DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".into())), |
| 28 | + ), |
| 29 | + ( |
| 30 | + "arrow_cast('a', 'Utf8')", |
| 31 | + "arrow_cast('b', 'LargeUtf8')", |
| 32 | + DataType::LargeUtf8, |
| 33 | + ), |
| 34 | + ( |
| 35 | + "arrow_cast('a', 'Utf8')", |
| 36 | + "arrow_cast('b', 'Utf8View')", |
| 37 | + DataType::Utf8View, |
| 38 | + ), |
| 39 | + ("CAST(1 AS SMALLINT)", "2.5", DataType::Float64), |
| 40 | + ( |
| 41 | + "CASE WHEN false THEN 1 ELSE 2.5 END", |
| 42 | + "NULL", |
| 43 | + DataType::Float64, |
| 44 | + ), |
| 45 | + ( |
| 46 | + "CASE WHEN false THEN 1 WHEN true THEN 2.5 END", |
| 47 | + "NULL", |
| 48 | + DataType::Float64, |
| 49 | + ), |
| 50 | + ]; |
| 51 | + for (left, right, expected) in cases { |
| 52 | + for (left, right) in [(left, right), (right, left)] { |
| 53 | + for operation in ["ALL", "DISTINCT", "ALL BY NAME", "BY NAME"] { |
| 54 | + let sql = format!( |
| 55 | + "SELECT x FROM (SELECT {left} AS x UNION {operation} SELECT {right} AS x) u" |
| 56 | + ); |
| 57 | + assert_union_schema(&ctx.sql(&sql).await?, &[("x", expected.clone())]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + Ok(()) |
| 62 | +} |
| 63 | + |
| 64 | +#[tokio::test] |
| 65 | +async fn union_infers_dictionary_types_before_analysis() -> Result<()> { |
| 66 | + let ctx = SessionContext::new(); |
| 67 | + for (sql, expected) in [ |
| 68 | + ( |
| 69 | + "SELECT NULL AS x UNION ALL SELECT arrow_cast('a', 'Dictionary(Int32, Utf8)') AS x", |
| 70 | + DataType::Utf8, |
| 71 | + ), |
| 72 | + ( |
| 73 | + "SELECT CAST(1 AS SMALLINT) AS x UNION ALL SELECT arrow_cast(2, 'Dictionary(Int32, Int32)') AS x", |
| 74 | + DataType::Int32, |
| 75 | + ), |
| 76 | + ] { |
| 77 | + assert_union_schema(&ctx.sql(sql).await?, &[("x", expected)]); |
| 78 | + } |
| 79 | + for sql in [ |
| 80 | + "SELECT 1 AS y UNION ALL BY NAME SELECT arrow_cast('a', 'Dictionary(Int32, Utf8)') AS x", |
| 81 | + "SELECT arrow_cast('a', 'Dictionary(Int32, Utf8)') AS x UNION ALL BY NAME SELECT 1 AS y", |
| 82 | + ] { |
| 83 | + let df = ctx.sql(sql).await?.select_columns(&["x", "y"])?; |
| 84 | + assert_union_schema(&df, &[("x", DataType::Utf8), ("y", DataType::Int64)]); |
| 85 | + } |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +#[tokio::test] |
| 90 | +async fn union_dataframe_and_by_name_schemas() -> Result<()> { |
| 91 | + let ctx = SessionContext::new(); |
| 92 | + for variant in 0..4 { |
| 93 | + let left = ctx.sql("SELECT CAST(1 AS SMALLINT) AS x").await?; |
| 94 | + let right = ctx.sql("SELECT 2.5 AS x").await?; |
| 95 | + let df = match variant { |
| 96 | + 0 => left.union(right)?, |
| 97 | + 1 => left.union_distinct(right)?, |
| 98 | + 2 => left.union_by_name(right)?, |
| 99 | + _ => left.union_by_name_distinct(right)?, |
| 100 | + }; |
| 101 | + assert_union_schema(&df.select_columns(&["x"])?, &[("x", DataType::Float64)]); |
| 102 | + } |
| 103 | + for sql in [ |
| 104 | + "SELECT CAST(1 AS SMALLINT) AS x, 1 AS y UNION ALL BY NAME SELECT 2 AS y, 2.5 AS x", |
| 105 | + "SELECT CAST(1 AS SMALLINT) AS x UNION ALL BY NAME SELECT 2.5 AS x, 2 AS y", |
| 106 | + ] { |
| 107 | + assert_union_schema( |
| 108 | + &ctx.sql(sql).await?, |
| 109 | + &[("x", DataType::Float64), ("y", DataType::Int64)], |
| 110 | + ); |
| 111 | + } |
| 112 | + let df = ctx |
| 113 | + .sql( |
| 114 | + "SELECT CAST(NULL AS SMALLINT) AS x WHERE false \ |
| 115 | + UNION ALL SELECT CAST(NULL AS BIGINT) AS x WHERE false", |
| 116 | + ) |
| 117 | + .await?; |
| 118 | + assert_union_schema(&df, &[("x", DataType::Int64)]); |
| 119 | + let df = ctx |
| 120 | + .sql( |
| 121 | + "SELECT max(x) AS x FROM (SELECT 1 AS x UNION ALL SELECT 2.5 AS x \ |
| 122 | + UNION ALL SELECT 3 AS x) u", |
| 123 | + ) |
| 124 | + .await?; |
| 125 | + assert_union_schema(&df, &[("x", DataType::Float64)]); |
| 126 | + Ok(()) |
| 127 | +} |
| 128 | + |
| 129 | +#[tokio::test] |
| 130 | +async fn union_bound_parameters_keep_inferred_type() -> Result<()> { |
| 131 | + let ctx = SessionContext::new(); |
| 132 | + let df = ctx |
| 133 | + .sql("SELECT CAST($1 AS INT) AS x UNION ALL SELECT 2.5 AS x") |
| 134 | + .await? |
| 135 | + .with_param_values(vec![ScalarValue::Int32(Some(1))])?; |
| 136 | + assert_union_schema(&df, &[("x", DataType::Float64)]); |
| 137 | + Ok(()) |
| 138 | +} |
0 commit comments