34
34
import verticapy ._config .config as conf
35
35
from verticapy ._utils ._object import create_new_vdf
36
36
from verticapy ._utils ._sql ._collect import save_verticapy_logs
37
+ from verticapy ._utils ._sql ._check import is_procedure
37
38
from verticapy ._utils ._sql ._dblink import replace_external_queries
38
39
from verticapy ._utils ._sql ._format import (
39
40
clean_query ,
40
41
replace_vars_in_query ,
41
42
)
42
43
from verticapy ._utils ._sql ._sys import _executeSQL
44
+ from verticapy .connection import current_cursor
43
45
from verticapy .connection .global_connection import get_global_connection
44
46
from verticapy .errors import QueryError
45
47
48
50
if TYPE_CHECKING :
49
51
from verticapy .core .vdataframe .base import vDataFrame
50
52
53
+ SPECIAL_WORDS = (
54
+ # ML Algos
55
+ "ARIMA" ,
56
+ "AUTOREGRESSOR" ,
57
+ "BALANCE" ,
58
+ "BISECTING_KMEANS" ,
59
+ "CROSS_VALIDATE" ,
60
+ "DETECT_OUTLIERS" ,
61
+ "IFOREST" ,
62
+ "IMPUTE" ,
63
+ "KMEANS" ,
64
+ "KPROTOTYPES" ,
65
+ "LINEAR_REG" ,
66
+ "LOGISTIC_REG" ,
67
+ "MOVING_AVERAGE" ,
68
+ "NAIVE_BAYES" ,
69
+ "NORMALIZE" ,
70
+ "NORMALIZE_FIT" ,
71
+ "ONE_HOT_ENCODER_FIT" ,
72
+ "PCA" ,
73
+ "POISSON_REG" ,
74
+ "RF_CLASSIFIER" ,
75
+ "RF_REGRESSOR" ,
76
+ "SVD" ,
77
+ "SVM_CLASSIFIER" ,
78
+ "SVM_REGRESSOR" ,
79
+ "XGB_CLASSIFIER" ,
80
+ "XGB_REGRESSOR" ,
81
+ # ML Management
82
+ "CHANGE_MODEL_STATUS" ,
83
+ "EXPORT_MODELS" ,
84
+ "IMPORT_MODELS" ,
85
+ "REGISTER_MODEL" ,
86
+ "UPGRADE_MODEL" ,
87
+ )
88
+
51
89
52
90
@save_verticapy_logs
53
91
@needs_local_scope
@@ -743,6 +781,12 @@ def sql_magic(
743
781
elif "-c" in options :
744
782
queries = options ["-c" ]
745
783
784
+ # Case when it is a procedure
785
+ if is_procedure (queries ):
786
+ current_cursor ().execute (queries )
787
+ print ("CREATE" )
788
+ return
789
+
746
790
# Cleaning the Query
747
791
queries = clean_query (queries )
748
792
queries = replace_vars_in_query (queries , locals ()["local_ns" ])
@@ -816,11 +860,14 @@ def sql_magic(
816
860
for i in range (n ):
817
861
query = queries [i ]
818
862
819
- if query .split (" " )[0 ]:
820
- query_type = query .split (" " )[0 ].upper ().replace ("(" , "" )
863
+ query_words = query .split (" " )
821
864
865
+ idx = 0 if query_words [0 ] else 1
866
+ query_type = query_words [idx ].upper ().replace ("(" , "" )
867
+ if len (query_words ) > 1 :
868
+ query_subtype = query_words [idx + 1 ].upper ()
822
869
else :
823
- query_type = query . split ( " " )[ 1 ]. upper (). replace ( "(" , "" )
870
+ query_subtype = "UNDEFINED"
824
871
825
872
if len (query_type ) > 1 and query_type .startswith (("/*" , "--" )):
826
873
query_type = "undefined"
@@ -843,7 +890,7 @@ def sql_magic(
843
890
844
891
elif (i < n - 1 ) or (
845
892
(i == n - 1 )
846
- and (query_type .lower () not in ("select" , "with" , "undefined" ))
893
+ and (query_type .lower () not in ("select" , "show" , " with" , "undefined" ))
847
894
):
848
895
error = ""
849
896
@@ -869,25 +916,45 @@ def sql_magic(
869
916
else :
870
917
error = ""
871
918
872
- try :
919
+ if query_type .lower () in ("show" ,):
920
+ final_result = _executeSQL (
921
+ query , method = "fetchall" , print_time_sql = False
922
+ )
923
+ columns = [d .name for d in current_cursor ().description ]
873
924
result = create_new_vdf (
874
- query ,
875
- _is_sql_magic = True ,
925
+ final_result ,
926
+ usecols = columns ,
876
927
)
877
- result ._vars ["sql_magic_result" ] = True
878
- # Display parameters
879
- if "-nrows" in options :
880
- result ._vars ["max_rows" ] = options ["-nrows" ]
881
- if "-ncols" in options :
882
- result ._vars ["max_columns" ] = options ["-ncols" ]
928
+ continue
883
929
884
- except :
930
+ is_vdf = False
931
+ if not (query_subtype .upper ().startswith (SPECIAL_WORDS )):
932
+ try :
933
+ result = create_new_vdf (
934
+ query ,
935
+ _is_sql_magic = True ,
936
+ )
937
+ result ._vars ["sql_magic_result" ] = True
938
+ # Display parameters
939
+ if "-nrows" in options :
940
+ result ._vars ["max_rows" ] = options ["-nrows" ]
941
+ if "-ncols" in options :
942
+ result ._vars ["max_columns" ] = options ["-ncols" ]
943
+ is_vdf = True
944
+ except :
945
+ pass # we could not create a vDataFrame out of the query.
946
+
947
+ if not (is_vdf ):
885
948
try :
886
949
final_result = _executeSQL (
887
950
query , method = "fetchfirstelem" , print_time_sql = False
888
951
)
889
952
if final_result and conf .get_option ("print_info" ):
890
953
print (final_result )
954
+ elif (
955
+ query_subtype .upper ().startswith (SPECIAL_WORDS )
956
+ ) and conf .get_option ("print_info" ):
957
+ print (query_subtype .upper ())
891
958
elif conf .get_option ("print_info" ):
892
959
print (query_type )
893
960
0 commit comments