Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maropu committed Nov 23, 2024
0 parents commit 3e3d90d
Show file tree
Hide file tree
Showing 14 changed files with 300,809 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/build_and_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Build and test

on:
push:
branches:
- master

jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
env:
RUST_BACKTRACE: 1
steps:
- name: Checkout pg_stats_exporter repository
uses: actions/checkout@v3
# In order to fetch changed files
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt, clippy
- name: Checkout DuckDB source code
run: make duckdb
- name: Build extension
run: make
- name: Run tests
run: make test
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
duckdb
build
cmake-build-debug
.idea
.DS_Store
.clang-format
.vscode
duckdb_unittest_tempdir
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 2.8.12)
set(TARGET_NAME csv_scanner)
project(${TARGET_NAME})

set(OVERRIDE_GIT_DESCRIBE "v1.1.1")

if(NOT MSVC)
set(CSV_SCANNER_EXTRA_CFLAGS
"-Wno-pedantic -Wno-sign-compare -Wno-unused-variable")
endif()

set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} ${CSV_SCANNER_EXTRA_CFLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} ${CSV_SCANNER_EXTRA_CFLAGS}")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO
"${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CSV_SCANNER_EXTRA_CFLAGS}")

set(CMAKE_C_FLAGS_DEBUG
"${CMAKE_C_FLAGS_DEBUG} ${CSV_SCANNER_EXTRA_CFLAGS} ${CSV_SCANNER_EXTRA_CFLAGS}"
)
set(CMAKE_C_FLAGS_RELEASE
"${CMAKE_C_FLAGS_RELEASE} ${CSV_SCANNER_EXTRA_CFLAGS}")
set(CMAKE_C_FLAGS_RELWITHDEBINFO
"${CMAKE_C_FLAGS_RELWITHDEBINFO} ${CSV_SCANNER_EXTRA_CFLAGS}")

add_subdirectory(src)


set(PARAMETERS "-no-warnings")
build_loadable_extension(${TARGET_NAME} ${PARAMETERS} ${ALL_OBJECT_FILES})

if(WIN32)
target_include_directories(
${TARGET_NAME}_loadable_extension)
endif()
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018-2024 Stichting DuckDB Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
.PHONY: all clean format debug release pull

all: release

DUCKDB_VERSION = "1.1.3"

MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJ_DIR := $(dir $(MKFILE_PATH))

ifeq ($(OS),Windows_NT)
TEST_PATH="/test/Release/unittest.exe"
else
TEST_PATH="/test/unittest"
endif

#### OSX config
OSX_BUILD_FLAG=
ifneq (${OSX_BUILD_ARCH}, "")
OSX_BUILD_FLAG=-DOSX_BUILD_ARCH=${OSX_BUILD_ARCH}
endif

#### VCPKG config
VCPKG_TOOLCHAIN_PATH?=
ifneq ("${VCPKG_TOOLCHAIN_PATH}", "")
TOOLCHAIN_FLAGS:=${TOOLCHAIN_FLAGS} -DVCPKG_MANIFEST_DIR='${PROJ_DIR}' -DVCPKG_BUILD=1 -DCMAKE_TOOLCHAIN_FILE='${VCPKG_TOOLCHAIN_PATH}'
endif
ifneq ("${VCPKG_TARGET_TRIPLET}", "")
TOOLCHAIN_FLAGS:=${TOOLCHAIN_FLAGS} -DVCPKG_TARGET_TRIPLET='${VCPKG_TARGET_TRIPLET}'
endif

#### Enable Ninja as generator
ifeq ($(GEN),ninja)
GENERATOR=-G "Ninja" -DFORCE_COLORED_OUTPUT=1
endif

#### Configuration for this extension
EXTENSION_NAME=CSV_SCANNER
EXTENSION_FLAGS=\
-DDUCKDB_EXTENSION_NAMES="csv_scanner" \
-DDUCKDB_EXTENSION_${EXTENSION_NAME}_PATH="$(PROJ_DIR)" \
-DDUCKDB_EXTENSION_${EXTENSION_NAME}_SHOULD_LINK=0 \
-DDUCKDB_EXTENSION_${EXTENSION_NAME}_LOAD_TESTS=1 \
-DDUCKDB_EXTENSION_${EXTENSION_NAME}_TEST_PATH="$(PROJ_DIR)test/sql"

BUILD_FLAGS=-DEXTENSION_STATIC_BUILD=1 $(EXTENSION_FLAGS) $(OSX_BUILD_FLAG) $(TOOLCHAIN_FLAGS) -DDUCKDB_EXPLICIT_PLATFORM='${DUCKDB_PLATFORM}'

duckdb:
./duckconfigure $(DUCKDB_VERSION)

clean:
rm -rf build
[ -d "duckdb" ] && (cd duckdb && make clean)

# Main build
debug: duckdb
mkdir -p build/debug && \
cmake $(GENERATOR) $(BUILD_FLAGS) -DCMAKE_BUILD_TYPE=Debug -S ./duckdb/ -B build/debug && \
cmake --build build/debug --config Debug

reldebug: duckdb
mkdir -p build/reldebug && \
cmake $(GENERATOR) $(BUILD_FLAGS) -DCMAKE_BUILD_TYPE=RelWithDebInfo -S ./duckdb/ -B build/reldebug && \
cmake --build build/reldebug --config RelWithDebInfo

release: duckdb
mkdir -p build/release && \
cmake $(GENERATOR) $(BUILD_FLAGS) -DCMAKE_BUILD_TYPE=Release -S ./duckdb/ -B build/release && \
cmake --build build/release --config Release

test: test_release
test_release: release
./build/release/$(TEST_PATH) --test-dir "$(PROJ_DIR)" "test/*"
test_debug: debug
./build/debug/$(TEST_PATH) --test-dir "$(PROJ_DIR)" "test/*"

format:
cp duckdb/.clang-format .
find src/ -iname "*.hpp" -o -iname "*.cpp" | xargs clang-format --sort-includes=0 -style=file -i
cmake-format -i CMakeLists.txt
rm .clang-format
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Build and test](https://github.com/maropu/duckdb_scanner_example/actions/workflows/build_and_tets.yml/badge.svg)](https://github.com/maropu/duckdb_scanner_example/actions/workflows/build_and_tets.yml)

This repository aims not to implenet a full-fledge CSV parser for DuckDB but to demonstrate a minimum table
function example for letting you know how to implement a custom scanner for your datasource.
This CSV parser implementation relies on a table function and if you would like to see how it works,
please see [maropu/duckdb_extension_example](https://github.com/maropu/duckdb_extension_example).

# Minimum Required Components

```bash
.
|-- CMakeLists.txt // Root CMake build file
|-- Makefile // Build script to wrap cmake
|-- data // Test data used in `test/sql/csv_scanner.test`
| |-- random.csv
| `-- test.csv
|-- duckdb // DuckDB source code that this extension depends on
|-- src
| |-- CMakeLists.txt // CMake build file to list source files
| |-- csv_scanner_extension.cpp // CSV parser implmenetation
| |-- include
| | `-- csv_scanner.hpp // Header file for the CSV parser
| `-- scan_csv.cpp // Entrypoint where DuckDB loads this extension
`-- test
`-- sql
`-- csv_scanner.test // Test code
```

# Specification of this toy CSV parser

- Multi-threading for scanning CSV data supported
- VARCHAR, BIGINT, and DOUBLE types only supported
- Schema inference not supported

# Running this example

```bash
$ make duckdb
Cloning into 'duckdb'...
...
Note: switching to 'tags/v1.1.3'.
...

$ make
mkdir -p build/release && \
cmake -DEXTENSION_STATIC_BUILD=1 -DDUCKDB_EXTENSION_NAMES="csv_scanner" -DDUCKDB_EXTENSION_CSV_SCANNER_PATH="./duckdb/duckdb_scanner_example/" -DDUCKDB_EXTENSION_CSV_SCANNER_SHOULD_LINK=0 -DDUCKDB_EXTENSION_CSV_SCANNER_LOAD_TESTS=1 -DDUCKDB_EXTENSION_CSV_SCANNER_TEST_PATH="./duckdb/duckdb_scanner_example/test/sql" -DOSX_BUILD_ARCH= -DDUCKDB_EXPLICIT_PLATFORM='' -DCMAKE_BUILD_TYPE=Release -S ./duckdb/ -B build/release && \
cmake --build build/release --config Release
-- git hash af39bd0dcf, version v1.1.3, extension folder v1.1.3
-- Extensions will be deployed to: ./duckdb/duckdb_scanner_example/build/release/repository
-- Load extension 'csv_scanner' from './duckdb/duckdb_scanner_example/'
-- Load extension 'parquet' from './duckdb/duckdb_scanner_example/duckdb/extensions' @ v1.1.3
-- Extensions linked into DuckDB: [parquet]
-- Extensions built but not linked: [csv_scanner]
-- Tests loaded for extensions: [csv_scanner]
-- Configuring done
-- Generating done
-- Build files have been written to: ./duckdb/duckdb_scanner_example/build/release
[ 1%] Built target duckdb_platform_binary
[ 1%] Built target duckdb_platform
...
[100%] Linking CXX executable unittest
[100%] Built target unittest
[100%] Built target imdb

$ make test
...
[1/1] (100%): test/sql/csv_scanner.test
===============================================================================
All tests passed (13 assertions in 1 test case)

$ cat data/test.csv
aaa,1,1.23
bbb,2,3.14
ccc,3,2.56

$ duckdb -unsigned
v1.1.3 af39bd0dcf
Enter ".help" for usage hints.
D LOAD './build/release/extension/csv_scanner/csv_scanner.duckdb_extension';
D SELECT * FROM scan_csv_ex('data/test.csv', {'a': 'varchar', 'b': 'bigint', 'c': 'double'});
┌─────────┬───────┬────────┐
│ a │ b │ c │
│ varchar │ int64 │ double │
├─────────┼───────┼────────┤
│ aaa │ 1 │ 1.23 │
│ bbb │ 2 │ 3.14 │
│ ccc │ 3 │ 2.56 │
└─────────┴───────┴────────┘
```

# Any Question?

If you have any question, please feel free to leave it on [Issues](https://github.com/maropu/duckdb_scanner_example/issues)
or Twitter ([@maropu](http://twitter.com/#!/maropu)).
Loading

0 comments on commit 3e3d90d

Please sign in to comment.