You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@madlib.apache.org by "Peterson, Eric" <er...@intel.com> on 2017/01/10 18:38:17 UTC
MADlib/k-means with CTEs?
Hello,
I'm wondering if it's possible to seed a k-means operation with data generated from a previous common table expression.
Using the example data/tables here: http://madlib.incubator.apache.org/docs/latest/group__grp__kmeans.html#examples , I am able to successfully generate a result.
SELECT data.*, (madlib.closest_column(centroids, points)).column_id as cluster_id
FROM public.km_sample as data,
(SELECT centroids
FROM madlib.kmeanspp('km_sample', 'points', 2,
'madlib.squared_dist_norm2',
'madlib.avg', 20, 0.001)) as centroids
ORDER BY data.pid;
=======================
pid
points
cluster_id
1
{14.23,1.71,2.43,15.6,127,2.8,3.06,0.28,2.29,5.64,1.04,3.92,1065}
0
2
{13.2,1.78,2.14,11.2,1,2.65,2.76,0.26,1.28,4.38,1.05,3.49,1050}
0
3
{13.16,2.36,2.67,18.6,101,2.8,3.24,0.3,2.81,5.6799,1.03,3.17,1185}
0
4
{14.37,1.95,2.5,16.8,113,3.85,3.49,0.24,2.18,7.8,0.86,3.45,1480}
0
5
{13.24,2.59,2.87,21,118,2.8,2.69,0.39,1.82,4.32,1.04,2.93,735}
1
6
{14.2,1.76,2.45,15.2,112,3.27,3.39,0.34,1.97,6.75,1.05,2.85,1450}
0
7
{14.39,1.87,2.45,14.6,96,2.5,2.52,0.3,1.98,5.25,1.02,3.58,1290}
0
8
{14.06,2.15,2.61,17.6,121,2.6,2.51,0.31,1.25,5.05,1.06,3.58,1295}
0
9
{14.83,1.64,2.17,14,97,2.8,2.98,0.29,1.98,5.2,1.08,2.85,1045}
0
10
{13.86,1.35,2.27,16,98,2.98,3.15,0.22,1.85,7.2199,1.01,3.55,1045}
0
When I attempt to pass the sample data to the k-means query via a CTE, the planner has no issue with syntax, but the query fails.
with cte_test as (
select * from km_sample)
SELECT data.*, (madlib.closest_column(centroids, points)).column_id as cluster_id
FROM cte_test as data,
(SELECT centroids
FROM madlib.kmeanspp('cte_test', 'points', 2,
'madlib.squared_dist_norm2',
'madlib.avg', 20, 0.001)) as centroids
ORDER BY data.pid;
=============================
QUERY PLAN
Gather Motion 120:1 (slice2; segments: 120) (cost=21.06..21.07 rows=4 width=68)
Merge Key: pid
-> Sort (cost=21.06..21.07 rows=1 width=68)
Sort Key: data.pid
-> Nested Loop (cost=1.37..21.02 rows=1 width=68)
-> Seq Scan on km_sample (cost=0.00..10.04 rows=1 width=129)
-> Materialize (cost=1.37..2.57 rows=1 width=32)
-> Broadcast Motion 1:120 (slice1; segments: 1) (cost=0.01..1.25 rows=120 width=32)
-> Subquery Scan kmeanspp (cost=0.01..0.03 rows=1 width=32)
-> Result (cost=0.01..0.02 rows=1 width=0)
InitPlan (slice3)
-> Result (cost=0.00..0.01 rows=1 width=0)
Optimizer status: legacy query optimizer
=============================
SQL error:
ERROR: plpy.Error: kmeans error: Data table does not exist! (plpython.c:4648)
CONTEXT: Traceback (most recent call last):
PL/Python function "__kmeans_validate_src", line 23, in <module>
return kmeans.kmeans_validate_src(**globals())
PL/Python function "__kmeans_validate_src", line 30, in kmeans_validate_src
PL/Python function "__kmeans_validate_src"
SQL statement "SELECT madlib.__kmeans_validate_src( $1 )"
PL/pgSQL function "kmeanspp_seeding" line 22 at perform
SQL statement "SELECT madlib.kmeans( $1 , $2 , madlib.kmeanspp_seeding( $1 , $2 , $3 , $4 , NULL, $5 ), $4 , $6 , $7 , $8 )"
PL/pgSQL function "kmeanspp" line 4 at assignment
SQL statement "SELECT madlib.kmeanspp( $1 , $2 , $3 , $4 , $5 , $6 , $7 , 1.0::DOUBLE PRECISION)"
PL/pgSQL function "kmeanspp" line 4 at assignment
Is this an unsupported method, or is there something I need to do differently?
Thanks in advance.
-Eric
Re: MADlib/k-means with CTEs?
Posted by Frank McQuillan <fm...@pivotal.io>.
Thanks Eric for bring this up and thanks Nandish for the investigation.
I opened a JIRA
https://issues.apache.org/jira/browse/MADLIB-1054
to address this for the next release v1.10
Frank
On Tue, Jan 10, 2017 at 3:26 PM, Nandish Jayaram <nj...@pivotal.io>
wrote:
> Hi,
>
> I don't think k-means in MADlib supports CTE for the "rel_source"
> parameter at the moment. The validation check in the code tries to see if
> "rel_source" is an existing table in either some specified schema or
> current_schemas(). If not, it throws the error you saw.
> For instance, the following is the actual query that checks if the
> "rel_source" parameter (cte_test in this example) is a table or not:
> ```
> SELECT EXISTS(
> SELECT 1
> FROM pg_class, pg_namespace
> WHERE relnamespace=pg_namespace.oid AND
> nspname IN ('pg_catalog','public') AND
> relname='cte_test' AND
> relkind IN ('r', 'v', 'm', 't', 'f')
> ) as table_exists;
> ```
>
> NJ
>
> On Tue, Jan 10, 2017 at 10:38 AM, Peterson, Eric <er...@intel.com>
> wrote:
>
>> Hello,
>>
>>
>>
>> I’m wondering if it’s possible to seed a k-means operation with data
>> generated from a previous common table expression.
>>
>>
>>
>> Using the example data/tables here: http://madlib.incubator.apache
>> .org/docs/latest/group__grp__kmeans.html#examples , I am able to
>> successfully generate a result.
>>
>>
>>
>> SELECT data.*, (madlib.closest_column(centroids, points)).column_id as
>> cluster_id
>>
>> FROM public.km_sample as data,
>>
>> (SELECT centroids
>>
>> FROM madlib.kmeanspp('km_sample', 'points', 2,
>>
>> 'madlib.squared_dist_norm2',
>>
>> 'madlib.avg', 20, 0.001)) as centroids
>>
>> ORDER BY data.pid;
>>
>>
>>
>> =======================
>>
>>
>>
>> *pid*
>>
>> *points*
>>
>> *cluster_id*
>>
>> 1
>>
>> {14.23,1.71,2.43,15.6,127,2.8,3.06,0.28,2.29,5.64,1.04,3.92,1065}
>>
>> 0
>>
>> 2
>>
>> {13.2,1.78,2.14,11.2,1,2.65,2.76,0.26,1.28,4.38,1.05,3.49,1050}
>>
>> 0
>>
>> 3
>>
>> {13.16,2.36,2.67,18.6,101,2.8,3.24,0.3,2.81,5.6799,1.03,3.17,1185}
>>
>> 0
>>
>> 4
>>
>> {14.37,1.95,2.5,16.8,113,3.85,3.49,0.24,2.18,7.8,0.86,3.45,1480}
>>
>> 0
>>
>> 5
>>
>> {13.24,2.59,2.87,21,118,2.8,2.69,0.39,1.82,4.32,1.04,2.93,735}
>>
>> 1
>>
>> 6
>>
>> {14.2,1.76,2.45,15.2,112,3.27,3.39,0.34,1.97,6.75,1.05,2.85,1450}
>>
>> 0
>>
>> 7
>>
>> {14.39,1.87,2.45,14.6,96,2.5,2.52,0.3,1.98,5.25,1.02,3.58,1290}
>>
>> 0
>>
>> 8
>>
>> {14.06,2.15,2.61,17.6,121,2.6,2.51,0.31,1.25,5.05,1.06,3.58,1295}
>>
>> 0
>>
>> 9
>>
>> {14.83,1.64,2.17,14,97,2.8,2.98,0.29,1.98,5.2,1.08,2.85,1045}
>>
>> 0
>>
>> 10
>>
>> {13.86,1.35,2.27,16,98,2.98,3.15,0.22,1.85,7.2199,1.01,3.55,1045}
>>
>> 0
>>
>>
>>
>>
>>
>> When I attempt to pass the sample data to the k-means query via a CTE,
>> the planner has no issue with syntax, but the query fails.
>>
>>
>>
>> with cte_test as (
>>
>> select * from km_sample)
>>
>>
>>
>> SELECT data.*, (madlib.closest_column(centroids, points)).column_id as
>> cluster_id
>>
>> FROM cte_test as data,
>>
>> (SELECT centroids
>>
>> FROM madlib.kmeanspp('cte_test', 'points', 2,
>>
>> 'madlib.squared_dist_norm2',
>>
>> 'madlib.avg', 20, 0.001)) as centroids
>>
>> ORDER BY data.pid;
>>
>>
>>
>>
>>
>> =============================
>>
>>
>>
>> *QUERY PLAN*
>>
>> Gather Motion 120:1 (slice2; segments: 120) (cost=21.06..21.07 rows=4
>> width=68)
>>
>> Merge Key: pid
>>
>> -> Sort (cost=21.06..21.07 rows=1 width=68)
>>
>> Sort Key: data.pid
>>
>> -> Nested Loop (cost=1.37..21.02 rows=1 width=68)
>>
>> -> Seq Scan on km_sample (cost=0.00..10.04 rows=1
>> width=129)
>>
>> -> Materialize (cost=1.37..2.57 rows=1 width=32)
>>
>> -> Broadcast Motion 1:120 (slice1; segments: 1)
>> (cost=0.01..1.25 rows=120 width=32)
>>
>> -> Subquery Scan kmeanspp (cost=0.01..0.03
>> rows=1 width=32)
>>
>> -> Result (cost=0.01..0.02 rows=1
>> width=0)
>>
>> InitPlan (slice3)
>>
>> -> Result (cost=0.00..0.01
>> rows=1 width=0)
>>
>> Optimizer status: legacy query optimizer
>>
>>
>>
>> =============================
>>
>>
>>
>> *SQL error:*
>>
>> ERROR: plpy.Error: kmeans error: Data table does not exist! (plpython.c:4648)
>>
>> CONTEXT: Traceback (most recent call last):
>>
>> PL/Python function "__kmeans_validate_src", line 23, in <module>
>>
>> return kmeans.kmeans_validate_src(**globals())
>>
>> PL/Python function "__kmeans_validate_src", line 30, in kmeans_validate_src
>>
>> PL/Python function "__kmeans_validate_src"
>>
>> SQL statement "SELECT madlib.__kmeans_validate_src( $1 )"
>>
>> PL/pgSQL function "kmeanspp_seeding" line 22 at perform
>>
>> SQL statement "SELECT madlib.kmeans( $1 , $2 , madlib.kmeanspp_seeding( $1 , $2 , $3 , $4 , NULL, $5 ), $4 , $6 , $7 , $8 )"
>>
>> PL/pgSQL function "kmeanspp" line 4 at assignment
>>
>> SQL statement "SELECT madlib.kmeanspp( $1 , $2 , $3 , $4 , $5 , $6 , $7 , 1.0::DOUBLE PRECISION)"
>>
>> PL/pgSQL function "kmeanspp" line 4 at assignment
>>
>>
>>
>>
>>
>>
>>
>> Is this an unsupported method, or is there something I need to do
>> differently?
>>
>>
>>
>> Thanks in advance.
>>
>>
>>
>> -Eric
>>
>>
>>
>
>
Re: MADlib/k-means with CTEs?
Posted by Jim Nasby <Ji...@BlueTreble.com>.
On 1/10/17 3:26 PM, Nandish Jayaram wrote:
> For instance, the following is the actual query that checks if the
> "rel_source" parameter (cte_test in this example) is a table or not:
You don't want to consider a toast table (relkind='t') as an actual
table in this case. The nspname criteria doesn't look right either.
It's probably easiest to just do SELECT relkind =
ANY('{r,v,m,f}'::"char"[] FROM pg_class WHERE oid = '<table
specifier>'::regclass. If <table specifier> isn't a plain number, the
cast to regclass will throw an expection if there's no such relation. If
the relation does exist but isn't a table, view, materialized view or
foreign table the query will return false.
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com
855-TREBLE2 (855-873-2532)
Re: MADlib/k-means with CTEs?
Posted by Nandish Jayaram <nj...@pivotal.io>.
Hi,
I don't think k-means in MADlib supports CTE for the "rel_source" parameter
at the moment. The validation check in the code tries to see if
"rel_source" is an existing table in either some specified schema or
current_schemas(). If not, it throws the error you saw.
For instance, the following is the actual query that checks if the
"rel_source" parameter (cte_test in this example) is a table or not:
```
SELECT EXISTS(
SELECT 1
FROM pg_class, pg_namespace
WHERE relnamespace=pg_namespace.oid AND
nspname IN ('pg_catalog','public') AND
relname='cte_test' AND
relkind IN ('r', 'v', 'm', 't', 'f')
) as table_exists;
```
NJ
On Tue, Jan 10, 2017 at 10:38 AM, Peterson, Eric <er...@intel.com>
wrote:
> Hello,
>
>
>
> I’m wondering if it’s possible to seed a k-means operation with data
> generated from a previous common table expression.
>
>
>
> Using the example data/tables here: http://madlib.incubator.
> apache.org/docs/latest/group__grp__kmeans.html#examples , I am able to
> successfully generate a result.
>
>
>
> SELECT data.*, (madlib.closest_column(centroids, points)).column_id as
> cluster_id
>
> FROM public.km_sample as data,
>
> (SELECT centroids
>
> FROM madlib.kmeanspp('km_sample', 'points', 2,
>
> 'madlib.squared_dist_norm2',
>
> 'madlib.avg', 20, 0.001)) as centroids
>
> ORDER BY data.pid;
>
>
>
> =======================
>
>
>
> *pid*
>
> *points*
>
> *cluster_id*
>
> 1
>
> {14.23,1.71,2.43,15.6,127,2.8,3.06,0.28,2.29,5.64,1.04,3.92,1065}
>
> 0
>
> 2
>
> {13.2,1.78,2.14,11.2,1,2.65,2.76,0.26,1.28,4.38,1.05,3.49,1050}
>
> 0
>
> 3
>
> {13.16,2.36,2.67,18.6,101,2.8,3.24,0.3,2.81,5.6799,1.03,3.17,1185}
>
> 0
>
> 4
>
> {14.37,1.95,2.5,16.8,113,3.85,3.49,0.24,2.18,7.8,0.86,3.45,1480}
>
> 0
>
> 5
>
> {13.24,2.59,2.87,21,118,2.8,2.69,0.39,1.82,4.32,1.04,2.93,735}
>
> 1
>
> 6
>
> {14.2,1.76,2.45,15.2,112,3.27,3.39,0.34,1.97,6.75,1.05,2.85,1450}
>
> 0
>
> 7
>
> {14.39,1.87,2.45,14.6,96,2.5,2.52,0.3,1.98,5.25,1.02,3.58,1290}
>
> 0
>
> 8
>
> {14.06,2.15,2.61,17.6,121,2.6,2.51,0.31,1.25,5.05,1.06,3.58,1295}
>
> 0
>
> 9
>
> {14.83,1.64,2.17,14,97,2.8,2.98,0.29,1.98,5.2,1.08,2.85,1045}
>
> 0
>
> 10
>
> {13.86,1.35,2.27,16,98,2.98,3.15,0.22,1.85,7.2199,1.01,3.55,1045}
>
> 0
>
>
>
>
>
> When I attempt to pass the sample data to the k-means query via a CTE, the
> planner has no issue with syntax, but the query fails.
>
>
>
> with cte_test as (
>
> select * from km_sample)
>
>
>
> SELECT data.*, (madlib.closest_column(centroids, points)).column_id as
> cluster_id
>
> FROM cte_test as data,
>
> (SELECT centroids
>
> FROM madlib.kmeanspp('cte_test', 'points', 2,
>
> 'madlib.squared_dist_norm2',
>
> 'madlib.avg', 20, 0.001)) as centroids
>
> ORDER BY data.pid;
>
>
>
>
>
> =============================
>
>
>
> *QUERY PLAN*
>
> Gather Motion 120:1 (slice2; segments: 120) (cost=21.06..21.07 rows=4
> width=68)
>
> Merge Key: pid
>
> -> Sort (cost=21.06..21.07 rows=1 width=68)
>
> Sort Key: data.pid
>
> -> Nested Loop (cost=1.37..21.02 rows=1 width=68)
>
> -> Seq Scan on km_sample (cost=0.00..10.04 rows=1
> width=129)
>
> -> Materialize (cost=1.37..2.57 rows=1 width=32)
>
> -> Broadcast Motion 1:120 (slice1; segments: 1)
> (cost=0.01..1.25 rows=120 width=32)
>
> -> Subquery Scan kmeanspp (cost=0.01..0.03
> rows=1 width=32)
>
> -> Result (cost=0.01..0.02 rows=1
> width=0)
>
> InitPlan (slice3)
>
> -> Result (cost=0.00..0.01
> rows=1 width=0)
>
> Optimizer status: legacy query optimizer
>
>
>
> =============================
>
>
>
> *SQL error:*
>
> ERROR: plpy.Error: kmeans error: Data table does not exist! (plpython.c:4648)
>
> CONTEXT: Traceback (most recent call last):
>
> PL/Python function "__kmeans_validate_src", line 23, in <module>
>
> return kmeans.kmeans_validate_src(**globals())
>
> PL/Python function "__kmeans_validate_src", line 30, in kmeans_validate_src
>
> PL/Python function "__kmeans_validate_src"
>
> SQL statement "SELECT madlib.__kmeans_validate_src( $1 )"
>
> PL/pgSQL function "kmeanspp_seeding" line 22 at perform
>
> SQL statement "SELECT madlib.kmeans( $1 , $2 , madlib.kmeanspp_seeding( $1 , $2 , $3 , $4 , NULL, $5 ), $4 , $6 , $7 , $8 )"
>
> PL/pgSQL function "kmeanspp" line 4 at assignment
>
> SQL statement "SELECT madlib.kmeanspp( $1 , $2 , $3 , $4 , $5 , $6 , $7 , 1.0::DOUBLE PRECISION)"
>
> PL/pgSQL function "kmeanspp" line 4 at assignment
>
>
>
>
>
>
>
> Is this an unsupported method, or is there something I need to do
> differently?
>
>
>
> Thanks in advance.
>
>
>
> -Eric
>
>
>