CQL Heuristics Calculator#1620
Conversation
…sion-replacement-v2
|
|
||
| private final CassandraOperationEvaluator evaluator = new CassandraOperationEvaluator(); | ||
|
|
||
| public double computeDistance(String cqlQuery, Iterable<Map<String, Object>> allRows) { |
There was a problem hiding this comment.
add an abstraction for allRows.
|
|
||
| private enum ComparisonType { EQUALS, GT, GTE, LT, LTE } | ||
|
|
||
| public Truthness evaluate(CqlQueryOperation op, Map<String, Object> row) { |
|
|
||
| import static org.evomaster.client.java.controller.cassandra.CassandraHeuristicsCalculator.*; | ||
|
|
||
| public class CassandraOperationEvaluator { |
| return any(op.getValue(), toElementList(rawCol)); | ||
| } | ||
|
|
||
| private Truthness evaluateContainsKey(ContainsKeyOperation<?> op, Map<String, Object> row) { |
|
|
||
| public Truthness evaluate(CqlQueryOperation op, Map<String, Object> row) { | ||
| if (op instanceof AndOperation) | ||
| return evaluateAnd((AndOperation) op, row); |
There was a problem hiding this comment.
add {...} for each if. Avoid one if-liners.
|
|
||
| private static String normalizeColumnName(String name) { | ||
| if (name == null) return null; | ||
| if (name.startsWith("\"") && name.endsWith("\"")) { |
There was a problem hiding this comment.
replace " with constant value
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| private static String normalizeColumnName(String name) { |
There was a problem hiding this comment.
add Javadoc explaining what normalization means for a column name
| this.nanos = nanos; | ||
| } | ||
|
|
||
| public static CqlDurationLiteral parse(String text) { |
There was a problem hiding this comment.
Move this code to a specific parser for this format.
- Small refactorings to follow dev guidelines - Refactor parsing of Duration literals and reorganise packages - Unify distance calculation for strings - Refactor operation evaluator - Add javadoc for calculator and CassandraRow abstraction
- More refactorings in parser utils - Add missing docs in parser utils
| return compareDuration(rowValue, literalValue, comparisonType); | ||
| } | ||
|
|
||
| SimpleLogger.uniqueWarn("CassandraHeuristicsCalculator: unsupported type " |
There was a problem hiding this comment.
If the type is not unsupported it should throw an exception
| } | ||
| } | ||
|
|
||
| private static Instant parseTimestampString(String rowValue) { |
There was a problem hiding this comment.
Please improve the readability of this method. Consider adding different methods to try to parse different formats.
| throw new IllegalArgumentException("Cannot parse timestamp string: " + rowValue); | ||
| } | ||
|
|
||
| private static boolean isCqlDuration(Object rowValue) { |
There was a problem hiding this comment.
I am a bit skeptical of using reflection here to handle CqlDuration. What do you think of using java.time.temporal.TemporalAmount instead of CqlDuration ? Can we get rid of reflection ?
| .equals("com.datastax.oss.driver.api.core.data.CqlDuration"); | ||
| } | ||
|
|
||
| private static int getDurationMonths(Object durationRowValue) throws Exception { |
There was a problem hiding this comment.
use java.time.temporal.TemporalAmount?
| return (int) durationRowValue.getClass().getMethod("getMonths").invoke(durationRowValue); | ||
| } | ||
|
|
||
| private static int getDurationDays(Object durationRowValue) throws Exception { |
There was a problem hiding this comment.
use java.time.temporal.TemporalAmount?
| TruthnessUtils.getEqualityTruthness(nanos, literalDurationValue.nanos) | ||
| ); | ||
| } catch (Exception e) { | ||
| return FALSE_TRUTHNESS; |
There was a problem hiding this comment.
why a try-catch here? What are the expected exceptions at this point?
| * {@code mo}, or {@code ms} consuming {@code m}. | ||
| */ | ||
| private static final Pattern TOKEN = | ||
| Pattern.compile("(\\d+)(mo|ms|µs|us|ns|y|d|h|m|s)", Pattern.CASE_INSENSITIVE); |
There was a problem hiding this comment.
replace mo, ms, us, µs, ns, y, d, h, m, s with corresponding constant values
| private static CqlDurationLiteral parseIso(String isoDuration) { | ||
| // Split on T (or t) to separate date and time parts | ||
| String upper = isoDuration.toUpperCase(); | ||
| int tIndex = upper.indexOf('T'); |
There was a problem hiding this comment.
replace 'T' with constant value
| long value = Long.parseLong(m.group(1)); | ||
| String unit = m.group(2).toLowerCase(); | ||
| switch (unit) { | ||
| case "y": months += (int)(value * 12); break; |
There was a problem hiding this comment.
replace these strings with constant values
| * @return {@code true} if {@code cqlCommand} is a SELECT statement | ||
| */ | ||
| public static boolean isSelect(String cqlCommand) { | ||
| return cqlCommand.trim().toUpperCase().startsWith("SELECT"); |
There was a problem hiding this comment.
replace "SELECT", "UPDATE", etc with constants
…ent-v2 More CqlSession Replacements
Added CassandraHeuristicsCalculator/CassandraOperationEvaluator to compute a distance heuristic for CQL WHERE clauses
Distance calculation for each operator/data type follows the spec from this document