Why does YugaByteDB YSQL select query return unexpected bytes when using bytea data type? - minikube

I am trying to store some binary data into yugabytedb on my laptop in minikube, so I used the bytea datatype as the documentation says.
But when it comes to retrieving the binary data back using a select query, the bytes returned are very different from what I expect!
What could be the cause of this?

I got a solution
Here was my code:
/*
===============================
GOLANG CODE REGISTERING A USER:
================================
*/
db := dbconnect()
defer db.Close()
log.Println("Registration Server says: user.Photo = ", user.Photo[:100])
usrInsert := fmt.Sprintf("INSERT INTO users (user_id,
username, Phone, Email, Nationality, Photo, pxwrd)
VALUES ('%s','%s','%s','%s','%s','%x','%s')",
user.user_id, user.username, user.Phone,
user.Email, user.Nationality, user.Photo,
encriptedpassword)
_, err := db.Exec(usrInsert)
if err != nil {
fmt.Println(err.Error());
}
/*
=============================================
OUTPUT OF 'log.Println' ABOVE WAS:
=============================================
Registration Server says: user.Photo = [1 0 0 0 0 0 6
213 194 0 0 0 24 255 216 255 224 0 16 74 70 73 70 0 1 2 1
1 44 1 44 0 0 255 225 16 115 69 120 105 102 0 0 77 77 0 42
0 0 0 8 0 11 1 15 0 2 0 0 0 18 0 0 0 146 1 16 0 2 0 0 0 10
0 0 0 164 1 18 0 3 0 0 0 1 0 1 0 0 1 26 0 5 0 0 0 1 0 0 0]
*/
/*
=============================
GOLANG CODE FOR USER LOGIN:
=============================
Note: the user profile Photo is
saved as 'bytea' data type
*/
//select query for loging in
selecQuery := fmt.Sprintf("SELECT * FROM users WHERE
username='%s' AND pxwrd='%x' ",
uname, encriptedpassword)
queryResult, err := db.Query(selecQuery)
if err != nil {
fmt.Println(err.Error())
}
defer queryResult.Close()
userExists := false
for queryResult.Next() {
userExists = true
var UserX rs_utils.User
var cnt, psw string
err = queryResult.Scan(&cnt, &UserX.User_id,
&UserX.Username, &UserX.Phone, &UserX.Email,
&UserX.Nationality, &UserX.Photo, &psw)
if err != nil {
log.Println("!! Error: ", err)
break
}
log.Println("UserX.Photo, psw :\n", UserX.Photo[:100], psw)
//...
/...
}
/*
==============================
OUTPUT OF 'log.Println' ABOVE:
==============================
UserX.Photo, psw :
[48 49 48 48 48 48 48 48 48 48 48 48 49 53 97 98 48 53 48 48 48
48 48 48 50 97 56 57 53 48 52 101 52 55 48 100 48 97 49 97 48 97
48 48 48 48 48 48 48 100 52 57 52 56 52 52 53 50 48 48 48 48 48
52 54 99 48 48 48 48 48 50 55 102 48 56 48 54 48 48 48 48 48 48
50 100 54 50 52 53 53 51 48 48 48 48 48
48 48 49] 8ae5776067290c4712fa454006c8ec6
*/
As you can see, the retrieved binary data was very different from
what was inserted.
The cause:
usrInsert := fmt.Sprintf("INSERT INTO users (user_id, username,
Phone, Email, Nationality, Photo, pxwrd)
VALUES ('%s','%s','%s','%s','%s','%x','%s')",
user.user_id, user.username, user.Phone,
user.Email, user.Nationality, user.Photo,
encriptedpassword)
_, err := db.Exec(usrInsert)
//AND
selecQuery := fmt.Sprintf("SELECT * FROM users WHERE
username='%s' AND pxwrd='%x' ", uname, encriptedpassword)
queryResult, err := db.Query(selecQuery)
//Notice the string formatting in the queries.
SOLUTION:
I replaced the above lines of code with:
_, err := db.Exec(`INSERT INTO users (user_id, username,
Phone, Email, Nationality, Photo, pxwrd)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
user.user_id, user.username, user.Phone, user.Email,
user.Nationality, user.Photo, encriptedpassword)
//AND
queryResult, err := db.Query(`SELECT * FROM users WHERE
username=$1 AND pxwrd=$2 `, uname, encriptedpassword)
respectively.

Related

Make histogram of pixel intensities without imhist

I have used the unique command to get the unique pixel intensities from my image. Then I tried to make a histogram using them, but it doesn't use all of the intensity values
I = imread('pout.tif');
[rows, columns] = size(I);
UniquePixels=unique(I);
hist=histogram(UniquePixels)
An alternative approach would be to use accumarray combined with unique. I would specifically use the third output of unique to transform your data into a consecutive sequence of 1 up to N where N is the total number of unique intensities, then leverage the first output of unique that will give you the list of unique intensities. Therefore, if the first output of unique is A and the output of accumarray is B, the effect is that at location B(i), this gives the total number of intensities of A(i).
Therefore:
[UniquePixels, ~, id] = unique(I);
histo = accumarray(id, 1);
UniquePixels gives you all unique pixels while histo gives you the counts of each unique pixel corresponding to each element in UniquePixels.
Here's a quick example:
>> I = randi(255, 10, 10)
I =
42 115 28 111 218 107 199 60 140 237
203 22 246 233 159 13 100 91 76 198
80 59 2 47 90 231 62 210 190 125
135 233 198 68 131 241 103 4 49 112
43 39 209 38 103 126 25 11 176 114
154 211 222 35 20 125 34 44 47 79
68 138 22 222 62 87 241 166 94 130
167 255 102 148 32 230 244 187 160 131
176 20 67 141 47 95 147 166 199 209
191 113 205 37 62 29 16 115 21 203
>> [UniquePixels, ~, id] = unique(I);
>> histo = accumarray(id, 1);
>> [UniquePixels histo]
ans =
2 1
4 1
11 1
13 1
16 1
20 2
21 1
22 2
25 1
28 1
29 1
32 1
34 1
35 1
37 1
38 1
39 1
42 1
43 1
44 1
47 3
49 1
59 1
60 1
62 3
67 1
68 2
76 1
79 1
80 1
87 1
90 1
91 1
94 1
95 1
100 1
102 1
103 2
107 1
111 1
112 1
113 1
114 1
115 2
125 2
126 1
130 1
131 2
135 1
138 1
140 1
141 1
147 1
148 1
154 1
159 1
160 1
166 2
167 1
176 2
187 1
190 1
191 1
198 2
199 2
203 2
205 1
209 2
210 1
211 1
218 1
222 2
230 1
231 1
233 2
237 1
241 2
244 1
246 1
255 1
If you double check the input example and the final output, you will see that only the unique pixels are shown combined with their counts. Any bins that were zero in count are not shown.

Matlab find zero value with certain range

I have this matrix:
A =[22 22 142 142 142 92 92 92 0 0
0 109 109 151 151 151 23 23 149 149
0 0 0 152 152 152 38 38 0 0
0 13 13 113 113 113 119 119 119 0
0 8 8 8 84 84 14 14 14 0
0 0 144 144 144 0 0 0 66 66
139 139 139 34 34 34 0 0 0 0
0 0 64 64 64 128 128 59 59 59
83 83 83 65 65 65 67 67 67 0];
How can I find indices (row, column) from matrix with zero value respectively 2 or more?
You can use find as follows:
[r,c] = find(A==0)
[rows,cols] = ind2sub(size(A),find(A==0))
find gives you the indices and ind2sub converts them in column-row format.

MRT function [1] "error code = 0"

When I make my MRT, I got two errors:
[1] "error code = 0" and Error in indval.default(Ynode, clustering =
clustnode, numitr = 1000) : All species must occur in at least one
plot. Does anyone have an idea of why? I checked and all my species
have an abundance >0...
MRTtest=mvpart(vegetation~ Placette+ Tourb + Transect + Largcanal + Annouvert + Elevation + Profnappe + Litiere+ Solnu+ Deblign+ Densiometre+ EpaissMO+ Vonpostvingt+ Vonpostsoixante+ Pyrovingt+ Pyrosoixante+ Sommesurfterr,tot,margin=0.08,cp=0,xv="pick",xval=10,xvmult=150,which=4,pca=F)
X-Val rep : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129 130 131 132 133
134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 149 150
Minimum tree sizes
tabmins
2 3 4 6
2 125 5 18
MRTtest1=MRT(MRTtest,percent=10,species=colnames(vegetation))
summary(MRTtest1)
Portion (%) of deviance explained by species for every particular node
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 1 ---
Complexity(R2) 14.87422
Sommesurfterr>=6.024 Sommesurfterr< 6.024
~ Discriminant species :
THOnmtot THOmtot
% of expl. deviance 17.61057298 38.419650816
Mean on the left 0.37621604 0.430818462
Mean on the right 0.08877576 0.006259911
[1] "error code = 0"
~ INDVAL species for this node: : left is 1, right is 2
cluster indicator_value probability
THOmtot 1 0.9597 0.001
THOnmtot 1 0.7878 0.001
LEG 1 0.5802 0.031
LIB 1 0.5078 0.010
MELnmtot 1 0.4710 0.047
EPNnmtot 1 0.4404 0.026
Sum of probabilities = 87.497
Sum of Indicator Values = 30.02
Sum of Significant Indicator Values = 12.67
Number of Significant Indicators = 29
Significant Indicator Distribution
1 2
8 21
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 2 ---
Complexity(R2) 7.920283
Densiometre< 19.88 Densiometre>=19.88
~ Discriminant species :
TRA THOmtot
% of expl. deviance 10.54536819 27.8848051
Mean on the left 0.02754503 0.5158733
Mean on the right 0.20823621 0.2220475
Error in indval.default(Ynode, clustering = clustnode, numitr = 1000)
: All species must occur in at least one plot

column Calculation if column > 1in T-SQL

hi i am trying to calculate a value of certain columns together and depending if a column is a certain value for instance
if lens.qty > 1 then (CASE LENS.LNS_PROGTYPE --DESIGN pOINTS
WHEN 762
THEN 70
when 767
THEN 70
when 768
THEN 70
WHEN 841
THEN 35
WHEN 842
then 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL'
then 50
when 'HVLLBLUE'
then 100
else 0
end +
CASE LENS.LNS_IDX --MATERIAL POINTS
when 53
THEN 35
WHEN 56
THEN 35
WHEN 58
then 35
when 61
then 35
else 0
END +
CASE LENS.LNS_MATCLR --COLOR POINTS
WHEN 00
THEN 0
WHEN 46
THEN 35
WHEN 47
THEN 35
WHEN 48
then 35
else 0
end as TOTAL_POINTS)*lens.qty / 2
else
CASE LENS.LNS_PROGTYPE --DESIGN pOINTS
WHEN 762
THEN 70
when 767
THEN 70
when 768
THEN 70
WHEN 841
THEN 35
WHEN 842
then 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL'
then 50
when 'HVLLBLUE'
then 100
else 0
end +
CASE LENS.LNS_IDX --MATERIAL POINTS
when 53
THEN 35
WHEN 56
THEN 35
WHEN 58
then 35
when 61
then 35
else 0
END +
CASE LENS.LNS_MATCLR --COLOR POINTS
WHEN 00
THEN 0
WHEN 46
THEN 35
WHEN 47
THEN 35
WHEN 48
then 35
else 0
end as TOTAL_POINTS)
i keep getting syntax error and i am not sure where i am going wrong
i am not sure how to do it and to be honest i don't completely understand the examples i have viewed your help would be greatly appreciated
I would do something like:
(CASE
WHEN LENS.LNS_PROGTYPE IN (762,767,768) THEN 70
WHEN LENS.LNS_PROGTYPE IN (841,842) THEN 35
else 0
end +
case LTRIM(RTRIM(LENS.COATTYP)) --ARC POINTS
when 'HVLL' then 50
when 'HVLLBLUE' then 100
else 0
end +
CASE
WHEN LENS.LNS_IDX IN (53,56,58,61) THEN 35
else 0
END +
CASE
WHEN LENS.LNS_MATCLR IN (46,47,48) THEN 35
else 0
end) * CASE WHEN lens.qty > 1 THEN lens.qty / 2 ELSE 1 END
For the entire expression. But, as I said, I'd also introduce some mapping tables rather than having all of these magic constants in the CASE expressions.
you must make sure, that all elements of that string/sum are of the same datatype. cast/convert them appropriately.

Construct matrix according to the arrangement in another matrix

I have a matrix 'eff_tot' with dimension (m x n) which I want to rearrange according to a matrix called 'matches' (e.g. [n2 n3; n4 n5]) and put all the collumns not specified in 'matches' at the end.
That is, I want to have [eff_tot(:,n2) eff_tot(:,n3) ; eff_tot(:,n4) eff_tot(:,n5) ; eff_tot(:,n1)].
That's all folks!
Taking the example in the first answer, what I would like to have is:
eff_tot =
81 15 45 15 24
44 86 11 14 42
92 63 97 87 5
19 36 1 58 91
27 52 78 55 95
82 41 0 0 0
87 8 0 0 0
9 24 0 0 0
40 13 0 0 0
26 19 0 0 0
Regards.
Create a vector listing the indices of all the columns in eff_tot and then use SETDIFF to determine which columns do not occur in [n2 n3 n4 n5]. These columns are the unmatched ones. Now concatenate the matched and unmatched column indices to create your column-reordered eff_tot matrix.
>> eff_tot = randi(100, 5, 7)
eff_tot =
45 82 81 15 15 41 24
11 87 44 14 86 8 42
97 9 92 87 63 24 5
1 40 19 58 36 13 91
78 26 27 55 52 19 95
>> n2 = 3; n3 = 5; n4 = 2; n5 = 6;
>> missingColumn = setdiff(1:size(eff_tot, 2), [n2 n3 n4 n5])
missingColumn =
1 4 7
>> eff_tot = [eff_tot(:,n2) eff_tot(:,n3) eff_tot(:,missingIndex); eff_tot(:,n4) eff_tot(:,n5) zeros(size(eff_tot, 1), length(missingIndex))];
eff_tot =
81 15 45 15 24
44 86 11 14 42
92 63 97 87 5
19 36 1 58 91
27 52 78 55 95
82 41 0 0 0
87 8 0 0 0
9 24 0 0 0
40 13 0 0 0
26 19 0 0 0