duckdb 0.3.5-dev525.0 → 0.3.5-dev534.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/duckdb.cpp CHANGED
@@ -463,6 +463,42 @@ private:
463
463
  };
464
464
  } // namespace duckdb
465
465
 
466
+ //===----------------------------------------------------------------------===//
467
+ // DuckDB
468
+ //
469
+ // duckdb/catalog/catalog_entry/type_catalog_entry.hpp
470
+ //
471
+ //
472
+ //===----------------------------------------------------------------------===//
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+ namespace duckdb {
481
+ class Serializer;
482
+ class Deserializer;
483
+
484
+ //! A type catalog entry
485
+ class TypeCatalogEntry : public StandardEntry {
486
+ public:
487
+ //! Create a TypeCatalogEntry and initialize storage for it
488
+ TypeCatalogEntry(Catalog *catalog, SchemaCatalogEntry *schema, CreateTypeInfo *info);
489
+
490
+ LogicalType user_type;
491
+
492
+ public:
493
+ //! Serialize the meta information of the TypeCatalogEntry a serializer
494
+ virtual void Serialize(Serializer &serializer);
495
+ //! Deserializes to a TypeCatalogEntry
496
+ static unique_ptr<CreateTypeInfo> Deserialize(Deserializer &source);
497
+
498
+ string ToSQL() override;
499
+ };
500
+ } // namespace duckdb
501
+
466
502
 
467
503
 
468
504
  //===----------------------------------------------------------------------===//
@@ -539,6 +575,37 @@ public:
539
575
 
540
576
 
541
577
 
578
+ //===----------------------------------------------------------------------===//
579
+ // DuckDB
580
+ //
581
+ // duckdb/catalog/default/default_types.hpp
582
+ //
583
+ //
584
+ //===----------------------------------------------------------------------===//
585
+
586
+
587
+
588
+
589
+
590
+
591
+ namespace duckdb {
592
+ class SchemaCatalogEntry;
593
+
594
+ class DefaultTypeGenerator : public DefaultGenerator {
595
+ public:
596
+ DefaultTypeGenerator(Catalog &catalog, SchemaCatalogEntry *schema);
597
+
598
+ SchemaCatalogEntry *schema;
599
+
600
+ public:
601
+ DUCKDB_API static LogicalTypeId GetDefaultType(const string &name);
602
+
603
+ unique_ptr<CatalogEntry> CreateDefaultEntry(ClientContext &context, const string &entry_name) override;
604
+ vector<string> GetDefaultEntries() override;
605
+ };
606
+
607
+ } // namespace duckdb
608
+
542
609
 
543
610
  namespace duckdb {
544
611
 
@@ -883,6 +950,19 @@ CollateCatalogEntry *Catalog::GetEntry(ClientContext &context, const string &sch
883
950
  error_context);
884
951
  }
885
952
 
953
+ template <>
954
+ TypeCatalogEntry *Catalog::GetEntry(ClientContext &context, const string &schema_name, const string &name,
955
+ bool if_exists, QueryErrorContext error_context) {
956
+ return (TypeCatalogEntry *)GetEntry(context, CatalogType::TYPE_ENTRY, schema_name, name, if_exists, error_context);
957
+ }
958
+
959
+ LogicalType Catalog::GetType(ClientContext &context, const string &schema, const string &name) {
960
+ auto user_type_catalog = GetEntry<TypeCatalogEntry>(context, schema, name);
961
+ auto result_type = user_type_catalog->user_type;
962
+ EnumType::SetCatalog(result_type, user_type_catalog);
963
+ return result_type;
964
+ }
965
+
886
966
  void Catalog::Alter(ClientContext &context, AlterInfo *info) {
887
967
  ModifyCatalog();
888
968
  auto lookup = LookupEntry(context, info->GetCatalogType(), info->schema, info->name);
@@ -1441,41 +1521,6 @@ unique_ptr<CreateMacroInfo> TableMacroCatalogEntry::Deserialize(Deserializer &ma
1441
1521
 
1442
1522
 
1443
1523
 
1444
- //===----------------------------------------------------------------------===//
1445
- // DuckDB
1446
- //
1447
- // duckdb/catalog/catalog_entry/type_catalog_entry.hpp
1448
- //
1449
- //
1450
- //===----------------------------------------------------------------------===//
1451
-
1452
-
1453
-
1454
-
1455
-
1456
-
1457
-
1458
- namespace duckdb {
1459
- class Serializer;
1460
- class Deserializer;
1461
-
1462
- //! A type catalog entry
1463
- class TypeCatalogEntry : public StandardEntry {
1464
- public:
1465
- //! Create a TypeCatalogEntry and initialize storage for it
1466
- TypeCatalogEntry(Catalog *catalog, SchemaCatalogEntry *schema, CreateTypeInfo *info);
1467
-
1468
- LogicalType user_type;
1469
-
1470
- public:
1471
- //! Serialize the meta information of the TypeCatalogEntry a serializer
1472
- virtual void Serialize(Serializer &serializer);
1473
- //! Deserializes to a TypeCatalogEntry
1474
- static unique_ptr<CreateTypeInfo> Deserialize(Deserializer &source);
1475
-
1476
- string ToSQL() override;
1477
- };
1478
- } // namespace duckdb
1479
1524
 
1480
1525
 
1481
1526
  //===----------------------------------------------------------------------===//
@@ -1648,6 +1693,7 @@ public:
1648
1693
 
1649
1694
 
1650
1695
 
1696
+
1651
1697
  #include <algorithm>
1652
1698
  #include <sstream>
1653
1699
 
@@ -1682,7 +1728,7 @@ SchemaCatalogEntry::SchemaCatalogEntry(Catalog *catalog, string name_p, bool int
1682
1728
  tables(*catalog, make_unique<DefaultViewGenerator>(*catalog, this)), indexes(*catalog), table_functions(*catalog),
1683
1729
  copy_functions(*catalog), pragma_functions(*catalog),
1684
1730
  functions(*catalog, make_unique<DefaultFunctionGenerator>(*catalog, this)), sequences(*catalog),
1685
- collations(*catalog), types(*catalog) {
1731
+ collations(*catalog), types(*catalog, make_unique<DefaultTypeGenerator>(*catalog, this)) {
1686
1732
  this->internal = internal;
1687
1733
  }
1688
1734
 
@@ -1735,8 +1781,8 @@ CatalogEntry *SchemaCatalogEntry::CreateSequence(ClientContext &context, CreateS
1735
1781
  }
1736
1782
 
1737
1783
  CatalogEntry *SchemaCatalogEntry::CreateType(ClientContext &context, CreateTypeInfo *info) {
1738
- auto sequence = make_unique<TypeCatalogEntry>(catalog, this, info);
1739
- return AddEntry(context, move(sequence), info->on_conflict);
1784
+ auto type_entry = make_unique<TypeCatalogEntry>(catalog, this, info);
1785
+ return AddEntry(context, move(type_entry), info->on_conflict);
1740
1786
  }
1741
1787
 
1742
1788
  CatalogEntry *SchemaCatalogEntry::CreateTable(ClientContext &context, BoundCreateTableInfo *info) {
@@ -3568,13 +3614,8 @@ unique_ptr<CatalogEntry> TableCatalogEntry::SetDefault(ClientContext &context, S
3568
3614
 
3569
3615
  unique_ptr<CatalogEntry> TableCatalogEntry::ChangeColumnType(ClientContext &context, ChangeColumnTypeInfo &info) {
3570
3616
  if (info.target_type.id() == LogicalTypeId::USER) {
3571
- auto &user_type_name = UserType::GetTypeName(info.target_type);
3572
- auto user_type_catalog = (TypeCatalogEntry *)context.db->GetCatalog().GetEntry(
3573
- context, CatalogType::TYPE_ENTRY, schema->name, user_type_name, true);
3574
- if (!user_type_catalog) {
3575
- throw NotImplementedException("DataType %s not supported yet...\n", user_type_name);
3576
- }
3577
- info.target_type = user_type_catalog->user_type;
3617
+ auto &catalog = Catalog::GetCatalog(context);
3618
+ info.target_type = catalog.GetType(context, schema->name, UserType::GetTypeName(info.target_type));
3578
3619
  }
3579
3620
  auto create_info = make_unique<CreateTableInfo>(schema->name, name);
3580
3621
  idx_t change_idx = GetColumnIndex(info.column_name);
@@ -3892,9 +3933,12 @@ namespace duckdb {
3892
3933
 
3893
3934
  TypeCatalogEntry::TypeCatalogEntry(Catalog *catalog, SchemaCatalogEntry *schema, CreateTypeInfo *info)
3894
3935
  : StandardEntry(CatalogType::TYPE_ENTRY, schema, catalog, info->name), user_type(info->type) {
3936
+ this->temporary = info->temporary;
3937
+ this->internal = info->internal;
3895
3938
  }
3896
3939
 
3897
3940
  void TypeCatalogEntry::Serialize(Serializer &serializer) {
3941
+ D_ASSERT(!internal);
3898
3942
  FieldWriter writer(serializer);
3899
3943
  writer.WriteString(schema->name);
3900
3944
  writer.WriteString(name);
@@ -4279,7 +4323,7 @@ bool CatalogSet::CreateEntry(ClientContext &context, const string &name, unique_
4279
4323
  // lock the catalog for writing
4280
4324
  lock_guard<mutex> write_lock(catalog.write_lock);
4281
4325
  // lock this catalog set to disallow reading
4282
- lock_guard<mutex> read_lock(catalog_lock);
4326
+ unique_lock<mutex> read_lock(catalog_lock);
4283
4327
 
4284
4328
  // first check if the entry exists in the unordered set
4285
4329
  idx_t entry_index;
@@ -4287,6 +4331,12 @@ bool CatalogSet::CreateEntry(ClientContext &context, const string &name, unique_
4287
4331
  if (mapping_value == nullptr || mapping_value->deleted) {
4288
4332
  // if it does not: entry has never been created
4289
4333
 
4334
+ // check if there is a default entry
4335
+ auto entry = CreateDefaultEntry(context, name, read_lock);
4336
+ if (entry) {
4337
+ return false;
4338
+ }
4339
+
4290
4340
  // first create a dummy deleted entry for this entry
4291
4341
  // so transactions started before the commit of this transaction don't
4292
4342
  // see it yet
@@ -4527,6 +4577,7 @@ MappingValue *CatalogSet::GetMapping(ClientContext &context, const string &name,
4527
4577
  if (entry != mapping.end()) {
4528
4578
  mapping_value = entry->second.get();
4529
4579
  } else {
4580
+
4530
4581
  return nullptr;
4531
4582
  }
4532
4583
  if (get_latest) {
@@ -4604,7 +4655,8 @@ CatalogEntry *CatalogSet::GetCommittedEntry(CatalogEntry *current) {
4604
4655
  }
4605
4656
 
4606
4657
  pair<string, idx_t> CatalogSet::SimilarEntry(ClientContext &context, const string &name) {
4607
- lock_guard<mutex> lock(catalog_lock);
4658
+ unique_lock<mutex> lock(catalog_lock);
4659
+ CreateDefaultEntries(context, lock);
4608
4660
 
4609
4661
  string result;
4610
4662
  idx_t current_score = (idx_t)-1;
@@ -4637,20 +4689,7 @@ CatalogEntry *CatalogSet::CreateEntryInternal(ClientContext &context, unique_ptr
4637
4689
  return catalog_entry;
4638
4690
  }
4639
4691
 
4640
- CatalogEntry *CatalogSet::GetEntry(ClientContext &context, const string &name) {
4641
- unique_lock<mutex> lock(catalog_lock);
4642
- auto mapping_value = GetMapping(context, name);
4643
- if (mapping_value != nullptr && !mapping_value->deleted) {
4644
- // we found an entry for this name
4645
- // check the version numbers
4646
-
4647
- auto catalog_entry = entries[mapping_value->index].get();
4648
- CatalogEntry *current = GetEntryForTransaction(context, catalog_entry);
4649
- if (current->deleted || (current->name != name && !UseTimestamp(context, mapping_value->timestamp))) {
4650
- return nullptr;
4651
- }
4652
- return current;
4653
- }
4692
+ CatalogEntry *CatalogSet::CreateDefaultEntry(ClientContext &context, const string &name, unique_lock<mutex> &lock) {
4654
4693
  // no entry found with this name, check for defaults
4655
4694
  if (!defaults || defaults->created_all_entries) {
4656
4695
  // no defaults either: return null
@@ -4678,6 +4717,23 @@ CatalogEntry *CatalogSet::GetEntry(ClientContext &context, const string &name) {
4678
4717
  return GetEntry(context, name);
4679
4718
  }
4680
4719
 
4720
+ CatalogEntry *CatalogSet::GetEntry(ClientContext &context, const string &name) {
4721
+ unique_lock<mutex> lock(catalog_lock);
4722
+ auto mapping_value = GetMapping(context, name);
4723
+ if (mapping_value != nullptr && !mapping_value->deleted) {
4724
+ // we found an entry for this name
4725
+ // check the version numbers
4726
+
4727
+ auto catalog_entry = entries[mapping_value->index].get();
4728
+ CatalogEntry *current = GetEntryForTransaction(context, catalog_entry);
4729
+ if (current->deleted || (current->name != name && !UseTimestamp(context, mapping_value->timestamp))) {
4730
+ return nullptr;
4731
+ }
4732
+ return current;
4733
+ }
4734
+ return CreateDefaultEntry(context, name, lock);
4735
+ }
4736
+
4681
4737
  void CatalogSet::UpdateTimestamp(CatalogEntry *entry, transaction_t timestamp) {
4682
4738
  entry->timestamp = timestamp;
4683
4739
  mapping[entry->name]->timestamp = timestamp;
@@ -4781,26 +4837,35 @@ void CatalogSet::Undo(CatalogEntry *entry) {
4781
4837
  entry->catalog->ModifyCatalog();
4782
4838
  }
4783
4839
 
4840
+ void CatalogSet::CreateDefaultEntries(ClientContext &context, unique_lock<mutex> &lock) {
4841
+ if (!defaults || defaults->created_all_entries) {
4842
+ return;
4843
+ }
4844
+ // this catalog set has a default set defined:
4845
+ auto default_entries = defaults->GetDefaultEntries();
4846
+ for (auto &default_entry : default_entries) {
4847
+ auto map_entry = mapping.find(default_entry);
4848
+ if (map_entry == mapping.end()) {
4849
+ // we unlock during the CreateEntry, since it might reference other catalog sets...
4850
+ // specifically for views this can happen since the view will be bound
4851
+ lock.unlock();
4852
+ auto entry = defaults->CreateDefaultEntry(context, default_entry);
4853
+ if (!entry) {
4854
+ throw InternalException("Failed to create default entry for %s", default_entry);
4855
+ }
4856
+
4857
+ lock.lock();
4858
+ CreateEntryInternal(context, move(entry));
4859
+ }
4860
+ }
4861
+ defaults->created_all_entries = true;
4862
+ }
4863
+
4784
4864
  void CatalogSet::Scan(ClientContext &context, const std::function<void(CatalogEntry *)> &callback) {
4785
4865
  // lock the catalog set
4786
4866
  unique_lock<mutex> lock(catalog_lock);
4787
- if (defaults && !defaults->created_all_entries) {
4788
- // this catalog set has a default set defined:
4789
- auto default_entries = defaults->GetDefaultEntries();
4790
- for (auto &default_entry : default_entries) {
4791
- auto map_entry = mapping.find(default_entry);
4792
- if (map_entry == mapping.end()) {
4793
- // we unlock during the CreateEntry, since it might reference other catalog sets...
4794
- // specifically for views this can happen since the view will be bound
4795
- lock.unlock();
4796
- auto entry = defaults->CreateDefaultEntry(context, default_entry);
4867
+ CreateDefaultEntries(context, lock);
4797
4868
 
4798
- lock.lock();
4799
- CreateEntryInternal(context, move(entry));
4800
- }
4801
- }
4802
- defaults->created_all_entries = true;
4803
- }
4804
4869
  for (auto &kv : entries) {
4805
4870
  auto entry = kv.second.get();
4806
4871
  entry = GetEntryForTransaction(context, entry);
@@ -4879,7 +4944,7 @@ static DefaultMacro internal_macros[] = {
4879
4944
  {"pg_catalog", "pg_get_viewdef", {"oid", nullptr}, "(select sql from duckdb_views() v where v.view_oid=oid)"},
4880
4945
  {"pg_catalog", "pg_get_constraintdef", {"constraint_oid", "pretty_bool", nullptr}, "(select constraint_text from duckdb_constraints() d_constraint where d_constraint.table_oid=constraint_oid/1000000 and d_constraint.constraint_index=constraint_oid%1000000)"},
4881
4946
  {"pg_catalog", "pg_get_expr", {"pg_node_tree", "relation_oid", nullptr}, "pg_node_tree"},
4882
- {"pg_catalog", "format_pg_type", {"type_name", nullptr}, "case when type_name='FLOAT' then 'real' when type_name='DOUBLE' then 'double precision' when type_name='DECIMAL' then 'numeric' when type_name='VARCHAR' then 'character varying' when type_name='BLOB' then 'bytea' when type_name='TIMESTAMP' then 'timestamp without time zone' when type_name='TIME' then 'time without time zone' else lower(type_name) end"},
4947
+ {"pg_catalog", "format_pg_type", {"type_name", nullptr}, "case when logical_type='FLOAT' then 'real' when logical_type='DOUBLE' then 'double precision' when logical_type='DECIMAL' then 'numeric' when logical_type='VARCHAR' then 'character varying' when logical_type='BLOB' then 'bytea' when logical_type='TIMESTAMP' then 'timestamp without time zone' when logical_type='TIME' then 'time without time zone' else lower(logical_type) end"},
4883
4948
  {"pg_catalog", "format_type", {"type_oid", "typemod", nullptr}, "(select format_pg_type(type_name) from duckdb_types() t where t.type_oid=type_oid) || case when typemod>0 then concat('(', typemod/1000, ',', typemod%1000, ')') else '' end"},
4884
4949
 
4885
4950
  {"pg_catalog", "pg_has_role", {"user", "role", "privilege", nullptr}, "true"}, //boolean //does user have privilege for role
@@ -5075,6 +5140,129 @@ vector<string> DefaultSchemaGenerator::GetDefaultEntries() {
5075
5140
 
5076
5141
 
5077
5142
 
5143
+ namespace duckdb {
5144
+
5145
+ struct DefaultType {
5146
+ const char *name;
5147
+ LogicalTypeId type;
5148
+ };
5149
+
5150
+ static DefaultType internal_types[] = {{"int", LogicalTypeId::INTEGER},
5151
+ {"int4", LogicalTypeId::INTEGER},
5152
+ {"signed", LogicalTypeId::INTEGER},
5153
+ {"integer", LogicalTypeId::INTEGER},
5154
+ {"integral", LogicalTypeId::INTEGER},
5155
+ {"int32", LogicalTypeId::INTEGER},
5156
+ {"varchar", LogicalTypeId::VARCHAR},
5157
+ {"bpchar", LogicalTypeId::VARCHAR},
5158
+ {"text", LogicalTypeId::VARCHAR},
5159
+ {"string", LogicalTypeId::VARCHAR},
5160
+ {"char", LogicalTypeId::VARCHAR},
5161
+ {"nvarchar", LogicalTypeId::VARCHAR},
5162
+ {"bytea", LogicalTypeId::BLOB},
5163
+ {"blob", LogicalTypeId::BLOB},
5164
+ {"varbinary", LogicalTypeId::BLOB},
5165
+ {"binary", LogicalTypeId::BLOB},
5166
+ {"int8", LogicalTypeId::BIGINT},
5167
+ {"bigint", LogicalTypeId::BIGINT},
5168
+ {"int64", LogicalTypeId::BIGINT},
5169
+ {"long", LogicalTypeId::BIGINT},
5170
+ {"oid", LogicalTypeId::BIGINT},
5171
+ {"int2", LogicalTypeId::SMALLINT},
5172
+ {"smallint", LogicalTypeId::SMALLINT},
5173
+ {"short", LogicalTypeId::SMALLINT},
5174
+ {"int16", LogicalTypeId::SMALLINT},
5175
+ {"timestamp", LogicalTypeId::TIMESTAMP},
5176
+ {"datetime", LogicalTypeId::TIMESTAMP},
5177
+ {"timestamp_us", LogicalTypeId::TIMESTAMP},
5178
+ {"timestamp_ms", LogicalTypeId::TIMESTAMP_MS},
5179
+ {"timestamp_ns", LogicalTypeId::TIMESTAMP_NS},
5180
+ {"timestamp_s", LogicalTypeId::TIMESTAMP_SEC},
5181
+ {"bool", LogicalTypeId::BOOLEAN},
5182
+ {"boolean", LogicalTypeId::BOOLEAN},
5183
+ {"logical", LogicalTypeId::BOOLEAN},
5184
+ {"decimal", LogicalTypeId::DECIMAL},
5185
+ {"dec", LogicalTypeId::DECIMAL},
5186
+ {"numeric", LogicalTypeId::DECIMAL},
5187
+ {"real", LogicalTypeId::FLOAT},
5188
+ {"float4", LogicalTypeId::FLOAT},
5189
+ {"float", LogicalTypeId::FLOAT},
5190
+ {"double", LogicalTypeId::DOUBLE},
5191
+ {"float8", LogicalTypeId::DOUBLE},
5192
+ {"tinyint", LogicalTypeId::TINYINT},
5193
+ {"int1", LogicalTypeId::TINYINT},
5194
+ {"date", LogicalTypeId::DATE},
5195
+ {"time", LogicalTypeId::TIME},
5196
+ {"interval", LogicalTypeId::INTERVAL},
5197
+ {"hugeint", LogicalTypeId::HUGEINT},
5198
+ {"int128", LogicalTypeId::HUGEINT},
5199
+ {"uuid", LogicalTypeId::UUID},
5200
+ {"guid", LogicalTypeId::UUID},
5201
+ {"struct", LogicalTypeId::STRUCT},
5202
+ {"row", LogicalTypeId::STRUCT},
5203
+ {"map", LogicalTypeId::MAP},
5204
+ {"utinyint", LogicalTypeId::UTINYINT},
5205
+ {"uint8", LogicalTypeId::UTINYINT},
5206
+ {"usmallint", LogicalTypeId::USMALLINT},
5207
+ {"uint16", LogicalTypeId::USMALLINT},
5208
+ {"uinteger", LogicalTypeId::UINTEGER},
5209
+ {"uint32", LogicalTypeId::UINTEGER},
5210
+ {"ubigint", LogicalTypeId::UBIGINT},
5211
+ {"uint64", LogicalTypeId::UBIGINT},
5212
+ {"timestamptz", LogicalTypeId::TIMESTAMP_TZ},
5213
+ {"timetz", LogicalTypeId::TIME_TZ},
5214
+ {"json", LogicalTypeId::JSON},
5215
+ {"null", LogicalTypeId::SQLNULL},
5216
+ {nullptr, LogicalTypeId::INVALID}};
5217
+
5218
+ LogicalTypeId DefaultTypeGenerator::GetDefaultType(const string &name) {
5219
+ auto lower_str = StringUtil::Lower(name);
5220
+ for (idx_t index = 0; internal_types[index].name != nullptr; index++) {
5221
+ if (internal_types[index].name == lower_str) {
5222
+ return internal_types[index].type;
5223
+ }
5224
+ }
5225
+ return LogicalTypeId::INVALID;
5226
+ }
5227
+
5228
+ DefaultTypeGenerator::DefaultTypeGenerator(Catalog &catalog, SchemaCatalogEntry *schema)
5229
+ : DefaultGenerator(catalog), schema(schema) {
5230
+ }
5231
+
5232
+ unique_ptr<CatalogEntry> DefaultTypeGenerator::CreateDefaultEntry(ClientContext &context, const string &entry_name) {
5233
+ if (schema->name != DEFAULT_SCHEMA) {
5234
+ return nullptr;
5235
+ }
5236
+ auto type_id = GetDefaultType(entry_name);
5237
+ if (type_id == LogicalTypeId::INVALID) {
5238
+ return nullptr;
5239
+ }
5240
+ CreateTypeInfo info;
5241
+ info.name = entry_name;
5242
+ info.type = LogicalType(type_id);
5243
+ info.internal = true;
5244
+ info.temporary = true;
5245
+ return make_unique_base<CatalogEntry, TypeCatalogEntry>(&catalog, schema, &info);
5246
+ }
5247
+
5248
+ vector<string> DefaultTypeGenerator::GetDefaultEntries() {
5249
+ vector<string> result;
5250
+ if (schema->name != DEFAULT_SCHEMA) {
5251
+ return result;
5252
+ }
5253
+ for (idx_t index = 0; internal_types[index].name != nullptr; index++) {
5254
+ result.emplace_back(internal_types[index].name);
5255
+ }
5256
+ return result;
5257
+ }
5258
+
5259
+ } // namespace duckdb
5260
+
5261
+
5262
+
5263
+
5264
+
5265
+
5078
5266
 
5079
5267
 
5080
5268
  namespace duckdb {
@@ -5165,7 +5353,6 @@ vector<string> DefaultViewGenerator::GetDefaultEntries() {
5165
5353
  }
5166
5354
  }
5167
5355
  return result;
5168
-
5169
5356
  }
5170
5357
 
5171
5358
  } // namespace duckdb
@@ -35929,7 +36116,9 @@ private:
35929
36116
  };
35930
36117
 
35931
36118
  // adapted from https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C++
35932
- idx_t StringUtil::LevenshteinDistance(const string &s1, const string &s2) {
36119
+ idx_t StringUtil::LevenshteinDistance(const string &s1_p, const string &s2_p) {
36120
+ auto s1 = StringUtil::Lower(s1_p);
36121
+ auto s2 = StringUtil::Lower(s2_p);
35933
36122
  idx_t len1 = s1.size();
35934
36123
  idx_t len2 = s2.size();
35935
36124
  if (len1 == 0) {
@@ -46503,6 +46692,7 @@ const sel_t ConstantVector::ZERO_VECTOR[STANDARD_VECTOR_SIZE] = {0};
46503
46692
 
46504
46693
 
46505
46694
 
46695
+
46506
46696
  #include <cmath>
46507
46697
 
46508
46698
  namespace duckdb {
@@ -46732,9 +46922,9 @@ string TypeIdToString(PhysicalType type) {
46732
46922
  case PhysicalType::INTERVAL:
46733
46923
  return "INTERVAL";
46734
46924
  case PhysicalType::STRUCT:
46735
- return "STRUCT<?>";
46925
+ return "STRUCT";
46736
46926
  case PhysicalType::LIST:
46737
- return "LIST<?>";
46927
+ return "LIST";
46738
46928
  case PhysicalType::INVALID:
46739
46929
  return "INVALID";
46740
46930
  case PhysicalType::BIT:
@@ -46902,11 +47092,11 @@ string LogicalTypeIdToString(LogicalTypeId id) {
46902
47092
  case LogicalTypeId::VALIDITY:
46903
47093
  return "VALIDITY";
46904
47094
  case LogicalTypeId::STRUCT:
46905
- return "STRUCT<?>";
47095
+ return "STRUCT";
46906
47096
  case LogicalTypeId::LIST:
46907
- return "LIST<?>";
47097
+ return "LIST";
46908
47098
  case LogicalTypeId::MAP:
46909
- return "MAP<?>";
47099
+ return "MAP";
46910
47100
  case LogicalTypeId::HASH:
46911
47101
  return "HASH";
46912
47102
  case LogicalTypeId::POINTER:
@@ -46920,7 +47110,7 @@ string LogicalTypeIdToString(LogicalTypeId id) {
46920
47110
  case LogicalTypeId::ENUM:
46921
47111
  return "ENUM";
46922
47112
  case LogicalTypeId::AGGREGATE_STATE:
46923
- return "AGGREGATE_STATE<?>";
47113
+ return "AGGREGATE_STATE";
46924
47114
  case LogicalTypeId::USER:
46925
47115
  return "USER";
46926
47116
  case LogicalTypeId::JSON:
@@ -46993,74 +47183,13 @@ string LogicalType::ToString() const {
46993
47183
  // LCOV_EXCL_STOP
46994
47184
 
46995
47185
  LogicalTypeId TransformStringToLogicalTypeId(const string &str) {
46996
- auto lower_str = StringUtil::Lower(str);
46997
- // Transform column type
46998
- if (lower_str == "int" || lower_str == "int4" || lower_str == "signed" || lower_str == "integer" ||
46999
- lower_str == "integral" || lower_str == "int32") {
47000
- return LogicalTypeId::INTEGER;
47001
- } else if (lower_str == "varchar" || lower_str == "bpchar" || lower_str == "text" || lower_str == "string" ||
47002
- lower_str == "char" || lower_str == "nvarchar") {
47003
- return LogicalTypeId::VARCHAR;
47004
- } else if (lower_str == "bytea" || lower_str == "blob" || lower_str == "varbinary" || lower_str == "binary") {
47005
- return LogicalTypeId::BLOB;
47006
- } else if (lower_str == "int8" || lower_str == "bigint" || lower_str == "int64" || lower_str == "long" ||
47007
- lower_str == "oid") {
47008
- return LogicalTypeId::BIGINT;
47009
- } else if (lower_str == "int2" || lower_str == "smallint" || lower_str == "short" || lower_str == "int16") {
47010
- return LogicalTypeId::SMALLINT;
47011
- } else if (lower_str == "timestamp" || lower_str == "datetime" || lower_str == "timestamp_us") {
47012
- return LogicalTypeId::TIMESTAMP;
47013
- } else if (lower_str == "timestamp_ms") {
47014
- return LogicalTypeId::TIMESTAMP_MS;
47015
- } else if (lower_str == "timestamp_ns") {
47016
- return LogicalTypeId::TIMESTAMP_NS;
47017
- } else if (lower_str == "timestamp_s") {
47018
- return LogicalTypeId::TIMESTAMP_SEC;
47019
- } else if (lower_str == "bool" || lower_str == "boolean" || lower_str == "logical") {
47020
- return LogicalTypeId::BOOLEAN;
47021
- } else if (lower_str == "decimal" || lower_str == "dec" || lower_str == "numeric") {
47022
- return LogicalTypeId::DECIMAL;
47023
- } else if (lower_str == "real" || lower_str == "float4" || lower_str == "float") {
47024
- return LogicalTypeId::FLOAT;
47025
- } else if (lower_str == "double" || lower_str == "float8") {
47026
- return LogicalTypeId::DOUBLE;
47027
- } else if (lower_str == "tinyint" || lower_str == "int1") {
47028
- return LogicalTypeId::TINYINT;
47029
- } else if (lower_str == "date") {
47030
- return LogicalTypeId::DATE;
47031
- } else if (lower_str == "time") {
47032
- return LogicalTypeId::TIME;
47033
- } else if (lower_str == "interval") {
47034
- return LogicalTypeId::INTERVAL;
47035
- } else if (lower_str == "hugeint" || lower_str == "int128") {
47036
- return LogicalTypeId::HUGEINT;
47037
- } else if (lower_str == "uuid" || lower_str == "guid") {
47038
- return LogicalTypeId::UUID;
47039
- } else if (lower_str == "struct" || lower_str == "row") {
47040
- return LogicalTypeId::STRUCT;
47041
- } else if (lower_str == "map") {
47042
- return LogicalTypeId::MAP;
47043
- } else if (lower_str == "utinyint" || lower_str == "uint8") {
47044
- return LogicalTypeId::UTINYINT;
47045
- } else if (lower_str == "usmallint" || lower_str == "uint16") {
47046
- return LogicalTypeId::USMALLINT;
47047
- } else if (lower_str == "uinteger" || lower_str == "uint32") {
47048
- return LogicalTypeId::UINTEGER;
47049
- } else if (lower_str == "ubigint" || lower_str == "uint64") {
47050
- return LogicalTypeId::UBIGINT;
47051
- } else if (lower_str == "timestamptz") {
47052
- return LogicalTypeId::TIMESTAMP_TZ;
47053
- } else if (lower_str == "timetz") {
47054
- return LogicalTypeId::TIME_TZ;
47055
- } else if (lower_str == "json") {
47056
- return LogicalTypeId::JSON;
47057
- } else if (lower_str == "null") {
47058
- return LogicalTypeId::SQLNULL;
47059
- } else {
47186
+ auto type = DefaultTypeGenerator::GetDefaultType(str);
47187
+ if (type == LogicalTypeId::INVALID) {
47060
47188
  // This is a User Type, at this point we don't know if its one of the User Defined Types or an error
47061
47189
  // It is checked in the binder
47062
- return LogicalTypeId::USER;
47190
+ type = LogicalTypeId::USER;
47063
47191
  }
47192
+ return type;
47064
47193
  }
47065
47194
 
47066
47195
  LogicalType TransformStringToLogicalType(const string &str) {
@@ -69905,6 +70034,9 @@ using std::stringstream;
69905
70034
 
69906
70035
  static void WriteCatalogEntries(stringstream &ss, vector<CatalogEntry *> &entries) {
69907
70036
  for (auto &entry : entries) {
70037
+ if (entry->internal) {
70038
+ continue;
70039
+ }
69908
70040
  ss << entry->ToSQL() << std::endl;
69909
70041
  }
69910
70042
  ss << std::endl;
@@ -107478,14 +107610,16 @@ void DuckDBTablesFun::RegisterFunction(BuiltinFunctions &set) {
107478
107610
 
107479
107611
 
107480
107612
 
107613
+
107481
107614
  namespace duckdb {
107482
107615
 
107483
107616
  struct DuckDBTypesData : public FunctionOperatorData {
107484
107617
  DuckDBTypesData() : offset(0) {
107485
107618
  }
107486
107619
 
107487
- vector<LogicalType> types;
107620
+ vector<TypeCatalogEntry *> entries;
107488
107621
  idx_t offset;
107622
+ unordered_set<int64_t> oids;
107489
107623
  };
107490
107624
 
107491
107625
  static unique_ptr<FunctionData> DuckDBTypesBind(ClientContext &context, TableFunctionBindInput &input,
@@ -107505,6 +107639,9 @@ static unique_ptr<FunctionData> DuckDBTypesBind(ClientContext &context, TableFun
107505
107639
  names.emplace_back("type_size");
107506
107640
  return_types.emplace_back(LogicalType::BIGINT);
107507
107641
 
107642
+ names.emplace_back("logical_type");
107643
+ return_types.emplace_back(LogicalType::VARCHAR);
107644
+
107508
107645
  // NUMERIC, STRING, DATETIME, BOOLEAN, COMPOSITE, USER
107509
107646
  names.emplace_back("type_category");
107510
107647
  return_types.emplace_back(LogicalType::VARCHAR);
@@ -107518,37 +107655,61 @@ static unique_ptr<FunctionData> DuckDBTypesBind(ClientContext &context, TableFun
107518
107655
  unique_ptr<FunctionOperatorData> DuckDBTypesInit(ClientContext &context, const FunctionData *bind_data,
107519
107656
  const vector<column_t> &column_ids, TableFilterCollection *filters) {
107520
107657
  auto result = make_unique<DuckDBTypesData>();
107521
- result->types = LogicalType::AllTypes();
107522
- // FIXME: add user-defined types here (when we have them)
107658
+ auto schemas = Catalog::GetCatalog(context).schemas->GetEntries<SchemaCatalogEntry>(context);
107659
+ for (auto &schema : schemas) {
107660
+ schema->Scan(context, CatalogType::TYPE_ENTRY,
107661
+ [&](CatalogEntry *entry) { result->entries.push_back((TypeCatalogEntry *)entry); });
107662
+ };
107663
+
107664
+ // check the temp schema as well
107665
+ ClientData::Get(context).temporary_objects->Scan(context, CatalogType::TYPE_ENTRY, [&](CatalogEntry *entry) {
107666
+ result->entries.push_back((TypeCatalogEntry *)entry);
107667
+ });
107523
107668
  return move(result);
107524
107669
  }
107525
107670
 
107526
107671
  void DuckDBTypesFunction(ClientContext &context, const FunctionData *bind_data, FunctionOperatorData *operator_state,
107527
107672
  DataChunk &output) {
107528
107673
  auto &data = (DuckDBTypesData &)*operator_state;
107529
- if (data.offset >= data.types.size()) {
107674
+ if (data.offset >= data.entries.size()) {
107530
107675
  // finished returning values
107531
107676
  return;
107532
107677
  }
107533
107678
  // start returning values
107534
107679
  // either fill up the chunk or return all the remaining columns
107535
107680
  idx_t count = 0;
107536
- while (data.offset < data.types.size() && count < STANDARD_VECTOR_SIZE) {
107537
- auto &type = data.types[data.offset++];
107681
+ while (data.offset < data.entries.size() && count < STANDARD_VECTOR_SIZE) {
107682
+ auto &type_entry = data.entries[data.offset++];
107683
+ auto &type = type_entry->user_type;
107538
107684
 
107539
107685
  // return values:
107540
- // schema_name, VARCHAR
107541
- output.SetValue(0, count, Value());
107542
- // schema_oid, BIGINT
107543
- output.SetValue(1, count, Value());
107686
+ // schema_name, LogicalType::VARCHAR
107687
+ output.SetValue(0, count, Value(type_entry->schema->name));
107688
+ // schema_oid, LogicalType::BIGINT
107689
+ output.SetValue(1, count, Value::BIGINT(type_entry->schema->oid));
107544
107690
  // type_oid, BIGINT
107545
- output.SetValue(2, count, Value::BIGINT(int(type.id())));
107691
+ int64_t oid;
107692
+ if (type_entry->internal) {
107693
+ oid = int64_t(type.id());
107694
+ } else {
107695
+ oid = type_entry->oid;
107696
+ }
107697
+ Value oid_val;
107698
+ if (data.oids.find(oid) == data.oids.end()) {
107699
+ data.oids.insert(oid);
107700
+ oid_val = Value::BIGINT(oid);
107701
+ } else {
107702
+ oid_val = Value();
107703
+ }
107704
+ output.SetValue(2, count, oid_val);
107546
107705
  // type_name, VARCHAR
107547
- output.SetValue(3, count, Value(type.ToString()));
107706
+ output.SetValue(3, count, Value(type_entry->name));
107548
107707
  // type_size, BIGINT
107549
107708
  auto internal_type = type.InternalType();
107550
107709
  output.SetValue(4, count,
107551
107710
  internal_type == PhysicalType::INVALID ? Value() : Value::BIGINT(GetTypeIdSize(internal_type)));
107711
+ // logical_type, VARCHAR
107712
+ output.SetValue(5, count, Value(LogicalTypeIdToString(type.id())));
107552
107713
  // type_category, VARCHAR
107553
107714
  string category;
107554
107715
  switch (type.id()) {
@@ -107592,9 +107753,9 @@ void DuckDBTypesFunction(ClientContext &context, const FunctionData *bind_data,
107592
107753
  default:
107593
107754
  break;
107594
107755
  }
107595
- output.SetValue(5, count, category.empty() ? Value() : Value(category));
107756
+ output.SetValue(6, count, category.empty() ? Value() : Value(category));
107596
107757
  // internal, BOOLEAN
107597
- output.SetValue(6, count, Value::BOOLEAN(true));
107758
+ output.SetValue(7, count, Value::BOOLEAN(type_entry->internal));
107598
107759
 
107599
107760
  count++;
107600
107761
  }
@@ -156527,6 +156688,7 @@ unique_ptr<CreateStatement> Transformer::TransformCreateEnum(duckdb_libpgquery::
156527
156688
  D_ASSERT(stmt);
156528
156689
  auto result = make_unique<CreateStatement>();
156529
156690
  auto info = make_unique<CreateTypeInfo>();
156691
+ info->internal = false;
156530
156692
  info->name = ReadPgListToString(stmt->typeName)[0];
156531
156693
  idx_t size = 0;
156532
156694
  auto ordered_array = ReadPgListToVector(stmt->vals, size);
@@ -157942,7 +158104,7 @@ unique_ptr<TableRef> Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *r
157942
158104
  break;
157943
158105
  }
157944
158106
  default: {
157945
- throw NotImplementedException("Join type %d not supported yet...\n", root->jointype);
158107
+ throw NotImplementedException("Join type %d not supported\n", root->jointype);
157946
158108
  }
157947
158109
  }
157948
158110
 
@@ -158063,7 +158225,7 @@ unique_ptr<TableRef> Transformer::TransformTableRefNode(duckdb_libpgquery::PGNod
158063
158225
  case duckdb_libpgquery::T_PGRangeFunction:
158064
158226
  return TransformRangeFunction(reinterpret_cast<duckdb_libpgquery::PGRangeFunction *>(n));
158065
158227
  default:
158066
- throw NotImplementedException("From Type %d not supported yet...", n->type);
158228
+ throw NotImplementedException("From Type %d not supported", n->type);
158067
158229
  }
158068
158230
  }
158069
158231
 
@@ -162691,14 +162853,8 @@ void Binder::BindLogicalType(ClientContext &context, LogicalType &type, const st
162691
162853
  type = LogicalType::MAP(child_types);
162692
162854
  }
162693
162855
  } else if (type.id() == LogicalTypeId::USER) {
162694
- auto &user_type_name = UserType::GetTypeName(type);
162695
- auto user_type_catalog = (TypeCatalogEntry *)context.db->GetCatalog().GetEntry(context, CatalogType::TYPE_ENTRY,
162696
- schema, user_type_name, true);
162697
- if (!user_type_catalog) {
162698
- throw NotImplementedException("DataType %s not supported yet...\n", user_type_name);
162699
- }
162700
- type = user_type_catalog->user_type;
162701
- EnumType::SetCatalog(type, user_type_catalog);
162856
+ auto &catalog = Catalog::GetCatalog(context);
162857
+ type = catalog.GetType(context, schema, UserType::GetTypeName(type));
162702
162858
  } else if (type.id() == LogicalTypeId::ENUM) {
162703
162859
  auto &enum_type_name = EnumType::GetTypeName(type);
162704
162860
  auto enum_type_catalog = (TypeCatalogEntry *)context.db->GetCatalog().GetEntry(context, CatalogType::TYPE_ENTRY,