|
| 1 | +package io.github.dfa1.vortex.cli; |
| 2 | + |
| 3 | +import io.github.dfa1.vortex.core.array.BoolArray; |
| 4 | +import io.github.dfa1.vortex.core.array.ByteArray; |
| 5 | +import io.github.dfa1.vortex.core.array.DoubleArray; |
| 6 | +import io.github.dfa1.vortex.core.array.FloatArray; |
| 7 | +import io.github.dfa1.vortex.core.array.IntArray; |
| 8 | +import io.github.dfa1.vortex.core.array.LongArray; |
| 9 | +import io.github.dfa1.vortex.core.array.ShortArray; |
| 10 | +import io.github.dfa1.vortex.core.array.VarBinArray; |
| 11 | +import io.github.dfa1.vortex.core.array.Array; |
| 12 | +import io.github.dfa1.vortex.csv.CsvExporter; |
| 13 | +import io.github.dfa1.vortex.csv.ExportOptions; |
| 14 | +import io.github.dfa1.vortex.csv.RowPredicate; |
| 15 | +import io.github.dfa1.vortex.scan.RowFilter; |
| 16 | +import io.github.dfa1.vortex.scan.ScanOptions; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.OutputStreamWriter; |
| 20 | +import java.io.Writer; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +final class FilterCommand { |
| 30 | + |
| 31 | + private FilterCommand() { |
| 32 | + } |
| 33 | + |
| 34 | + private enum Op { GT, GTE, LT, LTE, EQ } |
| 35 | + |
| 36 | + private record ParsedFilter(String column, Op op, Comparable<?> value) {} |
| 37 | + |
| 38 | + private static final Pattern EXPR = Pattern.compile( |
| 39 | + "^(\\w[\\w.]*?)\\s*(>=|<=|==|>|<|=)\\s*(.+)$"); |
| 40 | + |
| 41 | + static int run(String[] args) { |
| 42 | + if (args.length < 3) { |
| 43 | + System.err.println("usage: filter <file.vortex> <expr> (e.g. \"price >= 100\")"); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + Path path = Path.of(args[1]); |
| 47 | + if (!Files.exists(path)) { |
| 48 | + System.err.println("file not found: " + path); |
| 49 | + return 2; |
| 50 | + } |
| 51 | + String expr = String.join(" ", Arrays.asList(args).subList(2, args.length)); |
| 52 | + ParsedFilter pf; |
| 53 | + try { |
| 54 | + pf = parseFilter(expr); |
| 55 | + } catch (IllegalArgumentException e) { |
| 56 | + System.err.println("error: " + e.getMessage()); |
| 57 | + return 1; |
| 58 | + } |
| 59 | + ScanOptions scanOptions = new ScanOptions(List.of(), toRowFilter(pf), ScanOptions.NO_LIMIT); |
| 60 | + RowPredicate rowPred = toRowPredicate(pf); |
| 61 | + try { |
| 62 | + Writer stdout = new OutputStreamWriter(System.out, StandardCharsets.UTF_8); |
| 63 | + CsvExporter.exportCsvFiltered(path, stdout, ExportOptions.defaults(), scanOptions, rowPred); |
| 64 | + stdout.flush(); |
| 65 | + return 0; |
| 66 | + } catch (IOException e) { |
| 67 | + System.err.println("error: " + e.getMessage()); |
| 68 | + return 3; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static ParsedFilter parseFilter(String expr) { |
| 73 | + Matcher m = EXPR.matcher(expr.trim()); |
| 74 | + if (!m.matches()) { |
| 75 | + throw new IllegalArgumentException("invalid filter expression: \"" + expr |
| 76 | + + "\" expected: col op value (op: >, >=, <, <=, =, ==)"); |
| 77 | + } |
| 78 | + String col = m.group(1); |
| 79 | + Op op = switch (m.group(2)) { |
| 80 | + case ">" -> Op.GT; |
| 81 | + case ">=" -> Op.GTE; |
| 82 | + case "<" -> Op.LT; |
| 83 | + case "<=" -> Op.LTE; |
| 84 | + case "=", "==" -> Op.EQ; |
| 85 | + default -> throw new IllegalArgumentException("unknown operator: " + m.group(2)); |
| 86 | + }; |
| 87 | + Comparable<?> value = parseValue(m.group(3).trim()); |
| 88 | + return new ParsedFilter(col, op, value); |
| 89 | + } |
| 90 | + |
| 91 | + private static Comparable<?> parseValue(String raw) { |
| 92 | + try { |
| 93 | + return Long.parseLong(raw); |
| 94 | + } catch (NumberFormatException ignored) { |
| 95 | + } |
| 96 | + try { |
| 97 | + return Double.parseDouble(raw); |
| 98 | + } catch (NumberFormatException ignored) { |
| 99 | + } |
| 100 | + if ("true".equalsIgnoreCase(raw)) { |
| 101 | + return Boolean.TRUE; |
| 102 | + } |
| 103 | + if ("false".equalsIgnoreCase(raw)) { |
| 104 | + return Boolean.FALSE; |
| 105 | + } |
| 106 | + return raw; |
| 107 | + } |
| 108 | + |
| 109 | + /// Zone-map RowFilter for chunk pruning. Conservative for strict ops (> / <): |
| 110 | + /// uses Gte/Lte which may keep one extra chunk at the boundary, but row-level |
| 111 | + /// predicate corrects that. |
| 112 | + private static RowFilter toRowFilter(ParsedFilter pf) { |
| 113 | + return switch (pf.op()) { |
| 114 | + case GT, GTE -> RowFilter.gte(pf.column(), pf.value()); |
| 115 | + case LT, LTE -> RowFilter.lte(pf.column(), pf.value()); |
| 116 | + case EQ -> RowFilter.eq(pf.column(), pf.value()); |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + private static RowPredicate toRowPredicate(ParsedFilter pf) { |
| 121 | + return (chunk, rowIdx) -> { |
| 122 | + Array arr = chunk.column(pf.column()); |
| 123 | + if (arr == null) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + int cmp = compareValue(arr, rowIdx, pf.value()); |
| 127 | + return switch (pf.op()) { |
| 128 | + case GT -> cmp > 0; |
| 129 | + case GTE -> cmp >= 0; |
| 130 | + case LT -> cmp < 0; |
| 131 | + case LTE -> cmp <= 0; |
| 132 | + case EQ -> cmp == 0; |
| 133 | + }; |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + @SuppressWarnings("unchecked") |
| 138 | + private static int compareValue(Array arr, long rowIdx, Comparable<?> value) { |
| 139 | + return switch (arr) { |
| 140 | + case LongArray la -> compareNumeric(la.getLong(rowIdx), value); |
| 141 | + case IntArray ia -> compareNumeric(ia.getInt(rowIdx), value); |
| 142 | + case ShortArray sa -> compareNumeric(sa.getShort(rowIdx), value); |
| 143 | + case ByteArray ba -> compareNumeric(ba.getByte(rowIdx), value); |
| 144 | + case DoubleArray da -> compareDouble(da.getDouble(rowIdx), value); |
| 145 | + case FloatArray fa -> compareDouble(fa.getFloat(rowIdx), value); |
| 146 | + case BoolArray ba -> Boolean.compare(ba.getBoolean(rowIdx), (Boolean) value); |
| 147 | + case VarBinArray va -> { |
| 148 | + String v = new String(va.getBytes(rowIdx), StandardCharsets.UTF_8); |
| 149 | + yield v.compareTo((String) value); |
| 150 | + } |
| 151 | + default -> throw new IllegalArgumentException( |
| 152 | + "filter not supported for column type: " + arr.getClass().getSimpleName()); |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + private static int compareNumeric(long colVal, Comparable<?> value) { |
| 157 | + if (value instanceof Long l) { |
| 158 | + return Long.compare(colVal, l); |
| 159 | + } |
| 160 | + return Double.compare(colVal, (Double) value); |
| 161 | + } |
| 162 | + |
| 163 | + private static int compareDouble(double colVal, Comparable<?> value) { |
| 164 | + if (value instanceof Long l) { |
| 165 | + return Double.compare(colVal, l); |
| 166 | + } |
| 167 | + return Double.compare(colVal, (Double) value); |
| 168 | + } |
| 169 | +} |
0 commit comments