This is an automated email from the ASF dual-hosted git repository.

avamingli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 793bffd26e381c04b3003929e7361824a1a2e2f3
Author: Soumyadeep Chakraborty <[email protected]>
AuthorDate: Tue Dec 20 11:44:08 2022 -0800

    ao/co: Ban speculative insert in parse analysis
    
    We don't support speculative insert (INSERT ON CONFLICT) for AO/CO
    tables yet.
    
    This commit does two things:
    
    (1) Confirms that its okay to keep the speculative insert APIs
    unimplemented, by removing the FIXMEs and adding ereport().
    
    (2) Instead of relying on the ERROR at the table AM layer, it introduces
    a check at the parse analysis stage to perform the ban. This is
    important as we can't rely on the table AM layer always. For instance,
    with DO NOTHING, if there is no conflict, the table AM routine is
    invoked. However, if there is a conflict, then the routine doesn't get
    invoked and the customer would see:
    
    INSERT INTO spec_insert VALUES(1, 3) ON CONFLICT(i) DO NOTHING;
    INSERT 0 0
    
    This would lead to a false impression that it is supported. So, we ban
    it at the parse analysis stage.
---
 src/backend/access/aocs/aocsam_handler.c             | 15 ++++++++++++---
 src/backend/access/appendonly/appendonlyam_handler.c | 15 ++++++++++++---
 src/backend/parser/analyze.c                         |  6 ++++++
 src/test/regress/input/uao_dml/uao_dml.source        | 10 ++++++++++
 src/test/regress/output/uao_dml/uao_dml.source       | 13 +++++++++++++
 5 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/aocs/aocsam_handler.c 
b/src/backend/access/aocs/aocsam_handler.c
index 3ebf5d6a82..f8466269ef 100644
--- a/src/backend/access/aocs/aocsam_handler.c
+++ b/src/backend/access/aocs/aocsam_handler.c
@@ -1058,20 +1058,29 @@ aoco_tuple_insert(Relation relation, TupleTableSlot 
*slot, CommandId cid,
        pgstat_count_heap_insert(relation, 1);
 }
 
+/*
+ * We don't support speculative inserts on appendoptimized tables, i.e. we 
don't
+ * support INSERT ON CONFLICT DO NOTHING or INSERT ON CONFLICT DO UPDATE. Thus,
+ * the following functions are left unimplemented.
+ */
+
 static void
 aoco_tuple_insert_speculative(Relation relation, TupleTableSlot *slot,
                                     CommandId cid, int options,
                                     BulkInsertState bistate, uint32 specToken)
 {
-       /* GPDB_12_MERGE_FIXME: not supported. Can this function be left out 
completely? Or ereport()? */
-       elog(ERROR, "speculative insertion not supported on AO_COLUMN tables");
+       ereport(ERROR,
+                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("speculative insert is not supported on 
appendoptimized relations")));
 }
 
 static void
 aoco_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
                                       uint32 specToken, bool succeeded)
 {
-       elog(ERROR, "speculative insertion not supported on AO_COLUMN tables");
+       ereport(ERROR,
+                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("speculative insert is not supported on 
appendoptimized relations")));
 }
 
 /*
diff --git a/src/backend/access/appendonly/appendonlyam_handler.c 
b/src/backend/access/appendonly/appendonlyam_handler.c
index c86c143963..f1b07d2ada 100644
--- a/src/backend/access/appendonly/appendonlyam_handler.c
+++ b/src/backend/access/appendonly/appendonlyam_handler.c
@@ -928,20 +928,29 @@ appendonly_tuple_insert(Relation relation, TupleTableSlot 
*slot, CommandId cid,
        appendonly_free_memtuple(mtuple);
 }
 
+/*
+ * We don't support speculative inserts on appendoptimized tables, i.e. we 
don't
+ * support INSERT ON CONFLICT DO NOTHING or INSERT ON CONFLICT DO UPDATE. Thus,
+ * the following functions are left unimplemented.
+ */
+
 static void
 appendonly_tuple_insert_speculative(Relation relation, TupleTableSlot *slot,
                                                                CommandId cid, 
int options,
                                                                BulkInsertState 
bistate, uint32 specToken)
 {
-       /* GPDB_12_MERGE_FIXME: not supported. Can this function be left out 
completely? Or ereport()? */
-       elog(ERROR, "speculative insertion not supported on AO tables");
+       ereport(ERROR,
+                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("speculative insert is not supported on 
appendoptimized relations")));
 }
 
 static void
 appendonly_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
                                                                  uint32 
specToken, bool succeeded)
 {
-       elog(ERROR, "speculative insertion not supported on AO tables");
+       ereport(ERROR,
+                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("speculative insert is not supported on 
appendoptimized relations")));
 }
 
 /*
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 60dd13744a..b794228959 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -702,6 +702,12 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
        icolumns = checkInsertTargets(pstate, stmt->cols, &attrnos);
        Assert(list_length(icolumns) == list_length(attrnos));
 
+       /* GPDB: We don't support speculative insert for AO/CO tables yet */
+       if (RelationIsAppendOptimized(pstate->p_target_relation) && 
stmt->onConflictClause)
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("INSERT ON CONFLICT is not supported for 
appendoptimized relations"));
+
        /*
         * Determine which variant of INSERT we have.
         */
diff --git a/src/test/regress/input/uao_dml/uao_dml.source 
b/src/test/regress/input/uao_dml/uao_dml.source
index adb9f033b7..b51faff222 100644
--- a/src/test/regress/input/uao_dml/uao_dml.source
+++ b/src/test/regress/input/uao_dml/uao_dml.source
@@ -1015,3 +1015,13 @@ explain (costs off)
 select * from taocs where c4>5 and c3>3 and c2!=12 and c4<15 and c2!=10 and 
c3<15 and c2>1 and c2<15 and c2+c3!=28 and gp_segment_id=1;
 select * from taocs where c4>5 and c3>3 and c2!=12 and c4<15 and c2!=10 and 
c3<15 and c2>1 and c2<15 and c2+c3!=28 and gp_segment_id=1;
 
+-- @Description Tests that speculative inserts are not supported (for
+-- conflicting inserts and otherwise).
+--
+CREATE TABLE spec_insert(i int unique, j int) distributed by (i);
+INSERT INTO spec_insert VALUES(1, 2);
+INSERT INTO spec_insert VALUES(1, 3) ON CONFLICT(i) DO NOTHING;
+INSERT INTO spec_insert VALUES(1, 3) ON CONFLICT(i) DO UPDATE SET j = 3;
+
+INSERT INTO spec_insert VALUES(2, 3) ON CONFLICT(i) DO NOTHING;
+INSERT INTO spec_insert VALUES(2, 3) ON CONFLICT(i) DO UPDATE SET j = 4;
diff --git a/src/test/regress/output/uao_dml/uao_dml.source 
b/src/test/regress/output/uao_dml/uao_dml.source
index f07b1b8de1..67653949c5 100644
--- a/src/test/regress/output/uao_dml/uao_dml.source
+++ b/src/test/regress/output/uao_dml/uao_dml.source
@@ -1929,3 +1929,16 @@ select * from taocs where c4>5 and c3>3 and c2!=12 and 
c4<15 and c2!=10 and c3<1
   1 | 11 | 11 | 11 | 11
 (4 rows)
 
+-- @Description Tests that speculative inserts are not supported (for
+-- conflicting inserts and otherwise).
+--
+CREATE TABLE spec_insert(i int unique, j int) distributed by (i);
+INSERT INTO spec_insert VALUES(1, 2);
+INSERT INTO spec_insert VALUES(1, 3) ON CONFLICT(i) DO NOTHING;
+ERROR:  INSERT ON CONFLICT is not supported for appendoptimized relations
+INSERT INTO spec_insert VALUES(1, 3) ON CONFLICT(i) DO UPDATE SET j = 3;
+ERROR:  INSERT ON CONFLICT is not supported for appendoptimized relations
+INSERT INTO spec_insert VALUES(2, 3) ON CONFLICT(i) DO NOTHING;
+ERROR:  INSERT ON CONFLICT is not supported for appendoptimized relations
+INSERT INTO spec_insert VALUES(2, 3) ON CONFLICT(i) DO UPDATE SET j = 4;
+ERROR:  INSERT ON CONFLICT is not supported for appendoptimized relations


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to