Query from schema
A particularly attractive consequence of keeping all schemas in memory is that we can generate SQL queries based on them.
Here is a quick example to get you started.
import {
BQView,
BQField,
Fragment,
StringIdent,
sql,
fragments,
} from "@datoria/sdk";
import {
analyticsDataset,
dailyEventsTable,
} from "../../core-concepts/tables-and-partitioning.md";
function rewrite(f: BQField): [BQField, Fragment] | null {
// pick `user_id` column as-is
if (f.name === "user_id") {
return [f, sql`${StringIdent(f.name)}`];
// drop event_type column
} else if (f.name === "event_type") {
return null;
// give all other columns an alias
} else {
const newField = { ...f, name: f.name + "_alias" };
return [
newField,
sql`${StringIdent(f.name)} as ${StringIdent(newField.name)}`,
];
}
}
const parts = dailyEventsTable.schema.map(rewrite).filter((x) => x !== null);
const query = sql`
select ${fragments.joinWith(
parts.map(([, f]) => f),
sql`, `,
)}
from ${dailyEventsTable.allPartitions()}
`;
const schema = parts.map(([f]) => f.asViewType());
export const person_event_rewritten_view = BQView({
dataset: analyticsDataset,
name: "person_event_rewritten",
query,
schema,
});
This is advanced functionality (and a contrived example), but hopefully the massive potential of this feature is clear