Skip to content

Commit 5fca9d4

Browse files
committed
Added distinct.
1 parent 0e0c4f4 commit 5fca9d4

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

README.org

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,19 @@ steve 9 4 3 4
329329
steve 10 6 3 10
330330
#+end_src
331331

332+
In addition to all that, you can also use vellum:distinct to remove duplicated rows from data-frame.
333+
334+
#+BEGIN_SRC
335+
(vellum:transform (vellum:to-table '((1 2) (1 2) (1 3)) :columns '(a b))
336+
(vellum:bind-row (a b)
337+
(vellum:distinct a b)))
338+
339+
A B
340+
====
341+
1 2
342+
1 3
343+
#+END_SRC
344+
332345
** UNNEST
333346
It is quite common to require transforming data by replacing single rows holding lists into multiple rows holding atoms. Vellum has UNNEST function for this precise task.
334347

src/api/package.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#:aggregated-value
5151
#:alter-columns
5252
#:at
53+
#:distinct
5354
#:retry
5455
#:bind-row
5556
#:bind-row-closure

src/table/macros.lisp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@
4444
nil)
4545

4646

47+
(defmacro distinct (&rest data)
48+
(declare (ignore data))
49+
nil)
50+
51+
4752
(defclass aggregation-results ()
4853
((%aggregators :initarg :aggregators
4954
:reader aggregators)
5055
(%aggregation-column-names :initarg :aggregation-column-names
51-
:reader aggregation-column-names)))
56+
:reader aggregation-column-names)))
5257

5358

5459
(defclass group-by-aggregation-results ()
@@ -64,6 +69,7 @@
6469
(let* ((gathered-constructor-forms (make-hash-table :test 'eql))
6570
(gathered-constructor-variables (make-hash-table :test 'eql))
6671
(gathered-group-by-variables (list))
72+
(gathered-distinct-variables (make-hash-table :test 'eql))
6773
(group-names (list))
6874
(pre-form nil)
6975
(aggregation-symbol (gensym))
@@ -94,6 +100,9 @@
94100
,what
95101
,constructor-form
96102
,into)))
103+
((and (listp pre-form) (eq (car pre-form) 'vellum.table:distinct))
104+
(lret ((symbol (gensym)))
105+
(setf (gethash symbol gathered-distinct-variables) (rest pre-form))))
97106
((and (listp pre-form) (eq (car pre-form) 'vellum.table:group-by))
98107
(iterate
99108
(for elt in (rest pre-form))
@@ -118,6 +127,7 @@
118127
gathered-constructor-forms
119128
aggregation-symbol
120129
extract-value-symbol
130+
gathered-distinct-variables
121131
(nreverse gathered-group-by-variables)
122132
(nreverse group-names))))
123133

@@ -127,6 +137,7 @@
127137
gathered-constructor-forms
128138
aggregation-symbol
129139
extract-value-symbol
140+
gathered-distinct-variables
130141
gathered-group-by-variables
131142
group-names)
132143
(let* ((!grouped-aggregators (gensym)))
@@ -154,12 +165,18 @@
154165
(,!aggregators (or (gethash ',result-name ,!group)
155166
(cl-ds.alg.meta:call-constructor ,constructor-form))))
156167
(cl-ds.alg.meta:extract-result ,!aggregators))))))
168+
((gethash f gathered-distinct-variables)
169+
(let ((forms (shiftf (gethash f gathered-distinct-variables) nil)))
170+
(if (= 1 (length forms))
171+
`(vellum:drop-row-when (shiftf (gethash ,(first forms) ,f) t))
172+
`(vellum:drop-row-when (shiftf (gethash (list ,@forms) ,f) t)))))
157173
(t f)))
158174
result)
159175
gathered-constructor-variables
160176
gathered-constructor-forms
161177
aggregation-symbol
162178
extract-value-symbol
179+
gathered-distinct-variables
163180
gathered-group-by-variables
164181
group-names
165182
!grouped-aggregators)))
@@ -205,6 +222,7 @@
205222
constructor-forms
206223
aggregation-symbol
207224
extract-value-symbol
225+
gathered-distinct-variables
208226
gathered-group-by-variables
209227
group-names
210228
!grouped-aggregators)
@@ -218,9 +236,15 @@
218236
gensyms))
219237
env))
220238
(let-constructors
221-
(iterate
222-
(for (key value) in-hashtable constructor-variables)
223-
(collecting (list value `(cl-ds.alg.meta:call-constructor ,(gethash key constructor-forms)))))))
239+
(let ((result (list)))
240+
(iterate
241+
(for (key value) in-hashtable constructor-variables)
242+
(push (list value `(cl-ds.alg.meta:call-constructor ,(gethash key constructor-forms)))
243+
result))
244+
(iterate
245+
(for (key value) in-hashtable gathered-distinct-variables)
246+
(push (list key `(make-hash-table :test 'equal)) result))
247+
result)))
224248
(declare (ignore extract-value-symbol aggregation-symbol))
225249
(unless (= (length (remove-duplicates group-names :test #'equal))
226250
(length group-names))

src/table/package.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#:make-setfable-table-row
3737
#:iterator
3838
#:make-table
39+
#:distinct
3940
#:make-table*
4041
#:no-transformation
4142
#:aggregate

0 commit comments

Comments
 (0)