You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jonahgao (via GitHub)" <gi...@apache.org> on 2023/12/04 16:19:16 UTC

[PR] feat: customize column default values for external tables [arrow-datafusion]

jonahgao opened a new pull request, #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415

   ## Which issue does this PR close?
   
   It is a continuation of https://github.com/apache/arrow-datafusion/pull/8283.
   
   
   ## Rationale for this change
   
   We previously implemented customizing column default values on memory tables, and this PR implements it for external tables.
   
   ## What changes are included in this PR?
   
   Support customizing column default values for external tables.
   This feature will take effect when inserting.
   
   ## Are these changes tested?
   Yes
   
   ## Are there any user-facing changes?
   
   Yes.
   
   Add a new field `column_defaults` in `DdlStatement::CreateExternalTable`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1414190653


##########
datafusion/core/src/datasource/memory.rs:
##########
@@ -19,9 +19,9 @@
 
 use datafusion_physical_plan::metrics::MetricsSet;
 use futures::StreamExt;
-use hashbrown::HashMap;
 use log::debug;
 use std::any::Any;
+use std::collections::HashMap;

Review Comment:
   I introduced `hashbrown::HashMap` in https://github.com/apache/arrow-datafusion/pull/8283, now I am replacing it with `std::collections::HashMap`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1418020824


##########
datafusion/sqllogictest/test_files/insert.slt:
##########
@@ -382,6 +382,27 @@ select a,b,c,d from test_column_defaults
 1 10 100 ABC
 NULL 20 500 default_text
 
+# fill the timestamp column with default value `now()` again, it should be different from the previous one
+query IIITP
+insert into test_column_defaults(a, b, c, d) values(2, 20, 200, 'DEF')
+----
+1
+
+# Ensure that the default expression `now()` is evaluated during insertion, not optimized away.

Review Comment:
   👌 



##########
datafusion/sqllogictest/test_files/insert.slt:
##########
@@ -382,6 +382,27 @@ select a,b,c,d from test_column_defaults
 1 10 100 ABC
 NULL 20 500 default_text
 
+# fill the timestamp column with default value `now()` again, it should be different from the previous one
+query IIITP
+insert into test_column_defaults(a, b, c, d) values(2, 20, 200, 'DEF')
+----
+1
+
+# Ensure that the default expression `now()` is evaluated during insertion, not optimized away.

Review Comment:
   👌 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1414158951


##########
datafusion/proto/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -232,11 +232,12 @@ async fn roundtrip_custom_listing_tables() -> Result<()> {
             WITH ORDER (c ASC)
             LOCATION '../core/tests/data/window_2.csv';";
 
-    let plan = ctx.sql(query).await?.into_optimized_plan()?;

Review Comment:
   It always returned an [EmptyRelation](https://github.com/apache/arrow-datafusion/blob/4b4af65444761f2f2c87b74b0fac8a19db4912a9/datafusion/core/src/execution/context/mod.rs#L523) before, and did not achieve the testing purpose as expected.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1416240577


##########
datafusion/sqllogictest/test_files/insert_to_external.slt:
##########
@@ -543,3 +543,48 @@ select * from table_without_values;
 
 statement ok
 drop table table_without_values;
+
+
+### Test for specifying column's default value
+
+statement ok
+CREATE EXTERNAL TABLE test_column_defaults(
+  a int,
+  b int not null default null,
+  c int default 100*2+300,
+  d text default lower('DEFAULT_TEXT'),
+  e timestamp default now()
+) STORED AS parquet
+LOCATION 'test_files/scratch/insert_to_external/external_parquet_table_q6'
+OPTIONS (create_local_path 'true');
+
+query IIITP
+insert into test_column_defaults values(1, 10, 100, 'ABC', now())
+----
+1
+
+statement error DataFusion error: Execution error: Invalid batch column at '1' has null but schema specifies non-nullable
+insert into test_column_defaults(a) values(2)
+
+query IIITP
+insert into test_column_defaults(b) values(20)
+----
+1
+
+query IIIT rowsort
+select a,b,c,d from test_column_defaults

Review Comment:
   Could you also add a test that the value of `now()` was set?
   
   Maybe something basic like
   
   ```sql
   -- Expect all rows to be true as now() was inserted into the table
   select e < now() from test_column_defaults
   ```
   ?



##########
datafusion/proto/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -232,11 +232,12 @@ async fn roundtrip_custom_listing_tables() -> Result<()> {
             WITH ORDER (c ASC)
             LOCATION '../core/tests/data/window_2.csv';";
 
-    let plan = ctx.sql(query).await?.into_optimized_plan()?;
+    let plan = ctx.state().create_logical_plan(query).await?;
 
     let bytes = logical_plan_to_bytes(&plan)?;
     let logical_round_trip = logical_plan_from_bytes(&bytes, &ctx)?;
-    assert_eq!(format!("{plan:?}"), format!("{logical_round_trip:?}"));
+    // Use exact matching to verify everything, such as column defaults

Review Comment:
   Nice -- I think this check predated the `LogicalPlan::PartialEq` implementation. I think this is a nice change



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1416582958


##########
datafusion/sqllogictest/test_files/insert_to_external.slt:
##########
@@ -543,3 +543,48 @@ select * from table_without_values;
 
 statement ok
 drop table table_without_values;
+
+
+### Test for specifying column's default value
+
+statement ok
+CREATE EXTERNAL TABLE test_column_defaults(
+  a int,
+  b int not null default null,
+  c int default 100*2+300,
+  d text default lower('DEFAULT_TEXT'),
+  e timestamp default now()
+) STORED AS parquet
+LOCATION 'test_files/scratch/insert_to_external/external_parquet_table_q6'
+OPTIONS (create_local_path 'true');
+
+query IIITP
+insert into test_column_defaults values(1, 10, 100, 'ABC', now())
+----
+1
+
+statement error DataFusion error: Execution error: Invalid batch column at '1' has null but schema specifies non-nullable
+insert into test_column_defaults(a) values(2)
+
+query IIITP
+insert into test_column_defaults(b) values(20)
+----
+1
+
+query IIIT rowsort
+select a,b,c,d from test_column_defaults

Review Comment:
   Good idea! Added. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: customize column default values for external tables [arrow-datafusion]

Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #8415:
URL: https://github.com/apache/arrow-datafusion/pull/8415#discussion_r1414158951


##########
datafusion/proto/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -232,11 +232,12 @@ async fn roundtrip_custom_listing_tables() -> Result<()> {
             WITH ORDER (c ASC)
             LOCATION '../core/tests/data/window_2.csv';";
 
-    let plan = ctx.sql(query).await?.into_optimized_plan()?;

Review Comment:
   It always returned an [EmptyRelation](https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/src/execution/context/mod.rs#L523) before, and did not achieve the testing purpose as expected.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org