* Given a DistributedBy clause, construct a GpPolicy for it. */
| 3108 | * Given a DistributedBy clause, construct a GpPolicy for it. |
| 3109 | */ |
| 3110 | GpPolicy * |
| 3111 | getPolicyForDistributedBy(DistributedBy *distributedBy, TupleDesc tupdesc) |
| 3112 | { |
| 3113 | List *policykeys; |
| 3114 | List *policyopclasses; |
| 3115 | ListCell *lc; |
| 3116 | |
| 3117 | if (!distributedBy) |
| 3118 | return NULL; /* XXX or should we complain? */ |
| 3119 | |
| 3120 | switch(distributedBy->ptype) |
| 3121 | { |
| 3122 | case POLICYTYPE_PARTITIONED: |
| 3123 | /* Look up the attribute numbers for each column */ |
| 3124 | policykeys = NIL; |
| 3125 | policyopclasses = NIL; |
| 3126 | foreach(lc, distributedBy->keyCols) |
| 3127 | { |
| 3128 | DistributionKeyElem *dkelem = (DistributionKeyElem *) lfirst(lc); |
| 3129 | char *colname = dkelem->name; |
| 3130 | int i; |
| 3131 | bool found = false; |
| 3132 | |
| 3133 | for (i = 0; i < tupdesc->natts; i++) |
| 3134 | { |
| 3135 | Form_pg_attribute attr = TupleDescAttr(tupdesc, i); |
| 3136 | |
| 3137 | if (strcmp(colname, NameStr(attr->attname)) == 0) |
| 3138 | { |
| 3139 | Oid opclass; |
| 3140 | |
| 3141 | opclass = cdb_get_opclass_for_column_def(dkelem->opclass, attr->atttypid); |
| 3142 | |
| 3143 | policykeys = lappend_int(policykeys, attr->attnum); |
| 3144 | policyopclasses = lappend_oid(policyopclasses, opclass); |
| 3145 | found = true; |
| 3146 | } |
| 3147 | } |
| 3148 | if (!found) |
| 3149 | ereport(ERROR, |
| 3150 | (errcode(ERRCODE_UNDEFINED_COLUMN), |
| 3151 | errmsg("column \"%s\" does not exist", colname))); |
| 3152 | } |
| 3153 | |
| 3154 | return createHashPartitionedPolicy(policykeys, |
| 3155 | policyopclasses, |
| 3156 | distributedBy->numsegments);; |
| 3157 | |
| 3158 | case POLICYTYPE_ENTRY: |
| 3159 | elog(ERROR, "unexpected entry distribution policy"); |
| 3160 | return NULL; |
| 3161 | |
| 3162 | case POLICYTYPE_REPLICATED: |
| 3163 | return createReplicatedGpPolicy(distributedBy->numsegments); |
| 3164 | } |
| 3165 | elog(ERROR, "unrecognized policy type %d", distributedBy->ptype); |
| 3166 | return NULL; |
| 3167 | } |
no test coverage detected