#12: multiple joins (#280)

* The query language now supports multiple joins.
This commit is contained in:
Neil O'Toole 2023-07-03 09:34:19 -06:00 committed by GitHub
parent 1edc02c378
commit 7396aadb9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
89 changed files with 2694 additions and 1771 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 727 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

View File

@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Breaking changes are annotated with ☢️. Breaking changes are annotated with ☢️.
## Upcoming ## [v0.40.0] - 2023-07-03
This release features a complete overhaul of the [`join`](https://sq.io/docs/query#joins)
mechanism.
### Added ### Added
@ -18,6 +21,30 @@ Breaking changes are annotated with ☢️.
$ sq `@sakila | .actor:a | .a.first_name` $ sq `@sakila | .actor:a | .a.first_name`
``` ```
- New option `result.column.rename` that exposes a template used to rename
result set column names before display. The primary use case is to de-duplicate
columns names on a `SELECT * FROM tbl1 JOIN tbl2`, where `tbl1` and `tbl2`
have clashing column names ([docs](https://sq.io/docs/config/#recordcolumnrename)).
### Changed
- ☢️ [#12]: The table [join](https://sq.io/docs/query/#joins) mechanism has been
completely overhauled. Now there's support for multiple joins, as well as
other join types such as `LEFT OUTER JOIN`, `CROSS JOIN`, etc. See [docs](https://sq.io/docs/query/#joins).
```shell
# Previously, only a single join was possible
$ sq '.actor, .film_actor | join(.actor_id)'
# Now, an arbitrary number of joins
$ sq '.actor | join(.film_actor, .actor_id) | join(.film, .film_id)'
```
- ☢️ The alias for `--jsonl` (JSON Lines) has been changed to `-J`.
### Fixed
- Fixed bug where config options weren't being propagated correctly.
## [v0.39.1] - 2023-06-22 ## [v0.39.1] - 2023-06-22
### Fixed ### Fixed
@ -622,6 +649,7 @@ make working with lots of sources much easier.
- [#89]: Bug with SQL generated for joins. - [#89]: Bug with SQL generated for joins.
[#8]: https://github.com/neilotoole/sq/issues/8 [#8]: https://github.com/neilotoole/sq/issues/8
[#12]: https://github.com/neilotoole/sq/issues/12
[#15]: https://github.com/neilotoole/sq/issues/15 [#15]: https://github.com/neilotoole/sq/issues/15
[#89]: https://github.com/neilotoole/sq/pull/89 [#89]: https://github.com/neilotoole/sq/pull/89
[#91]: https://github.com/neilotoole/sq/pull/91 [#91]: https://github.com/neilotoole/sq/pull/91
@ -695,3 +723,4 @@ make working with lots of sources much easier.
[v0.38.1]: https://github.com/neilotoole/sq/compare/v0.38.0...v0.38.1 [v0.38.1]: https://github.com/neilotoole/sq/compare/v0.38.0...v0.38.1
[v0.39.0]: https://github.com/neilotoole/sq/compare/v0.38.1...v0.39.0 [v0.39.0]: https://github.com/neilotoole/sq/compare/v0.38.1...v0.39.0
[v0.39.1]: https://github.com/neilotoole/sq/compare/v0.39.0...v0.39.1 [v0.39.1]: https://github.com/neilotoole/sq/compare/v0.39.0...v0.39.1
[v0.40.0]: https://github.com/neilotoole/sq/compare/v0.39.1...v0.40.0

118
README.md
View File

@ -13,7 +13,8 @@ structured data sources: SQL databases, or document formats like CSV or Excel.
![sq](.images/splash.png) ![sq](.images/splash.png)
`sq` executes jq-like [queries](https://sq.io/docs/query), or database-native [SQL](https://sq.io/docs/cmd/sql/). `sq` executes jq-like [queries](https://sq.io/docs/query), or database-native [SQL](https://sq.io/docs/cmd/sql/).
It can perform cross-source [joins](https://sq.io/docs/query/#cross-source-joins). It can [join](https://sq.io/docs/query/#cross-source-joins) across sources: join a CSV file to a Postgres table, or
MySQL with Excel.
`sq` outputs to a multitude of [formats](https://sq.io/docs/output#formats) `sq` outputs to a multitude of [formats](https://sq.io/docs/output#formats)
including [JSON](https://sq.io/docs/output#json), including [JSON](https://sq.io/docs/output#json),
@ -21,8 +22,10 @@ including [JSON](https://sq.io/docs/output#json),
[HTML](https://sq.io/docs/output#html), [Markdown](https://sq.io/docs/output#markdown) [HTML](https://sq.io/docs/output#html), [Markdown](https://sq.io/docs/output#markdown)
and [XML](https://sq.io/docs/output#xml), and can [insert](https://sq.io/docs/output#insert) query and [XML](https://sq.io/docs/output#xml), and can [insert](https://sq.io/docs/output#insert) query
results directly to a SQL database. results directly to a SQL database.
`sq` can also [inspect](https://sq.io/docs/inspect) sources to view metadata about the source structure (tables, `sq` can also [inspect](https://sq.io/docs/inspect) sources to view metadata about the source structure (tables,
columns, size) and has commands for common database operations to columns, size). You can use [`sq diff`](https://sq.io/docs/diff) to compare tables, or
entire databases. `sq` has commands for common database operations to
[copy](https://sq.io/docs/cmd/tbl-copy), [truncate](https://sq.io/docs/cmd/tbl-truncate), [copy](https://sq.io/docs/cmd/tbl-copy), [truncate](https://sq.io/docs/cmd/tbl-truncate),
and [drop](https://sq.io/docs/cmd/tbl-drop) tables. and [drop](https://sq.io/docs/cmd/tbl-drop) tables.
@ -121,46 +124,25 @@ $ sq ping
Fundamentally, `sq` is for querying data. The jq-style syntax is covered in Fundamentally, `sq` is for querying data. The jq-style syntax is covered in
detail in the [query guide](https://sq.io/docs/query). detail in the [query guide](https://sq.io/docs/query).
```shell ![sq query where slq](./.images/sq_query_where_slq.png)
$ sq '.actor | where(.actor_id < 100) | .[0:3]'
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2020-02-15T06:59:28Z
2 NICK WAHLBERG 2020-02-15T06:59:28Z
3 ED CHASE 2020-02-15T06:59:28Z
```
The above query selected some rows from the `actor` table. You could also The above query selected some rows from the `actor` table. You could also
use [native SQL](https://sq.io/docs/cmd/sql), e.g.: use [native SQL](https://sq.io/docs/cmd/sql), e.g.:
```shell ![sq query where sql](./.images/sq_query_where_sql.png)
$ sq sql 'SELECT * FROM actor WHERE actor_id < 100 LIMIT 3'
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2020-02-15T06:59:28Z
2 NICK WAHLBERG 2020-02-15T06:59:28Z
3 ED CHASE 2020-02-15T06:59:28Z
```
But we're flying a bit blind here: how did we know about the `actor` table? But we're flying a bit blind here: how did we know about the `actor` table?
### Inspect ### Inspect
[`sq inspect`](https://sq.io/docs/inspect) is your friend (output abbreviated): [`sq inspect`](https://sq.io/docs/inspect) is your friend.
```shell ![sq inspect](./.images/sq_inspect_source_text.png)
$ sq inspect
SOURCE DRIVER NAME FQ NAME SIZE TABLES VIEWS LOCATION
@sakila/sqlite sqlite3 sakila.db sakila.db/main 5.6MB 16 5 sqlite3:///Users/neilotoole/work/sq/sq/drivers/sqlite3/testdata/sakila.db
NAME TYPE ROWS COLS
actor table 200 actor_id, first_name, last_name, last_update
address table 603 address_id, address, address2, district, city_id, postal_code, phone, last_update
category table 16 category_id, name, last_update
```
Use [`sq inspect -v`](https://sq.io/docs/cmd/inspect) to see more detail. Use [`sq inspect -v`](https://sq.io/docs/cmd/inspect) to see more detail.
Or use [`-j`](https://sq.io/docs/output#json) to get JSON output: Or use [`-j`](https://sq.io/docs/output#json) to get JSON output:
![sq inspect -j](https://sq.io/images/sq_inspect_sakila_sqlite_json.png) ![sq inspect -j](./.images/sq_inspect_sakila_sqlite_json.png)
Combine `sq inspect` with [jq](https://jqlang.github.io/jq/) for some useful capabilities. Combine `sq inspect` with [jq](https://jqlang.github.io/jq/) for some useful capabilities.
Here's how to [list](https://sq.io/docs/cookbook/#list-table-names) Here's how to [list](https://sq.io/docs/cookbook/#list-table-names)
@ -191,14 +173,9 @@ category.csv customer.csv film_actor.csv film_text.csv payment.csv sale
Note that you can also inspect an individual table: Note that you can also inspect an individual table:
```shell ![sq inspect actor verbose](./.images/sq_inspect_actor_verbose.png)
$ sq inspect @sakila.actor -v
NAME TYPE ROWS COLS NAME TYPE PK Read more about [`sq inspect`](https://sq.io/docs/inspect).
actor table 200 4 actor_id int4 pk
first_name varchar
last_name varchar
last_update timestamp
```
### Diff ### Diff
@ -209,8 +186,10 @@ Use [`sq diff`](https://sq.io/docs/diff) to compare source metadata, or row data
### Insert query results ### Insert query results
`sq` query results can be [output](https://sq.io/docs/output) in various formats `sq` query results can be [output](https://sq.io/docs/output) in various formats
(JSON, XML, CSV, etc), and can also be "outputted" as an ([`text`](https://sq.io/docs/output#text),
[*insert*](https://sq.io/docs/output#insert) into database sources. [`json`](https://sq.io/docs/output#json),
[`csv`](https://sq.io/docs/output#csv), etc.). Those results can also be "outputted"
as an [*insert*](https://sq.io/docs/output#insert) into a database table.
That is, you can use `sq` to insert results from a Postgres query into a MySQL table, That is, you can use `sq` to insert results from a Postgres query into a MySQL table,
or copy an Excel worksheet into a SQLite table, or a push a CSV file into or copy an Excel worksheet into a SQLite table, or a push a CSV file into
@ -219,61 +198,28 @@ a SQL Server table etc.
> **Note:** If you want to copy a table inside the same (database) source, > **Note:** If you want to copy a table inside the same (database) source,
> use [`sq tbl copy`](https://sq.io/docs/cmd/tbl-copy) instead, which uses the database's native table copy functionality. > use [`sq tbl copy`](https://sq.io/docs/cmd/tbl-copy) instead, which uses the database's native table copy functionality.
For this example, we'll insert an Excel worksheet into our `@sakila` Here we query a CSV file, and insert the results into a Postgres table.
SQLite database. First, we
download the XLSX file, and `sq add` it as a source. ![sq query insert inspect](./.images/sq_query_insert_inspect.png)
### Cross-source joins
`sq` can perform the usual [joins](https://sq.io/docs/query#joins). Here's how you would
join tables `actor`, `film_actor`, and `film`:
```shell ```shell
$ wget https://sq.io/testdata/xl_demo.xlsx $ sq '.actor | join(.film_actor, .actor_id) | join(.film, .film_id) | .first_name, .last_name, .title'
$ sq add ./xl_demo.xlsx --ingest.header=true
@xl_demo xlsx xl_demo.xlsx
$ sq @xl_demo.person
uid username email address_id
1 neilotoole neilotoole@apache.org 1
2 ksoze kaiser@soze.org 2
3 kubla kubla@khan.mn NULL
[...]
``` ```
Now, execute the same query, but this time `sq` inserts the results into a new But `sq` can also join across data sources. That is, you can join an Excel worksheet with a
table (`person`) Postgres table, or join a CSV file with MySQL, and so on.
in the SQLite `@sakila` source:
```shell This example joins a Postgres database, an Excel worksheet, and a CSV file.
$ sq @xl_demo.person --insert @sakila.person
Inserted 7 rows into @sakila.person
$ sq inspect @sakila.person ![sq join multi source](./.images/sq_join_multi_source.png)
TABLE ROWS COL NAMES
person 7 uid, username, email, address_id
$ sq @sakila.person Read more about cross-source joins in the [query guide](https://sq.io/docs/query/joins).
uid username email address_id
1 neilotoole neilotoole@apache.org 1
2 ksoze kaiser@soze.org 2
3 kubla kubla@khan.mn NULL
[...]
```
### Cross-source join
`sq` has rudimentary support for cross-source [joins](https://sq.io/docs/query#join). That is, you can join an Excel worksheet with a
CSV file, or Postgres table, etc.
See the [tutorial](https://sq.io/docs/tutorial/#join) for further details, but
given an Excel source `@xl_demo` and a CSV source `@csv_demo`, you can do:
```shell
$ sq '@csv_demo.data, @xl_demo.address | join(.D == .address_id) | .C, .city'
C city
neilotoole@apache.org Washington
kaiser@soze.org Ulan Bator
nikola@tesla.rs Washington
augustus@caesar.org Ulan Bator
plato@athens.gr Washington
```
### Table commands ### Table commands

View File

@ -27,6 +27,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/cli/run" "github.com/neilotoole/sq/cli/run"
"github.com/neilotoole/sq/cli/flag" "github.com/neilotoole/sq/cli/flag"
@ -70,6 +72,7 @@ func Execute(ctx context.Context, stdin *os.File, stdout, stderr io.Writer, args
defer ru.Close() // ok to call ru.Close on nil ru defer ru.Close() // ok to call ru.Close on nil ru
ctx = lg.NewContext(ctx, log) ctx = lg.NewContext(ctx, log)
return ExecuteWith(ctx, ru, args) return ExecuteWith(ctx, ru, args)
} }
@ -77,6 +80,7 @@ func Execute(ctx context.Context, stdin *os.File, stdout, stderr io.Writer, args
// resulting in a command being executed. The caller must // resulting in a command being executed. The caller must
// invoke ru.Close. // invoke ru.Close.
func ExecuteWith(ctx context.Context, ru *run.Run, args []string) error { func ExecuteWith(ctx context.Context, ru *run.Run, args []string) error {
ctx = options.NewContext(ctx, ru.Config.Options)
log := lg.FromContext(ctx) log := lg.FromContext(ctx)
log.Debug("EXECUTE", "args", strings.Join(args, " ")) log.Debug("EXECUTE", "args", strings.Join(args, " "))
log.Debug("Build info", "build", buildinfo.Get()) log.Debug("Build info", "build", buildinfo.Get())

View File

@ -92,13 +92,17 @@ func execConfigEditOptions(cmd *cobra.Command, _ []string) error {
return nil return nil
} }
opts := options.Options{} o := options.Options{}
if err = ioz.UnmarshallYAML(after, &opts); err != nil { if err = ioz.UnmarshallYAML(after, &o); err != nil {
return err
}
if o, err = ru.OptionsRegistry.Process(o); err != nil {
return err return err
} }
// TODO: if --verbose, show diff // TODO: if --verbose, show diff
cfg.Options = opts cfg.Options = o
if err = ru.ConfigStore.Save(ctx, cfg); err != nil { if err = ru.ConfigStore.Save(ctx, cfg); err != nil {
return err return err
} }

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/cli/run" "github.com/neilotoole/sq/cli/run"
"github.com/samber/lo" "github.com/samber/lo"
@ -202,5 +204,7 @@ See docs for more: https://sq.io/docs/config
` `
w := cmd.OutOrStdout() w := cmd.OutOrStdout()
fmt.Fprintf(w, tpl, key, opt.DefaultAny(), opt.Help())
defVal := fmt.Sprintf("%v", opt.DefaultAny())
fmt.Fprintf(w, tpl, key, stringz.ShellEscape(defVal), opt.Help())
} }

View File

@ -0,0 +1,44 @@
package cli_test
import (
"testing"
"github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/cli/testrun"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/testh"
"github.com/neilotoole/sq/testh/sakila"
)
// TestCmdConfigSet verifies that setting config options actually takes effect.
// In this test, we use driver.OptResultColRename, setting that template such
// that the column name is transformed to uppercase.
func TestCmdConfigSet(t *testing.T) {
th := testh.New(t)
src := th.Source(sakila.SL3)
tr := testrun.New(th.Context, t, nil).Hush().Add(*src)
err := tr.Exec(".actor | .[0]")
require.NoError(t, err)
got := tr.Out.String()
require.Contains(t, got, "actor_id")
require.NotContains(t, got, "ACTOR_ID")
tr = testrun.New(th.Context, t, tr)
const tpl = `{{.Name | upper}}`
err = tr.Exec("config", "set", driver.OptResultColRename.Key(), stringz.ShellEscape(tpl))
require.NoError(t, err)
tr = testrun.New(th.Context, t, tr)
err = tr.Exec(".actor | .[0]")
require.NoError(t, err)
got = tr.Out.String()
require.Contains(t, got, "ACTOR_ID")
require.NotContains(t, got, "actor_id")
}

View File

@ -131,8 +131,8 @@ func TestCmdSLQ_OutputFlag(t *testing.T) {
require.Equal(t, sakila.TblActorCount, len(recs)) require.Equal(t, sakila.TblActorCount, len(recs))
} }
func TestCmdSLQ_Join(t *testing.T) { func TestCmdSLQ_Join_cross_source(t *testing.T) {
const queryTpl = `%s.customer, %s.address | join(.address_id) | where(.customer_id == %d) | .[0] | .customer_id, .email, .city_id` //nolint:lll const queryTpl = `%s.customer | join(%s.address, .address_id) | where(.customer_id == %d) | .[0] | .customer_id, .email, .city_id` //nolint:lll
handles := sakila.SQLAll() handles := sakila.SQLAll()
// Attempt to join every SQL test source against every SQL test source. // Attempt to join every SQL test source against every SQL test source.

View File

@ -0,0 +1,3 @@
options:
# Should fail because not_a_func is not a valid function.
result.column.rename: "{{not_a_func .data}}"

View File

@ -0,0 +1,4 @@
options:
# The "upper" func is from the sprig template library. This
# test verifies that sprig is present.
result.column.rename: "{{.Name | upper}}"

View File

@ -87,8 +87,9 @@ func TestFileStore_Load(t *testing.T) {
match := match match := match
t.Run(tutil.Name(match), func(t *testing.T) { t.Run(tutil.Name(match), func(t *testing.T) {
fs.Path = match fs.Path = match
_, err = fs.Load(context.Background()) cfg, err := fs.Load(context.Background())
require.NoError(t, err, match) require.NoError(t, err, match)
require.NotNil(t, cfg)
}) })
} }
@ -96,8 +97,10 @@ func TestFileStore_Load(t *testing.T) {
match := match match := match
t.Run(tutil.Name(match), func(t *testing.T) { t.Run(tutil.Name(match), func(t *testing.T) {
fs.Path = match fs.Path = match
_, err := fs.Load(context.Background()) cfg, err := fs.Load(context.Background())
t.Log(err)
require.Error(t, err, match) require.Error(t, err, match)
require.Nil(t, cfg)
}) })
} }
} }

View File

@ -50,7 +50,7 @@ const (
JSONAShort = "A" JSONAShort = "A"
JSONAUsage = "Output LF-delimited JSON arrays" JSONAUsage = "Output LF-delimited JSON arrays"
JSONL = "jsonl" JSONL = "jsonl"
JSONLShort = "l" JSONLShort = "J"
JSONLUsage = "Output LF-delimited JSON objects" JSONLUsage = "Output LF-delimited JSON objects"
Markdown = "markdown" Markdown = "markdown"

View File

@ -37,6 +37,7 @@ var (
"", "",
0, 0,
getDefaultLogFilePath(), getDefaultLogFilePath(),
nil,
"Log file path", "Log file path",
`Path to log file. Empty value disables logging.`, `Path to log file. Empty value disables logging.`,
) )

View File

@ -141,6 +141,7 @@ func RegisterDefaultOpts(reg *options.Registry) {
OptDateFormatAsNumber, OptDateFormatAsNumber,
OptTimeFormat, OptTimeFormat,
OptTimeFormatAsNumber, OptTimeFormatAsNumber,
driver.OptResultColRename,
OptVerbose, OptVerbose,
OptPrintHeader, OptPrintHeader,
OptMonochrome, OptMonochrome,

View File

@ -19,7 +19,7 @@ func TestRegisterDefaultOpts(t *testing.T) {
log.Debug("options.Registry (after)", "reg", reg) log.Debug("options.Registry (after)", "reg", reg)
keys := reg.Keys() keys := reg.Keys()
require.Len(t, keys, 31) require.Len(t, keys, 32)
for _, opt := range reg.Opts() { for _, opt := range reg.Opts() {
opt := opt opt := opt

View File

@ -109,6 +109,7 @@ Generally, it is not necessary to fiddle this knob.`,
"", "",
0, 0,
"RFC3339", "RFC3339",
nil,
"Timestamp format: constant such as RFC3339 or a strftime format", "Timestamp format: constant such as RFC3339 or a strftime format",
`Timestamp format. This can be one of several predefined constants such `Timestamp format. This can be one of several predefined constants such
as "RFC3339" or "Unix", or a strftime format such as "%Y-%m-%d %H:%M:%S". as "RFC3339" or "Unix", or a strftime format such as "%Y-%m-%d %H:%M:%S".
@ -141,6 +142,7 @@ is not an integer.
"", "",
0, 0,
"DateOnly", "DateOnly",
nil,
"Date format: constant such as DateOnly or a strftime format", "Date format: constant such as DateOnly or a strftime format",
`Date format. This can be one of several predefined constants such `Date format. This can be one of several predefined constants such
as "DateOnly" or "Unix", or a strftime format such as "%Y-%m-%d". as "DateOnly" or "Unix", or a strftime format such as "%Y-%m-%d".
@ -174,6 +176,7 @@ Note that this option is no-op if the rendered value is not an integer.
"", "",
0, 0,
"TimeOnly", "TimeOnly",
nil,
"Time format: constant such as TimeOnly or a strftime format", "Time format: constant such as TimeOnly or a strftime format",
`Time format. This can be one of several predefined constants such `Time format. This can be one of several predefined constants such
as "TimeOnly" or "Unix", or a strftime format such as "%Y-%m-%d". as "TimeOnly" or "Unix", or a strftime format such as "%Y-%m-%d".

View File

@ -45,7 +45,7 @@ func (w *sourceWriter) Collection(coll *source.Collection) error {
} }
if coll.Active() != nil && coll.Active().Handle == src.Handle { if coll.Active() != nil && coll.Active().Handle == src.Handle {
row[0] = pr.Active.Sprintf(row[0]) row[0] = pr.Active.Sprintf(row[0]) + pr.Faint.Sprint("*")
} }
rows = append(rows, row) rows = append(rows, row)

View File

@ -63,6 +63,7 @@ func New(ctx context.Context, t testing.TB, from *TestRun) *TestRun {
} }
tr.Run, tr.Out, tr.ErrOut = newRun(ctx, t, cfgStore) tr.Run, tr.Out, tr.ErrOut = newRun(ctx, t, cfgStore)
tr.Context = options.NewContext(ctx, tr.Run.Config.Options)
return tr return tr
} }

View File

@ -47,6 +47,7 @@ var OptDelim = options.NewString(
"", "",
0, 0,
delimCommaKey, delimCommaKey,
nil,
"Delimiter for ingest CSV data", "Delimiter for ingest CSV data",
`Delimiter to use for CSV files. Default is "comma". `Delimiter to use for CSV files. Default is "comma".
Possible values are: comma, space, pipe, tab, colon, semi, period.`, Possible values are: comma, space, pipe, tab, colon, semi, period.`,

View File

@ -128,7 +128,7 @@ func getRecMeta(ctx context.Context, scratchDB driver.Database, tblDef *sqlmodel
return nil, err return nil, err
} }
destMeta, _, err := scratchDB.SQLDriver().RecordMeta(colTypes) destMeta, _, err := scratchDB.SQLDriver().RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -63,7 +63,7 @@ func getRecMeta(ctx context.Context, scratchDB driver.Database, tblDef *sqlmodel
return nil, err return nil, err
} }
destMeta, _, err := scratchDB.SQLDriver().RecordMeta(colTypes) destMeta, _, err := scratchDB.SQLDriver().RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -26,8 +26,6 @@ import (
"github.com/neilotoole/sq/libsq/core/lg" "github.com/neilotoole/sq/libsq/core/lg"
"golang.org/x/exp/slog"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
@ -38,7 +36,7 @@ import (
// kindFromDBTypeName determines the Kind from the database // kindFromDBTypeName determines the Kind from the database
// type name. For example, "VARCHAR(64)" -> kind.Text. // type name. For example, "VARCHAR(64)" -> kind.Text.
func kindFromDBTypeName(log *slog.Logger, colName, dbTypeName string) kind.Kind { func kindFromDBTypeName(ctx context.Context, colName, dbTypeName string) kind.Kind {
var knd kind.Kind var knd kind.Kind
dbTypeName = strings.ToUpper(dbTypeName) dbTypeName = strings.ToUpper(dbTypeName)
@ -51,7 +49,7 @@ func kindFromDBTypeName(log *slog.Logger, colName, dbTypeName string) kind.Kind
switch dbTypeName { switch dbTypeName {
default: default:
log.Warn( lg.FromContext(ctx).Warn(
"Unknown MySQL column type: using alt type", "Unknown MySQL column type: using alt type",
lga.DBType, dbTypeName, lga.DBType, dbTypeName,
lga.Col, colName, lga.Col, colName,
@ -91,16 +89,28 @@ func kindFromDBTypeName(log *slog.Logger, colName, dbTypeName string) kind.Kind
return knd return knd
} }
func recordMetaFromColumnTypes(log *slog.Logger, colTypes []*sql.ColumnType) record.Meta { func recordMetaFromColumnTypes(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta, error) {
recMeta := make(record.Meta, len(colTypes)) sColTypeData := make([]*record.ColumnTypeData, len(colTypes))
ogColNames := make([]string, len(colTypes))
for i, colType := range colTypes { for i, colType := range colTypes {
knd := kindFromDBTypeName(log, colType.Name(), colType.DatabaseTypeName()) knd := kindFromDBTypeName(ctx, colType.Name(), colType.DatabaseTypeName())
colTypeData := record.NewColumnTypeData(colType, knd) colTypeData := record.NewColumnTypeData(colType, knd)
recMeta[i] = record.NewFieldMeta(colTypeData) sColTypeData[i] = colTypeData
ogColNames[i] = colTypeData.Name
} }
return recMeta mungedColNames, err := driver.MungeColNames(ctx, ogColNames)
if err != nil {
return nil, err
}
recMeta := make(record.Meta, len(colTypes))
for i := range sColTypeData {
sColTypeData[i].Name = mungedColNames[i]
recMeta[i] = record.NewFieldMeta(sColTypeData[i])
}
return recMeta, nil
} }
// getNewRecordFunc returns a NewRecordFunc that, after interacting // getNewRecordFunc returns a NewRecordFunc that, after interacting
@ -226,7 +236,7 @@ ORDER BY cols.ordinal_position ASC`
} }
col.DefaultValue = defVal.String col.DefaultValue = defVal.String
col.Kind = kindFromDBTypeName(log, col.Name, col.BaseType) col.Kind = kindFromDBTypeName(ctx, col.Name, col.BaseType)
cols = append(cols, col) cols = append(cols, col)
} }
@ -468,7 +478,7 @@ ORDER BY c.TABLE_NAME ASC, c.ORDINAL_POSITION ASC`
return nil, err return nil, err
} }
col.Kind = kindFromDBTypeName(log, col.Name, col.BaseType) col.Kind = kindFromDBTypeName(ctx, col.Name, col.BaseType)
if strings.Contains(colKey.String, "PRI") { if strings.Contains(colKey.String, "PRI") {
col.PrimaryKey = true col.PrimaryKey = true
} }

View File

@ -1,8 +1,11 @@
package mysql_test package mysql_test
import ( import (
"context"
"testing" "testing"
"github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/slogt" "github.com/neilotoole/slogt"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -16,6 +19,8 @@ import (
func TestKindFromDBTypeName(t *testing.T) { func TestKindFromDBTypeName(t *testing.T) {
t.Parallel() t.Parallel()
ctx := lg.NewContext(context.Background(), slogt.New(t))
testCases := map[string]kind.Kind{ testCases := map[string]kind.Kind{
"": kind.Unknown, "": kind.Unknown,
"INTEGER": kind.Int, "INTEGER": kind.Int,
@ -58,9 +63,8 @@ func TestKindFromDBTypeName(t *testing.T) {
"BOOLEAN": kind.Bool, "BOOLEAN": kind.Bool,
} }
log := slogt.New(t)
for dbTypeName, wantKind := range testCases { for dbTypeName, wantKind := range testCases {
gotKind := mysql.KindFromDBTypeName(log, "col", dbTypeName) gotKind := mysql.KindFromDBTypeName(ctx, "col", dbTypeName)
require.Equal(t, wantKind, gotKind, "{%s} should produce %s but got %s", dbTypeName, wantKind, gotKind) require.Equal(t, wantKind, gotKind, "{%s} should produce %s but got %s", dbTypeName, wantKind, gotKind)
} }
} }

View File

@ -6,6 +6,11 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/samber/lo"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/neilotoole/sq/libsq/ast/render" "github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
@ -111,11 +116,11 @@ func (d *driveri) Dialect() dialect.Dialect {
return dialect.Dialect{ return dialect.Dialect{
Type: Type, Type: Type,
Placeholders: placeholders, Placeholders: placeholders,
IdentQuote: '`',
Enquote: stringz.BacktickQuote, Enquote: stringz.BacktickQuote,
IntBool: true, IntBool: true,
MaxBatchValues: 250, MaxBatchValues: 250,
Ops: dialect.DefaultOps(), Ops: dialect.DefaultOps(),
Joins: lo.Without(jointype.All(), jointype.FullOuter),
} }
} }
@ -133,8 +138,13 @@ func (d *driveri) Renderer() *render.Renderer {
} }
// RecordMeta implements driver.SQLDriver. // RecordMeta implements driver.SQLDriver.
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (record.Meta, driver.NewRecordFunc, error) { func (d *driveri) RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta,
recMeta := recordMetaFromColumnTypes(d.log, colTypes) driver.NewRecordFunc, error,
) {
recMeta, err := recordMetaFromColumnTypes(ctx, colTypes)
if err != nil {
return nil, nil, err
}
mungeFn := getNewRecordFunc(recMeta) mungeFn := getNewRecordFunc(recMeta)
return recMeta, mungeFn, nil return recMeta, mungeFn, nil
} }
@ -284,13 +294,12 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
) ([]*sql.ColumnType, error) { ) ([]*sql.ColumnType, error) {
const queryTpl = "SELECT %s FROM %s LIMIT 0" const queryTpl = "SELECT %s FROM %s LIMIT 0"
dialect := d.Dialect() enquote := d.Dialect().Enquote
quote := string(dialect.IdentQuote) tblNameQuoted := enquote(tblName)
tblNameQuoted := dialect.Enquote(tblName)
colsClause := "*" colsClause := "*"
if len(colNames) > 0 { if len(colNames) > 0 {
colNamesQuoted := stringz.SurroundSlice(colNames, quote) colNamesQuoted := loz.Apply(colNames, enquote)
colsClause = strings.Join(colNamesQuoted, driver.Comma) colsClause = strings.Join(colNamesQuoted, driver.Comma)
} }
@ -328,7 +337,7 @@ func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName st
return nil, err return nil, err
} }
destCols, _, err := d.RecordMeta(colTypes) destCols, _, err := d.RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,6 +8,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
@ -103,10 +105,10 @@ func (d *driveri) Dialect() dialect.Dialect {
return dialect.Dialect{ return dialect.Dialect{
Type: Type, Type: Type,
Placeholders: placeholders, Placeholders: placeholders,
IdentQuote: '"',
Enquote: stringz.DoubleQuote, Enquote: stringz.DoubleQuote,
MaxBatchValues: 1000, MaxBatchValues: 1000,
Ops: dialect.DefaultOps(), Ops: dialect.DefaultOps(),
Joins: jointype.All(),
} }
} }
@ -408,8 +410,9 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
// (SELECT username FROM person LIMIT 1) AS username, // (SELECT username FROM person LIMIT 1) AS username,
// (SELECT email FROM person LIMIT 1) AS email // (SELECT email FROM person LIMIT 1) AS email
// LIMIT 1; // LIMIT 1;
quote := string(d.Dialect().IdentQuote)
tblNameQuoted := stringz.Surround(tblName, quote) enquote := d.Dialect().Enquote
tblNameQuoted := enquote(tblName)
var query string var query string
@ -426,7 +429,7 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
var sb strings.Builder var sb strings.Builder
sb.WriteString("SELECT\n") sb.WriteString("SELECT\n")
for i, colName := range colNames { for i, colName := range colNames {
colNameQuoted := stringz.Surround(colName, quote) colNameQuoted := enquote(colName)
sb.WriteString(fmt.Sprintf(" (SELECT %s FROM %s LIMIT 1) AS %s", colNameQuoted, tblNameQuoted, colNameQuoted)) sb.WriteString(fmt.Sprintf(" (SELECT %s FROM %s LIMIT 1) AS %s", colNameQuoted, tblNameQuoted, colNameQuoted))
if i < len(colNames)-1 { if i < len(colNames)-1 {
sb.WriteRune(',') sb.WriteRune(',')
@ -469,7 +472,7 @@ func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName st
return nil, err return nil, err
} }
destCols, _, err := d.RecordMeta(colTypes) destCols, _, err := d.RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -518,19 +521,34 @@ func getTableColumnNames(ctx context.Context, db sqlz.DB, tblName string) ([]str
} }
// RecordMeta implements driver.SQLDriver. // RecordMeta implements driver.SQLDriver.
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (record.Meta, driver.NewRecordFunc, error) { func (d *driveri) RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta,
driver.NewRecordFunc, error,
) {
// The jackc/pgx driver doesn't report nullability (sql.ColumnType) // The jackc/pgx driver doesn't report nullability (sql.ColumnType)
// Apparently this is due to what postgres sends over the wire. // Apparently this is due to what postgres sends over the wire.
// See https://github.com/jackc/pgx/issues/276#issuecomment-526831493 // See https://github.com/jackc/pgx/issues/276#issuecomment-526831493
// So, we'll set the scan type for each column to the nullable // So, we'll set the scan type for each column to the nullable
// version below. // version below.
recMeta := make(record.Meta, len(colTypes)) sColTypeData := make([]*record.ColumnTypeData, len(colTypes))
ogColNames := make([]string, len(colTypes))
for i, colType := range colTypes { for i, colType := range colTypes {
knd := kindFromDBTypeName(d.log, colType.Name(), colType.DatabaseTypeName()) knd := kindFromDBTypeName(d.log, colType.Name(), colType.DatabaseTypeName())
colTypeData := record.NewColumnTypeData(colType, knd) colTypeData := record.NewColumnTypeData(colType, knd)
setScanType(d.log, colTypeData, knd) setScanType(d.log, colTypeData, knd)
recMeta[i] = record.NewFieldMeta(colTypeData) sColTypeData[i] = colTypeData
ogColNames[i] = colTypeData.Name
}
mungedColNames, err := driver.MungeColNames(ctx, ogColNames)
if err != nil {
return nil, nil, err
}
recMeta := make(record.Meta, len(colTypes))
for i := range sColTypeData {
sColTypeData[i].Name = mungedColNames[i]
recMeta[i] = record.NewFieldMeta(sColTypeData[i])
} }
mungeFn := func(vals []any) (record.Record, error) { mungeFn := func(vals []any) (record.Record, error) {

View File

@ -8,6 +8,8 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
@ -16,16 +18,15 @@ import (
"github.com/neilotoole/sq/libsq/core/lg" "github.com/neilotoole/sq/libsq/core/lg"
"golang.org/x/exp/slog"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/source" "github.com/neilotoole/sq/libsq/source"
) )
// recordMetaFromColumnTypes returns recordMetaFromColumnTypes for rows. // recordMetaFromColumnTypes returns recordMetaFromColumnTypes for rows.
func recordMetaFromColumnTypes(log *slog.Logger, colTypes []*sql.ColumnType) (record.Meta, error) { func recordMetaFromColumnTypes(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta, error) {
recMeta := make([]*record.FieldMeta, len(colTypes)) sColTypeData := make([]*record.ColumnTypeData, len(colTypes))
ogColNames := make([]string, len(colTypes))
for i, colType := range colTypes { for i, colType := range colTypes {
// sqlite is very forgiving at times, e.g. execute // sqlite is very forgiving at times, e.g. execute
// a query with a non-existent column name. // a query with a non-existent column name.
@ -33,13 +34,26 @@ func recordMetaFromColumnTypes(log *slog.Logger, colTypes []*sql.ColumnType) (re
// happens for functions such as COUNT(*). // happens for functions such as COUNT(*).
dbTypeName := colType.DatabaseTypeName() dbTypeName := colType.DatabaseTypeName()
kind := kindFromDBTypeName(log, colType.Name(), dbTypeName, colType.ScanType()) kind := kindFromDBTypeName(ctx, colType.Name(), dbTypeName, colType.ScanType())
colTypeData := record.NewColumnTypeData(colType, kind) colTypeData := record.NewColumnTypeData(colType, kind)
// It's necessary to explicitly set the scan type because // It's necessary to explicitly set the scan type because
// the backing driver doesn't set it for whatever reason. // the backing driver doesn't set it for whatever reason.
setScanType(log, colTypeData) // FIXME: legacy? setScanType(ctx, colTypeData) // REVISIT: Legacy? Do we still need this?
recMeta[i] = record.NewFieldMeta(colTypeData)
sColTypeData[i] = colTypeData
ogColNames[i] = colTypeData.Name
}
mungedColNames, err := driver.MungeColNames(ctx, ogColNames)
if err != nil {
return nil, err
}
recMeta := make(record.Meta, len(colTypes))
for i := range sColTypeData {
sColTypeData[i].Name = mungedColNames[i]
recMeta[i] = record.NewFieldMeta(sColTypeData[i])
} }
return recMeta, nil return recMeta, nil
@ -52,7 +66,7 @@ func recordMetaFromColumnTypes(log *slog.Logger, colTypes []*sql.ColumnType) (re
// //
// If the scan type is NOT a sql.NullTYPE, the corresponding sql.NullTYPE will // If the scan type is NOT a sql.NullTYPE, the corresponding sql.NullTYPE will
// be set. // be set.
func setScanType(log *slog.Logger, colType *record.ColumnTypeData) { func setScanType(ctx context.Context, colType *record.ColumnTypeData) {
scanType, knd := colType.ScanType, colType.Kind scanType, knd := colType.ScanType, colType.Kind
if scanType != nil { if scanType != nil {
@ -79,7 +93,7 @@ func setScanType(log *slog.Logger, colType *record.ColumnTypeData) {
switch knd { switch knd {
default: default:
// Shouldn't happen? // Shouldn't happen?
log.Warn("Unknown kind for col", lg.FromContext(ctx).Warn("Unknown kind for col",
lga.Col, colType.Name, lga.Col, colType.Name,
lga.DBType, colType.DatabaseTypeName, lga.DBType, colType.DatabaseTypeName,
) )
@ -119,7 +133,8 @@ func setScanType(log *slog.Logger, colType *record.ColumnTypeData) {
// The scanType arg may be nil (it may not be available to the caller): when // The scanType arg may be nil (it may not be available to the caller): when
// non-nil it may be used to determine ambiguous cases. For example, // non-nil it may be used to determine ambiguous cases. For example,
// dbTypeName is empty string for "COUNT(*)" // dbTypeName is empty string for "COUNT(*)"
func kindFromDBTypeName(log *slog.Logger, colName, dbTypeName string, scanType reflect.Type) kind.Kind { func kindFromDBTypeName(ctx context.Context, colName, dbTypeName string, scanType reflect.Type) kind.Kind {
log := lg.FromContext(ctx)
if dbTypeName == "" { if dbTypeName == "" {
// dbTypeName can be empty for functions such as COUNT() etc. // dbTypeName can be empty for functions such as COUNT() etc.
// But we can infer the type from scanType (if non-nil). // But we can infer the type from scanType (if non-nil).
@ -301,7 +316,7 @@ func getTableMetadata(ctx context.Context, db sqlz.DB, tblName string) (*source.
col.ColumnType = col.BaseType col.ColumnType = col.BaseType
col.Nullable = notnull == 0 col.Nullable = notnull == 0
col.DefaultValue = defaultValue.String col.DefaultValue = defaultValue.String
col.Kind = kindFromDBTypeName(log, col.Name, col.BaseType, nil) col.Kind = kindFromDBTypeName(ctx, col.Name, col.BaseType, nil)
tblMeta.Columns = append(tblMeta.Columns, col) tblMeta.Columns = append(tblMeta.Columns, col)
} }
@ -398,7 +413,7 @@ ORDER BY m.name, p.cid
col.ColumnType = col.BaseType col.ColumnType = col.BaseType
col.Nullable = notnull == 0 col.Nullable = notnull == 0
col.DefaultValue = colDefault.String col.DefaultValue = colDefault.String
col.Kind = kindFromDBTypeName(log, col.Name, col.BaseType, nil) col.Kind = kindFromDBTypeName(ctx, col.Name, col.BaseType, nil)
curTblMeta.Columns = append(curTblMeta.Columns, col) curTblMeta.Columns = append(curTblMeta.Columns, col)
} }

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/slogt" "github.com/neilotoole/slogt"
@ -93,6 +95,8 @@ func TestCurrentTime(t *testing.T) {
func TestKindFromDBTypeName(t *testing.T) { func TestKindFromDBTypeName(t *testing.T) {
t.Parallel() t.Parallel()
ctx := lg.NewContext(context.Background(), slogt.New(t))
testCases := map[string]kind.Kind{ testCases := map[string]kind.Kind{
"": kind.Bytes, "": kind.Bytes,
"NUMERIC": kind.Decimal, "NUMERIC": kind.Decimal,
@ -125,9 +129,8 @@ func TestKindFromDBTypeName(t *testing.T) {
"TIME": kind.Time, "TIME": kind.Time,
} }
log := slogt.New(t)
for dbTypeName, wantKind := range testCases { for dbTypeName, wantKind := range testCases {
gotKind := sqlite3.KindFromDBTypeName(log, "col", dbTypeName, nil) gotKind := sqlite3.KindFromDBTypeName(ctx, "col", dbTypeName, nil)
require.Equal(t, wantKind, gotKind, "%s should produce %s but got %s", dbTypeName) require.Equal(t, wantKind, gotKind, "%s should produce %s but got %s", dbTypeName)
} }
} }
@ -250,7 +253,7 @@ func TestRecordMetadata(t *testing.T) {
colTypes, err := rows.ColumnTypes() colTypes, err := rows.ColumnTypes()
require.NoError(t, err) require.NoError(t, err)
recMeta, _, err := th.SQLDriverFor(src).RecordMeta(colTypes) recMeta, _, err := th.SQLDriverFor(src).RecordMeta(th.Context, colTypes)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(tc.colNames), len(recMeta)) require.Equal(t, len(tc.colNames), len(recMeta))

View File

@ -15,6 +15,10 @@ import (
"sync" "sync"
"time" "time"
"github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/driver/dialect" "github.com/neilotoole/sq/libsq/driver/dialect"
@ -217,10 +221,10 @@ func (d *driveri) Dialect() dialect.Dialect {
return dialect.Dialect{ return dialect.Dialect{
Type: Type, Type: Type,
Placeholders: placeholders, Placeholders: placeholders,
IdentQuote: '"',
Enquote: stringz.DoubleQuote, Enquote: stringz.DoubleQuote,
MaxBatchValues: 500, MaxBatchValues: 500,
Ops: dialect.DefaultOps(), Ops: dialect.DefaultOps(),
Joins: jointype.All(),
} }
} }
@ -275,8 +279,10 @@ func (d *driveri) CopyTable(ctx context.Context, db sqlz.DB, fromTable, toTable
} }
// RecordMeta implements driver.SQLDriver. // RecordMeta implements driver.SQLDriver.
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (record.Meta, driver.NewRecordFunc, error) { func (d *driveri) RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta,
recMeta, err := recordMetaFromColumnTypes(d.log, colTypes) driver.NewRecordFunc, error,
) {
recMeta, err := recordMetaFromColumnTypes(ctx, colTypes)
if err != nil { if err != nil {
return nil, nil, errw(err) return nil, nil, errw(err)
} }
@ -704,13 +710,12 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
// impls, LIMIT can be 0. // impls, LIMIT can be 0.
const queryTpl = "SELECT %s FROM %s LIMIT 1" const queryTpl = "SELECT %s FROM %s LIMIT 1"
dialect := d.Dialect() enquote := d.Dialect().Enquote
quote := string(dialect.IdentQuote) tblNameQuoted := enquote(tblName)
tblNameQuoted := stringz.Surround(tblName, quote)
colsClause := "*" colsClause := "*"
if len(colNames) > 0 { if len(colNames) > 0 {
colNamesQuoted := stringz.SurroundSlice(colNames, quote) colNamesQuoted := loz.Apply(colNames, enquote)
colsClause = strings.Join(colNamesQuoted, driver.Comma) colsClause = strings.Join(colNamesQuoted, driver.Comma)
} }
@ -763,7 +768,7 @@ func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName st
return nil, err return nil, err
} }
destCols, _, err := d.RecordMeta(colTypes) destCols, _, err := d.RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,6 +8,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/driver/dialect" "github.com/neilotoole/sq/libsq/driver/dialect"
@ -108,10 +112,10 @@ func (d *driveri) Dialect() dialect.Dialect {
return dialect.Dialect{ return dialect.Dialect{
Type: Type, Type: Type,
Placeholders: placeholders, Placeholders: placeholders,
IdentQuote: '"',
Enquote: stringz.DoubleQuote, Enquote: stringz.DoubleQuote,
MaxBatchValues: 1000, MaxBatchValues: 1000,
Ops: dialect.DefaultOps(), Ops: dialect.DefaultOps(),
Joins: jointype.All(),
} }
} }
@ -249,13 +253,12 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
// ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY; // ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY" const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"
dialect := d.Dialect() enquote := d.Dialect().Enquote
quote := string(dialect.IdentQuote) tblNameQuoted := enquote(tblName)
tblNameQuoted := stringz.Surround(tblName, quote)
colsClause := "*" colsClause := "*"
if len(colNames) > 0 { if len(colNames) > 0 {
colNamesQuoted := stringz.SurroundSlice(colNames, quote) colNamesQuoted := loz.Apply(colNames, enquote)
colsClause = strings.Join(colNamesQuoted, driver.Comma) colsClause = strings.Join(colNamesQuoted, driver.Comma)
} }
@ -286,13 +289,28 @@ func (d *driveri) TableColumnTypes(ctx context.Context, db sqlz.DB, tblName stri
} }
// RecordMeta implements driver.SQLDriver. // RecordMeta implements driver.SQLDriver.
func (d *driveri) RecordMeta(colTypes []*sql.ColumnType) (record.Meta, driver.NewRecordFunc, error) { func (d *driveri) RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta,
recMeta := make([]*record.FieldMeta, len(colTypes)) driver.NewRecordFunc, error,
) {
sColTypeData := make([]*record.ColumnTypeData, len(colTypes))
ogColNames := make([]string, len(colTypes))
for i, colType := range colTypes { for i, colType := range colTypes {
kind := kindFromDBTypeName(d.log, colType.Name(), colType.DatabaseTypeName()) kind := kindFromDBTypeName(d.log, colType.Name(), colType.DatabaseTypeName())
colTypeData := record.NewColumnTypeData(colType, kind) colTypeData := record.NewColumnTypeData(colType, kind)
setScanType(colTypeData, kind) setScanType(colTypeData, kind)
recMeta[i] = record.NewFieldMeta(colTypeData) sColTypeData[i] = colTypeData
ogColNames[i] = colTypeData.Name
}
mungedColNames, err := driver.MungeColNames(ctx, ogColNames)
if err != nil {
return nil, nil, err
}
recMeta := make(record.Meta, len(colTypes))
for i := range sColTypeData {
sColTypeData[i].Name = mungedColNames[i]
recMeta[i] = record.NewFieldMeta(sColTypeData[i])
} }
mungeFn := func(vals []any) (record.Record, error) { mungeFn := func(vals []any) (record.Record, error) {
@ -456,10 +474,9 @@ func (d *driveri) getTableColsMeta(ctx context.Context, db sqlz.DB,
// ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY; // ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY" const queryTpl = "SELECT %s FROM %s ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY"
dialect := d.Dialect() enquote := d.Dialect().Enquote
quote := string(dialect.IdentQuote) tblNameQuoted := enquote(tblName)
tblNameQuoted := stringz.Surround(tblName, quote) colNamesQuoted := loz.Apply(colNames, enquote)
colNamesQuoted := stringz.SurroundSlice(colNames, quote)
colsJoined := strings.Join(colNamesQuoted, driver.Comma) colsJoined := strings.Join(colNamesQuoted, driver.Comma)
query := fmt.Sprintf(queryTpl, colsJoined, tblNameQuoted) query := fmt.Sprintf(queryTpl, colsJoined, tblNameQuoted)
@ -478,7 +495,7 @@ func (d *driveri) getTableColsMeta(ctx context.Context, db sqlz.DB,
return nil, errw(rows.Err()) return nil, errw(rows.Err())
} }
destCols, _, err := d.RecordMeta(colTypes) destCols, _, err := d.RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close) lg.WarnIfFuncError(d.log, lgm.CloseDBRows, rows.Close)
return nil, errw(err) return nil, errw(err)

14
go.mod
View File

@ -46,22 +46,34 @@ require (
require ( require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/frankban/quicktest v1.11.3 // indirect github.com/frankban/quicktest v1.14.4 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.0 // indirect github.com/jackc/puddle/v2 v2.2.0 // indirect
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect github.com/kr/text v0.2.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/muesli/mango v0.1.0 // indirect github.com/muesli/mango v0.1.0 // indirect
github.com/muesli/mango-pflag v0.1.0 // indirect github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
golang.org/x/crypto v0.10.0 // indirect golang.org/x/crypto v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect golang.org/x/text v0.10.0 // indirect

43
go.sum
View File

@ -4,6 +4,19 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aov
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o= github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b h1:6+ZFm0flnudZzdSE0JxlhR2hKnGPcNB35BjQf4RYQDY= github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b h1:6+ZFm0flnudZzdSE0JxlhR2hKnGPcNB35BjQf4RYQDY=
@ -27,6 +40,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
github.com/frankban/quicktest v1.5.0/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o= github.com/frankban/quicktest v1.5.0/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o=
github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY= github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
@ -43,6 +58,8 @@ github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EO
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
@ -51,6 +68,12 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@ -88,8 +111,14 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/microsoft/go-mssqldb v1.1.0 h1:jsV+tpvcPTbNNKW0o3kiCD69kOHICsfjZ2VcVu2lKYc= github.com/microsoft/go-mssqldb v1.1.0 h1:jsV+tpvcPTbNNKW0o3kiCD69kOHICsfjZ2VcVu2lKYc=
github.com/microsoft/go-mssqldb v1.1.0/go.mod h1:LzkFdl4z2Ck+Hi+ycGOTbL56VEfgoyA2DvYejrNGbRk= github.com/microsoft/go-mssqldb v1.1.0/go.mod h1:LzkFdl4z2Ck+Hi+ycGOTbL56VEfgoyA2DvYejrNGbRk=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA=
github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
@ -130,6 +159,12 @@ github.com/segmentio/encoding v0.1.14 h1:BfnglNbNRohLaBLf93uP5/IwKqeWrezXK/g6IRn
github.com/segmentio/encoding v0.1.14/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw= github.com/segmentio/encoding v0.1.14/go.mod h1:RWhr02uzMB9gQC1x+MfYxedtmBibb9cZ6Vv9VxRSSbw=
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec= github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw= github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@ -137,8 +172,10 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
@ -156,6 +193,7 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
@ -171,6 +209,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
@ -190,6 +229,7 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -197,6 +237,7 @@ golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
@ -205,6 +246,7 @@ golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
@ -230,6 +272,7 @@ gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHN
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -23,7 +23,7 @@ element
| exprElement; | exprElement;
// cmpr is a comparison operator. // cmpr is a comparison operator.
cmpr: LT_EQ | LT | GT_EQ | GT | EQ | NEQ; //cmpr: LT_EQ | LT | GT_EQ | GT | EQ | NEQ;
@ -41,12 +41,47 @@ funcName
// an underscore to the func name, e.g. _date(xyz). // an underscore to the func name, e.g. _date(xyz).
PROPRIETARY_FUNC_NAME: '_' ID; PROPRIETARY_FUNC_NAME: '_' ID;
/*
join
----
join: ('join') '(' joinConstraint ')'; join implements SQL's JOIN mechanism.
@sakila_pg | .actor | join(.film_actor, .actor_id)
@sakila_pg | .actor | join(@sakila_my.film_actor, .actor_id)
@sakila_pg | .actor | join(.film_actor, .actor.actor_id == .film_actor.actor_id)
@sakila_pg | .actor:a | join(.film_actor:fa, .a.actor_id == .fa.actor_id)
@sakila_pg.actor:a | join(@sakila_my.film_actor:fa, .a.actor_id == .fa.actor_id)
See:
- https://www.sqlite.org/syntax/join-clause.html
- https://www.sqlite.org/syntax/join-operator.html
*/
join: JOIN_TYPE '(' joinTable (',' expr)? ')';
joinTable: (HANDLE)? NAME (alias)?;
// JOIN_TYPE is the set of join types, and their aliases.
// Note that not every database may support every join type, but
// this is not the concern of the grammar.
//
// Note that NATURAL JOIN is not supported, as its implementation
// is spotty in various DBs, and it's often considered an anti-pattern.
JOIN_TYPE
: 'join'
| 'inner_join'
| 'left_join'
| 'ljoin'
| 'left_outer_join'
| 'lojoin'
| 'right_join'
| 'rjoin'
| 'right_outer_join'
| 'rojoin'
| 'full_outer_join'
| 'fojoin'
| 'cross_join'
| 'xjoin'
;
joinConstraint
: selector cmpr selector // .user.uid == .address.userid
| selector ; // .uid
/* /*
uniqueFunc uniqueFunc

View File

@ -24,6 +24,8 @@ func (v *parseTreeVisitor) VisitAlias(ctx *slq.AliasContext) any {
switch node := v.cur.(type) { switch node := v.cur.(type) {
case *SelectorNode: case *SelectorNode:
node.alias = alias node.alias = alias
case *TblSelectorNode:
node.alias = alias
case *ExprElementNode: case *ExprElementNode:
node.alias = alias node.alias = alias
case *FuncNode: case *FuncNode:

View File

@ -1,7 +1,10 @@
// Package ast holds types and functionality for the SLQ AST. // Package ast holds types and functionality for the SLQ AST.
// //
// Note: the SLQ language implementation is fairly rudimentary // The entrypoint is ast.Parse, which accepts a SLQ query, and
// and has some incomplete functionality. // returns an *ast.AST. The ast is a tree of ast.Node instances.
//
// Note that much of the testing of package ast is performed in
// package libsq.
package ast package ast
import ( import (
@ -62,7 +65,6 @@ func buildAST(log *slog.Logger, query slq.IQueryContext) (*AST, error) {
{typeSelectorNode, narrowTblSel}, {typeSelectorNode, narrowTblSel},
{typeSelectorNode, narrowTblColSel}, {typeSelectorNode, narrowTblColSel},
{typeSelectorNode, narrowColSel}, {typeSelectorNode, narrowColSel},
{typeJoinNode, determineJoinTables},
{typeRowRangeNode, verifyRowRange}, {typeRowRangeNode, verifyRowRange},
} }
@ -152,13 +154,13 @@ func (a *AST) SetChildren(children []Node) error {
return nil return nil
} }
// Context implements ast.Node. // context implements ast.Node.
func (a *AST) Context() antlr.ParseTree { func (a *AST) context() antlr.ParseTree {
return a.ctx return a.ctx
} }
// SetContext implements ast.Node. // setContext implements ast.Node.
func (a *AST) SetContext(ctx antlr.ParseTree) error { func (a *AST) setContext(ctx antlr.ParseTree) error {
qCtx, ok := ctx.(*slq.QueryContext) qCtx, ok := ctx.(*slq.QueryContext)
if !ok { if !ok {
return errorf("expected *parser.QueryContext, but got %T", ctx) return errorf("expected *parser.QueryContext, but got %T", ctx)

View File

@ -57,7 +57,7 @@ func (ex *ExprElementNode) ExprNode() *ExprNode {
// SetChildren implements Node. // SetChildren implements Node.
func (ex *ExprElementNode) SetChildren(children []Node) error { func (ex *ExprElementNode) SetChildren(children []Node) error {
ex.setChildren(children) ex.doSetChildren(children)
return nil return nil
} }
@ -142,7 +142,7 @@ func (n *ExprNode) AddChild(child Node) error {
// SetChildren implements Node. // SetChildren implements Node.
func (n *ExprNode) SetChildren(children []Node) error { func (n *ExprNode) SetChildren(children []Node) error {
n.setChildren(children) n.doSetChildren(children)
return nil return nil
} }
@ -154,13 +154,19 @@ func (n *ExprNode) String() string {
// VisitExpr implements slq.SLQVisitor. // VisitExpr implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitExpr(ctx *slq.ExprContext) any { func (v *parseTreeVisitor) VisitExpr(ctx *slq.ExprContext) any {
// check if the expr is a selector, e.g. ".uid" // Historically, if an expression only contains a selector, then
if selCtx := ctx.Selector(); selCtx != nil { // we want to elide the expression and directly add the selector.
selNode, err := newSelectorNode(v.cur, selCtx) // However, this may have been a bad choice? For ast.JoinNode, we
if err != nil { // want to always have its child be an ast.ExprNode.
return err // This mechanism should be revisited.
if _, ok := v.cur.(*JoinNode); !ok {
if selCtx := ctx.Selector(); selCtx != nil {
selNode, err := newSelectorNode(v.cur, selCtx)
if err != nil {
return err
}
return v.cur.AddChild(selNode)
} }
return v.cur.AddChild(selNode)
} }
node := &ExprNode{} node := &ExprNode{}

View File

@ -60,7 +60,7 @@ func (fn *FuncNode) Alias() string {
// SetChildren implements Node. // SetChildren implements Node.
func (fn *FuncNode) SetChildren(children []Node) error { func (fn *FuncNode) SetChildren(children []Node) error {
fn.setChildren(children) fn.doSetChildren(children)
return nil return nil
} }

View File

@ -35,7 +35,7 @@ func (n *GroupByNode) SetChildren(children []Node) error {
return err return err
} }
n.setChildren(children) n.doSetChildren(children)
return nil return nil
} }

View File

@ -3,6 +3,8 @@ package ast
import ( import (
"reflect" "reflect"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/samber/lo" "github.com/samber/lo"
) )
@ -165,15 +167,15 @@ func (in *Inspector) FindGroupByNode() (*GroupByNode, error) {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }
// FindTablerSegments returns the segments that have at least one child // FindTableSegments returns the segments that have at least one child
// that implements Tabler. // that is a ast.TblSelectorNode.
func (in *Inspector) FindTablerSegments() []*SegmentNode { func (in *Inspector) FindTableSegments() []*SegmentNode {
segs := in.ast.Segments() segs := in.ast.Segments()
selSegs := make([]*SegmentNode, 0, 2) selSegs := make([]*SegmentNode, 0, 2)
for _, seg := range segs { for _, seg := range segs {
for _, child := range seg.Children() { for _, child := range seg.Children() {
if _, ok := child.(Tabler); ok { if _, ok := child.(*TblSelectorNode); ok {
selSegs = append(selSegs, seg) selSegs = append(selSegs, seg)
break break
} }
@ -203,10 +205,32 @@ func (in *Inspector) FindFirstHandle() (handle string) {
return "" return ""
} }
// FindFinalTablerSegment returns the final segment that // FindFirstTableSelector returns the first top-level (child of a segment)
// has at least one child that implements Tabler. // table selector node.
func (in *Inspector) FindFinalTablerSegment() (*SegmentNode, error) { func (in *Inspector) FindFirstTableSelector() *TblSelectorNode {
selectableSegs := in.FindTablerSegments() segs := in.ast.Segments()
if len(segs) == 0 {
return nil
}
var tblSelNode *TblSelectorNode
var ok bool
for _, seg := range segs {
for _, child := range seg.Children() {
if tblSelNode, ok = child.(*TblSelectorNode); ok {
return tblSelNode
}
}
}
return nil
}
// FindFinalTableSegment returns the final segment that
// has at least one child that is an ast.TblSelectorNode.
func (in *Inspector) FindFinalTableSegment() (*SegmentNode, error) {
selectableSegs := in.FindTableSegments()
if len(selectableSegs) == 0 { if len(selectableSegs) == 0 {
return nil, errorf("no selectable segments") return nil, errorf("no selectable segments")
} }
@ -214,6 +238,21 @@ func (in *Inspector) FindFinalTablerSegment() (*SegmentNode, error) {
return selectableSeg, nil return selectableSeg, nil
} }
// FindJoins returns all ast.JoinNode instances.
func (in *Inspector) FindJoins() ([]*JoinNode, error) {
nodes := in.FindNodes(typeJoinNode)
joinNodes := make([]*JoinNode, len(nodes))
var ok bool
for i := range nodes {
joinNodes[i], ok = nodes[i].(*JoinNode)
if !ok {
return nil, errz.Errorf("expected %T but got %T", (*JoinNode)(nil), nodes[i])
}
}
return joinNodes, nil
}
// FindUniqueNode returns any UniqueNode, or nil. // FindUniqueNode returns any UniqueNode, or nil.
func (in *Inspector) FindUniqueNode() (*UniqueNode, error) { func (in *Inspector) FindUniqueNode() (*UniqueNode, error) {
nodes := in.FindNodes(typeUniqueNode) nodes := in.FindNodes(typeUniqueNode)

View File

@ -6,9 +6,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestInspector_findSelectableSegments(t *testing.T) { func TestInspector_findTableSegments(t *testing.T) {
// `@mydb1 | .user | .uid, .username` const q1 = `@mydb1 | .user | .uid, .username`
ast, err := buildInitialAST(t, fixtSelect1)
ast, err := buildInitialAST(t, q1)
require.Nil(t, err) require.Nil(t, err)
err = NewWalker(ast).AddVisitor(typeSelectorNode, narrowTblSel).Walk() err = NewWalker(ast).AddVisitor(typeSelectorNode, narrowTblSel).Walk()
require.Nil(t, err) require.Nil(t, err)
@ -18,26 +19,9 @@ func TestInspector_findSelectableSegments(t *testing.T) {
segs := ast.Segments() segs := ast.Segments()
require.Equal(t, 3, len(segs)) require.Equal(t, 3, len(segs))
selSegs := insp.FindTablerSegments() selSegs := insp.FindTableSegments()
require.Equal(t, 1, len(selSegs), "should be 1 selectable segment: the tbl sel segment") require.Equal(t, 1, len(selSegs), "should be 1 table segment: the tbl sel segment")
finalSelSeg, err := insp.FindFinalTablerSegment() finalSelSeg, err := insp.FindFinalTableSegment()
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, selSegs[0], finalSelSeg) require.Equal(t, selSegs[0], finalSelSeg)
// `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country`
ast, err = buildInitialAST(t, fixtJoinQuery1)
require.Nil(t, err)
err = NewWalker(ast).AddVisitor(typeSelectorNode, narrowTblSel).Walk()
require.Nil(t, err)
insp = NewInspector(ast)
segs = ast.Segments()
require.Equal(t, 4, len(segs))
selSegs = insp.FindTablerSegments()
require.Equal(t, 2, len(selSegs), "should be 2 selectable segments: the tbl selector segment, and the join segment")
finalSelSeg, err = insp.FindFinalTablerSegment()
require.Nil(t, err)
require.Equal(t, selSegs[1], finalSelSeg)
} }

File diff suppressed because one or more lines are too long

View File

@ -16,8 +16,8 @@ T__14=15
T__15=16 T__15=16
T__16=17 T__16=17
T__17=18 T__17=18
T__18=19 PROPRIETARY_FUNC_NAME=19
PROPRIETARY_FUNC_NAME=20 JOIN_TYPE=20
WHERE=21 WHERE=21
GROUP_BY=22 GROUP_BY=22
ORDER_ASC=23 ORDER_ASC=23
@ -53,19 +53,18 @@ LINECOMMENT=49
'avg'=4 'avg'=4
'max'=5 'max'=5
'min'=6 'min'=6
'join'=7 'unique'=7
'unique'=8 'count'=8
'count'=9 '.['=9
'.['=10 '||'=10
'||'=11 '/'=11
'/'=12 '%'=12
'%'=13 '<<'=13
'<<'=14 '>>'=14
'>>'=15 '&'=15
'&'=16 '&&'=16
'&&'=17 '~'=17
'~'=18 '!'=18
'!'=19
'group_by'=22 'group_by'=22
'+'=23 '+'=23
'-'=24 '-'=24

File diff suppressed because one or more lines are too long

View File

@ -16,8 +16,8 @@ T__14=15
T__15=16 T__15=16
T__16=17 T__16=17
T__17=18 T__17=18
T__18=19 PROPRIETARY_FUNC_NAME=19
PROPRIETARY_FUNC_NAME=20 JOIN_TYPE=20
WHERE=21 WHERE=21
GROUP_BY=22 GROUP_BY=22
ORDER_ASC=23 ORDER_ASC=23
@ -53,19 +53,18 @@ LINECOMMENT=49
'avg'=4 'avg'=4
'max'=5 'max'=5
'min'=6 'min'=6
'join'=7 'unique'=7
'unique'=8 'count'=8
'count'=9 '.['=9
'.['=10 '||'=10
'||'=11 '/'=11
'/'=12 '%'=12
'%'=13 '<<'=13
'<<'=14 '>>'=14
'>>'=15 '&'=15
'&'=16 '&&'=16
'&&'=17 '~'=17
'~'=18 '!'=18
'!'=19
'group_by'=22 'group_by'=22
'+'=23 '+'=23
'-'=24 '-'=24

View File

@ -44,12 +44,6 @@ func (s *BaseSLQListener) EnterElement(ctx *ElementContext) {}
// ExitElement is called when production element is exited. // ExitElement is called when production element is exited.
func (s *BaseSLQListener) ExitElement(ctx *ElementContext) {} func (s *BaseSLQListener) ExitElement(ctx *ElementContext) {}
// EnterCmpr is called when production cmpr is entered.
func (s *BaseSLQListener) EnterCmpr(ctx *CmprContext) {}
// ExitCmpr is called when production cmpr is exited.
func (s *BaseSLQListener) ExitCmpr(ctx *CmprContext) {}
// EnterFuncElement is called when production funcElement is entered. // EnterFuncElement is called when production funcElement is entered.
func (s *BaseSLQListener) EnterFuncElement(ctx *FuncElementContext) {} func (s *BaseSLQListener) EnterFuncElement(ctx *FuncElementContext) {}
@ -74,11 +68,11 @@ func (s *BaseSLQListener) EnterJoin(ctx *JoinContext) {}
// ExitJoin is called when production join is exited. // ExitJoin is called when production join is exited.
func (s *BaseSLQListener) ExitJoin(ctx *JoinContext) {} func (s *BaseSLQListener) ExitJoin(ctx *JoinContext) {}
// EnterJoinConstraint is called when production joinConstraint is entered. // EnterJoinTable is called when production joinTable is entered.
func (s *BaseSLQListener) EnterJoinConstraint(ctx *JoinConstraintContext) {} func (s *BaseSLQListener) EnterJoinTable(ctx *JoinTableContext) {}
// ExitJoinConstraint is called when production joinConstraint is exited. // ExitJoinTable is called when production joinTable is exited.
func (s *BaseSLQListener) ExitJoinConstraint(ctx *JoinConstraintContext) {} func (s *BaseSLQListener) ExitJoinTable(ctx *JoinTableContext) {}
// EnterUniqueFunc is called when production uniqueFunc is entered. // EnterUniqueFunc is called when production uniqueFunc is entered.
func (s *BaseSLQListener) EnterUniqueFunc(ctx *UniqueFuncContext) {} func (s *BaseSLQListener) EnterUniqueFunc(ctx *UniqueFuncContext) {}

View File

@ -23,10 +23,6 @@ func (v *BaseSLQVisitor) VisitElement(ctx *ElementContext) interface{} {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }
func (v *BaseSLQVisitor) VisitCmpr(ctx *CmprContext) interface{} {
return v.VisitChildren(ctx)
}
func (v *BaseSLQVisitor) VisitFuncElement(ctx *FuncElementContext) interface{} { func (v *BaseSLQVisitor) VisitFuncElement(ctx *FuncElementContext) interface{} {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }
@ -43,7 +39,7 @@ func (v *BaseSLQVisitor) VisitJoin(ctx *JoinContext) interface{} {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }
func (v *BaseSLQVisitor) VisitJoinConstraint(ctx *JoinConstraintContext) interface{} { func (v *BaseSLQVisitor) VisitJoinTable(ctx *JoinTableContext) interface{} {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }

View File

@ -44,15 +44,15 @@ func slqlexerLexerInit() {
"DEFAULT_MODE", "DEFAULT_MODE",
} }
staticData.literalNames = []string{ staticData.literalNames = []string{
"", "';'", "'*'", "'sum'", "'avg'", "'max'", "'min'", "'join'", "'unique'", "", "';'", "'*'", "'sum'", "'avg'", "'max'", "'min'", "'unique'", "'count'",
"'count'", "'.['", "'||'", "'/'", "'%'", "'<<'", "'>>'", "'&'", "'&&'", "'.['", "'||'", "'/'", "'%'", "'<<'", "'>>'", "'&'", "'&&'", "'~'",
"'~'", "'!'", "", "", "'group_by'", "'+'", "'-'", "", "", "", "'null'", "'!'", "", "", "", "'group_by'", "'+'", "'-'", "", "", "", "'null'",
"", "", "'('", "')'", "'['", "']'", "','", "'|'", "':'", "", "", "'<='", "", "", "'('", "')'", "'['", "']'", "','", "'|'", "':'", "", "", "'<='",
"'<'", "'>='", "'>'", "'!='", "'=='", "'<'", "'>='", "'>'", "'!='", "'=='",
} }
staticData.symbolicNames = []string{ staticData.symbolicNames = []string{
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "PROPRIETARY_FUNC_NAME", "WHERE", "GROUP_BY", "ORDER_ASC", "", "", "PROPRIETARY_FUNC_NAME", "JOIN_TYPE", "WHERE", "GROUP_BY", "ORDER_ASC",
"ORDER_DESC", "ORDER_BY", "ALIAS_RESERVED", "ARG", "NULL", "ID", "WS", "ORDER_DESC", "ORDER_BY", "ALIAS_RESERVED", "ARG", "NULL", "ID", "WS",
"LPAR", "RPAR", "LBRA", "RBRA", "COMMA", "PIPE", "COLON", "NN", "NUMBER", "LPAR", "RPAR", "LBRA", "RBRA", "COMMA", "PIPE", "COLON", "NN", "NUMBER",
"LT_EQ", "LT", "GT_EQ", "GT", "NEQ", "EQ", "NAME", "HANDLE", "STRING", "LT_EQ", "LT", "GT_EQ", "GT", "NEQ", "EQ", "NAME", "HANDLE", "STRING",
@ -61,17 +61,17 @@ func slqlexerLexerInit() {
staticData.ruleNames = []string{ staticData.ruleNames = []string{
"T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8", "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8",
"T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16", "T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16",
"T__17", "T__18", "PROPRIETARY_FUNC_NAME", "WHERE", "GROUP_BY", "ORDER_ASC", "T__17", "PROPRIETARY_FUNC_NAME", "JOIN_TYPE", "WHERE", "GROUP_BY",
"ORDER_DESC", "ORDER_BY", "ALIAS_RESERVED", "ARG", "NULL", "ID", "WS", "ORDER_ASC", "ORDER_DESC", "ORDER_BY", "ALIAS_RESERVED", "ARG", "NULL",
"LPAR", "RPAR", "LBRA", "RBRA", "COMMA", "PIPE", "COLON", "NN", "NUMBER", "ID", "WS", "LPAR", "RPAR", "LBRA", "RBRA", "COMMA", "PIPE", "COLON",
"INTF", "EXP", "LT_EQ", "LT", "GT_EQ", "GT", "NEQ", "EQ", "NAME", "HANDLE", "NN", "NUMBER", "INTF", "EXP", "LT_EQ", "LT", "GT_EQ", "GT", "NEQ",
"STRING", "ESC", "UNICODE", "HEX", "DIGIT", "A", "B", "C", "D", "E", "EQ", "NAME", "HANDLE", "STRING", "ESC", "UNICODE", "HEX", "DIGIT",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"T", "U", "V", "W", "X", "Y", "Z", "LINECOMMENT", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "LINECOMMENT",
} }
staticData.predictionContextCache = antlr.NewPredictionContextCache() staticData.predictionContextCache = antlr.NewPredictionContextCache()
staticData.serializedATN = []int32{ staticData.serializedATN = []int32{
4, 0, 49, 529, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 0, 49, 648, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2,
4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2,
10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15,
7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7,
@ -88,225 +88,281 @@ func slqlexerLexerInit() {
73, 7, 73, 2, 74, 7, 74, 2, 75, 7, 75, 2, 76, 7, 76, 2, 77, 7, 77, 2, 78, 73, 7, 73, 2, 74, 7, 74, 2, 75, 7, 75, 2, 76, 7, 76, 2, 77, 7, 77, 2, 78,
7, 78, 2, 79, 7, 79, 2, 80, 7, 80, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 2, 7, 78, 2, 79, 7, 79, 2, 80, 7, 80, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 2,
1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 5, 1, 5,
1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 5, 1, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7,
1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 10, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 10, 1, 10, 1,
1, 10, 1, 10, 1, 11, 1, 11, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 13, 1, 13, 1, 13, 1, 14, 1, 14, 1, 15,
14, 1, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 16, 1, 17, 1, 17, 1, 18, 1, 18, 1, 15, 1, 15, 1, 16, 1, 16, 1, 17, 1, 17, 1, 18, 1, 18, 1, 18, 1, 19, 1,
1, 19, 1, 19, 1, 19, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
20, 1, 20, 1, 20, 1, 20, 3, 20, 241, 8, 20, 1, 21, 1, 21, 1, 21, 1, 21, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1,
1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1, 22, 1, 22, 1, 23, 1, 23, 1, 24, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1,
1, 24, 1, 24, 1, 24, 3, 24, 271, 8, 24, 1, 25, 1, 25, 1, 25, 1, 25, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1,
19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1,
19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1,
19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 1, 19,
1, 19, 1, 19, 1, 19, 1, 19, 1, 19, 3, 19, 347, 8, 19, 1, 20, 1, 20, 1,
20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 1, 20, 3, 20, 360,
8, 20, 1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1, 21, 1,
22, 1, 22, 1, 23, 1, 23, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24,
1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 1, 24, 3, 24, 390, 8,
24, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25,
1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1,
25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25,
1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1,
25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 3, 25, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25, 1, 25,
329, 8, 25, 1, 26, 1, 26, 1, 26, 1, 27, 1, 27, 1, 27, 1, 27, 1, 27, 1, 1, 25, 1, 25, 1, 25, 1, 25, 3, 25, 448, 8, 25, 1, 26, 1, 26, 1, 26, 1,
28, 1, 28, 5, 28, 341, 8, 28, 10, 28, 12, 28, 344, 9, 28, 1, 29, 4, 29, 27, 1, 27, 1, 27, 1, 27, 1, 27, 1, 28, 1, 28, 5, 28, 460, 8, 28, 10, 28,
347, 8, 29, 11, 29, 12, 29, 348, 1, 29, 1, 29, 1, 30, 1, 30, 1, 31, 1, 12, 28, 463, 9, 28, 1, 29, 4, 29, 466, 8, 29, 11, 29, 12, 29, 467, 1, 29,
31, 1, 32, 1, 32, 1, 33, 1, 33, 1, 34, 1, 34, 1, 35, 1, 35, 1, 36, 1, 36, 1, 29, 1, 30, 1, 30, 1, 31, 1, 31, 1, 32, 1, 32, 1, 33, 1, 33, 1, 34, 1,
1, 37, 1, 37, 1, 38, 1, 38, 3, 38, 371, 8, 38, 1, 38, 1, 38, 1, 38, 4, 34, 1, 35, 1, 35, 1, 36, 1, 36, 1, 37, 1, 37, 1, 38, 1, 38, 3, 38, 490,
38, 376, 8, 38, 11, 38, 12, 38, 377, 1, 38, 3, 38, 381, 8, 38, 1, 38, 3, 8, 38, 1, 38, 1, 38, 1, 38, 4, 38, 495, 8, 38, 11, 38, 12, 38, 496, 1,
38, 384, 8, 38, 1, 38, 1, 38, 1, 38, 1, 38, 3, 38, 390, 8, 38, 1, 38, 3, 38, 3, 38, 500, 8, 38, 1, 38, 3, 38, 503, 8, 38, 1, 38, 1, 38, 1, 38, 1,
38, 393, 8, 38, 1, 39, 1, 39, 1, 39, 5, 39, 398, 8, 39, 10, 39, 12, 39, 38, 3, 38, 509, 8, 38, 1, 38, 3, 38, 512, 8, 38, 1, 39, 1, 39, 1, 39, 5,
401, 9, 39, 3, 39, 403, 8, 39, 1, 40, 1, 40, 3, 40, 407, 8, 40, 1, 40, 39, 517, 8, 39, 10, 39, 12, 39, 520, 9, 39, 3, 39, 522, 8, 39, 1, 40, 1,
1, 40, 1, 41, 1, 41, 1, 41, 1, 42, 1, 42, 1, 43, 1, 43, 1, 43, 1, 44, 1, 40, 3, 40, 526, 8, 40, 1, 40, 1, 40, 1, 41, 1, 41, 1, 41, 1, 42, 1, 42,
44, 1, 45, 1, 45, 1, 45, 1, 46, 1, 46, 1, 46, 1, 47, 1, 47, 1, 47, 1, 47, 1, 43, 1, 43, 1, 43, 1, 44, 1, 44, 1, 45, 1, 45, 1, 45, 1, 46, 1, 46, 1,
3, 47, 431, 8, 47, 1, 48, 1, 48, 1, 48, 1, 48, 5, 48, 437, 8, 48, 10, 48, 46, 1, 47, 1, 47, 1, 47, 1, 47, 3, 47, 550, 8, 47, 1, 48, 1, 48, 1, 48,
12, 48, 440, 9, 48, 1, 49, 1, 49, 1, 49, 5, 49, 445, 8, 49, 10, 49, 12, 1, 48, 5, 48, 556, 8, 48, 10, 48, 12, 48, 559, 9, 48, 1, 49, 1, 49, 1,
49, 448, 9, 49, 1, 49, 1, 49, 1, 50, 1, 50, 1, 50, 3, 50, 455, 8, 50, 1, 49, 5, 49, 564, 8, 49, 10, 49, 12, 49, 567, 9, 49, 1, 49, 1, 49, 1, 50,
51, 1, 51, 1, 51, 1, 51, 1, 51, 1, 51, 1, 52, 1, 52, 1, 53, 1, 53, 1, 54, 1, 50, 1, 50, 3, 50, 574, 8, 50, 1, 51, 1, 51, 1, 51, 1, 51, 1, 51, 1,
1, 54, 1, 55, 1, 55, 1, 56, 1, 56, 1, 57, 1, 57, 1, 58, 1, 58, 1, 59, 1, 51, 1, 52, 1, 52, 1, 53, 1, 53, 1, 54, 1, 54, 1, 55, 1, 55, 1, 56, 1, 56,
59, 1, 60, 1, 60, 1, 61, 1, 61, 1, 62, 1, 62, 1, 63, 1, 63, 1, 64, 1, 64, 1, 57, 1, 57, 1, 58, 1, 58, 1, 59, 1, 59, 1, 60, 1, 60, 1, 61, 1, 61, 1,
1, 65, 1, 65, 1, 66, 1, 66, 1, 67, 1, 67, 1, 68, 1, 68, 1, 69, 1, 69, 1, 62, 1, 62, 1, 63, 1, 63, 1, 64, 1, 64, 1, 65, 1, 65, 1, 66, 1, 66, 1, 67,
70, 1, 70, 1, 71, 1, 71, 1, 72, 1, 72, 1, 73, 1, 73, 1, 74, 1, 74, 1, 75, 1, 67, 1, 68, 1, 68, 1, 69, 1, 69, 1, 70, 1, 70, 1, 71, 1, 71, 1, 72, 1,
1, 75, 1, 76, 1, 76, 1, 77, 1, 77, 1, 78, 1, 78, 1, 79, 1, 79, 1, 80, 1, 72, 1, 73, 1, 73, 1, 74, 1, 74, 1, 75, 1, 75, 1, 76, 1, 76, 1, 77, 1, 77,
80, 5, 80, 521, 8, 80, 10, 80, 12, 80, 524, 9, 80, 1, 80, 1, 80, 1, 80, 1, 78, 1, 78, 1, 79, 1, 79, 1, 80, 1, 80, 5, 80, 640, 8, 80, 10, 80, 12,
1, 80, 1, 522, 0, 81, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 80, 643, 9, 80, 1, 80, 1, 80, 1, 80, 1, 80, 1, 641, 0, 81, 1, 1, 3, 2,
17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 14, 29, 15, 31, 16, 33, 17, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25,
35, 18, 37, 19, 39, 20, 41, 21, 43, 22, 45, 23, 47, 24, 49, 25, 51, 26, 13, 27, 14, 29, 15, 31, 16, 33, 17, 35, 18, 37, 19, 39, 20, 41, 21, 43,
53, 27, 55, 28, 57, 29, 59, 30, 61, 31, 63, 32, 65, 33, 67, 34, 69, 35, 22, 45, 23, 47, 24, 49, 25, 51, 26, 53, 27, 55, 28, 57, 29, 59, 30, 61,
71, 36, 73, 37, 75, 38, 77, 39, 79, 0, 81, 0, 83, 40, 85, 41, 87, 42, 89, 31, 63, 32, 65, 33, 67, 34, 69, 35, 71, 36, 73, 37, 75, 38, 77, 39, 79,
43, 91, 44, 93, 45, 95, 46, 97, 47, 99, 48, 101, 0, 103, 0, 105, 0, 107, 0, 81, 0, 83, 40, 85, 41, 87, 42, 89, 43, 91, 44, 93, 45, 95, 46, 97, 47,
0, 109, 0, 111, 0, 113, 0, 115, 0, 117, 0, 119, 0, 121, 0, 123, 0, 125, 99, 48, 101, 0, 103, 0, 105, 0, 107, 0, 109, 0, 111, 0, 113, 0, 115, 0,
0, 127, 0, 129, 0, 131, 0, 133, 0, 135, 0, 137, 0, 139, 0, 141, 0, 143, 117, 0, 119, 0, 121, 0, 123, 0, 125, 0, 127, 0, 129, 0, 131, 0, 133, 0,
0, 145, 0, 147, 0, 149, 0, 151, 0, 153, 0, 155, 0, 157, 0, 159, 0, 161, 135, 0, 137, 0, 139, 0, 141, 0, 143, 0, 145, 0, 147, 0, 149, 0, 151, 0,
49, 1, 0, 35, 3, 0, 65, 90, 95, 95, 97, 122, 4, 0, 48, 57, 65, 90, 95, 153, 0, 155, 0, 157, 0, 159, 0, 161, 49, 1, 0, 35, 3, 0, 65, 90, 95, 95,
95, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 1, 0, 48, 57, 1, 0, 49, 57, 2, 97, 122, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 3, 0, 9, 10, 13, 13, 32,
0, 69, 69, 101, 101, 2, 0, 43, 43, 45, 45, 2, 0, 34, 34, 92, 92, 8, 0, 32, 1, 0, 48, 57, 1, 0, 49, 57, 2, 0, 69, 69, 101, 101, 2, 0, 43, 43, 45,
34, 34, 47, 47, 92, 92, 98, 98, 102, 102, 110, 110, 114, 114, 116, 116, 45, 2, 0, 34, 34, 92, 92, 8, 0, 34, 34, 47, 47, 92, 92, 98, 98, 102, 102,
3, 0, 48, 57, 65, 70, 97, 102, 2, 0, 65, 65, 97, 97, 2, 0, 66, 66, 98, 110, 110, 114, 114, 116, 116, 3, 0, 48, 57, 65, 70, 97, 102, 2, 0, 65,
98, 2, 0, 67, 67, 99, 99, 2, 0, 68, 68, 100, 100, 2, 0, 70, 70, 102, 102, 65, 97, 97, 2, 0, 66, 66, 98, 98, 2, 0, 67, 67, 99, 99, 2, 0, 68, 68, 100,
2, 0, 71, 71, 103, 103, 2, 0, 72, 72, 104, 104, 2, 0, 73, 73, 105, 105, 100, 2, 0, 70, 70, 102, 102, 2, 0, 71, 71, 103, 103, 2, 0, 72, 72, 104,
2, 0, 74, 74, 106, 106, 2, 0, 75, 75, 107, 107, 2, 0, 76, 76, 108, 108, 104, 2, 0, 73, 73, 105, 105, 2, 0, 74, 74, 106, 106, 2, 0, 75, 75, 107,
2, 0, 77, 77, 109, 109, 2, 0, 78, 78, 110, 110, 2, 0, 79, 79, 111, 111, 107, 2, 0, 76, 76, 108, 108, 2, 0, 77, 77, 109, 109, 2, 0, 78, 78, 110,
2, 0, 80, 80, 112, 112, 2, 0, 81, 81, 113, 113, 2, 0, 82, 82, 114, 114, 110, 2, 0, 79, 79, 111, 111, 2, 0, 80, 80, 112, 112, 2, 0, 81, 81, 113,
2, 0, 83, 83, 115, 115, 2, 0, 84, 84, 116, 116, 2, 0, 85, 85, 117, 117, 113, 2, 0, 82, 82, 114, 114, 2, 0, 83, 83, 115, 115, 2, 0, 84, 84, 116,
2, 0, 86, 86, 118, 118, 2, 0, 87, 87, 119, 119, 2, 0, 88, 88, 120, 120, 116, 2, 0, 85, 85, 117, 117, 2, 0, 86, 86, 118, 118, 2, 0, 87, 87, 119,
2, 0, 89, 89, 121, 121, 2, 0, 90, 90, 122, 122, 525, 0, 1, 1, 0, 0, 0, 119, 2, 0, 88, 88, 120, 120, 2, 0, 89, 89, 121, 121, 2, 0, 90, 90, 122,
0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 122, 657, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1,
0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15,
0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0,
0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 0, 33, 1, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 27, 1, 0, 0, 0, 0, 29, 1, 0, 0, 0,
0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 0, 31, 1, 0, 0, 0, 0, 33, 1, 0, 0, 0, 0, 35, 1, 0, 0, 0, 0, 37, 1, 0, 0,
1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0, 0, 0, 0, 47, 1, 0, 0, 0, 0, 0, 0, 39, 1, 0, 0, 0, 0, 41, 1, 0, 0, 0, 0, 43, 1, 0, 0, 0, 0, 45, 1, 0,
49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1, 0, 0, 0, 0, 55, 1, 0, 0, 0, 0, 0, 0, 47, 1, 0, 0, 0, 0, 49, 1, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 53, 1,
0, 57, 1, 0, 0, 0, 0, 59, 1, 0, 0, 0, 0, 61, 1, 0, 0, 0, 0, 63, 1, 0, 0, 0, 0, 0, 0, 55, 1, 0, 0, 0, 0, 57, 1, 0, 0, 0, 0, 59, 1, 0, 0, 0, 0, 61,
0, 0, 65, 1, 0, 0, 0, 0, 67, 1, 0, 0, 0, 0, 69, 1, 0, 0, 0, 0, 71, 1, 0, 1, 0, 0, 0, 0, 63, 1, 0, 0, 0, 0, 65, 1, 0, 0, 0, 0, 67, 1, 0, 0, 0, 0,
0, 0, 0, 73, 1, 0, 0, 0, 0, 75, 1, 0, 0, 0, 0, 77, 1, 0, 0, 0, 0, 83, 1, 69, 1, 0, 0, 0, 0, 71, 1, 0, 0, 0, 0, 73, 1, 0, 0, 0, 0, 75, 1, 0, 0, 0,
0, 0, 0, 0, 85, 1, 0, 0, 0, 0, 87, 1, 0, 0, 0, 0, 89, 1, 0, 0, 0, 0, 91, 0, 77, 1, 0, 0, 0, 0, 83, 1, 0, 0, 0, 0, 85, 1, 0, 0, 0, 0, 87, 1, 0, 0,
1, 0, 0, 0, 0, 93, 1, 0, 0, 0, 0, 95, 1, 0, 0, 0, 0, 97, 1, 0, 0, 0, 0, 0, 0, 89, 1, 0, 0, 0, 0, 91, 1, 0, 0, 0, 0, 93, 1, 0, 0, 0, 0, 95, 1, 0,
99, 1, 0, 0, 0, 0, 161, 1, 0, 0, 0, 1, 163, 1, 0, 0, 0, 3, 165, 1, 0, 0, 0, 0, 0, 97, 1, 0, 0, 0, 0, 99, 1, 0, 0, 0, 0, 161, 1, 0, 0, 0, 1, 163,
0, 5, 167, 1, 0, 0, 0, 7, 171, 1, 0, 0, 0, 9, 175, 1, 0, 0, 0, 11, 179, 1, 0, 0, 0, 3, 165, 1, 0, 0, 0, 5, 167, 1, 0, 0, 0, 7, 171, 1, 0, 0, 0,
1, 0, 0, 0, 13, 183, 1, 0, 0, 0, 15, 188, 1, 0, 0, 0, 17, 195, 1, 0, 0, 9, 175, 1, 0, 0, 0, 11, 179, 1, 0, 0, 0, 13, 183, 1, 0, 0, 0, 15, 190,
0, 19, 201, 1, 0, 0, 0, 21, 204, 1, 0, 0, 0, 23, 207, 1, 0, 0, 0, 25, 209, 1, 0, 0, 0, 17, 196, 1, 0, 0, 0, 19, 199, 1, 0, 0, 0, 21, 202, 1, 0, 0,
1, 0, 0, 0, 27, 211, 1, 0, 0, 0, 29, 214, 1, 0, 0, 0, 31, 217, 1, 0, 0, 0, 23, 204, 1, 0, 0, 0, 25, 206, 1, 0, 0, 0, 27, 209, 1, 0, 0, 0, 29, 212,
0, 33, 219, 1, 0, 0, 0, 35, 222, 1, 0, 0, 0, 37, 224, 1, 0, 0, 0, 39, 226, 1, 0, 0, 0, 31, 214, 1, 0, 0, 0, 33, 217, 1, 0, 0, 0, 35, 219, 1, 0, 0,
1, 0, 0, 0, 41, 240, 1, 0, 0, 0, 43, 242, 1, 0, 0, 0, 45, 251, 1, 0, 0, 0, 37, 221, 1, 0, 0, 0, 39, 346, 1, 0, 0, 0, 41, 359, 1, 0, 0, 0, 43, 361,
0, 47, 253, 1, 0, 0, 0, 49, 270, 1, 0, 0, 0, 51, 328, 1, 0, 0, 0, 53, 330, 1, 0, 0, 0, 45, 370, 1, 0, 0, 0, 47, 372, 1, 0, 0, 0, 49, 389, 1, 0, 0,
1, 0, 0, 0, 55, 333, 1, 0, 0, 0, 57, 338, 1, 0, 0, 0, 59, 346, 1, 0, 0, 0, 51, 447, 1, 0, 0, 0, 53, 449, 1, 0, 0, 0, 55, 452, 1, 0, 0, 0, 57, 457,
0, 61, 352, 1, 0, 0, 0, 63, 354, 1, 0, 0, 0, 65, 356, 1, 0, 0, 0, 67, 358, 1, 0, 0, 0, 59, 465, 1, 0, 0, 0, 61, 471, 1, 0, 0, 0, 63, 473, 1, 0, 0,
1, 0, 0, 0, 69, 360, 1, 0, 0, 0, 71, 362, 1, 0, 0, 0, 73, 364, 1, 0, 0, 0, 65, 475, 1, 0, 0, 0, 67, 477, 1, 0, 0, 0, 69, 479, 1, 0, 0, 0, 71, 481,
0, 75, 366, 1, 0, 0, 0, 77, 392, 1, 0, 0, 0, 79, 402, 1, 0, 0, 0, 81, 404, 1, 0, 0, 0, 73, 483, 1, 0, 0, 0, 75, 485, 1, 0, 0, 0, 77, 511, 1, 0, 0,
1, 0, 0, 0, 83, 410, 1, 0, 0, 0, 85, 413, 1, 0, 0, 0, 87, 415, 1, 0, 0, 0, 79, 521, 1, 0, 0, 0, 81, 523, 1, 0, 0, 0, 83, 529, 1, 0, 0, 0, 85, 532,
0, 89, 418, 1, 0, 0, 0, 91, 420, 1, 0, 0, 0, 93, 423, 1, 0, 0, 0, 95, 426, 1, 0, 0, 0, 87, 534, 1, 0, 0, 0, 89, 537, 1, 0, 0, 0, 91, 539, 1, 0, 0,
1, 0, 0, 0, 97, 432, 1, 0, 0, 0, 99, 441, 1, 0, 0, 0, 101, 451, 1, 0, 0, 0, 93, 542, 1, 0, 0, 0, 95, 545, 1, 0, 0, 0, 97, 551, 1, 0, 0, 0, 99, 560,
0, 103, 456, 1, 0, 0, 0, 105, 462, 1, 0, 0, 0, 107, 464, 1, 0, 0, 0, 109, 1, 0, 0, 0, 101, 570, 1, 0, 0, 0, 103, 575, 1, 0, 0, 0, 105, 581, 1, 0,
466, 1, 0, 0, 0, 111, 468, 1, 0, 0, 0, 113, 470, 1, 0, 0, 0, 115, 472, 0, 0, 107, 583, 1, 0, 0, 0, 109, 585, 1, 0, 0, 0, 111, 587, 1, 0, 0, 0,
1, 0, 0, 0, 117, 474, 1, 0, 0, 0, 119, 476, 1, 0, 0, 0, 121, 478, 1, 0, 113, 589, 1, 0, 0, 0, 115, 591, 1, 0, 0, 0, 117, 593, 1, 0, 0, 0, 119,
0, 0, 123, 480, 1, 0, 0, 0, 125, 482, 1, 0, 0, 0, 127, 484, 1, 0, 0, 0, 595, 1, 0, 0, 0, 121, 597, 1, 0, 0, 0, 123, 599, 1, 0, 0, 0, 125, 601,
129, 486, 1, 0, 0, 0, 131, 488, 1, 0, 0, 0, 133, 490, 1, 0, 0, 0, 135, 1, 0, 0, 0, 127, 603, 1, 0, 0, 0, 129, 605, 1, 0, 0, 0, 131, 607, 1, 0,
492, 1, 0, 0, 0, 137, 494, 1, 0, 0, 0, 139, 496, 1, 0, 0, 0, 141, 498, 0, 0, 133, 609, 1, 0, 0, 0, 135, 611, 1, 0, 0, 0, 137, 613, 1, 0, 0, 0,
1, 0, 0, 0, 143, 500, 1, 0, 0, 0, 145, 502, 1, 0, 0, 0, 147, 504, 1, 0, 139, 615, 1, 0, 0, 0, 141, 617, 1, 0, 0, 0, 143, 619, 1, 0, 0, 0, 145,
0, 0, 149, 506, 1, 0, 0, 0, 151, 508, 1, 0, 0, 0, 153, 510, 1, 0, 0, 0, 621, 1, 0, 0, 0, 147, 623, 1, 0, 0, 0, 149, 625, 1, 0, 0, 0, 151, 627,
155, 512, 1, 0, 0, 0, 157, 514, 1, 0, 0, 0, 159, 516, 1, 0, 0, 0, 161, 1, 0, 0, 0, 153, 629, 1, 0, 0, 0, 155, 631, 1, 0, 0, 0, 157, 633, 1, 0,
518, 1, 0, 0, 0, 163, 164, 5, 59, 0, 0, 164, 2, 1, 0, 0, 0, 165, 166, 5, 0, 0, 159, 635, 1, 0, 0, 0, 161, 637, 1, 0, 0, 0, 163, 164, 5, 59, 0, 0,
42, 0, 0, 166, 4, 1, 0, 0, 0, 167, 168, 5, 115, 0, 0, 168, 169, 5, 117, 164, 2, 1, 0, 0, 0, 165, 166, 5, 42, 0, 0, 166, 4, 1, 0, 0, 0, 167, 168,
0, 0, 169, 170, 5, 109, 0, 0, 170, 6, 1, 0, 0, 0, 171, 172, 5, 97, 0, 0, 5, 115, 0, 0, 168, 169, 5, 117, 0, 0, 169, 170, 5, 109, 0, 0, 170, 6, 1,
172, 173, 5, 118, 0, 0, 173, 174, 5, 103, 0, 0, 174, 8, 1, 0, 0, 0, 175, 0, 0, 0, 171, 172, 5, 97, 0, 0, 172, 173, 5, 118, 0, 0, 173, 174, 5, 103,
176, 5, 109, 0, 0, 176, 177, 5, 97, 0, 0, 177, 178, 5, 120, 0, 0, 178, 0, 0, 174, 8, 1, 0, 0, 0, 175, 176, 5, 109, 0, 0, 176, 177, 5, 97, 0, 0,
10, 1, 0, 0, 0, 179, 180, 5, 109, 0, 0, 180, 181, 5, 105, 0, 0, 181, 182, 177, 178, 5, 120, 0, 0, 178, 10, 1, 0, 0, 0, 179, 180, 5, 109, 0, 0, 180,
5, 110, 0, 0, 182, 12, 1, 0, 0, 0, 183, 184, 5, 106, 0, 0, 184, 185, 5, 181, 5, 105, 0, 0, 181, 182, 5, 110, 0, 0, 182, 12, 1, 0, 0, 0, 183, 184,
111, 0, 0, 185, 186, 5, 105, 0, 0, 186, 187, 5, 110, 0, 0, 187, 14, 1, 5, 117, 0, 0, 184, 185, 5, 110, 0, 0, 185, 186, 5, 105, 0, 0, 186, 187,
0, 0, 0, 188, 189, 5, 117, 0, 0, 189, 190, 5, 110, 0, 0, 190, 191, 5, 105, 5, 113, 0, 0, 187, 188, 5, 117, 0, 0, 188, 189, 5, 101, 0, 0, 189, 14,
0, 0, 191, 192, 5, 113, 0, 0, 192, 193, 5, 117, 0, 0, 193, 194, 5, 101, 1, 0, 0, 0, 190, 191, 5, 99, 0, 0, 191, 192, 5, 111, 0, 0, 192, 193, 5,
0, 0, 194, 16, 1, 0, 0, 0, 195, 196, 5, 99, 0, 0, 196, 197, 5, 111, 0, 117, 0, 0, 193, 194, 5, 110, 0, 0, 194, 195, 5, 116, 0, 0, 195, 16, 1,
0, 197, 198, 5, 117, 0, 0, 198, 199, 5, 110, 0, 0, 199, 200, 5, 116, 0, 0, 0, 0, 196, 197, 5, 46, 0, 0, 197, 198, 5, 91, 0, 0, 198, 18, 1, 0, 0,
0, 200, 18, 1, 0, 0, 0, 201, 202, 5, 46, 0, 0, 202, 203, 5, 91, 0, 0, 203, 0, 199, 200, 5, 124, 0, 0, 200, 201, 5, 124, 0, 0, 201, 20, 1, 0, 0, 0,
20, 1, 0, 0, 0, 204, 205, 5, 124, 0, 0, 205, 206, 5, 124, 0, 0, 206, 22, 202, 203, 5, 47, 0, 0, 203, 22, 1, 0, 0, 0, 204, 205, 5, 37, 0, 0, 205,
1, 0, 0, 0, 207, 208, 5, 47, 0, 0, 208, 24, 1, 0, 0, 0, 209, 210, 5, 37, 24, 1, 0, 0, 0, 206, 207, 5, 60, 0, 0, 207, 208, 5, 60, 0, 0, 208, 26,
0, 0, 210, 26, 1, 0, 0, 0, 211, 212, 5, 60, 0, 0, 212, 213, 5, 60, 0, 0, 1, 0, 0, 0, 209, 210, 5, 62, 0, 0, 210, 211, 5, 62, 0, 0, 211, 28, 1, 0,
213, 28, 1, 0, 0, 0, 214, 215, 5, 62, 0, 0, 215, 216, 5, 62, 0, 0, 216, 0, 0, 212, 213, 5, 38, 0, 0, 213, 30, 1, 0, 0, 0, 214, 215, 5, 38, 0, 0,
30, 1, 0, 0, 0, 217, 218, 5, 38, 0, 0, 218, 32, 1, 0, 0, 0, 219, 220, 5, 215, 216, 5, 38, 0, 0, 216, 32, 1, 0, 0, 0, 217, 218, 5, 126, 0, 0, 218,
38, 0, 0, 220, 221, 5, 38, 0, 0, 221, 34, 1, 0, 0, 0, 222, 223, 5, 126, 34, 1, 0, 0, 0, 219, 220, 5, 33, 0, 0, 220, 36, 1, 0, 0, 0, 221, 222, 5,
0, 0, 223, 36, 1, 0, 0, 0, 224, 225, 5, 33, 0, 0, 225, 38, 1, 0, 0, 0, 95, 0, 0, 222, 223, 3, 57, 28, 0, 223, 38, 1, 0, 0, 0, 224, 225, 5, 106,
226, 227, 5, 95, 0, 0, 227, 228, 3, 57, 28, 0, 228, 40, 1, 0, 0, 0, 229, 0, 0, 225, 226, 5, 111, 0, 0, 226, 227, 5, 105, 0, 0, 227, 347, 5, 110,
230, 5, 119, 0, 0, 230, 231, 5, 104, 0, 0, 231, 232, 5, 101, 0, 0, 232, 0, 0, 228, 229, 5, 105, 0, 0, 229, 230, 5, 110, 0, 0, 230, 231, 5, 110,
233, 5, 114, 0, 0, 233, 241, 5, 101, 0, 0, 234, 235, 5, 115, 0, 0, 235, 0, 0, 231, 232, 5, 101, 0, 0, 232, 233, 5, 114, 0, 0, 233, 234, 5, 95,
236, 5, 101, 0, 0, 236, 237, 5, 108, 0, 0, 237, 238, 5, 101, 0, 0, 238, 0, 0, 234, 235, 5, 106, 0, 0, 235, 236, 5, 111, 0, 0, 236, 237, 5, 105,
239, 5, 99, 0, 0, 239, 241, 5, 116, 0, 0, 240, 229, 1, 0, 0, 0, 240, 234, 0, 0, 237, 347, 5, 110, 0, 0, 238, 239, 5, 108, 0, 0, 239, 240, 5, 101,
1, 0, 0, 0, 241, 42, 1, 0, 0, 0, 242, 243, 5, 103, 0, 0, 243, 244, 5, 114, 0, 0, 240, 241, 5, 102, 0, 0, 241, 242, 5, 116, 0, 0, 242, 243, 5, 95,
0, 0, 244, 245, 5, 111, 0, 0, 245, 246, 5, 117, 0, 0, 246, 247, 5, 112, 0, 0, 243, 244, 5, 106, 0, 0, 244, 245, 5, 111, 0, 0, 245, 246, 5, 105,
0, 0, 247, 248, 5, 95, 0, 0, 248, 249, 5, 98, 0, 0, 249, 250, 5, 121, 0, 0, 0, 246, 347, 5, 110, 0, 0, 247, 248, 5, 108, 0, 0, 248, 249, 5, 106,
0, 250, 44, 1, 0, 0, 0, 251, 252, 5, 43, 0, 0, 252, 46, 1, 0, 0, 0, 253, 0, 0, 249, 250, 5, 111, 0, 0, 250, 251, 5, 105, 0, 0, 251, 347, 5, 110,
254, 5, 45, 0, 0, 254, 48, 1, 0, 0, 0, 255, 256, 5, 111, 0, 0, 256, 257, 0, 0, 252, 253, 5, 108, 0, 0, 253, 254, 5, 101, 0, 0, 254, 255, 5, 102,
5, 114, 0, 0, 257, 258, 5, 100, 0, 0, 258, 259, 5, 101, 0, 0, 259, 260, 0, 0, 255, 256, 5, 116, 0, 0, 256, 257, 5, 95, 0, 0, 257, 258, 5, 111,
5, 114, 0, 0, 260, 261, 5, 95, 0, 0, 261, 262, 5, 98, 0, 0, 262, 271, 5, 0, 0, 258, 259, 5, 117, 0, 0, 259, 260, 5, 116, 0, 0, 260, 261, 5, 101,
121, 0, 0, 263, 264, 5, 115, 0, 0, 264, 265, 5, 111, 0, 0, 265, 266, 5, 0, 0, 261, 262, 5, 114, 0, 0, 262, 263, 5, 95, 0, 0, 263, 264, 5, 106,
114, 0, 0, 266, 267, 5, 116, 0, 0, 267, 268, 5, 95, 0, 0, 268, 269, 5, 0, 0, 264, 265, 5, 111, 0, 0, 265, 266, 5, 105, 0, 0, 266, 347, 5, 110,
98, 0, 0, 269, 271, 5, 121, 0, 0, 270, 255, 1, 0, 0, 0, 270, 263, 1, 0, 0, 0, 267, 268, 5, 108, 0, 0, 268, 269, 5, 111, 0, 0, 269, 270, 5, 106,
0, 0, 271, 50, 1, 0, 0, 0, 272, 273, 5, 58, 0, 0, 273, 274, 5, 99, 0, 0, 0, 0, 270, 271, 5, 111, 0, 0, 271, 272, 5, 105, 0, 0, 272, 347, 5, 110,
274, 275, 5, 111, 0, 0, 275, 276, 5, 117, 0, 0, 276, 277, 5, 110, 0, 0, 0, 0, 273, 274, 5, 114, 0, 0, 274, 275, 5, 105, 0, 0, 275, 276, 5, 103,
277, 329, 5, 116, 0, 0, 278, 279, 5, 58, 0, 0, 279, 280, 5, 99, 0, 0, 280, 0, 0, 276, 277, 5, 104, 0, 0, 277, 278, 5, 116, 0, 0, 278, 279, 5, 95,
281, 5, 111, 0, 0, 281, 282, 5, 117, 0, 0, 282, 283, 5, 110, 0, 0, 283, 0, 0, 279, 280, 5, 106, 0, 0, 280, 281, 5, 111, 0, 0, 281, 282, 5, 105,
284, 5, 116, 0, 0, 284, 285, 5, 95, 0, 0, 285, 286, 5, 117, 0, 0, 286, 0, 0, 282, 347, 5, 110, 0, 0, 283, 284, 5, 114, 0, 0, 284, 285, 5, 106,
287, 5, 110, 0, 0, 287, 288, 5, 105, 0, 0, 288, 289, 5, 113, 0, 0, 289, 0, 0, 285, 286, 5, 111, 0, 0, 286, 287, 5, 105, 0, 0, 287, 347, 5, 110,
290, 5, 117, 0, 0, 290, 329, 5, 101, 0, 0, 291, 292, 5, 58, 0, 0, 292, 0, 0, 288, 289, 5, 114, 0, 0, 289, 290, 5, 105, 0, 0, 290, 291, 5, 103,
293, 5, 97, 0, 0, 293, 294, 5, 118, 0, 0, 294, 329, 5, 103, 0, 0, 295, 0, 0, 291, 292, 5, 104, 0, 0, 292, 293, 5, 116, 0, 0, 293, 294, 5, 95,
296, 5, 58, 0, 0, 296, 297, 5, 103, 0, 0, 297, 298, 5, 114, 0, 0, 298, 0, 0, 294, 295, 5, 111, 0, 0, 295, 296, 5, 117, 0, 0, 296, 297, 5, 116,
299, 5, 111, 0, 0, 299, 300, 5, 117, 0, 0, 300, 301, 5, 112, 0, 0, 301, 0, 0, 297, 298, 5, 101, 0, 0, 298, 299, 5, 114, 0, 0, 299, 300, 5, 95,
302, 5, 95, 0, 0, 302, 303, 5, 98, 0, 0, 303, 329, 5, 121, 0, 0, 304, 305, 0, 0, 300, 301, 5, 106, 0, 0, 301, 302, 5, 111, 0, 0, 302, 303, 5, 105,
5, 58, 0, 0, 305, 306, 5, 109, 0, 0, 306, 307, 5, 97, 0, 0, 307, 329, 5, 0, 0, 303, 347, 5, 110, 0, 0, 304, 305, 5, 114, 0, 0, 305, 306, 5, 111,
120, 0, 0, 308, 309, 5, 58, 0, 0, 309, 310, 5, 109, 0, 0, 310, 311, 5, 0, 0, 306, 307, 5, 106, 0, 0, 307, 308, 5, 111, 0, 0, 308, 309, 5, 105,
105, 0, 0, 311, 329, 5, 110, 0, 0, 312, 313, 5, 58, 0, 0, 313, 314, 5, 0, 0, 309, 347, 5, 110, 0, 0, 310, 311, 5, 102, 0, 0, 311, 312, 5, 117,
111, 0, 0, 314, 315, 5, 114, 0, 0, 315, 316, 5, 100, 0, 0, 316, 317, 5, 0, 0, 312, 313, 5, 108, 0, 0, 313, 314, 5, 108, 0, 0, 314, 315, 5, 95,
101, 0, 0, 317, 318, 5, 114, 0, 0, 318, 319, 5, 95, 0, 0, 319, 320, 5, 0, 0, 315, 316, 5, 111, 0, 0, 316, 317, 5, 117, 0, 0, 317, 318, 5, 116,
98, 0, 0, 320, 329, 5, 121, 0, 0, 321, 322, 5, 58, 0, 0, 322, 323, 5, 117, 0, 0, 318, 319, 5, 101, 0, 0, 319, 320, 5, 114, 0, 0, 320, 321, 5, 95,
0, 0, 323, 324, 5, 110, 0, 0, 324, 325, 5, 105, 0, 0, 325, 326, 5, 113, 0, 0, 321, 322, 5, 106, 0, 0, 322, 323, 5, 111, 0, 0, 323, 324, 5, 105,
0, 0, 326, 327, 5, 117, 0, 0, 327, 329, 5, 101, 0, 0, 328, 272, 1, 0, 0, 0, 0, 324, 347, 5, 110, 0, 0, 325, 326, 5, 102, 0, 0, 326, 327, 5, 111,
0, 328, 278, 1, 0, 0, 0, 328, 291, 1, 0, 0, 0, 328, 295, 1, 0, 0, 0, 328, 0, 0, 327, 328, 5, 106, 0, 0, 328, 329, 5, 111, 0, 0, 329, 330, 5, 105,
304, 1, 0, 0, 0, 328, 308, 1, 0, 0, 0, 328, 312, 1, 0, 0, 0, 328, 321, 0, 0, 330, 347, 5, 110, 0, 0, 331, 332, 5, 99, 0, 0, 332, 333, 5, 114,
1, 0, 0, 0, 329, 52, 1, 0, 0, 0, 330, 331, 5, 36, 0, 0, 331, 332, 3, 57, 0, 0, 333, 334, 5, 111, 0, 0, 334, 335, 5, 115, 0, 0, 335, 336, 5, 115,
28, 0, 332, 54, 1, 0, 0, 0, 333, 334, 5, 110, 0, 0, 334, 335, 5, 117, 0, 0, 0, 336, 337, 5, 95, 0, 0, 337, 338, 5, 106, 0, 0, 338, 339, 5, 111,
0, 335, 336, 5, 108, 0, 0, 336, 337, 5, 108, 0, 0, 337, 56, 1, 0, 0, 0, 0, 0, 339, 340, 5, 105, 0, 0, 340, 347, 5, 110, 0, 0, 341, 342, 5, 120,
338, 342, 7, 0, 0, 0, 339, 341, 7, 1, 0, 0, 340, 339, 1, 0, 0, 0, 341, 0, 0, 342, 343, 5, 106, 0, 0, 343, 344, 5, 111, 0, 0, 344, 345, 5, 105,
344, 1, 0, 0, 0, 342, 340, 1, 0, 0, 0, 342, 343, 1, 0, 0, 0, 343, 58, 1, 0, 0, 345, 347, 5, 110, 0, 0, 346, 224, 1, 0, 0, 0, 346, 228, 1, 0, 0,
0, 0, 0, 344, 342, 1, 0, 0, 0, 345, 347, 7, 2, 0, 0, 346, 345, 1, 0, 0, 0, 346, 238, 1, 0, 0, 0, 346, 247, 1, 0, 0, 0, 346, 252, 1, 0, 0, 0, 346,
0, 347, 348, 1, 0, 0, 0, 348, 346, 1, 0, 0, 0, 348, 349, 1, 0, 0, 0, 349, 267, 1, 0, 0, 0, 346, 273, 1, 0, 0, 0, 346, 283, 1, 0, 0, 0, 346, 288,
350, 1, 0, 0, 0, 350, 351, 6, 29, 0, 0, 351, 60, 1, 0, 0, 0, 352, 353, 1, 0, 0, 0, 346, 304, 1, 0, 0, 0, 346, 310, 1, 0, 0, 0, 346, 325, 1, 0,
5, 40, 0, 0, 353, 62, 1, 0, 0, 0, 354, 355, 5, 41, 0, 0, 355, 64, 1, 0, 0, 0, 346, 331, 1, 0, 0, 0, 346, 341, 1, 0, 0, 0, 347, 40, 1, 0, 0, 0,
0, 0, 356, 357, 5, 91, 0, 0, 357, 66, 1, 0, 0, 0, 358, 359, 5, 93, 0, 0, 348, 349, 5, 119, 0, 0, 349, 350, 5, 104, 0, 0, 350, 351, 5, 101, 0, 0,
359, 68, 1, 0, 0, 0, 360, 361, 5, 44, 0, 0, 361, 70, 1, 0, 0, 0, 362, 363, 351, 352, 5, 114, 0, 0, 352, 360, 5, 101, 0, 0, 353, 354, 5, 115, 0, 0,
5, 124, 0, 0, 363, 72, 1, 0, 0, 0, 364, 365, 5, 58, 0, 0, 365, 74, 1, 0, 354, 355, 5, 101, 0, 0, 355, 356, 5, 108, 0, 0, 356, 357, 5, 101, 0, 0,
0, 0, 366, 367, 3, 79, 39, 0, 367, 76, 1, 0, 0, 0, 368, 393, 3, 75, 37, 357, 358, 5, 99, 0, 0, 358, 360, 5, 116, 0, 0, 359, 348, 1, 0, 0, 0, 359,
0, 369, 371, 5, 45, 0, 0, 370, 369, 1, 0, 0, 0, 370, 371, 1, 0, 0, 0, 371, 353, 1, 0, 0, 0, 360, 42, 1, 0, 0, 0, 361, 362, 5, 103, 0, 0, 362, 363,
372, 1, 0, 0, 0, 372, 373, 3, 79, 39, 0, 373, 375, 5, 46, 0, 0, 374, 376, 5, 114, 0, 0, 363, 364, 5, 111, 0, 0, 364, 365, 5, 117, 0, 0, 365, 366,
7, 3, 0, 0, 375, 374, 1, 0, 0, 0, 376, 377, 1, 0, 0, 0, 377, 375, 1, 0, 5, 112, 0, 0, 366, 367, 5, 95, 0, 0, 367, 368, 5, 98, 0, 0, 368, 369, 5,
0, 0, 377, 378, 1, 0, 0, 0, 378, 380, 1, 0, 0, 0, 379, 381, 3, 81, 40, 121, 0, 0, 369, 44, 1, 0, 0, 0, 370, 371, 5, 43, 0, 0, 371, 46, 1, 0, 0,
0, 380, 379, 1, 0, 0, 0, 380, 381, 1, 0, 0, 0, 381, 393, 1, 0, 0, 0, 382, 0, 372, 373, 5, 45, 0, 0, 373, 48, 1, 0, 0, 0, 374, 375, 5, 111, 0, 0,
384, 5, 45, 0, 0, 383, 382, 1, 0, 0, 0, 383, 384, 1, 0, 0, 0, 384, 385, 375, 376, 5, 114, 0, 0, 376, 377, 5, 100, 0, 0, 377, 378, 5, 101, 0, 0,
1, 0, 0, 0, 385, 386, 3, 79, 39, 0, 386, 387, 3, 81, 40, 0, 387, 393, 1, 378, 379, 5, 114, 0, 0, 379, 380, 5, 95, 0, 0, 380, 381, 5, 98, 0, 0, 381,
0, 0, 0, 388, 390, 5, 45, 0, 0, 389, 388, 1, 0, 0, 0, 389, 390, 1, 0, 0, 390, 5, 121, 0, 0, 382, 383, 5, 115, 0, 0, 383, 384, 5, 111, 0, 0, 384,
0, 390, 391, 1, 0, 0, 0, 391, 393, 3, 79, 39, 0, 392, 368, 1, 0, 0, 0, 385, 5, 114, 0, 0, 385, 386, 5, 116, 0, 0, 386, 387, 5, 95, 0, 0, 387,
392, 370, 1, 0, 0, 0, 392, 383, 1, 0, 0, 0, 392, 389, 1, 0, 0, 0, 393, 388, 5, 98, 0, 0, 388, 390, 5, 121, 0, 0, 389, 374, 1, 0, 0, 0, 389, 382,
78, 1, 0, 0, 0, 394, 403, 5, 48, 0, 0, 395, 399, 7, 4, 0, 0, 396, 398, 1, 0, 0, 0, 390, 50, 1, 0, 0, 0, 391, 392, 5, 58, 0, 0, 392, 393, 5, 99,
7, 3, 0, 0, 397, 396, 1, 0, 0, 0, 398, 401, 1, 0, 0, 0, 399, 397, 1, 0, 0, 0, 393, 394, 5, 111, 0, 0, 394, 395, 5, 117, 0, 0, 395, 396, 5, 110,
0, 0, 399, 400, 1, 0, 0, 0, 400, 403, 1, 0, 0, 0, 401, 399, 1, 0, 0, 0, 0, 0, 396, 448, 5, 116, 0, 0, 397, 398, 5, 58, 0, 0, 398, 399, 5, 99, 0,
402, 394, 1, 0, 0, 0, 402, 395, 1, 0, 0, 0, 403, 80, 1, 0, 0, 0, 404, 406, 0, 399, 400, 5, 111, 0, 0, 400, 401, 5, 117, 0, 0, 401, 402, 5, 110, 0,
7, 5, 0, 0, 405, 407, 7, 6, 0, 0, 406, 405, 1, 0, 0, 0, 406, 407, 1, 0, 0, 402, 403, 5, 116, 0, 0, 403, 404, 5, 95, 0, 0, 404, 405, 5, 117, 0,
0, 0, 407, 408, 1, 0, 0, 0, 408, 409, 3, 79, 39, 0, 409, 82, 1, 0, 0, 0, 0, 405, 406, 5, 110, 0, 0, 406, 407, 5, 105, 0, 0, 407, 408, 5, 113, 0,
410, 411, 5, 60, 0, 0, 411, 412, 5, 61, 0, 0, 412, 84, 1, 0, 0, 0, 413, 0, 408, 409, 5, 117, 0, 0, 409, 448, 5, 101, 0, 0, 410, 411, 5, 58, 0,
414, 5, 60, 0, 0, 414, 86, 1, 0, 0, 0, 415, 416, 5, 62, 0, 0, 416, 417, 0, 411, 412, 5, 97, 0, 0, 412, 413, 5, 118, 0, 0, 413, 448, 5, 103, 0,
5, 61, 0, 0, 417, 88, 1, 0, 0, 0, 418, 419, 5, 62, 0, 0, 419, 90, 1, 0, 0, 414, 415, 5, 58, 0, 0, 415, 416, 5, 103, 0, 0, 416, 417, 5, 114, 0,
0, 0, 420, 421, 5, 33, 0, 0, 421, 422, 5, 61, 0, 0, 422, 92, 1, 0, 0, 0, 0, 417, 418, 5, 111, 0, 0, 418, 419, 5, 117, 0, 0, 419, 420, 5, 112, 0,
423, 424, 5, 61, 0, 0, 424, 425, 5, 61, 0, 0, 425, 94, 1, 0, 0, 0, 426, 0, 420, 421, 5, 95, 0, 0, 421, 422, 5, 98, 0, 0, 422, 448, 5, 121, 0, 0,
430, 5, 46, 0, 0, 427, 431, 3, 53, 26, 0, 428, 431, 3, 57, 28, 0, 429, 423, 424, 5, 58, 0, 0, 424, 425, 5, 109, 0, 0, 425, 426, 5, 97, 0, 0, 426,
431, 3, 99, 49, 0, 430, 427, 1, 0, 0, 0, 430, 428, 1, 0, 0, 0, 430, 429, 448, 5, 120, 0, 0, 427, 428, 5, 58, 0, 0, 428, 429, 5, 109, 0, 0, 429,
1, 0, 0, 0, 431, 96, 1, 0, 0, 0, 432, 433, 5, 64, 0, 0, 433, 438, 3, 57, 430, 5, 105, 0, 0, 430, 448, 5, 110, 0, 0, 431, 432, 5, 58, 0, 0, 432,
28, 0, 434, 435, 5, 47, 0, 0, 435, 437, 3, 57, 28, 0, 436, 434, 1, 0, 0, 433, 5, 111, 0, 0, 433, 434, 5, 114, 0, 0, 434, 435, 5, 100, 0, 0, 435,
0, 437, 440, 1, 0, 0, 0, 438, 436, 1, 0, 0, 0, 438, 439, 1, 0, 0, 0, 439, 436, 5, 101, 0, 0, 436, 437, 5, 114, 0, 0, 437, 438, 5, 95, 0, 0, 438,
98, 1, 0, 0, 0, 440, 438, 1, 0, 0, 0, 441, 446, 5, 34, 0, 0, 442, 445, 439, 5, 98, 0, 0, 439, 448, 5, 121, 0, 0, 440, 441, 5, 58, 0, 0, 441, 442,
3, 101, 50, 0, 443, 445, 8, 7, 0, 0, 444, 442, 1, 0, 0, 0, 444, 443, 1, 5, 117, 0, 0, 442, 443, 5, 110, 0, 0, 443, 444, 5, 105, 0, 0, 444, 445,
0, 0, 0, 445, 448, 1, 0, 0, 0, 446, 444, 1, 0, 0, 0, 446, 447, 1, 0, 0, 5, 113, 0, 0, 445, 446, 5, 117, 0, 0, 446, 448, 5, 101, 0, 0, 447, 391,
0, 447, 449, 1, 0, 0, 0, 448, 446, 1, 0, 0, 0, 449, 450, 5, 34, 0, 0, 450, 1, 0, 0, 0, 447, 397, 1, 0, 0, 0, 447, 410, 1, 0, 0, 0, 447, 414, 1, 0,
100, 1, 0, 0, 0, 451, 454, 5, 92, 0, 0, 452, 455, 7, 8, 0, 0, 453, 455, 0, 0, 447, 423, 1, 0, 0, 0, 447, 427, 1, 0, 0, 0, 447, 431, 1, 0, 0, 0,
3, 103, 51, 0, 454, 452, 1, 0, 0, 0, 454, 453, 1, 0, 0, 0, 455, 102, 1, 447, 440, 1, 0, 0, 0, 448, 52, 1, 0, 0, 0, 449, 450, 5, 36, 0, 0, 450,
0, 0, 0, 456, 457, 5, 117, 0, 0, 457, 458, 3, 105, 52, 0, 458, 459, 3, 451, 3, 57, 28, 0, 451, 54, 1, 0, 0, 0, 452, 453, 5, 110, 0, 0, 453, 454,
105, 52, 0, 459, 460, 3, 105, 52, 0, 460, 461, 3, 105, 52, 0, 461, 104, 5, 117, 0, 0, 454, 455, 5, 108, 0, 0, 455, 456, 5, 108, 0, 0, 456, 56,
1, 0, 0, 0, 462, 463, 7, 9, 0, 0, 463, 106, 1, 0, 0, 0, 464, 465, 7, 3, 1, 0, 0, 0, 457, 461, 7, 0, 0, 0, 458, 460, 7, 1, 0, 0, 459, 458, 1, 0,
0, 0, 465, 108, 1, 0, 0, 0, 466, 467, 7, 10, 0, 0, 467, 110, 1, 0, 0, 0, 0, 0, 460, 463, 1, 0, 0, 0, 461, 459, 1, 0, 0, 0, 461, 462, 1, 0, 0, 0,
468, 469, 7, 11, 0, 0, 469, 112, 1, 0, 0, 0, 470, 471, 7, 12, 0, 0, 471, 462, 58, 1, 0, 0, 0, 463, 461, 1, 0, 0, 0, 464, 466, 7, 2, 0, 0, 465, 464,
114, 1, 0, 0, 0, 472, 473, 7, 13, 0, 0, 473, 116, 1, 0, 0, 0, 474, 475, 1, 0, 0, 0, 466, 467, 1, 0, 0, 0, 467, 465, 1, 0, 0, 0, 467, 468, 1, 0,
7, 5, 0, 0, 475, 118, 1, 0, 0, 0, 476, 477, 7, 14, 0, 0, 477, 120, 1, 0, 0, 0, 468, 469, 1, 0, 0, 0, 469, 470, 6, 29, 0, 0, 470, 60, 1, 0, 0, 0,
0, 0, 478, 479, 7, 15, 0, 0, 479, 122, 1, 0, 0, 0, 480, 481, 7, 16, 0, 471, 472, 5, 40, 0, 0, 472, 62, 1, 0, 0, 0, 473, 474, 5, 41, 0, 0, 474,
0, 481, 124, 1, 0, 0, 0, 482, 483, 7, 17, 0, 0, 483, 126, 1, 0, 0, 0, 484, 64, 1, 0, 0, 0, 475, 476, 5, 91, 0, 0, 476, 66, 1, 0, 0, 0, 477, 478, 5,
485, 7, 18, 0, 0, 485, 128, 1, 0, 0, 0, 486, 487, 7, 19, 0, 0, 487, 130, 93, 0, 0, 478, 68, 1, 0, 0, 0, 479, 480, 5, 44, 0, 0, 480, 70, 1, 0, 0,
1, 0, 0, 0, 488, 489, 7, 20, 0, 0, 489, 132, 1, 0, 0, 0, 490, 491, 7, 21, 0, 481, 482, 5, 124, 0, 0, 482, 72, 1, 0, 0, 0, 483, 484, 5, 58, 0, 0,
0, 0, 491, 134, 1, 0, 0, 0, 492, 493, 7, 22, 0, 0, 493, 136, 1, 0, 0, 0, 484, 74, 1, 0, 0, 0, 485, 486, 3, 79, 39, 0, 486, 76, 1, 0, 0, 0, 487,
494, 495, 7, 23, 0, 0, 495, 138, 1, 0, 0, 0, 496, 497, 7, 24, 0, 0, 497, 512, 3, 75, 37, 0, 488, 490, 5, 45, 0, 0, 489, 488, 1, 0, 0, 0, 489, 490,
140, 1, 0, 0, 0, 498, 499, 7, 25, 0, 0, 499, 142, 1, 0, 0, 0, 500, 501, 1, 0, 0, 0, 490, 491, 1, 0, 0, 0, 491, 492, 3, 79, 39, 0, 492, 494, 5,
7, 26, 0, 0, 501, 144, 1, 0, 0, 0, 502, 503, 7, 27, 0, 0, 503, 146, 1, 46, 0, 0, 493, 495, 7, 3, 0, 0, 494, 493, 1, 0, 0, 0, 495, 496, 1, 0, 0,
0, 0, 0, 504, 505, 7, 28, 0, 0, 505, 148, 1, 0, 0, 0, 506, 507, 7, 29, 0, 496, 494, 1, 0, 0, 0, 496, 497, 1, 0, 0, 0, 497, 499, 1, 0, 0, 0, 498,
0, 0, 507, 150, 1, 0, 0, 0, 508, 509, 7, 30, 0, 0, 509, 152, 1, 0, 0, 0, 500, 3, 81, 40, 0, 499, 498, 1, 0, 0, 0, 499, 500, 1, 0, 0, 0, 500, 512,
510, 511, 7, 31, 0, 0, 511, 154, 1, 0, 0, 0, 512, 513, 7, 32, 0, 0, 513, 1, 0, 0, 0, 501, 503, 5, 45, 0, 0, 502, 501, 1, 0, 0, 0, 502, 503, 1, 0,
156, 1, 0, 0, 0, 514, 515, 7, 33, 0, 0, 515, 158, 1, 0, 0, 0, 516, 517, 0, 0, 503, 504, 1, 0, 0, 0, 504, 505, 3, 79, 39, 0, 505, 506, 3, 81, 40,
7, 34, 0, 0, 517, 160, 1, 0, 0, 0, 518, 522, 5, 35, 0, 0, 519, 521, 9, 0, 506, 512, 1, 0, 0, 0, 507, 509, 5, 45, 0, 0, 508, 507, 1, 0, 0, 0, 508,
0, 0, 0, 520, 519, 1, 0, 0, 0, 521, 524, 1, 0, 0, 0, 522, 523, 1, 0, 0, 509, 1, 0, 0, 0, 509, 510, 1, 0, 0, 0, 510, 512, 3, 79, 39, 0, 511, 487,
0, 522, 520, 1, 0, 0, 0, 523, 525, 1, 0, 0, 0, 524, 522, 1, 0, 0, 0, 525, 1, 0, 0, 0, 511, 489, 1, 0, 0, 0, 511, 502, 1, 0, 0, 0, 511, 508, 1, 0,
526, 5, 10, 0, 0, 526, 527, 1, 0, 0, 0, 527, 528, 6, 80, 0, 0, 528, 162, 0, 0, 512, 78, 1, 0, 0, 0, 513, 522, 5, 48, 0, 0, 514, 518, 7, 4, 0, 0,
1, 0, 0, 0, 21, 0, 240, 270, 328, 342, 348, 370, 377, 380, 383, 389, 392, 515, 517, 7, 3, 0, 0, 516, 515, 1, 0, 0, 0, 517, 520, 1, 0, 0, 0, 518,
399, 402, 406, 430, 438, 444, 446, 454, 522, 1, 6, 0, 0, 516, 1, 0, 0, 0, 518, 519, 1, 0, 0, 0, 519, 522, 1, 0, 0, 0, 520, 518,
1, 0, 0, 0, 521, 513, 1, 0, 0, 0, 521, 514, 1, 0, 0, 0, 522, 80, 1, 0,
0, 0, 523, 525, 7, 5, 0, 0, 524, 526, 7, 6, 0, 0, 525, 524, 1, 0, 0, 0,
525, 526, 1, 0, 0, 0, 526, 527, 1, 0, 0, 0, 527, 528, 3, 79, 39, 0, 528,
82, 1, 0, 0, 0, 529, 530, 5, 60, 0, 0, 530, 531, 5, 61, 0, 0, 531, 84,
1, 0, 0, 0, 532, 533, 5, 60, 0, 0, 533, 86, 1, 0, 0, 0, 534, 535, 5, 62,
0, 0, 535, 536, 5, 61, 0, 0, 536, 88, 1, 0, 0, 0, 537, 538, 5, 62, 0, 0,
538, 90, 1, 0, 0, 0, 539, 540, 5, 33, 0, 0, 540, 541, 5, 61, 0, 0, 541,
92, 1, 0, 0, 0, 542, 543, 5, 61, 0, 0, 543, 544, 5, 61, 0, 0, 544, 94,
1, 0, 0, 0, 545, 549, 5, 46, 0, 0, 546, 550, 3, 53, 26, 0, 547, 550, 3,
57, 28, 0, 548, 550, 3, 99, 49, 0, 549, 546, 1, 0, 0, 0, 549, 547, 1, 0,
0, 0, 549, 548, 1, 0, 0, 0, 550, 96, 1, 0, 0, 0, 551, 552, 5, 64, 0, 0,
552, 557, 3, 57, 28, 0, 553, 554, 5, 47, 0, 0, 554, 556, 3, 57, 28, 0,
555, 553, 1, 0, 0, 0, 556, 559, 1, 0, 0, 0, 557, 555, 1, 0, 0, 0, 557,
558, 1, 0, 0, 0, 558, 98, 1, 0, 0, 0, 559, 557, 1, 0, 0, 0, 560, 565, 5,
34, 0, 0, 561, 564, 3, 101, 50, 0, 562, 564, 8, 7, 0, 0, 563, 561, 1, 0,
0, 0, 563, 562, 1, 0, 0, 0, 564, 567, 1, 0, 0, 0, 565, 563, 1, 0, 0, 0,
565, 566, 1, 0, 0, 0, 566, 568, 1, 0, 0, 0, 567, 565, 1, 0, 0, 0, 568,
569, 5, 34, 0, 0, 569, 100, 1, 0, 0, 0, 570, 573, 5, 92, 0, 0, 571, 574,
7, 8, 0, 0, 572, 574, 3, 103, 51, 0, 573, 571, 1, 0, 0, 0, 573, 572, 1,
0, 0, 0, 574, 102, 1, 0, 0, 0, 575, 576, 5, 117, 0, 0, 576, 577, 3, 105,
52, 0, 577, 578, 3, 105, 52, 0, 578, 579, 3, 105, 52, 0, 579, 580, 3, 105,
52, 0, 580, 104, 1, 0, 0, 0, 581, 582, 7, 9, 0, 0, 582, 106, 1, 0, 0, 0,
583, 584, 7, 3, 0, 0, 584, 108, 1, 0, 0, 0, 585, 586, 7, 10, 0, 0, 586,
110, 1, 0, 0, 0, 587, 588, 7, 11, 0, 0, 588, 112, 1, 0, 0, 0, 589, 590,
7, 12, 0, 0, 590, 114, 1, 0, 0, 0, 591, 592, 7, 13, 0, 0, 592, 116, 1,
0, 0, 0, 593, 594, 7, 5, 0, 0, 594, 118, 1, 0, 0, 0, 595, 596, 7, 14, 0,
0, 596, 120, 1, 0, 0, 0, 597, 598, 7, 15, 0, 0, 598, 122, 1, 0, 0, 0, 599,
600, 7, 16, 0, 0, 600, 124, 1, 0, 0, 0, 601, 602, 7, 17, 0, 0, 602, 126,
1, 0, 0, 0, 603, 604, 7, 18, 0, 0, 604, 128, 1, 0, 0, 0, 605, 606, 7, 19,
0, 0, 606, 130, 1, 0, 0, 0, 607, 608, 7, 20, 0, 0, 608, 132, 1, 0, 0, 0,
609, 610, 7, 21, 0, 0, 610, 134, 1, 0, 0, 0, 611, 612, 7, 22, 0, 0, 612,
136, 1, 0, 0, 0, 613, 614, 7, 23, 0, 0, 614, 138, 1, 0, 0, 0, 615, 616,
7, 24, 0, 0, 616, 140, 1, 0, 0, 0, 617, 618, 7, 25, 0, 0, 618, 142, 1,
0, 0, 0, 619, 620, 7, 26, 0, 0, 620, 144, 1, 0, 0, 0, 621, 622, 7, 27,
0, 0, 622, 146, 1, 0, 0, 0, 623, 624, 7, 28, 0, 0, 624, 148, 1, 0, 0, 0,
625, 626, 7, 29, 0, 0, 626, 150, 1, 0, 0, 0, 627, 628, 7, 30, 0, 0, 628,
152, 1, 0, 0, 0, 629, 630, 7, 31, 0, 0, 630, 154, 1, 0, 0, 0, 631, 632,
7, 32, 0, 0, 632, 156, 1, 0, 0, 0, 633, 634, 7, 33, 0, 0, 634, 158, 1,
0, 0, 0, 635, 636, 7, 34, 0, 0, 636, 160, 1, 0, 0, 0, 637, 641, 5, 35,
0, 0, 638, 640, 9, 0, 0, 0, 639, 638, 1, 0, 0, 0, 640, 643, 1, 0, 0, 0,
641, 642, 1, 0, 0, 0, 641, 639, 1, 0, 0, 0, 642, 644, 1, 0, 0, 0, 643,
641, 1, 0, 0, 0, 644, 645, 5, 10, 0, 0, 645, 646, 1, 0, 0, 0, 646, 647,
6, 80, 0, 0, 647, 162, 1, 0, 0, 0, 22, 0, 346, 359, 389, 447, 461, 467,
489, 496, 499, 502, 508, 511, 518, 521, 525, 549, 557, 563, 565, 573, 641,
1, 6, 0, 0,
} }
deserializer := antlr.NewATNDeserializer(nil) deserializer := antlr.NewATNDeserializer(nil)
staticData.atn = deserializer.Deserialize(staticData.serializedATN) staticData.atn = deserializer.Deserialize(staticData.serializedATN)
@ -365,8 +421,8 @@ const (
SLQLexerT__15 = 16 SLQLexerT__15 = 16
SLQLexerT__16 = 17 SLQLexerT__16 = 17
SLQLexerT__17 = 18 SLQLexerT__17 = 18
SLQLexerT__18 = 19 SLQLexerPROPRIETARY_FUNC_NAME = 19
SLQLexerPROPRIETARY_FUNC_NAME = 20 SLQLexerJOIN_TYPE = 20
SLQLexerWHERE = 21 SLQLexerWHERE = 21
SLQLexerGROUP_BY = 22 SLQLexerGROUP_BY = 22
SLQLexerORDER_ASC = 23 SLQLexerORDER_ASC = 23

View File

@ -19,9 +19,6 @@ type SLQListener interface {
// EnterElement is called when entering the element production. // EnterElement is called when entering the element production.
EnterElement(c *ElementContext) EnterElement(c *ElementContext)
// EnterCmpr is called when entering the cmpr production.
EnterCmpr(c *CmprContext)
// EnterFuncElement is called when entering the funcElement production. // EnterFuncElement is called when entering the funcElement production.
EnterFuncElement(c *FuncElementContext) EnterFuncElement(c *FuncElementContext)
@ -34,8 +31,8 @@ type SLQListener interface {
// EnterJoin is called when entering the join production. // EnterJoin is called when entering the join production.
EnterJoin(c *JoinContext) EnterJoin(c *JoinContext)
// EnterJoinConstraint is called when entering the joinConstraint production. // EnterJoinTable is called when entering the joinTable production.
EnterJoinConstraint(c *JoinConstraintContext) EnterJoinTable(c *JoinTableContext)
// EnterUniqueFunc is called when entering the uniqueFunc production. // EnterUniqueFunc is called when entering the uniqueFunc production.
EnterUniqueFunc(c *UniqueFuncContext) EnterUniqueFunc(c *UniqueFuncContext)
@ -103,9 +100,6 @@ type SLQListener interface {
// ExitElement is called when exiting the element production. // ExitElement is called when exiting the element production.
ExitElement(c *ElementContext) ExitElement(c *ElementContext)
// ExitCmpr is called when exiting the cmpr production.
ExitCmpr(c *CmprContext)
// ExitFuncElement is called when exiting the funcElement production. // ExitFuncElement is called when exiting the funcElement production.
ExitFuncElement(c *FuncElementContext) ExitFuncElement(c *FuncElementContext)
@ -118,8 +112,8 @@ type SLQListener interface {
// ExitJoin is called when exiting the join production. // ExitJoin is called when exiting the join production.
ExitJoin(c *JoinContext) ExitJoin(c *JoinContext)
// ExitJoinConstraint is called when exiting the joinConstraint production. // ExitJoinTable is called when exiting the joinTable production.
ExitJoinConstraint(c *JoinConstraintContext) ExitJoinTable(c *JoinTableContext)
// ExitUniqueFunc is called when exiting the uniqueFunc production. // ExitUniqueFunc is called when exiting the uniqueFunc production.
ExitUniqueFunc(c *UniqueFuncContext) ExitUniqueFunc(c *UniqueFuncContext)

File diff suppressed because it is too large Load Diff

View File

@ -19,9 +19,6 @@ type SLQVisitor interface {
// Visit a parse tree produced by SLQParser#element. // Visit a parse tree produced by SLQParser#element.
VisitElement(ctx *ElementContext) interface{} VisitElement(ctx *ElementContext) interface{}
// Visit a parse tree produced by SLQParser#cmpr.
VisitCmpr(ctx *CmprContext) interface{}
// Visit a parse tree produced by SLQParser#funcElement. // Visit a parse tree produced by SLQParser#funcElement.
VisitFuncElement(ctx *FuncElementContext) interface{} VisitFuncElement(ctx *FuncElementContext) interface{}
@ -34,8 +31,8 @@ type SLQVisitor interface {
// Visit a parse tree produced by SLQParser#join. // Visit a parse tree produced by SLQParser#join.
VisitJoin(ctx *JoinContext) interface{} VisitJoin(ctx *JoinContext) interface{}
// Visit a parse tree produced by SLQParser#joinConstraint. // Visit a parse tree produced by SLQParser#joinTable.
VisitJoinConstraint(ctx *JoinConstraintContext) interface{} VisitJoinTable(ctx *JoinTableContext) interface{}
// Visit a parse tree produced by SLQParser#uniqueFunc. // Visit a parse tree produced by SLQParser#uniqueFunc.
VisitUniqueFunc(ctx *UniqueFuncContext) interface{} VisitUniqueFunc(ctx *UniqueFuncContext) interface{}

View File

@ -3,7 +3,10 @@ package ast
import ( import (
"fmt" "fmt"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/neilotoole/sq/libsq/ast/internal/slq" "github.com/neilotoole/sq/libsq/ast/internal/slq"
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/antlr/antlr4/runtime/Go/antlr/v4" "github.com/antlr/antlr4/runtime/Go/antlr/v4"
) )
@ -16,293 +19,242 @@ func (v *parseTreeVisitor) VisitJoin(ctx *slq.JoinContext) any {
return errorf("parent of JOIN() must be SegmentNode, but got: %T", v.cur) return errorf("parent of JOIN() must be SegmentNode, but got: %T", v.cur)
} }
join := &JoinNode{seg: seg, ctx: ctx} var err error
err := seg.AddChild(join) node := &JoinNode{
if err != nil { seg: seg,
ctx: ctx,
text: ctx.GetText(),
}
if node.jt, node.jtVal, err = getJoinType(ctx); err != nil {
return err return err
} }
expr := ctx.JoinConstraint() if ctx.JoinTable() == nil {
if expr == nil { return errz.Errorf("invalid join: %s: table is nil: %s", node.jtVal, node.text)
return nil
} }
// the join contains a constraint, let's hit it var jtCtx *slq.JoinTableContext
v.cur = join if jtCtx, ok = ctx.JoinTable().(*slq.JoinTableContext); !ok {
err2 := v.VisitJoinConstraint(expr.(*slq.JoinConstraintContext)) return errz.Errorf("invalid join: %s: invalid table type: expected %T but got %T: %s",
if err2 != nil { node.jtVal, jtCtx, ctx.JoinTable(), node.text)
return err2
} }
// set cur back to previous
v.cur = seg if e := v.using(node, func() any {
return nil return v.VisitJoinTable(jtCtx)
}); e != nil {
return e
}
if ctx.Expr() == nil {
if node.jt.HasPredicate() {
return errorf("invalid join: %s: predicate required: %s",
node.jtVal, node.text)
}
}
if ctx.Expr() != nil {
// Expression can be nil for cross join.
var exprCtx *slq.ExprContext
if exprCtx, ok = ctx.Expr().(*slq.ExprContext); !ok {
return errorf("invalid join: %s: expression type: expected %T but got %T: %s",
node.jtVal, exprCtx, ctx.Expr(), node.text)
}
if e := v.using(node, func() any {
return v.VisitExpr(exprCtx)
}); e != nil {
return e
}
}
return seg.AddChild(node)
} }
// VisitJoinConstraint implements slq.SLQVisitor. // VisitJoinTable implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitJoinConstraint(ctx *slq.JoinConstraintContext) any { func (v *parseTreeVisitor) VisitJoinTable(ctx *slq.JoinTableContext) any {
joinNode, ok := v.cur.(*JoinNode) joinNode, ok := v.cur.(*JoinNode)
if !ok { if !ok {
return errorf("JOIN constraint must have JOIN parent, but got %T", v.cur) return errorf("JOIN constraint must have JOIN parent, but got %T", v.cur)
} }
// the constraint could be empty var handle string
children := ctx.GetChildren() var tblName string
if len(children) == 0 {
return nil if ctx.HANDLE() != nil {
// It's ok to have a nil/empty handle
handle = ctx.HANDLE().GetText()
} }
// the constraint could be a single SEL (in which case, there's no comparison operator) if ctx.NAME() == nil {
if ctx.Cmpr() == nil { return errorf("invalid %T: table name is nil", ctx)
// there should be exactly one SEL
sels := ctx.AllSelector()
if len(sels) != 1 {
return errorf("JOIN constraint without a comparison operator must have exactly one selector")
}
joinExprNode := &JoinConstraint{join: joinNode, ctx: ctx}
colSelNode, err := newSelectorNode(joinExprNode, sels[0])
if err != nil {
return err
}
if err := joinExprNode.AddChild(colSelNode); err != nil {
return err
}
return joinNode.AddChild(joinExprNode)
} }
// We've got a comparison operator tblName, err := extractSelVal(ctx.NAME())
sels := ctx.AllSelector()
if len(sels) != 2 {
// REVISIT: probably unnecessary, should be caught by the parser
return errorf("JOIN constraint must have 2 operands (left & right), but got %d", len(sels))
}
join, ok := v.cur.(*JoinNode)
if !ok {
return errorf("JoinConstraint must have JoinNode parent, but got %T", v.cur)
}
joinCondition := &JoinConstraint{join: join, ctx: ctx}
leftSel, err := newSelectorNode(joinCondition, sels[0])
if err != nil { if err != nil {
return err return err
} }
if err = joinCondition.AddChild(leftSel); err != nil { tblSelNode := &TblSelectorNode{
return err SelectorNode: SelectorNode{
baseNode: baseNode{
parent: joinNode,
ctx: ctx.NAME(),
text: ctx.NAME().GetText(),
},
name0: tblName,
},
handle: handle,
tblName: tblName,
} }
cmpr := newCmprNode(joinCondition, ctx.Cmpr()) var aliasCtx *slq.AliasContext
if err = joinCondition.AddChild(cmpr); err != nil { if ctx.Alias() != nil {
return err if aliasCtx, ok = ctx.Alias().(*slq.AliasContext); !ok {
return errorf("invalid %T: expected %T but got %T", ctx, aliasCtx, ctx.Alias())
}
} }
rightSel, err := newSelectorNode(joinCondition, sels[1]) if e := v.using(tblSelNode, func() any {
if err != nil { return v.VisitAlias(aliasCtx)
return err }); e != nil {
return e
} }
if err = joinCondition.AddChild(rightSel); err != nil { joinNode.targetTbl = tblSelNode
return err return nil
}
return join.AddChild(joinCondition)
} }
var _ Node = (*JoinNode)(nil) var _ Node = (*JoinNode)(nil)
// JoinNode models a SQL JOIN node. It has a child of type JoinConstraint. // JoinNode models a SQL JOIN node.
type JoinNode struct { type JoinNode struct {
seg *SegmentNode seg *SegmentNode
ctx antlr.ParseTree ctx antlr.ParseTree
constraint *JoinConstraint text string
leftTbl *TblSelectorNode jt jointype.Type
rightTbl *TblSelectorNode jtVal string
predicateExpr *ExprNode
targetTbl *TblSelectorNode
} }
// LeftTbl is the selector for the left table of the join. // Predicate returns the join predicate, which
func (jn *JoinNode) LeftTbl() *TblSelectorNode { // may be nil.
return jn.leftTbl func (n *JoinNode) Predicate() *ExprNode {
return n.predicateExpr
} }
// RightTbl is the selector for the right table of the join. // JoinType returns the join type.
func (jn *JoinNode) RightTbl() *TblSelectorNode { func (n *JoinNode) JoinType() jointype.Type {
return jn.rightTbl return n.jt
} }
// Tabler implements the Tabler marker interface. // Table is the selector for join's target table.
func (jn *JoinNode) tabler() { func (n *JoinNode) Table() *TblSelectorNode {
// no-op return n.targetTbl
} }
func (jn *JoinNode) Parent() Node { // Parent implements ast.Node.
return jn.seg func (n *JoinNode) Parent() Node {
return n.seg
} }
func (jn *JoinNode) SetParent(parent Node) error { // SetParent implements ast.Node.
func (n *JoinNode) SetParent(parent Node) error {
seg, ok := parent.(*SegmentNode) seg, ok := parent.(*SegmentNode)
if !ok { if !ok {
return errorf("%T requires parent of type %s", jn, typeSegmentNode) return errorf("%T requires parent of type %s", n, typeSegmentNode)
} }
jn.seg = seg n.seg = seg
return nil return nil
} }
func (jn *JoinNode) Children() []Node { // Children implements ast.Node.
if jn.constraint == nil { func (n *JoinNode) Children() []Node {
if n.predicateExpr == nil {
return []Node{} return []Node{}
} }
return []Node{jn.constraint} return []Node{n.predicateExpr}
} }
func (jn *JoinNode) AddChild(node Node) error { // AddChild implements ast.Node.
jc, ok := node.(*JoinConstraint) func (n *JoinNode) AddChild(node Node) error {
expr, ok := node.(*ExprNode)
if !ok { if !ok {
return errorf("JOIN() child must be *JoinConstraint, but got: %T", node) return errorf("join child must be %T, but got: %T", expr, node)
} }
if jn.constraint != nil { if n.predicateExpr != nil {
return errorf("JOIN() has max 1 child: failed to add: %T", node) return errorf("JOIN() has max 1 child: failed to add: %T", node)
} }
jn.constraint = jc n.predicateExpr = expr
return nil return nil
} }
func (jn *JoinNode) SetChildren(children []Node) error { // SetChildren implements ast.Node.
if len(children) == 0 { func (n *JoinNode) SetChildren(children []Node) error {
jn.constraint = nil switch len(children) {
case 0:
n.predicateExpr = nil
return nil return nil
} case 1:
n.predicateExpr = nil
if len(children) > 1 { return n.AddChild(children[0])
return errorf("JOIN() can have only one child: failed to add %d children", len(children))
}
expr, ok := children[0].(*JoinConstraint)
if !ok {
return errorf("JOIN() child must be *FnJoinExpr, but got: %T", children[0])
}
jn.constraint = expr
return nil
}
func (jn *JoinNode) Context() antlr.ParseTree {
return jn.ctx
}
func (jn *JoinNode) SetContext(ctx antlr.ParseTree) error {
jn.ctx = ctx
return nil
}
func (jn *JoinNode) Text() string {
return jn.ctx.GetText()
}
func (jn *JoinNode) Segment() *SegmentNode {
return jn.seg
}
func (jn *JoinNode) String() string {
text := nodeString(jn)
leftTblName := ""
rightTblName := ""
if jn.leftTbl != nil {
leftTblName, _ = jn.leftTbl.SelValue()
}
if jn.rightTbl != nil {
rightTblName, _ = jn.rightTbl.SelValue()
}
text += fmt.Sprintf(" | left_table: {%s} | right_table: {%s}", leftTblName, rightTblName)
return text
}
var _ Node = (*JoinConstraint)(nil)
// JoinConstraint models a join's constraint.
// For example the elements inside the parentheses
// in "join(.uid == .user_id)".
type JoinConstraint struct {
// join is the parent node
join *JoinNode
ctx antlr.ParseTree
children []Node
}
func (n *JoinConstraint) Parent() Node {
return n.join
}
func (n *JoinConstraint) SetParent(parent Node) error {
join, ok := parent.(*JoinNode)
if !ok {
return errorf("%T requires parent of type %s", n, typeJoinNode)
}
n.join = join
return nil
}
func (n *JoinConstraint) Children() []Node {
return n.children
}
func (n *JoinConstraint) AddChild(child Node) error {
nodeCtx := child.Context()
switch nodeCtx.(type) {
case *antlr.TerminalNodeImpl:
case *slq.SelectorContext:
default: default:
return errorf("cannot add child node %T to %T", nodeCtx, n.ctx) return errorf("join: max of one child allowed; failed to add %d children", len(children))
} }
n.children = append(n.children, child)
return nil
} }
func (n *JoinConstraint) SetChildren(children []Node) error { // context implements ast.Node.
for _, child := range children { func (n *JoinNode) context() antlr.ParseTree {
nodeCtx := child.Context()
switch nodeCtx.(type) {
case *antlr.TerminalNodeImpl:
case *slq.SelectorContext:
default:
return errorf("cannot add child node %T to %T", nodeCtx, n.ctx)
}
}
if len(children) == 0 {
n.children = children
return nil
}
n.children = children
return nil
}
func (n *JoinConstraint) Context() antlr.ParseTree {
return n.ctx return n.ctx
} }
func (n *JoinConstraint) SetContext(ctx antlr.ParseTree) error { // setContext implements ast.Node.
n.ctx = ctx // TODO: check for correct type func (n *JoinNode) setContext(ctx antlr.ParseTree) error {
n.ctx = ctx
return nil return nil
} }
func (n *JoinConstraint) Text() string { // Text implements ast.Node.
func (n *JoinNode) Text() string {
return n.ctx.GetText() return n.ctx.GetText()
} }
func (n *JoinConstraint) String() string { func (n *JoinNode) Segment() *SegmentNode {
return nodeString(n) return n.seg
}
// String implements ast.Node.
func (n *JoinNode) String() string {
text := nodeString(n)
rightTblName := ""
if n.targetTbl != nil {
rightTblName, _ = n.targetTbl.SelValue()
}
text += fmt.Sprintf("|target:%s", rightTblName)
return text
}
// getJoinType returns the canonical join type, as well as the
// input value (which could be the canonical type, or the type's alias).
func getJoinType(ctx *slq.JoinContext) (typ jointype.Type, val string, err error) {
if ctx == nil {
return "", val, errorf("%T is nil", ctx)
}
terminal := ctx.JOIN_TYPE()
if terminal == nil {
// Shouldn't happen
return "", val, errz.Errorf("JOIN_TYPE (%T) is nil", terminal)
}
val = terminal.GetText()
err = typ.UnmarshalText([]byte(val))
return typ, val, err
} }

View File

@ -10,6 +10,12 @@ import (
// Node is an AST node. // Node is an AST node.
type Node interface { type Node interface {
// context returns the parse tree context.
context() antlr.ParseTree
// setContext sets the parse tree context, returning an error if illegal.
setContext(ctx antlr.ParseTree) error
// Parent returns the node's parent, which may be nil.. // Parent returns the node's parent, which may be nil..
Parent() Node Parent() Node
@ -25,26 +31,11 @@ type Node interface {
// AddChild adds a child node, returning an error if illegal. // AddChild adds a child node, returning an error if illegal.
AddChild(child Node) error AddChild(child Node) error
// Context returns the parse tree context. // Text returns the node's raw text value.
Context() antlr.ParseTree Text() string
// SetContext sets the parse tree context, returning an error if illegal.
SetContext(ctx antlr.ParseTree) error
// String returns a debug-friendly string representation. // String returns a debug-friendly string representation.
String() string String() string
// Text returns the node's text value. This is convenience
// method for Node.Context().GetText().
Text() string
}
// Tabler is a Node marker interface to indicate that the node can be
// selected from. That is, the node represents a SQL table, view, or
// join table, and can be used like "SELECT * FROM [tabler]".
type Tabler interface {
Node
tabler()
} }
// Selector is a Node marker interface for selector node types. A selector node // Selector is a Node marker interface for selector node types. A selector node
@ -113,7 +104,7 @@ func (bn *baseNode) SetChildren(children []Node) error {
return errorf(msgNodeNoAddChildren, bn, len(children)) return errorf(msgNodeNoAddChildren, bn, len(children))
} }
func (bn *baseNode) setChildren(children []Node) { func (bn *baseNode) doSetChildren(children []Node) {
bn.children = children bn.children = children
} }
@ -125,11 +116,13 @@ func (bn *baseNode) Text() string {
return bn.ctx.GetText() return bn.ctx.GetText()
} }
func (bn *baseNode) Context() antlr.ParseTree { // context implements ast.Node.
func (bn *baseNode) context() antlr.ParseTree {
return bn.ctx return bn.ctx
} }
func (bn *baseNode) SetContext(ctx antlr.ParseTree) error { // setContext implements ast.Node.
func (bn *baseNode) setContext(ctx antlr.ParseTree) error {
bn.ctx = ctx bn.ctx = ctx
return nil return nil
} }
@ -142,7 +135,7 @@ func nodeString(n Node) string {
// nodeReplace replaces old with new. That is, nu becomes a child // nodeReplace replaces old with new. That is, nu becomes a child
// of old's parent. // of old's parent.
func nodeReplace(old, nu Node) error { func nodeReplace(old, nu Node) error {
err := nu.SetContext(old.Context()) err := nu.setContext(old.context())
if err != nil { if err != nil {
return err return err
} }
@ -346,7 +339,6 @@ var (
typeSegmentNode = reflect.TypeOf((*SegmentNode)(nil)) typeSegmentNode = reflect.TypeOf((*SegmentNode)(nil))
_ = reflect.TypeOf((*Selector)(nil)).Elem() _ = reflect.TypeOf((*Selector)(nil)).Elem()
typeSelectorNode = reflect.TypeOf((*SelectorNode)(nil)) typeSelectorNode = reflect.TypeOf((*SelectorNode)(nil))
_ = reflect.TypeOf((*Tabler)(nil)).Elem()
typeTblColSelectorNode = reflect.TypeOf((*TblColSelectorNode)(nil)) typeTblColSelectorNode = reflect.TypeOf((*TblColSelectorNode)(nil))
typeTblSelectorNode = reflect.TypeOf((*TblSelectorNode)(nil)) typeTblSelectorNode = reflect.TypeOf((*TblSelectorNode)(nil))
typeUniqueNode = reflect.TypeOf((*UniqueNode)(nil)) typeUniqueNode = reflect.TypeOf((*UniqueNode)(nil))

View File

@ -9,12 +9,11 @@ import (
) )
func TestChildIndex(t *testing.T) { func TestChildIndex(t *testing.T) {
log := slogt.New(t) const q1 = `@mydb1 | .user | join(.address, .user.uid == .address.uid) | .uid, .username, .country`
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country` p := getSLQParser(q1)
p := getSLQParser(fixtJoinQuery1)
query := p.Query() query := p.Query()
ast, err := buildAST(log, query) ast, err := buildAST(slogt.New(t), query)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, ast) require.NotNil(t, ast)
require.Equal(t, 4, len(ast.Segments())) require.Equal(t, 4, len(ast.Segments()))
@ -34,12 +33,6 @@ func TestNodesWithType(t *testing.T) {
require.Equal(t, 0, len(nodesWithType(nodes, typeJoinNode))) require.Equal(t, 0, len(nodesWithType(nodes, typeJoinNode)))
} }
func TestAvg(t *testing.T) {
const input = `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country | .[0:2] | avg(.uid)` //nolint:lll
ast := mustParse(t, input)
require.NotNil(t, ast)
}
func TestNodePrevNextSibling(t *testing.T) { func TestNodePrevNextSibling(t *testing.T) {
const in = `@sakila | .actor | .actor_id == 2` const in = `@sakila | .actor | .actor_id == 2`

View File

@ -47,7 +47,7 @@ func (n *OrderByNode) SetChildren(children []Node) error {
} }
} }
n.setChildren(children) n.doSetChildren(children)
return nil return nil
} }
@ -100,7 +100,7 @@ func (n *OrderByTermNode) SetChildren(children []Node) error {
n, len(children)) n, len(children))
} }
n.setChildren(children) n.doSetChildren(children)
return nil return nil
} }

View File

@ -174,10 +174,8 @@ func (v *parseTreeVisitor) Visit(ctx antlr.ParseTree) any {
return v.VisitJoin(ctx) return v.VisitJoin(ctx)
case *slq.AliasContext: case *slq.AliasContext:
return v.VisitAlias(ctx) return v.VisitAlias(ctx)
case *slq.JoinConstraintContext: case *slq.JoinTableContext:
return v.VisitJoinConstraint(ctx) return v.VisitJoinTable(ctx)
case *slq.CmprContext:
return v.VisitCmpr(ctx)
case *slq.RowRangeContext: case *slq.RowRangeContext:
return v.VisitRowRange(ctx) return v.VisitRowRange(ctx)
case *slq.ExprElementContext: case *slq.ExprElementContext:
@ -250,11 +248,6 @@ func (v *parseTreeVisitor) VisitElement(ctx *slq.ElementContext) any {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }
// VisitCmpr implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitCmpr(ctx *slq.CmprContext) any {
return v.VisitChildren(ctx)
}
// VisitStmtList implements slq.SLQVisitor. // VisitStmtList implements slq.SLQVisitor.
func (v *parseTreeVisitor) VisitStmtList(_ *slq.StmtListContext) any { func (v *parseTreeVisitor) VisitStmtList(_ *slq.StmtListContext) any {
return nil // not using StmtList just yet return nil // not using StmtList just yet

View File

@ -3,6 +3,8 @@ package ast
import ( import (
"testing" "testing"
"github.com/neilotoole/sq/testh/tutil"
"github.com/neilotoole/slogt" "github.com/neilotoole/slogt"
"github.com/antlr/antlr4/runtime/Go/antlr/v4" "github.com/antlr/antlr4/runtime/Go/antlr/v4"
@ -11,31 +13,6 @@ import (
"github.com/neilotoole/sq/libsq/ast/internal/slq" "github.com/neilotoole/sq/libsq/ast/internal/slq"
) )
const (
fixtRowRange1 = `@mydb1 | .user | .uid, .username | .[]`
fixtRowRange2 = `@mydb1 | .user | .uid, .username | .[2]`
fixtRowRange3 = `@mydb1 | .user | .uid, .username | .[1:3]`
fixtRowRange4 = `@mydb1 | .user | .uid, .username | .[0:3]`
fixtRowRange5 = `@mydb1 | .user | .uid, .username | .[:3]`
fixtRowRange6 = `@mydb1 | .user | .uid, .username | .[2:]`
fixtJoinQuery1 = `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country`
fixtSelect1 = `@mydb1 | .user | .uid, .username`
)
var slqInputs = map[string]string{
"rr1": `@mydb1 | .user | .uid, .username | .[]`,
"rr2": `@mydb1 | .user | .uid, .username | .[2]`,
"rr3": `@mydb1 | .user | .uid, .username | .[1:3]`,
"rr4": `@mydb1 | .user | .uid, .username | .[0:3]`,
"rr5": `@mydb1 | .user | .uid, .username | .[:3]`,
"rr6": `@mydb1 | .user | .uid, .username | .[2:]`,
"join with row range": `@my1 |.user, .address | join(.uid) | .[0:4] | .user.uid, .username, .country`,
"join1": `@mydb1 | .user, .address | join(.user.uid == .address.uid) | .uid, .username, .country`,
"select1": `@mydb1 | .user | .uid, .username`,
"tbl datasource": `@mydb1.user | .uid, .username`,
"count1": `@mydb1.user | count`,
}
// getSLQParser returns a parser for the given SQL input. // getSLQParser returns a parser for the given SQL input.
func getSLQParser(input string) *slq.SLQParser { func getSLQParser(input string) *slq.SLQParser {
is := antlr.NewInputStream(input) is := antlr.NewInputStream(input)
@ -71,10 +48,10 @@ func mustParse(t *testing.T, input string) *AST {
} }
func TestSimpleQuery(t *testing.T) { func TestSimpleQuery(t *testing.T) {
const q1 = `@mydb1 | .user | .uid, .username`
log := slogt.New(t) log := slogt.New(t)
const input = fixtSelect1
ptree, err := parseSLQ(log, input) ptree, err := parseSLQ(log, q1)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, ptree) require.NotNil(t, ptree)
@ -83,15 +60,33 @@ func TestSimpleQuery(t *testing.T) {
require.NotNil(t, ast) require.NotNil(t, ast)
} }
// TestParseBuild performs some basic testing of the parser.
// These tests are largely duplicates of other tests, and
// probably should be consolidated.
func TestParseBuild(t *testing.T) { func TestParseBuild(t *testing.T) {
for test, input := range slqInputs { testCases := []struct {
test, input := test, input name string
in string
}{
{"rr1", `@mydb1 | .user | .uid, .username | .[]`},
{"rr2", `@mydb1 | .user | .uid, .username | .[2]`},
{"rr3", `@mydb1 | .user | .uid, .username | .[1:3]`},
{"rr4", `@mydb1 | .user | .uid, .username | .[0:3]`},
{"rr5", `@mydb1 | .user | .uid, .username | .[:3]`},
{"rr6", `@mydb1 | .user | .uid, .username | .[2:]`},
{"join with row range", `@my1 |.user | join(.address, .uid) | .[0:4] | .user.uid, .username, .country`},
{"join1", `@mydb1 | .user | join(.address, .user.uid == .address.uid) | .uid, .username, .country`},
{"select1", `@mydb1 | .user | .uid, .username`},
{"tbl datasource", `@mydb1.user | .uid, .username`},
{"count1", `@mydb1.user | count`},
}
t.Run(test, func(t *testing.T) { for i, tc := range testCases {
t.Logf(input) t.Run(tutil.Name(i, tc.name), func(t *testing.T) {
t.Logf(tc.in)
log := slogt.New(t) log := slogt.New(t)
ptree, err := parseSLQ(log, input) ptree, err := parseSLQ(log, tc.in)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, ptree) require.NotNil(t, ptree)

View File

@ -66,7 +66,7 @@ func narrowTblColSel(w *Walker, node Node) error {
parent := sel.Parent() parent := sel.Parent()
switch parent := parent.(type) { switch parent := parent.(type) {
case *JoinConstraint, *FuncNode, *OrderByTermNode, *GroupByNode, *ExprNode: case *FuncNode, *OrderByTermNode, *GroupByNode, *ExprNode:
if sel.name1 == "" { if sel.name1 == "" {
return nil return nil
} }
@ -78,14 +78,14 @@ func narrowTblColSel(w *Walker, node Node) error {
return nodeReplace(sel, tblColSelNode) return nodeReplace(sel, tblColSelNode)
case *SegmentNode: case *SegmentNode:
// if the parent is a segment, this is a "top-level" selector. // if the parent is a segment, this is a "top-level" selector.
// Only top-level selectors after the final tabler seg are // Only top-level selectors after the final table seg are
// convert to TblColSelectorNode. // converted to TblColSelectorNode.
tablerSeg, err := NewInspector(w.root.(*AST)).FindFinalTablerSegment() tblSeg, err := NewInspector(w.root.(*AST)).FindFinalTableSegment()
if err != nil { if err != nil {
return err return err
} }
if parent.SegIndex() <= tablerSeg.SegIndex() { if parent.SegIndex() <= tblSeg.SegIndex() {
// Skipping this selector because it's not after the final selectable segment // Skipping this selector because it's not after the final selectable segment
return nil return nil
} }
@ -119,7 +119,7 @@ func narrowColSel(w *Walker, node Node) error {
parent := sel.Parent() parent := sel.Parent()
switch parent := parent.(type) { switch parent := parent.(type) {
case *JoinConstraint, *FuncNode, *OrderByTermNode, *GroupByNode, *ExprNode: case *FuncNode, *OrderByTermNode, *GroupByNode, *ExprNode:
colSel, err := newColSelectorNode(sel) colSel, err := newColSelectorNode(sel)
if err != nil { if err != nil {
return err return err
@ -127,14 +127,14 @@ func narrowColSel(w *Walker, node Node) error {
return nodeReplace(sel, colSel) return nodeReplace(sel, colSel)
case *SegmentNode: case *SegmentNode:
// if the parent is a segment, this is a "top-level" selector. // if the parent is a segment, this is a "top-level" selector.
// Only top-level selectors after the final tabler seg are // Only top-level selectors after the final table seg are
// convert to colSels. // convert to colSels.
tablerSeg, err := NewInspector(w.root.(*AST)).FindFinalTablerSegment() tblSeg, err := NewInspector(w.root.(*AST)).FindFinalTableSegment()
if err != nil { if err != nil {
return err return err
} }
if parent.SegIndex() <= tablerSeg.SegIndex() { if parent.SegIndex() <= tblSeg.SegIndex() {
// Skipping this selector because it's not after the final selectable segment // Skipping this selector because it's not after the final selectable segment
return nil return nil
} }
@ -151,38 +151,3 @@ func narrowColSel(w *Walker, node Node) error {
return nil return nil
} }
// determineJoinTables attempts to determine the tables that a JOIN refers to.
func determineJoinTables(_ *Walker, node Node) error {
// node is guaranteed to be FnJoin
fnJoin, ok := node.(*JoinNode)
if !ok {
return errorf("expected *FnJoin but got %T", node)
}
seg, ok := fnJoin.Parent().(*SegmentNode)
if !ok {
return errorf("JOIN() must have a *SegmentNode parent, but got %T", fnJoin.Parent())
}
prevSeg := seg.Prev()
if prevSeg == nil {
return errorf("JOIN() must not be in the first segment")
}
if len(prevSeg.Children()) != 2 || len(nodesWithType(prevSeg.Children(), typeTblSelectorNode)) != 2 {
return errorf("JOIN() must have two table selectors in the preceding segment")
}
fnJoin.leftTbl, ok = prevSeg.Children()[0].(*TblSelectorNode)
if !ok {
return errorf("JOIN() expected table selector in previous segment, but was %T(%s)", prevSeg.Children()[0],
prevSeg.Children()[0].Text())
}
fnJoin.rightTbl, ok = prevSeg.Children()[1].(*TblSelectorNode)
if !ok {
return errorf("JOIN() expected table selector in previous segment, but was %T(%s)", prevSeg.Children()[1],
prevSeg.Children()[1].Text())
}
return nil
}

View File

@ -3,62 +3,51 @@ package ast
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/neilotoole/sq/testh/tutil"
"github.com/stretchr/testify/require"
) )
// [] select all rows (no range) // TestRowRange tests the row range mechanism.
// [1] select row[1] //
// [10:15] select rows 10 thru 15 // [] select all rows (no range)
// [0:15] select rows 0 thru 15 // [1] select row[1]
// [:15] same as above (0 thru 15) // [10:15] select rows 10 thru 15
// [10:] select all rows from 10 onwards // [0:15] select rows 0 thru 15
// [:15] same as above (0 thru 15)
// [10:] select all rows from 10 onwards
func TestRowRange(t *testing.T) {
testCases := []struct {
in string
wantRowRange bool
wantOffset int
wantLimit int
}{
{".actor | .[]", false, 0, 0},
{".actor | .[2]", true, 2, 1},
{".actor | .[1:3]", true, 1, 2},
{".actor | .[0:3]", true, 0, 3},
{".actor | .[:3]", true, 0, 3},
{".actor | .[2:]", true, 2, -1},
}
// TODO: Move this to libsq/query_range_test.go for i, tc := range testCases {
tc := tc
t.Run(tutil.Name(i, tc.in), func(t *testing.T) {
ast := mustParse(t, tc.in)
insp := NewInspector(ast)
nodes := insp.FindNodes(typeRowRangeNode)
func TestRowRange1(t *testing.T) { if !tc.wantRowRange {
ast := mustParse(t, fixtRowRange1) require.Empty(t, nodes)
assert.Equal(t, 0, NewInspector(ast).CountNodes(typeRowRangeNode)) return
} }
func TestRowRange2(t *testing.T) { require.Len(t, nodes, 1)
ast := mustParse(t, fixtRowRange2) rr, ok := nodes[0].(*RowRangeNode)
insp := NewInspector(ast) require.True(t, ok)
assert.Equal(t, 1, insp.CountNodes(typeRowRangeNode))
nodes := insp.FindNodes(typeRowRangeNode) require.Equal(t, tc.wantOffset, rr.Offset)
assert.Equal(t, 1, len(nodes)) require.Equal(t, tc.wantLimit, rr.Limit)
rr, _ := nodes[0].(*RowRangeNode) })
assert.Equal(t, 2, rr.Offset) }
assert.Equal(t, 1, rr.Limit)
}
func TestRowRange3(t *testing.T) {
ast := mustParse(t, fixtRowRange3)
insp := NewInspector(ast)
rr, _ := insp.FindNodes(typeRowRangeNode)[0].(*RowRangeNode)
assert.Equal(t, 1, rr.Offset)
assert.Equal(t, 2, rr.Limit)
}
func TestRowRange4(t *testing.T) {
ast := mustParse(t, fixtRowRange4)
insp := NewInspector(ast)
rr, _ := insp.FindNodes(typeRowRangeNode)[0].(*RowRangeNode)
assert.Equal(t, 0, rr.Offset)
assert.Equal(t, 3, rr.Limit)
}
func TestRowRange5(t *testing.T) {
ast := mustParse(t, fixtRowRange5)
insp := NewInspector(ast)
rr, _ := insp.FindNodes(typeRowRangeNode)[0].(*RowRangeNode)
assert.Equal(t, 0, rr.Offset)
assert.Equal(t, 3, rr.Limit)
}
func TestRowRange6(t *testing.T) {
ast := mustParse(t, fixtRowRange6)
insp := NewInspector(ast)
rr, _ := insp.FindNodes(typeRowRangeNode)[0].(*RowRangeNode)
assert.Equal(t, 2, rr.Offset)
assert.Equal(t, -1, rr.Limit)
} }

View File

@ -18,7 +18,7 @@ func doFromTable(rc *Context, tblSel *ast.TblSelectorNode) (string, error) {
clause := "FROM " + rc.Dialect.Enquote(tblName) clause := "FROM " + rc.Dialect.Enquote(tblName)
alias := tblSel.Alias() alias := tblSel.Alias()
if alias != "" { if alias != "" {
clause += " " + rc.Dialect.Enquote(alias) clause += " AS " + rc.Dialect.Enquote(alias)
} }
return clause, nil return clause, nil

View File

@ -1,86 +1,101 @@
package render package render
import ( import (
"fmt"
"github.com/neilotoole/sq/libsq/ast" "github.com/neilotoole/sq/libsq/ast"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/samber/lo"
) )
func doJoin(rc *Context, fnJoin *ast.JoinNode) (string, error) { func renderJoinType(jt jointype.Type) (string, error) {
switch jt {
case jointype.Inner:
return "INNER JOIN", nil
case jointype.Left:
return "LEFT JOIN", nil
case jointype.LeftOuter:
return "LEFT OUTER JOIN", nil
case jointype.Right:
return "RIGHT JOIN", nil
case jointype.RightOuter:
return "RIGHT OUTER JOIN", nil
case jointype.FullOuter:
return "FULL OUTER JOIN", nil
case jointype.Cross:
return "CROSS JOIN", nil
default:
return "", errz.Errorf("unknown join type: %s", jt)
}
}
func doJoin(rc *Context, leftTbl *ast.TblSelectorNode, joins []*ast.JoinNode) (string, error) {
enquote := rc.Dialect.Enquote enquote := rc.Dialect.Enquote
joinType := "INNER JOIN" allTbls := make([]*ast.TblSelectorNode, len(joins)+1)
onClause := "" allTbls[0] = leftTbl
for i := range joins {
if len(fnJoin.Children()) == 0 { allTbls[i+1] = joins[i].Table()
joinType = "NATURAL JOIN"
} else {
joinExpr, ok := fnJoin.Children()[0].(*ast.JoinConstraint)
if !ok {
return "", errz.Errorf("expected *FnJoinExpr but got %T", fnJoin.Children()[0])
}
leftOperand := ""
operator := ""
rightOperand := ""
if len(joinExpr.Children()) == 1 {
// It's a single col selector
colSel, ok := joinExpr.Children()[0].(*ast.ColSelectorNode)
if !ok {
return "", errz.Errorf("expected *ColSelectorNode but got %T", joinExpr.Children()[0])
}
colVal, err := colSel.SelValue()
if err != nil {
return "", err
}
leftTblVal := fnJoin.LeftTbl().TblName()
leftOperand = fmt.Sprintf(
"%s.%s",
enquote(leftTblVal),
enquote(colVal),
)
operator = "=="
rightTblVal := fnJoin.RightTbl().TblName()
rightOperand = fmt.Sprintf(
"%s.%s",
enquote(rightTblVal),
enquote(colVal),
)
} else {
var err error
leftOperand, err = renderSelectorNode(rc.Dialect, joinExpr.Children()[0])
if err != nil {
return "", err
}
operator = joinExpr.Children()[1].Text()
rightOperand, err = renderSelectorNode(rc.Dialect, joinExpr.Children()[2])
if err != nil {
return "", err
}
}
if operator == "==" {
operator = "="
}
onClause = fmt.Sprintf("ON %s %s %s", leftOperand, operator, rightOperand)
} }
sql := fmt.Sprintf( sql := "FROM "
"FROM %s %s %s", sql = sqlAppend(sql, enquote(leftTbl.TblName()))
enquote(fnJoin.LeftTbl().TblName()), if leftTbl.Alias() != "" {
joinType, sql = sqlAppend(sql, "AS "+enquote(leftTbl.Alias()))
enquote(fnJoin.RightTbl().TblName()), }
)
sql = sqlAppend(sql, onClause) for i, join := range joins {
var s string
var err error
jt := join.JoinType()
if !lo.Contains(rc.Dialect.Joins, jt) {
return "", errz.Errorf("driver {%s} does not support join type {%s}",
rc.Dialect.Type, jt)
}
if s, err = renderJoinType(jt); err != nil {
return "", err
}
tbl := join.Table()
s = sqlAppend(s, enquote(tbl.TblName()))
if tbl.Alias() != "" {
s = sqlAppend(s, "AS "+enquote(tbl.Alias()))
}
if expr := join.Predicate(); expr != nil {
if !join.JoinType().HasPredicate() {
return "", errz.Errorf("invalid join: {%s} does not accept a predicate: %s",
join.JoinType(), join.Text())
}
s = sqlAppend(s, "ON")
// Special handling for: .left_tbl | join(.right_tbl, .col)
// This is rendered as:
// FROM left_tbl JOIN right_tbl ON left_tbl.col = right_tbl.col
children := expr.Children()
if len(children) == 1 {
if colSel, ok := children[0].(*ast.ColSelectorNode); ok {
// TODO: should be able to handle ast.TblColSelector also?
colName := colSel.ColName()
text := enquote(allTbls[i].TblAliasOrName()) + "." + enquote(colName)
text += " = "
text += enquote(allTbls[i+1].TblAliasOrName()) + "." + enquote(colName)
s = sqlAppend(s, text)
sql = sqlAppend(sql, s)
continue
}
}
var text string
if text, err = rc.Renderer.Expr(rc, expr); err != nil {
return "", err
}
s = sqlAppend(s, text)
}
sql = sqlAppend(sql, s)
}
return sql, nil return sql, nil
} }

View File

@ -19,19 +19,18 @@ func doRange(_ *Context, rr *ast.RowRangeNode) (string, error) {
limit := "" limit := ""
offset := "" offset := ""
if rr.Limit > -1 { if rr.Limit > -1 {
limit = fmt.Sprintf(" LIMIT %d", rr.Limit) limit = fmt.Sprintf("LIMIT %d", rr.Limit)
} }
if rr.Offset > -1 { if rr.Offset > -1 {
offset = fmt.Sprintf(" OFFSET %d", rr.Offset) offset = fmt.Sprintf("OFFSET %d", rr.Offset)
if rr.Limit == -1 { if rr.Limit == -1 {
// MySQL requires a LIMIT if OFFSET is used. Therefore // MySQL requires a LIMIT if OFFSET is used. Therefore
// we make the LIMIT a very large number // we make the LIMIT a very large number
limit = fmt.Sprintf(" LIMIT %d", math.MaxInt64) limit = fmt.Sprintf("LIMIT %d", math.MaxInt64)
} }
} }
sql := limit + offset sql := sqlAppend(limit, offset)
return sql, nil return sql, nil
} }

View File

@ -47,7 +47,7 @@ type Renderer struct {
GroupBy func(rc *Context, gb *ast.GroupByNode) (string, error) GroupBy func(rc *Context, gb *ast.GroupByNode) (string, error)
// Join renders a join fragment. // Join renders a join fragment.
Join func(rc *Context, fnJoin *ast.JoinNode) (string, error) Join func(rc *Context, leftTbl *ast.TblSelectorNode, joins []*ast.JoinNode) (string, error)
// Function renders a function fragment. // Function renders a function fragment.
Function func(rc *Context, fn *ast.FuncNode) (string, error) Function func(rc *Context, fn *ast.FuncNode) (string, error)
@ -159,7 +159,6 @@ const (
// renderSelectorNode renders a selector such as ".actor.first_name" // renderSelectorNode renders a selector such as ".actor.first_name"
// or ".last_name". // or ".last_name".
func renderSelectorNode(d dialect.Dialect, node ast.Node) (string, error) { func renderSelectorNode(d dialect.Dialect, node ast.Node) (string, error) {
// FIXME: switch to using enquote
switch node := node.(type) { switch node := node.(type) {
case *ast.ColSelectorNode: case *ast.ColSelectorNode:
return d.Enquote(node.ColName()), nil return d.Enquote(node.ColName()), nil

View File

@ -62,22 +62,22 @@ func (s *SegmentNode) AddChild(child Node) error {
// SetChildren implements ast.Node. // SetChildren implements ast.Node.
func (s *SegmentNode) SetChildren(children []Node) error { func (s *SegmentNode) SetChildren(children []Node) error {
s.bn.setChildren(children) s.bn.doSetChildren(children)
return nil return nil
} }
// Context implements ast.Node. // context implements ast.Node.
func (s *SegmentNode) Context() antlr.ParseTree { func (s *SegmentNode) context() antlr.ParseTree {
return s.bn.Context() return s.bn.context()
} }
// SetContext implements ast.Node. // setContext implements ast.Node.
func (s *SegmentNode) SetContext(ctx antlr.ParseTree) error { func (s *SegmentNode) setContext(ctx antlr.ParseTree) error {
segCtx, ok := ctx.(*slq.SegmentContext) segCtx, ok := ctx.(*slq.SegmentContext)
if !ok { if !ok {
return errorf("expected *parser.SegmentContext, but got %T", ctx) return errorf("expected *parser.SegmentContext, but got %T", ctx)
} }
return s.bn.SetContext(segCtx) return s.bn.setContext(segCtx)
} }
// ChildType returns the expected Type of the segment's elements, based // ChildType returns the expected Type of the segment's elements, based
@ -142,7 +142,7 @@ func (s *SegmentNode) String() string {
// Text implements ast.Node. // Text implements ast.Node.
func (s *SegmentNode) Text() string { func (s *SegmentNode) Text() string {
return s.bn.Context().GetText() return s.bn.context().GetText()
} }
// Prev returns the previous segment, or nil if this is // Prev returns the previous segment, or nil if this is

View File

@ -7,8 +7,8 @@ import (
) )
func TestSegment(t *testing.T) { func TestSegment(t *testing.T) {
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country` const q1 = `@mydb1 | .user | join(.address, .user.uid == .address.uid) | .uid, .username, .country`
ast := mustParse(t, fixtJoinQuery1) ast := mustParse(t, q1)
segs := ast.Segments() segs := ast.Segments()
assert.Equal(t, 4, len(segs)) assert.Equal(t, 4, len(segs))

View File

@ -121,10 +121,7 @@ func (s *SelectorNode) SelValue() (string, error) {
return extractSelVal(s.ctx) return extractSelVal(s.ctx)
} }
var ( var _ Node = (*TblSelectorNode)(nil)
_ Node = (*TblSelectorNode)(nil)
_ Tabler = (*TblSelectorNode)(nil)
)
// TblSelectorNode is a selector for a table, such as ".my_table" // TblSelectorNode is a selector for a table, such as ".my_table"
// or "@my_src.my_table". // or "@my_src.my_table".
@ -150,6 +147,25 @@ func (n *TblSelectorNode) TblName() string {
return n.tblName return n.tblName
} }
// SyncTblNameAlias sets the table name to the alias value,
// if the alias is non-empty, and then sets the alias to empty.
func (n *TblSelectorNode) SyncTblNameAlias() {
if n.alias != "" {
n.tblName = n.alias
n.alias = ""
}
}
// TblAliasOrName returns the table alias if set; if not, it
// returns the table name.
func (n *TblSelectorNode) TblAliasOrName() string {
if n.alias != "" {
return n.alias
}
return n.tblName
}
// Alias returns the node's alias, or empty string. // Alias returns the node's alias, or empty string.
func (n *TblSelectorNode) Alias() string { func (n *TblSelectorNode) Alias() string {
return n.alias return n.alias
@ -160,9 +176,9 @@ func (n *TblSelectorNode) Handle() string {
return n.handle return n.handle
} }
// Tabler implements the Tabler marker interface. // SetHandle sets the handle.
func (n *TblSelectorNode) tabler() { func (n *TblSelectorNode) SetHandle(h string) {
// no-op n.handle = h
} }
// SelValue returns the table name. // SelValue returns the table name.
@ -305,36 +321,15 @@ func (n *ColSelectorNode) String() string {
return str return str
} }
var _ Node = (*CmprNode)(nil)
// CmprNode models a comparison, such as ".age == 42".
type CmprNode struct {
baseNode
}
// String returns a log/debug-friendly representation.
func (c *CmprNode) String() string {
return nodeString(c)
}
func newCmprNode(parent Node, ctx slq.ICmprContext) *CmprNode {
leaf, _ := ctx.GetChild(0).(*antlr.TerminalNodeImpl) // FIXME: return an error
cmpr := &CmprNode{}
cmpr.ctx = leaf
cmpr.text = leaf.GetText()
cmpr.parent = parent
return cmpr
}
// extractSelVal extracts the value of the selector. The function takes // extractSelVal extracts the value of the selector. The function takes
// a selector node type as input, e.g. ast.SelectorNode. // a selector node type as input, e.g. ast.SelectorNode.
// Example inputs: // Example inputs:
// //
// - .actor --> actor // .actor --> actor
// - .first_name --> first_name // .first_name --> first_name
// - ."first name" --> first name // ."first name" --> first name
// //
// The function will remove the leading period, and quotes around the name. // The function will remove the leading period, and any quotes around the name.
func extractSelVal(ctx antlr.ParseTree) (string, error) { func extractSelVal(ctx antlr.ParseTree) (string, error) {
if ctx == nil { if ctx == nil {
return "", errorf("invalid selector: is nil") return "", errorf("invalid selector: is nil")

View File

@ -8,12 +8,11 @@ import (
) )
func TestWalker(t *testing.T) { func TestWalker(t *testing.T) {
log := slogt.New(t) const q1 = `@mydb1 | .user | join(.address, .user.uid == .address.uid) | .uid, .username, .country`
// `@mydb1 | .user, .address | join(.uid == .uid) | .uid, .username, .country` p := getSLQParser(q1)
p := getSLQParser(fixtJoinQuery1)
query := p.Query() query := p.Query()
ast, err := buildAST(log, query) ast, err := buildAST(slogt.New(t), query)
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, ast) assert.NotNil(t, ast)
@ -48,6 +47,6 @@ func TestWalker(t *testing.T) {
walker.AddVisitor(typeColSelectorNode, visitorB) walker.AddVisitor(typeColSelectorNode, visitorB)
err = walker.Walk() err = walker.Walk()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, countA) assert.Equal(t, 1, countA)
assert.Equal(t, 3, countB) assert.Equal(t, 3, countB)
} }

View File

@ -0,0 +1,94 @@
// Package jointype enumerates the various SQL JOIN types.
package jointype
import "github.com/neilotoole/sq/libsq/core/errz"
// Type indicates the type of join, e.g. "INNER JOIN"
// or "RIGHT OUTER JOIN", etc.
type Type string
// String returns the string value.
func (jt Type) String() string {
return string(jt)
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (jt *Type) UnmarshalText(text []byte) error {
switch string(text) {
case string(Inner), JoinAlias:
*jt = Inner
case string(Left), LeftAlias:
*jt = Left
case string(LeftOuter), LeftOuterAlias:
*jt = LeftOuter
case string(Right), RightAlias:
*jt = Right
case string(RightOuter), RightOuterAlias:
*jt = RightOuter
case string(FullOuter), FullOuterAlias:
*jt = FullOuter
case string(Cross), CrossAlias:
*jt = Cross
default:
return errz.Errorf("invalid join type {%s}", string(text))
}
return nil
}
// HasPredicate returns true if the join type accepts a
// join predicate. Only jointype.Cross returns false.
func (jt Type) HasPredicate() bool {
return jt != Cross
}
const (
Inner Type = "inner_join"
JoinAlias string = "join"
Left Type = "left_join"
LeftAlias string = "ljoin"
LeftOuter Type = "left_outer_join"
LeftOuterAlias string = "lojoin"
Right Type = "right_join"
RightAlias string = "rjoin"
RightOuter Type = "right_outer_join"
RightOuterAlias string = "rojoin"
FullOuter Type = "full_outer_join"
FullOuterAlias string = "fojoin"
Cross Type = "cross_join"
CrossAlias string = "xjoin"
)
// All returns the set of join.Type values.
func All() []Type {
return []Type{
Inner,
Left,
LeftOuter,
Right,
RightOuter,
FullOuter,
Cross,
}
}
// AllValues returns all possible join type values, including
// both canonical names ("cross_join") and aliases ("xjoin").
func AllValues() []string {
return []string{
JoinAlias,
string(Inner),
string(Left),
LeftAlias,
string(LeftOuter),
LeftOuterAlias,
string(Right),
RightAlias,
string(RightOuter),
RightOuterAlias,
string(FullOuter),
FullOuterAlias,
string(Cross),
CrossAlias,
}
}

View File

@ -9,6 +9,16 @@ func All[T any](elems ...T) []T {
return a return a
} }
// Apply returns a new slice whose elements are the result of applying fn to
// each element of collection.
func Apply[T any](collection []T, fn func(item T) T) []T {
a := make([]T, len(collection))
for i := range collection {
a[i] = fn(collection[i])
}
return a
}
// ToSliceType returns a new slice of type T, having performed // ToSliceType returns a new slice of type T, having performed
// type conversion on each element of in. // type conversion on each element of in.
func ToSliceType[S, T any](in ...S) (out []T, ok bool) { func ToSliceType[S, T any](in ...S) (out []T, ok bool) {

View File

@ -3,6 +3,8 @@ package loz_test
import ( import (
"testing" "testing"
"github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/loz" "github.com/neilotoole/sq/libsq/core/loz"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -26,3 +28,10 @@ func TestToSliceType(t *testing.T) {
require.Len(t, got, 2) require.Len(t, got, 2)
require.Equal(t, []string{"hello", "world"}, got) require.Equal(t, []string{"hello", "world"}, got)
} }
func TestApply(t *testing.T) {
input := []string{"hello", "world"}
want := []string{"'hello'", "'world'"}
got := loz.Apply(input, stringz.SingleQuote)
require.Equal(t, want, got)
}

View File

@ -159,11 +159,15 @@ func (op BaseOpt) Process(o Options) (Options, error) {
var _ Opt = String{} var _ Opt = String{}
// NewString returns an options.String instance. If flag is empty, the // NewString returns an options.String instance. If flag is empty, the
// value of key is used. // value of key is used. If valid Fn is non-nil, it is called from
func NewString(key, flag string, short rune, defaultVal, usage, help string, tags ...string) String { // the process function.
func NewString(key, flag string, short rune, defaultVal string,
validFn func(string) error, usage, help string, tags ...string,
) String {
return String{ return String{
BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...), BaseOpt: NewBaseOpt(key, flag, short, usage, help, tags...),
defaultVal: defaultVal, defaultVal: defaultVal,
validFn: validFn,
} }
} }
@ -171,6 +175,7 @@ func NewString(key, flag string, short rune, defaultVal, usage, help string, tag
type String struct { type String struct {
BaseOpt BaseOpt
defaultVal string defaultVal string
validFn func(string) error
} }
// GetAny implements options.Opt. // GetAny implements options.Opt.
@ -209,6 +214,31 @@ func (op String) Get(o Options) string {
return s return s
} }
// Process implements options.Opt. If the String was constructed
// with validator function, it is invoked on the value of the Opt,
// if it is set. Otherwise the method is no-op.
func (op String) Process(o Options) (Options, error) {
if op.validFn == nil {
return o, nil
}
v, ok := o[op.key]
if !ok || v == nil {
return o, nil
}
var s string
if s, ok = v.(string); !ok {
return nil, errz.Errorf("expected string value for {%s} but got %T: %v", op.key, v, v)
}
if err := op.validFn(s); err != nil {
return nil, err
}
return o, nil
}
var _ Opt = Int{} var _ Opt = Int{}
// NewInt returns an options.Int instance. If flag is empty, the // NewInt returns an options.Int instance. If flag is empty, the

View File

@ -159,7 +159,7 @@ func TestOptions_LogValue(t *testing.T) {
} }
func TestEffective(t *testing.T) { func TestEffective(t *testing.T) {
optHello := options.NewString("hello", "", 0, "world", "", "") optHello := options.NewString("hello", "", 0, "world", nil, "", "")
optCount := options.NewInt("count", "", 0, 1, "", "") optCount := options.NewInt("count", "", 0, 1, "", "")
in := options.Options{"count": 7} in := options.Options{"count": 7}

View File

@ -13,9 +13,14 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"text/template"
"time" "time"
"unicode" "unicode"
"github.com/alessio/shellescape"
"github.com/Masterminds/sprig/v3"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/google/uuid" "github.com/google/uuid"
@ -632,3 +637,42 @@ func ElementsHavingPrefix(a []string, prefix string) []string {
return strings.HasPrefix(item, prefix) return strings.HasPrefix(item, prefix)
}) })
} }
// NewTemplate returns a new text template, with the sprig
// functions already loaded.
func NewTemplate(name, tpl string) (*template.Template, error) {
t, err := template.New(name).Funcs(sprig.FuncMap()).Parse(tpl)
if err != nil {
return nil, errz.Err(err)
}
return t, nil
}
// ValidTemplate is a convenience wrapper around NewTemplate. It
// returns an error if the tpl is not a valid text template.
func ValidTemplate(name, tpl string) error {
_, err := NewTemplate(name, tpl)
return err
}
// ExecuteTemplate is a convenience function that constructs
// and executes a text template, returning the string value.
func ExecuteTemplate(name, tpl string, data any) (string, error) {
t, err := NewTemplate(name, tpl)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
if err = t.Execute(buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// ShellEscape escapes s, making it safe to pass to a shell.
// Note that empty string will be returned as two single quotes.
func ShellEscape(s string) string {
return shellescape.Quote(s)
}

View File

@ -501,3 +501,59 @@ __`
got := stringz.IndentLines(input, "__") got := stringz.IndentLines(input, "__")
require.Equal(t, got, want) require.Equal(t, got, want)
} }
func TestTemplate(t *testing.T) {
data := map[string]string{"Name": "wubble"}
testCases := []struct {
tpl string
data any
want string
wantErr bool
}{
// "upper" is a sprig func. Verify that it loads.
{"{{.Name | upper}}", data, "WUBBLE", false},
{"{{not_a_func .Name}}_", data, "", true},
}
for i, tc := range testCases {
tc := tc
t.Run(tutil.Name(i, tc.tpl), func(t *testing.T) {
got, gotErr := stringz.ExecuteTemplate(t.Name(), tc.tpl, tc.data)
t.Logf("\nTPL: %s\nGOT: %s\nERR: %v", tc.tpl, got, gotErr)
if tc.wantErr {
require.Error(t, gotErr)
// Also test ValidTemplate while we're at it.
gotErr = stringz.ValidTemplate(t.Name(), tc.tpl)
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
gotErr = stringz.ValidTemplate(t.Name(), tc.tpl)
require.NoError(t, gotErr)
require.Equal(t, tc.want, got)
})
}
}
func TestShellEscape(t *testing.T) {
testCases := []struct {
in string
want string
}{
{"", "''"},
{" ", `' '`},
{"huzzah", "huzzah"},
{"huz zah", `'huz zah'`},
{`huz ' zah`, `'huz '"'"' zah'`},
}
for i, tc := range testCases {
tc := tc
t.Run(tutil.Name(i, tc), func(t *testing.T) {
got := stringz.ShellEscape(tc.in)
require.Equal(t, tc.want, got)
})
}
}

View File

@ -1,7 +1,10 @@
// Package dialect contains functionality for SQL dialects. // Package dialect contains functionality for SQL dialects.
package dialect package dialect
import "github.com/neilotoole/sq/libsq/source" import (
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/neilotoole/sq/libsq/source"
)
// Dialect holds driver-specific SQL dialect values and functions. // Dialect holds driver-specific SQL dialect values and functions.
type Dialect struct { type Dialect struct {
@ -12,16 +15,9 @@ type Dialect struct {
// For example "(?, ?, ?)" or "($1, $2, $3), ($4, $5, $6)". // For example "(?, ?, ?)" or "($1, $2, $3), ($4, $5, $6)".
Placeholders func(numCols, numRows int) string Placeholders func(numCols, numRows int) string
// IdentQuote is the identifier quote rune. Most often this is
// double-quote, e.g. SELECT * FROM "my_table", but can be other
// values such as backtick, e.g. SELECT * FROM `my_table`.
//
// Arguably, this field should be deprecated. There's probably
// no reason not to always use Enquote.
IdentQuote rune `json:"quote"`
// Enquote is a function that quotes and escapes an // Enquote is a function that quotes and escapes an
// identifier (such as a table or column name). // identifier (such as a table or column name). Typically the func
// uses the double-quote rune (although MySQL uses backtick).
Enquote func(string) string Enquote func(string) string
// IntBool is true if BOOLEAN is handled as an INT by the DB driver. // IntBool is true if BOOLEAN is handled as an INT by the DB driver.
@ -30,9 +26,14 @@ type Dialect struct {
// MaxBatchValues is the maximum number of values in a batch insert. // MaxBatchValues is the maximum number of values in a batch insert.
MaxBatchValues int MaxBatchValues int
// Ops is a map of SLQ operator (e.g. "==" or "!=") to // Ops is a map of overridden SLQ operator (e.g. "==" or "!=") to
// its default SQL rendering. // its SQL rendering.
Ops map[string]string Ops map[string]string
// Joins is the set of JOIN types (e.g. "RIGHT JOIN") that
// the dialect supports. Not all drivers support each join type. For
// example, MySQL doesn't support jointype.FullOuter.
Joins []jointype.Type
} }
// String returns a log/debug-friendly representation. // String returns a log/debug-friendly representation.

View File

@ -173,7 +173,7 @@ type DatabaseOpener interface {
type JoinDatabaseOpener interface { type JoinDatabaseOpener interface {
// OpenJoin opens an appropriate Database for use as // OpenJoin opens an appropriate Database for use as
// a work DB for joining across sources. // a work DB for joining across sources.
OpenJoin(ctx context.Context, src1, src2 *source.Source, srcN ...*source.Source) (Database, error) OpenJoin(ctx context.Context, srcs ...*source.Source) (Database, error)
} }
// ScratchDatabaseOpener opens a scratch database. A scratch database is // ScratchDatabaseOpener opens a scratch database. A scratch database is
@ -251,7 +251,11 @@ type SQLDriver interface {
// //
// RecordMeta also returns a NewRecordFunc which can be // RecordMeta also returns a NewRecordFunc which can be
// applied to the scan row from sql.Rows. // applied to the scan row from sql.Rows.
RecordMeta(colTypes []*sql.ColumnType) (record.Meta, NewRecordFunc, error) //
// Implementations of RecordMeta are expected to invoke driver.MungeColNames
// on the column names. This mechanism handles the case of duplicate column
// names in a record.
RecordMeta(ctx context.Context, colTypes []*sql.ColumnType) (record.Meta, NewRecordFunc, error)
// PrepareInsertStmt prepares a statement for inserting // PrepareInsertStmt prepares a statement for inserting
// values to destColNames in destTbl. numRows specifies // values to destColNames in destTbl. numRows specifies
@ -376,7 +380,10 @@ type Metadata struct {
DefaultPort int `json:"default_port" yaml:"default_port"` DefaultPort int `json:"default_port" yaml:"default_port"`
} }
var _ DatabaseOpener = (*Databases)(nil) var (
_ DatabaseOpener = (*Databases)(nil)
_ JoinDatabaseOpener = (*Databases)(nil)
)
// Databases provides a mechanism for getting Database instances. // Databases provides a mechanism for getting Database instances.
// Note that at this time instances returned by Open are cached // Note that at this time instances returned by Open are cached
@ -487,17 +494,13 @@ func (d *Databases) OpenScratch(ctx context.Context, name string) (Database, err
// to OpenScratch. // to OpenScratch.
// //
// OpenJoin implements JoinDatabaseOpener. // OpenJoin implements JoinDatabaseOpener.
func (d *Databases) OpenJoin(ctx context.Context, src1, src2 *source.Source, srcN ...*source.Source) (Database, error) { func (d *Databases) OpenJoin(ctx context.Context, srcs ...*source.Source) (Database, error) {
if len(srcN) > 0 { var names []string
return nil, errz.Errorf("Currently only two-source join is supported") for _, src := range srcs {
names = append(names, src.Handle[1:])
} }
names := []string{src1.Handle, src2.Handle} d.log.Debug("OpenJoin", "sources", strings.Join(names, ","))
for _, src := range srcN {
names = append(names, src.Handle)
}
d.log.Debug("OpenJoin: [%s]", strings.Join(names, ","))
return d.OpenScratch(ctx, "joindb__"+strings.Join(names, "_")) return d.OpenScratch(ctx, "joindb__"+strings.Join(names, "_"))
} }

View File

@ -1,9 +1,12 @@
package driver_test package driver_test
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
@ -142,7 +145,7 @@ func TestDriver_CreateTable_Minimal(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, len(colNames), len(colTypes)) require.Equal(t, len(colNames), len(colTypes))
recMeta, _, err := drvr.RecordMeta(colTypes) recMeta, _, err := drvr.RecordMeta(th.Context, colTypes)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, colNames, recMeta.Names()) require.Equal(t, colNames, recMeta.Names())
@ -400,7 +403,6 @@ func TestRegistry_DriversMetadata_SQL(t *testing.T) {
dialect := sqlDrvr.Dialect() dialect := sqlDrvr.Dialect()
require.Equal(t, typ, dialect.Type) require.Equal(t, typ, dialect.Type)
require.NotEmpty(t, dialect.IdentQuote)
require.NotNil(t, dialect.Placeholders) require.NotNil(t, dialect.Placeholders)
}) })
} }
@ -470,7 +472,7 @@ func TestDatabase_SourceMetadata(t *testing.T) {
// TestDatabase_SourceMetadata_concurrent tests the behavior of the // TestDatabase_SourceMetadata_concurrent tests the behavior of the
// drivers when SourceMetadata is invoked concurrently. // drivers when SourceMetadata is invoked concurrently.
func TestDatabase_SourceMetadata_concurrent(t *testing.T) { //nolint:tparallel func TestDatabase_SourceMetadata_concurrent(t *testing.T) { //nolint:tparallel
const concurrency = 10 const concurrency = 5
handles := sakila.SQLLatest() handles := sakila.SQLLatest()
for _, handle := range handles { for _, handle := range handles {
@ -623,3 +625,24 @@ func TestSQLDriver_ErrWrap_IsErrNotExist(t *testing.T) {
}) })
} }
} }
func TestMungeColNames(t *testing.T) {
testCases := []struct {
in []string
want []string
}{
{[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
{[]string{"a", "b", "a", "d"}, []string{"a", "b", "a_1", "d"}},
{[]string{"a", "b", "a", "b", "d", "a"}, []string{"a", "b", "a_1", "b_1", "d", "a_2"}},
}
for i, tc := range testCases {
tc := tc
t.Run(tutil.Name(i, tc.in), func(t *testing.T) {
ctx := options.NewContext(context.Background(), options.Options{})
got, err := driver.MungeColNames(ctx, tc.in)
require.NoError(t, err)
require.Equal(t, tc.want, got)
})
}
}

View File

@ -1,6 +1,7 @@
package driver package driver
import ( import (
"bytes"
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
@ -9,6 +10,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
@ -332,8 +337,8 @@ func PrepareInsertStmt(ctx context.Context, drvr SQLDriver, db sqlz.Preparer, de
} }
dialect := drvr.Dialect() dialect := drvr.Dialect()
quote := string(dialect.IdentQuote) tblNameQuoted := dialect.Enquote(destTbl)
tblNameQuoted, colNamesQuoted := stringz.Surround(destTbl, quote), stringz.SurroundSlice(destCols, quote) colNamesQuoted := loz.Apply(destCols, dialect.Enquote)
colsJoined := strings.Join(colNamesQuoted, Comma) colsJoined := strings.Join(colNamesQuoted, Comma)
placeholders := dialect.Placeholders(len(colNamesQuoted), numRows) placeholders := dialect.Placeholders(len(colNamesQuoted), numRows)
@ -594,3 +599,113 @@ func mungeSetZeroValue(i int, rec []any, destMeta record.Meta) {
z := reflect.Zero(destMeta[i].ScanType()).Interface() z := reflect.Zero(destMeta[i].ScanType()).Interface()
rec[i] = z rec[i] = z
} }
// OptResultColRename transforms a column name returned from the DB.
var OptResultColRename = options.NewString(
"result.column.rename",
"",
0,
"{{.Name}}{{with .Recurrence}}_{{.}}{{end}}",
func(s string) error {
return stringz.ValidTemplate("result.column.rename", s)
},
"Template to rename result columns",
`This Go text template is executed on the column names returned
from the DB. Its primary purpose is to rename duplicate column names. For
example, given a query that results in this SQL:
SELECT * FROM actor JOIN film_actor ON actor.actor_id = film_actor.actor_id
The returned result set will have these column names:
actor_id, first_name, last_name, last_update, actor_id, film_id, last_update
|- from "actor" -| |- from "film_actor" -|
Note the duplicate "actor_id" and "last_update" column names. When output in a
format (such as JSON) that doesn't permit duplicate keys, only one of each
duplicate column could appear.
The fields available in the template are:
.Name column name
.Index zero-based index of the column in the result set
.Recurrence nth recurrence of the colum name in the result set
For a unique column name, e.g. "first_name" above, ".Recurrence" will be 0.
For duplicate column names, ".Recurrence" will be 0 for the first instance,
then 1 for the next instance, and so on.
The default template renames the columns to:
actor_id, first_name, last_name, last_update, actor_id_1, film_id, last_update_1`,
)
// MungeColNames transforms column names, per the template defined
// in the option driver.OptResultColRename found on the context.
// This mechanism is used to deduplicate column names, as can happen in
// in "SELECT * FROM ... JOIN" situations. For example, if the result set
// has columns [actor_id, first_name, actor_id], the columns might be
// transformed to [actor_id, first_name, actor_id_1].
//
// MungeColNames should be invoked by each impl of SQLDriver.RecordMeta
// before returning the record.Meta.
func MungeColNames(ctx context.Context, ogColNames []string) (colNames []string, err error) {
if len(ogColNames) == 0 {
return ogColNames, nil
}
o := options.FromContext(ctx)
tplText := OptResultColRename.Get(o)
if tplText == "" {
return ogColNames, nil
}
tpl, err := stringz.NewTemplate(OptResultColRename.Key(), tplText)
if err != nil {
return nil, errz.Wrap(err, "config: ")
}
cols := make([]colMungeData, len(ogColNames))
for i := range ogColNames {
data := colMungeData{
Name: ogColNames[i],
Index: i,
}
for j := 0; j < i; j++ {
if ogColNames[j] == data.Name {
data.Recurrence++
}
}
cols[i] = data
}
colNames = make([]string, len(cols))
buf := &bytes.Buffer{}
for i := range cols {
if err = tpl.Execute(buf, cols[i]); err != nil {
return nil, err
}
colNames[i] = buf.String()
buf.Reset()
}
return colNames, nil
}
// colMungeData is the struct passed to the template from OptResultColRename,
// used in MungeColNames.
type colMungeData struct {
// Name is the original column name.
Name string
// Index is the column index.
Index int
// Recurrence is the count of times this column name has already
// appeared in the list of column names. If the column name is unique,
// this value is zero.
Recurrence int
}

View File

@ -95,24 +95,23 @@ type RecordWriter interface {
// ExecuteSLQ executes the slq query, writing the results to recw. // ExecuteSLQ executes the slq query, writing the results to recw.
// The caller is responsible for closing qc. // The caller is responsible for closing qc.
func ExecuteSLQ(ctx context.Context, qc *QueryContext, query string, recw RecordWriter) error { func ExecuteSLQ(ctx context.Context, qc *QueryContext, query string, recw RecordWriter) error {
ng, err := newEngine(ctx, qc, query) p, err := newPipeline(ctx, qc, query)
if err != nil { if err != nil {
return err return err
} }
return ng.execute(ctx, recw) return p.execute(ctx, recw)
} }
// SLQ2SQL simulates execution of a SLQ query, but instead of executing // SLQ2SQL simulates execution of a SLQ query, but instead of executing
// the resulting SQL query, that ultimate SQL is returned. Effectively it is // the resulting SQL query, that ultimate SQL is returned. Effectively it is
// equivalent to libsq.ExecuteSLQ, but without the execution. // equivalent to libsq.ExecuteSLQ, but without the execution.
func SLQ2SQL(ctx context.Context, qc *QueryContext, query string) (targetSQL string, err error) { func SLQ2SQL(ctx context.Context, qc *QueryContext, query string) (targetSQL string, err error) {
var ng *engine p, err := newPipeline(ctx, qc, query)
ng, err = newEngine(ctx, qc, query)
if err != nil { if err != nil {
return "", err return "", err
} }
return ng.targetSQL, nil return p.targetSQL, nil
} }
// QuerySQL executes the SQL query against dbase, writing // QuerySQL executes the SQL query against dbase, writing
@ -173,7 +172,7 @@ func QuerySQL(ctx context.Context, dbase driver.Database, recw RecordWriter, que
} }
drvr := dbase.SQLDriver() drvr := dbase.SQLDriver()
recMeta, recFromScanRowFn, err := drvr.RecordMeta(colTypes) recMeta, recFromScanRowFn, err := drvr.RecordMeta(ctx, colTypes)
if err != nil { if err != nil {
return errw(err) return errw(err)
} }

View File

@ -2,8 +2,13 @@ package libsq_test
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/samber/lo"
"github.com/neilotoole/sq/testh/tutil" "github.com/neilotoole/sq/testh/tutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -111,3 +116,27 @@ func TestQuerySQL_Count(t *testing.T) { //nolint:tparallel
}) })
} }
} }
// TestJoinDuplicateColNamesAreRenamed tests handling of multiple occurrences
// of the same result column name. The expected behavior is that the duplicate
// column is renamed.
func TestJoinDuplicateColNamesAreRenamed(t *testing.T) {
th := testh.New(t)
src := th.Source(sakila.SL3)
const query = "SELECT * FROM actor INNER JOIN film_actor ON actor.actor_id = film_actor.actor_id LIMIT 1"
sink, err := th.QuerySQL(src, query)
require.NoError(t, err)
colNames := sink.RecMeta.Names()
// Without intervention, the returned column names would contain duplicates.
// [actor_id, first_name, last_name, last_update, actor_id, film_id, last_update]
t.Logf("Cols: [%s]", strings.Join(colNames, ", "))
colCounts := lo.CountValues(colNames)
for col, count := range colCounts {
assert.True(t, count == 1, "col name {%s} is not unique (occurs %d times)",
col, count)
}
}

View File

@ -2,6 +2,9 @@ package libsq
import ( import (
"context" "context"
"fmt"
"github.com/samber/lo"
"github.com/neilotoole/sq/libsq/source" "github.com/neilotoole/sq/libsq/source"
@ -15,8 +18,6 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"golang.org/x/exp/slog"
"github.com/neilotoole/sq/libsq/ast" "github.com/neilotoole/sq/libsq/ast"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/sqlmodel"
@ -25,10 +26,9 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
// engine executes a queryModel and writes to a RecordWriter. // pipeline is used to execute a SLQ query,
type engine struct { // and write the resulting records to a RecordWriter.
log *slog.Logger type pipeline struct {
// query is the SLQ query // query is the SLQ query
query string query string
@ -36,8 +36,8 @@ type engine struct {
qc *QueryContext qc *QueryContext
// rc is the Context for rendering SQL. // rc is the Context for rendering SQL.
// This field is set during engine.prepare. It can't be set before // This field is set during pipeline.prepare. It can't be set before
// then because the target DB to use is calculated during engine.prepare, // then because the target DB to use is calculated during pipeline.prepare,
// based on the input query and other context. // based on the input query and other context.
rc *render.Context rc *render.Context
@ -54,7 +54,9 @@ type engine struct {
targetDB driver.Database targetDB driver.Database
} }
func newEngine(ctx context.Context, qc *QueryContext, query string) (*engine, error) { // newPipeline parses query, returning a pipeline prepared for
// execution via pipeline.execute.
func newPipeline(ctx context.Context, qc *QueryContext, query string) (*pipeline, error) {
log := lg.FromContext(ctx) log := lg.FromContext(ctx)
a, err := ast.Parse(log, query) a, err := ast.Parse(log, query)
@ -62,57 +64,54 @@ func newEngine(ctx context.Context, qc *QueryContext, query string) (*engine, er
return nil, err return nil, err
} }
qModel, err := buildQueryModel(log, a) qModel, err := buildQueryModel(qc, a)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ng := &engine{ p := &pipeline{
log: log,
qc: qc, qc: qc,
query: query, query: query,
} }
if err = ng.prepare(ctx, qModel); err != nil { if err = p.prepare(ctx, qModel); err != nil {
return nil, err return nil, err
} }
return ng, nil return p, nil
} }
// execute executes the plan that was built by engine.prepare. // execute executes the pipeline, writing results to recw.
func (ng *engine) execute(ctx context.Context, recw RecordWriter) error { func (p *pipeline) execute(ctx context.Context, recw RecordWriter) error {
ng.log.Debug( lg.FromContext(ctx).Debug(
"Execute SQL query", "Execute SQL query",
lga.Src, ng.targetDB.Source(), lga.Src, p.targetDB.Source(),
// lga.Target, ng.targetDB.Source().Handle, lga.SQL, p.targetSQL,
lga.SQL, ng.targetSQL,
) )
err := ng.executeTasks(ctx) if err := p.executeTasks(ctx); err != nil {
if err != nil {
return err return err
} }
return QuerySQL(ctx, ng.targetDB, recw, ng.targetSQL) return QuerySQL(ctx, p.targetDB, recw, p.targetSQL)
} }
// executeTasks executes any tasks in engine.tasks. // executeTasks executes any tasks in pipeline.tasks.
// These tasks may exist if preparatory work must be performed // These tasks may exist if preparatory work must be performed
// before engine.targetSQL can be executed. // before pipeline.targetSQL can be executed.
func (ng *engine) executeTasks(ctx context.Context) error { func (p *pipeline) executeTasks(ctx context.Context) error {
switch len(ng.tasks) { switch len(p.tasks) {
case 0: case 0:
return nil return nil
case 1: case 1:
return ng.tasks[0].executeTask(ctx) return p.tasks[0].executeTask(ctx)
default: default:
} }
g, gCtx := errgroup.WithContext(ctx) g, gCtx := errgroup.WithContext(ctx)
g.SetLimit(driver.OptTuningErrgroupLimit.Get(options.FromContext(ctx))) g.SetLimit(driver.OptTuningErrgroupLimit.Get(options.FromContext(ctx)))
for _, task := range ng.tasks { for _, task := range p.tasks {
task := task task := task
g.Go(func() error { g.Go(func() error {
@ -128,12 +127,13 @@ func (ng *engine) executeTasks(ctx context.Context) error {
return g.Wait() return g.Wait()
} }
// prepareNoTabler is invoked when the queryModel doesn't have a tabler. // prepareNoTable is invoked when the queryModel doesn't have a table.
// That is to say, the query doesn't have a "FROM table" clause. It is // That is to say, the query doesn't have a "FROM table" clause. It is
// this function's responsibility to figure out what source to use, and // this function's responsibility to figure out what source to use, and
// to set the relevant engine fields. // to set the relevant pipeline fields.
func (ng *engine) prepareNoTabler(ctx context.Context, qm *queryModel) error { func (p *pipeline) prepareNoTable(ctx context.Context, qm *queryModel) error {
ng.log.Debug("No Tabler in query; will look for source to use...") log := lg.FromContext(ctx)
log.Debug("No table in query; will look for source to use...")
var ( var (
src *source.Source src *source.Source
@ -142,35 +142,35 @@ func (ng *engine) prepareNoTabler(ctx context.Context, qm *queryModel) error {
) )
if handle == "" { if handle == "" {
if src = ng.qc.Collection.Active(); src == nil { if src = p.qc.Collection.Active(); src == nil {
ng.log.Debug("No active source, will use scratchdb.") log.Debug("No active source, will use scratchdb.")
ng.targetDB, err = ng.qc.ScratchDBOpener.OpenScratch(ctx, "scratch") p.targetDB, err = p.qc.ScratchDBOpener.OpenScratch(ctx, "scratch")
if err != nil { if err != nil {
return err return err
} }
ng.rc = &render.Context{ p.rc = &render.Context{
Renderer: ng.targetDB.SQLDriver().Renderer(), Renderer: p.targetDB.SQLDriver().Renderer(),
Args: ng.qc.Args, Args: p.qc.Args,
Dialect: ng.targetDB.SQLDriver().Dialect(), Dialect: p.targetDB.SQLDriver().Dialect(),
} }
return nil return nil
} }
ng.log.Debug("Using active source.", lga.Src, src) log.Debug("Using active source.", lga.Src, src)
} else if src, err = ng.qc.Collection.Get(handle); err != nil { } else if src, err = p.qc.Collection.Get(handle); err != nil {
return err return err
} }
// At this point, src is non-nil. // At this point, src is non-nil.
if ng.targetDB, err = ng.qc.DBOpener.Open(ctx, src); err != nil { if p.targetDB, err = p.qc.DBOpener.Open(ctx, src); err != nil {
return err return err
} }
ng.rc = &render.Context{ p.rc = &render.Context{
Renderer: ng.targetDB.SQLDriver().Renderer(), Renderer: p.targetDB.SQLDriver().Renderer(),
Args: ng.qc.Args, Args: p.qc.Args,
Dialect: ng.targetDB.SQLDriver().Dialect(), Dialect: p.targetDB.SQLDriver().Dialect(),
} }
return nil return nil
@ -178,36 +178,36 @@ func (ng *engine) prepareNoTabler(ctx context.Context, qm *queryModel) error {
// prepareFromTable builds the "FROM table" fragment. // prepareFromTable builds the "FROM table" fragment.
// //
// When this function returns, ng.rc will be set. // When this function returns, pipeline.rc will be set.
func (ng *engine) prepareFromTable(ctx context.Context, tblSel *ast.TblSelectorNode) (fromClause string, func (p *pipeline) prepareFromTable(ctx context.Context, tblSel *ast.TblSelectorNode) (fromClause string,
fromConn driver.Database, err error, fromConn driver.Database, err error,
) { ) {
handle := tblSel.Handle() handle := tblSel.Handle()
if handle == "" { if handle == "" {
handle = ng.qc.Collection.ActiveHandle() handle = p.qc.Collection.ActiveHandle()
if handle == "" { if handle == "" {
return "", nil, errz.New("query does not specify source, and no active source") return "", nil, errz.New("query does not specify source, and no active source")
} }
} }
src, err := ng.qc.Collection.Get(handle) src, err := p.qc.Collection.Get(handle)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
fromConn, err = ng.qc.DBOpener.Open(ctx, src) fromConn, err = p.qc.DBOpener.Open(ctx, src)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
rndr := fromConn.SQLDriver().Renderer() rndr := fromConn.SQLDriver().Renderer()
ng.rc = &render.Context{ p.rc = &render.Context{
Renderer: rndr, Renderer: rndr,
Args: ng.qc.Args, Args: p.qc.Args,
Dialect: fromConn.SQLDriver().Dialect(), Dialect: fromConn.SQLDriver().Dialect(),
} }
fromClause, err = rndr.FromTable(ng.rc, tblSel) fromClause, err = rndr.FromTable(p.rc, tblSel)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
@ -215,51 +215,92 @@ func (ng *engine) prepareFromTable(ctx context.Context, tblSel *ast.TblSelectorN
return fromClause, fromConn, nil return fromClause, fromConn, nil
} }
// joinClause models the SQL "JOIN" construct.
type joinClause struct {
leftTbl *ast.TblSelectorNode
joins []*ast.JoinNode
}
// tables returns a new slice containing all referenced tables.
func (jc *joinClause) tables() []*ast.TblSelectorNode {
tbls := make([]*ast.TblSelectorNode, len(jc.joins)+1)
tbls[0] = jc.leftTbl
for i := range jc.joins {
tbls[i+1] = jc.joins[i].Table()
}
return tbls
}
// handles returns the set of (non-empty) handles from the tables,
// without any duplicates.
func (jc *joinClause) handles() []string {
handles := make([]string, len(jc.joins)+1)
handles[0] = jc.leftTbl.Handle()
for i := 0; i < len(jc.joins); i++ {
handles[i+1] = jc.joins[i].Table().Handle()
}
handles = lo.Uniq(handles)
handles = lo.Without(handles, "")
return handles
}
// isSingleSource returns true if the joins refer to the same handle.
func (jc *joinClause) isSingleSource() bool {
leftHandle := jc.leftTbl.Handle()
for _, join := range jc.joins {
joinHandle := join.Table().Handle()
if joinHandle == "" {
continue
}
if joinHandle != leftHandle {
return false
}
}
return true
}
// prepareFromJoin builds the "JOIN" clause. // prepareFromJoin builds the "JOIN" clause.
// //
// When this function returns, ng.rc will be set. // When this function returns, pipeline.rc will be set.
func (ng *engine) prepareFromJoin(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string, func (p *pipeline) prepareFromJoin(ctx context.Context, jc *joinClause) (fromClause string,
fromConn driver.Database, err error, fromConn driver.Database, err error,
) { ) {
if fnJoin.LeftTbl() == nil || fnJoin.LeftTbl().TblName() == "" { if jc.isSingleSource() {
return "", nil, errz.Errorf("JOIN is missing left table reference") return p.joinSingleSource(ctx, jc)
} }
if fnJoin.RightTbl() == nil || fnJoin.RightTbl().TblName() == "" { return p.joinCrossSource(ctx, jc)
return "", nil, errz.Errorf("JOIN is missing right table reference")
}
if fnJoin.LeftTbl().Handle() != fnJoin.RightTbl().Handle() {
return ng.joinCrossSource(ctx, fnJoin)
}
return ng.joinSingleSource(ctx, fnJoin)
} }
// joinSingleSource sets up a join against a single source. // joinSingleSource sets up a join against a single source.
// //
// On return, ng.rc will be set. // On return, pipeline.rc will be set.
func (ng *engine) joinSingleSource(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string, func (p *pipeline) joinSingleSource(ctx context.Context, jc *joinClause) (fromClause string,
fromDB driver.Database, err error, fromDB driver.Database, err error,
) { ) {
src, err := ng.qc.Collection.Get(fnJoin.LeftTbl().Handle()) src, err := p.qc.Collection.Get(jc.leftTbl.Handle())
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
fromDB, err = ng.qc.DBOpener.Open(ctx, src) fromDB, err = p.qc.DBOpener.Open(ctx, src)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
rndr := fromDB.SQLDriver().Renderer() rndr := fromDB.SQLDriver().Renderer()
ng.rc = &render.Context{ p.rc = &render.Context{
Renderer: rndr, Renderer: rndr,
Args: ng.qc.Args, Args: p.qc.Args,
Dialect: fromDB.SQLDriver().Dialect(), Dialect: fromDB.SQLDriver().Dialect(),
} }
fromClause, err = rndr.Join(ng.rc, fnJoin) fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
@ -270,65 +311,67 @@ func (ng *engine) joinSingleSource(ctx context.Context, fnJoin *ast.JoinNode) (f
// joinCrossSource returns a FROM clause that forms part of // joinCrossSource returns a FROM clause that forms part of
// the SQL SELECT statement against fromDB. // the SQL SELECT statement against fromDB.
// //
// On return, ng.rc will be set. // On return, pipeline.rc will be set.
func (ng *engine) joinCrossSource(ctx context.Context, fnJoin *ast.JoinNode) (fromClause string, fromDB driver.Database, func (p *pipeline) joinCrossSource(ctx context.Context, jc *joinClause) (fromClause string,
err error, fromDB driver.Database, err error,
) { ) {
leftTblName, rightTblName := fnJoin.LeftTbl().TblName(), fnJoin.RightTbl().TblName() // FIXME: finish tidying up
if leftTblName == rightTblName {
return "", nil, errz.Errorf("JOIN tables must have distinct names (or use aliases): duplicate tbl name {%s}",
fnJoin.LeftTbl().TblName())
}
leftSrc, err := ng.qc.Collection.Get(fnJoin.LeftTbl().Handle()) handles := jc.handles()
if err != nil { srcs := make([]*source.Source, 0, len(handles))
return "", nil, err for _, handle := range handles {
} var src *source.Source
if src, err = p.qc.Collection.Get(handle); err != nil {
rightSrc, err := ng.qc.Collection.Get(fnJoin.RightTbl().Handle()) return "", nil, err
if err != nil { }
return "", nil, err srcs = append(srcs, src)
} }
// Open the join db // Open the join db
joinDB, err := ng.qc.JoinDBOpener.OpenJoin(ctx, leftSrc, rightSrc) joinDB, err := p.qc.JoinDBOpener.OpenJoin(ctx, srcs...)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
rndr := joinDB.SQLDriver().Renderer() rndr := joinDB.SQLDriver().Renderer()
ng.rc = &render.Context{ p.rc = &render.Context{
Renderer: rndr, Renderer: rndr,
Args: ng.qc.Args, Args: p.qc.Args,
Dialect: joinDB.SQLDriver().Dialect(), Dialect: joinDB.SQLDriver().Dialect(),
} }
leftDB, err := ng.qc.DBOpener.Open(ctx, leftSrc) leftHandle := jc.leftTbl.Handle()
if err != nil { // TODO: verify not empty
return "", nil, err
} tbls := jc.tables()
leftCopyTask := &joinCopyTask{ for _, tbl := range tbls {
fromDB: leftDB, tbl := tbl
fromTblName: leftTblName, handle := tbl.Handle()
toDB: joinDB, if handle == "" {
toTblName: leftTblName, handle = leftHandle
}
var src *source.Source
if src, err = p.qc.Collection.Get(handle); err != nil {
return "", nil, err
}
var db driver.Database
if db, err = p.qc.DBOpener.Open(ctx, src); err != nil {
return "", nil, err
}
task := &joinCopyTask{
fromDB: db,
fromTblName: tbl.TblName(),
toDB: joinDB,
toTblName: tbl.TblAliasOrName(),
}
tbl.SyncTblNameAlias()
p.tasks = append(p.tasks, task)
} }
rightDB, err := ng.qc.DBOpener.Open(ctx, rightSrc) fromClause, err = rndr.Join(p.rc, jc.leftTbl, jc.joins)
if err != nil {
return "", nil, err
}
rightCopyTask := &joinCopyTask{
fromDB: rightDB,
fromTblName: rightTblName,
toDB: joinDB,
toTblName: rightTblName,
}
ng.tasks = append(ng.tasks, leftCopyTask)
ng.tasks = append(ng.tasks, rightCopyTask)
fromClause, err = rndr.Join(ng.rc, fnJoin)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
@ -395,6 +438,8 @@ func execCopyTable(ctx context.Context, fromDB driver.Database, fromTblName stri
if err != nil { if err != nil {
return errz.Wrapf(err, "insert %s.%s failed", destDB.Source().Handle, destTblName) return errz.Wrapf(err, "insert %s.%s failed", destDB.Source().Handle, destTblName)
} }
log.Debug("Copied %d rows to %s.%s", affected, destDB.Source().Handle, destTblName) log.Debug("Copied rows to dest", lga.Count, affected,
lga.From, fmt.Sprintf("%s.%s", fromDB.Source().Handle, fromTblName),
lga.To, fmt.Sprintf("%s.%s", destDB.Source().Handle, destTblName))
return nil return nil
} }

View File

@ -4,83 +4,77 @@ import (
"context" "context"
"github.com/neilotoole/sq/libsq/ast/render" "github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/ast"
"github.com/neilotoole/sq/libsq/core/errz"
) )
// prepare prepares the engine to execute queryModel. // prepare prepares the pipeline to execute queryModel.
// When this method returns, targetDB and targetSQL will be set, // When this method returns, targetDB and targetSQL will be set,
// as will any tasks (which may be empty). The tasks must be executed // as will any tasks (which may be empty). The tasks must be executed
// against targetDB before targetSQL is executed (the engine.execute // against targetDB before targetSQL is executed (the pipeline.execute
// method does this work). // method does this work).
func (ng *engine) prepare(ctx context.Context, qm *queryModel) error { func (p *pipeline) prepare(ctx context.Context, qm *queryModel) error {
var ( var (
err error err error
frags = &render.Fragments{} frags = &render.Fragments{}
) )
// After this switch, ng.rc will be set. // After this switch, p.rc will be set.
switch node := qm.Table.(type) { switch {
case nil: case qm.Table == nil:
if err = ng.prepareNoTabler(ctx, qm); err != nil { if err = p.prepareNoTable(ctx, qm); err != nil {
return err return err
} }
case *ast.TblSelectorNode: case len(qm.Joins) > 0:
if frags.From, ng.targetDB, err = ng.prepareFromTable(ctx, node); err != nil { jc := &joinClause{leftTbl: qm.Table, joins: qm.Joins}
return err if frags.From, p.targetDB, err = p.prepareFromJoin(ctx, jc); err != nil {
}
case *ast.JoinNode:
if frags.From, ng.targetDB, err = ng.prepareFromJoin(ctx, node); err != nil {
return err return err
} }
default: default:
// Should never happen if frags.From, p.targetDB, err = p.prepareFromTable(ctx, qm.Table); err != nil {
return errz.Errorf("unknown ast.Tabler %T: %s", node, node) return err
}
} }
rndr := ng.rc.Renderer rndr := p.rc.Renderer
if frags.Columns, err = rndr.SelectCols(p.rc, qm.Cols); err != nil {
if frags.Columns, err = rndr.SelectCols(ng.rc, qm.Cols); err != nil {
return err return err
} }
if qm.Distinct != nil { if qm.Distinct != nil {
if frags.Distinct, err = rndr.Distinct(ng.rc, qm.Distinct); err != nil { if frags.Distinct, err = rndr.Distinct(p.rc, qm.Distinct); err != nil {
return err return err
} }
} }
if qm.Range != nil { if qm.Range != nil {
if frags.Range, err = rndr.Range(ng.rc, qm.Range); err != nil { if frags.Range, err = rndr.Range(p.rc, qm.Range); err != nil {
return err return err
} }
} }
if qm.Where != nil { if qm.Where != nil {
if frags.Where, err = rndr.Where(ng.rc, qm.Where); err != nil { if frags.Where, err = rndr.Where(p.rc, qm.Where); err != nil {
return err return err
} }
} }
if qm.OrderBy != nil { if qm.OrderBy != nil {
if frags.OrderBy, err = rndr.OrderBy(ng.rc, qm.OrderBy); err != nil { if frags.OrderBy, err = rndr.OrderBy(p.rc, qm.OrderBy); err != nil {
return err return err
} }
} }
if qm.GroupBy != nil { if qm.GroupBy != nil {
if frags.GroupBy, err = rndr.GroupBy(ng.rc, qm.GroupBy); err != nil { if frags.GroupBy, err = rndr.GroupBy(p.rc, qm.GroupBy); err != nil {
return err return err
} }
} }
if rndr.PreRender != nil { if rndr.PreRender != nil {
if err = rndr.PreRender(ng.rc, frags); err != nil { if err = rndr.PreRender(p.rc, frags); err != nil {
return err return err
} }
} }
ng.targetSQL, err = rndr.Render(ng.rc, frags) p.targetSQL, err = rndr.Render(p.rc, frags)
return err return err
} }

View File

@ -1,8 +1,18 @@
package libsq_test package libsq_test
import ( import (
"fmt"
"testing" "testing"
"github.com/neilotoole/sq/drivers/postgres"
"github.com/neilotoole/sq/drivers/sqlite3"
"github.com/neilotoole/sq/drivers/sqlserver"
"github.com/neilotoole/sq/libsq/source"
"github.com/neilotoole/sq/libsq/core/jointype"
"github.com/samber/lo"
"github.com/neilotoole/sq/testh/tutil" "github.com/neilotoole/sq/testh/tutil"
"github.com/neilotoole/sq/testh/sakila" "github.com/neilotoole/sq/testh/sakila"
@ -12,14 +22,334 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
func TestQuery_join_args(t *testing.T) {
testCases := []queryTestCase{
{
name: "error/missing-predicate",
in: `@sakila | .actor | join(.film_actor)`,
wantErr: true,
repeatReplace: predicateJoinNames,
},
{
name: "error/unwanted-predicate",
in: `@sakila | .actor | join(.film_actor)`,
wantErr: true,
repeatReplace: noPredicateJoinNames,
},
{
name: "error/too-many-args",
in: `@sakila | .actor | join(.film_actor, .actor_id, .first_name)`,
wantErr: true,
repeatReplace: jointype.AllValues(),
},
{
name: "error/no-args",
in: `@sakila | .store | join()`,
wantErr: true,
repeatReplace: jointype.AllValues(),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}
//nolint:exhaustive,lll
func TestQuery_join_inner(t *testing.T) {
testCases := []queryTestCase{
{
name: "n1/equals-no-alias",
in: `@sakila | .store | join(.address, .store.address_id == .address.address_id)`,
wantRecCount: 2,
},
{
name: "n1/equals-with-alias",
in: `@sakila | .store:s | join(.address:a, .s.address_id == .a.address_id)`,
wantSQL: `SELECT * FROM "store" AS "s" INNER JOIN "address" AS "a" ON "s"."address_id" = "a"."address_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` AS `s` INNER JOIN `address` AS `a` ON `s`.`address_id` = `a`.`address_id`"},
wantRecCount: 2,
repeatReplace: innerJoins,
},
{
name: "n2/equals-with-alias/unqualified-cols",
in: `@sakila | .actor:a | join(.film_actor:fa, .a.actor_id == .fa.actor_id) | join(.film:f, .fa.film_id == .f.film_id) | .first_name, .last_name, .title`,
wantSQL: `SELECT "first_name", "last_name", "title" FROM "actor" AS "a" INNER JOIN "film_actor" AS "fa" ON "a"."actor_id" = "fa"."actor_id" INNER JOIN "film" AS "f" ON "fa"."film_id" = "f"."film_id"`,
override: driverMap{mysql.Type: "SELECT `first_name`, `last_name`, `title` FROM `actor` AS `a` INNER JOIN `film_actor` AS `fa` ON `a`.`actor_id` = `fa`.`actor_id` INNER JOIN `film` AS `f` ON `fa`.`film_id` = `f`.`film_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: innerJoins,
},
{
name: "n2/equals-with-alias/qualified-cols",
in: `@sakila | .actor:a | join(.film_actor:fa, .a.actor_id == .fa.actor_id) | join(.film:f, .fa.film_id == .f.film_id) | .a.first_name, .a.last_name, .f.title`,
wantSQL: `SELECT "a"."first_name", "a"."last_name", "f"."title" FROM "actor" AS "a" INNER JOIN "film_actor" AS "fa" ON "a"."actor_id" = "fa"."actor_id" INNER JOIN "film" AS "f" ON "fa"."film_id" = "f"."film_id"`,
override: driverMap{mysql.Type: "SELECT `a`.`first_name`, `a`.`last_name`, `f`.`title` FROM `actor` AS `a` INNER JOIN `film_actor` AS `fa` ON `a`.`actor_id` = `fa`.`actor_id` INNER JOIN `film` AS `f` ON `fa`.`film_id` = `f`.`film_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: innerJoins,
},
{
name: "n1/single-selector-no-alias",
in: `@sakila | .store | join(.address, .address_id)`,
wantSQL: `SELECT * FROM "store" INNER JOIN "address" ON "store"."address_id" = "address"."address_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` INNER JOIN `address` ON `store`.`address_id` = `address`.`address_id`"},
wantRecCount: 2,
repeatReplace: innerJoins,
},
{
name: "n1/table-handle-single-selector-no-alias",
in: `@sakila.store | join(.address, .address_id)`,
wantSQL: `SELECT * FROM "store" INNER JOIN "address" ON "store"."address_id" = "address"."address_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` INNER JOIN `address` ON `store`.`address_id` = `address`.`address_id`"},
wantRecCount: 2,
repeatReplace: innerJoins,
},
{
name: "n1/single-selector-with-alias",
in: `@sakila | .store:s | join(.address:a, .address_id)`,
wantSQL: `SELECT * FROM "store" AS "s" INNER JOIN "address" AS "a" ON "s"."address_id" = "a"."address_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` AS `s` INNER JOIN `address` AS `a` ON `s`.`address_id` = `a`.`address_id`"},
wantRecCount: 2,
repeatReplace: innerJoins,
},
{
name: "cross-join/n1/no-constraint",
in: `@sakila | .store | cross_join(.address)`,
wantSQL: `SELECT * FROM "store" CROSS JOIN "address"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` CROSS JOIN `address`"},
wantRecCount: 1206,
repeatReplace: []string{string(jointype.Cross), jointype.CrossAlias},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}
//nolint:lll
func TestQuery_join_multi_source(t *testing.T) {
testCases := []queryTestCase{
{
name: "n1/equals-no-alias",
in: fmt.Sprintf(
`@sakila | .store | join(%s.address, .store.address_id == .address.address_id)`,
sakila.SL3,
),
wantSQL: `SELECT * FROM "store" INNER JOIN "address" ON "store"."address_id" = "address"."address_id"`,
wantRecCount: 2,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinStoreAddress...),
},
},
{
name: "n1/table-handle-equals-no-alias",
in: fmt.Sprintf(
`@sakila.store | join(%s.address, .store.address_id == .address.address_id)`,
sakila.SL3,
),
wantSQL: `SELECT * FROM "store" INNER JOIN "address" ON "store"."address_id" = "address"."address_id"`,
wantRecCount: 2,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinStoreAddress...),
},
},
{
name: "n1/equals-with-alias",
in: fmt.Sprintf(
`@sakila | .store:s | join(%s.address:a, .s.address_id == .a.address_id)`,
sakila.Pg,
),
wantRecCount: 2,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinStoreAddress...),
},
},
{
name: "n2/two-sources",
in: fmt.Sprintf(
`@sakila | .actor | join(%s.film_actor, .actor_id) | join(.film, .film_id) | .first_name, .last_name, .title`,
sakila.Pg,
),
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames("first_name", "last_name", "title"),
},
},
{
name: "n2/three-sources-no-alias-no-col-alias",
in: fmt.Sprintf(
`@sakila | .actor | join(%s.film_actor, .actor_id) | join(%s.film, .film_id) | .first_name, .last_name, .title`,
sakila.Pg,
sakila.My,
),
wantSQL: `SELECT "first_name", "last_name", "title" FROM "actor" INNER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id" INNER JOIN "film" ON "film_actor"."film_id" = "film"."film_id"`,
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames("first_name", "last_name", "title"),
},
},
{
name: "n2/three-sources-no-alias-all-cols",
in: fmt.Sprintf(
`@sakila | .actor | join(%s.film_actor, .actor_id) | join(%s.film, .film_id)`,
sakila.Pg,
sakila.My,
),
wantSQL: `SELECT * FROM "actor" INNER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id" INNER JOIN "film" ON "film_actor"."film_id" = "film"."film_id"`,
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: innerJoins,
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActorFilm...),
},
},
{
name: "n2/equals-with-alias/unqualified-cols",
in: fmt.Sprintf(
`@sakila | .actor:a | join(%s.film_actor:fa, .a.actor_id == .fa.actor_id) | join(%s.film:f, .fa.film_id == .f.film_id) | .first_name, .last_name, .title`,
sakila.Pg,
sakila.My,
),
wantRecCount: sakila.TblFilmActorCount,
sinkFns: []SinkTestFunc{
assertSinkColNames("first_name", "last_name", "title"),
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}
// TestQuery_join_others tests the join types other than INNER JOIN.
//
//nolint:exhaustive,lll
func TestQuery_join_others(t *testing.T) {
testCases := []queryTestCase{
{
name: "left_join",
in: `@sakila | .actor | left_join(.film_actor, .actor_id)`,
wantSQL: `SELECT * FROM "actor" LEFT JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` LEFT JOIN `film_actor` ON `actor`.`actor_id` = `film_actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: []string{string(jointype.Left), jointype.LeftAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
{
name: "left_outer_join",
in: `@sakila | .actor | left_outer_join(.film_actor, .actor_id)`,
wantSQL: `SELECT * FROM "actor" LEFT OUTER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` LEFT OUTER JOIN `film_actor` ON `actor`.`actor_id` = `film_actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: []string{string(jointype.LeftOuter), jointype.LeftOuterAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
{
name: "right_join",
in: `@sakila | .actor | right_join(.film_actor, .actor_id)`,
wantSQL: `SELECT * FROM "actor" RIGHT JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` RIGHT JOIN `film_actor` ON `actor`.`actor_id` = `film_actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: []string{string(jointype.Right), jointype.RightAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
{
name: "right_outer_join",
in: `@sakila | .actor | right_outer_join(.film_actor, .actor_id)`,
wantSQL: `SELECT * FROM "actor" RIGHT OUTER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` RIGHT OUTER JOIN `film_actor` ON `actor`.`actor_id` = `film_actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: []string{string(jointype.RightOuter), jointype.RightOuterAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
{
name: "full_outer_join",
in: `@sakila | .actor | full_outer_join(.film_actor, .actor_id)`,
wantSQL: `SELECT * FROM "actor" FULL OUTER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
// Note that MySQL doesn't support full outer join.
onlyFor: []source.DriverType{sqlite3.Type, postgres.Type, sqlserver.Type},
wantRecCount: sakila.TblFilmActorCount,
repeatReplace: []string{string(jointype.FullOuter), jointype.FullOuterAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
{
name: "full_outer_join/error-mysql",
in: `@sakila | .actor | full_outer_join(.film_actor, .actor_id)`,
// Note that MySQL doesn't support full outer join.
onlyFor: []source.DriverType{mysql.Type},
wantErr: true,
repeatReplace: []string{string(jointype.FullOuter), jointype.FullOuterAlias},
},
{
name: "cross/store-address",
in: `@sakila | .store | cross_join(.address)`,
wantSQL: `SELECT * FROM "store" CROSS JOIN "address"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` CROSS JOIN `address`"},
wantRecCount: 1206,
repeatReplace: []string{string(jointype.Cross), jointype.CrossAlias},
},
{
name: "cross/store-staff",
in: `@sakila | .store | cross_join(.staff)`,
wantSQL: `SELECT * FROM "store" CROSS JOIN "staff"`,
override: driverMap{mysql.Type: "SELECT * FROM `store` CROSS JOIN `staff`"},
wantRecCount: 4,
repeatReplace: []string{string(jointype.Cross), jointype.CrossAlias},
},
{
name: "cross/actor-film_actor/no-constraint",
in: `@sakila | .actor | cross_join(.film_actor) | .[0:10]`,
wantRecCount: 10,
repeatReplace: []string{string(jointype.Cross), jointype.CrossAlias},
sinkFns: []SinkTestFunc{
assertSinkColNames(colsJoinActorFilmActor...),
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
}
}
// TestQuery_table_alias is tested with the joins, because table aliases
// are primarily for use with join.
//
//nolint:exhaustive //nolint:exhaustive
func TestQuery_table_alias(t *testing.T) { func TestQuery_table_alias(t *testing.T) {
testCases := []queryTestCase{ testCases := []queryTestCase{
{ {
name: "table-alias", name: "table-alias",
in: `@sakila | .actor:a | .a.first_name`, in: `@sakila | .actor:a | .a.first_name`,
wantSQL: `SELECT "a"."first_name" FROM "actor" "a"`, wantSQL: `SELECT "a"."first_name" FROM "actor" AS "a"`,
override: driverMap{mysql.Type: "SELECT `a`.`first_name` FROM `actor` `a`"}, override: driverMap{mysql.Type: "SELECT `a`.`first_name` FROM `actor` AS `a`"},
wantRecCount: sakila.TblActorCount, wantRecCount: sakila.TblActorCount,
sinkFns: []SinkTestFunc{ sinkFns: []SinkTestFunc{
assertSinkColName(0, "first_name"), assertSinkColName(0, "first_name"),
@ -28,8 +358,8 @@ func TestQuery_table_alias(t *testing.T) {
{ {
name: "table-whitespace-alias", name: "table-whitespace-alias",
in: `@sakila | .actor:"oy vey" | ."oy vey".first_name`, in: `@sakila | .actor:"oy vey" | ."oy vey".first_name`,
wantSQL: `SELECT "oy vey"."first_name" FROM "actor" "oy vey"`, wantSQL: `SELECT "oy vey"."first_name" FROM "actor" AS "oy vey"`,
override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` FROM `actor` `oy vey`"}, override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` FROM `actor` AS `oy vey`"},
wantRecCount: sakila.TblActorCount, wantRecCount: sakila.TblActorCount,
sinkFns: []SinkTestFunc{ sinkFns: []SinkTestFunc{
assertSinkColName(0, "first_name"), assertSinkColName(0, "first_name"),
@ -38,8 +368,8 @@ func TestQuery_table_alias(t *testing.T) {
{ {
name: "table-whitespace-alias-with-col-alias", name: "table-whitespace-alias-with-col-alias",
in: `@sakila | .actor:"oy vey" | ."oy vey".first_name:given_name`, in: `@sakila | .actor:"oy vey" | ."oy vey".first_name:given_name`,
wantSQL: `SELECT "oy vey"."first_name" AS "given_name" FROM "actor" "oy vey"`, wantSQL: `SELECT "oy vey"."first_name" AS "given_name" FROM "actor" AS "oy vey"`,
override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` AS `given_name` FROM `actor` `oy vey`"}, override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` AS `given_name` FROM `actor` AS `oy vey`"},
wantRecCount: sakila.TblActorCount, wantRecCount: sakila.TblActorCount,
sinkFns: []SinkTestFunc{ sinkFns: []SinkTestFunc{
assertSinkColName(0, "given_name"), assertSinkColName(0, "given_name"),
@ -48,8 +378,8 @@ func TestQuery_table_alias(t *testing.T) {
{ {
name: "table-whitespace-alias-with-col-whitespace-alias", name: "table-whitespace-alias-with-col-whitespace-alias",
in: `@sakila | .actor:"oy vey" | ."oy vey".first_name:"oy vey"`, in: `@sakila | .actor:"oy vey" | ."oy vey".first_name:"oy vey"`,
wantSQL: `SELECT "oy vey"."first_name" AS "oy vey" FROM "actor" "oy vey"`, wantSQL: `SELECT "oy vey"."first_name" AS "oy vey" FROM "actor" AS "oy vey"`,
override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` AS `oy vey` FROM `actor` `oy vey`"}, override: driverMap{mysql.Type: "SELECT `oy vey`.`first_name` AS `oy vey` FROM `actor` AS `oy vey`"},
wantRecCount: sakila.TblActorCount, wantRecCount: sakila.TblActorCount,
sinkFns: []SinkTestFunc{ sinkFns: []SinkTestFunc{
assertSinkColName(0, "oy vey"), assertSinkColName(0, "oy vey"),
@ -65,36 +395,59 @@ func TestQuery_table_alias(t *testing.T) {
} }
} }
//nolint:exhaustive,lll var (
func TestQuery_join(t *testing.T) { noPredicateJoinNames = []string{
testCases := []queryTestCase{ string(jointype.Cross),
{ jointype.CrossAlias,
name: "join/single-selector",
in: `@sakila | .actor, .film_actor | join(.actor_id)`,
wantSQL: `SELECT * FROM "actor" INNER JOIN "film_actor" ON "actor"."actor_id" = "film_actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` INNER JOIN `film_actor` ON `actor`.`actor_id` = `film_actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
},
{
name: "join/fq-table-cols-equal",
in: `@sakila | .actor, .film_actor | join(.film_actor.actor_id == .actor.actor_id)`,
wantSQL: `SELECT * FROM "actor" INNER JOIN "film_actor" ON "film_actor"."actor_id" = "actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` INNER JOIN `film_actor` ON `film_actor`.`actor_id` = `actor`.`actor_id`"},
wantRecCount: sakila.TblFilmActorCount,
},
{
name: "join/fq-table-cols-equal-whitespace",
in: `@sakila | .actor, ."film actor" | join(."film actor".actor_id == .actor.actor_id)`,
wantSQL: `SELECT * FROM "actor" INNER JOIN "film actor" ON "film actor"."actor_id" = "actor"."actor_id"`,
override: driverMap{mysql.Type: "SELECT * FROM `actor` INNER JOIN `film actor` ON `film actor`.`actor_id` = `actor`.`actor_id`"},
skipExec: true,
},
} }
innerJoins = []string{
for _, tc := range testCases { jointype.JoinAlias,
tc := tc string(jointype.Inner),
t.Run(tc.name, func(t *testing.T) {
execQueryTestCase(t, tc)
})
} }
} predicateJoinNames = lo.Without(jointype.AllValues(), noPredicateJoinNames...)
colsJoinActorFilmActor = []string{
"actor_id",
"first_name",
"last_name",
"last_update",
"actor_id_1",
"film_id",
"last_update_1",
}
colsJoinActorFilmActorFilm = []string{
"actor_id",
"first_name",
"last_name",
"last_update",
"actor_id_1",
"film_id",
"last_update_1",
"film_id_1",
"title",
"description",
"release_year",
"language_id",
"original_language_id",
"rental_duration",
"rental_rate",
"length",
"replacement_cost",
"rating",
"special_features",
"last_update_2",
}
colsJoinStoreAddress = []string{
"store_id",
"manager_staff_id",
"address_id",
"last_update",
"address_id_1",
"address",
"address2",
"district",
"city_id",
"postal_code",
"phone",
"last_update_1",
}
)

View File

@ -7,13 +7,13 @@ import (
"github.com/neilotoole/sq/libsq/ast" "github.com/neilotoole/sq/libsq/ast"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"golang.org/x/exp/slog"
) )
// queryModel is a model of an SLQ query built from the AST. // queryModel is a model of an SLQ query built from the AST.
type queryModel struct { type queryModel struct {
AST *ast.AST AST *ast.AST
Table ast.Tabler Table *ast.TblSelectorNode
Joins []*ast.JoinNode
Cols []ast.ResultColumn Cols []ast.ResultColumn
Range *ast.RowRangeNode Range *ast.RowRangeNode
Where *ast.WhereNode Where *ast.WhereNode
@ -27,35 +27,36 @@ func (qm *queryModel) String() string {
} }
// buildQueryModel creates a queryModel instance from the AST. // buildQueryModel creates a queryModel instance from the AST.
func buildQueryModel(log *slog.Logger, a *ast.AST) (*queryModel, error) { func buildQueryModel(qc *QueryContext, a *ast.AST) (*queryModel, error) {
if len(a.Segments()) == 0 { if len(a.Segments()) == 0 {
return nil, errz.Errorf("query model error: query does not have enough segments") return nil, errz.Errorf("invalid query: no segments")
} }
insp := ast.NewInspector(a) var (
ok bool
err error
insp = ast.NewInspector(a)
qm = &queryModel{AST: a}
)
var tabler ast.Tabler qm.Table = insp.FindFirstTableSelector()
var ok bool if qm.Table != nil {
tablerSeg, err := insp.FindFinalTablerSegment() // If the table selector doesn't specify a handle, set the
if err != nil { // table's handle to the active handle.
log.Debug("No Tabler segment.") if qm.Table.Handle() == "" {
} // It's possible that there's no active source: this
// is effectively a no-op in that case.
if tablerSeg != nil { qm.Table.SetHandle(qc.Collection.ActiveHandle())
if len(tablerSeg.Children()) != 1 {
return nil, errz.Errorf(
"the final selectable segment must have exactly one selectable element, but found %d elements",
len(tablerSeg.Children()))
}
if tabler, ok = tablerSeg.Children()[0].(ast.Tabler); !ok {
return nil, errz.Errorf(
"the final selectable segment must have exactly one selectable element, but found element %T(%s)",
tablerSeg.Children()[0], tablerSeg.Children()[0].Text())
} }
} }
qm := &queryModel{AST: a, Table: tabler} if qm.Joins, err = insp.FindJoins(); err != nil {
return nil, err
}
if len(qm.Joins) > 0 && qm.Table == nil {
return nil, errz.Errorf("invalid query: join doesn't have a preceding table selector")
}
if qm.Range, err = insp.FindRowRangeNode(); err != nil { if qm.Range, err = insp.FindRowRangeNode(); err != nil {
return nil, err return nil, err

View File

@ -4,6 +4,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/neilotoole/sq/testh/tutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/neilotoole/sq/libsq" "github.com/neilotoole/sq/libsq"
@ -30,7 +32,7 @@ type queryTestCase struct {
name string name string
// skip indicates the test should be skipped. Useful for test cases // skip indicates the test should be skipped. Useful for test cases
// that we wantSQL to implement in the future. // that we want to implement in the future.
skip bool skip bool
// in is the SLQ input. The "@sakila" handle is replaced // in is the SLQ input. The "@sakila" handle is replaced
@ -44,7 +46,8 @@ type queryTestCase struct {
// wantErr indicates that an error is expected // wantErr indicates that an error is expected
wantErr bool wantErr bool
// wantSQL is the wanted SQL // wantSQL is the desired SQL. If empty, the returned SQL is
// not tested (but is still executed).
wantSQL string wantSQL string
// override allows an alternative "wantSQL" for a specific driver type. // override allows an alternative "wantSQL" for a specific driver type.
@ -68,11 +71,26 @@ type queryTestCase struct {
// sinkTest, if non-nil, is executed against the sink returned // sinkTest, if non-nil, is executed against the sink returned
// from the query execution. // from the query execution.
sinkFns []SinkTestFunc sinkFns []SinkTestFunc
// repeatReplace, when non-empty, instructs the test runner to repeat
// the test, replacing in the input string all occurrences of the first
// slice element with each subsequent element. For example, given:
//
// in: ".actor | join(.address, .address_id):
// repeatReplace: []string{"join", "inner_join"}
//
// The test will run once using "join" (the original query) and then another
// time with ".actor | inner_join(.address, .address_id)".
//
// Thus the field must be empty, or have at least two elements.
repeatReplace []string
} }
// SinkTestFunc is a function that tests a sink. // SinkTestFunc is a function that tests a sink.
type SinkTestFunc func(t testing.TB, sink *testh.RecordSink) type SinkTestFunc func(t testing.TB, sink *testh.RecordSink)
// execQueryTestCase is called by test functions to execute
// a queryTestCase.
func execQueryTestCase(t *testing.T, tc queryTestCase) { func execQueryTestCase(t *testing.T, tc queryTestCase) {
if tc.skip { if tc.skip {
t.Skip() t.Skip()
@ -80,8 +98,52 @@ func execQueryTestCase(t *testing.T, tc queryTestCase) {
t.Helper() t.Helper()
coll := testh.New(t).NewCollection(sakila.SQLLatest()...) switch len(tc.repeatReplace) {
case 0:
doExecQueryTestCase(t, tc)
return
case 1:
t.Fatalf("queryTestCase.repeatReplace must be empty or have at least two elements")
return
default:
}
subTests := make([]queryTestCase, len(tc.repeatReplace))
for i := range tc.repeatReplace {
subTests[i] = tc
subTests[i].name += "/" + tutil.Name(tc.repeatReplace[i])
if i == 0 {
// No need for replacement on first item, it's the original.
continue
}
subTests[i].in = strings.ReplaceAll(
subTests[i].in,
tc.repeatReplace[0],
tc.repeatReplace[i],
)
}
for _, st := range subTests {
st := st
t.Run(st.name, func(t *testing.T) {
doExecQueryTestCase(t, st)
})
}
}
// doExecQueryTestCase is called by execQueryTestCase to
// execute a queryTestCase. This function should not be called
// directly by test functions. The query is executed for each
// of the sources in sakila.SQLLatest. To do so, the first
// occurrence of the string "@sakila." is replaced with the
// actual handle of each source. E.g:
//
// "@sakila | .actor" --> "@sakila_pg12 | .actor"
func doExecQueryTestCase(t *testing.T, tc queryTestCase) {
t.Helper()
coll := testh.New(t).NewCollection(sakila.SQLLatest()...)
for _, src := range coll.Sources() { for _, src := range coll.Sources() {
src := src src := src
@ -95,7 +157,7 @@ func execQueryTestCase(t *testing.T, tc queryTestCase) {
} }
in := strings.Replace(tc.in, "@sakila", src.Handle, 1) in := strings.Replace(tc.in, "@sakila", src.Handle, 1)
t.Log(in) t.Logf("QUERY:\n\n%s\n\n", in)
want := tc.wantSQL want := tc.wantSQL
if overrideWant, ok := tc.override[src.Type]; ok { if overrideWant, ok := tc.override[src.Type]; ok {
want = overrideWant want = overrideWant
@ -117,13 +179,17 @@ func execQueryTestCase(t *testing.T, tc queryTestCase) {
gotSQL, gotErr := libsq.SLQ2SQL(th.Context, qc, in) gotSQL, gotErr := libsq.SLQ2SQL(th.Context, qc, in)
if tc.wantErr { if tc.wantErr {
require.Error(t, gotErr) assert.Error(t, gotErr)
t.Logf("ERROR: %v", gotErr)
return return
} }
t.Logf("SQL:\n\n%s\n\n", gotSQL)
require.NoError(t, gotErr) require.NoError(t, gotErr)
require.Equal(t, want, gotSQL)
t.Log(gotSQL) if want != "" {
require.Equal(t, want, gotSQL)
}
if tc.skipExec { if tc.skipExec {
return return
@ -157,3 +223,11 @@ func assertSinkColName(colIndex int, name string) SinkTestFunc { //nolint:unpara
assert.Equal(t, name, sink.RecMeta[colIndex].Name(), "column %d", colIndex) assert.Equal(t, name, sink.RecMeta[colIndex].Name(), "column %d", colIndex)
} }
} }
// assertSinkColNames returns a SinkTestFunc that matches col names.
func assertSinkColNames(names ...string) SinkTestFunc {
return func(t testing.TB, sink *testh.RecordSink) {
gotNames := sink.RecMeta.Names()
assert.Equal(t, names, gotNames)
}
}