Skip to content

Commit 02243ce

Browse files
author
Brent Gardner
committed
Passing serde test
1 parent 119cf6f commit 02243ce

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

datafusion/core/src/test_util.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::any::Any;
2121
use std::collections::BTreeMap;
2222
use std::{env, error::Error, path::PathBuf, sync::Arc};
2323

24-
use crate::datasource::custom::CustomTable;
2524
use crate::datasource::datasource::TableProviderFactory;
2625
use crate::datasource::{empty::EmptyTable, provider_as_source, TableProvider};
2726
use crate::execution::context::SessionState;
@@ -331,39 +330,31 @@ pub struct TestTableFactory {}
331330
impl TableProviderFactory for TestTableFactory {
332331
async fn create(
333332
&self,
334-
_name: &str,
335333
url: &str,
336334
) -> datafusion_common::Result<Arc<dyn TableProvider>> {
337-
Ok(Arc::new(CustomTable::new(
338-
"deltatable",
339-
url,
340-
Arc::new(TestTableProvider {}),
341-
)))
335+
Ok(Arc::new(TestTableProvider { url: url.to_string() }))
342336
}
343337

344338
fn with_schema(
345339
&self,
346340
_schema: SchemaRef,
347-
table_type: &str,
348341
url: &str,
349342
) -> datafusion_common::Result<Arc<dyn TableProvider>> {
350-
Ok(Arc::new(CustomTable::new(
351-
table_type,
352-
url,
353-
Arc::new(TestTableProvider {}),
354-
)))
343+
Ok(Arc::new(TestTableProvider { url: url.to_string() }))
355344
}
356345
}
357346

358347
/// TableProvider for testing purposes
359-
pub struct TestTableProvider {}
348+
pub struct TestTableProvider {
349+
pub url: String
350+
}
360351

361352
impl TestTableProvider {}
362353

363354
#[async_trait]
364355
impl TableProvider for TestTableProvider {
365356
fn as_any(&self) -> &dyn Any {
366-
unimplemented!("TestTableProvider is a stub for testing.")
357+
self
367358
}
368359

369360
fn schema(&self) -> SchemaRef {

datafusion/proto/src/lib.rs

+71-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod roundtrip_tests {
5151
logical_plan_to_bytes, logical_plan_to_bytes_with_extension_codec,
5252
};
5353
use crate::logical_plan::LogicalExtensionCodec;
54-
use arrow::datatypes::Schema;
54+
use arrow::datatypes::{Schema, SchemaRef};
5555
use arrow::{
5656
array::ArrayRef,
5757
datatypes::{
@@ -65,7 +65,7 @@ mod roundtrip_tests {
6565
use datafusion::prelude::{
6666
create_udf, CsvReadOptions, SessionConfig, SessionContext,
6767
};
68-
use datafusion::test_util::TestTableFactory;
68+
use datafusion::test_util::{TestTableFactory, TestTableProvider};
6969
use datafusion_common::{DFSchemaRef, DataFusionError, ScalarValue};
7070
use datafusion_expr::create_udaf;
7171
use datafusion_expr::expr::{Between, BinaryExpr, Case, GroupingSet, Like};
@@ -81,6 +81,7 @@ mod roundtrip_tests {
8181
use std::fmt::Debug;
8282
use std::fmt::Formatter;
8383
use std::sync::Arc;
84+
use datafusion::datasource::TableProvider;
8485

8586
#[cfg(feature = "json")]
8687
fn roundtrip_json_test(proto: &protobuf::LogicalExprNode) {
@@ -135,22 +136,73 @@ mod roundtrip_tests {
135136
Ok(())
136137
}
137138

139+
#[derive(Clone, PartialEq, ::prost::Message)]
140+
pub struct TestTableProto {
141+
/// URL of the table root
142+
#[prost(string, tag = "1")]
143+
pub url: String,
144+
}
145+
146+
#[derive(Debug)]
147+
pub struct TestTableProviderCodec {}
148+
149+
impl LogicalExtensionCodec for TestTableProviderCodec {
150+
fn try_decode(&self, buf: &[u8], inputs: &[LogicalPlan], ctx: &SessionContext) -> Result<Extension, DataFusionError> {
151+
Err(DataFusionError::NotImplemented(
152+
"No extension codec provided".to_string(),
153+
))
154+
}
155+
156+
fn try_encode(&self, node: &Extension, buf: &mut Vec<u8>) -> Result<(), DataFusionError> {
157+
Err(DataFusionError::NotImplemented(
158+
"No extension codec provided".to_string(),
159+
))
160+
}
161+
162+
fn try_decode_table_provider(&self, buf: &[u8], schema: SchemaRef, ctx: &SessionContext) -> Result<Arc<dyn TableProvider>, DataFusionError> {
163+
let msg = TestTableProto::decode(buf)
164+
.map_err(|_| DataFusionError::Internal("Error encoding test table".to_string()))?;
165+
let state = ctx.state.read();
166+
let factory = state
167+
.runtime_env
168+
.table_factories
169+
.get("testtable")
170+
.ok_or_else(|| {
171+
DataFusionError::Plan(format!(
172+
"Unable to find testtable factory",
173+
))
174+
})?;
175+
let provider = (*factory).with_schema(schema, msg.url.as_str())?;
176+
Ok(provider)
177+
}
178+
179+
fn try_encode_table_provider(&self, node: Arc<dyn TableProvider>, buf: &mut Vec<u8>) -> Result<(), DataFusionError> {
180+
let table = node.as_ref().as_any().downcast_ref::<TestTableProvider>()
181+
.ok_or(DataFusionError::Internal("Can't encode non-test tables".to_string()))?;
182+
let msg = TestTableProto {
183+
url: table.url.clone()
184+
};
185+
msg.encode(buf).map_err(|_| DataFusionError::Internal("Error encoding test table".to_string()))
186+
}
187+
}
188+
138189
#[tokio::test]
139190
async fn roundtrip_custom_tables() -> Result<(), DataFusionError> {
140191
let mut table_factories: HashMap<String, Arc<dyn TableProviderFactory>> =
141192
HashMap::new();
142-
table_factories.insert("deltatable".to_string(), Arc::new(TestTableFactory {}));
193+
table_factories.insert("testtable".to_string(), Arc::new(TestTableFactory {}));
143194
let cfg = RuntimeConfig::new().with_table_factories(table_factories);
144195
let env = RuntimeEnv::new(cfg).unwrap();
145196
let ses = SessionConfig::new();
146197
let ctx = SessionContext::with_config_rt(ses, Arc::new(env));
147198

148-
let sql = "CREATE EXTERNAL TABLE dt STORED AS DELTATABLE LOCATION 's3://bucket/schema/table';";
199+
let sql = "CREATE EXTERNAL TABLE t STORED AS testtable LOCATION 's3://bucket/schema/table';";
149200
ctx.sql(sql).await.unwrap();
150201

151-
let scan = ctx.table("dt")?.to_logical_plan()?;
152-
let bytes = logical_plan_to_bytes(&scan)?;
153-
let logical_round_trip = logical_plan_from_bytes(&bytes, &ctx)?;
202+
let codec = TestTableProviderCodec {};
203+
let scan = ctx.table("t")?.to_logical_plan()?;
204+
let bytes = logical_plan_to_bytes_with_extension_codec(&scan, &codec)?;
205+
let logical_round_trip = logical_plan_from_bytes_with_extension_codec(&bytes, &ctx, &codec)?;
154206
assert_eq!(format!("{:?}", scan), format!("{:?}", logical_round_trip));
155207
Ok(())
156208
}
@@ -350,6 +402,18 @@ mod roundtrip_tests {
350402
))
351403
}
352404
}
405+
406+
fn try_decode_table_provider(&self, buf: &[u8], schema: SchemaRef, ctx: &SessionContext) -> Result<Arc<dyn TableProvider>, DataFusionError> {
407+
Err(DataFusionError::Internal(
408+
"unsupported plan type".to_string(),
409+
))
410+
}
411+
412+
fn try_encode_table_provider(&self, node: Arc<dyn TableProvider>, buf: &mut Vec<u8>) -> Result<(), DataFusionError> {
413+
Err(DataFusionError::Internal(
414+
"unsupported plan type".to_string(),
415+
))
416+
}
353417
}
354418

355419
#[test]

0 commit comments

Comments
 (0)