* Convert key according to map key type. * For some types, like Enum, Date, DateTime which can be map key type, they need to convert to correct Field. Otherwise, query will be wrong. * For example, there has one map column named a and one row data * a Map(Date, UInt64) KV : {'2022-05-31': 1}) * Here we query for key '2022-05-31', but in fact, the Date type is essentially
| 102 | * After conversion, here we query for key 19143 which can be handled correctly. |
| 103 | */ |
| 104 | Field getMapKeyField(ASTFunction & node, QueryNormalizer::Data & data, const DataTypePtr & key_type) |
| 105 | { |
| 106 | ASTLiteral * key_lit = node.arguments->children[1]->as<ASTLiteral>(); // Constant Literal |
| 107 | ASTFunction * key_func = node.arguments->children[1]->as<ASTFunction>(); // for constant foldable functions' case |
| 108 | |
| 109 | Field res = Null(); |
| 110 | if (key_lit) // key is literal |
| 111 | { |
| 112 | Field key_field = tryConvertToMapKeyField(key_type, key_lit->getColumnName()); |
| 113 | /// return origin key field if failed to convert to map key field, so the ast can be rewritten multi-times |
| 114 | return key_field.isNull() ? key_lit->value : key_field; |
| 115 | } |
| 116 | else if (key_func) |
| 117 | { |
| 118 | /// check whether key_func's inputs are all constant literal, if yes, invoke constant folding logic. |
| 119 | /// This handling is specially added for Date key which is toDate('YYYY-MM_DD') in SQL |
| 120 | bool all_input_const = true; |
| 121 | for (auto & c : key_func->arguments->children) |
| 122 | { |
| 123 | if (!c->as<ASTLiteral>()) |
| 124 | { |
| 125 | all_input_const = false; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (all_input_const) |
| 131 | { |
| 132 | std::pair<Field, DataTypePtr> value_raw = evaluateConstantExpression(node.arguments->children[1], data.context); |
| 133 | res = value_raw.first; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return res; |
| 138 | } |
| 139 | |
| 140 | void rewriteMapElement(ASTPtr & ast, const String & map_name, const String & key_name) |
| 141 | { |
no test coverage detected