alexandreyc commented on code in PR #1725:
URL: https://github.com/apache/arrow-adbc/pull/1725#discussion_r1570746494


##########
rust2/drivers/dummy/src/lib.rs:
##########
@@ -0,0 +1,890 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashSet;
+use std::sync::Arc;
+use std::{collections::HashMap, fmt::Debug, hash::Hash};
+
+use arrow::array::{
+    Array, ArrayRef, BinaryArray, BooleanArray, Float64Array, Int16Array, 
Int32Array, Int64Array,
+    ListArray, MapArray, StringArray, StructArray, UInt32Array, UInt64Array, 
UnionArray,
+};
+use arrow::buffer::{Buffer, OffsetBuffer, ScalarBuffer};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::error::ArrowError;
+use arrow::record_batch::{RecordBatch, RecordBatchReader};
+
+use adbc_core::{
+    error::{Error, Result, Status},
+    ffi::constants,
+    options::{
+        InfoCode, ObjectDepth, OptionConnection, OptionDatabase, 
OptionStatement, OptionValue,
+    },
+    schemas, Connection, Database, Driver, Optionable, PartitionedResult, 
Statement,
+};
+
+#[derive(Debug)]
+pub struct SingleBatchReader {
+    batch: Option<RecordBatch>,
+    schema: SchemaRef,
+}
+
+impl SingleBatchReader {
+    pub fn new(batch: RecordBatch) -> Self {
+        let schema = batch.schema();
+        Self {
+            batch: Some(batch),
+            schema,
+        }
+    }
+}
+
+impl Iterator for SingleBatchReader {
+    type Item = std::result::Result<RecordBatch, ArrowError>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        Ok(self.batch.take()).transpose()
+    }
+}
+
+impl RecordBatchReader for SingleBatchReader {
+    fn schema(&self) -> SchemaRef {
+        self.schema.clone()
+    }
+}
+
+fn get_table_schema() -> Schema {
+    Schema::new(vec![
+        Field::new("a", DataType::UInt32, true),
+        Field::new("b", DataType::Float64, false),
+        Field::new("c", DataType::Utf8, true),
+    ])
+}
+
+fn get_table_data() -> RecordBatch {
+    RecordBatch::try_new(
+        Arc::new(get_table_schema()),
+        vec![
+            Arc::new(UInt32Array::from(vec![1, 2, 3])),
+            Arc::new(Float64Array::from(vec![1.5, 2.5, 3.5])),
+            Arc::new(StringArray::from(vec!["A", "B", "C"])),
+        ],
+    )
+    .unwrap()
+}
+
+fn set_option<T>(options: &mut HashMap<T, OptionValue>, key: T, value: 
OptionValue) -> Result<()>
+where
+    T: Eq + Hash,
+{
+    options.insert(key, value);
+    Ok(())
+}
+
+fn get_option_bytes<T>(options: &HashMap<T, OptionValue>, key: T, kind: &str) 
-> Result<Vec<u8>>
+where
+    T: Eq + Hash + Debug,
+{
+    let value = options.get(&key);
+    match value {
+        None => Err(Error::with_message_and_status(
+            format!("Unrecognized {kind} option: {key:?}"),
+            Status::NotFound,
+        )),
+        Some(value) => match value {
+            OptionValue::Bytes(value) => Ok(value.clone()),
+            _ => Err(Error::with_message_and_status(
+                format!("Incorrect value for {kind} option: {key:?}"),
+                Status::InvalidData,
+            )),
+        },
+    }
+}
+
+fn get_option_double<T>(options: &HashMap<T, OptionValue>, key: T, kind: &str) 
-> Result<f64>
+where
+    T: Eq + Hash + Debug,
+{
+    let value = options.get(&key);
+    match value {
+        None => Err(Error::with_message_and_status(
+            format!("Unrecognized {kind} option: {key:?}"),
+            Status::NotFound,
+        )),
+        Some(value) => match value {
+            OptionValue::Double(value) => Ok(*value),
+            _ => Err(Error::with_message_and_status(
+                format!("Incorrect value for {kind} option: {key:?}"),
+                Status::InvalidData,
+            )),
+        },
+    }
+}
+
+fn get_option_int<T>(options: &HashMap<T, OptionValue>, key: T, kind: &str) -> 
Result<i64>
+where
+    T: Eq + Hash + Debug,
+{
+    let value = options.get(&key);
+    match value {
+        None => Err(Error::with_message_and_status(
+            format!("Unrecognized {kind} option: {key:?}"),
+            Status::NotFound,
+        )),
+        Some(value) => match value {
+            OptionValue::Int(value) => Ok(*value),
+            _ => Err(Error::with_message_and_status(
+                format!("Incorrect value for {kind} option: {key:?}"),
+                Status::InvalidData,
+            )),
+        },
+    }
+}
+
+fn get_option_string<T>(options: &HashMap<T, OptionValue>, key: T, kind: &str) 
-> Result<String>
+where
+    T: Eq + Hash + Debug,
+{
+    let value = options.get(&key);
+    match value {
+        None => Err(Error::with_message_and_status(
+            format!("Unrecognized {kind} option: {key:?}"),
+            Status::NotFound,
+        )),
+        Some(value) => match value {
+            OptionValue::String(value) => Ok(value.clone()),
+            _ => Err(Error::with_message_and_status(
+                format!("Incorrect value for {kind} option: {key:?}"),
+                Status::InvalidData,
+            )),
+        },
+    }
+}
+
+/// A dummy driver used for testing purposes.
+#[derive(Default)]
+pub struct DummyDriver {}
+
+impl Driver for DummyDriver {
+    type DatabaseType = DummyDatabase;
+
+    fn new_database(&mut self) -> Result<Self::DatabaseType> {
+        self.new_database_with_opts([].into_iter())
+    }
+
+    fn new_database_with_opts(
+        &mut self,
+        opts: impl Iterator<Item = (<Self::DatabaseType as 
Optionable>::Option, OptionValue)>,
+    ) -> Result<Self::DatabaseType> {
+        let mut database = Self::DatabaseType {
+            options: HashMap::new(),
+        };
+        for (key, value) in opts {
+            database.set_option(key, value)?;
+        }
+        Ok(database)
+    }
+}
+
+pub struct DummyDatabase {
+    options: HashMap<OptionDatabase, OptionValue>,
+}
+
+impl Optionable for DummyDatabase {
+    type Option = OptionDatabase;
+
+    fn set_option(&mut self, key: Self::Option, value: OptionValue) -> 
Result<()> {
+        set_option(&mut self.options, key, value)
+    }
+
+    fn get_option_bytes(&self, key: Self::Option) -> Result<Vec<u8>> {
+        get_option_bytes(&self.options, key, "database")
+    }
+
+    fn get_option_double(&self, key: Self::Option) -> Result<f64> {
+        get_option_double(&self.options, key, "database")
+    }
+
+    fn get_option_int(&self, key: Self::Option) -> Result<i64> {
+        get_option_int(&self.options, key, "database")
+    }
+
+    fn get_option_string(&self, key: Self::Option) -> Result<String> {
+        get_option_string(&self.options, key, "database")
+    }
+}
+
+impl Database for DummyDatabase {
+    type ConnectionType = DummyConnection;
+
+    fn new_connection(&mut self) -> Result<Self::ConnectionType> {
+        self.new_connection_with_opts([].into_iter())
+    }
+
+    fn new_connection_with_opts(
+        &mut self,
+        opts: impl Iterator<Item = (<Self::ConnectionType as 
Optionable>::Option, OptionValue)>,
+    ) -> Result<Self::ConnectionType> {
+        let mut connection = Self::ConnectionType {
+            options: HashMap::new(),
+        };
+        for (key, value) in opts {
+            connection.set_option(key, value)?;
+        }
+        Ok(connection)
+    }
+}
+
+pub struct DummyConnection {
+    options: HashMap<OptionConnection, OptionValue>,
+}
+
+impl Optionable for DummyConnection {
+    type Option = OptionConnection;
+
+    fn set_option(&mut self, key: Self::Option, value: OptionValue) -> 
Result<()> {
+        set_option(&mut self.options, key, value)
+    }
+
+    fn get_option_bytes(&self, key: Self::Option) -> Result<Vec<u8>> {
+        get_option_bytes(&self.options, key, "connection")
+    }
+
+    fn get_option_double(&self, key: Self::Option) -> Result<f64> {
+        get_option_double(&self.options, key, "connection")
+    }
+
+    fn get_option_int(&self, key: Self::Option) -> Result<i64> {
+        get_option_int(&self.options, key, "connection")
+    }
+
+    fn get_option_string(&self, key: Self::Option) -> Result<String> {
+        get_option_string(&self.options, key, "connection")
+    }
+}
+
+impl Connection for DummyConnection {
+    type StatementType = DummyStatement;
+
+    fn new_statement(&mut self) -> Result<Self::StatementType> {
+        Ok(Self::StatementType {
+            options: HashMap::new(),
+        })
+    }
+
+    // This method is used to test that errors round-trip correctly.
+    fn cancel(&mut self) -> Result<()> {
+        let mut error = Error::with_message_and_status("message", 
Status::Cancelled);
+        error.vendor_code = constants::ADBC_ERROR_VENDOR_CODE_PRIVATE_DATA;
+        error.sqlstate = [1, 2, 3, 4, 5];
+        error.details = Some(vec![
+            ("key1".into(), b"AAA".into()),
+            ("key2".into(), b"ZZZZZ".into()),
+        ]);
+        Err(error)
+    }
+
+    fn commit(&mut self) -> Result<()> {
+        Ok(())
+    }
+
+    fn get_info(&self, _codes: Option<HashSet<InfoCode>>) -> Result<impl 
RecordBatchReader> {

Review Comment:
   I could try to artifically use the codes but it won't provide much value. If 
we want to usefully use them it will take a fair amount of work I think.
   
   The goal here is just to test that data round-trip correctly between Rust -> 
C -> Rust (testing both the driver exporter & manager).



-- 
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-unsubscr...@arrow.apache.org

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

Reply via email to