"Implement ByteSize and TimeDuration Parsers with Aggregate-Stats Directive". #972
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.
This pull request enhances CDAP Wrangler with native support for parsing byte size and time duration units and introduces an aggregate-stats directive to compute totals from source columns. The changes cover grammar modifications, API updates, core parser adjustments, directive implementation, and comprehensive testing as specified in the Software Engineer Intern Assignment.
Changes Overview
Grammar Modifications
Directives.g4:
Added lexer tokens BYTE_SIZE (supporting B, KB, MB, GB, TB) and TIME_DURATION (supporting ns, ms, s, m, h).
Introduced parser rules (e.g., byteSizeArg and timeDurationArg) to capture these tokens.
Regenerated ANTLR parser code using mvnd compile.
API Updates (wrangler-api module)
Implemented ByteSize.java and TimeDuration.java to parse input strings like "10KB" and "100ms" into canonical values (bytes and nanoseconds).
Updated relevant token definitions (in TokenType and UsageDefinition) to support the new types.
Fixed Checkstyle issues in wrangler-api.
Core Parser Updates (wrangler-core module)
Modified RecipeVisitor.java to handle the new parser rules via corresponding visitor methods (e.g., visitByteSizeArg and visitTimeDurationArg).
Ensured that recipes that use the new tokens parse correctly.
New Directive Implementation (wrangler-core module)
Created AggregateStatsDirective.java in the package io.cdap/directives/aggregates.
5. Testing (wrangler-core module)
Unit Tests for Parsers:
Added ByteSizeTest.java and TimeDurationTest.java to validate parsing of various inputs such as "10kb", "1.5MB", "5ms", and "2.1s".
Parser Tests:
Updated GrammarBasedParserTest.java / RecipeCompilerTest.java to ensure recipes that use new syntax compile correctly and that invalid syntax is rejected.
Directive Testing:
Added AggregateStatsDirectiveTest.java (or AggregateStatsTest.java) using TestingRig.execute(recipe, rows) to simulate log or transaction data, verifying that:
The aggregate output row contains the correct value for total size (in MB) and total time (in seconds).
Aggregation is performed accurately across multiple rows.