Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to fix timestamp and bigint/hugeint filters #10

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions ndc-duckduckapi/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,82 @@ export const SCALAR_TYPES: { [key: string]: ScalarType } = {
type: "custom",
argument_type: {
type: "named",
name: "Int",
name: "BigInt",
},
},
_lt: {
type: "custom",
argument_type: {
type: "named",
name: "Int",
name: "BigInt",
},
},
_gte: {
type: "custom",
argument_type: {
type: "named",
name: "Int",
name: "BigInt",
},
},
_lte: {
type: "custom",
argument_type: {
type: "named",
name: "Int",
name: "BigInt",
},
},
_neq: {
type: "custom",
argument_type: {
type: "named",
name: "Int",
name: "BigInt",
},
},
},
}, Int: {
},
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" }},
},
},
Int: {
aggregate_functions: {
// sum: {
// result_type: {
Expand Down
6 changes: 3 additions & 3 deletions ndc-duckduckapi/src/generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const determineType = (t: string): string => {
case "DOUBLE":
return "Float";
case "HUGEINT":
return "BigInt";
return "HugeInt";
case "INTEGER":
return "Int";
case "INTERVAL":
Expand All @@ -50,9 +50,9 @@ const determineType = (t: string): string => {
case "TINYINT":
return "Int";
case "UBIGINT":
return "BigInt";
return "UBigInt";
case "UHUGEINT":
return "BigInt";
return "UHugeInt";
case "UINTEGER":
return "Int";
case "USMALLINT":
Expand Down
103 changes: 82 additions & 21 deletions ndc-duckduckapi/src/handlers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ function wrap_rows(s: string): string {
`;
}

function isTimestampType(field_def: any): boolean {
if (!field_def) return false;

function checkType(type: any): boolean {
if (type.type === "nullable") {
return checkType(type.underlying_type);
}
return type.type === "named" && type.name === "Timestamp";
}

return checkType(field_def.type);
}

function getIntegerType(field_def: any): string | null {
if (!field_def) return null;

function checkType(type: any): string | null {
if (type.type === "nullable") {
return checkType(type.underlying_type);
}
if (type.type === "named") {
switch (type.name) {
case "BigInt": return "BIGINT";
case "UBigInt": return "UBIGINT";
case "HugeInt": return "HUGEINT";
case "UHugeInt": return "UHUGEINT";
default: return null;
}
}
return null;
}

return checkType(field_def.type);
}

function getRhsExpression(type: string | null): string {
if (!type) return "?";
return `CAST(? AS ${type})`;
}

function build_where(
expression: Expression,
collection_relationships: {
Expand All @@ -95,7 +135,9 @@ function build_where(
args: any[],
variables: QueryVariables,
prefix: string,
collection_aliases: { [k: string]: string }
collection_aliases: { [k: string]: string },
config: Configuration,
query_request: QueryRequest
): string {
let sql = "";
switch (expression.type) {
Expand All @@ -111,6 +153,13 @@ function build_where(
}
break;
case "binary_comparison_operator":
const object_type = config.duckdbConfig?.object_types[query_request.collection];
const field_def = object_type?.fields[expression.column.name];
const isTimestamp = isTimestampType(field_def);
const integerType = getIntegerType(field_def);
const type = isTimestamp ? "TIMESTAMP" : integerType;
const lhs = escape_double(expression.column.name);
const rhs = getRhsExpression(type);
switch (expression.value.type) {
case "scalar":
args.push(expression.value.value);
Expand All @@ -127,33 +176,33 @@ function build_where(
}
switch (expression.operator) {
case "_eq":
sql = `${expression.column.name} = ?`;
break;
case "_like":
args[args.length - 1] = `%${args[args.length - 1]}%`;
sql = `${expression.column.name} LIKE ?`;
break;
case "_glob":
sql = `${expression.column.name} GLOB ?`;
sql = `${lhs} = ${rhs}`;
break;
case "_neq":
sql = `${expression.column.name} != ?`;
sql = `${lhs} != ${rhs}`;
break;
case "_gt":
sql = `${expression.column.name} > ?`;
sql = `${lhs} > ${rhs}`;
break;
case "_lt":
sql = `${expression.column.name} < ?`;
sql = `${lhs} < ${rhs}`;
break;
case "_gte":
sql = `${expression.column.name} >= ?`;
sql = `${lhs} >= ${rhs}`;
break;
case "_lte":
sql = `${expression.column.name} <= ?`;
sql = `${lhs} <= ${rhs}`;
break;
case "_like":
args[args.length - 1] = `%${args[args.length - 1]}%`;
sql = `${lhs} LIKE ?`;
break;
case "_glob":
sql = `${lhs} GLOB ?`;
break;
default:
throw new Forbidden(
"Binary Comparison Custom Operator not implemented",
`Binary Comparison Custom Operator ${expression.operator} not implemented`,
{}
);
}
Expand All @@ -170,7 +219,9 @@ function build_where(
args,
variables,
prefix,
collection_aliases
collection_aliases,
config,
query_request
);
clauses.push(res);
}
Expand All @@ -189,7 +240,9 @@ function build_where(
args,
variables,
prefix,
collection_aliases
collection_aliases,
config,
query_request
);
clauses.push(res);
}
Expand All @@ -203,7 +256,9 @@ function build_where(
args,
variables,
prefix,
collection_aliases
collection_aliases,
config,
query_request
);
sql = `NOT (${not_result})`;
break;
Expand All @@ -228,7 +283,9 @@ function build_where(
args,
variables,
prefix,
collection_aliases
collection_aliases,
config,
query_request
)
: "1 = 1"
}
Expand Down Expand Up @@ -366,7 +423,9 @@ function build_query(
agg_args,
variables,
collection_alias,
config.duckdbConfig.collection_aliases
config.duckdbConfig.collection_aliases,
config,
query_request
)})`
);
}
Expand Down Expand Up @@ -455,7 +514,9 @@ function build_query(
args,
variables,
collection_alias,
config.duckdbConfig.collection_aliases
config.duckdbConfig.collection_aliases,
config,
query_request
)})`
);
}
Expand Down