However, when data sources are very large (several terabytes), loading the entire table only to use a summary or a subset is inefficient. It unnecessarily consumes network bandwidth and RAM.
Although PROC CASUTIL offers simple options (WHERE=, VAR=), there is a much more powerful method for performing complex aggregations or joins directly in the database before the data even reaches CAS: FedSQL Pass-Through.
This tutorial explores two methods for delegating work to the database: Implicit mode and Explicit mode.
Implicit Pass-Through allows you to write standard FedSQL code (similar to ANSI SQL). The CAS Data Connector handles translating your query into the native language of the database (Postgres SQL, T-SQL, PL/SQL, etc.).
Example: Simple Aggregation
Imagine a Postgres table with 2 million rows (orders_large). We want to load into CAS only the sum of sales by order type, filtered by date.
cas mysession;
/* Définition de la connexion à Postgres */
caslib caspgr datasource=(srctype="postgres", database="DCON", server="...", ...) libref=caspgr;
proc fedsql sessref=mysession;
/* Création de la table résultante directement dans la RAM de CAS */
create table caspgr.revenuesummary{options replace=true} as
select order_type, sum(total_sale) as revenue
from caspgr."orders_large" /* Notez les guillemets pour préserver la casse */
where order_date < date'2015-01-01'
group by order_type;
quit;
/* Création de la table résultante directement dans la RAM de CAS */
7
create TABLE caspgr.revenuesummary{options replace=true} as
8
select order_type, sum(total_sale) as revenue
9
from caspgr."orders_large"/* Notez les guillemets pour préserver la casse */
10
where order_date < date'2015-01-01'
11
group BY order_type;
12
QUIT;
What happens behind the scenes:
If the orders_large table is not already loaded into CAS, the engine detects that it is an external table. It translates the query into Postgres SQL, sends it to the server, and loads only the aggregated result (a few rows) into CAS.
Performance Gain:
Classic Method (Full Load + CAS Aggregation): ~38 seconds.
Pass-Through Method (Database Aggregation + Result Load): ~0.32 seconds.
Method 2: Explicit Pass-Through (The "Power User" Method)
Sometimes, you need to use features very specific to your database (JSON functions, geospatial functions, special indexes) that standard FedSQL is not aware of.
SELECT ... FROM CONNECTION TO nom_caslib ( votre_requête_native_ici )
1
SELECT ... FROM CONNECTION TO nom_caslib ( votre_requête_native_ici )
Advanced Example: Parsing JSON in Postgres
Suppose your Postgres table jsonorders contains a JSON type column. You want to extract structured information (customer state, quantity) stored in this JSON and aggregate it before loading it into CAS.
FedSQL doesn't know how to parse JSON natively, but Postgres does.
proc fedsql sessref=mysession;
create table caspgr.StateSalesSummary as
select customerstate, minquantity, maxquantity, sumquantity
from connection to caspgr
(
/* DEBUT DU SQL NATIF POSTGRES */
SELECT customerstate,
min(quantity) as minquantity,
max(quantity) as maxquantity,
sum(quantity) as sumquantity
FROM
(
/* Utilisation des opérateurs JSON Postgres -> et ->> */
SELECT orderinfo -> 'customer' ->> 'state' as customerstate,
cast(orderinfo -> 'items' ->> 'qty' as integer) as quantity
FROM jsonorders
) t
GROUP BY customerstate
/* FIN DU SQL NATIF POSTGRES */
);
quit;
Result: Postgres performs the heavy JSON extraction and aggregation. CAS receives only a small, ready-to-use summary table.
Aviso importante
Los códigos y ejemplos proporcionados en WeAreCAS.eu son con fines educativos. Es imperativo no copiarlos y pegarlos ciegamente en sus entornos de producción. El mejor enfoque es comprender la lógica antes de aplicarla. Recomendamos encarecidamente probar estos scripts en un entorno de prueba (Sandbox/Dev). WeAreCAS no acepta ninguna responsabilidad por cualquier impacto o pérdida de datos en sus sistemas.
SAS y todos los demás nombres de productos o servicios de SAS Institute Inc. son marcas registradas o marcas comerciales de SAS Institute Inc. en los EE. UU. y otros países. ® indica registro en los EE. UU. WeAreCAS es un sitio comunitario independiente y no está afiliado a SAS Institute Inc.
Este sitio utiliza cookies técnicas y analíticas para mejorar su experiencia.
Saber más.