-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f0d0bc8
Showing
67 changed files
with
2,285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.22 | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Martin Kopka Drlík | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# rex | ||
|
||
Exploring incomplete data for relational database model. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package box | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"iter" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
type relationBox struct { | ||
schema []string | ||
rows []map[string]string | ||
max map[string]int | ||
} | ||
|
||
func Relation(schema []string, tuples iter.Seq[map[string]any]) interface{ String() string } { | ||
rb := &relationBox{ | ||
schema: schema, | ||
rows: []map[string]string{}, | ||
max: map[string]int{}, | ||
} | ||
for _, s := range rb.schema { | ||
rb.max[s] = utf8.RuneCountInString(s) | ||
} | ||
for t := range tuples { | ||
rb.addRow(t) | ||
} | ||
return rb | ||
} | ||
|
||
func (t *relationBox) addRow(tuple map[string]any) { | ||
str := func(v any) string { return fmt.Sprintf("%v", v) } | ||
row := map[string]string{} | ||
for k, v := range tuple { | ||
s := str(v) | ||
if l := utf8.RuneCountInString(s); t.max[k] < l { | ||
t.max[k] = l | ||
} | ||
row[k] = s | ||
} | ||
t.rows = append(t.rows, row) | ||
} | ||
|
||
func (t *relationBox) String() string { | ||
sb := &strings.Builder{} | ||
t.writeTop(sb) | ||
t.writeHeader(sb) | ||
if len(t.rows) > 0 { | ||
t.writeSeparator(sb) | ||
t.writeRows(sb) | ||
} | ||
t.writeBottom(sb) | ||
return sb.String() | ||
} | ||
|
||
func (t *relationBox) writeTop(w io.Writer) { | ||
// ┏━━━━━━┯━━━━━━┓ | ||
t.writeRow(w, "┏", "┯", "┓", func(s string) string { | ||
return strings.Repeat("━", t.max[s]+2) | ||
}) | ||
} | ||
|
||
func (t *relationBox) writeHeader(w io.Writer) { | ||
// ┃ x │ y ┃ | ||
t.writeRow(w, "┃", "│", "┃", func(s string) string { | ||
return fmt.Sprintf(" %s ", t.pad(s, s)) | ||
}) | ||
} | ||
|
||
func (t *relationBox) writeSeparator(w io.Writer) { | ||
// ┠──────┼──────┨ | ||
t.writeRow(w, "┠", "┼", "┨", func(s string) string { | ||
return strings.Repeat("─", t.max[s]+2) | ||
}) | ||
} | ||
|
||
func (t *relationBox) writeRows(w io.Writer) { | ||
for _, row := range t.rows { | ||
// ┃ 2023 │ 2024 ┃ | ||
t.writeRow(w, "┃", "│", "┃", func(s string) string { | ||
v, ok := row[s] | ||
return fmt.Sprintf(" %s ", t.pad(s, val(v, ok))) | ||
}) | ||
} | ||
} | ||
|
||
func (t *relationBox) writeBottom(w io.Writer) { | ||
// ┗━━━━━━┷━━━━━━┛ | ||
t.writeRow(w, "┗", "┷", "┛", func(s string) string { | ||
return strings.Repeat("━", t.max[s]+2) | ||
}) | ||
} | ||
|
||
func val(v string, ok bool) string { | ||
if ok { | ||
return v | ||
} | ||
return "?" | ||
} | ||
|
||
func (t *relationBox) writeRow(w io.Writer, left, middle, right string, valueFunc func(string) string) { | ||
fmt.Fprint(w, left) | ||
for i, s := range t.schema { | ||
if i > 0 { | ||
fmt.Fprint(w, middle) | ||
} | ||
fmt.Fprint(w, valueFunc(s)) | ||
} | ||
fmt.Fprintln(w, right) | ||
} | ||
|
||
func (bt *relationBox) pad(s, v string) string { | ||
return fmt.Sprintf("%s%s", v, strings.Repeat(" ", bt.max[s]-utf8.RuneCountInString(v))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package box_test | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/martindrlik/rex/box" | ||
) | ||
|
||
func ExampleRelation() { | ||
fmt.Println(box.Relation( | ||
[]string{"title", "year"}, | ||
slices.Values([]map[string]any{ | ||
{"title": "Adventure Time", "year": 2010}, | ||
{"title": "What We Do in the Shadows", "year": 2019}, | ||
{"title": "The Last of Us"}}))) | ||
|
||
fmt.Println(box.Relation([]string{"empty", "table"}, slices.Values([]map[string]any{}))) | ||
|
||
// Output: | ||
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━┓ | ||
// ┃ title │ year ┃ | ||
// ┠───────────────────────────┼──────┨ | ||
// ┃ Adventure Time │ 2010 ┃ | ||
// ┃ What We Do in the Shadows │ 2019 ┃ | ||
// ┃ The Last of Us │ ? ┃ | ||
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━┛ | ||
// | ||
// ┏━━━━━━━┯━━━━━━━┓ | ||
// ┃ empty │ table ┃ | ||
// ┗━━━━━━━┷━━━━━━━┛ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/zsh | ||
|
||
table_one='[{"year": 2049}, {"year": 2050}]' | ||
table_two='[{"year": 2050}]' | ||
result=`go run . difference -ia $table_one -ia $table_two -of json` | ||
|
||
echo "Table 1: $table_one" | ||
echo "Table 2: $table_two" | ||
echo "Result: $result" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/zsh | ||
|
||
cast='[{"movie_id": "br2049", "actor_id": "rg", "character_id": "br2049k"}]' | ||
movies='[{"movie_id": "br2049", "movie_name": "Blade Runner 2049", "movie_release_year": 2017}]' | ||
actors='[{"actor_id": "rg", "actor_name": "Ryan Gosling"}]' | ||
characters='[{"character_id": "br2049k", "character_name": "K"}]' | ||
|
||
result=`go run . natural-join -ia $cast -ia $movies -of json` | ||
result=`go run . natural-join -ia $result -ia $actors -of json` | ||
result=`go run . natural-join -ia $result -ia $characters -of json movie_name actor_name character_name` | ||
|
||
echo "Cast: $cast" | ||
echo "Movies: $movies" | ||
echo "Actors: $actors" | ||
echo "Characters: $characters" | ||
echo "Projection: {\"movie_name\", \"actor_name\", \"character_name\"}" | ||
echo "Result: $result" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/zsh | ||
|
||
table_one='[{"year": 2049}]' | ||
table_two='[{"year": 2050}]' | ||
result=`go run . union -ia $table_one -ia $table_two -of json` | ||
|
||
echo "Table 1: $table_one" | ||
echo "Table 2: $table_two" | ||
echo "Result: $result" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/martindrlik/rex | ||
|
||
go 1.23.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go 1.23.1 | ||
|
||
use . |
Oops, something went wrong.