Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Mar 16, 2024
1 parent 9505ebd commit c38ac66
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ public class ExpressionNormalization extends ExpressionRewrite {
// from_unixtime(timestamp, 'yyyyMMdd') to 'yyyyMMdd'
public static final List<ExpressionRewriteRule> NORMALIZE_REWRITE_RULES = ImmutableList.of(
bottomUp(
new ReplaceVariableByLiteral(),
new SupportJavaDateFormatter(),
new NormalizeBinaryPredicatesRule(),
new InPredicateDedup(),
new InPredicateToEqualToRule(),
new SimplifyNotExprRule(),
new SimplifyArithmeticRule()
ReplaceVariableByLiteral.INSTANCE,
SupportJavaDateFormatter.INSTANCE,
NormalizeBinaryPredicatesRule.INSTANCE,
InPredicateDedup.INSTANCE,
InPredicateToEqualToRule.INSTANCE,
SimplifyNotExprRule.INSTANCE,
SimplifyArithmeticRule.INSTANCE
),
FoldConstantRule.INSTANCE,
bottomUp(
new SimplifyCastRule(),
new DigitalMaskingConvert(),
new SimplifyArithmeticComparisonRule(),
new CheckCast()
SimplifyCastRule.INSTANCE,
DigitalMaskingConvert.INSTANCE,
SimplifyArithmeticComparisonRule.INSTANCE,
CheckCast.INSTANCE
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* check cast valid
*/
public class CheckCast implements ExpressionPatternRuleFactory {
public static CheckCast INSTANCE = new CheckCast();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* Convert DigitalMasking to Concat
*/
public class DigitalMaskingConvert implements ExpressionPatternRuleFactory {
public static DigitalMaskingConvert INSTANCE = new DigitalMaskingConvert();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* where A in (x, x) ==> where A in (x)
*/
public class InPredicateDedup implements ExpressionPatternRuleFactory {
public static final InPredicateDedup INSTANCE = new InPredicateDedup();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* They are same processes, so must change synchronously.
*/
public class InPredicateToEqualToRule implements ExpressionPatternRuleFactory {
public static final InPredicateToEqualToRule INSTANCE = new InPredicateToEqualToRule();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
* 5 > id -> id < 5
*/
public class NormalizeBinaryPredicatesRule implements ExpressionPatternRuleFactory {
public static final NormalizeBinaryPredicatesRule INSTANCE = new NormalizeBinaryPredicatesRule();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* replace varaible to real expression
*/
public class ReplaceVariableByLiteral implements ExpressionPatternRuleFactory {
public static ReplaceVariableByLiteral INSTANCE = new ReplaceVariableByLiteral();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
* a / -2 > 1 => a < -2
*/
public class SimplifyArithmeticComparisonRule implements ExpressionPatternRuleFactory {
public static SimplifyArithmeticComparisonRule INSTANCE = new SimplifyArithmeticComparisonRule();

// don't rearrange multiplication because divide may loss precision
private static final Map<Class<? extends Expression>, Class<? extends Expression>> REARRANGEMENT_MAP = ImmutableMap
.<Class<? extends Expression>, Class<? extends Expression>>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
* TODO: handle cases like: '1 - IA < 1' to 'IA > 0'
*/
public class SimplifyArithmeticRule implements ExpressionPatternRuleFactory {
public static final SimplifyArithmeticRule INSTANCE = new SimplifyArithmeticRule();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

/** SupportJavaDateFormatter2 */
public class SupportJavaDateFormatter implements ExpressionPatternRuleFactory {
public static final SupportJavaDateFormatter INSTANCE = new SupportJavaDateFormatter();

@Override
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testNotRewrite() {
@Test
void testNormalizeExpressionRewrite() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new NormalizeBinaryPredicatesRule())
ExpressionRewrite.bottomUp(NormalizeBinaryPredicatesRule.INSTANCE)
));

assertRewrite("1 = 1", "1 = 1");
Expand Down Expand Up @@ -165,7 +165,7 @@ void testExtractCommonFactorRewrite() {
@Test
void testInPredicateToEqualToRule() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(new InPredicateToEqualToRule())
bottomUp(InPredicateToEqualToRule.INSTANCE)
));

assertRewrite("a in (1)", "a = 1");
Expand All @@ -183,7 +183,7 @@ void testInPredicateToEqualToRule() {
@Test
void testInPredicateDedup() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(new InPredicateDedup())
bottomUp(InPredicateDedup.INSTANCE)
));

assertRewrite("a in (1, 2, 1, 2)", "a in (1, 2)");
Expand All @@ -192,7 +192,7 @@ void testInPredicateDedup() {
@Test
void testSimplifyCastRule() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(new SimplifyCastRule())
bottomUp(SimplifyCastRule.INSTANCE)
));

// deduplicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SimplifyArithmeticRuleTest extends ExpressionRewriteTestHelper {
@Test
void testSimplifyArithmetic() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule()),
bottomUp(SimplifyArithmeticRule.INSTANCE),
FunctionBinder.INSTANCE,
FoldConstantRule.INSTANCE
));
Expand All @@ -55,7 +55,7 @@ void testSimplifyArithmetic() {
@Test
void testSimplifyArithmeticRuleOnly() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule())
bottomUp(SimplifyArithmeticRule.INSTANCE)
));

// add and subtract
Expand Down Expand Up @@ -94,10 +94,12 @@ void testSimplifyArithmeticRuleOnly() {
@Test
void testSimplifyArithmeticComparison() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule()),
bottomUp(SimplifyArithmeticRule.INSTANCE),
FoldConstantRule.INSTANCE,
ExpressionRewrite.bottomUp(new SimplifyArithmeticComparisonRule()),
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule()),
bottomUp(
SimplifyArithmeticComparisonRule.INSTANCE,
SimplifyArithmeticRule.INSTANCE
),
FunctionBinder.INSTANCE,
FoldConstantRule.INSTANCE
));
Expand Down Expand Up @@ -134,10 +136,12 @@ void testSimplifyArithmeticComparison() {
@Test
void testSimplifyDateTimeComparison() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule()),
bottomUp(SimplifyArithmeticRule.INSTANCE),
FoldConstantRule.INSTANCE,
ExpressionRewrite.bottomUp(new SimplifyArithmeticComparisonRule()),
ExpressionRewrite.bottomUp(new SimplifyArithmeticRule()),
bottomUp(
SimplifyArithmeticComparisonRule.INSTANCE,
SimplifyArithmeticRule.INSTANCE
),
FunctionBinder.INSTANCE,
FoldConstantRule.INSTANCE
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testProcess() {
Map<String, Slot> nameToSlot = new HashMap<>();
nameToSlot.put("a", new SlotReference("a", IntegerType.INSTANCE));
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyArithmeticComparisonRule())
ExpressionRewrite.bottomUp(SimplifyArithmeticComparisonRule.INSTANCE)
));
assertRewriteAfterSimplify("a + 1 > 1", "a > cast((1 - 1) as INT)", nameToSlot);
assertRewriteAfterSimplify("a - 1 > 1", "a > cast((1 + 1) as INT)", nameToSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SimplifyCastRuleTest extends ExpressionRewriteTestHelper {
@Test
public void testSimplify() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
ExpressionRewrite.bottomUp(new SimplifyCastRule()))
ExpressionRewrite.bottomUp(SimplifyCastRule.INSTANCE))
);
assertRewriteAfterSimplify("CAST('1' AS STRING)", "'1'", StringType.INSTANCE);
assertRewriteAfterSimplify("CAST('1' AS VARCHAR)", "'1'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SimplifyComparisonPredicateTest extends ExpressionRewriteTestHelper {
void testSimplifyComparisonPredicateRule() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(
new SimplifyCastRule(),
SimplifyCastRule.INSTANCE,
SimplifyComparisonPredicate.INSTANCE
)
));
Expand Down Expand Up @@ -93,7 +93,7 @@ void testSimplifyComparisonPredicateRule() {
void testDateTimeV2CmpDateTimeV2() {
executor = new ExpressionRuleExecutor(ImmutableList.of(
bottomUp(
new SimplifyCastRule(),
SimplifyCastRule.INSTANCE,
SimplifyComparisonPredicate.INSTANCE
)
));
Expand Down

0 comments on commit c38ac66

Please sign in to comment.