This is an automated email from the ASF dual-hosted git repository.

jakevin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 57e38fb16a Support array literal with scalar function (#8884)
57e38fb16a is described below

commit 57e38fb16a9918fb16c2c50e2188cc22c0afe4ad
Author: Jay Zhan <[email protected]>
AuthorDate: Thu Jan 18 11:19:23 2024 +0800

    Support array literal with scalar function (#8884)
    
    * fix struct
    
    Signed-off-by: jayzhan211 <[email protected]>
    
    * add test
    
    Signed-off-by: jayzhan211 <[email protected]>
    
    * support functions
    
    Signed-off-by: jayzhan211 <[email protected]>
    
    ---------
    
    Signed-off-by: jayzhan211 <[email protected]>
---
 datafusion/sql/src/expr/value.rs             | 40 +++++-----------------------
 datafusion/sqllogictest/test_files/array.slt | 26 ++++++++++++++++--
 2 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/datafusion/sql/src/expr/value.rs b/datafusion/sql/src/expr/value.rs
index 9f88318ab2..c0870cc541 100644
--- a/datafusion/sql/src/expr/value.rs
+++ b/datafusion/sql/src/expr/value.rs
@@ -24,8 +24,8 @@ use datafusion_common::{
 };
 use datafusion_expr::expr::ScalarFunction;
 use datafusion_expr::expr::{BinaryExpr, Placeholder};
+use datafusion_expr::BuiltinScalarFunction;
 use datafusion_expr::{lit, Expr, Operator};
-use datafusion_expr::{BuiltinScalarFunction, ScalarFunctionDefinition};
 use log::debug;
 use sqlparser::ast::{BinaryOperator, Expr as SQLExpr, Interval, Value};
 use sqlparser::parser::ParserError::ParserError;
@@ -135,38 +135,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         elements: Vec<SQLExpr>,
         schema: &DFSchema,
     ) -> Result<Expr> {
-        let mut values = Vec::with_capacity(elements.len());
-
-        for element in elements {
-            let value = self.sql_expr_to_logical_expr(
-                element,
-                schema,
-                &mut PlannerContext::new(),
-            )?;
-
-            match value {
-                Expr::Literal(_) => {
-                    values.push(value);
-                }
-                Expr::ScalarFunction(ScalarFunction {
-                    func_def: ScalarFunctionDefinition::BuiltIn(fun),
-                    ..
-                }) => {
-                    if fun == BuiltinScalarFunction::MakeArray {
-                        values.push(value);
-                    } else {
-                        return not_impl_err!(
-                            "ScalarFunctions without MakeArray are not 
supported: {value}"
-                        );
-                    }
-                }
-                _ => {
-                    return not_impl_err!(
-                        "Arrays with elements other than literal are not 
supported: {value}"
-                    );
-                }
-            }
-        }
+        let values = elements
+            .into_iter()
+            .map(|element| {
+                self.sql_expr_to_logical_expr(element, schema, &mut 
PlannerContext::new())
+            })
+            .collect::<Result<Vec<_>>>()?;
 
         Ok(Expr::ScalarFunction(ScalarFunction::new(
             BuiltinScalarFunction::MakeArray,
diff --git a/datafusion/sqllogictest/test_files/array.slt 
b/datafusion/sqllogictest/test_files/array.slt
index 342fcb5bec..55cd177245 100644
--- a/datafusion/sqllogictest/test_files/array.slt
+++ b/datafusion/sqllogictest/test_files/array.slt
@@ -454,11 +454,33 @@ AS
   FROM nested_arrays_with_repeating_elements
 ;
 
+# Array literal
+
+## boolean coercion is not supported
 query error
 select [1, true, null]
 
-query error DataFusion error: This feature is not implemented: ScalarFunctions 
without MakeArray are not supported: now()
-SELECT [now()]
+## wrapped in array_length to get deterministic results
+query I
+SELECT array_length([now()])
+----
+1
+
+## array literal with functions
+query ?
+select [abs(-1.2), sin(-1), log(2), ceil(3.141)]
+----
+[1.2, -0.8414709848078965, 0.3010299801826477, 4.0]
+
+## array literal with nested types
+query ???
+select
+  [struct('foo', 1)],
+  [struct('foo', [1,2,3])],
+  [struct('foo', [struct(3, 'x')])]
+;
+----
+[{c0: foo, c1: 1}] [{c0: foo, c1: [1, 2, 3]}] [{c0: foo, c1: [{c0: 3, c1: x}]}]
 
 query TTT
 select arrow_typeof(column1), arrow_typeof(column2), arrow_typeof(column3) 
from arrays;

Reply via email to