diff --git a/doc/source/notebooks/embedding.ipynb b/doc/source/notebooks/embedding.ipynb index 12353331..b112239a 100644 --- a/doc/source/notebooks/embedding.ipynb +++ b/doc/source/notebooks/embedding.ipynb @@ -2,17 +2,39 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/home/gpadmin/GreenplumPython\n", + "Defaulting to user installation because normal site-packages is not writeable\n", + "Processing /home/gpadmin/GreenplumPython\n", + " Installing build dependencies ... \u001b[?25ldone\n", + "\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n", + "\u001b[?25h Preparing wheel metadata ... \u001b[?25ldone\n", + "\u001b[?25hRequirement already satisfied, skipping upgrade: dill==0.3.6 in /home/gpadmin/.local/lib/python3.9/site-packages (from greenplum-python==1.0.1) (0.3.6)\n", + "Requirement already satisfied, skipping upgrade: psycopg2-binary==2.9.5 in /home/gpadmin/.local/lib/python3.9/site-packages (from greenplum-python==1.0.1) (2.9.5)\n", + "Building wheels for collected packages: greenplum-python\n", + " Building wheel for greenplum-python (PEP 517) ... \u001b[?25ldone\n", + "\u001b[?25h Created wheel for greenplum-python: filename=greenplum_python-1.0.1-py3-none-any.whl size=70509 sha256=995d00c1fdf47e7721a42c1f1f1e0ffa3af7b02ea1403620ee8c2cebdacf69c6\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-34hu1ytc/wheels/bb/1f/99/ff8594e48ec11df99af6e0ee8611a5e560e9f44d1a3fefb351\n", + "Successfully built greenplum-python\n", + "Installing collected packages: greenplum-python\n", + "Successfully installed greenplum-python-1.0.1\n" + ] + } + ], "source": [ - "%cd $(find ~ -name GreenplumPython)\n", + "%cd ../../../\n", "!python3 -m pip install --upgrade ." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -26,9 +48,147 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " CREATE TABLE \"cte_7721c0fd66c041c4b7c5ff4074b3479b\"\n", + " (id,content)\n", + " \n", + " AS SELECT unnest(ARRAY[0,1]) AS \"id\",unnest(ARRAY['I have a dog.','I like eating apples.']) AS \"content\"\n", + " \n", + " DISTRIBUTED BY (id)\n", + " \n", + " \n", + "WITH cte_d0f82a0d4eee4d86a93fa03651ebcb8a AS (TABLE \"cte_7721c0fd66c041c4b7c5ff4074b3479b\")SELECT to_json(cte_1fd18725c25a4c9a877fd6a0aa180e0a)::TEXT FROM cte_d0f82a0d4eee4d86a93fa03651ebcb8a AS cte_1fd18725c25a4c9a877fd6a0aa180e0a\n", + "----------------------------\n", + " id | content \n", + "----+-----------------------\n", + " 0 | I have a dog. \n", + " 1 | I like eating apples. \n", + "----------------------------\n", + "(2 rows)\n", + "\n", + "CREATE UNIQUE INDEX ON \"cte_7721c0fd66c041c4b7c5ff4074b3479b\" (id)\n", + "CREATE FUNCTION \"pg_temp\".\"func_54569d267ed6412e9026f55a5bf7601b\" (content \"text\",model_name \"text\") RETURNS \"vector\"(384) AS $$\n", + "try:\n", + " return GD['__func_54569d267ed6412e9026f55a5bf7601b'](content=content,model_name=model_name)\n", + "except KeyError:\n", + " try:\n", + " import dill as __lib_64f0f94e2fe74186b2d943881574a343\n", + " import sysconfig as __lib_605eb3b464e84c5999150fadf59627c7\n", + " import base64 as __lib_bddbb4833d4c4ea7879fcdffa7ff8270\n", + " import sys as __lib_6f40e80834a64c8d91808b5226eda448\n", + " if __lib_605eb3b464e84c5999150fadf59627c7.get_python_version() != '3.9':\n", + " raise ModuleNotFoundError\n", + " setattr(__lib_6f40e80834a64c8d91808b5226eda448.modules['plpy'], '_SD', SD)\n", + " GD['__func_54569d267ed6412e9026f55a5bf7601b'] = __lib_64f0f94e2fe74186b2d943881574a343.loads(__lib_bddbb4833d4c4ea7879fcdffa7ff8270.b64decode(b'gASVNgMAAAAAAACMCmRpbGwuX2RpbGyUjBBfY3JlYXRlX2Z1bmN0aW9ulJOUKGgAjAxfY3JlYXRlX2NvZGWUk5QoSwJLAEsASwdLBUtDQ2JkAWQAbAB9AnQBgwCgAmQCfAJqA2QDGQBqBKECfQNkBHwDdgFyRGQBZAVsBW0GfQQBAHwEfAGDAX0FfAV8A2QEPABuCHwDZAQZAH0FfAVqB3wAZAZkB40CfQZ8BqAIoQBTAJQoTksAjAJTRJSMBHBscHmUjAVtb2RlbJSME1NlbnRlbmNlVHJhbnNmb3JtZXKUhZSIjBRub3JtYWxpemVfZW1iZWRkaW5nc5SFlHSUKIwDc3lzlIwHZ2xvYmFsc5SMA2dldJSMB21vZHVsZXOUjANfU0SUjBVzZW50ZW5jZV90cmFuc2Zvcm1lcnOUaAmMBmVuY29kZZSMBnRvbGlzdJR0lCiMB2NvbnRlbnSUjAptb2RlbF9uYW1llGgOaAZoCWgIjANlbWKUdJSMRy9ob21lL2dwYWRtaW4vR3JlZW5wbHVtUHl0aG9uL2dyZWVucGx1bXB5dGhvbi9leHBlcmltZW50YWwvZW1iZWRkaW5nLnB5lIwTX2dlbmVyYXRlX2VtYmVkZGluZ5RLCkMSAAIIAhYBCAEMAggBCgIIAw4BlCkpdJRSlH2UjAhfX25hbWVfX5SMJmdyZWVucGx1bXB5dGhvbi5leHBlcmltZW50YWwuZW1iZWRkaW5nlHNoHE5OdJRSlH2UfZSMD19fYW5ub3RhdGlvbnNfX5R9lChoF2gAjApfbG9hZF90eXBllJOUjANzdHKUhZRSlGgYaC2MBnJldHVybpSMFGdyZWVucGx1bXB5dGhvbi50eXBllIwEVHlwZZSTlCmBlH2UKIwFX25hbWWUjAZ2ZWN0b3KUjAtfYW5ub3RhdGlvbpROjA9fY3JlYXRlZF9pbl9kYnOUTowHX3NjaGVtYZROjAlfbW9kaWZpZXKUTYABjBNfcXVhbGlmaWVkX25hbWVfc3RylIwNInZlY3RvciIoMzg0KZR1YnVzhpRiaCCMB2dsb2JhbHOUjAhidWlsdGluc5SMB2dsb2JhbHOUk5RzMC4='))\n", + " except ModuleNotFoundError:\n", + " exec(\"def __func_54569d267ed6412e9026f55a5bf7601b(content, model_name):\\n import sys\\n SD = globals().get('SD', sys.modules['plpy']._SD)\\n if 'model' not in SD:\\n from sentence_transformers import SentenceTransformer\\n model = SentenceTransformer(model_name)\\n SD['model'] = model\\n else:\\n model = SD['model']\\n emb = model.encode(content, normalize_embeddings=True)\\n return emb.tolist()\", globals())\n", + " GD['__func_54569d267ed6412e9026f55a5bf7601b'] = globals()['__func_54569d267ed6412e9026f55a5bf7601b']\n", + " return GD['__func_54569d267ed6412e9026f55a5bf7601b'](content=content,model_name=model_name)\n", + "$$ LANGUAGE plpython3u;\n", + "\n", + " CREATE TABLE \"cte_25a74c5b96e64b4183b4b55256867459\"\n", + " (id,_emb_23dd83d7748d40cd8eda21e5f2129629)\n", + " \n", + " AS WITH cte_d0f82a0d4eee4d86a93fa03651ebcb8a AS (TABLE \"cte_7721c0fd66c041c4b7c5ff4074b3479b\"),cte_6a96d6faca4846f6ab4870e8dfc80e29 AS (SELECT *, (\"pg_temp\".\"func_54569d267ed6412e9026f55a5bf7601b\"( cte_d0f82a0d4eee4d86a93fa03651ebcb8a.\"content\",'all-MiniLM-L6-v2')::\"vector\"(384)) AS _emb_23dd83d7748d40cd8eda21e5f2129629 FROM cte_d0f82a0d4eee4d86a93fa03651ebcb8a)\n", + " SELECT cte_6a96d6faca4846f6ab4870e8dfc80e29.\"id\",cte_6a96d6faca4846f6ab4870e8dfc80e29.\"_emb_23dd83d7748d40cd8eda21e5f2129629\"\n", + " FROM cte_6a96d6faca4846f6ab4870e8dfc80e29\n", + " \n", + " \n", + " DISTRIBUTED BY (id)\n", + " \n", + " \n", + "WITH cte_14c1fce52f0e4af08972a2eb07df02a9 AS (TABLE \"cte_25a74c5b96e64b4183b4b55256867459\")SELECT to_json(cte_baadcbf448304ef7a6693f98b217a984)::TEXT FROM cte_14c1fce52f0e4af08972a2eb07df02a9 AS cte_baadcbf448304ef7a6693f98b217a984\n", + "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", + " id | _emb_23dd83d7748d40cd8eda21e5f2129629 \n", + "----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", + " 0 | [-0.03659846,-0.012087755,0.08805456,0.061151367,-0.043457735,-0.015592895,0.07047544,-0.002039735,0.082576185,-0.027372142,0.0441429,-0.03269936,0.013636178,0.04161656,0.010410312,-0.0015930303,-0.06705982,-0.044098374,-0.0057846555,-0.06406435,-0.06587656,0.07500747,0.012162317,-0.005788625,-0.109905936,0.027304199,-0.039163478,-0.05016219,0.0029828846,-0.038391765,-0.015229778,-0.055909287,-0.011802607,-0.0048770546,-0.042732246,-0.041694522,0.0065653264,-0.013692718,0.103015944,0.08045541,0.047172282,0.014515043,0.06301976,-0.008371227,-0.00376406,0.037010953,-0.08730184,-0.019860007,0.116164975,-0.009175166,-0.02942207,0.05760907,-0.017986163,0.030363861,-0.018659858,-0.023920199,0.0075364825,0.030293372,-0.0017754125,-0.027292343,0.010452809,0.06776974,0.009428498,0.047237813,0.00020278565,0.02074401,-0.061770048,0.06334932,-0.06630364,0.055175032,0.03636048,0.033629246,0.0447184,0.09541598,-0.035489805,-0.10748742,0.06386299,-0.030471267,0.18042347,0.07920897,-0.08705959,-0.06174667,-0.042777605,0.04772073,0.04045522,0.011489479,0.07283991,0.06658613,-0.117522456,0.011569888,-0.022578657,-0.049202647,-0.03411386,0.01763423,-0.0032649997,-0.010033316,-0.022944551,-0.033948276,-0.021662703,0.089601316,0.008443466,0.028806083,0.07188612,0.045687683,0.09596068,0.023099585,-0.09392849,0.060846657,-0.010293214,0.0019619083,-0.012627958,0.009032658,-0.023953663,0.10200912,0.047290858,0.045499124,-0.07541447,-0.024221145,0.06080323,-0.09191942,0.011989021,0.021896897,-0.044340197,0.02122626,0.019848485,-0.058525886,0.03497772,-9.026044e-33,-0.0053451294,-0.0290533,0.014672238,0.04659998,-0.028272917,0.013217353,-0.038185634,0.030172182,-0.05259568,-0.016775027,0.0034630536,0.00057961704,-0.020373803,-0.034381036,-0.0033685626,0.0013990739,0.051134076,0.01848566,0.08034311,-0.00014362897,-0.013998878,-0.021286957,0.03914335,0.017298121,-0.017837863,-0.012515484,-0.013980058,-0.083431505,-0.02655956,0.024582446,0.028264284,0.020893436,0.045632925,-0.041542996,-0.105518915,-0.03636643,-0.05349343,-0.05543646,-0.043980815,0.052545346,0.08640961,-0.0042671263,0.017281698,-0.0003456163,0.0046999496,-0.034805853,0.008263829,0.020119112,-0.09260091,0.01470312,0.011787518,-0.03307292,0.0042901468,-0.08931934,-0.029248364,-0.041016944,0.059762184,-0.009189991,0.019669637,0.08591937,0.022527453,0.0075523625,-0.030852512,0.029306179,0.051727347,-0.090517536,-0.09521753,-0.041740306,-0.0011757809,0.014292587,-0.024682235,-0.0035219707,0.0077362237,-0.017399674,0.07142882,-0.0123587465,-0.00534226,-0.003308827,-0.01875911,-0.07966144,0.019006351,0.0018609086,0.00706818,0.057706404,0.07751448,0.059841618,-0.029955173,-0.0058063352,-0.023169437,0.0026582994,-0.065715685,-0.043993074,0.03394865,-0.027996289,0.04052676,5.238207e-33,0.010241467,0.03607309,0.046909,0.013635992,-0.005335444,0.0016521218,-0.020371608,0.04564495,-0.082175665,0.06402261,-0.001709206,0.044672277,0.10069537,0.00045673744,0.062299304,0.03769347,-0.039460346,-0.019606683,0.050265815,-0.05616924,-0.18455045,0.08040064,0.07426137,0.019323843,-0.026447829,0.040501535,-0.019648906,-0.02372921,-0.058951914,-0.0853744,-0.045682464,-0.12889871,-0.055900462,-0.068548314,-0.0058031273,0.066947535,-0.023167405,-0.1457526,-0.0123237185,-0.059538133,0.036701616,-0.0021032416,0.04832922,0.078937754,0.014486305,0.029141134,0.014654051,-0.06743171,0.00976345,0.033080045,-0.026131311,-0.008976268,-0.028050678,-0.062519975,-0.0033331455,-0.014157539,-0.07179509,-0.067832775,0.014238785,0.008521254,-0.031684905,0.09964349,-0.05202337,0.13799058,-0.019717641,-0.0868198,-0.0071095424,-0.0557247,0.011921498,-0.07336916,-0.0079654865,0.07029794,-0.031166447,-0.055607356,0.0108316,0.04010841,0.051589135,-0.0015768349,0.03786852,0.015498465,-0.06851167,-0.04085385,0.009224494,-0.010765805,-0.001525135,-0.03769954,-0.00508086,0.05028555,-0.0018060899,0.047179505,-0.032873716,0.0786257,0.0219288,-0.055561442,0.0068103974,-1.6011152e-08,-0.047843613,-0.0016648023,-0.0019612245,-0.0025547266,0.05134095,0.035634715,0.0084129,-0.06416773,-0.03193827,-0.019677935,0.03140499,-0.0173519,-0.043358684,0.02033876,0.10461025,0.025110237,0.017567858,8.451519e-06,0.034815624,0.1194926,-0.071207054,0.014109294,0.079820834,-0.006870605,-0.0052823867,-0.029617261,0.073567234,0.06555545,-0.09733238,0.06841361,-0.032084044,0.10998643,-0.031699374,0.018973608,0.02462254,-0.069597505,0.070999734,-0.050207775,0.044230375,0.021497803,0.05741905,0.12532367,-0.08883319,-0.01811394,0.0011768066,0.06459078,-0.0014821606,-0.09094167,-0.0075864797,-0.00019054905,-0.124157004,-0.064882055,0.09381429,0.051018275,-0.020306546,-0.004231254,-0.018098317,-0.07439528,0.056705363,0.036972076,0.03879501,0.044584196,-0.080352895,-0.030577179] \n", + " 1 | [0.021809116,-0.0155318845,0.011607787,0.08773645,-0.060896672,-0.035311002,0.1109756,-0.05388055,0.015478594,0.025643239,0.034682155,-0.09349968,0.018253846,0.003201303,0.043405153,-0.037074342,0.088959046,-0.0040923767,-0.010021047,0.005995185,-0.078318,0.066143945,0.042326793,-0.027101,0.017702201,0.04703828,0.06959306,-0.037545238,-0.08466894,-0.0149313845,-0.05919541,2.3302038e-05,0.013309361,0.012327695,-0.054391142,0.008196508,0.14044063,-0.07974372,-0.041333504,-0.02224858,0.01838698,0.066759095,0.060005356,0.040904347,-0.057686333,-0.008572934,-0.0006931544,-0.017934252,0.09348519,0.04610809,0.042312067,0.0042564836,-0.035399742,-0.031868268,0.05509771,0.03063401,0.017477227,0.007607798,0.002851456,-0.00848902,0.070586,-0.06596947,-0.003001832,0.017515425,0.03681233,-0.05101503,-0.05168192,-0.007240675,-0.056723353,-0.0003316012,-0.016689977,0.05097667,0.09232242,0.048701957,-0.0233264,0.014426018,0.09440483,-0.08410635,-0.065320976,0.010295285,-0.06000792,-0.0066203084,0.018760884,0.006218706,-0.016821042,-0.051536806,-0.019194003,0.019247968,-0.055921093,0.0744291,0.0011268512,-0.018572511,-0.033866387,0.04826349,0.0018755798,0.02145841,0.026700653,-0.07195235,-0.035215978,0.09375797,0.009641733,0.03153927,-0.006521103,0.059988238,0.02907713,0.006436106,-0.1688827,-0.0121928835,0.00831767,-0.0010369162,0.020289466,-0.015101374,-0.036400627,-0.0053182486,0.016343202,0.04836314,0.052492004,0.0022888575,0.013867832,-0.011067111,-0.0063246978,0.08962686,-0.05633277,-5.0772534e-05,0.0003743617,-0.043979187,0.030548064,-6.112132e-33,-0.100447245,-0.047969893,0.050677996,-0.031848874,0.017650874,0.00557819,0.035132963,0.09510477,0.09157566,-0.02606478,-0.0059387456,-0.023844875,-0.037891146,-0.0062694843,0.024072707,-0.06319935,-0.025684582,0.07265957,-0.04208773,-0.014134076,-0.017349897,-0.092400596,-0.006409122,0.09291194,-0.027069112,-0.08738224,0.042585004,-0.12305705,0.062073916,0.01713978,0.043850727,-0.0055547897,-0.035159733,-0.05796307,-0.0016850628,-0.029315371,0.0721106,0.04989424,-0.028748112,0.0011031058,-0.0070465477,0.02051565,0.0671912,0.021492135,0.06486443,0.006083919,0.025401652,0.07397291,-0.030965947,-0.007620959,-0.045778204,-0.048278432,0.09053187,0.032227647,-0.015725302,-0.010724716,0.013521858,-0.036038376,-0.092461206,0.013104383,-0.078536704,0.049683243,0.0088001145,-0.007872616,-0.11311237,0.11412768,-0.035817996,-0.047303315,0.014969718,0.023965022,-0.042791124,0.03148287,-0.022683943,0.0005804951,-0.11246335,-0.09787001,0.04521075,-0.031591777,-0.055069365,-0.023562724,0.052014757,-0.0024513777,0.0039027003,-0.010034752,0.033652805,0.122117504,-0.067184255,-0.0667508,0.1081975,-0.015414982,0.00400915,0.021052312,0.016455468,0.019499231,-0.12814389,5.5857056e-33,-0.0018572047,-0.080793984,-0.013305316,0.018411051,-0.037682965,-0.06759447,-0.08707167,0.013579468,-0.02803436,-0.03244555,-0.026130449,-0.0068652094,-0.02230584,-0.016416704,0.023153821,0.024428546,-0.011959954,0.09368941,-0.0325776,0.026465558,-0.046098772,0.008481753,-0.006716867,0.019120444,0.016167276,-0.023132937,-0.0042774444,0.04393394,-0.018111901,0.059962098,0.051095933,-0.07903501,-0.059705775,-0.13360043,0.04902078,0.03544222,-0.093780324,-0.056613877,-0.0022577501,0.03077088,0.015449573,0.0032539142,0.031303108,0.11281747,0.036288813,0.093467966,0.03139063,0.058778938,0.02215492,0.05777489,0.0009719842,-0.026091043,-0.06628838,0.015047404,0.03955509,0.05236228,0.0069718575,0.0009398838,-0.03959814,-0.075498044,-0.10264736,0.06432404,0.018766893,0.0139612565,0.060313296,-0.02941947,-0.030336095,-0.05356687,-0.07672768,0.012401418,-0.009276501,-0.054574188,-0.056601916,-0.024081068,-0.0397901,-0.035410695,0.01184497,0.036265045,-0.08490439,0.05896337,-0.030408576,0.10739633,0.0100452835,0.06581673,0.04995253,0.056139104,-0.018259417,0.023479536,-0.04595968,0.038907755,-0.005904816,-0.015094102,0.013457788,-0.039148435,0.01151064,-1.5212933e-08,-0.045827802,-0.029699314,0.03503022,-0.01087893,-0.0031904539,0.07422464,-0.07662781,0.054133236,0.021378785,-0.040636804,0.062867165,0.085515775,-0.08906479,0.056114767,0.048328143,0.008293789,0.08469364,-0.027762378,-0.015386821,0.06791649,-0.09377292,0.018911818,-0.013141011,0.04376481,-0.018527081,0.021828363,0.0024259256,0.020919835,0.10574034,0.063920565,0.05623135,0.053664792,-0.0830025,0.06855377,-0.005921287,-0.0768514,0.010081457,-0.01137772,-0.012504763,-0.10047469,-0.049601573,-0.002936133,0.015598559,-0.04278624,-0.0998226,0.02282328,0.06384441,0.011207105,0.0207268,0.08571721,0.04142781,0.026192738,0.096607804,0.08237023,0.03691293,-0.014799454,0.04348573,-0.07760758,0.01575173,0.078169346,0.11799159,0.05871559,0.021846,-0.016581286] \n", + "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", + "(2 rows)\n", + "\n", + "CREATE UNIQUE INDEX ON \"cte_25a74c5b96e64b4183b4b55256867459\" (id)\n", + "CREATE INDEX \"idx_f820481a5497411a9b74d29d2fe98ea5\" ON \"cte_25a74c5b96e64b4183b4b55256867459\" USING \"ivfflat\" ( \"_emb_23dd83d7748d40cd8eda21e5f2129629\")\n", + "\n", + " DO $$\n", + " BEGIN\n", + " SET LOCAL allow_system_table_mods TO ON;\n", + "\n", + " WITH embedding_info AS (\n", + " SELECT attrelid, attnum, 'all-MiniLM-L6-v2' AS model\n", + " FROM pg_attribute\n", + " WHERE \n", + " attrelid = '\"cte_7721c0fd66c041c4b7c5ff4074b3479b\"'::regclass::oid AND\n", + " attname = 'content'\n", + " )\n", + " UPDATE pg_class\n", + " SET reloptions = array_append(\n", + " reloptions, \n", + " format('_pygp_emb_%s=%s', attnum::text, to_json(embedding_info))\n", + " )\n", + " FROM embedding_info;\n", + "\n", + " WITH embedding_info AS (\n", + " SELECT attrelid, attnum, 'all-MiniLM-L6-v2' AS model\n", + " FROM pg_attribute\n", + " WHERE \n", + " attrelid = '\"cte_7721c0fd66c041c4b7c5ff4074b3479b\"'::regclass::oid AND\n", + " attname = 'content'\n", + " )\n", + " INSERT INTO pg_depend\n", + " SELECT\n", + " 'pg_class'::regclass::oid AS classid,\n", + " '\"cte_25a74c5b96e64b4183b4b55256867459\"'::regclass::oid AS objid,\n", + " 0::oid AS objsubid,\n", + " 'pg_class'::regclass::oid AS refclassid,\n", + " embedding_info.attrelid AS refobjid,\n", + " embedding_info.attnum AS refobjsubid,\n", + " 'n' AS deptype\n", + " FROM embedding_info;\n", + " END;\n", + " $$;\n", + " \n" + ] + }, + { + "data": { + "text/html": [ + "\n", + "\t\n", + "\t\t\n", + "\t\t\n", + "\t\n", + "\t\n", + "\t\t\n", + "\t\t\n", + "\t\n", + "\t\n", + "\t\t\n", + "\t\t\n", + "\t\n", + "
idcontent
0I have a dog.
1I like eating apples.
" + ], + "text/plain": [ + "----------------------------\n", + " id | content \n", + "----+-----------------------\n", + " 0 | I have a dog. \n", + " 1 | I like eating apples. \n", + "----------------------------\n", + "(2 rows)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import greenplumpython.experimental.embedding\n", "\n", @@ -43,9 +203,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " WITH embedding_oid AS (\n", + " SELECT attrelid, attnum\n", + " FROM pg_attribute\n", + " WHERE \n", + " attrelid = '\"cte_7721c0fd66c041c4b7c5ff4074b3479b\"'::regclass::oid AND\n", + " attname = 'content'\n", + " ), reloptions AS (\n", + " SELECT unnest(reloptions) AS option\n", + " FROM pg_class, embedding_oid\n", + " WHERE pg_class.oid = attrelid\n", + " ), embedding_info_json AS (\n", + " SELECT split_part(option, '=', 2)::json AS val\n", + " FROM reloptions, embedding_oid\n", + " WHERE option LIKE format('_pygp_emb_%s=%%', attnum)\n", + " ), embedding_info AS (\n", + " SELECT * \n", + " FROM embedding_info_json, json_to_record(val) AS (attnum int4, attrelid oid, model text)\n", + " )\n", + " SELECT nspname, relname, model\n", + " FROM embedding_info, pg_class, pg_namespace\n", + " WHERE \n", + " pg_class.oid = attrelid AND\n", + " relnamespace = pg_namespace.oid;\n", + " \n" + ] + }, + { + "ename": "AssertionError", + "evalue": "Database is required to create function.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m t\u001b[39m.\u001b[39;49membedding()\u001b[39m.\u001b[39;49msearch(column\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mcontent\u001b[39;49m\u001b[39m\"\u001b[39;49m, query\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mapple\u001b[39;49m\u001b[39m\"\u001b[39;49m, top_k\u001b[39m=\u001b[39;49m\u001b[39m1\u001b[39;49m)\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/experimental/embedding.py:154\u001b[0m, in \u001b[0;36mEmbedding.search\u001b[0;34m(self, column, query, top_k)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_dataframe\u001b[39m.\u001b[39munique_key \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[1;32m 152\u001b[0m distance \u001b[39m=\u001b[39m gp\u001b[39m.\u001b[39moperator(\u001b[39m\"\u001b[39m\u001b[39m<#>\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 153\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_dataframe\u001b[39m.\u001b[39mjoin(\n\u001b[0;32m--> 154\u001b[0m embedding_df\u001b[39m.\u001b[39;49massign(\n\u001b[1;32m 155\u001b[0m distance\u001b[39m=\u001b[39;49m\u001b[39mlambda\u001b[39;49;00m t: distance(t[\u001b[39m\"\u001b[39;49m\u001b[39m_emb_\u001b[39;49m\u001b[39m\"\u001b[39;49m], _generate_embedding(query, model))\n\u001b[1;32m 156\u001b[0m )\u001b[39m.\u001b[39morder_by(\u001b[39m\"\u001b[39m\u001b[39mdistance\u001b[39m\u001b[39m\"\u001b[39m)[:top_k],\n\u001b[1;32m 157\u001b[0m how\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39minner\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 158\u001b[0m on\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_dataframe\u001b[39m.\u001b[39munique_key,\n\u001b[1;32m 159\u001b[0m self_columns\u001b[39m=\u001b[39m{\u001b[39m\"\u001b[39m\u001b[39m*\u001b[39m\u001b[39m\"\u001b[39m},\n\u001b[1;32m 160\u001b[0m other_columns\u001b[39m=\u001b[39m{},\n\u001b[1;32m 161\u001b[0m )\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/dataframe.py:492\u001b[0m, in \u001b[0;36mDataFrame.assign\u001b[0;34m(self, **new_columns)\u001b[0m\n\u001b[1;32m 490\u001b[0m other_parents[v\u001b[39m.\u001b[39m_other_dataframe\u001b[39m.\u001b[39m_name] \u001b[39m=\u001b[39m v\u001b[39m.\u001b[39m_other_dataframe\n\u001b[1;32m 491\u001b[0m v \u001b[39m=\u001b[39m v\u001b[39m.\u001b[39m_bind(db\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_db)\n\u001b[0;32m--> 492\u001b[0m targets\u001b[39m.\u001b[39mappend(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00m_serialize(v)\u001b[39m}\u001b[39;00m\u001b[39m AS \u001b[39m\u001b[39m{\u001b[39;00mk\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 493\u001b[0m \u001b[39mreturn\u001b[39;00m DataFrame(\n\u001b[1;32m 494\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mSELECT *, \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m,\u001b[39m\u001b[39m'\u001b[39m\u001b[39m.\u001b[39mjoin(targets)\u001b[39m}\u001b[39;00m\u001b[39m FROM \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_name\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 495\u001b[0m parents\u001b[39m=\u001b[39m[\u001b[39mself\u001b[39m] \u001b[39m+\u001b[39m \u001b[39mlist\u001b[39m(other_parents\u001b[39m.\u001b[39mvalues()),\n\u001b[1;32m 496\u001b[0m )\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/expr.py:561\u001b[0m, in \u001b[0;36m_serialize\u001b[0;34m(value)\u001b[0m\n\u001b[1;32m 551\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 552\u001b[0m \u001b[39m:meta private:\u001b[39;00m\n\u001b[1;32m 553\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[39m in Python 3 and Python 2 is EOL officially.\u001b[39;00m\n\u001b[1;32m 559\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 560\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(value, Expr):\n\u001b[0;32m--> 561\u001b[0m \u001b[39mreturn\u001b[39;00m value\u001b[39m.\u001b[39;49m_serialize()\n\u001b[1;32m 562\u001b[0m \u001b[39mreturn\u001b[39;00m adapt(value)\u001b[39m.\u001b[39mgetquoted()\u001b[39m.\u001b[39mdecode(\u001b[39m\"\u001b[39m\u001b[39mutf-8\u001b[39m\u001b[39m\"\u001b[39m)\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/expr.py:636\u001b[0m, in \u001b[0;36mBinaryExpr._serialize\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 633\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mgreenplumpython\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mexpr\u001b[39;00m \u001b[39mimport\u001b[39;00m _serialize\n\u001b[1;32m 635\u001b[0m left_str \u001b[39m=\u001b[39m _serialize(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_left)\n\u001b[0;32m--> 636\u001b[0m right_str \u001b[39m=\u001b[39m _serialize(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_right)\n\u001b[1;32m 637\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m(\u001b[39m\u001b[39m{\u001b[39;00mleft_str\u001b[39m}\u001b[39;00m\u001b[39m \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_operator\u001b[39m}\u001b[39;00m\u001b[39m \u001b[39m\u001b[39m{\u001b[39;00mright_str\u001b[39m}\u001b[39;00m\u001b[39m)\u001b[39m\u001b[39m\"\u001b[39m\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/expr.py:561\u001b[0m, in \u001b[0;36m_serialize\u001b[0;34m(value)\u001b[0m\n\u001b[1;32m 551\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 552\u001b[0m \u001b[39m:meta private:\u001b[39;00m\n\u001b[1;32m 553\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 558\u001b[0m \u001b[39m in Python 3 and Python 2 is EOL officially.\u001b[39;00m\n\u001b[1;32m 559\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 560\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(value, Expr):\n\u001b[0;32m--> 561\u001b[0m \u001b[39mreturn\u001b[39;00m value\u001b[39m.\u001b[39;49m_serialize()\n\u001b[1;32m 562\u001b[0m \u001b[39mreturn\u001b[39;00m adapt(value)\u001b[39m.\u001b[39mgetquoted()\u001b[39m.\u001b[39mdecode(\u001b[39m\"\u001b[39m\u001b[39mutf-8\u001b[39m\u001b[39m\"\u001b[39m)\n", + "File \u001b[0;32m~/GreenplumPython/greenplumpython/func.py:86\u001b[0m, in \u001b[0;36mFunctionExpr._serialize\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_serialize\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mstr\u001b[39m:\n\u001b[1;32m 84\u001b[0m \u001b[39m# noqa D400\u001b[39;00m\n\u001b[1;32m 85\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\":meta private:\"\"\"\u001b[39;00m\n\u001b[0;32m---> 86\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_db \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m, \u001b[39m\"\u001b[39m\u001b[39mDatabase is required to create function.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 87\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_function\u001b[39m.\u001b[39m_create_in_db(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_db)\n\u001b[1;32m 88\u001b[0m distinct \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mDISTINCT\u001b[39m\u001b[39m\"\u001b[39m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_distinct \u001b[39melse\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39m\"\u001b[39m\n", + "\u001b[0;31mAssertionError\u001b[0m: Database is required to create function." + ] + } + ], "source": [ "t.embedding().search(column=\"content\", query=\"apple\", top_k=1)" ] diff --git a/greenplumpython/experimental/embedding.py b/greenplumpython/experimental/embedding.py index 8f7e366a..79028ec6 100644 --- a/greenplumpython/experimental/embedding.py +++ b/greenplumpython/experimental/embedding.py @@ -72,35 +72,42 @@ def create_index(self, column: str, model: str) -> gp.DataFrame: DO $$ BEGIN SET LOCAL allow_system_table_mods TO ON; + WITH embedding_info AS ( - SELECT attrelid, attnum, `{model}` AS model + SELECT attrelid, attnum, '{model}' AS model + FROM pg_attribute + WHERE + attrelid = '{self._dataframe._qualified_table_name}'::regclass::oid AND + attname = '{column}' + ) + UPDATE pg_class + SET reloptions = array_append( + reloptions, + format('_pygp_emb_%s=%s', attnum::text, to_json(embedding_info)) + ) + FROM embedding_info; + + WITH embedding_info AS ( + SELECT attrelid, attnum, '{model}' AS model FROM pg_attribute WHERE attrelid = '{self._dataframe._qualified_table_name}'::regclass::oid AND attname = '{column}' - ), add_option AS ( - UPDATE pg_class - FROM embedding_info - SET reloptions = array_append( - reloptions, - format('_pygp_emb_%s=%s', attnum::text, to_json(embedding_info)) - ) - ), add_dependency AS ( - INSERT INTO pg_depend - SELECT - 'pg_class'::regclass::oid AS classid, - '{embedding_df._qualified_table_name}'::regclass::oid AS objid, - 0::oid AS objsubid, - 'pg_class'::regclass::oid AS refclassid, - embedding_info.attrelid AS refobjid, - embedding_info.attnum AS refobjsubid - FROM embedding_info - RETURNING * ) - SELECT * FROM add_dependency; + INSERT INTO pg_depend + SELECT + 'pg_class'::regclass::oid AS classid, + '{embedding_df._qualified_table_name}'::regclass::oid AS objid, + 0::oid AS objsubid, + 'pg_class'::regclass::oid AS refclassid, + embedding_info.attrelid AS refobjid, + embedding_info.attnum AS refobjsubid, + 'n' AS deptype + FROM embedding_info; END; $$; - """ + """, + has_results=False ) return self._dataframe @@ -108,31 +115,32 @@ def search(self, column: str, query: Any, top_k: int) -> gp.DataFrame: assert self._dataframe._db is not None embdedding_info = self._dataframe._db._execute( f""" - WITH embedding_oid AS ( - SELECT attrelid, attnum - FROM pg_attribute - WHERE - attrelid = '{self._dataframe._qualified_table_name}'::regclass::oid AND - attname = '{column}' - ), reloptions AS ( - SELECT unnest(reloptions) AS option - FROM pg_class, embedding_oid - WEHRE oid = attrelid - ), embedding_info AS ( - SELECT split_part(option, '=', 2)::jsonb AS info - FROM reloptions, embedding_oid - WHERE option LIKE format('_pygp_emb%s=%%', attnum) - ) embedding_table_qualified_name AS ( - SELECT nspname, relname, embedding.info->'model' AS model - FROM embedding_table, pg_class, pg_namespace - WHERE - pg_class.oid = embedding.info->'attrelid' AND - relnamespace = pg_namespace.oid - ) - SELECT * FROM embedding_table_qualified_name + WITH embedding_oid AS ( + SELECT attrelid, attnum + FROM pg_attribute + WHERE + attrelid = '{self._dataframe._qualified_table_name}'::regclass::oid AND + attname = '{column}' + ), reloptions AS ( + SELECT unnest(reloptions) AS option + FROM pg_class, embedding_oid + WHERE pg_class.oid = attrelid + ), embedding_info_json AS ( + SELECT split_part(option, '=', 2)::json AS val + FROM reloptions, embedding_oid + WHERE option LIKE format('_pygp_emb_%s=%%', attnum) + ), embedding_info AS ( + SELECT * + FROM embedding_info_json, json_to_record(val) AS (attnum int4, attrelid oid, model text) + ) + SELECT nspname, relname, model + FROM embedding_info, pg_class, pg_namespace + WHERE + pg_class.oid = attrelid AND + relnamespace = pg_namespace.oid; """ ) - assert isinstance(embdedding_info, abc.Mapping[str, Any]) + # assert isinstance(embdedding_info, abc.Mapping) embedding_table_name = None for row in embdedding_info: embedding_table_name = f'"{row["nspname"]}"."{row["relname"]}"' diff --git a/greenplumpython/type.py b/greenplumpython/type.py index cb3f4bdd..8956aa3b 100644 --- a/greenplumpython/type.py +++ b/greenplumpython/type.py @@ -222,8 +222,6 @@ def to_pg_type( Returns: str: name of type in SQL """ - if isinstance(annotation, Type): - return annotation._qualified_name_str if annotation is not None and hasattr(annotation, "__origin__"): # The `or` here is to make the function work on Python 3.6. # Python 3.6 is the default Python version on CentOS 7 and Ubuntu 18.04 @@ -235,6 +233,8 @@ def to_pg_type( return f"{to_pg_type(args[0], db)}[]" # type: ignore raise NotImplementedError() else: + if isinstance(annotation, Type): + return annotation._qualified_name_str assert db is not None, "Database is required to create type" if annotation not in _defined_types: type_name = "type_" + uuid4().hex