SAP HANA Machine Learning
In-database machine learning with PAL and APL libraries.
PAL (Predictive Analysis Library)
-- Train a linear regression model
CREATE PROCEDURE TRAIN_REGRESSION()
LANGUAGE SQLSCRIPT AS
BEGIN
CALL _SYS_AFL.PAL_LINEAR_REGRESSION(
TRAIN_DATA => (SELECT * FROM TRAINING_DATA),
PARAMETERS => '{"THREAD_NUMBER":4}',
MODEL => RESULT_MODEL,
STATISTICS => RESULT_STATS
);
INSERT INTO MODEL_TABLE SELECT * FROM :RESULT_MODEL;
END;
APL (Automated Predictive Library)
-- Automated model selection
CALL _SYS_AFL.APL_FORECAST(
INPUT => (SELECT * FROM SALES_DATA),
PARAMETERS => '{
"HORIZON": 12,
"TARGET": "sales_amount",
"DATE_COLUMN": "posting_date",
"GRANULARITY": "MONTH",
"LAST_TRAINING_DATE": "2026-01-01"
}',
RESULT => FORECAST_RESULT
);
SELECT * FROM :FORECAST_RESULT;
HANA ML Python Client
from hana_ml import dataframe
from hana_ml.algorithms.pal import linear_regression
# Connect
conn = dataframe.ConnectionContext(
address='my-hana.cfapps.us10.hana.ondemand.com',
port=443, user='SYSTEM', password='...', encrypt=True
)
# Load data
df = conn.table('TRAINING_DATA', schema='ZSALES').select(['X1','X2','Y']).collect()
# Train model
lr = linear_regression.LinearRegression()
lr.fit(data=df, key='ID', features=['X1','X2'], label='Y')
# Predict
predictions = lr.predict(data=conn.table('PRODUCTION_DATA'))
conn.create_table(predictions, 'PREDICTIONS')
Integration with ABAP
" Call HANA ML procedure from ABAP via ADBC
DATA(lo_sql) = NEW cl_sql_statement( ).
lo_sql->execute_ddl(
|CALL ZSP_REGRESSION_PREDICT( iv_model_name = 'LREG_001', |
& |iv_input_table = 'Z_NEW_DATA', iv_output_table = 'Z_PREDICTIONS' )|
).
Available Algorithms
| Category |
PAL Algorithms |
APL Algorithms |
| Classification |
SVM, Decision Tree, Random Forest, Naive Bayes |
Automated classifier |
| Regression |
Linear, Polynomial, GLM, Exponential |
Automated regression |
| Clustering |
K-Means, DBSCAN, Agglomerative |
— |
| Time Series |
ARIMA, Exponential Smoothing, Croston |
Automated forecasting |
| Association |
Apriori, FP-Growth |
— |
| Recommendation |
ALS (Collaborative Filtering) |
— |
Gotchas
- PAL requires
_SYS_AFL schema privilege
- Model training is CPU-intensive — use dedicated HANA worker threads
- Python client needs
hana_ml pip package + HANA ODBC driver
- APL auto-selects best algorithm — use PAL when you need specific algorithm control
1---2name: sap-hana-ml3description: SAP HANA Machine Learning — PAL (Predictive Analysis Library), APL (Automated Predictive Library), HANA ML Python client, built-in ML algorithms (regression, classification, clustering, time series), model training in HANA, model consumption from ABAP/CAP. Use when implementing HANA ML models, calling PAL/APL procedures, or integrating ML into ABAP applications.4---56# SAP HANA Machine Learning78In-database machine learning with PAL and APL libraries.910## PAL (Predictive Analysis Library)1112```sql13-- Train a linear regression model14CREATE PROCEDURE TRAIN_REGRESSION()15LANGUAGE SQLSCRIPT AS16BEGIN17 CALL _SYS_AFL.PAL_LINEAR_REGRESSION(18 TRAIN_DATA => (SELECT * FROM TRAINING_DATA),19 PARAMETERS => '{"THREAD_NUMBER":4}',20 MODEL => RESULT_MODEL,21 STATISTICS => RESULT_STATS22 );23 INSERT INTO MODEL_TABLE SELECT * FROM :RESULT_MODEL;24END;25```2627## APL (Automated Predictive Library)2829```sql30-- Automated model selection31CALL _SYS_AFL.APL_FORECAST(32 INPUT => (SELECT * FROM SALES_DATA),33 PARAMETERS => '{34 "HORIZON": 12,35 "TARGET": "sales_amount",36 "DATE_COLUMN": "posting_date",37 "GRANULARITY": "MONTH",38 "LAST_TRAINING_DATE": "2026-01-01"39 }',40 RESULT => FORECAST_RESULT41);42SELECT * FROM :FORECAST_RESULT;43```4445## HANA ML Python Client4647```python48from hana_ml import dataframe49from hana_ml.algorithms.pal import linear_regression5051# Connect52conn = dataframe.ConnectionContext(53 address='my-hana.cfapps.us10.hana.ondemand.com',54 port=443, user='SYSTEM', password='...', encrypt=True55)5657# Load data58df = conn.table('TRAINING_DATA', schema='ZSALES').select(['X1','X2','Y']).collect()5960# Train model61lr = linear_regression.LinearRegression()62lr.fit(data=df, key='ID', features=['X1','X2'], label='Y')6364# Predict65predictions = lr.predict(data=conn.table('PRODUCTION_DATA'))66conn.create_table(predictions, 'PREDICTIONS')67```6869## Integration with ABAP7071```abap72" Call HANA ML procedure from ABAP via ADBC73DATA(lo_sql) = NEW cl_sql_statement( ).74lo_sql->execute_ddl(75 |CALL ZSP_REGRESSION_PREDICT( iv_model_name = 'LREG_001', |76 & |iv_input_table = 'Z_NEW_DATA', iv_output_table = 'Z_PREDICTIONS' )|77).78```7980## Available Algorithms8182| Category | PAL Algorithms | APL Algorithms |83|---|---|---|84| Classification | SVM, Decision Tree, Random Forest, Naive Bayes | Automated classifier |85| Regression | Linear, Polynomial, GLM, Exponential | Automated regression |86| Clustering | K-Means, DBSCAN, Agglomerative | — |87| Time Series | ARIMA, Exponential Smoothing, Croston | Automated forecasting |88| Association | Apriori, FP-Growth | — |89| Recommendation | ALS (Collaborative Filtering) | — |9091## Gotchas92- PAL requires `_SYS_AFL` schema privilege93- Model training is CPU-intensive — use dedicated HANA worker threads94- Python client needs `hana_ml` pip package + HANA ODBC driver95- APL auto-selects best algorithm — use PAL when you need specific algorithm control