-
Notifications
You must be signed in to change notification settings - Fork 330
Add data compaction policy schema and its validator #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
polaris-core/src/main/java/org/apache/polaris/core/policy/BasePolicyValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.policy; | ||
|
|
||
| import java.io.InputStream; | ||
| import org.everit.json.schema.Schema; | ||
| import org.everit.json.schema.ValidationException; | ||
| import org.everit.json.schema.loader.SchemaLoader; | ||
| import org.json.JSONObject; | ||
| import org.json.JSONTokener; | ||
|
|
||
| public class BasePolicyValidator implements PolicyValidator { | ||
| @Override | ||
| public boolean validate(Policy policy) { | ||
| try { | ||
| InputStream schemaStream = | ||
| getResourceAsStream("schemas/policies/system/data-compaction/2025-02-03.json"); | ||
| JSONObject rawSchema = new JSONObject(new JSONTokener(schemaStream)); | ||
| Schema schema = SchemaLoader.load(rawSchema); | ||
|
|
||
| var jsonData = new JSONObject(new JSONTokener(policy.content)); | ||
|
|
||
| schema.validate(jsonData); | ||
| return true; | ||
| } catch (ValidationException e) { | ||
| // The JSON failed validation; handle error information | ||
| System.out.println("JSON validation failed: " + e.getMessage()); | ||
| // You can also inspect e.getCausingExceptions() for detail on multiple errors | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static InputStream getResourceAsStream(String fileName) { | ||
| return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
polaris-core/src/main/java/org/apache/polaris/core/policy/Policy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.policy; | ||
|
|
||
| public class Policy { | ||
| String content; | ||
|
|
||
| public Policy(String content) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| public String getContent() { | ||
| return content; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
polaris-core/src/main/java/org/apache/polaris/core/policy/PolicyValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.policy; | ||
|
|
||
| public interface PolicyValidator { | ||
| boolean validate(Policy policy); | ||
| } |
40 changes: 40 additions & 0 deletions
40
polaris-core/src/main/resources/schemas/policies/system/data-compaction/2025-02-03.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "$id": "https://polaris.apache.org/schemas/policies/system/data-compaction/2025-02-03.json", | ||
| "title": "Data Compaction Policy", | ||
| "description": "Inheritable Polaris policy schema for Iceberg table data compaction.", | ||
| "type": "object", | ||
| "properties": { | ||
| "version": { | ||
| "type": "string", | ||
| "const": "2025-02-03", | ||
| "description": "Schema version." | ||
| }, | ||
| "enable": { | ||
| "type": "boolean", | ||
| "description": "Enable or disable data compaction." | ||
| }, | ||
| "target_file_size_bytes": { | ||
| "type": "number", | ||
| "description": "Target data file size in bytes." | ||
| }, | ||
| "config": { | ||
| "type": "object", | ||
| "description": "A map containing custom configuration properties. Please note that interoperability is not guaranteed.", | ||
| "additionalProperties": {} | ||
| } | ||
| }, | ||
| "required": ["enable"], | ||
| "additionalProperties": false, | ||
| "examples": [ | ||
| { | ||
| "version": "2025-02-03", | ||
| "enable": true, | ||
| "target_file_size_bytes": 134217728, | ||
| "config": { | ||
| "compaction_strategy": "bin-pack", | ||
| "max-concurrent-file-group-rewrites": 5, | ||
| "my-key": "my-value" | ||
| } | ||
| } | ||
| ] | ||
| } |
65 changes: 65 additions & 0 deletions
65
polaris-core/src/test/java/org/apache/polaris/core/policy/BasePolicyValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.core.policy; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class BasePolicyValidatorTest { | ||
| private BasePolicyValidator validator; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| validator = new BasePolicyValidator(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidateValidPolicy() { | ||
| var validJson = "{\"enable\": False}"; | ||
| var result = validator.validate(new Policy(validJson)); | ||
| assertThat(result).isTrue(); | ||
|
|
||
| validJson = "{\"enable\": true, \"target_file_size_bytes\": 12342}"; | ||
| result = validator.validate(new Policy(validJson)); | ||
| assertThat(result).isTrue(); | ||
|
|
||
| validJson = "{\"version\":\"2025-02-03\", \"enable\": true, \"target_file_size_bytes\": 12342}"; | ||
| result = validator.validate(new Policy(validJson)); | ||
| assertThat(result).isTrue(); | ||
|
|
||
| validJson = "{\"enable\": true, \"config\": {\"key1\": \"value1\", \"key2\": true}}"; | ||
| result = validator.validate(new Policy(validJson)); | ||
| assertThat(result).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInValidateValidPolicy() { | ||
| // missing required key | ||
| var inValidJson = "{}"; | ||
| var result = validator.validate(new Policy(inValidJson)); | ||
| assertThat(result).isFalse(); | ||
|
|
||
| // invalid keys | ||
| inValidJson = "{\"enable\": true, \"invalid_key\": 12342}"; | ||
| result = validator.validate(new Policy(inValidJson)); | ||
| assertThat(result).isFalse(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be out-of-scope of this PR: Will we extend this object to include other info like
name,type,description?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's for the demo only and will be removed before this is ready for review.