Repository navigation
Expand file tree
/
Copy pathdialect.cpp
More file actions
240 lines (226 loc) · 9.81 KB
/
Copy pathdialect.cpp
File metadata and controls
240 lines (226 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <teaql/provider_sqlite/dialect.hpp>
#include <algorithm>
#include <charconv>
namespace teaql {
namespace {
[[noreturn]] void invalid(Memory memory, std::string_view message) {
throw SqlCompileError(std::allocator_arg,
Allocator(memory),
SqlCompileError::Code::InvalidFunctionArguments,
message);
}
bool decimal_column(const EntityDescriptor& entity, const Expr& expression) {
const auto* property =
expression.kind == Expr::Kind::Column ? entity.property_by_name(expression.name) : nullptr;
return property && property->data_type == DataType::Decimal;
}
bool decimal_domain(const EntityDescriptor& entity, const Expr& expression) {
if (decimal_column(entity, expression)) {
return true;
}
if (expression.kind == Expr::Kind::Value) {
if (std::holds_alternative<Decimal>(expression.literal.data)) {
return true;
}
if (const auto* list = expression.literal.as_list()) {
return std::ranges::any_of(*list, [](const Value& value) {
return std::holds_alternative<Decimal>(value.data);
});
}
}
if (expression.kind == Expr::Kind::Function &&
(expression.function_kind == ExprFunction::Identity ||
expression.function_kind == ExprFunction::Sum ||
expression.function_kind == ExprFunction::Min ||
expression.function_kind == ExprFunction::Max)) {
return std::ranges::any_of(expression.args, [&](const Expr& argument) {
return decimal_domain(entity, argument);
});
}
return false;
}
bool exact_operand(const EntityDescriptor& entity, const Expr& expression) {
if (expression.kind == Expr::Kind::Value) {
const auto& value = expression.literal;
if (std::holds_alternative<Decimal>(value.data) ||
std::holds_alternative<std::int64_t>(value.data) ||
std::holds_alternative<std::uint64_t>(value.data) || value.is_null()) {
return true;
}
const auto text = value.try_text();
return text && Decimal::parse(*text).has_value();
}
if (expression.kind == Expr::Kind::Column) {
const auto* property = entity.property_by_name(expression.name);
return property &&
(property->data_type == DataType::Decimal || property->data_type == DataType::I64 ||
property->data_type == DataType::U64);
}
if (expression.kind == Expr::Kind::Function) {
if (expression.function_kind == ExprFunction::Count ||
expression.function_kind == ExprFunction::CountDistinct) {
return true;
}
if (expression.function_kind == ExprFunction::Sum ||
expression.function_kind == ExprFunction::Min ||
expression.function_kind == ExprFunction::Max ||
expression.function_kind == ExprFunction::Identity) {
return std::ranges::all_of(expression.args, [&](const Expr& argument) {
return exact_operand(entity, argument);
});
}
}
return false;
}
bool references_decimal(const EntityDescriptor& entity, const Expr& expression) {
if (expression.kind == Expr::Kind::Column) {
return decimal_column(entity, expression);
}
if (expression.kind == Expr::Kind::Value) {
return std::holds_alternative<Decimal>(expression.literal.data);
}
if (expression.kind == Expr::Kind::LikePattern) {
return false;
}
return std::ranges::any_of(expression.args, [&](const Expr& argument) {
return references_decimal(entity, argument);
});
}
String exact_sql(Memory memory, std::string_view sql) {
String result("(CAST(", memory);
result.append(sql).append(" AS TEXT) COLLATE TEAQL_DECIMAL)");
return result;
}
void append_number(String& output, std::uint32_t value) {
char buffer[16];
const auto end = std::to_chars(buffer, buffer + sizeof(buffer), value).ptr;
output.append(buffer, end);
}
}
String SqliteDialect::comparison_operand_sql(Memory memory,
const EntityDescriptor& entity,
const Expr& operand,
std::span<const Expr* const> peers,
String sql) const {
if (!decimal_domain(entity, operand) && !std::ranges::any_of(peers, [&](const Expr* peer) {
return decimal_domain(entity, *peer);
})) {
return String(std::move(sql), memory);
}
if (!exact_operand(entity, operand)) {
invalid(memory, "SQLite exact Decimal comparison requires an exact numeric operand");
}
return exact_sql(memory, sql);
}
String SqliteDialect::order_by_sql(Memory memory,
const EntityDescriptor& entity,
const OrderBy& order,
SqlBindings& bindings) const {
auto sql = resolve_order_field(memory, entity, order, bindings);
std::optional<Expr> fallback;
if (!order.expr) {
fallback.emplace(Expr::column(memory, order.field));
}
sql = comparison_operand_sql(
memory, entity, order.expr ? *order.expr : *fallback, {}, std::move(sql));
sql.append(order.direction == SortDirection::Asc ? " ASC" : " DESC");
return sql;
}
String SqliteDialect::aggregate_call_sql_for(Memory memory,
const EntityDescriptor& entity,
const Aggregate& aggregate,
std::string_view field) const {
validate_aggregate_field_type(memory, entity, aggregate);
const auto* property = entity.property_by_name(aggregate.field);
if (aggregate.function != AggregateFunction::Count &&
aggregate.function != AggregateFunction::CountDistinct && property &&
property->data_type == DataType::Decimal) {
if (aggregate.function == AggregateFunction::Sum) {
String result("teaql_decimal_sum(", memory);
result.append(field).append(")");
return result;
}
if (aggregate.function != AggregateFunction::Min &&
aggregate.function != AggregateFunction::Max) {
invalid(memory, "SQLite exact Decimal aggregate is not supported for this function");
}
}
return aggregate_call_sql(memory, aggregate.function, field);
}
String SqliteDialect::compile_statistical_operand(Memory memory,
const EntityDescriptor& entity,
const Expr& operand,
DataType type,
SqlBindings& bindings) const {
auto sql = compile_expr(memory, entity, operand, bindings);
return type == DataType::Decimal ? exact_sql(memory, sql) : std::move(sql);
}
String SqliteDialect::compile_single_arg_function(Memory memory,
const EntityDescriptor& entity,
std::string_view function,
std::span<const Expr> args,
SqlBindings& bindings) const {
if (args.size() != 1) {
String message(function, memory);
message.append(" expects exactly one argument");
invalid(memory, message);
}
const bool column = decimal_column(entity, args.front());
if (function != "COUNT" && !column && references_decimal(entity, args.front())) {
invalid(memory,
"SQLite exact Decimal expression aggregate requires a direct Decimal column");
}
if (column && function != "COUNT" && function != "SUM" && function != "MIN" &&
function != "MAX") {
invalid(memory,
"SQLite exact Decimal expression aggregate is not supported for this function");
}
auto argument = compile_expr(memory, entity, args.front(), bindings);
String result(column && function == "SUM" ? "teaql_decimal_sum" : function, memory);
result.append("(").append(argument).append(")");
return result;
}
String
SqliteDialect::schema_type_sql(Memory r, DataType type, const PropertyDescriptor& property) const {
switch (type) {
case DataType::Decimal: {
String result("DECIMAL_TEXT", r);
if (property.numeric_precision && property.numeric_scale) {
result += '(';
append_number(result, *property.numeric_precision);
result += ',';
append_number(result, *property.numeric_scale);
result += ')';
}
const auto column = quote_ident(r, property.column_name);
result.append(" COLLATE TEAQL_DECIMAL CHECK (")
.append(column)
.append(" IS NULL OR teaql_decimal_valid(")
.append(column)
.append("))");
return result;
}
case DataType::Json:
return String("JSON", r);
case DataType::Date:
return String("DATE", r);
case DataType::Timestamp:
return String("TIMESTAMP", r);
default:
return SqlDialect::schema_type_sql(r, type, property);
}
}
String SqliteDialect::compile_add_column(Memory r,
const EntityDescriptor& entity,
const PropertyDescriptor& property) const {
// SQLite's source override intentionally supplies no generic fallback
// DEFAULT. Whether existing rows permit adding this column is a schema
// provider concern, not a compiler rewrite.
auto definition = column_definition_sql(r, property);
String sql("ALTER TABLE ", r);
sql += quote_ident(r, entity.table_name);
sql += " ADD COLUMN ";
sql += definition;
return sql;
}
}