csvzall 0.4.0
CSV command-line tool and emerging C++ library
Loading...
Searching...
No Matches
schema_inference.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef CSVZALL_HAVE_POSTGRESQL
4
5#include <string>
6#include <vector>
7
8namespace csv {
9class CSVRow;
10enum class DataType;
11}
12
13namespace csvzall::postgres {
14
15// PostgreSQL column type
16enum class ColumnType {
17 INTEGER,
18 BIGINT,
19 NUMERIC,
21 TEXT
22};
23
24// Get PostgreSQL type name
25std::string ColumnTypeToString(ColumnType type);
26
27// Represents a single inferred column
29 std::string name;
31 bool nullable = true;
32};
33
34// Schema inference state
36 public:
37 struct ColumnStats {
38 std::string name;
40 int bigint_count = 0;
42 int text_count = 0;
44 int null_count = 0;
45 };
46
47 explicit SchemaInference(const std::vector<std::string>& headers);
48
49 // Add a row and update type statistics
50 void observe_row(const csv::CSVRow& row);
51 void observe_row(const std::vector<std::string>& row);
52
53 // Finalize inference and return inferred columns
54 std::vector<InferredColumn> finalize();
55 static std::vector<InferredColumn> finalize_stats(const std::vector<ColumnStats>& stats);
56 static void observe_type(ColumnStats& stats, csv::DataType type);
57
58 // Check if we've observed enough rows to stop inference
59 bool has_enough_samples() const { return samples_ >= max_samples_; }
60
61 private:
62 static constexpr int max_samples_ = 1000;
63 int samples_ = 0;
64
65 std::vector<ColumnStats> column_stats_;
66
67 // Decide final type for a column
68 static ColumnType infer_type(const ColumnStats& stats);
69};
70
71} // namespace csvzall::postgres
72
73namespace csvzall::pipeline {
74namespace postgres = ::csvzall::postgres;
75}
76
77#endif // CSVZALL_HAVE_POSTGRESQL
Definition schema_inference.hpp:35
static std::vector< InferredColumn > finalize_stats(const std::vector< ColumnStats > &stats)
Definition schema_inference.cpp:56
bool has_enough_samples() const
Definition schema_inference.hpp:59
void observe_row(const csv::CSVRow &row)
Definition schema_inference.cpp:42
std::vector< InferredColumn > finalize()
Definition schema_inference.cpp:52
static void observe_type(ColumnStats &stats, csv::DataType type)
Definition schema_inference.cpp:70
std::vector< JsonValue > array
Definition json_extract.cpp:33
Definition schema_inference.hpp:8
Definition chart_spec.hpp:100
Definition commands.hpp:16
ColumnType
Definition schema_inference.hpp:16
std::string ColumnTypeToString(ColumnType type)
Definition schema_inference.cpp:9
Definition schema_inference.hpp:28
std::string name
Definition schema_inference.hpp:29
bool nullable
Definition schema_inference.hpp:31
ColumnType type
Definition schema_inference.hpp:30
Definition schema_inference.hpp:37
int total_non_null
Definition schema_inference.hpp:43
int text_count
Definition schema_inference.hpp:42
int bigint_count
Definition schema_inference.hpp:40
int numeric_count
Definition schema_inference.hpp:41
int null_count
Definition schema_inference.hpp:44
std::string name
Definition schema_inference.hpp:38
int integer_count
Definition schema_inference.hpp:39