@@ -808,7 +808,13 @@ virtual TStatus HandleCreateTable(TKiCreateTable create, TExprContext& ctx) over
808
808
809
809
columnMeta.SetDefaultFromLiteral ();
810
810
811
- if (auto pgConst = constraint.Value ().Maybe <TCoPgConst>()) {
811
+ YQL_ENSURE (constraint.Value ().IsValid ());
812
+ const auto & constrValue = constraint.Value ().Cast ();
813
+ bool isPgNull = constrValue.Ptr ()->IsCallable () &&
814
+ constrValue.Ptr ()->Content () == " PgCast" && constrValue.Ptr ()->ChildrenSize () >= 1 &&
815
+ constrValue.Ptr ()->Child (0 )->IsCallable () && constrValue.Ptr ()->Child (0 )->Content () == " Null" ;
816
+
817
+ if (constrValue.Maybe <TCoPgConst>() || isPgNull) {
812
818
auto actualPgType = actualType->Cast <TPgExprType>();
813
819
YQL_ENSURE (actualPgType);
814
820
@@ -819,25 +825,38 @@ virtual TStatus HandleCreateTable(TKiCreateTable create, TExprContext& ctx) over
819
825
return TStatus::Error;
820
826
}
821
827
822
- TString content = TString (pgConst.Cast ().Value ().Value ());
823
- auto parseResult = NKikimr::NPg::PgNativeBinaryFromNativeText (content, typeDesc);
824
- if (parseResult.Error ) {
825
- ctx.AddError (TIssue (ctx.GetPosition (constraint.Pos ()),
826
- TStringBuilder () << " Failed to parse default expr for typename " << actualPgType->GetName ()
827
- << " , error reason: " << *parseResult.Error ));
828
- return TStatus::Error;
828
+ if (isPgNull) {
829
+ if (columnMeta.NotNull ) {
830
+ ctx.AddError (TIssue (ctx.GetPosition (constraint.Pos ()), TStringBuilder () << " Default expr " << columnName
831
+ << " is nullable or optional, but column has not null constraint. " ));
832
+ return TStatus::Error;
833
+ }
834
+
835
+ columnMeta.DefaultFromLiteral .mutable_value ()->set_null_flag_value (NProtoBuf::NULL_VALUE);
836
+
837
+ } else {
838
+ YQL_ENSURE (constrValue.Maybe <TCoPgConst>());
839
+ auto pgConst = constrValue.Cast <TCoPgConst>();
840
+ TString content = TString (pgConst.Value ().Value ());
841
+ auto parseResult = NKikimr::NPg::PgNativeBinaryFromNativeText (content, typeDesc);
842
+ if (parseResult.Error ) {
843
+ ctx.AddError (TIssue (ctx.GetPosition (constraint.Pos ()),
844
+ TStringBuilder () << " Failed to parse default expr for typename " << actualPgType->GetName ()
845
+ << " , error reason: " << *parseResult.Error ));
846
+ return TStatus::Error;
847
+ }
848
+
849
+ columnMeta.DefaultFromLiteral .mutable_value ()->set_bytes_value (parseResult.Str );
829
850
}
830
851
831
- columnMeta.DefaultFromLiteral .mutable_value ()->set_bytes_value (parseResult.Str );
832
852
auto * pg = columnMeta.DefaultFromLiteral .mutable_type ()->mutable_pg_type ();
833
-
834
853
pg->set_type_name (NKikimr::NPg::PgTypeNameFromTypeDesc (typeDesc));
835
854
pg->set_oid (NKikimr::NPg::PgTypeIdFromTypeDesc (typeDesc));
836
- } else if (auto literal = constraint. Value () .Maybe <TCoDataCtor>()) {
837
- FillLiteralProto (constraint. Value (). Cast <TCoDataCtor> (), columnMeta.DefaultFromLiteral );
855
+ } else if (auto literal = constrValue .Maybe <TCoDataCtor>()) {
856
+ FillLiteralProto (literal. Cast (), columnMeta.DefaultFromLiteral );
838
857
} else {
839
858
ctx.AddError (TIssue (ctx.GetPosition (constraint.Pos ()),
840
- TStringBuilder () << " Unsupported type of default value " << constraint. Value (). Cast (). Ptr ()-> Content () ));
859
+ TStringBuilder () << " Unsupported type of default value" ));
841
860
return TStatus::Error;
842
861
}
843
862
0 commit comments