You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "singhpk234 (via GitHub)" <gi...@apache.org> on 2025/04/26 01:17:52 UTC
[PR] [JDBC] Support Policy [polaris]
singhpk234 opened a new pull request, #1468:
URL: https://github.com/apache/polaris/pull/1468
## About the Change
Add support for Policy via JDBC persistence.
cc @flyrain @HonahX
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062807141
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
I see so we want prefix search kind of a functionality, that makes sense then, let me revert that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain merged PR #1468:
URL: https://github.com/apache/polaris/pull/1468
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064192278
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
Review Comment:
yup the same is happening, i structed one additional else as if its inheritable its gonna be an update.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064373748
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
Having the `policyTypeCode` here doesn't imply anything about the primary key IMO. `loadEntity` already takes an entity type, for example. If it helps perf, we can add it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064154590
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
Good point! For loadAllTargetsOnPolicy, it’s currently only used when dropping a policy entity, so it shouldn’t be a very frequent call. There are a few directions we could explore to optimize this:
1. We do an additional `lookupEntity` query in `loadAllTargetsOnPolicy` to get the type code
2. We pass in `policyTypeCode` as another argument of `loadAllTargetsOnPolicy`
3. We add another index (policyCatalogId, policyId)
Personally, I’m leaning toward Option 1 since it avoids adding a new argument or index for something that’s relatively infrequent. Would love to hear any thoughts or suggestions you might have!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064171258
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
My current plan is that if we want to optimize this we can do another `lookupEntity` inside this to get the `policyTypeCode`. Please let me know if you have any thoughts on this and probably we can continue the discussion here: https://github.com/apache/polaris/pull/1468#discussion_r2064154590 : )
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062807141
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
I see so we want prefix search kind of a functionality, that makes sense then, let me revert that.
don't we then wanna have type_code in the middle ?
don't we wanna optimize this as well :
```
public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
@Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2061136265
##########
scripts/postgres/schema-v1-postgresql.sql:
##########
@@ -94,3 +94,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_catalog_id, policy_id, target_catalog_id, target_id);
Review Comment:
```suggestion
CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, target_catalog_id, target_id);
```
Would you mind helping add `policy_type_code` to the index as well? I realize this field is not currently indexed in the EclipseLink implementation, but including it could help support future features, such as efficiently retrieving all entities with a specific type of policy (e.g., data compaction policies).
cc @flyrain
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -727,6 +732,164 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.get(0));
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.get(0);
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
Review Comment:
In the case where there is an existing record with same target and same policy (i.e., same primary key), `writeToPolicyMappingRecord`, all attributes of the new record should replace the existing one. Hence I think we need an update statement in this case.
I just realized I made this mistake in EclipseLink Persistence Implementation which result in a primary key violation, I've opened a fix for that: https://github.com/apache/polaris/pull/1469
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064244635
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,179 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+ String insertQuery =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
Review Comment:
Maybe add a comment like
```
Only one policy of the same type can be attached to an entity when the policy is inheritable.
```
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,179 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+ String insertQuery =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ String updateQuery =
Review Comment:
Maybe add a comment like:
```
In case of the mapping exist, update the policy mapping with the new parameters.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062807141
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
I see so we want prefix search kind of a functionality, that makes sense then, let me revert that.
don't we then wanna have type_code in the middle ?
is this query we wanna optimize :
```
public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
@Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2061211631
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -727,6 +732,164 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.get(0));
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.get(0);
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ }
+
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateInsertQuery(modelPolicyMappingRecord, realmId);
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
Review Comment:
yes its just here for re-using the fetchPolicyMapping.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064255125
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, policy_type_code, target_catalog_id, target_id);
Review Comment:
```suggestion
CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, target_catalog_id, target_id);
```
Is the second `poicy_type_code` needed?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064255517
##########
extension/persistence/relational-jdbc/src/main/resources/h2/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters TEXT NOT NULL DEFAULT '{}',
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, policy_type_code, target_catalog_id, target_id);
Review Comment:
```suggestion
CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, target_catalog_id, target_id);
```
Same as above
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062801540
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
```suggestion
CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, target_catalog_id, target_id);
```
In this index, the policy related fields should be in the front otherwise it would be the same as the primary key.
##########
extension/persistence/relational-jdbc/src/main/resources/h2/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters TEXT NOT NULL DEFAULT '{}',
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
Similar as above
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2061150313
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -727,6 +732,164 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.get(0));
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.get(0);
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ }
+
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateInsertQuery(modelPolicyMappingRecord, realmId);
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
Review Comment:
Just to confirm, in reality this should not be possible due to the primary key constraint right?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064113966
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
Review Comment:
Nit: -> precondition.checkState()
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
Review Comment:
It doesn't do anything in case of non-inheritable policies.
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
Review Comment:
I think the logic would be like this:
```
if (inheritable) {
// check if the mapping exist
// throw if it does
}
// insert or update a new mapping record.
```
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
Just wanna to confirm: we are going to add policy type as a parameter, right? cc @HonahX
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
Review Comment:
Nit: using `var` to keep them in one line
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064252962
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
Thanks for the explanation! The main reason I’m hesitant to add `policyTypeCode` here is that I want to maintain the principle that "catalogId + entityId" should be sufficient to uniquely identify a policy. `policyTypeCode` should be used purely as an optimization rather than being necessary for correctness.
That said, I agree it makes sense to add it for optimization purposes. How about I open a follow-up PR for that change after this one is merged, so it doesn’t block the current work?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064259169
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, policy_type_code, target_catalog_id, target_id);
Review Comment:
No, apologies switching a couple of branches, this got added somehow, reverted
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062807141
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
I see so we want prefix search kind of a functionality, that makes sense then, let me revert that.
don't we wanna optimize this as well :
```
public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
@Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
```
or do we plan to send type_code in this to optimize these queries ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "singhpk234 (via GitHub)" <gi...@apache.org>.
singhpk234 commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2061212394
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -727,6 +732,164 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.get(0));
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.get(0);
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
Review Comment:
sounds good, added update query
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064190805
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
What if business layer got the policy type already? The major concern is the performance, esp. for postgres, adding another query instead mean we have to wrap 2 queries in a transaction(remember this method is supposed to be atomic), which is usually slower. It makes more sense to look up in either in business layer or metastore manager layer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "HonahX (via GitHub)" <gi...@apache.org>.
HonahX commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2062801540
##########
extension/persistence/relational-jdbc/src/main/resources/postgres/schema-v1.sql:
##########
@@ -91,3 +91,17 @@ CREATE TABLE IF NOT EXISTS principal_authentication_data (
);
COMMENT ON TABLE principal_authentication_data IS 'authentication data for client';
+
+DROP TABLE IF EXISTS policy_mapping_record;
+CREATE TABLE IF NOT EXISTS policy_mapping_record (
+ realm_id TEXT NOT NULL,
+ target_catalog_id BIGINT NOT NULL,
+ target_id BIGINT NOT NULL,
+ policy_type_code INTEGER NOT NULL,
+ policy_catalog_id BIGINT NOT NULL,
+ policy_id BIGINT NOT NULL,
+ parameters JSONB NOT NULL DEFAULT '{}'::JSONB,
+ PRIMARY KEY (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id)
+);
+
+CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, target_catalog_id, target_id, policy_type_code, policy_catalog_id, policy_id);
Review Comment:
```suggestion
CREATE INDEX IF NOT EXISTS idx_policy_mapping_record ON policy_mapping_record (realm_id, policy_type_code, policy_catalog_id, policy_id, target_catalog_id, target_id);
```
In this index, I think the policy related fields should be in the front otherwise it would be the same as the primary key.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [JDBC] Support Policy [polaris]
Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #1468:
URL: https://github.com/apache/polaris/pull/1468#discussion_r2064190805
##########
extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -701,6 +712,180 @@ public void deletePrincipalSecrets(
}
}
+ @Override
+ public void writeToPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ try {
+ datasourceOperations.runWithinTransaction(
+ statement -> {
+ PolicyType policyType = PolicyType.fromCode(record.getPolicyTypeCode());
+ Preconditions.checkArgument(
+ policyType != null, "Invalid policy type code: %s", record.getPolicyTypeCode());
+
+ String query =
+ generateInsertQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), realmId);
+ if (policyType.isInheritable()) {
+ List<PolarisPolicyMappingRecord> existingRecords =
+ loadPoliciesOnTargetByType(
+ callCtx,
+ record.getTargetCatalogId(),
+ record.getTargetId(),
+ record.getPolicyTypeCode());
+ if (existingRecords.size() > 1) {
+ throw new PolicyMappingAlreadyExistsException(existingRecords.getFirst());
+ } else if (existingRecords.size() == 1) {
+ PolarisPolicyMappingRecord existingRecord = existingRecords.getFirst();
+ if (existingRecord.getPolicyCatalogId() != record.getPolicyCatalogId()
+ || existingRecord.getPolicyId() != record.getPolicyId()) {
+ throw new PolicyMappingAlreadyExistsException(existingRecord);
+ }
+ Map<String, Object> updateClause =
+ Map.of(
+ "target_catalog_id",
+ record.getTargetCatalogId(),
+ "target_id",
+ record.getTargetId(),
+ "policy_type_code",
+ record.getPolicyTypeCode(),
+ "policy_id",
+ record.getPolicyId(),
+ "policy_catalog_id",
+ record.getPolicyCatalogId(),
+ "realm_id",
+ realmId);
+ query =
+ generateUpdateQuery(
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record), updateClause);
+ }
+ statement.executeUpdate(query);
+ }
+ return true;
+ });
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteFromPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx, @Nonnull PolarisPolicyMappingRecord record) {
+ ModelPolicyMappingRecord modelPolicyMappingRecord =
+ ModelPolicyMappingRecord.fromPolicyMappingRecord(record);
+ String query = generateDeleteQuery(modelPolicyMappingRecord, realmId);
+ try {
+ datasourceOperations.executeUpdate(query);
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to write to policy records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Override
+ public void deleteAllEntityPolicyMappingRecords(
+ @Nonnull PolarisCallContext callCtx,
+ @Nonnull PolarisEntityCore entity,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnTarget,
+ @Nonnull List<PolarisPolicyMappingRecord> mappingOnPolicy) {
+ try {
+ datasourceOperations.executeUpdate(
+ generateDeleteQueryForEntityPolicyMappingRecords(entity, realmId));
+ } catch (SQLException e) {
+ throw new RuntimeException(
+ String.format("Failed to delete policy mapping records due to %s", e.getMessage()), e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public PolarisPolicyMappingRecord lookupPolicyMappingRecord(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode,
+ long policyCatalogId,
+ long policyId) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "policy_id",
+ policyId,
+ "policy_catalog_id",
+ policyCatalogId,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ List<PolarisPolicyMappingRecord> results = fetchPolicyMappingRecords(query);
+ if (results.size() > 1) {
+ throw new IllegalStateException(
+ String.format("More than 1 policy %s for a given policy mapping", results.getFirst()));
+ }
+ return results.size() == 1 ? results.getFirst() : null;
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadPoliciesOnTargetByType(
+ @Nonnull PolarisCallContext callCtx,
+ long targetCatalogId,
+ long targetId,
+ int policyTypeCode) {
+ Map<String, Object> params =
+ Map.of(
+ "target_catalog_id",
+ targetCatalogId,
+ "target_id",
+ targetId,
+ "policy_type_code",
+ policyTypeCode,
+ "realm_id",
+ realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllPoliciesOnTarget(
+ @Nonnull PolarisCallContext callCtx, long targetCatalogId, long targetId) {
+ Map<String, Object> params =
+ Map.of("target_catalog_id", targetCatalogId, "target_id", targetId, "realm_id", realmId);
+ String query = generateSelectQuery(ModelPolicyMappingRecord.class, params);
+ return fetchPolicyMappingRecords(query);
+ }
+
+ @Nonnull
+ @Override
+ public List<PolarisPolicyMappingRecord> loadAllTargetsOnPolicy(
+ @Nonnull PolarisCallContext callCtx, long policyCatalogId, long policyId) {
Review Comment:
What if business layer got the policy type already? The major concern is the performance, esp. for postgres, adding another query inside means we have to wrap 2 queries in a transaction(remember this method is supposed to be atomic), which is usually slower. It makes more sense to look up in either in business layer or metastore manager layer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org