Skip to content

Commit

Permalink
add table comments and special case Timestamps & Bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
TristenHarr committed Jan 8, 2025
1 parent 97fdfa4 commit df5bb91
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# DuckDB Connector Changelog
This changelog documents changes between release tags.

## [0.1.1] - 2025-01-08
* Add table comments to descriptions
* Special case Timestamps and BigInt filtering

## [0.1.0] - 2024-08-22
* Update Documentation for ndc-hub

Expand Down
59 changes: 46 additions & 13 deletions generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const con = db.connect();
const determineType = (t: string): string => {
switch (t) {
case "BIGINT":
return "Int";
return "BigInt";
case "BIT":
return "String";
case "BOOLEAN":
Expand All @@ -28,7 +28,7 @@ const determineType = (t: string): string => {
case "DOUBLE":
return "Float";
case "HUGEINT":
return "String";
return "HugeInt";
case "INTEGER":
return "Int";
case "INTERVAL":
Expand All @@ -42,13 +42,15 @@ const determineType = (t: string): string => {
case "TIME":
return "String";
case "TIMESTAMP":
return "String";
return "Timestamp";
case "TIMESTAMP WITH TIME ZONE":
return "String";
return "TimestampTz";
case "TINYINT":
return "Int";
case "UBIGINT":
return "String";
return "UBigInt";
case "UHUGEINT":
return "UHugeInt"
case "UINTEGER":
return "Int";
case "USMALLINT":
Expand All @@ -59,8 +61,6 @@ const determineType = (t: string): string => {
return "String";
case "VARCHAR":
return "String";
case "HUGEINT":
return "String";
default:
if (t.startsWith("DECIMAL")){
return "Float";
Expand All @@ -86,22 +86,55 @@ async function main() {
const tableAliases: {[k: string]: string} = {};
const objectTypes: { [k: string]: ObjectType } = {};
const tables = await queryAll(con, "SHOW ALL TABLES");

// Get table comments
const tableComments = await queryAll(con, `
SELECT table_name, comment
FROM duckdb_tables()
WHERE schema_name = 'main'
`);

// Create a map of table comments for easier lookup
const tableCommentMap = new Map(
tableComments.map(row => [row.table_name, row.comment || "No description available"])
);
// Get all column comments upfront
const columnComments = await queryAll(con, `
SELECT table_name, column_name, comment
FROM duckdb_columns()
WHERE schema_name = 'main'
`);
// Create a nested map for column comments: table_name -> column_name -> comment
const columnCommentMap = new Map();
for (const row of columnComments) {
if (!columnCommentMap.has(row.table_name)) {
columnCommentMap.set(row.table_name, new Map());
}
columnCommentMap.get(row.table_name).set(row.column_name, row.comment || "No description available");
}

for (let table of tables){
const tableName = `${table.database}_${table.schema}_${table.name}`;
const tableName = table.name;
const aliasName = `${table.database}.${table.schema}.${table.name}`;
tableNames.push(tableName);
tableAliases[tableName] = aliasName;
if (!objectTypes[tableName]){
objectTypes[tableName] = {
fields: {}
fields: {},
description: tableCommentMap.get(tableName) || "No Description Available"
};
}
for (let i = 0; i < table.column_names.length; i++){
objectTypes[tableName]['fields'][table.column_names[i]] = {
const columnName = table.column_names[i];
objectTypes[tableName]["fields"][columnName] = {
type: {
type: "named",
name: determineType(table.column_types[i])
}
type: "nullable",
underlying_type: {
type: "named",
name: determineType(table.column_types[i]),
},
},
description: columnCommentMap.get(tableName)?.get(columnName) || "No description available"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "duckdb-sdk",
"version": "0.1.0",
"version": "0.1.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
199 changes: 199 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,197 @@ export const CAPABILITIES_RESPONSE: Capabilities = {
};
export const MAX_32_INT: number = 2147483647;
export const SCALAR_TYPES: { [key: string]: ScalarType } = {
BigInt: {
representation: {
type: "biginteger"
},
aggregate_functions: {
// sum: {
// result_type: {
// type: "named",
// name: "Int"
// }
// }
},
comparison_operators: {
_eq: {
type: "equal",
},
_gt: {
type: "custom",
argument_type: {
type: "named",
name: "BigInt",
},
},
_lt: {
type: "custom",
argument_type: {
type: "named",
name: "BigInt",
},
},
_gte: {
type: "custom",
argument_type: {
type: "named",
name: "BigInt",
},
},
_lte: {
type: "custom",
argument_type: {
type: "named",
name: "BigInt",
},
},
_neq: {
type: "custom",
argument_type: {
type: "named",
name: "BigInt",
},
},
},
},
UBigInt: {
representation: {
type: "biginteger"
},
aggregate_functions: {},
comparison_operators: {
_eq: { type: "equal" },
_gt: { type: "custom", argument_type: { type: "named", name: "UBigInt" }},
_lt: { type: "custom", argument_type: { type: "named", name: "UBigInt" }},
_gte: { type: "custom", argument_type: { type: "named", name: "UBigInt" }},
_lte: { type: "custom", argument_type: { type: "named", name: "UBigInt" }},
_neq: { type: "custom", argument_type: { type: "named", name: "UBigInt" }},
},
},
HugeInt: {
representation: {
type: "biginteger"
},
aggregate_functions: {},
comparison_operators: {
_eq: { type: "equal" },
_gt: { type: "custom", argument_type: { type: "named", name: "HugeInt" }},
_lt: { type: "custom", argument_type: { type: "named", name: "HugeInt" }},
_gte: { type: "custom", argument_type: { type: "named", name: "HugeInt" }},
_lte: { type: "custom", argument_type: { type: "named", name: "HugeInt" }},
_neq: { type: "custom", argument_type: { type: "named", name: "HugeInt" }},
},
},
UHugeInt: {
representation: {
type: "biginteger"
},
aggregate_functions: {},
comparison_operators: {
_eq: { type: "equal" },
_gt: { type: "custom", argument_type: { type: "named", name: "UHugeInt" }},
_lt: { type: "custom", argument_type: { type: "named", name: "UHugeInt" }},
_gte: { type: "custom", argument_type: { type: "named", name: "UHugeInt" }},
_lte: { type: "custom", argument_type: { type: "named", name: "UHugeInt" }},
_neq: { type: "custom", argument_type: { type: "named", name: "UHugeInt" }},
},
},
Timestamp: {
representation: {
type: "timestamp"
},
aggregate_functions: {},
comparison_operators: {
_eq: {
type: "equal",
},
_gt: {
type: "custom",
argument_type: {
type: "named",
name: "Timestamp",
},
},
_lt: {
type: "custom",
argument_type: {
type: "named",
name: "Timestamp",
},
},
_gte: {
type: "custom",
argument_type: {
type: "named",
name: "Timestamp",
},
},
_lte: {
type: "custom",
argument_type: {
type: "named",
name: "Timestamp",
},
},
_neq: {
type: "custom",
argument_type: {
type: "named",
name: "Timestamp",
},
},
},
},
TimestampTz: {
representation: {
type: "timestamptz"
},
aggregate_functions: {},
comparison_operators: {
_eq: {
type: "equal",
},
_gt: {
type: "custom",
argument_type: {
type: "named",
name: "TimestampTz",
},
},
_lt: {
type: "custom",
argument_type: {
type: "named",
name: "TimestampTz",
},
},
_gte: {
type: "custom",
argument_type: {
type: "named",
name: "TimestampTz",
},
},
_lte: {
type: "custom",
argument_type: {
type: "named",
name: "TimestampTz",
},
},
_neq: {
type: "custom",
argument_type: {
type: "named",
name: "TimestampTz",
},
},
},
},
Int: {
representation: {
type: "int64"
},
aggregate_functions: {
// sum: {
// result_type: {
Expand Down Expand Up @@ -67,6 +257,9 @@ export const SCALAR_TYPES: { [key: string]: ScalarType } = {
}
},
Float: {
representation: {
type: "float64"
},
aggregate_functions: {
// sum: {
// result_type: {
Expand Down Expand Up @@ -117,6 +310,9 @@ export const SCALAR_TYPES: { [key: string]: ScalarType } = {
}
},
String: {
representation: {
type: "string"
},
aggregate_functions: {},
comparison_operators: {
_eq: {
Expand Down Expand Up @@ -174,6 +370,9 @@ export const SCALAR_TYPES: { [key: string]: ScalarType } = {
}
},
Boolean: {
representation: {
type: "boolean"
},
aggregate_functions: {},
comparison_operators: {
_eq: {
Expand Down
Loading

0 comments on commit df5bb91

Please sign in to comment.